. */ require_once(FILESYSTEMROOTPATH."include/classes/IDao.php"); require_once(FILESYSTEMROOTPATH."jc/include/model/PollBallots.php"); class PollBallotsDao implements IDao { /** MySQL object */ private $mysql; /** * Default constructor */ public function __construct(MySQL $mysql) { $this->mysql = $mysql; } /** * Load PollBallots by id * * @param id * @return */ public function load($id) { $query = "SELECT * FROM `pollBallots` WHERE `id`='".$id."'"; if( $this->mysql->query($query) ) { return new PollBallots($this->mysql->fetchArray()); }else { return array(); } } public function loadByTicket($ticket) { $query = 'SELECT * FROM `pollBallots` 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($poll_id, $body, $delegate) { $query = "INSERT INTO `pollBallots` SET `poll_id`=$poll_id, `ticket` = 0, `pollOption_id`=-1, `body`='$body', `delegate`='$delegate'"; $this->mysql->query($query); $id = $this->mysql->getInsertId(); //$aid = $_SESSION['JC_MODULE']['AgoraId'] % 100; /* we don't need to 'encrypt' the ticket $seed = $id; srand($seed); $checksum = rand()%10000; $ticket = $id*10000+$checksum; $ticket = sprintf('%012d', $ticket); $ticket = substr($ticket,0,4).'-'.substr($ticket,4,4).'-'.substr($ticket,8,4);*/ $ticket = $id; //we don't delete it, in order to store ticket even for empty pollBallot $query2 = "UPDATE `pollBallots` set `ticket`='".$ticket."' WHERE `id`=".$id; $this->mysql->query($query2); return $ticket; } public function loadByPoll($pid){ $query = 'SELECT * FROM `pollBallots` WHERE `poll_id`='.$pid.' ORDER BY `ticket`,`pollOption_id`'; if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { $ret[] = $row; } return $ret; }else { return array(); } } //returns max votes left public function getMyVotes($uid, $poll_id) { $aid = $_SESSION['JC_MODULE']['AgoraId']; $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(); }else { return array();} $local_votes = $result['numberOfVotes']; $bodyCode = $result['bodyCode']; $query = 'SELECT `BodyName` FROM ab.`bodies` WHERE `BodyCode`="'.$bodyCode.'"'; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return array();} $bodyName = $result['BodyName']; //get present delegates $query = "SELECT count(1) number_of_delegates FROM `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 array();} $local_delegates = $result['number_of_delegates']; //votes delegate has cast $query = 'SELECT COUNT(DISTINCT ticket) AS already_cast FROM `pollBallots` WHERE delegate="'.$uid.'" AND poll_id='.$poll_id; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return array();} $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 `pollBallots` WHERE body ="'.$bodyCode.'" AND delegate != "'.$uid.'" AND poll_id='.$poll_id; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); $delegate_2_votes_already_cast = $result['already_cast']; }else { return ''; } }elseif($local_delegates == 3){ $query = 'SELECT COUNT(DISTINCT ticket) AS already_cast FROM `pollBallots` WHERE body ="'.$bodyCode.'" AND delegate != "'.$uid.'" AND poll_id='.$poll_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 array(); } } $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, 'bodyName' => $bodyName); 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 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(); }else { return array(); } return $result['numberOfVotes']; } public function getLocal_Ballots($pid,$bodyCode){ $query = 'SELECT `id`,`pollOption` FROM `pollOptions` WHERE `poll_id`='.$pid; $options = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { $options[$row['id']] = $row['pollOption']; } }else{ return array(); } $options[0] = ''; $options[-1] = ''; $query = 'SELECT ticket,COUNT(ticket) AS tc FROM `pollBallots` WHERE `poll_id`='.$pid.' AND `body`="'.$bodyCode.'" GROUP BY ticket'; $tc = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { $tc[$row['ticket']] = $row['tc']; } } $query = 'SELECT * FROM `pollBallots` WHERE `poll_id`='.$pid.' AND `body`="'.$bodyCode.'" ORDER BY ticket, pollOption_id'; $ret = array(); if($this->mysql->query($query)){ while( $row = $this->mysql->fetchArray() ) { if($row['pollOption_id']==-1 && $tc[$row['ticket']]>1){ //do not show ticket, unless empty ballot continue; } $row['pollOption'] = $options[$row['pollOption_id']]; $ret[] = $row; } } //remove -1 if not blank 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; } } /** * Save or update PollBallots: * @param object with the data * @return boolean true in case of success, false otherwise */ public function saveOrUpdate(IModel $pollBallot) { $query=""; $recordID=$pollBallot->getId(); if($recordID==NULL) { $query.="INSERT INTO `pollBallots` SET "; } else { $query.="UPDATE `pollBallots` SET "; } $query .="`pollOption_id` = '".$this->mysql->escape($pollBallot->getPollOption_id())."', "; $query .="`poll_id` = '".$this->mysql->escape($pollBallot->getPoll_id())."', "; $query .="`body` = '".$this->mysql->escape($pollBallot->getBody())."', "; $query .="`delegate` = '".$this->mysql->escape($pollBallot->getDelegate())."', "; $query .="`ticket` = '".$this->mysql->escape($pollBallot->getTicket())."'"; if($recordID!=NULL) { $query.=" WHERE `id` = ".$recordID; } //echo $query; return $this->mysql->query($query); } } ?>