. */ require_once(FILESYSTEMROOTPATH."include/classes/IDao.php"); require_once(FILESYSTEMROOTPATH."jc/include/model/RankedVoteBallots.php"); class RankedVoteBallotsDao implements IDao { /** MySQL object */ private $mysql; /** * Default constructor */ public function __construct(MySQL $mysql) { $this->mysql = $mysql; } /** * Load RankedVoteBallots by id * * @param id * @return */ public function load($id) { $query = "SELECT * FROM `rankedVoteBallots` WHERE `id`='".$id."'"; if( $this->mysql->query($query) ) { return new RankedVoteBallots($this->mysql->fetchArray()); }else { return array(); } } public function loadByTicket($ticket) { $query = 'SELECT * FROM `rankedVoteBallots` WHERE `ticket`="'.$ticket.'"'; if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { $ret[] = $row; } return $ret; }else { return array(); } } public function getTicket($rankedVote_id, $bodyCode, $delegate) { $query = "INSERT INTO `rankedVoteBallots` SET `rankedVote_id`=$rankedVote_id, `ticket` = 0, `rankedVoteOption_id`=-1, `bodyCode`='$bodyCode', `delegate`='$delegate', `ranking`=0"; $this->mysql->query($query); $ticket = $this->mysql->getInsertId(); //public voting, we don't need to 'encrypt' the ticket $query2 = "UPDATE `rankedVoteBallots` set `ticket`='$ticket' WHERE `id`=$ticket"; $this->mysql->query($query2); return $ticket; } public function loadByRankedVote($pid){ $query = 'SELECT COUNT(*) as `N` FROM `rankedVoteOptions` WHERE `rankedVote_id`='.$pid; if($this->mysql->query($query)){ $row = $this->mysql->fetchArray(); $N = $row['N']; }else{ return array(); } $query = 'SELECT * FROM `rankedVoteBallots` WHERE `rankedVote_id`='.$pid.' ORDER BY `ticket`,`rankedVoteOption_id`'; //order by ranking? if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { if ($row['ranking']==($N+1)){ $row['ranking'] = '-'; } $ret[] = $row; } return $ret; }else { return array(); } } //returns max votes left public function getMyVotes($uid, $rankedVote_id) { $aid = $_SESSION['JC_MODULE']['AgoraId']; $local_votes = $this->getBody_numberofvotes(); $bodyCode = $_SESSION['JC_MODULE']['BodyCode']; $err = array(); //get present delegates $query = "SELECT count(1) AS `number_of_delegates` FROM `statutoryvote`.`voting_delegates` WHERE `registered`=1 AND `departed`=0 AND `status` = 'delegate' AND `bodyCode` ='".$bodyCode."' AND `agora_id`=".$aid; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return $err["error"]='Error 3: cannot load delegates';} $local_delegates = $result['number_of_delegates']; //votes delegate has cast $query = 'SELECT COUNT(DISTINCT `ticket`) AS `already_cast` FROM `rankedVoteBallots` WHERE `delegate`="'.$uid.'" AND `rankedVote_id`='.$rankedVote_id; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return $err["error"]='Error 4: cannot load votes';} $delegate_votes_already_cast = $result['already_cast']; //get other delegates $delegate_2_votes_already_cast = 0; $delegate_3_votes_already_cast = 0; if($local_delegates == 2){ $query = 'SELECT COUNT(DISTINCT `ticket`) AS `already_cast` FROM `rankedVoteBallots` WHERE `bodyCode`="'.$bodyCode.'" AND `delegate`!= "'.$uid.'" AND `rankedVote_id`='.$rankedVote_id; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); $delegate_2_votes_already_cast = $result['already_cast']; }else { return $err["error"]='Error 4: cannot load delegates'; } }elseif($local_delegates == 3){ $query = 'SELECT COUNT(DISTINCT `ticket`) AS `already_cast` FROM `rankedVoteBallots` WHERE `bodyCode`="'.$bodyCode.'" AND `delegate`!= "'.$uid.'" AND `rankedVote_id`='.$rankedVote_id.' GROUP BY `delegate`'; if( $this->mysql->query($query) ) { $row = $this->mysql->fetchArray(); $delegate_2_votes_already_cast = $row['already_cast']; $row = $this->mysql->fetchArray(); $delegate_3_votes_already_cast = $row['already_cast']; }else { return $err["error"]='Error 6: cannot load delegates'; } } $local_votes_already_cast =$delegate_votes_already_cast + $delegate_2_votes_already_cast + $delegate_3_votes_already_cast; $my_votes = $this->max_votes_left($local_votes, $local_delegates, $delegate_votes_already_cast, $delegate_2_votes_already_cast, $delegate_3_votes_already_cast); $final_result = array('my_votes' => $my_votes, 'votes_cast' => $delegate_votes_already_cast, 'local_votes' => $local_votes, 'local_votes_cast' => $local_votes_already_cast, 'local_delegates' => $local_delegates, 'bodyCode' => $bodyCode); return $final_result; } #### This function calculates how many votes a delegate can still cast. #### It implements the CIA requirements set by the Agora Working Format, 7. Decisions and ammendments, (2) #### 2The votes have to be divided equally among the delegates. #### 3The difference of the votes between the delegates of the same body cannot exceed one vote. #### 4It is the body that decides upon the division of votes. private function max_votes_left ( $local_votes, #how many votes a local has in total $local_delegates, #how many registered delegates the local has $delegate_votes_already_cast, #how many votes have been already cast coming from this delegate (this is the delegate we are calculating for) $delegate_2_votes_already_cast, #how many votes have been already cast coming from delegate number two (if there is no 2nd delegate, give 0) $delegate_3_votes_already_cast #how many votes have been already cast coming from delegate number three (if there is no 3rd delegate, give 0) ) { #The modulus can be 0, 1 or 2. $modulus = $local_votes % $local_delegates; #Calculates the minimum amount of votes that all delegates of that local have (regardless of the modulus) #eg, in the case of 7 votes and 3 delegates, the variable will get the value 2 $delegate_min_votes = ( $local_votes - $modulus ) / $local_delegates; #Calculates how many votes the current delegate can still cast $delegate_votes_can_cast = $delegate_min_votes - $delegate_votes_already_cast; #Now, we will check if the delegate can have one more vote than $delegate_min_votes #If the modulus is 0, the votes can equally be split, so all delegates have the same amount of votes, so nothing to check #If the modulus is 1, that means that one delegate can have one more vote than $delegate_min_votes. ##If none of the other two delegates did not use that extra vote already, then $delegate_votes_can_cast can use it. if ( $modulus == 1 ){ if ( ( $delegate_2_votes_already_cast <= $delegate_min_votes ) && ( $delegate_3_votes_already_cast <= $delegate_min_votes ) ) { $delegate_votes_can_cast++; } #If the modulus is 2, that means that two delegates can have one more vote than $delegate_min_votes. ##If one of the other two delegates did not use that extra vote already, then $delegate_votes_can_cast can use it. } else if ( $modulus == 2 ){ if ( ( $delegate_2_votes_already_cast <= $delegate_min_votes ) || ( $delegate_3_votes_already_cast <= $delegate_min_votes ) ) { $delegate_votes_can_cast++; } } #to avoid eg registration after other delegates have voted if ($local_votes <= $delegate_2_votes_already_cast + $delegate_3_votes_already_cast + $delegate_votes_already_cast){ $delegate_votes_can_cast = 0; } #return the amount of votes the delegate can still cast return $delegate_votes_can_cast; } public function getBody_numberofvotes(){ if (isset($_SESSION['JC_MODULE']['body_numberofvotes'])){ return $_SESSION['JC_MODULE']['body_numberofvotes']; }else{ $aid = $_SESSION['JC_MODULE']['AgoraId']; $bodyCode = $_SESSION['JC_MODULE']['BodyCode']; $query = "SELECT * FROM `voting_numberofvotes` WHERE bodyCode='$bodyCode' AND agora_id=".$aid; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); $numberofvotes = $result['numberOfVotes']; if(!$numberofvotes){ $numberofvotes = 0; } $_SESSION['JC_MODULE']['body_numberofvotes'] = $numberofvotes; return $numberofvotes; }else { return array(); } } } public function getLocal_numberofvotes($bodyCode){ $aid = $_SESSION['JC_MODULE']['AgoraId']; $uid = $_SESSION['sess_uid']; $query = 'SELECT * FROM `voting_delegates` AS `d`, `voting_numberofvotes` AS `b` WHERE d.uid="'.$uid.'" AND b.bodyCode=d.bodyCode AND b.agora_id='.$aid.' AND d.agora_id='.$aid; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); return $result['numberOfVotes']; }else { return array(); } } public function getLocal_Ballots($pid, $bodyCode){ $query = 'SELECT `id`,`rankedVoteOption` FROM `rankedVoteOptions` WHERE `rankedVote_id`='.$pid; $options = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { $options[$row['id']] = $row['rankedVoteOption']; } }else{ return array(); } //$options[0] = ''; //Display not ranked? //$options[-1] = ''; $N = count($options); $query = 'SELECT `ticket`, COUNT(`ticket`) AS `tc` FROM `rankedVoteBallots` WHERE `rankedVote_id`='.$pid.' AND `bodyCode`="'.$bodyCode.'" GROUP BY `ticket`'; $tc = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { $tc[$row['ticket']] = $row['tc']; } } $query = 'SELECT * FROM `rankedVoteBallots` WHERE `rankedVote_id`='.$pid.' AND `bodyCode`="'.$bodyCode.'" ORDER BY ticket, ranking'; $ret = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { if($row['rankedVoteOption_id']==-1 && $tc[$row['ticket']]>1){ //do not show ticket, unless empty ballot continue; } if ($row['ranking']==($N+1)){ $row['ranking'] = '-'; } $row['rankedVoteOption'] = $options[$row['rankedVoteOption_id']]; $ret[] = $row; } } return $ret; } public function isRegistered($uid){ $query = 'SELECT * FROM `voting_delegates` WHERE `uid`="'.$uid.'" AND `agora_id`='.$_SESSION['JC_MODULE']['AgoraId']; $this->mysql->query($query); $row = $this->mysql->fetchArray(); if($row['registered']==1 AND $row['departed']==0 AND $row['status']=='delegate'){ return true; }else{ return false; } } public function saveMultiple($rankedVoteBallots) { $query=""; $query.="INSERT INTO `rankedVoteBallots` (`rankedVoteOption_id`, `rankedVote_id`, `ranking`, `bodyCode`, `delegate`, `ticket`) VALUES "; $is_first = true; foreach($rankedVoteBallots as $rankedVoteBallot){ if($is_first){ $is_first = false; }else{ $query .= ', '; } $query .="('".$this->mysql->escape($rankedVoteBallot->getRankedVoteOption_id())."', "; $query .="'".$this->mysql->escape($rankedVoteBallot->getRankedVote_id())."', "; $query .="'".$this->mysql->escape($rankedVoteBallot->getRanking())."', "; $query .="'".$this->mysql->escape($rankedVoteBallot->getBodyCode())."', "; $query .="'".$this->mysql->escape($rankedVoteBallot->getDelegate())."', "; $query .="'".$this->mysql->escape($rankedVoteBallot->getTicket())."')"; } return $this->mysql->query($query); } /** * Save or update RankedVoteBallots: * @param object with the data * @return boolean true in case of success, false otherwise */ public function saveOrUpdate(IModel $rankedVoteBallot) { $query=""; $recordID=$rankedVoteBallot->getId(); if($recordID==NULL) { $query.="INSERT INTO `rankedVoteBallots` SET "; } else { $query.="UPDATE `rankedVoteBallots` SET "; } $query .="`rankedVoteOption_id` = '".$this->mysql->escape($rankedVoteBallot->getRankedVoteOption_id())."', "; $query .="`rankedVote_id` = '".$this->mysql->escape($rankedVoteBallot->getRankedVote_id())."', "; $query .="`ranking` = '".$this->mysql->escape($rankedVoteBallot->getRanking())."', "; $query .="`bodyCode` = '".$this->mysql->escape($rankedVoteBallot->getBodyCode())."', "; $query .="`delegate` = '".$this->mysql->escape($rankedVoteBallot->getDelegate())."', "; $query .="`ticket` = '".$this->mysql->escape($rankedVoteBallot->getTicket())."'"; if($recordID!=NULL) { $query.=" WHERE `id` = ".$recordID; } return $this->mysql->query($query); } } ?>