/** * @param int $id * * @return array * @throws \ACP3\Core\Controller\Exception\ResultNotExistsException */ public function execute($id) { $this->setCacheResponseCacheable($this->config->getSettings(Schema::MODULE_NAME)['cache_lifetime']); if ($this->pollRepository->pollExists($id, $this->date->getCurrentDateTime()) === true) { $question = $this->pollRepository->getOneByIdWithTotalVotes($id); $answers = $this->answerRepository->getAnswersWithVotesByPollId($id); $cAnswers = count($answers); $totalVotes = $question['total_votes']; for ($i = 0; $i < $cAnswers; ++$i) { $answers[$i]['percent'] = $totalVotes > 0 ? round(100 * $answers[$i]['votes'] / $totalVotes, 2) : '0'; } return ['question' => $question['title'], 'answers' => $answers, 'total_votes' => $totalVotes]; } throw new Core\Controller\Exception\ResultNotExistsException(); }
/** * @return array */ public function execute() { $poll = $this->pollRepository->getLatestPoll($this->date->getCurrentDateTime()); $answers = []; $this->setTemplate('Polls/Widget/index.vote.tpl'); if (!empty($poll)) { $answers = $this->answerRepository->getAnswersWithVotesByPollId($poll['id']); if ($this->hasAlreadyVoted($poll['id'])) { $totalVotes = $poll['total_votes']; $cAnswers = count($answers); for ($i = 0; $i < $cAnswers; ++$i) { $votes = $answers[$i]['votes']; $answers[$i]['votes'] = $votes > 1 ? $this->translator->t('polls', 'number_of_votes', ['%votes%' => $votes]) : $this->translator->t('polls', $votes == 1 ? 'one_vote' : 'no_votes'); $answers[$i]['percent'] = $totalVotes > 0 ? round(100 * $votes / $totalVotes, 2) : '0'; } $this->setTemplate('Polls/Widget/index.result.tpl'); } } return ['sidebar_polls' => $poll, 'sidebar_poll_answers' => $answers]; }