Feature/clicker quiz and reset #76

Merged
gnilebein merged 2 commits from feature/clicker-quiz-and-reset into main 2026-11-13 10:22:51 +01:00
5 changed files with 120 additions and 19 deletions

No files matched your search

+5 -2
View File
@@ -145,6 +145,9 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures
`SlidesTable::closeIfExpired()` beim State-Abruf. **Zählungen und `is_correct` dürfen vor `finished` nie im `SlidesTable::closeIfExpired()` beim State-Abruf. **Zählungen und `is_correct` dürfen vor `finished` nie im
JSON/HTML landen** (weder Presenter noch Teilnehmende); danach sehen Teilnehmende die Lösung und ob sie JSON/HTML landen** (weder Presenter noch Teilnehmende); danach sehen Teilnehmende die Lösung und ob sie
richtig lagen (ohne Verteilung). Zurücksetzen setzt das Quiz wieder auf `ready`. richtig lagen (ohne Verteilung). Zurücksetzen setzt das Quiz wieder auf `ready`.
**Klicker/Presenter-Stick** (sendet nur →/← bzw. Bild↑/↓): `AdminController::control` behandelt `next` auf einem Quiz als
Schritt – `ready` → starten, `running` → beenden (Lösung), erst `finished` → nächste Folie. `prev` geht immer zur vorigen Folie
und startet nie ein Quiz. Tests: `QuizClickerTest.php`.
- `Poll`/`Response` haben `_hidden` für sensible Felder; mass assignment über `_accessible` restriktiv halten. - `Poll`/`Response` haben `_hidden` für sensible Felder; mass assignment über `_accessible` restriktiv halten.
- Alle POST-Formulare laufen über CSRF (`CsrfProtectionMiddleware`). JS sendet `X-CSRF-Token` aus `<meta name="csrf-token">`. - Alle POST-Formulare laufen über CSRF (`CsrfProtectionMiddleware`). JS sendet `X-CSRF-Token` aus `<meta name="csrf-token">`.
- Ausgaben in Templates immer mit `h()` escapen. JS baut DOM mit `textContent`, nie `innerHTML` mit Nutzerdaten. - Ausgaben in Templates immer mit `h()` escapen. JS baut DOM mit `textContent`, nie `innerHTML` mit Nutzerdaten.
@@ -202,8 +205,8 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures
Teilnehmende nur die Lösung mit eigenen Platzierungen und `order.isCorrect`. Tests: `OrderTest.php`, `OrderSlideTest.php`. Teilnehmende nur die Lösung mit eigenen Platzierungen und `order.isCorrect`. Tests: `OrderTest.php`, `OrderSlideTest.php`.
- **Zurücksetzen** (`ResetService`) löscht **nur Antworten**: Folie (`slides:reset`) bzw. alle Folien der Umfrage - **Zurücksetzen** (`ResetService`) löscht **nur Antworten**: Folie (`slides:reset`) bzw. alle Folien der Umfrage
(`admin:reset`, Dashboard → „Sicherheit“). Nur Quiz (`ready`) und Q&A (`collect`) springen zurück, weil ihre Phasen (`admin:reset`, Dashboard → „Sicherheit“). Jede Folie geht danach in den Zustand einer neuen Folie (`Slide::startFresh()`:
ohne Antworten keinen Sinn ergeben; offen/geschlossen anderer Folien und Einstellungen bleiben. Das Zurücksetzen der Quiz `ready`, Q&A `collect`, alle anderen offen – bei Reihenfolge bleibt die Lösung so verdeckt); Einstellungen bleiben. Das Zurücksetzen der
Umfrage setzt außerdem die Präsentation auf „nicht gestartet“ (`Poll::notStarted()`: weder live noch pausiert). Vorab eingetragene Q&A-Fragen bleiben standardmäßig erhalten (nur ihre Upvotes gehen weg): Umfrage setzt außerdem die Präsentation auf „nicht gestartet“ (`Poll::notStarted()`: weder live noch pausiert). Vorab eingetragene Q&A-Fragen bleiben standardmäßig erhalten (nur ihre Upvotes gehen weg):
Folien-Reset behält sie immer, der Umfrage-Reset hat die Checkbox `keep_prepared` (nur sichtbar, wenn es welche gibt). Folien-Reset behält sie immer, der Umfrage-Reset hat die Checkbox `keep_prepared` (nur sichtbar, wenn es welche gibt).
Idempotent, in einer Transaktion. Tests: `PollResetTest.php`. Idempotent, in einer Transaktion. Tests: `PollResetTest.php`.
+11 -1
View File
@@ -196,7 +196,8 @@ class AdminController extends AdminAppController
* SlideControlService (open, close, toggle, stage, question). * SlideControlService (open, close, toggle, stage, question).
* *
* On a Q&A slide that is being discussed, next/prev step through the questions * On a Q&A slide that is being discussed, next/prev step through the questions
* first (like builds in presentation software). While paused, next/prev resume * first (like builds in presentation software); on a quiz, next starts it and then ends it
* before moving on, prev always goes to the previous slide. While paused, next/prev resume
* the slide that was live before. * the slide that was live before.
* *
* Answers with the new state as JSON for XHR requests, otherwise redirects back. * Answers with the new state as JSON for XHR requests, otherwise redirects back.
@@ -220,6 +221,15 @@ class AdminController extends AdminAppController
) { ) {
$action = 'none'; $action = 'none';
} }
// Forward on a quiz is a build step too: start it, then end it, only then the next slide.
// Back never touches the quiz, so a clicker can page through the slides afterwards.
if ($current !== null && $current->isQuiz() && $action === 'next') {
$phase = $current->quizPhase();
if ($phase !== Slide::QUIZ_FINISHED) {
(new SlideControlService())->apply($current, $phase === Slide::QUIZ_READY ? 'open' : 'close');
$action = 'none';
}
}
switch ($action) { switch ($action) {
case 'none': case 'none':
+5 -13
View File
@@ -11,7 +11,7 @@ use Cake\ORM\Locator\LocatorAwareTrait;
/** /**
* Deletes responses of a slide or of the whole poll. Everything else (slides, settings) stays; * Deletes responses of a slide or of the whole poll. Everything else (slides, settings) stays;
* quiz and Q&A slides go back to their start, since their phases make no sense without answers, * every slide goes back to the state of a new one (Slide::startFresh()),
* and resetting the poll puts the presentation back to "not started". * and resetting the poll puts the presentation back to "not started".
* Idempotent: resetting twice gives the same result. * Idempotent: resetting twice gives the same result.
*/ */
@@ -37,8 +37,8 @@ class ResetService
} }
/** /**
* Deletes all responses of a slide. A quiz goes back to "not started" * Deletes all responses of a slide and puts it back to the state of a new slide: a quiz is
* (so it can be played again), a Q&A slide back to collecting questions. * "not started" (so it can be played again), a Q&A slide collects questions, all others are open.
* *
* Questions the admin entered beforehand (Q&A) are kept unless $keepPrepared is false; * Questions the admin entered beforehand (Q&A) are kept unless $keepPrepared is false;
* only the upvotes on them are removed. * only the upvotes on them are removed.
@@ -55,17 +55,9 @@ class ResetService
$conditions['participant_hash !='] = VoteService::PREPARED_HASH; $conditions['participant_hash !='] = VoteService::PREPARED_HASH;
} }
$this->Responses->deleteAll($conditions); $this->Responses->deleteAll($conditions);
if ($slide->isQa()) { // Back to the state of a new slide: quiz ready, Q&A collecting, everything else open.
(new QaService())->reset($slide); $slide->startFresh();
return;
}
if ($slide->isQuiz()) {
$slide->is_voting_open = false;
$slide->voting_started_at = null;
$slide->voting_ends_at = null;
$this->Slides->saveOrFail($slide); $this->Slides->saveOrFail($slide);
}
}); });
} }
+4 -3
View File
@@ -52,9 +52,10 @@ class PollResetTest extends TestCase
$quiz->setVotingOpen(true); $quiz->setVotingOpen(true);
$quiz->is_voting_open = false; $quiz->is_voting_open = false;
$order->is_voting_open = false; $order->is_voting_open = false;
$choice->is_voting_open = false;
$qa->stage = Slide::QA_DISCUSS; $qa->stage = Slide::QA_DISCUSS;
$qa->is_voting_open = false; $qa->is_voting_open = false;
$this->slidesTable()->saveManyOrFail([$quiz, $order, $qa]); $this->slidesTable()->saveManyOrFail([$quiz, $order, $qa, $choice]);
$poll->showSlide($qa->id); $poll->showSlide($qa->id);
$poll->pause(); $poll->pause();
$this->pollsTable()->saveOrFail($poll); $this->pollsTable()->saveOrFail($poll);
@@ -75,10 +76,10 @@ class PollResetTest extends TestCase
$slides = $this->slidesTable(); $slides = $this->slidesTable();
$this->assertSame(Slide::QUIZ_READY, $slides->get($quiz->id)->quizPhase()); $this->assertSame(Slide::QUIZ_READY, $slides->get($quiz->id)->quizPhase());
$this->assertNull($slides->get($quiz->id)->voting_started_at); $this->assertNull($slides->get($quiz->id)->voting_started_at);
$this->assertFalse($slides->get($order->id)->is_voting_open, 'open/closed is kept'); $this->assertTrue($slides->get($order->id)->is_voting_open, 'ordering is open again, solution hidden');
$this->assertSame(Slide::QA_COLLECT, $slides->get($qa->id)->qaStage()); $this->assertSame(Slide::QA_COLLECT, $slides->get($qa->id)->qaStage());
$this->assertTrue($slides->get($qa->id)->is_voting_open); $this->assertTrue($slides->get($qa->id)->is_voting_open);
$this->assertTrue($slides->get($choice->id)->is_voting_open); $this->assertTrue($slides->get($choice->id)->is_voting_open, 'closed slides are open again');
// Idempotent: a second reset changes nothing and does not fail. // Idempotent: a second reset changes nothing and does not fail.
$this->post("/admin/$token/reset"); $this->post("/admin/$token/reset");
@@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace App\Test\TestCase\Controller;
use App\Model\Entity\Poll;
use App\Model\Entity\Slide;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Stepping through a presentation with a clicker (only next/prev): a quiz is started and ended before moving on.
*/
class QuizClickerTest extends TestCase
{
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
protected function setUp(): void
{
parent::setUp();
$this->enableCsrfToken();
}
/**
* @return array<string, mixed>
*/
protected function control(string $token, string $action): array
{
$this->configRequest(['headers' => ['Accept' => 'application/json']]);
$this->post("/admin/$token/control", ['action' => $action]);
$this->assertResponseOk();
return json_decode((string)$this->_response->getBody(), true);
}
protected function createQuiz(Poll $poll, array $data = []): Slide
{
$quiz = $this->createSlide($poll, Slide::TYPE_QUIZ, $data);
$quiz->startFresh();
return $this->slidesTable()->saveOrFail($quiz);
}
public function testNextStartsAndEndsQuizBeforeMovingOn(): void
{
[$poll, $token] = $this->createPoll();
$intro = $this->createSlide($poll, Slide::TYPE_CONTENT);
$quiz = $this->createQuiz($poll, ['settings' => ['time_limit' => 0] + Slide::DEFAULT_SETTINGS[Slide::TYPE_QUIZ]]);
$outro = $this->createSlide($poll, Slide::TYPE_CONTENT);
$this->goLive($poll, $intro);
// Arriving at the quiz does not start it.
$state = $this->control($token, 'next');
$this->assertSame($quiz->id, $state['slide']['id']);
$this->assertSame(Slide::QUIZ_READY, $this->slidesTable()->get($quiz->id)->quizPhase());
// Next: start. Next: end. Next: next slide.
$state = $this->control($token, 'next');
$this->assertSame($quiz->id, $state['slide']['id']);
$this->assertSame(Slide::QUIZ_RUNNING, $this->slidesTable()->get($quiz->id)->quizPhase());
$state = $this->control($token, 'next');
$this->assertSame($quiz->id, $state['slide']['id']);
$this->assertSame(Slide::QUIZ_FINISHED, $this->slidesTable()->get($quiz->id)->quizPhase());
$state = $this->control($token, 'next');
$this->assertSame($outro->id, $state['slide']['id']);
// Back never restarts the quiz: it is passed through and stays finished.
$state = $this->control($token, 'prev');
$this->assertSame($quiz->id, $state['slide']['id']);
$this->assertSame(Slide::QUIZ_FINISHED, $this->slidesTable()->get($quiz->id)->quizPhase());
$state = $this->control($token, 'prev');
$this->assertSame($intro->id, $state['slide']['id']);
}
public function testPrevOnUnstartedQuizJustGoesBack(): void
{
[$poll, $token] = $this->createPoll();
$intro = $this->createSlide($poll, Slide::TYPE_CONTENT);
$quiz = $this->createQuiz($poll);
$this->goLive($poll, $quiz);
$state = $this->control($token, 'prev');
$this->assertSame($intro->id, $state['slide']['id']);
$this->assertSame(Slide::QUIZ_READY, $this->slidesTable()->get($quiz->id)->quizPhase());
}
}