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,139 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model\Table;
|
||||
|
||||
use App\Service\VoteService;
|
||||
use Cake\ORM\Table;
|
||||
|
||||
/**
|
||||
* @property \App\Model\Table\SlidesTable&\Cake\ORM\Association\BelongsTo $Slides
|
||||
* @property \App\Model\Table\SlideOptionsTable&\Cake\ORM\Association\BelongsTo $SlideOptions
|
||||
* @method \App\Model\Entity\Response newEntity(array $data, array $options = [])
|
||||
*/
|
||||
class ResponsesTable extends Table
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $config The configuration for the Table.
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config);
|
||||
|
||||
$this->setTable('responses');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->addBehavior('Timestamp', [
|
||||
'events' => ['Model.beforeSave' => ['created' => 'new']],
|
||||
]);
|
||||
|
||||
$this->belongsTo('Slides', ['foreignKey' => 'slide_id', 'joinType' => 'INNER']);
|
||||
$this->belongsTo('SlideOptions', ['foreignKey' => 'slide_option_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $slideId Slide id
|
||||
* @param string $participantHash Participant hash
|
||||
* @return bool
|
||||
*/
|
||||
public function hasResponded(int $slideId, string $participantHash): bool
|
||||
{
|
||||
return $this->exists(['slide_id' => $slideId, 'participant_hash' => $participantHash]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Word cloud terms a participant named/upvoted and downvoted on a slide.
|
||||
*
|
||||
* @param int $slideId Slide id
|
||||
* @param string $participantHash Participant hash
|
||||
* @return array{up: list<string>, down: list<string>} raw text values
|
||||
*/
|
||||
public function wordVotesOf(int $slideId, string $participantHash): array
|
||||
{
|
||||
$rows = $this->find()
|
||||
->select(['text_value', 'numeric_value'])
|
||||
->where(['slide_id' => $slideId, 'participant_hash' => $participantHash, 'text_value IS NOT' => null])
|
||||
->orderBy(['id' => 'ASC'])
|
||||
->disableHydration()
|
||||
->all();
|
||||
|
||||
$votes = ['up' => [], 'down' => []];
|
||||
foreach ($rows as $row) {
|
||||
$votes[(int)$row['numeric_value'] === VoteService::DOWNVOTE ? 'down' : 'up'][] = (string)$row['text_value'];
|
||||
}
|
||||
|
||||
return $votes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether anybody named (or upvoted) a word cloud term on a slide.
|
||||
*
|
||||
* @param int $slideId Slide id
|
||||
* @param string $key Word key, see VoteService::wordKey()
|
||||
* @return bool
|
||||
*/
|
||||
public function wordExists(int $slideId, string $key): bool
|
||||
{
|
||||
$rows = $this->find()
|
||||
->select(['text_value'])
|
||||
->where([
|
||||
'slide_id' => $slideId,
|
||||
'text_value IS NOT' => null,
|
||||
'numeric_value IS' => null,
|
||||
])
|
||||
->disableHydration()
|
||||
->all();
|
||||
foreach ($rows as $row) {
|
||||
if (VoteService::wordKey((string)$row['text_value']) === $key) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of distinct participants that answered a slide.
|
||||
*
|
||||
* @param int $slideId Slide id
|
||||
* @return int
|
||||
*/
|
||||
public function countParticipants(int $slideId): int
|
||||
{
|
||||
$row = $this->find()
|
||||
->select(['n' => $this->find()->func()->count('DISTINCT participant_hash')])
|
||||
->where(['slide_id' => $slideId])
|
||||
->disableHydration()
|
||||
->first();
|
||||
|
||||
return (int)($row['n'] ?? 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Number of responding participants per slide of a poll.
|
||||
*
|
||||
* @param int $pollId Poll id
|
||||
* @return array<int, int> slide_id => participant count
|
||||
*/
|
||||
public function countParticipantsPerSlide(int $pollId): array
|
||||
{
|
||||
$rows = $this->find()
|
||||
->select([
|
||||
'slide_id' => 'Responses.slide_id',
|
||||
'n' => $this->find()->func()->count('DISTINCT Responses.participant_hash'),
|
||||
])
|
||||
->innerJoinWith('Slides')
|
||||
->where(['Slides.poll_id' => $pollId])
|
||||
->groupBy(['Responses.slide_id'])
|
||||
->disableHydration()
|
||||
->all();
|
||||
|
||||
$counts = [];
|
||||
foreach ($rows as $row) {
|
||||
$counts[(int)$row['slide_id']] = (int)$row['n'];
|
||||
}
|
||||
|
||||
return $counts;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user