Add initial Plenario live polling app
CakePHP 5.4 app for live polls: create via email without login, manage via secret admin link, join via code. Includes slide types (choice, wordcloud, open, scale, content), presenter mode, CSV export, captcha, throttling, tests and Gitea CI/deploy workflow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
eb319f8c1e
120 files changed
+15572
No files matched your search
@@ -0,0 +1,185 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\ResponsesTable;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
|
||||
/**
|
||||
* Builds the JSON state polled by the presenter screen (/admin/{token}/state)
|
||||
* and the participant page (/p/{code}/state).
|
||||
*
|
||||
* The participant state never contains results unless the poll allows it and
|
||||
* the participant has answered (or voting is closed). Exception: word clouds
|
||||
* with upvotes enabled show the live cloud while voting is open, because
|
||||
* participants upvote by clicking words in it.
|
||||
*/
|
||||
class PollStateService
|
||||
{
|
||||
use LocatorAwareTrait;
|
||||
|
||||
protected SlidesTable $Slides;
|
||||
|
||||
protected ResponsesTable $Responses;
|
||||
|
||||
protected ResultsService $results;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
/** @var \App\Model\Table\SlidesTable $slides */
|
||||
$slides = $this->fetchTable('Slides');
|
||||
$this->Slides = $slides;
|
||||
/** @var \App\Model\Table\ResponsesTable $responses */
|
||||
$responses = $this->fetchTable('Responses');
|
||||
$this->Responses = $responses;
|
||||
$this->results = new ResultsService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return \App\Model\Entity\Slide|null
|
||||
*/
|
||||
public function currentSlide(Poll $poll): ?Slide
|
||||
{
|
||||
if ($poll->current_slide_id === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var \App\Model\Entity\Slide|null $slide */
|
||||
$slide = $this->Slides->find()
|
||||
->where(['Slides.id' => $poll->current_slide_id, 'Slides.poll_id' => $poll->id])
|
||||
->contain(['SlideOptions'])
|
||||
->first();
|
||||
|
||||
return $slide;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forPresenter(Poll $poll): array
|
||||
{
|
||||
$slides = $this->Slides->listForPoll($poll->id);
|
||||
$current = null;
|
||||
$index = null;
|
||||
foreach ($slides as $i => $slide) {
|
||||
if ($slide->id === $poll->current_slide_id) {
|
||||
$current = $slide;
|
||||
$index = $i;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'poll' => [
|
||||
'title' => $poll->title,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
],
|
||||
'slides' => array_map(fn(Slide $s) => [
|
||||
'id' => $s->id,
|
||||
'type' => $s->type,
|
||||
'question' => $s->question,
|
||||
], $slides),
|
||||
'index' => $index,
|
||||
'count' => count($slides),
|
||||
'slide' => $current ? $this->slidePayload($current) : null,
|
||||
'results' => $current ? $this->results->forSlide($current) : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param string $participantHash Participant hash
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forParticipant(Poll $poll, string $participantHash): array
|
||||
{
|
||||
$slide = $poll->is_active ? $this->currentSlide($poll) : null;
|
||||
$words = $slide?->type === Slide::TYPE_WORDCLOUD ? $this->wordState($slide, $participantHash) : null;
|
||||
$hasResponded = $words !== null
|
||||
? $words['mine'] !== [] || $words['down'] !== []
|
||||
: $slide && $slide->isInteractive() && $this->Responses->hasResponded($slide->id, $participantHash);
|
||||
|
||||
$showResults = $slide !== null
|
||||
&& $slide->isInteractive()
|
||||
&& (
|
||||
($poll->show_results_to_participants && ($hasResponded || !$slide->is_voting_open))
|
||||
|| ($words !== null && $words['allowUpvotes'] && $slide->is_voting_open)
|
||||
);
|
||||
|
||||
return [
|
||||
'isActive' => $poll->is_active,
|
||||
'slideId' => $slide?->id,
|
||||
'isVotingOpen' => $slide?->is_voting_open ?? false,
|
||||
'hasResponded' => $hasResponded,
|
||||
// Changes whenever the participant page must re-render.
|
||||
'version' => implode(':', [
|
||||
(int)$poll->is_active,
|
||||
$slide?->id ?? 0,
|
||||
(int)($slide?->is_voting_open ?? 0),
|
||||
// Word clouds are answered word by word without reload.
|
||||
$words !== null ? (int)$words['limitReached'] : (int)$hasResponded,
|
||||
$slide?->modified?->toUnixString() ?? 0,
|
||||
]),
|
||||
'slide' => $showResults ? $this->slidePayload($slide) : null,
|
||||
'results' => $showResults ? $this->results->forSlide($slide) : null,
|
||||
'words' => $words,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Word cloud progress of one participant.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Word cloud slide
|
||||
* @param string $participantHash Participant hash
|
||||
* @return array{mine: list<string>, down: list<string>, max: int, remaining: int|null,
|
||||
* limitReached: bool, allowUpvotes: bool, allowDownvotes: bool}
|
||||
* `mine` (named/upvoted) and `down` are lower case word keys, `remaining` is null when unlimited
|
||||
*/
|
||||
public function wordState(Slide $slide, string $participantHash): array
|
||||
{
|
||||
$votes = $this->Responses->wordVotesOf($slide->id, $participantHash);
|
||||
$mine = array_values(array_unique(array_map(VoteService::wordKey(...), $votes['up'])));
|
||||
$down = array_values(array_unique(array_map(VoteService::wordKey(...), $votes['down'])));
|
||||
$max = (int)$slide->setting('max_words');
|
||||
$remaining = $max > 0 ? max(0, $max - count($mine)) : null;
|
||||
$allowUpvotes = (bool)$slide->setting('allow_upvotes');
|
||||
|
||||
return [
|
||||
'mine' => $mine,
|
||||
'down' => $down,
|
||||
'max' => $max,
|
||||
'remaining' => $remaining,
|
||||
'limitReached' => $remaining === 0,
|
||||
'allowUpvotes' => $allowUpvotes,
|
||||
'allowDownvotes' => $allowUpvotes && (bool)$slide->setting('allow_downvotes'),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function slidePayload(Slide $slide): array
|
||||
{
|
||||
return [
|
||||
'id' => $slide->id,
|
||||
'type' => $slide->type,
|
||||
'typeLabel' => $slide->typeLabel(),
|
||||
'question' => $slide->question,
|
||||
'description' => (string)$slide->description,
|
||||
'isVotingOpen' => $slide->is_voting_open,
|
||||
'isInteractive' => $slide->isInteractive(),
|
||||
'settings' => $slide->getSettings(),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user