. */ require_once(FILESYSTEMROOTPATH."include/classes/IDao.php"); require_once(FILESYSTEMROOTPATH."jc/include/model/Elections.php"); class ElectionsDao implements IDao { /** MySQL object */ private $mysql; /** * Default constructor */ public function __construct(MySQL $mysql) { $this->mysql = $mysql; } /** * Load Elections by id * * @param id * @return */ public function load($id) { $query = "SELECT * FROM `elections` WHERE `id`='".$id."'"; if( $this->mysql->query($query) ) { return new Elections($this->mysql->fetchArray()); }else { return array(); } } public function loadByAgora($agora_id) { $query = 'SELECT * FROM `elections` WHERE `agora_id`="'.$agora_id.'"'; if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { $row['edit'] = 'Edit'; if (date("Y-m-d H:i:s") > $row['close']){ $row['results'] = 'Results';; } else{ $row['results'] = '-'; } $row['percentage'] = 0; $row['candidates'] = 'Candidates'; $ret[$row['id']] = $row; } foreach ($ret as $id => $row){ $already_cast = $this->votes_cast($id); $votes_available = $this->votes_to_be_cast(); $percentage = sprintf("%.2f",100*$already_cast/$votes_available); $ret[$id]['percentage']=$percentage; } return $ret; }else { return array(); } } public function votes_to_be_cast(){ $aid = $_SESSION['JC_MODULE']['AgoraId']; $query = 'SELECT SUM(numberOfVotes) AS to_be_cast FROM `voting_numberofvotes` WHERE agora_id='.$aid.' AND BodyCode IN (SELECT DISTINCT bodyCode FROM `voting_delegates` WHERE `registered`=1 AND `departed`=0 AND `status`="delegate" AND `agora_id`='.$aid.')'; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return ''; } return $result['to_be_cast']; } public function votes_cast($id){ $aid = $_SESSION['JC_MODULE']['AgoraId']; $query = 'SELECT SUM( votes1 ) AS s1, SUM( votes2 ) AS s2, SUM( votes3 ) AS s3 FROM `elections_bodies` WHERE election_id='.$id; if( $this->mysql->query($query) ) { $result = $this->mysql->fetchArray(); }else { return ''; } return $result['s1'] + $result['s2'] + $result['s3']; } public function loadOpen($agora_id){ $query = 'SELECT *,closemysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { $row['candidates'] = 'Candidates'; $row['vote'] = 'Vote'; if($row['hasClosed']==0){ $row['vote']=''.$row['vote'].''; $row['position'] = ''.$row['position'].''; } $ret[$row['id']] = $row; } return $ret; }else { return array(); } } public function countVotes($id) { $query = "SELECT `candidate_id`, count(*) AS `votes` FROM `ballots` WHERE `election_id`=".$id." GROUP BY `candidate_id` HAVING `candidate_id`>0"; if( $this->mysql->query($query) ) { $res = array(); while( $row = $this->mysql->fetchArray() ) { $res[] = $row; } foreach($res as $candidate){ $q = "UPDATE `candidates2` SET `votes`=".$candidate['votes'].' WHERE `id`='.$candidate['candidate_id']; $this->mysql->query($q); } } } public function loadClosed($agora_id){ $query = 'SELECT * FROM `elections` WHERE `agora_id`="'.$agora_id.'" AND `close` < NOW() AND `published`=1'; if( $this->mysql->query($query) ) { $ret = array(); while( $row = $this->mysql->fetchArray() ) { $row['candidates'] = 'Candidates'; $row['view'] = 'View'; $ret[$row['id']] = $row; } return $ret; }else { return array(); } } 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 ''; } } /** * Save or update Elections: * @param object with the data * @return boolean true in case of success, false otherwise */ public function saveOrUpdate(IModel $election) { $query=""; $recordID=$election->getId(); if($recordID==NULL) { $query.="INSERT INTO `elections` SET "; } else { $query.="UPDATE `elections` SET "; } $query .="`agora_id` = '".$this->mysql->escape($election->getAgora_id())."', "; $query .="`position` = '".$this->mysql->escape($election->getPosition())."', "; $query .="`maxVotes` = '".$this->mysql->escape($election->getMaxVotes())."', "; $query .="`published` = '".$this->mysql->escape($election->getPublished())."', "; $query .="`open` = '".$this->mysql->escape($election->getOpen())."', "; $query .="`close` = '".$this->mysql->escape($election->getClose())."'"; if($recordID!=NULL) { $query.=" where `id` = ".$recordID; } //echo $query; return $this->mysql->query($query); } public function get_last_id(){ return $this->mysql->getInsertId(); } } ?>