Files
Plenario/src/Service/SlideControlService.php
T
gnilebeinandClaude Opus 5.5 07a292d109
CI / Deploy / test (push) Successful in 1m4s
CI / Deploy / deploy (push) Skipped
Add embeddable slides and pause mode
Every slide gets an embed URL /e/{key}/{id} for presentation software
like Canva. The page goes live while it is visible, keeps itself live
with heartbeats and pauses when it disappears; stale embeds are paused
lazily. The embed key is separate from the admin token and only allows
showing, activating, pausing and voting controls of that slide.

Participants see a pause page while no slide is live. The presenter
can pause (P) and resume the paused slide with the arrow keys; the
dashboard has a pause button.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 16:26:05 +01:00

91 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Service;
use App\Model\Entity\Slide;
use App\Model\Table\SlidesTable;
use Cake\Http\Exception\BadRequestException;
use Cake\ORM\Locator\LocatorAwareTrait;
/**
* Presenter actions on a single slide, shared by the presenter screen
* (AdminController::control) and embedded slides (EmbedController::control).
*
* Actions: open, close, toggle, Q&A: stage (+ stage: collect|vote|discuss), question (+ step: 1|-1).
* On Q&A slides `toggle` switches to the next stage (no-op while discussing).
*/
class SlideControlService
{
use LocatorAwareTrait;
public const ACTIONS = ['open', 'close', 'toggle', 'stage', 'question'];
protected SlidesTable $Slides;
/**
* Constructor
*/
public function __construct()
{
/** @var \App\Model\Table\SlidesTable $slides */
$slides = $this->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<string, mixed> $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);
}
}