Add Q&A slide type
CI / Deploy / test (push) Successful in 48s
CI / Deploy / deploy (push) Skipped

Participants submit questions, upvote them and the presenter discusses
them one by one, most upvoted first.

- Stages collect -> vote -> discuss, switched manually by the presenter
  (V key or stepper, going back is possible)
- Configurable upvote budget per participant, freely distributable
  (several upvotes on one question are allowed), votes can be withdrawn
- Participants see all questions while collecting to avoid duplicates;
  no counts before the discussion
- While discussing, next/prev step through the questions before
  moving to the next slide
- Migration: slides.stage, slides.focus_response_id, responses.parent_id

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 16:09:46 +01:00
1 parent ea944b080f
commit f0c6687100
21 files changed
+1327 -27

No files matched your search

+92 -2
View File
@@ -23,6 +23,11 @@ use Cake\Utility\Security;
* word of someone else. Each participant rates a word only once (up or down).
* - quiz: exactly one option, only while the quiz runs (Slide::isVotingOpen()
* includes the deadline of a timed quiz).
* - Q&A (see QaService for the stages): `question` (text) only while collecting,
* at most MAX_QA_QUESTIONS per participant, the same question (case-insensitive)
* only once per slide. `vote=up|remove` + `question_id` only while voting: upvote
* questions of others, at most `max_upvotes` in total, freely distributed (several
* on one question are allowed); `remove` withdraws one own upvote of the question.
*/
class VoteService
{
@@ -30,6 +35,7 @@ class VoteService
public const MAX_WORD_LENGTH = 40;
public const MAX_TEXT_LENGTH = 500;
public const MAX_QA_QUESTIONS = 10;
/**
* `responses.numeric_value` of a word cloud downvote (upvotes/words: null).
@@ -84,10 +90,11 @@ class VoteService
throw new VoteException(match (true) {
$slide->isQuiz() && $slide->voting_started_at === null => __('Das Quiz hat noch nicht begonnen.'),
$slide->isQuiz() => __('Die Zeit für dieses Quiz ist abgelaufen.'),
$slide->isQa() => __('Die Fragerunde läuft – Fragen und Stimmen sind nicht mehr möglich.'),
default => __('Die Abstimmung zu dieser Frage ist geschlossen.'),
});
}
if ($slide->type !== Slide::TYPE_WORDCLOUD && $this->Responses->hasResponded($slide->id, $participantHash)) {
if (!$slide->allowsMultipleResponses() && $this->Responses->hasResponded($slide->id, $participantHash)) {
throw new VoteException(__('Du hast bereits abgestimmt.'));
}
@@ -97,6 +104,7 @@ class VoteService
Slide::TYPE_OPEN => $this->openRows($data),
Slide::TYPE_SCALE => $this->scaleRows($slide, $data),
Slide::TYPE_QUIZ => $this->quizRows($slide, $data),
Slide::TYPE_QA => $this->qaRows($slide, $participantHash, $data),
default => throw new VoteException(__('Unbekannter Folientyp.')),
};
@@ -107,7 +115,9 @@ class VoteService
'participant_hash' => $participantHash,
]);
}
$this->Responses->saveManyOrFail($entities);
if ($entities !== []) {
$this->Responses->saveManyOrFail($entities);
}
}
/**
@@ -149,6 +159,86 @@ class VoteService
return [['slide_option_id' => $id]];
}
/**
* Q&A: a new question while collecting, an upvote (or its withdrawal) while voting.
* Withdrawing deletes one upvote row and returns no new rows.
*
* @param \App\Model\Entity\Slide $slide Slide
* @param string $participantHash Participant hash
* @param array<string, mixed> $data Request data
* @return list<array<string, mixed>>
*/
protected function qaRows(Slide $slide, string $participantHash, array $data): array
{
$stage = $slide->qaStage();
$vote = $data['vote'] ?? null;
$own = $this->Responses->qaVotesOf($slide->id, $participantHash);
if ($vote === null) {
if ($stage !== Slide::QA_COLLECT) {
throw new VoteException(__('Es können keine neuen Fragen mehr eingereicht werden.'));
}
$text = trim((string)preg_replace('/\s+/u', ' ', (string)($data['question'] ?? '')));
if ($text === '') {
throw new VoteException(__('Bitte gib eine Frage ein.'));
}
if (mb_strlen($text) > self::MAX_TEXT_LENGTH) {
throw new VoteException(__('Die Frage darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH));
}
$key = mb_strtolower($text);
foreach ($this->Responses->questionsOf($slide->id)->select(['text_value'])->disableHydration() as $row) {
if (mb_strtolower((string)$row['text_value']) === $key) {
throw new VoteException(__('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.'));
}
}
if (count($own['questions']) >= self::MAX_QA_QUESTIONS) {
throw new VoteException(__('Du kannst höchstens {0} Fragen stellen.', self::MAX_QA_QUESTIONS));
}
return [['text_value' => $text]];
}
if ($stage !== Slide::QA_VOTE) {
throw new VoteException(__('Die Abstimmung über die Fragen hat noch nicht begonnen.'));
}
$id = is_scalar($data['question_id'] ?? null) ? filter_var($data['question_id'], FILTER_VALIDATE_INT) : false;
if ($id === false || !$this->Responses->questionsOf($slide->id)->where(['Responses.id' => $id])->count()) {
throw new VoteException(__('Diese Frage gibt es nicht.'));
}
if ($vote === 'remove') {
// Withdraws one of possibly several own upvotes on the question.
$upvote = $this->Responses->find()
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash, 'parent_id' => $id])
->orderBy(['id' => 'DESC'])
->first();
if ($upvote === null) {
throw new VoteException(__('Du hast diese Frage nicht unterstützt.'));
}
$this->Responses->deleteOrFail($upvote);
return [];
}
if ($vote !== 'up') {
throw new VoteException(__('Ungültige Stimme.'));
}
if (in_array($id, $own['questions'], true)) {
throw new VoteException(__('Deine eigene Frage kannst du nicht unterstützen.'));
}
// Several upvotes on the same question are allowed (push important questions).
$max = (int)$slide->setting('max_upvotes');
if (count($own['upvotes']) >= $max) {
throw new VoteException(__n(
'Du hast deine Stimme schon vergeben. Nimm sie zurück, um eine andere Frage zu unterstützen.',
'Du hast alle {0} Stimmen vergeben. Nimm eine zurück, um eine andere Frage zu unterstützen.',
$max,
$max,
));
}
return [['parent_id' => $id]];
}
/**
* Normalized key of a word cloud term: whitespace collapsed, trimmed, cut to
* MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison.