private function generatePoll($poll)
{
$sth = $this->db->query("SELECT COUNT(*) AS votes, selection FROM `pollanswers` WHERE `pollid` = " . $poll["id"] . " AND selection != 255 GROUP BY selection");
$pollAnswers = $sth->fetchAll(PDO::FETCH_ASSOC);
$sth = $this->db->query("SELECT COUNT(*) FROM `pollanswers` WHERE `pollid` = " . $poll["id"]);
$res = $sth->fetch();
$totalAnswers = $res[0];
$sth = $this->db->query("SELECT COUNT(*) FROM `pollanswers` WHERE `userid` = " . $this->user->getId() . " AND `pollid` = " . $poll["id"]);
$res = $sth->fetch();
$hasVoted = false;
if ($res[0] == 1) {
$hasVoted = true;
}
$sum = 0;
foreach ($pollAnswers as $answer) {
$sum += $answer["votes"];
}
if ($sum > 0) {
$multiplier = 100 / $sum;
} else {
$multiplier = 1;
}
foreach ($pollAnswers as &$answer) {
$answer["percent"] = round($answer["votes"] * $multiplier);
}
$pollAnswersIndexed = array();
foreach ($pollAnswers as $a) {
$pollAnswersIndexed[$a["selection"]] = $a;
}
$poll["totalAnswers"] = $totalAnswers;
$poll["hasVoted"] = $hasVoted;
for ($i = 0; $i < 20; $i++) {
if (strlen($poll["option" . $i]) > 0) {
if ($pollAnswersIndexed[$i]) {
$pollAnswersIndexed[$i]["title"] = $poll["option" . $i];
$item = $pollAnswersIndexed[$i];
} else {
$item = array("votes" => 0, "percent" => 0, "title" => $poll["option" . $i], "selection" => $i);
}
$poll["options"][$i] = $item;
}
}
return $poll;
}