Merge pull request 'Add quiz slide type' (#6) from feature/quiz-slide into main
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
commit
50010484be
18 files changed
+999
-43
No files matched your search
@@ -194,12 +194,13 @@ class AdminController extends AdminAppController
|
||||
case 'close':
|
||||
case 'toggle':
|
||||
if ($index !== false) {
|
||||
// Opening a quiz (re)starts its timer, see Slide::setVotingOpen().
|
||||
$slide = $slides[$index];
|
||||
$slide->is_voting_open = match ($action) {
|
||||
$slide->setVotingOpen(match ($action) {
|
||||
'open' => true,
|
||||
'close' => false,
|
||||
default => !$slide->is_voting_open,
|
||||
};
|
||||
default => !$slide->isVotingOpen(),
|
||||
});
|
||||
$this->Slides->saveOrFail($slide);
|
||||
}
|
||||
break;
|
||||
@@ -258,7 +259,9 @@ class AdminController extends AdminAppController
|
||||
}
|
||||
$labels = [];
|
||||
foreach ($slide->slide_options ?? [] as $option) {
|
||||
$labels[$option->id] = $option->label;
|
||||
$labels[$option->id] = $slide->isQuiz()
|
||||
? __('{0} ({1})', $option->label, $option->is_correct ? __('richtig') : __('falsch'))
|
||||
: $option->label;
|
||||
}
|
||||
$responses = $this->Responses->find()
|
||||
->where(['slide_id' => $slide->id])
|
||||
|
||||
@@ -40,10 +40,14 @@ class SlidesController extends AdminAppController
|
||||
]);
|
||||
$slide->setNew(true);
|
||||
$slide->poll_id = $this->poll->id;
|
||||
// A quiz is started by the presenter (that also starts its timer).
|
||||
$slide->is_voting_open = $type !== Slide::TYPE_QUIZ;
|
||||
$optionsText = $type === Slide::TYPE_CHOICE ? "\n" : '';
|
||||
$quizOptions = [];
|
||||
|
||||
if ($this->request->is('post')) {
|
||||
$optionsText = (string)$this->request->getData('options_text', '');
|
||||
$quizOptions = $this->postedQuizOptions();
|
||||
if ($this->saveSlide($slide, $optionsText)) {
|
||||
$this->Flash->success(__('Folie angelegt.'));
|
||||
|
||||
@@ -51,7 +55,7 @@ class SlidesController extends AdminAppController
|
||||
}
|
||||
}
|
||||
|
||||
$this->set(compact('slide', 'optionsText'));
|
||||
$this->set(compact('slide', 'optionsText', 'quizOptions'));
|
||||
$this->render('form');
|
||||
|
||||
return null;
|
||||
@@ -65,9 +69,11 @@ class SlidesController extends AdminAppController
|
||||
{
|
||||
$slide = $this->loadSlide($id);
|
||||
$optionsText = implode("\n", array_map(fn($o) => $o->label, $slide->slide_options ?? []));
|
||||
$quizOptions = array_map(fn($o) => ['label' => $o->label, 'correct' => $o->is_correct], $slide->slide_options ?? []);
|
||||
|
||||
if ($this->request->is(['post', 'put', 'patch'])) {
|
||||
$optionsText = (string)$this->request->getData('options_text', '');
|
||||
$quizOptions = $this->postedQuizOptions();
|
||||
if ($this->saveSlide($slide, $optionsText)) {
|
||||
$this->Flash->success(__('Folie gespeichert.'));
|
||||
|
||||
@@ -76,7 +82,7 @@ class SlidesController extends AdminAppController
|
||||
}
|
||||
|
||||
$hasResponses = $this->Slides->Responses->exists(['slide_id' => $slide->id]);
|
||||
$this->set(compact('slide', 'optionsText', 'hasResponses'));
|
||||
$this->set(compact('slide', 'optionsText', 'quizOptions', 'hasResponses'));
|
||||
$this->render('form');
|
||||
|
||||
return null;
|
||||
@@ -125,6 +131,13 @@ class SlidesController extends AdminAppController
|
||||
$this->request->allowMethod(['post']);
|
||||
$slide = $this->loadSlide($id);
|
||||
$this->Slides->Responses->deleteAll(['slide_id' => $slide->id]);
|
||||
if ($slide->isQuiz()) {
|
||||
// Back to "not started" so the quiz can be played again.
|
||||
$slide->is_voting_open = false;
|
||||
$slide->voting_started_at = null;
|
||||
$slide->voting_ends_at = null;
|
||||
$this->Slides->saveOrFail($slide);
|
||||
}
|
||||
$this->Flash->success(__('Antworten der Folie „{0}“ wurden gelöscht.', $slide->question));
|
||||
|
||||
return $this->redirect($this->adminUrl());
|
||||
@@ -150,6 +163,26 @@ class SlidesController extends AdminAppController
|
||||
return $slide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Quiz option rows as posted by the form (for re-rendering after errors).
|
||||
*
|
||||
* @return list<array{label: string, correct: bool}>
|
||||
*/
|
||||
protected function postedQuizOptions(): array
|
||||
{
|
||||
$rows = [];
|
||||
foreach ((array)$this->request->getData('quiz_options', []) as $row) {
|
||||
if (is_array($row)) {
|
||||
$rows[] = [
|
||||
'label' => (string)($row['label'] ?? ''),
|
||||
'correct' => filter_var($row['correct'] ?? false, FILTER_VALIDATE_BOOLEAN),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Patches and saves a slide incl. its options.
|
||||
*
|
||||
@@ -162,27 +195,40 @@ class SlidesController extends AdminAppController
|
||||
$data = (array)$this->request->getData();
|
||||
// The type of an existing slide cannot change (would invalidate responses).
|
||||
$data['type'] = $slide->type;
|
||||
$this->Slides->patchEntity($slide, $data, [
|
||||
'fields' => ['type', 'question', 'description', 'settings', 'is_voting_open'],
|
||||
'associated' => [],
|
||||
]);
|
||||
$fields = ['type', 'question', 'description', 'settings', 'is_voting_open'];
|
||||
if ($slide->isQuiz()) {
|
||||
// Quiz voting is controlled by the presenter only.
|
||||
$fields = array_values(array_diff($fields, ['is_voting_open']));
|
||||
}
|
||||
$this->Slides->patchEntity($slide, $data, ['fields' => $fields, 'associated' => []]);
|
||||
|
||||
$labels = SlidesTable::parseOptionLines($optionsText);
|
||||
$correct = [];
|
||||
if ($slide->type === Slide::TYPE_CHOICE && count($labels) < 2) {
|
||||
$slide->setError('options_text', __('Bitte gib mindestens zwei Antwortoptionen an (eine pro Zeile).'));
|
||||
}
|
||||
if ($slide->isQuiz()) {
|
||||
['labels' => $labels, 'correct' => $correct] = SlidesTable::parseQuizOptions(
|
||||
(array)$this->request->getData('quiz_options', []),
|
||||
);
|
||||
if (count($labels) < 2) {
|
||||
$slide->setError('quiz_options', __('Bitte gib mindestens zwei Antworten an.'));
|
||||
} elseif ($correct === []) {
|
||||
$slide->setError('quiz_options', __('Bitte markiere mindestens eine Antwort als richtig.'));
|
||||
}
|
||||
}
|
||||
if ($slide->hasErrors()) {
|
||||
$this->Flash->error(__('Bitte prüfe deine Eingaben.'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels): bool {
|
||||
return (bool)$this->Slides->getConnection()->transactional(function () use ($slide, $labels, $correct): bool {
|
||||
if (!$this->Slides->save($slide)) {
|
||||
return false;
|
||||
}
|
||||
if ($slide->type === Slide::TYPE_CHOICE) {
|
||||
$this->Slides->syncOptions($slide, $labels);
|
||||
if ($slide->type === Slide::TYPE_CHOICE || $slide->isQuiz()) {
|
||||
$this->Slides->syncOptions($slide, $labels, $correct);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model\Entity;
|
||||
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
@@ -17,6 +18,8 @@ use Cake\ORM\Entity;
|
||||
* @property int $position
|
||||
* @property array<string, mixed>|null $settings
|
||||
* @property bool $is_voting_open
|
||||
* @property \Cake\I18n\DateTime|null $voting_started_at Quiz: start of the voting window
|
||||
* @property \Cake\I18n\DateTime|null $voting_ends_at Quiz: deadline (null = closed manually)
|
||||
* @property \Cake\I18n\DateTime $created
|
||||
* @property \Cake\I18n\DateTime $modified
|
||||
* @property \App\Model\Entity\Poll $poll
|
||||
@@ -30,6 +33,21 @@ class Slide extends Entity
|
||||
public const TYPE_OPEN = 'open';
|
||||
public const TYPE_SCALE = 'scale';
|
||||
public const TYPE_CONTENT = 'content';
|
||||
public const TYPE_QUIZ = 'quiz';
|
||||
|
||||
/**
|
||||
* Quiz phases, see quizPhase().
|
||||
*/
|
||||
public const QUIZ_READY = 'ready';
|
||||
public const QUIZ_RUNNING = 'running';
|
||||
public const QUIZ_FINISHED = 'finished';
|
||||
|
||||
/**
|
||||
* Selectable quiz time limits in seconds (0 = presenter ends the quiz manually).
|
||||
*
|
||||
* @var list<int>
|
||||
*/
|
||||
public const QUIZ_TIME_LIMITS = [0, 10, 15, 20, 30, 45, 60, 90, 120, 180, 300];
|
||||
|
||||
/**
|
||||
* Default settings per type. Unknown keys are dropped on save.
|
||||
@@ -43,6 +61,8 @@ class Slide extends Entity
|
||||
self::TYPE_OPEN => [],
|
||||
self::TYPE_SCALE => ['scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => ''],
|
||||
self::TYPE_CONTENT => [],
|
||||
// time_limit in seconds, 0 = no limit (presenter ends the quiz)
|
||||
self::TYPE_QUIZ => ['time_limit' => 30],
|
||||
];
|
||||
|
||||
/**
|
||||
@@ -66,6 +86,7 @@ class Slide extends Entity
|
||||
self::TYPE_WORDCLOUD => __('Wortwolke'),
|
||||
self::TYPE_OPEN => __('Offene Frage'),
|
||||
self::TYPE_SCALE => __('Skala'),
|
||||
self::TYPE_QUIZ => __('Quiz'),
|
||||
self::TYPE_CONTENT => __('Inhaltsfolie'),
|
||||
];
|
||||
}
|
||||
@@ -80,6 +101,7 @@ class Slide extends Entity
|
||||
self::TYPE_WORDCLOUD => __('Teilnehmende geben Begriffe ein oder unterstützen vorhandene, häufige werden größer.'),
|
||||
self::TYPE_OPEN => __('Freitext-Antworten, live als Liste.'),
|
||||
self::TYPE_SCALE => __('Bewertung auf einer Skala, z. B. 1 bis 5.'),
|
||||
self::TYPE_QUIZ => __('Frage mit richtiger Antwort und Zeitlimit, aufgelöst wird erst am Ende.'),
|
||||
self::TYPE_CONTENT => __('Nur Text anzeigen, ohne Abstimmung.'),
|
||||
];
|
||||
}
|
||||
@@ -102,6 +124,95 @@ class Slide extends Entity
|
||||
return $this->type !== self::TYPE_CONTENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether answers are accepted right now: voting is open and the deadline
|
||||
* of a timed quiz has not passed.
|
||||
*
|
||||
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
|
||||
* @return bool
|
||||
*/
|
||||
public function isVotingOpen(?DateTime $now = null): bool
|
||||
{
|
||||
if (!$this->is_voting_open) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->voting_ends_at === null || ($now ?? DateTime::now())->lessThan($this->voting_ends_at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a timed quiz is still flagged open although its deadline has passed.
|
||||
*
|
||||
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired(?DateTime $now = null): bool
|
||||
{
|
||||
return $this->is_voting_open && !$this->isVotingOpen($now);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isQuiz(): bool
|
||||
{
|
||||
return $this->type === self::TYPE_QUIZ;
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase of a quiz: ready (not started yet), running, finished (time up or
|
||||
* closed by the presenter). Results and correct answers are only revealed
|
||||
* when finished.
|
||||
*
|
||||
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
|
||||
* @return string One of the QUIZ_* constants
|
||||
*/
|
||||
public function quizPhase(?DateTime $now = null): string
|
||||
{
|
||||
if ($this->isVotingOpen($now)) {
|
||||
return self::QUIZ_RUNNING;
|
||||
}
|
||||
|
||||
return $this->voting_started_at === null ? self::QUIZ_READY : self::QUIZ_FINISHED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seconds left of a running timed quiz, null without time limit.
|
||||
*
|
||||
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
|
||||
* @return int|null
|
||||
*/
|
||||
public function remainingSeconds(?DateTime $now = null): ?int
|
||||
{
|
||||
if ($this->voting_ends_at === null) {
|
||||
return null;
|
||||
}
|
||||
if (!$this->isVotingOpen($now)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return max(0, $this->voting_ends_at->getTimestamp() - ($now ?? DateTime::now())->getTimestamp());
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens or closes voting. Opening a quiz that is not running (re)starts its
|
||||
* voting window and timer; opening a running quiz changes nothing.
|
||||
*
|
||||
* @param bool $open Open or close
|
||||
* @param \Cake\I18n\DateTime|null $now Reference time (default: now)
|
||||
* @return void
|
||||
*/
|
||||
public function setVotingOpen(bool $open, ?DateTime $now = null): void
|
||||
{
|
||||
$now ??= DateTime::now();
|
||||
if ($open && $this->isQuiz() && !$this->isVotingOpen($now)) {
|
||||
$limit = (int)$this->setting('time_limit');
|
||||
$this->voting_started_at = $now;
|
||||
$this->voting_ends_at = $limit > 0 ? $now->addSeconds($limit) : null;
|
||||
}
|
||||
$this->is_voting_open = $open;
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings merged with the type defaults.
|
||||
*
|
||||
|
||||
@@ -10,6 +10,7 @@ use Cake\ORM\Entity;
|
||||
* @property int $slide_id
|
||||
* @property string $label
|
||||
* @property int $position
|
||||
* @property bool $is_correct Quiz: correct answer (never expose before the quiz is finished)
|
||||
*/
|
||||
class SlideOption extends Entity
|
||||
{
|
||||
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
];
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in new issue
Block a user