fetchTable('Polls'); $this->Polls = $polls; } /** * @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event * @return void */ public function beforeFilter(EventInterface $event): void { parent::beforeFilter($event); $pid = (string)$this->request->getCookie(self::COOKIE, ''); if (!preg_match('/^[a-f0-9]{32}$/', $pid)) { $pid = bin2hex(random_bytes(16)); $this->newParticipant = true; } $this->participantId = $pid; } /** * @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event * @return void */ public function afterFilter(EventInterface $event): void { parent::afterFilter($event); if ($this->newParticipant) { $this->response = $this->response->withCookie(Cookie::create(self::COOKIE, $this->participantId, [ 'expires' => new DateTime('+1 year'), 'path' => '/', 'httponly' => true, 'secure' => $this->request->is('https'), 'samesite' => 'Lax', ])); } } /** * @param string $code Join code * @return void */ public function view(string $code): void { $poll = $this->loadPoll($code); $stateService = new PollStateService(); $slide = $poll->is_active ? $stateService->currentSlide($poll) : null; $participantHash = $this->participantHash($poll); $state = $stateService->forParticipant($poll, $participantHash, $slide); $ownText = $slide !== null && VoteService::hasEditableText($slide) && $state['hasResponded'] ? (new VoteService())->ownText($slide, $participantHash) : null; $this->set(compact('poll', 'slide', 'state', 'participantHash', 'ownText')); $this->setPollInterval(); } /** * @param string $code Join code * @return \Cake\Http\Response */ public function state(string $code): Response { $poll = $this->loadPoll($code); return $this->json((new PollStateService())->forParticipant($poll, $this->participantHash($poll))); } /** * Stores an answer. AJAX requests (word cloud entries and upvotes) get JSON * `{ok, message, state}` instead of a redirect with flash message. * * @param string $code Join code * @return \Cake\Http\Response|null */ public function vote(string $code): ?Response { $this->request->allowMethod(['post']); $poll = $this->loadPoll($code); $stateService = new PollStateService(); $slide = $stateService->currentSlide($poll); $submittedSlideId = (int)$this->request->getData('slide_id'); $ajax = $this->request->is('ajax'); $status = 200; try { // Generous per IP (a whole audience may share one Wi-Fi), only stops scripted floods. if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) { $status = 429; throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.')); } if ($slide === null || $slide->id !== $submittedSlideId) { throw new VoteException(__('Diese Frage ist nicht mehr aktiv.')); } (new VoteService())->submit($poll, $slide, $this->participantHash($poll), (array)$this->request->getData()); $ok = true; $message = match (true) { !$slide->isQa() => __('Danke für deine Antwort!'), $this->request->getData('vote') === 'up' => __('Deine Stimme ist angekommen.'), $this->request->getData('vote') === 'remove' => __('Deine Stimme wurde zurückgenommen.'), default => __('Danke, deine Frage ist angekommen!'), }; } catch (VoteException $e) { $ok = false; $message = $e->getMessage(); } if ($ajax) { return $this->json([ 'ok' => $ok, 'message' => $message, 'state' => $stateService->forParticipant($poll, $this->participantHash($poll), $slide), ], $status); } $ok ? $this->Flash->success($message) : $this->Flash->error($message); return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]); } /** * Replaces the own answer text (open question) or comment (scale) while voting is open, * e.g. to fix a typo. See VoteService::editText(). * * @param string $code Join code * @return \Cake\Http\Response|null */ public function edit(string $code): ?Response { $this->request->allowMethod(['post']); $poll = $this->loadPoll($code); $slide = (new PollStateService())->currentSlide($poll); try { if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) { throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.')); } if ($slide === null || $slide->id !== (int)$this->request->getData('slide_id')) { throw new VoteException(__('Diese Frage ist nicht mehr aktiv.')); } $data = (array)$this->request->getData(); (new VoteService())->editText($poll, $slide, $this->participantHash($poll), $data); $comment = $data['comment'] ?? ''; $this->Flash->success(match (true) { $slide->type === Slide::TYPE_OPEN => __('Deine Antwort wurde geändert.'), !is_scalar($comment) || trim((string)$comment) === '' => __('Dein Kommentar wurde entfernt.'), default => __('Dein Kommentar wurde gespeichert.'), }); } catch (VoteException $e) { $this->Flash->error($e->getMessage()); } return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]); } /** * @param string $code Join code * @return \App\Model\Entity\Poll */ protected function loadPoll(string $code): Poll { /** @var \App\Model\Entity\Poll|null $poll */ $poll = $this->Polls->find('byJoinCode', code: $code)->first(); if ($poll === null) { throw new NotFoundException(__('Zu diesem Code wurde keine Umfrage gefunden.')); } return $poll; } /** * @param \App\Model\Entity\Poll $poll Poll * @return string */ protected function participantHash(Poll $poll): string { return VoteService::participantHash($this->participantId, $poll->id); } }