$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, upvotes: list} 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 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, down: list} 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 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; } }