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>
This commit is contained in:
1 parent
58145e9753
commit
07a292d109
19 files changed
+1116
-68
No files matched your search
@@ -5,6 +5,7 @@ namespace App\Service;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\PollsTable;
|
||||
use App\Model\Table\ResponsesTable;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
@@ -33,6 +34,8 @@ class PollStateService
|
||||
|
||||
protected ResponsesTable $Responses;
|
||||
|
||||
protected PollsTable $Polls;
|
||||
|
||||
protected ResultsService $results;
|
||||
|
||||
/**
|
||||
@@ -46,6 +49,9 @@ class PollStateService
|
||||
/** @var \App\Model\Table\ResponsesTable $responses */
|
||||
$responses = $this->fetchTable('Responses');
|
||||
$this->Responses = $responses;
|
||||
/** @var \App\Model\Table\PollsTable $polls */
|
||||
$polls = $this->fetchTable('Polls');
|
||||
$this->Polls = $polls;
|
||||
$this->results = new ResultsService();
|
||||
}
|
||||
|
||||
@@ -55,6 +61,7 @@ class PollStateService
|
||||
*/
|
||||
public function currentSlide(Poll $poll): ?Slide
|
||||
{
|
||||
$this->Polls->pauseIfEmbedStale($poll);
|
||||
if ($poll->current_slide_id === null) {
|
||||
return null;
|
||||
}
|
||||
@@ -74,6 +81,7 @@ class PollStateService
|
||||
*/
|
||||
public function forPresenter(Poll $poll): array
|
||||
{
|
||||
$this->Polls->pauseIfEmbedStale($poll);
|
||||
$slides = $this->Slides->listForPoll($poll->id);
|
||||
$current = null;
|
||||
$index = null;
|
||||
@@ -90,6 +98,7 @@ class PollStateService
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $poll->isPaused(),
|
||||
],
|
||||
'slides' => array_map(fn(Slide $s) => [
|
||||
'id' => $s->id,
|
||||
@@ -103,6 +112,35 @@ class PollStateService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* State of one embedded slide (/e/{key}/{slide}/state). Same shape as forPresenter(),
|
||||
* but always for this slide, live or not, plus `isLive`. Never contains the slide list.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param \App\Model\Entity\Slide $slide Slide of this poll incl. slide_options
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forEmbed(Poll $poll, Slide $slide): array
|
||||
{
|
||||
$this->Polls->pauseIfEmbedStale($poll);
|
||||
$slide = $this->Slides->closeIfExpired($slide);
|
||||
|
||||
return [
|
||||
'poll' => [
|
||||
'title' => $poll->title,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $poll->isPaused(),
|
||||
],
|
||||
'isLive' => $poll->current_slide_id === $slide->id,
|
||||
'index' => null,
|
||||
'count' => null,
|
||||
'slide' => $this->slidePayload($slide),
|
||||
'results' => $this->results->forSlide($slide),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param string $participantHash Participant hash
|
||||
@@ -131,6 +169,7 @@ class PollStateService
|
||||
|
||||
return [
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $slide === null && $poll->isPaused(),
|
||||
'slideId' => $slide?->id,
|
||||
'isVotingOpen' => $isVotingOpen,
|
||||
'hasResponded' => $hasResponded,
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user