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;
|
||||
|
||||
Reference in new issue
Block a user