Add quiz slide type
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:
1 parent
04cbc5bcd3
commit
fd8da4c16c
18 files changed
+999
-43
No files matched your search
@@ -21,6 +21,7 @@ use Cake\Validation\Validator;
|
||||
class SlidesTable extends Table
|
||||
{
|
||||
public const MAX_OPTIONS = 20;
|
||||
public const MAX_QUIZ_OPTIONS = 10;
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $config The configuration for the Table.
|
||||
@@ -151,6 +152,12 @@ class SlidesTable extends Table
|
||||
$settings['min_label'] = mb_substr(trim((string)($input['min_label'] ?? '')), 0, 60);
|
||||
$settings['max_label'] = mb_substr(trim((string)($input['max_label'] ?? '')), 0, 60);
|
||||
break;
|
||||
case Slide::TYPE_QUIZ:
|
||||
$limit = (int)($input['time_limit'] ?? $settings['time_limit']);
|
||||
if (in_array($limit, Slide::QUIZ_TIME_LIMITS, true)) {
|
||||
$settings['time_limit'] = $limit;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $settings;
|
||||
@@ -175,6 +182,53 @@ class SlidesTable extends Table
|
||||
return array_slice($labels, 0, self::MAX_OPTIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the quiz option rows of the slide form (`quiz_options[i][label|correct]`).
|
||||
* Empty rows are skipped, duplicate labels are merged.
|
||||
*
|
||||
* @param array<mixed> $rows Raw form rows
|
||||
* @return array{labels: list<string>, correct: list<string>}
|
||||
*/
|
||||
public static function parseQuizOptions(array $rows): array
|
||||
{
|
||||
$labels = [];
|
||||
$correct = [];
|
||||
foreach ($rows as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$label = mb_substr(trim((string)($row['label'] ?? '')), 0, 255);
|
||||
if ($label === '' || count($labels) >= self::MAX_QUIZ_OPTIONS) {
|
||||
continue;
|
||||
}
|
||||
if (!in_array($label, $labels, true)) {
|
||||
$labels[] = $label;
|
||||
}
|
||||
if (filter_var($row['correct'] ?? false, FILTER_VALIDATE_BOOLEAN) && !in_array($label, $correct, true)) {
|
||||
$correct[] = $label;
|
||||
}
|
||||
}
|
||||
|
||||
return ['labels' => $labels, 'correct' => $correct];
|
||||
}
|
||||
|
||||
/**
|
||||
* Persists the end of a timed quiz once its deadline has passed. Safe to
|
||||
* call repeatedly (no-op unless the slide is still flagged open).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return \App\Model\Entity\Slide
|
||||
*/
|
||||
public function closeIfExpired(Slide $slide): Slide
|
||||
{
|
||||
if ($slide->isExpired()) {
|
||||
$slide->is_voting_open = false;
|
||||
$this->saveOrFail($slide);
|
||||
}
|
||||
|
||||
return $slide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronises the answer options of a slide with the given labels.
|
||||
* Options whose label is unchanged keep their id (and therefore their votes);
|
||||
@@ -182,9 +236,10 @@ class SlidesTable extends Table
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Saved slide
|
||||
* @param list<string> $labels New labels in display order
|
||||
* @param list<string> $correct Labels of the correct options (quiz)
|
||||
* @return void
|
||||
*/
|
||||
public function syncOptions(Slide $slide, array $labels): void
|
||||
public function syncOptions(Slide $slide, array $labels, array $correct = []): void
|
||||
{
|
||||
$existing = $this->SlideOptions->find()
|
||||
->where(['slide_id' => $slide->id])
|
||||
@@ -192,11 +247,12 @@ class SlidesTable extends Table
|
||||
->indexBy('label')
|
||||
->toArray();
|
||||
|
||||
$this->getConnection()->transactional(function () use ($slide, $labels, $existing): void {
|
||||
$this->getConnection()->transactional(function () use ($slide, $labels, $correct, $existing): void {
|
||||
foreach ($labels as $index => $label) {
|
||||
$option = $existing[$label] ?? $this->SlideOptions->newEntity(['label' => $label]);
|
||||
$option->set('slide_id', $slide->id);
|
||||
$option->set('position', $index + 1);
|
||||
$option->set('is_correct', in_array($label, $correct, true));
|
||||
$this->SlideOptions->saveOrFail($option);
|
||||
unset($existing[$label]);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user