.
*/
include("./include/include.php");
require_once(FILESYSTEMROOTPATH."jc/include/classes/DefaultLayout.php");
require_once(FILESYSTEMROOTPATH."jc/include/classes/MyCheckboxField.php");
require_once(FILESYSTEMROOTPATH."jc/include/classes/AbstractForm3.php");
/**
* Helper classes
*/
class BallotForm extends AbstractForm3 {
public $maxVotes;
public function __construct(Layout $layout, IModel $model, $maxVotes) {
parent::__construct($layout, $model,'form', array('Vote'),'votes_check(\'form\',\'candidate_id[]\')');
$this->$maxVotes = $maxVotes;
}
public function save(IModel $model) {
$election_id = $_POST['election_id'];
//if Javascript disabled:
if (isset($_POST['candidate_id']) AND (count($_POST['candidate_id']) > $_POST['maxVotes']))
{
$this->layout->echoln('
Your vote has not been saved: You can cast up to '.$_POST['maxVotes'].' votes!
');
$this->layout->echoln('Go back and try again!
');
return;
}
$ebService = $GLOBALS['ClassFactory']->getElections_BodiesService();
$eb = $ebService->find($election_id,$_POST['bodyCode']);
$rank = $ebService->getRank($eb,$_SESSION['sess_uid']);
$votesLeft = $ebService->getMyVotes($eb,$_SESSION['sess_uid']);
//delegate has votes?
if ($votesLeft > 0){
$ebService->increase($eb->getId(), $rank);
}
else{
$this->layout->echoln('
You have no more votes left!
');
return;
}
//save an empty vote to take the ticket.
$bService = $GLOBALS['ClassFactory']->getBallotsService();
$ticket = $bService->getTicket($election_id);
//if empty ballot?
if (!isset($_POST['candidate_id']))
{
$ballot = new Ballots('');
$ballot->setCandidate_id(0);
$ballot->setElection_id($election_id);
$ballot->setTicket($ticket);
$bService->saveOrUpdate($ballot);
}
else{
//model has not been set! do it from POST!!!
foreach ($_POST['candidate_id'] as $cid)
{
//create new ballot.
$ballot = new Ballots('');
$ballot->setCandidate_id($cid);
$ballot->setElection_id($election_id);
$ballot->setTicket($ticket);
$bService->saveOrUpdate($ballot);
}
}
//empty ballots can have only their ticket.
// count them with
// SELECT count( * ) AS ticket_counter FROM `Ballots` WHERE election_id =$eid GROUP BY ticket HAVING ticket_counter=1
//return ticket.
$this->layout->echoln('Your vote has been stored successfully.
Store this voting ticket: ');
$this->layout->echoln(''.$ticket.'');
$this->layout->echoln('
You can check your vote with it later');
$this->layout->echoln('
Go back to cast more votes (if any)');
}
}
/**
* End of helper classes
*/
$layout = new DefaultLayout(Access::BOARD, "./jc/");
$layout->init();
$layout->addJavascript('js/votes_check.js');
$ns = $GLOBALS['ClassFactory']->getNotificationsService();
//get id if set
if (isset($_GET['eid'])){
$election_id = $_GET['eid'];
$election = $GLOBALS['ClassFactory']->getElectionsService()->load($election_id);
$ns->seen($election_id,'Election');
unset($_SESSION['JC_MODULE']['Notifications']);
}
else{
$layout->redirect('elections_vote1.php');
}
if (date("Y-m-d H:i:s") > $election->getClose()){
$layout->echoln('Elections have closed!');
$layout->echoln('
Back');
$layout->page_footer();
exit();
}elseif (date("Y-m-d H:i:s") < $election->getOpen()){
$layout->echoln('Elections have not opened yet!');
$layout->echoln('
Back');
$layout->page_footer();
exit();
}
$bService = $GLOBALS['ClassFactory']->getBallotsService();
$ebService = $GLOBALS['ClassFactory']->getElections_BodiesService();
$myLocal = $ebService->getMyLocal($_SESSION['sess_uid']);
$LocalName = $GLOBALS['ClassFactory']->getDelegatesService()->getBodyName($myLocal);
$LocalName = $LocalName['BodyName'];
$eb = $ebService->find($election_id,$myLocal);
$rank = $ebService->getRank($eb,$_SESSION['sess_uid']);
if ($rank==0){
$layout->echoln('You are not in the delegates list!');
$layout->page_footer();
exit();
}
$votesLeft = $ebService->getMyVotes($eb,$_SESSION['sess_uid']);
$method = 'getVotes'.$rank;
$cast = $eb->$method();
$local_votes=$eb->getVotes();
$local_delegates=$ebService->getLocal_delegates($myLocal);
$local_cast_votes=($eb->getVotes1()+$eb->getVotes2()+$eb->getVotes3());
if (empty($_POST)){
$layout->echoln(''.$LocalName.' has '.$local_votes.' votes and '.$local_delegates.' delegate(s).
');
if( $local_votes%$local_delegates==0 ) $layout->echoln('Each delegate can cast '.$local_votes/$local_delegates.' vote(s)
');
elseif( $local_votes%$local_delegates==1 ) $layout->echoln('Since '.$local_votes.' votes cannot be equally distributed to '.$local_delegates.' delegates, 1 delegate can cast 1 vote more than the other 2 delegates.');
elseif( $local_votes%$local_delegates==2 ) $layout->echoln('Since '.$local_votes.' votes cannot be equally distributed to '.$local_delegates.' delegates, 2 delegates can cast 1 vote more than the 3rd delegate.');
}
$layout->echoln('Voting for: '.$election->getPosition().'
');
if (empty($_POST)){
$layout->echoln(''.$LocalName.' has already cast: '.$local_cast_votes.' vote(s). You have already cast: '.$cast.' vote(s).
');
$layout->echoln('Maximum amount of votes you can still cast: '.$votesLeft.' vote(s)
');
}
//$layout->echoln('
'.$election->getPosition().'
');
//body has votes?
if ($votesLeft > 0){
//ok;
//list of candidates
$candidates = $GLOBALS['ClassFactory']->getCandidatesService()->loadbyElection($election_id);
if (empty($_POST)){
$layout->echoln('
You can select up to '.$election->getMaxVotes().' candidates:
');
}
$ballot = new Ballots('');
$ballot->setElection_id($election_id);
$form = new BallotForm($layout, $ballot, $election->getMaxVotes());
$i=0;
foreach ($candidates as $candidate){
//$ballot->setCandidate_id($candidate['id']);
$cb = new MyCheckboxField('candidate_id[]', $candidate['name'].' '.$candidate['surname'], $candidate['id']);
$form->addField($cb);
}
$hf1 = new HiddenField('bodyCode',$myLocal);
$form->addHiddenField($hf1);
$hf2 = new HiddenField('election_id',$election_id);
$form->addHiddenField($hf2);
$hf3 = new HiddenField('maxVotes',$election->getMaxVotes());
$form->addHiddenField($hf3);
$form->display();
}
else{
$layout->echoln('
You have no more votes left!
');
}
$layout->page_footer();
?>