Example #1
0
 public static function getSingleResults($electionId)
 {
     $singles = Single::getListWithTickets($electionId, true, false);
     $votes = Vote::getSingleVotes($electionId);
     if (empty($votes)) {
         return self::noTicketVotes();
     }
     $total_cast_votes = 0;
     foreach ($votes as $v) {
         $total_cast_votes += $v['votes'];
         $key = $v['singleId'];
         $sorted_votes[$key][$v['ticketId']] = $v['votes'];
     }
     // sorted_votes   array(singleId => array(ticketId => votes))
     if (empty($singles)) {
         return null;
     }
     foreach ($singles as $ballot) {
         $ballot_row['title'] = $ballot['title'];
         foreach ($ballot['tickets'] as $ticket) {
             if (isset($sorted_votes[$ticket['singleId']][$ticket['id']])) {
                 $vote = $sorted_votes[$ticket['singleId']][$ticket['id']];
                 $percentage = round($vote / $total_cast_votes * 100, 1);
                 $ticket['votes'] = "{$vote} ({$percentage}%)";
                 $ticket_rows[$vote . '.' . $ticket['id']] = self::ticketTemplate($ticket);
             }
         }
         krsort($ticket_rows);
         $ballot_row['tickets'] = implode("\n", $ticket_rows);
         $tpl['ballots'][] = $ballot_row;
     }
     $template = new \Template();
     $template->addVariables($tpl);
     $template->setModuleTemplate('election', 'Admin/Report/Single.html');
     return $template->get();
 }