Merge pull request 'Reset poll to "not started" and keep prepared Q&A questions' (#75) from feature/reset-not-started into main
CI / Deploy / PHP 8.5 (push) Successful in 1m4s
CI / Deploy / PHP 8.4 (push) Successful in 1m5s
CI / Deploy / test (push) Successful in 2s
CI / Deploy / deploy (push) Successful in 11s

Reviewed-on: #75
This commit was merged in pull request #75.
This commit is contained in:
gnilebein committed 2026-11-13 10:03:49 +01:00
commit 3c66597696
7 files changed
+119 -22

No files matched your search

+8 -4
View File
@@ -59,7 +59,9 @@ class AdminController extends AdminAppController
// Created on first visit, so every slide has its embed URL ready to copy.
$embedKey = $this->Polls->ensureEmbedKey($this->poll);
$this->set(compact('slides', 'responseCounts', 'typeLabels', 'typeDescriptions', 'joinUrl', 'embedKey'));
$hasPrepared = $this->Responses->hasPrepared($this->poll->id);
$this->set(compact('slides', 'responseCounts', 'hasPrepared', 'typeLabels', 'typeDescriptions', 'joinUrl', 'embedKey'));
}
/**
@@ -117,15 +119,17 @@ class AdminController extends AdminAppController
}
/**
* Deletes all responses of all slides; slides, settings and the presentation are kept
* (e.g. to run the poll with another group).
* Deletes all responses of all slides and puts the presentation back to "not started"; slides and settings
* are kept (e.g. to run the poll with another group). Prepared Q&A questions stay unless
* `keep_prepared` is sent as 0.
*
* @return \Cake\Http\Response|null
*/
public function reset(): ?Response
{
$this->request->allowMethod(['post']);
(new ResetService())->poll($this->poll);
$keepPrepared = $this->request->getData('keep_prepared', '1') !== '0';
(new ResetService())->poll($this->poll, $keepPrepared);
$this->Flash->success(__('Alle Antworten wurden gelöscht.'));
return $this->redirect($this->adminUrl());
+12
View File
@@ -109,6 +109,18 @@ class Poll extends Entity
$this->embed_seen_at = null;
}
/**
* Back to "not started": neither live nor paused. Idempotent.
*
* @return void
*/
public function notStarted(): void
{
$this->current_slide_id = null;
$this->paused_slide_id = null;
$this->embed_seen_at = null;
}
/**
* A slide leaves the presentation (deleted or hidden). If it was live or is the one a pause
* resumes, the poll pauses and resumes with $resumeSlideId instead (null: no slides left =
+12
View File
@@ -172,6 +172,18 @@ class ResponsesTable extends Table
return (int)($row['n'] ?? 0);
}
/**
* @param int $pollId Poll id
* @return bool Whether the poll has Q&A questions entered beforehand
*/
public function hasPrepared(int $pollId): bool
{
return $this->find()
->innerJoinWith('Slides')
->where(['Slides.poll_id' => $pollId, 'Responses.participant_hash' => VoteService::PREPARED_HASH])
->count() > 0;
}
/**
* Number of responding participants per slide of a poll.
*
+28 -11
View File
@@ -10,9 +10,9 @@ use App\Model\Table\SlidesTable;
use Cake\ORM\Locator\LocatorAwareTrait;
/**
* Deletes responses of a slide or of the whole poll. Everything else (slides, settings,
* the presentation's current slide) stays; only quiz and Q&A slides go back to their
* start, since their phases make no sense without answers.
* 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,
* and resetting the poll puts the presentation back to "not started".
* Idempotent: resetting twice gives the same result.
*/
class ResetService
@@ -40,13 +40,21 @@ class ResetService
* Deletes all responses of a slide. A quiz goes back to "not started"
* (so it can be played again), a Q&A slide back to collecting questions.
*
* Questions the admin entered beforehand (Q&A) are kept unless $keepPrepared is false;
* only the upvotes on them are removed.
*
* @param \App\Model\Entity\Slide $slide Slide
* @param bool $keepPrepared Keep prepared Q&A questions
* @return void
*/
public function slide(Slide $slide): void
public function slide(Slide $slide, bool $keepPrepared = true): void
{
$this->Slides->getConnection()->transactional(function () use ($slide): void {
$this->Responses->deleteAll(['slide_id' => $slide->id]);
$this->Slides->getConnection()->transactional(function () use ($slide, $keepPrepared): void {
$conditions = ['slide_id' => $slide->id];
if ($keepPrepared) {
$conditions['participant_hash !='] = VoteService::PREPARED_HASH;
}
$this->Responses->deleteAll($conditions);
if ($slide->isQa()) {
(new QaService())->reset($slide);
@@ -62,19 +70,28 @@ class ResetService
}
/**
* Deletes the responses of all slides of the poll (see slide()).
* The presentation stays where it is.
* Deletes the responses of all slides of the poll (see slide()) and puts the
* presentation back to "not started" (no live and no paused slide). Idempotent.
*
* @param \App\Model\Entity\Poll $poll Poll
* @param bool $keepPrepared Keep prepared Q&A questions
* @return void
*/
public function poll(Poll $poll): void
public function poll(Poll $poll, bool $keepPrepared = true): void
{
$this->Slides->getConnection()->transactional(function () use ($poll): void {
$this->Slides->getConnection()->transactional(function () use ($poll, $keepPrepared): void {
foreach ($this->Slides->find()->where(['poll_id' => $poll->id])->all() as $slide) {
/** @var \App\Model\Entity\Slide $slide */
$this->slide($slide);
$this->slide($slide, $keepPrepared);
}
if ($poll->current_slide_id === null && $poll->paused_slide_id === null) {
return;
}
$poll->notStarted();
/** @var \App\Model\Table\PollsTable $polls */
$polls = $this->fetchTable('Polls');
$polls->saveOrFail($poll);
});
}
}