Add initial Plenario live polling app
CI / Deploy / test (push) Successful in 49s
CI / Deploy / deploy (push) Failing after 5s

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:
gnilebeinandClaude Opus 5.5 committed 2026-11-07 13:24:29 +01:00
commit eb319f8c1e
120 files changed
+15572

No files matched your search

+168
View File
@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Model\Entity\Poll;
use App\Model\Table\PollsTable;
use App\Service\PollStateService;
use App\Service\VoteException;
use App\Service\VoteService;
use Cake\Core\Configure;
use Cake\Event\EventInterface;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Exception\NotFoundException;
use Cake\Http\Response;
use Cake\I18n\DateTime;
/**
* Participant pages under /p/{code}.
*
* Participants are anonymous. A random id is stored in the cookie
* `plenario_pid`; only an HMAC of it (scoped per poll) ends up in the database.
*/
class ParticipateController extends AppController
{
public const COOKIE = 'plenario_pid';
protected PollsTable $Polls;
protected string $participantId;
protected bool $newParticipant = false;
/**
* @return void
*/
public function initialize(): void
{
parent::initialize();
/** @var \App\Model\Table\PollsTable $polls */
$polls = $this->fetchTable('Polls');
$this->Polls = $polls;
}
/**
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
* @return void
*/
public function beforeFilter(EventInterface $event): void
{
parent::beforeFilter($event);
$pid = (string)$this->request->getCookie(self::COOKIE, '');
if (!preg_match('/^[a-f0-9]{32}$/', $pid)) {
$pid = bin2hex(random_bytes(16));
$this->newParticipant = true;
}
$this->participantId = $pid;
}
/**
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
* @return void
*/
public function afterFilter(EventInterface $event): void
{
parent::afterFilter($event);
if ($this->newParticipant) {
$this->response = $this->response->withCookie(Cookie::create(self::COOKIE, $this->participantId, [
'expires' => new DateTime('+1 year'),
'path' => '/',
'httponly' => true,
'secure' => $this->request->is('https'),
'samesite' => 'Lax',
]));
}
}
/**
* @param string $code Join code
* @return void
*/
public function view(string $code): void
{
$poll = $this->loadPoll($code);
$stateService = new PollStateService();
$slide = $poll->is_active ? $stateService->currentSlide($poll) : null;
$state = $stateService->forParticipant($poll, $this->participantHash($poll));
$this->set(compact('poll', 'slide', 'state'));
$this->set('pollIntervalMs', (int)Configure::read('Plenario.pollIntervalMs', 2000));
}
/**
* @param string $code Join code
* @return \Cake\Http\Response
*/
public function state(string $code): Response
{
$poll = $this->loadPoll($code);
return $this->json((new PollStateService())->forParticipant($poll, $this->participantHash($poll)));
}
/**
* Stores an answer. AJAX requests (word cloud entries and upvotes) get JSON
* `{ok, message, state}` instead of a redirect with flash message.
*
* @param string $code Join code
* @return \Cake\Http\Response|null
*/
public function vote(string $code): ?Response
{
$this->request->allowMethod(['post']);
$poll = $this->loadPoll($code);
$stateService = new PollStateService();
$slide = $stateService->currentSlide($poll);
$submittedSlideId = (int)$this->request->getData('slide_id');
$ajax = $this->request->is('ajax');
try {
if ($slide === null || $slide->id !== $submittedSlideId) {
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
}
(new VoteService())->submit($poll, $slide, $this->participantHash($poll), (array)$this->request->getData());
$ok = true;
$message = __('Danke für deine Antwort!');
} catch (VoteException $e) {
$ok = false;
$message = $e->getMessage();
}
if ($ajax) {
return $this->json([
'ok' => $ok,
'message' => $message,
'state' => $stateService->forParticipant($poll, $this->participantHash($poll)),
]);
}
$ok ? $this->Flash->success($message) : $this->Flash->error($message);
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
}
/**
* @param string $code Join code
* @return \App\Model\Entity\Poll
*/
protected function loadPoll(string $code): Poll
{
/** @var \App\Model\Entity\Poll|null $poll */
$poll = $this->Polls->find('byJoinCode', code: $code)->first();
if ($poll === null) {
throw new NotFoundException(__('Zu diesem Code wurde keine Umfrage gefunden.'));
}
return $poll;
}
/**
* @param \App\Model\Entity\Poll $poll Poll
* @return string
*/
protected function participantHash(Poll $poll): string
{
return VoteService::participantHash($this->participantId, $poll->id);
}
}