Add Q&A slide type
Participants submit questions, upvote them and the presenter discusses them one by one, most upvoted first. - Stages collect -> vote -> discuss, switched manually by the presenter (V key or stepper, going back is possible) - Configurable upvote budget per participant, freely distributable (several upvotes on one question are allowed), votes can be withdrawn - Participants see all questions while collecting to avoid duplicates; no counts before the discussion - While discussing, next/prev step through the questions before moving to the next slide - Migration: slides.stage, slides.focus_response_id, responses.parent_id Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea944b080f
commit
f0c6687100
21 files changed
+1327
-27
No files matched your search
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\ResponsesTable;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Q&A slides ("Fragen & Antworten"), presenter side.
|
||||
*
|
||||
* Stages (Slide::qaStage()):
|
||||
* - collect: participants submit questions (voting open)
|
||||
* - vote: participants upvote questions of others, at most `max_upvotes` each (voting open)
|
||||
* - discuss: voting closed; the presenter steps through the questions, most upvoted
|
||||
* first (`slides.focus_response_id` = question on screen)
|
||||
*
|
||||
* The presenter switches stages manually and may go back (e.g. reopen the
|
||||
* collection). Submitting and upvoting itself is validated in VoteService.
|
||||
*/
|
||||
class QaService
|
||||
{
|
||||
use LocatorAwareTrait;
|
||||
|
||||
protected SlidesTable $Slides;
|
||||
|
||||
protected ResponsesTable $Responses;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Questions of a slide, most upvoted first (ties: oldest first).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @return list<array{id: int, text: string, votes: int}>
|
||||
*/
|
||||
public function rankedQuestions(Slide $slide): array
|
||||
{
|
||||
$votes = $this->Responses->upvotesPerQuestion($slide->id);
|
||||
$questions = [];
|
||||
$rows = $this->Responses->questionsOf($slide->id)
|
||||
->select(['id', 'text_value'])
|
||||
->orderBy(['id' => 'ASC'])
|
||||
->disableHydration()
|
||||
->all();
|
||||
foreach ($rows as $row) {
|
||||
$id = (int)$row['id'];
|
||||
$questions[] = ['id' => $id, 'text' => (string)$row['text_value'], 'votes' => $votes[$id] ?? 0];
|
||||
}
|
||||
usort($questions, fn($a, $b) => [$b['votes'], $a['id']] <=> [$a['votes'], $b['id']]);
|
||||
|
||||
return $questions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Switches the stage. Collecting and voting open the slide for input,
|
||||
* discussing closes it and puts the top question on screen. Setting the
|
||||
* current stage again changes nothing.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @param string $stage One of Slide::QA_STAGES
|
||||
* @return \App\Model\Entity\Slide
|
||||
*/
|
||||
public function setStage(Slide $slide, string $stage): Slide
|
||||
{
|
||||
if (!$slide->isQa() || !in_array($stage, Slide::QA_STAGES, true)) {
|
||||
throw new InvalidArgumentException('Unknown Q&A stage ' . $stage);
|
||||
}
|
||||
if ($slide->qaStage() === $stage && $slide->stage !== null) {
|
||||
return $slide;
|
||||
}
|
||||
$slide->stage = $stage;
|
||||
$slide->is_voting_open = $stage !== Slide::QA_DISCUSS;
|
||||
$slide->focus_response_id = $stage === Slide::QA_DISCUSS
|
||||
? ($this->rankedQuestions($slide)[0]['id'] ?? null)
|
||||
: null;
|
||||
$this->Slides->saveOrFail($slide);
|
||||
|
||||
return $slide;
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves to the following stage (collect → vote → discuss).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @return bool False when already discussing
|
||||
*/
|
||||
public function advance(Slide $slide): bool
|
||||
{
|
||||
$index = (int)array_search($slide->qaStage(), Slide::QA_STAGES, true);
|
||||
if (!isset(Slide::QA_STAGES[$index + 1])) {
|
||||
return false;
|
||||
}
|
||||
$this->setStage($slide, Slide::QA_STAGES[$index + 1]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Steps to the next/previous question while discussing.
|
||||
*
|
||||
* Saved without touching `modified`, so participant pages update live
|
||||
* instead of reloading (see PollStateService version).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @param int $step +1 next, -1 previous
|
||||
* @return bool False when there is no such question (or not discussing)
|
||||
*/
|
||||
public function moveFocus(Slide $slide, int $step): bool
|
||||
{
|
||||
if (!$slide->isQa() || $slide->qaStage() !== Slide::QA_DISCUSS) {
|
||||
return false;
|
||||
}
|
||||
$ids = array_column($this->rankedQuestions($slide), 'id');
|
||||
$index = array_search($slide->focus_response_id, $ids, true);
|
||||
$target = $index === false ? 0 : $index + $step;
|
||||
if (!isset($ids[$target]) || $ids[$target] === $slide->focus_response_id) {
|
||||
return false;
|
||||
}
|
||||
$this->Slides->updateAll(['focus_response_id' => $ids[$target]], ['id' => $slide->id]);
|
||||
$slide->focus_response_id = $ids[$target];
|
||||
$slide->setDirty('focus_response_id', false);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back to the start after all responses were deleted.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @return void
|
||||
*/
|
||||
public function reset(Slide $slide): void
|
||||
{
|
||||
$slide->stage = Slide::QA_COLLECT;
|
||||
$slide->is_voting_open = true;
|
||||
$slide->focus_response_id = null;
|
||||
$this->Slides->saveOrFail($slide);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user