.
*/
require_once(FILESYSTEMROOTPATH."include/classes/IDao.php");
require_once(FILESYSTEMROOTPATH."jc/include/model/Votes.php");
class VotesDao implements IDao {
/** MySQL object */
private $mysql;
/**
* Default constructor
*/
public function __construct(MySQL $mysql) {
$this->mysql = $mysql;
}
/**
* Load vote by id
* only for compliance, we should never use it!
* @param id
* @return
*/
public function load($id) {
$query = "SELECT * FROM votes WHERE `id`='".$id."'";
if( $this->mysql->query($query) ) {
return new Votes($this->mysql->fetchArray());
}else {
return array();
}
}
public function loadByPID($pid, $type) {
$query = "SELECT * FROM votes WHERE `proposal_id`='".$pid."' AND `type`='".$type."'";
if( $this->mysql->query($query) ) {
$ret = array();
while($row = $this->mysql->fetchArray())
{
$ret[$row['id']]=$row;
}
return $ret;
}else {
return array();
}
}
/**
* Save or update Agorae:
* create one Agora before opening proposals!
* @param Agorae object with the data
* @return boolean true in case of success, false otherwise
*/
public function saveOrUpdate(IModel $votes) {
$query="";
$recordID=$votes->getId();
if($recordID==NULL)
{
$query.="INSERT INTO `votes` SET ";
}
else
{
$query.="UPDATE `votes` SET ";
}
$query .="`id` = '".$this->mysql->escape($votes->getId())."', ";
$query .="`uid` = '".$this->mysql->escape($votes->getUid())."', ";
$query .="`bodyCode` = '".$this->mysql->escape($votes->getBodyCode())."', ";
$query .="`proposal_id` = '".$this->mysql->escape($votes->getProposal_id())."', ";
$query .="`type` = '".$this->mysql->escape($votes->getType())."', ";
$query .="`vote` = '".$this->mysql->escape($votes->getVote())."', ";
$query .="`submit_date` = '".$this->mysql->escape($votes->getSubmit_date())."'";
if($recordID!=NULL)
{
$query.=" where `id` = ".$recordID;
}
//echo $query;
return $this->mysql->query($query);
}
/*not really belong to any dao..*/
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 get_my_votes($uid, $pid, $type){
$aid = $_SESSION['JC_MODULE']['AgoraId'];
$bodyCode = $_SESSION['JC_MODULE']['BodyCode'];
$local_votes = $this->getBody_numberofvotes();
$local_delegates = $this->getLocal_delegates($bodyCode);
//number of votes the delegate has cast
$query = "SELECT COUNT(1) AS already_cast FROM `votes` WHERE uid='$uid' AND proposal_id=$pid AND `type` ='$type'";
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
$delegate_votes_already_cast = $result['already_cast'];
//get number of votes cast by other delegates
$delegate_2_votes_already_cast = 0;
$delegate_3_votes_already_cast = 0;
if($local_delegates == 2){
$query = "SELECT COUNT(1) AS already_cast FROM `votes` WHERE bodyCode='$bodyCode' AND uid!='$uid' AND proposal_id=$pid AND `type`='$type'";
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(1) AS already_cast FROM `votes` WHERE bodyCode='$bodyCode' AND uid!='$uid' AND proposal_id=$pid AND `type`='$type' GROUP BY uid";
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 ''; }
}
$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, $local_votes_already_cast, $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);
return $final_result;
}
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();
}
}
}
/*not needed anymore*/
public function get_max_votes($uid, $pid, $type){
$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 '';
}
$local_votes = $result['numberOfVotes'];
$local_delegates = $this->getLocal_delegates($result['bodyCode']);
$bodyCode = $result['bodyCode'];
$query = 'SELECT COUNT(1) AS already_cast FROM `votes` WHERE uid="'.$uid.'" AND proposal_id='.$pid.' AND `type` ="'.$type.'"';
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
$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(1) AS already_cast FROM `votes` WHERE bodyCode ="'.$bodyCode.'" AND uid != "'.$uid.'" AND proposal_id='.$pid.' AND `type` ="'.$type.'"';
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(1) AS already_cast FROM `votes` WHERE bodyCode ="'.$bodyCode.'" AND uid != "'.$uid.'" AND proposal_id='.$pid.' AND `type` ="'.$type.'" GROUP BY uid';
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 ''; }
}
/*
$query = 'SELECT count(1) already_cast FROM `votes` WHERE BodyCode="'.$bodyCode.'" AND proposal_id='.$pid.'AND type ="'.$type.'"';
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
$local_votes_already_cast = $result['already_cast'];
*/
$local_votes_already_cast =$delegate_votes_already_cast + $delegate_2_votes_already_cast + $delegate_3_votes_already_cast;
return $this->max_votes_left($local_votes, $local_delegates, $local_votes_already_cast, $delegate_votes_already_cast, $delegate_2_votes_already_cast, $delegate_3_votes_already_cast);
}
/*not needed anymore*/
public function get_min_votes($uid){
$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 '';
}
return $this->min_votes($result['numberOfVotes'],$this->getLocal_delegates($result['bodyCode']));
}
public function getLocal_delegates($bodyCode){
$aid = $_SESSION['JC_MODULE']['AgoraId'];
$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();
return $result['number_of_delegates'];
}else { return ''; }
}
public function getLocal_votes($bodyCode){
$query = 'SELECT * FROM `voting_numberofvotes` WHERE bodyCode="'.$bodyCode.'" AND agora_id='.$_SESSION['JC_MODULE']['AgoraId'];
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
return $result['numberOfVotes'];
}else { return '';
}
}
/*not needed anymore*/
private function min_votes($local_votes, $local_delegates)
{
#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
return ( $local_votes - $modulus ) / $local_delegates;
}
#### 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 delegates the local has
$local_votes_already_cast, #how many votes have been already cast coming from this local
$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 get_registered_bodies($aid){
$query = 'SELECT COUNT(DISTINCT bodyCode) AS `bodies_available` FROM `voting_delegates` WHERE `registered`=1 AND agora_id='.$aid;
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
return $row['bodies_available'];
}else {
return '';
}
}
public function count_votes($pid, $type){
$aid = $_SESSION['JC_MODULE']['AgoraId'];
$query = 'SELECT COUNT(DISTINCT bodyCode) AS bodies_present FROM `votes` WHERE proposal_id='.$pid.' AND `type`="'.$type.'"';
$results=array();
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
$results['bodies_present'] = $row['bodies_present'];
}else { return '';
}
$query = 'SELECT COUNT(DISTINCT bodyCode) AS `bodies_available` FROM `voting_delegates` WHERE `registered`=1 AND agora_id='.$aid;
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
$results['bodies_available'] = $row['bodies_available'];
}else { return '';}
$query = 'SELECT COUNT(1) in_favour FROM `votes` WHERE proposal_id='.$pid.' AND `type`="'.$type.'" AND vote="in_favour"';
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
$results['in_favour'] = $row['in_favour'];
}else { return ''; }
$query = 'SELECT COUNT(1) against FROM `votes` WHERE proposal_id='.$pid.' AND `type`="'.$type.'" AND vote="against"';
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
$results['against'] = $row['against'];
}else { return ''; }
$query = 'SELECT COUNT(1) abstention FROM `votes` WHERE proposal_id='.$pid.' AND `type`="'.$type.'" AND vote="abstention"';
if( $this->mysql->query($query) ) {
$row = $this->mysql->fetchArray();
$results['abstention'] = $row['abstention'];
}else { return ''; }
$results['votes_available'] = $this->votes_to_be_cast();
return $results;
}
/*not needed anymore*/
public function get_cast_votes($uid, $pid, $type){
$query = 'SELECT COUNT(1) AS already_cast FROM `votes` WHERE uid="'.$uid.'" AND proposal_id='.$pid.' AND `type`="'.$type.'"';
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
return $result['already_cast'];
}
/*not needed anymore*/
public function get_local_cast_votes($bodyCode, $pid, $type){
$query = 'SELECT COUNT(1) AS already_cast FROM `votes` WHERE bodyCode="'.$bodyCode.'" AND proposal_id='.$pid.' AND `type`="'.$type.'"';
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
return $result['already_cast'];
}
public function votes_to_be_cast(){
$aid = $_SESSION['JC_MODULE']['AgoraId'];
$query = 'SELECT SUM(numberOfVotes) 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($proposal_id, $type){
$query = "SELECT COUNT(*) AS votes_cast FROM `votes` WHERE `proposal_id`=".$proposal_id." AND `type`='".$type."'";
if( $this->mysql->query($query) ) {
$result = $this->mysql->fetchArray();
}else { return '';
}
return $result['votes_cast'];
}
public function check(){
$types = array('proposal','rollcall');
$q1 = "SELECT DISTINCT proposal_id FROM votes";
$this->mysql->query($q1);
$pids = array();
while( $row = $this->mysql->fetchArray() ) {
$pids[]=$row['proposal_id'];
}
foreach($types as $type){
foreach($pids as $proposal_id){
echo '
'.$type.' '.$proposal_id;
$q = "SELECT b.`bodyCode` , b.`numberOfVotes` , COUNT( v.bodyCode ) AS `count`
FROM votes AS v, voting_numberofvotes AS b
WHERE v.proposal_id =".$proposal_id."
AND `type` ='".$type."' AND v.bodyCode = b.bodyCode AND b.agora_id=".$_SESSION['JC_MODULE']['AgoraId']."
GROUP BY v.`BodyCode`, b.`numberOfVotes`
HAVING count(v.bodyCode) > b.`numberOfVotes`" ;
$this->mysql->query($q);
//echo '
'.$q.'
';
while( $row = $this->mysql->fetchArray() ) {
print_r($row);
}
}
}
}
public function getMyLocalVotes($pid,$type,$bodyCode){
$query = 'SELECT * FROM `votes` WHERE `type`="'.$type.'" AND `proposal_id`='.$pid.' AND `bodyCode`="'.$bodyCode.'"';
$t = array('in_favour'=>'In Favour','against'=>'Against','abstention'=>'Abstention');
$this->mysql->query($query);
$ret = array();
while( $row = $this->mysql->fetchArray() ) {
$row['vote2'] = $t[$row['vote']];
$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;
}
}
}
?>