. */ require_once(FILESYSTEMROOTPATH."include/classes/IDao.php"); require_once(FILESYSTEMROOTPATH."jc/include/model/Elections_Bodies.php"); class Elections_BodiesDao implements IDao { /** MySQL object */ private $mysql; /** * Default constructor */ public function __construct(MySQL $mysql) { $this->mysql = $mysql; } /** * Load Elections_Bodies by id * * @param id * @return */ public function load($id) { $query = "SELECT * FROM `elections_bodies` WHERE `id`='".$id."'"; if( $this->mysql->query($query) ) { return new Elections_Bodies($this->mysql->fetchArray()); }else { return array(); } } //if not exist, create empty! public function find($election_id, $bodyCode) { $query = "SELECT * FROM `elections_bodies` WHERE `election_id`='".$election_id."' AND `bodyCode`='".$bodyCode."'"; if( $this->mysql->query($query) ) { $data = $this->mysql->fetchArray(); if ($data == NULL){ $data2=array('id'=>'', 'bodyCode'=>$bodyCode,'election_id'=>$election_id, 'votes'=>0,'votes1'=>0,'votes2'=>0,'votes3'=>0, 'uid1'=>'','uid2'=>'','uid3'=>''); $data2['votes'] = $this->getBodyVotesByBC($bodyCode); $q = 'SELECT `uid` FROM `voting_delegates` WHERE `bodyCode` = "'.$bodyCode.'" AND `status` = "delegate" AND `agora_id`='.$_SESSION['JC_MODULE']['AgoraId']; if (!$this->mysql->query($q)) return ''; $d=0; while($row = $this->mysql->fetchArray()) { $d++; $data2['uid'.$d]=$row['uid']; } $eb = new Elections_Bodies($data2); $this->saveOrUpdate($eb); $eb->setId($this->mysql->getInsertId()); return $eb; } else{ $eb = new Elections_Bodies($data); //on creation, all delegates where iincluded, registered or not. //$this->update_delegate($eb); return $eb; } }else { return ''; } } public function getMyLocal($uid){ $query = 'SELECT * FROM `voting_delegates` WHERE `uid`="'.$uid.'" AND `agora_id` = '.$_SESSION['JC_MODULE']['AgoraId']; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); return $result['bodyCode']; }else { return ''; } } public function getMyVotes(Elections_Bodies $eb, $uid) { $local_votes = $eb->getVotes(); $local_delegates = $this->getLocal_delegates($eb->getBodyCode()); $local_votes_already_cast = $eb->getVotes1()+$eb->getVotes2()+$eb->getVotes3(); $delegate = 0; $other = 2; for($i=1;$i<=3;$i++){ $method = 'getUid'.$i; $method2 = 'getVotes'.$i; if(strtolower($eb->$method())==strtolower($uid)){ $delegate = $i; $delegate_votes_already_cast = $eb->$method2(); }else{ $string ='delegate_'.$other.'_votes_already_cast'; $$string=$eb->$method2(); $other++; } } if($delegate == 0){ echo 'UPDATE DELEGATES'; return 0; } return $this->max_votes_left($local_votes, $local_delegates, $delegate_votes_already_cast, $delegate_2_votes_already_cast, $delegate_3_votes_already_cast); } public function getRank($eb, $uid){ for($i=1;$i<=3;$i++){ $method = 'getUid'.$i; if( strtolower($eb->$method())==strtolower($uid)){ //$rank = $i; return $i; } } return 0; } #### 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 getBodyVotes($uid){ $query = 'SELECT * FROM `voting_delegates` AS `d`, `voting_numberofvotes` AS `b` WHERE d.uid="'.$uid.'" AND b.bodyCode=d.bodyCode AND d.`agora_id`='.$_SESSION['JC_MODULE']['AgoraId'].' AND b.`agora_id`='.$_SESSION['JC_MODULE']['AgoraId']; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); return $result['numberOfVotes']; }else { return ''; } } public function getBodyVotesByBC($bc){ $query = 'SELECT * FROM `voting_numberofvotes` WHERE `bodyCode`="'.$bc.'" AND `agora_id`='.$_SESSION['JC_MODULE']['AgoraId']; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); return $result['numberOfVotes']; }else { return ''; } } //get present delegates public function getLocal_delegates($bodyCode){ $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`=".$_SESSION['JC_MODULE']['AgoraId']; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); return $result['number_of_delegates']; }else { return ''; } } public function increase($id, $rank) { $query = "UPDATE `elections_bodies` SET `votes".$rank."`=`votes".$rank."`+1 WHERE `id`=".$id; return $this->mysql->query($query); } /* private function update_delegate($eb){ $q = 'SELECT `uid` FROM `voting_delegates` WHERE `bodyCode` = "'.$eb->getBodyCode().'" AND registered=1 AND `status` = "delegate"'; if (!$this->mysql->query($q)) return ''; while($row = $this->mysql->fetchArray()) { $found = false; for($i=1;$i<=3;$i++){ $method = 'getUid'.$i; if($eb->$method()==$row['uid']){ $found = true; break; } } //insert not found if(!$found){ for($i=1;$i<=3;$i++){ $method = 'getUid'.$i; if($eb->$method()==''){ $method2 = 'setUid'.$i; $eb->$method2($row['uid']); break; } } } } } */ /** * Save or update Elections_Bodies: * @param object with the data * @return boolean true in case of success, false otherwise */ public function saveOrUpdate(IModel $elections_bodies) { $query=""; $recordID=$elections_bodies->getId(); if($recordID==NULL) { $query.="INSERT INTO `elections_bodies` SET "; } else { $query.="UPDATE `elections_bodies` SET "; } $query .="`election_id` = '".$this->mysql->escape($elections_bodies->getElection_id())."', "; $query .="`bodycode` = '".$this->mysql->escape($elections_bodies->getBodyCode())."', "; $query .="`votes` = '".$this->mysql->escape($elections_bodies->getVotes())."', "; $query .="`uid1` = '".$this->mysql->escape($elections_bodies->getUid1())."', "; $query .="`uid2` = '".$this->mysql->escape($elections_bodies->getUid2())."', "; $query .="`uid3` = '".$this->mysql->escape($elections_bodies->getUid3())."', "; $query .="`votes1` = '".$this->mysql->escape($elections_bodies->getVotes1())."', "; $query .="`votes2` = '".$this->mysql->escape($elections_bodies->getVotes2())."', "; $query .="`votes3` = '".$this->mysql->escape($elections_bodies->getVotes3())."'"; if($recordID!=NULL) { $query.=" where `id` = ".$recordID; } //echo $query; return $this->mysql->query($query); } } ?>