fetchTable('Slides'); $this->Slides = $slides; } /** * @param \App\Model\Entity\Slide $slide Slide (belongs to the poll, checked by the caller) * @param string $action One of self::ACTIONS * @param array $data Request data (stage, step) * @return void * @throws \Cake\Http\Exception\BadRequestException */ public function apply(Slide $slide, string $action, array $data = []): void { $qa = new QaService(); switch ($action) { case 'stage': case 'question': if (!$slide->isQa()) { throw new BadRequestException('Not a Q&A slide'); } if ($action === 'question') { $qa->moveFocus($slide, (int)($data['step'] ?? 1) < 0 ? -1 : 1); break; } $stage = (string)($data['stage'] ?? ''); if (!in_array($stage, Slide::QA_STAGES, true)) { throw new BadRequestException('Unknown stage'); } $qa->setStage($slide, $stage); break; case 'toggle': if ($slide->isQa()) { $qa->advance($slide); break; } $this->setVotingOpen($slide, !$slide->isVotingOpen()); break; case 'open': case 'close': $this->setVotingOpen($slide, $action === 'open'); break; default: throw new BadRequestException('Unknown action'); } } /** * Opening a quiz (re)starts its timer, see Slide::setVotingOpen(). * * @param \App\Model\Entity\Slide $slide Slide * @param bool $open Open or close * @return void */ protected function setVotingOpen(Slide $slide, bool $open): void { $slide->setVotingOpen($open); $this->Slides->saveOrFail($slide); } }