fetchTable('Responses'); $this->Responses = $responses; } /** * Derives the anonymous participant hash from the cookie id. The poll id is * part of the HMAC key so participants cannot be correlated across polls. * * @param string $participantId Random id from the participant cookie * @param int $pollId Poll id * @return string */ public static function participantHash(string $participantId, int $pollId): string { return hash_hmac('sha256', $participantId, Security::getSalt() . '|poll|' . $pollId); } /** * @param \App\Model\Entity\Poll $poll Poll * @param \App\Model\Entity\Slide $slide Slide (with slide_options for choice slides) * @param string $participantHash Participant hash * @param array $data Request data * @return void * @throws \App\Service\VoteException */ public function submit(Poll $poll, Slide $slide, string $participantHash, array $data): void { if (!$poll->is_active) { throw new VoteException(__('Diese Umfrage ist derzeit geschlossen.')); } if ($slide->poll_id !== $poll->id || $poll->current_slide_id !== $slide->id) { throw new VoteException(__('Diese Frage ist nicht mehr aktiv.')); } if (!$slide->isInteractive()) { throw new VoteException(__('Auf dieser Folie kann nicht abgestimmt werden.')); } if (!$slide->is_voting_open) { throw new VoteException(__('Die Abstimmung zu dieser Frage ist geschlossen.')); } if ($slide->type !== Slide::TYPE_WORDCLOUD && $this->Responses->hasResponded($slide->id, $participantHash)) { throw new VoteException(__('Du hast bereits abgestimmt.')); } $rows = match ($slide->type) { Slide::TYPE_CHOICE => $this->choiceRows($slide, $data), Slide::TYPE_WORDCLOUD => $this->wordRows($slide, $participantHash, $data), Slide::TYPE_OPEN => $this->openRows($data), Slide::TYPE_SCALE => $this->scaleRows($slide, $data), default => throw new VoteException(__('Unbekannter Folientyp.')), }; $entities = []; foreach ($rows as $row) { $entities[] = $this->Responses->newEntity($row + [ 'slide_id' => $slide->id, 'participant_hash' => $participantHash, ]); } $this->Responses->saveManyOrFail($entities); } /** * @param \App\Model\Entity\Slide $slide Slide * @param array $data Request data * @return list> */ protected function choiceRows(Slide $slide, array $data): array { $selected = $data['option_ids'] ?? $data['option_id'] ?? []; $selected = array_values(array_unique(array_map('intval', (array)$selected))); $validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []); $selected = array_values(array_intersect($selected, $validIds)); if ($selected === []) { throw new VoteException(__('Bitte wähle eine Antwort aus.')); } if (!$slide->setting('multiple') && count($selected) > 1) { throw new VoteException(__('Bitte wähle nur eine Antwort aus.')); } return array_map(fn(int $id) => ['slide_option_id' => $id], $selected); } /** * Normalized key of a word cloud term: whitespace collapsed, trimmed, cut to * MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison. * * @param string $word Raw input * @return string */ public static function cleanWord(string $word): string { $word = trim((string)preg_replace('/\s+/u', ' ', $word)); return mb_substr($word, 0, self::MAX_WORD_LENGTH); } /** * Case-insensitive grouping key of a word cloud term. * * @param string $word Word * @return string */ public static function wordKey(string $word): string { return mb_strtolower(self::cleanWord($word)); } /** * Accepts `word` (string) or `words` (list). Words the participant already * submitted are skipped, the rest is cut to the remaining limit. * * @param \App\Model\Entity\Slide $slide Slide * @param string $participantHash Participant hash * @param array $data Request data * @return list> */ protected function wordRows(Slide $slide, string $participantHash, array $data): array { $votes = $this->Responses->wordVotesOf($slide->id, $participantHash); $existing = array_values(array_unique(array_map(self::wordKey(...), $votes['up']))); $downvoted = array_fill_keys(array_map(self::wordKey(...), $votes['down']), true); if (($data['vote'] ?? null) === 'down') { return $this->downvoteRows($slide, (string)($data['word'] ?? ''), $existing, $downvoted); } $input = array_merge((array)($data['words'] ?? []), (array)($data['word'] ?? [])); $seen = array_fill_keys($existing, true); $words = []; $duplicate = false; $rejected = false; foreach ($input as $word) { $word = self::cleanWord((string)$word); $key = mb_strtolower($word); if ($word === '') { continue; } if (isset($downvoted[$key])) { $rejected = true; continue; } if (isset($seen[$key])) { $duplicate = true; continue; } $seen[$key] = true; $words[] = $word; } $max = (int)$slide->setting('max_words'); if ($max > 0) { $remaining = max(0, $max - count($existing)); if ($remaining === 0) { throw new VoteException(__n( 'Du hast deinen Begriff bereits abgegeben.', 'Du hast bereits alle {0} Begriffe abgegeben.', $max, $max, )); } $words = array_slice($words, 0, $remaining); } if ($words === []) { throw new VoteException(match (true) { $duplicate => __('Diesen Begriff hast du schon genannt.'), $rejected => __('Diesen Begriff hast du bereits abgelehnt.'), default => __('Bitte gib einen Begriff ein.'), }); } return array_map(fn(string $word) => ['text_value' => $word], $words); } /** * @param \App\Model\Entity\Slide $slide Slide * @param string $word Word to downvote * @param list $own Keys the participant named/upvoted * @param array $downvoted Keys the participant downvoted * @return list> */ protected function downvoteRows(Slide $slide, string $word, array $own, array $downvoted): array { if (!$slide->setting('allow_upvotes') || !$slide->setting('allow_downvotes')) { throw new VoteException(__('Downvotes sind bei dieser Frage nicht möglich.')); } $word = self::cleanWord($word); $key = mb_strtolower($word); if ($word === '' || !$this->Responses->wordExists($slide->id, $key)) { throw new VoteException(__('Diesen Begriff gibt es nicht in der Wortwolke.')); } if (in_array($key, $own, true)) { throw new VoteException(__('Du hast diesen Begriff selbst genannt.')); } if (isset($downvoted[$key])) { throw new VoteException(__('Diesen Begriff hast du bereits abgelehnt.')); } return [['text_value' => $word, 'numeric_value' => self::DOWNVOTE]]; } /** * @param array $data Request data * @return list> */ protected function openRows(array $data): array { $text = trim((string)($data['text'] ?? '')); if ($text === '') { throw new VoteException(__('Bitte gib eine Antwort ein.')); } if (mb_strlen($text) > self::MAX_TEXT_LENGTH) { throw new VoteException(__('Die Antwort darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH)); } return [['text_value' => $text]]; } /** * @param \App\Model\Entity\Slide $slide Slide * @param array $data Request data * @return list> */ protected function scaleRows(Slide $slide, array $data): array { $value = filter_var($data['value'] ?? null, FILTER_VALIDATE_INT); $min = (int)$slide->setting('scale_min'); $max = (int)$slide->setting('scale_max'); if ($value === false || $value < $min || $value > $max) { throw new VoteException(__('Bitte wähle einen Wert zwischen {0} und {1}.', $min, $max)); } return [['numeric_value' => $value]]; } }