The slide form takes questions entered beforehand (e.g. received by mail), one per line, while collecting. They are stored as regular questions with a reserved participant hash, synced idempotently, and not counted as participants. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
202 lines
6.3 KiB
PHP
202 lines
6.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Model\Table;
|
|
|
|
use App\Service\VoteService;
|
|
use Cake\ORM\Query\SelectQuery;
|
|
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']);
|
|
}
|
|
|
|
/**
|
|
* Q&A questions of a slide (rows with text, without parent).
|
|
*
|
|
* @param int $slideId Slide id
|
|
* @return \Cake\ORM\Query\SelectQuery
|
|
*/
|
|
public function questionsOf(int $slideId): SelectQuery
|
|
{
|
|
return $this->find()->where([
|
|
'Responses.slide_id' => $slideId,
|
|
'Responses.parent_id IS' => null,
|
|
'Responses.text_value IS NOT' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Q&A progress of one participant on a slide.
|
|
*
|
|
* @param int $slideId Slide id
|
|
* @param string $participantHash Participant hash
|
|
* @return array{questions: list<int>, upvotes: list<int>} ids of own questions / question id of each own upvote (repeated per upvote)
|
|
*/
|
|
public function qaVotesOf(int $slideId, string $participantHash): array
|
|
{
|
|
$rows = $this->find()
|
|
->select(['id', 'parent_id'])
|
|
->where(['slide_id' => $slideId, 'participant_hash' => $participantHash])
|
|
->orderBy(['id' => 'ASC'])
|
|
->disableHydration()
|
|
->all();
|
|
|
|
$result = ['questions' => [], 'upvotes' => []];
|
|
foreach ($rows as $row) {
|
|
if ($row['parent_id'] === null) {
|
|
$result['questions'][] = (int)$row['id'];
|
|
} else {
|
|
$result['upvotes'][] = (int)$row['parent_id'];
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Upvotes per Q&A question of a slide.
|
|
*
|
|
* @param int $slideId Slide id
|
|
* @return array<int, int> question id => upvotes
|
|
*/
|
|
public function upvotesPerQuestion(int $slideId): array
|
|
{
|
|
return $this->find()
|
|
->select(['parent_id', 'n' => $this->find()->func()->count('*')])
|
|
->where(['slide_id' => $slideId, 'parent_id IS NOT' => null])
|
|
->groupBy(['parent_id'])
|
|
->disableHydration()
|
|
->all()
|
|
->combine(fn($row) => (int)$row['parent_id'], fn($row) => (int)$row['n'])
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* @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, 'participant_hash !=' => VoteService::PREPARED_HASH])
|
|
->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, 'Responses.participant_hash !=' => VoteService::PREPARED_HASH])
|
|
->groupBy(['Responses.slide_id'])
|
|
->disableHydration()
|
|
->all();
|
|
|
|
$counts = [];
|
|
foreach ($rows as $row) {
|
|
$counts[(int)$row['slide_id']] = (int)$row['n'];
|
|
}
|
|
|
|
return $counts;
|
|
}
|
|
}
|