Add quiz slide type
CI / Deploy / test (push) Successful in 41s
CI / Deploy / deploy (push) Skipped

Quiz slides have predefined answers with one or more marked as correct.
The presenter starts the quiz, which then runs until its optional time
limit expires or the presenter ends it. Counts and correct answers are
only revealed once the quiz is finished; participants then see whether
their answer was right.

- migration: slide_options.is_correct, slides.voting_started_at/voting_ends_at
- Slide::isVotingOpen() includes the quiz deadline, expired quizzes are
  closed lazily when the state is read
- presenter countdown, quiz tiles and reveal with correct-answer summary
- CSV export marks quiz answers as right/wrong

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 14:39:21 +01:00
1 parent 04cbc5bcd3
commit fd8da4c16c
18 files changed
+999 -43

No files matched your search

+54 -9
View File
@@ -17,6 +17,11 @@ use Cake\ORM\Locator\LocatorAwareTrait;
* the participant has answered (or voting is closed). Exception: word clouds
* with upvotes enabled show the live cloud while voting is open, because
* participants upvote by clicking words in it.
*
* Quiz slides reveal results (and the participant's own correctness) only
* once the quiz is finished – then always, independent of
* `show_results_to_participants`. Timed quizzes are closed lazily here
* when their deadline has passed.
*/
class PollStateService
{
@@ -58,7 +63,7 @@ class PollStateService
->contain(['SlideOptions'])
->first();
return $slide;
return $slide ? $this->Slides->closeIfExpired($slide) : null;
}
/**
@@ -72,7 +77,7 @@ class PollStateService
$index = null;
foreach ($slides as $i => $slide) {
if ($slide->id === $poll->current_slide_id) {
$current = $slide;
$current = $this->Slides->closeIfExpired($slide);
$index = $i;
}
}
@@ -105,27 +110,29 @@ class PollStateService
{
$slide = $poll->is_active ? $this->currentSlide($poll) : null;
$words = $slide?->type === Slide::TYPE_WORDCLOUD ? $this->wordState($slide, $participantHash) : null;
$quiz = $slide?->isQuiz() ? $this->quizState($slide, $participantHash) : null;
$hasResponded = $words !== null
? $words['mine'] !== [] || $words['down'] !== []
: $slide && $slide->isInteractive() && $this->Responses->hasResponded($slide->id, $participantHash);
$isVotingOpen = $slide?->isVotingOpen() ?? false;
$showResults = $slide !== null
&& $slide->isInteractive()
&& (
($poll->show_results_to_participants && ($hasResponded || !$slide->is_voting_open))
|| ($words !== null && $words['allowUpvotes'] && $slide->is_voting_open)
);
&& ($quiz !== null
? $quiz['phase'] === Slide::QUIZ_FINISHED
: ($poll->show_results_to_participants && ($hasResponded || !$isVotingOpen))
|| ($words !== null && $words['allowUpvotes'] && $isVotingOpen));
return [
'isActive' => $poll->is_active,
'slideId' => $slide?->id,
'isVotingOpen' => $slide?->is_voting_open ?? false,
'isVotingOpen' => $isVotingOpen,
'hasResponded' => $hasResponded,
// Changes whenever the participant page must re-render.
'version' => implode(':', [
(int)$poll->is_active,
$slide?->id ?? 0,
(int)($slide?->is_voting_open ?? 0),
(int)$isVotingOpen,
// Word clouds are answered word by word without reload.
$words !== null ? 0 : (int)$hasResponded,
$slide?->modified?->toUnixString() ?? 0,
@@ -133,6 +140,44 @@ class PollStateService
'slide' => $showResults ? $this->slidePayload($slide) : null,
'results' => $showResults ? $this->results->forSlide($slide) : null,
'words' => $words,
'quiz' => $quiz,
];
}
/**
* Quiz progress of one participant. The own answer and its correctness are
* only included once the quiz is finished.
*
* @param \App\Model\Entity\Slide $slide Quiz slide incl. slide_options
* @param string $participantHash Participant hash
* @return array{phase: string, remaining: int|null, answerId: int|null, isCorrect: bool|null}
*/
public function quizState(Slide $slide, string $participantHash): array
{
$phase = $slide->quizPhase();
$answerId = null;
$isCorrect = null;
if ($phase === Slide::QUIZ_FINISHED) {
$response = $this->Responses->find()
->select(['slide_option_id'])
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
->first();
$answerId = $response?->slide_option_id;
if ($answerId !== null) {
$isCorrect = false;
foreach ($slide->slide_options ?? [] as $option) {
if ($option->id === $answerId) {
$isCorrect = $option->is_correct;
}
}
}
}
return [
'phase' => $phase,
'remaining' => $phase === Slide::QUIZ_RUNNING ? $slide->remainingSeconds() : null,
'answerId' => $answerId,
'isCorrect' => $isCorrect,
];
}
@@ -171,7 +216,7 @@ class PollStateService
'typeLabel' => $slide->typeLabel(),
'question' => $slide->question,
'description' => (string)$slide->description,
'isVotingOpen' => $slide->is_voting_open,
'isVotingOpen' => $slide->isVotingOpen(),
'isInteractive' => $slide->isInteractive(),
'settings' => $slide->getSettings(),
];
+46
View File
@@ -19,6 +19,11 @@ use Cake\ORM\Locator\LocatorAwareTrait;
* - open: 'answers' => [['text', 'created']] newest first
* - scale: 'buckets' => [['value', 'count', 'percent']], 'average' => float|null,
* 'min', 'max', 'minLabel', 'maxLabel'
* - quiz: 'phase' (ready|running|finished), 'timeLimit' (s, 0 = manual), 'remaining' (s|null),
* 'revealed' (bool), 'options' => [['id', 'label']] while not finished;
* once finished additionally per option 'count', 'percent', 'correct' (bool)
* and 'correctCount' (participants with a correct answer).
* Counts and correct answers are NEVER part of the result before the quiz is finished.
*/
class ResultsService
{
@@ -54,6 +59,7 @@ class ResultsService
Slide::TYPE_WORDCLOUD => $this->wordcloud($slide),
Slide::TYPE_OPEN => $this->open($slide),
Slide::TYPE_SCALE => $this->scale($slide),
Slide::TYPE_QUIZ => $this->quiz($slide),
default => [],
};
}
@@ -88,6 +94,46 @@ class ResultsService
return ['options' => $options, 'votes' => $total];
}
/**
* @param \App\Model\Entity\Slide $slide Slide
* @return array<string, mixed>
*/
protected function quiz(Slide $slide): array
{
$phase = $slide->quizPhase();
$result = [
'phase' => $phase,
'timeLimit' => (int)$slide->setting('time_limit'),
'remaining' => $phase === Slide::QUIZ_RUNNING ? $slide->remainingSeconds() : null,
'revealed' => $phase === Slide::QUIZ_FINISHED,
];
if ($phase !== Slide::QUIZ_FINISHED) {
$result['options'] = array_map(fn($option) => [
'id' => $option->id,
'label' => $option->label,
], $slide->slide_options ?? []);
return $result;
}
$choice = $this->choice($slide);
$correctIds = [];
foreach ($slide->slide_options ?? [] as $option) {
if ($option->is_correct) {
$correctIds[] = $option->id;
}
}
$result['options'] = array_map(fn(array $option) => $option + [
'correct' => in_array($option['id'], $correctIds, true),
], $choice['options']);
$result['correctCount'] = $correctIds === [] ? 0 : $this->Responses->find()
->where(['slide_id' => $slide->id, 'slide_option_id IN' => $correctIds])
->distinct(['participant_hash'])
->count();
return $result;
}
/**
* @param \App\Model\Entity\Slide $slide Slide
* @return array<string, mixed>
+26 -2
View File
@@ -21,6 +21,8 @@ use Cake\Utility\Security;
* exists in the cloud is an upvote – both are stored as the same row.
* `vote=down` stores a downvote (numeric_value = DOWNVOTE) for an existing
* 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).
*/
class VoteService
{
@@ -78,8 +80,12 @@ class VoteService
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->isVotingOpen()) {
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.'),
default => __('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.'));
@@ -90,6 +96,7 @@ class VoteService
Slide::TYPE_WORDCLOUD => $this->wordRows($slide, $participantHash, $data),
Slide::TYPE_OPEN => $this->openRows($data),
Slide::TYPE_SCALE => $this->scaleRows($slide, $data),
Slide::TYPE_QUIZ => $this->quizRows($slide, $data),
default => throw new VoteException(__('Unbekannter Folientyp.')),
};
@@ -125,6 +132,23 @@ class VoteService
return array_map(fn(int $id) => ['slide_option_id' => $id], $selected);
}
/**
* @param \App\Model\Entity\Slide $slide Slide
* @param array<string, mixed> $data Request data
* @return list<array<string, mixed>>
*/
protected function quizRows(Slide $slide, array $data): array
{
$selected = $data['option_id'] ?? null;
$id = is_scalar($selected) ? filter_var($selected, FILTER_VALIDATE_INT) : false;
$validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []);
if ($id === false || !in_array($id, $validIds, true)) {
throw new VoteException(__('Bitte wähle eine Antwort aus.'));
}
return [['slide_option_id' => $id]];
}
/**
* Normalized key of a word cloud term: whitespace collapsed, trimmed, cut to
* MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison.