Exemplo n.º 1
0
 public static function getMultipleResults($electionId)
 {
     $total_votes = Election::getTotalVotes($electionId);
     $multiples = Multiple::getListWithCandidates($electionId);
     if (empty($multiples)) {
         return null;
     }
     $votes = Vote::getMultipleVotes($electionId);
     if (empty($votes)) {
         return 'No multiple chair votes recorded.';
     }
     $total_cast_votes = array();
     foreach ($votes as $v) {
         $key = $v['multipleId'];
         if (!isset($total_cast_votes[$key])) {
             $total_cast_votes[$key] = 0;
         }
         $total_cast_votes[$key] += $v['votes'];
         $sorted_votes[$key][$v['candidateId']] = $v['votes'];
     }
     foreach ($multiples as $ballot) {
         $ballot_row['title'] = $ballot['title'];
         $ballot_row['seats'] = $ballot['seatNumber'];
         $candidates = array();
         foreach ($ballot['candidates'] as $c) {
             if (isset($sorted_votes[$ballot['id']][$c['id']])) {
                 $vote = $sorted_votes[$ballot['id']][$c['id']];
                 $template = new \Template();
                 $template->setModuleTemplate('election', 'Admin/Report/Candidate.html');
                 $template->add('name', $c['firstName'] . ' ' . $c['lastName']);
                 $percentage = round($vote / $total_cast_votes[$ballot['id']] * 100, 1);
                 $template->add('vote', "{$vote} ({$percentage}%)");
                 $template->add('picture', $c['picture']);
                 $candidates[$vote . '.' . $c['id']] = $template->get();
             }
         }
         if (!empty($candidates)) {
             krsort($candidates);
             $ballot_row['candidates'] = implode("\n", $candidates);
         } else {
             $ballot_row['candidates'] = self::noCandidateVotes();
         }
         $tpl['ballots'][] = $ballot_row;
     }
     $template = new \Template();
     $template->addVariables($tpl);
     $template->setModuleTemplate('election', 'Admin/Report/Multiple.html');
     return $template->get();
 }