/** * @param int $id * * @return array|\Symfony\Component\HttpFoundation\RedirectResponse * @throws \ACP3\Core\Controller\Exception\ResultNotExistsException */ public function execute($id) { $answer = $this->request->getPost()->get('answer'); $time = $this->date->getCurrentDateTime(); if ($this->pollRepository->pollExists($id, $time, is_array($answer)) === true) { if (!empty($answer) || is_array($answer) === true) { return $this->executePost($this->request->getPost()->all(), $time, $id); } $poll = $this->pollRepository->getOneById($id); return ['question' => $poll['title'], 'multiple' => $poll['multiple'], 'answers' => $this->answerRepository->getAnswersByPollId($id)]; } throw new Core\Controller\Exception\ResultNotExistsException(); }
/** * @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(); }
/** * @param array $answers * @param int $pollId * * @return bool|int */ public function saveAnswers(array $answers, $pollId) { $bool = false; foreach ($answers as $row) { if (empty($row['id'])) { if (!empty($row['text']) && !isset($row['delete'])) { $data = ['text' => $this->secure->strEncode($row['text']), 'poll_id' => $pollId]; $bool = $this->answerRepository->insert($data); } } elseif (isset($row['delete'])) { $this->answerRepository->delete((int) $row['id']); } elseif (!empty($row['text'])) { $data = ['text' => $this->secure->strEncode($row['text'])]; $bool = $this->answerRepository->update($data, (int) $row['id']); } } return $bool; }
/** * @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]; }