. */ 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 PollBallotForm extends AbstractForm3 { public $maxVotes; public function __construct(Layout $layout, IModel $model, $maxVotes) { parent::__construct($layout, $model, 'form', array('Vote'),'votes_check(\'form\',\'pollOption_id[]\')'); $this->$maxVotes = $maxVotes; } public function save(IModel $model) { $poll_id = $_POST['poll_id']; //if Javascript disabled: if (isset($_POST['pollOption_id']) AND (count($_POST['pollOption_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; } $pbService = $GLOBALS['ClassFactory']->getPollBallotsService(); $votesLeft = $pbService->getMyVotes($model->getPoll_id(),$_SESSION['sess_uid']); //delegate has votes? if ($votesLeft <= 0){ $this->layout->echoln('
You have no more votes left!
'); return; } //save an empty vote to take the ticket. $ticket = $pbService->getTicket($poll_id, $_POST['body'], $_POST['delegate']); if (isset($_POST['pollOption_id'])) { //model has not been set! do it from POST!!! foreach ($_POST['pollOption_id'] as $poid) { //create new ballot. $pballot = new PollBallots(''); $pballot->setPollOption_id($poid); $pballot->setPoll_id($poll_id); $pballot->setTicket($ticket); $pballot->setBody($_POST['body']); $pballot->setDelegate($_POST['delegate']); $pbService->saveOrUpdate($pballot); } } //else empty ballot //empty ballots have only their ticket. // count them with // SELECT count( * ) AS ticket_counter FROM `pollBallots` WHERE poll_id =$pid GROUP BY ticket HAVING ticket_counter=1 $this->layout->redirect('polls_vote2.php?pid='.$poll_id); } } /** * 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['pid'])){ $poll_id = $_GET['pid']; $poll = $GLOBALS['ClassFactory']->getPollsService()->load($poll_id); $ns->seen($poll_id,'Poll'); unset($_SESSION['JC_MODULE']['Notifications']); } else{ $layout->redirect('polls_vote1.php'); } if (date("Y-m-d H:i:s") > $poll->getClose()){ $layout->echoln('Poll has closed!'); $layout->echoln('
Back'); $layout->page_footer(); exit(); }elseif (date("Y-m-d H:i:s") < $poll->getOpen()){ $layout->echoln('Poll has not opened yet!'); $layout->echoln('
Back'); $layout->page_footer(); exit(); } $pbService = $GLOBALS['ClassFactory']->getPollBallotsService(); $registered = $pbService->isRegistered($_SESSION['sess_uid']); if($registered==false){ $layout->echoln('You have not been registered!'); $layout->page_footer(); exit(0); } $result = $pbService->getMyVotes($_SESSION['sess_uid'], $poll_id); $votesLeft = $result['my_votes']; $cast = $result['votes_cast']; $local_delegates = $result['local_delegates']; $local_votes = $result['local_votes']; $local_votes_cast = $result['local_votes_cast']; $myLocal = $result['bodyCode']; $LocalName = $result['bodyName']; 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: '.$poll->getTitle().'

'); if (empty($_POST)){ $layout->echoln('

'.$LocalName.' has already cast: '.$local_votes_cast.' vote(s). You have already cast: '.$cast.' vote(s).
'); $layout->echoln('Maximum amount of votes you can still cast: '.$votesLeft.' vote(s)

'); } //delegate has votes? if ($votesLeft > 0){ //list of candidates $pollOptions = $GLOBALS['ClassFactory']->getPollOptionsService()->loadbyPoll($poll_id); if (empty($_POST)){ $layout->echoln('
You can select up to '.$poll->getMaxVotes().' options:
'); } $pballot = new PollBallots(''); $pballot->setPoll_id($poll_id); $form = new PollBallotForm($layout, $pballot, $poll->getMaxVotes()); $i=0; foreach ($pollOptions as $pollOption){ //$ballot->setCandidate_id($candidate['id']); $pob = new MyCheckboxField('pollOption_id[]', $pollOption['pollOption'], $pollOption['id']); $form->addField($pob); } $hf1 = new HiddenField('bodyCode',$myLocal); $form->addHiddenField($hf1); $hf2 = new HiddenField('poll_id',$poll_id); $form->addHiddenField($hf2); $hf3 = new HiddenField('maxVotes',$poll->getMaxVotes()); $form->addHiddenField($hf3); $hf3 = new HiddenField('body',$myLocal); $form->addHiddenField($hf3); $hf3 = new HiddenField('delegate',$_SESSION['sess_uid']); $form->addHiddenField($hf3); $form->display(); } else{ $layout->echoln('
You have no more votes left!
'); } $layout->page_footer(); ?>