Allow Q&A rounds without upvotes and limit upvotes per question
- max_upvotes = 0 disables upvoting: the voting stage is skipped (collect -> discuss), questions are discussed in submission order - new setting max_upvotes_per_question caps a participant's upvotes on a single question (0 = no limit besides max_upvotes) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e44ded107
commit
bc86ec2db6
16 files changed
+235
-38
No files matched your search
@@ -5,6 +5,7 @@ namespace App\Controller;
|
||||
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use App\Service\QaService;
|
||||
use App\Service\ResetService;
|
||||
use App\Service\ResultsService;
|
||||
use App\Service\YouTubeService;
|
||||
@@ -300,6 +301,10 @@ class SlidesController extends AdminAppController
|
||||
if ($slide->type === Slide::TYPE_CHOICE || $slide->isQuiz() || $slide->isOrder()) {
|
||||
$this->Slides->syncOptions($slide, $labels, $correct);
|
||||
}
|
||||
if ($slide->isQa() && $slide->qaStage() === Slide::QA_VOTE && !$slide->hasQaUpvotes()) {
|
||||
// Upvotes were switched off while voting: continue with the discussion.
|
||||
(new QaService())->setStage($slide, Slide::QA_DISCUSS);
|
||||
}
|
||||
if ($youtube['warning'] !== null) {
|
||||
$this->Flash->warning($youtube['warning']);
|
||||
}
|
||||
|
||||
@@ -104,9 +104,10 @@ class Slide extends Entity
|
||||
self::TYPE_QUIZ => ['time_limit' => 30],
|
||||
// url as entered, video_id parsed from it, start in seconds
|
||||
self::TYPE_YOUTUBE => ['url' => '', 'video_id' => '', 'start' => 0, 'autoplay' => false],
|
||||
// upvotes per participant in the voting stage (1..QA_MAX_UPVOTES);
|
||||
// max_upvotes: upvotes per participant in the voting stage (0..QA_MAX_UPVOTES, 0 = no voting stage);
|
||||
// max_upvotes_per_question: own upvotes per question (0 = no limit besides max_upvotes);
|
||||
// allow_self_upvote: upvotes on own questions (for prioritizing topics, off by default)
|
||||
self::TYPE_QA => ['max_upvotes' => 3, 'allow_self_upvote' => false],
|
||||
self::TYPE_QA => ['max_upvotes' => 3, 'max_upvotes_per_question' => 0, 'allow_self_upvote' => false],
|
||||
// The correct order is the order of the slide_options (position), revealed when voting closes.
|
||||
self::TYPE_ORDER => [],
|
||||
];
|
||||
@@ -316,6 +317,39 @@ class Slide extends Entity
|
||||
return in_array($this->stage, self::QA_STAGES, true) ? (string)$this->stage : self::QA_COLLECT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a Q&A slide has a voting stage (setting `max_upvotes` > 0).
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasQaUpvotes(): bool
|
||||
{
|
||||
return (int)$this->setting('max_upvotes') > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stages of a Q&A slide in order: without upvotes the voting stage is skipped.
|
||||
*
|
||||
* @return list<string>
|
||||
*/
|
||||
public function qaStages(): array
|
||||
{
|
||||
return $this->hasQaUpvotes() ? self::QA_STAGES : [self::QA_COLLECT, self::QA_DISCUSS];
|
||||
}
|
||||
|
||||
/**
|
||||
* Upvotes one participant may give a single Q&A question (never more than `max_upvotes`).
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function qaUpvotesPerQuestion(): int
|
||||
{
|
||||
$max = (int)$this->setting('max_upvotes');
|
||||
$perQuestion = (int)$this->setting('max_upvotes_per_question');
|
||||
|
||||
return $perQuestion > 0 ? min($perQuestion, $max) : $max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a participant may answer the slide several times (word cloud: one
|
||||
* row per word, Q&A: questions and upvotes).
|
||||
|
||||
@@ -172,7 +172,11 @@ class SlidesTable extends Table
|
||||
break;
|
||||
case Slide::TYPE_QA:
|
||||
$max = (int)($input['max_upvotes'] ?? $settings['max_upvotes']);
|
||||
$settings['max_upvotes'] = max(1, min(Slide::QA_MAX_UPVOTES, $max));
|
||||
// 0 = no upvotes (no voting stage)
|
||||
$settings['max_upvotes'] = max(0, min(Slide::QA_MAX_UPVOTES, $max));
|
||||
$perQuestion = (int)($input['max_upvotes_per_question'] ?? $settings['max_upvotes_per_question']);
|
||||
// 0 = no limit per question
|
||||
$settings['max_upvotes_per_question'] = max(0, min(Slide::QA_MAX_UPVOTES, $perQuestion));
|
||||
$settings['allow_self_upvote'] = filter_var($input['allow_self_upvote'] ?? false, FILTER_VALIDATE_BOOLEAN);
|
||||
break;
|
||||
case Slide::TYPE_YOUTUBE:
|
||||
|
||||
@@ -280,10 +280,11 @@ class PollStateService
|
||||
* - discuss: all questions ranked with upvotes, `focusId` = question being discussed
|
||||
* `upvoted` = own upvotes per question id (several per question are allowed),
|
||||
* `allowSelfUpvote` = own questions may be upvoted too (setting `allow_self_upvote`).
|
||||
* `maxUpvotes` = 0: no voting stage; `maxPerQuestion` = own upvotes allowed per question.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @param string $participantHash Participant hash
|
||||
* @return array{stage: string, maxUpvotes: int, votesLeft: int, allowSelfUpvote: bool, mine: list<int>, upvoted: array<int, int>,
|
||||
* @return array{stage: string, maxUpvotes: int, maxPerQuestion: int, votesLeft: int, allowSelfUpvote: bool, mine: list<int>, upvoted: array<int, int>,
|
||||
* questions: list<array{id: int, text: string, votes?: int}>, focusId: int|null}
|
||||
*/
|
||||
public function qaState(Slide $slide, string $participantHash): array
|
||||
@@ -305,6 +306,7 @@ class PollStateService
|
||||
return [
|
||||
'stage' => $stage,
|
||||
'maxUpvotes' => $max,
|
||||
'maxPerQuestion' => $slide->qaUpvotesPerQuestion(),
|
||||
'votesLeft' => max(0, $max - count($own['upvotes'])),
|
||||
'allowSelfUpvote' => (bool)$slide->setting('allow_self_upvote'),
|
||||
'mine' => $own['questions'],
|
||||
|
||||
@@ -15,7 +15,8 @@ use InvalidArgumentException;
|
||||
* Stages (Slide::qaStage()):
|
||||
* - collect: participants submit questions (voting open)
|
||||
* - vote: participants upvote questions of others (own ones with `allow_self_upvote`),
|
||||
* at most `max_upvotes` each (voting open)
|
||||
* at most `max_upvotes` each and `max_upvotes_per_question` per question (voting open).
|
||||
* Skipped with `max_upvotes` = 0 (Slide::qaStages()).
|
||||
* - discuss: voting closed; the presenter steps through the questions, most upvoted
|
||||
* first (`slides.focus_response_id` = question on screen)
|
||||
*
|
||||
@@ -70,15 +71,15 @@ class QaService
|
||||
/**
|
||||
* 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.
|
||||
* current stage again changes nothing. Without upvotes there is no voting stage.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Q&A slide
|
||||
* @param string $stage One of Slide::QA_STAGES
|
||||
* @param string $stage One of Slide::qaStages()
|
||||
* @return \App\Model\Entity\Slide
|
||||
*/
|
||||
public function setStage(Slide $slide, string $stage): Slide
|
||||
{
|
||||
if (!$slide->isQa() || !in_array($stage, Slide::QA_STAGES, true)) {
|
||||
if (!$slide->isQa() || !in_array($stage, $slide->qaStages(), true)) {
|
||||
throw new InvalidArgumentException('Unknown Q&A stage ' . $stage);
|
||||
}
|
||||
if ($slide->qaStage() === $stage && $slide->stage !== null) {
|
||||
@@ -95,18 +96,19 @@ class QaService
|
||||
}
|
||||
|
||||
/**
|
||||
* Moves to the following stage (collect → vote → discuss).
|
||||
* Moves to the following stage (collect → vote → discuss, without upvotes collect → 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])) {
|
||||
$stages = $slide->qaStages();
|
||||
$index = (int)array_search($slide->qaStage(), $stages, true);
|
||||
if (!isset($stages[$index + 1])) {
|
||||
return false;
|
||||
}
|
||||
$this->setStage($slide, Slide::QA_STAGES[$index + 1]);
|
||||
$this->setStage($slide, $stages[$index + 1]);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,9 @@ use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
* order with 'position' (1-based), 'placed' (participants who put the item at its position),
|
||||
* 'percent', plus 'correctCount' (participants with the completely correct order).
|
||||
* Neither the correct order nor counts are part of the result before it is revealed.
|
||||
* - qa: 'stage' (collect|vote|discuss), 'maxUpvotes', 'upvotes' (total),
|
||||
* - qa: 'stage' (collect|vote|discuss), 'stages' (available stages in order, no 'vote' without upvotes),
|
||||
* 'maxUpvotes' (per participant, 0 = no upvotes), 'maxPerQuestion' (per participant and question),
|
||||
* 'upvotes' (total),
|
||||
* 'questions' => [['id', 'text', 'votes']] most upvoted first (ties: oldest first),
|
||||
* 'focusId' (question being discussed|null), 'focusIndex' (0-based position in questions|null).
|
||||
* Presenter only – participants get their own view, see PollStateService::qaState().
|
||||
@@ -221,7 +223,9 @@ class ResultsService
|
||||
|
||||
return [
|
||||
'stage' => $slide->qaStage(),
|
||||
'stages' => $slide->qaStages(),
|
||||
'maxUpvotes' => (int)$slide->setting('max_upvotes'),
|
||||
'maxPerQuestion' => $slide->qaUpvotesPerQuestion(),
|
||||
'upvotes' => array_sum(array_column($questions, 'votes')),
|
||||
'questions' => $questions,
|
||||
'focusId' => $focusIndex === null ? null : $slide->focus_response_id,
|
||||
|
||||
@@ -54,7 +54,7 @@ class SlideControlService
|
||||
break;
|
||||
}
|
||||
$stage = (string)($data['stage'] ?? '');
|
||||
if (!in_array($stage, Slide::QA_STAGES, true)) {
|
||||
if (!in_array($stage, $slide->qaStages(), true)) {
|
||||
throw new BadRequestException('Unknown stage');
|
||||
}
|
||||
$qa->setStage($slide, $stage);
|
||||
|
||||
@@ -31,7 +31,8 @@ use Cake\Utility\Security;
|
||||
* at most MAX_QA_QUESTIONS per participant, the same question (case-insensitive)
|
||||
* only once per slide. `vote=up|remove` + `question_id` only while voting: upvote
|
||||
* questions of others (own ones too with setting `allow_self_upvote`), at most
|
||||
* `max_upvotes` in total, freely distributed (several on one question are allowed);
|
||||
* `max_upvotes` in total, freely distributed (several on one question are allowed,
|
||||
* at most `max_upvotes_per_question` if set; `max_upvotes` = 0 means no voting stage);
|
||||
* `remove` withdraws one own upvote of the question.
|
||||
* - ordering: `order` = all option ids, each exactly once, in the participant's order.
|
||||
* Stored as one row with the ids comma separated in `text_value` (see parseOrder()).
|
||||
@@ -297,6 +298,9 @@ class VoteService
|
||||
return [['text_value' => $text]];
|
||||
}
|
||||
|
||||
if (!$slide->hasQaUpvotes()) {
|
||||
throw new VoteException(__('Bei dieser Fragerunde wird nicht abgestimmt.'));
|
||||
}
|
||||
if ($stage !== Slide::QA_VOTE) {
|
||||
throw new VoteException(__('Die Abstimmung über die Fragen hat noch nicht begonnen.'));
|
||||
}
|
||||
@@ -324,8 +328,18 @@ class VoteService
|
||||
if (!$slide->setting('allow_self_upvote') && in_array($id, $own['questions'], true)) {
|
||||
throw new VoteException(__('Deine eigene Frage kannst du nicht unterstützen.'));
|
||||
}
|
||||
// Several upvotes on the same question are allowed (push important questions).
|
||||
// Several upvotes on the same question are allowed (push important questions),
|
||||
// up to the limit per question.
|
||||
$max = (int)$slide->setting('max_upvotes');
|
||||
$perQuestion = $slide->qaUpvotesPerQuestion();
|
||||
if ($perQuestion < $max && count(array_keys($own['upvotes'], $id, true)) >= $perQuestion) {
|
||||
throw new VoteException(__n(
|
||||
'Du kannst jeder Frage nur eine Stimme geben.',
|
||||
'Du kannst jeder Frage höchstens {0} Stimmen geben.',
|
||||
$perQuestion,
|
||||
$perQuestion,
|
||||
));
|
||||
}
|
||||
if (count($own['upvotes']) >= $max) {
|
||||
throw new VoteException(__n(
|
||||
'Du hast deine Stimme schon vergeben. Nimm sie zurück, um eine andere Frage zu unterstützen.',
|
||||
|
||||
Reference in new issue
Block a user