public static function saveVote() { self::check_logged_in(); $p = $_POST; $user = User::findByPK($p['user_id']); $poll = Poll::findByPK($p['poll_id']); $polloptions = PollOption::findByPollId($poll->id); $curruser = self::get_user_logged_in(); if (!($curruser->id == $user->id)) { Redirect::to('/user/' . $curruser->id, array('error' => 'Virheellinen pyyntö!')); } $status = Poll::checkVoteStatus($user->id, $poll->id); if ($status === NULL) { Redirect::to('/user/' . $curruser->id, array('error' => 'Sinulla ei ole äänioikeutta pyytämääsi äänestykseen!')); } elseif ($status === TRUE) { Redirect::to('/user/' . $curruser->id, array('error' => 'Olet jo käyttänyt äänioikeutesi äänestyksessä ' . $poll->name . '!')); } $errors = array(); $poll_start = new DateTime($poll->start_time); $poll_end = new DateTime($poll->end_time); if ($poll_start->getTimeStamp() >= time()) { $errors[] = 'Äänestys ei ole vielä alkanut!'; } if ($poll_end->getTimeStamp() <= time()) { $errors[] = 'Äänestys on päättynyt – et voi enää äänestää!'; } if (!isset($_POST['choice'])) { $errors[] = 'Et valinnut yhtään vaihtoehtoa. Valitse vaihtoehto äänestääksesi!'; } else { $polloptions[$p['choice']]->chosen = TRUE; } if (!isset($_POST['accept'])) { $errors[] = 'Et ilmaissut hyväksyväsi äänioikeutesi ainutkertaisuuttä. Et voi äänestää hyväksymättä sitä.'; } if (!empty($errors)) { View::make('poll/vote.html', array('user' => $user, 'poll' => $poll, 'polloptions' => $polloptions, 'errors' => $errors)); } else { $vote = new Vote(array('users_id' => $user->id, 'polls_id' => $poll->id, 'poll_options_id' => $polloptions[$p['choice']]->id, 'time' => date('c'))); $vote->save(); Redirect::to('/user/' . $curruser->id, array('message' => 'Äänesi äänestyksessä ' . $poll->name . ' on tallennettu! Antamaasi ääntä ei voida yhdistää sinuun myöhemmin.')); } }
public function getResults() { $r = array('id' => $this->id, 'name' => $this->name, 'eligible_count' => $this->getEligibleVotersCount(), 'vote_count' => 0, 'turnout' => 0.0, 'options' => array()); $options = PollOption::findByPollId($this->id); foreach ($options as $option) { $r['options'][$option->id] = array('id' => $option->id, 'name' => $option->name, 'vote_count' => 0); $votes = Vote::findByPollIdAndOptionId($this->id, $option->id); foreach ($votes as $vote) { ++$r['vote_count']; ++$r['options'][$option->id]['vote_count']; } $r['options'][$option->id]['percentage'] = $r['eligible_count'] > 0 ? round($r['options'][$option->id]['vote_count'] / $r['eligible_count'] * 100) : 0; } $r['turnout'] = $r['eligible_count'] > 0 ? round($r['vote_count'] / $r['eligible_count'] * 100) : 0; uasort($r['options'], "self::cmpVoteCount"); return $r; }