Make Q&A upvote budget a share of the questions
CI / Deploy / test (push) Successful in 58s
CI / Deploy / deploy (push) Skipped

The fixed number of votes per person does not fit varying question
counts. The budget is now `upvote_share` percent of the questions
(rounded up, at least one), so it grows with the number of questions.
A migration converts existing Q&A slides (no voting stays off, others
get 25 %).

On the stage the voting step no longer lists the questions but points
participants to their devices.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-11 23:53:32 +01:00
1 parent 3762b880cb
commit 575fb8649d
14 files changed
+171 -76

No files matched your search

+34 -10
View File
@@ -84,10 +84,17 @@ class Slide extends Entity
public const QA_STAGES = [self::QA_COLLECT, self::QA_VOTE, self::QA_DISCUSS];
/**
* Highest selectable number of upvotes per participant on a Q&A slide.
* Highest selectable number of upvotes one participant may give a single question.
*/
public const QA_MAX_UPVOTES = 10;
/**
* Selectable shares (percent of the questions) that make up each participant's upvote budget.
*
* @var list<int>
*/
public const QA_UPVOTE_SHARES = [10, 20, 25, 33, 50];
/**
* Default settings per type. Unknown keys are dropped on save.
*
@@ -117,10 +124,11 @@ 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],
// 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);
// upvote_share: percent of the questions each participant may upvote in the voting stage, so the
// budget grows with the number of questions (qaBudget(); 0 = no voting stage, else QA_UPVOTE_SHARES);
// max_upvotes_per_question: own upvotes per question (0 = no limit besides the budget);
// allow_self_upvote: upvotes on own questions (for prioritizing topics, off by default)
self::TYPE_QA => ['max_upvotes' => 3, 'max_upvotes_per_question' => 0, 'allow_self_upvote' => false],
self::TYPE_QA => ['upvote_share' => 25, '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 => [],
];
@@ -331,13 +339,13 @@ class Slide extends Entity
}
/**
* Whether a Q&A slide has a voting stage (setting `max_upvotes` > 0).
* Whether a Q&A slide has a voting stage (setting `upvote_share` > 0).
*
* @return bool
*/
public function hasQaUpvotes(): bool
{
return (int)$this->setting('max_upvotes') > 0;
return (int)$this->setting('upvote_share') > 0;
}
/**
@@ -351,16 +359,32 @@ class Slide extends Entity
}
/**
* Upvotes one participant may give a single Q&A question (never more than `max_upvotes`).
* Upvotes each participant gets in the voting stage: `upvote_share` percent of the questions,
* rounded up and at least one (0 without questions or without a voting stage).
*
* @param int $questionCount Number of questions on the slide
* @return int
*/
public function qaUpvotesPerQuestion(): int
public function qaBudget(int $questionCount): int
{
if ($questionCount <= 0 || !$this->hasQaUpvotes()) {
return 0;
}
return max(1, (int)ceil($questionCount * (int)$this->setting('upvote_share') / 100));
}
/**
* Upvotes one participant may give a single Q&A question (never more than the budget).
*
* @param int $budget Upvotes per participant, see qaBudget()
* @return int
*/
public function qaUpvotesPerQuestion(int $budget): int
{
$max = (int)$this->setting('max_upvotes');
$perQuestion = (int)$this->setting('max_upvotes_per_question');
return $perQuestion > 0 ? min($perQuestion, $max) : $max;
return $perQuestion > 0 ? min($perQuestion, $budget) : $budget;
}
/**
+4 -2
View File
@@ -173,9 +173,11 @@ class SlidesTable extends Table
}
break;
case Slide::TYPE_QA:
$max = (int)($input['max_upvotes'] ?? $settings['max_upvotes']);
$share = (int)($input['upvote_share'] ?? $settings['upvote_share']);
// 0 = no upvotes (no voting stage)
$settings['max_upvotes'] = max(0, min(Slide::QA_MAX_UPVOTES, $max));
$settings['upvote_share'] = $share === 0 || in_array($share, Slide::QA_UPVOTE_SHARES, true)
? $share
: Slide::DEFAULT_SETTINGS[Slide::TYPE_QA]['upvote_share'];
$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));
+3 -3
View File
@@ -283,7 +283,7 @@ 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.
* `maxUpvotes` = upvote budget of the participant (share of the questions), 0: no voting stage; `maxPerQuestion` = own upvotes allowed per question.
*
* @param \App\Model\Entity\Slide $slide Q&A slide
* @param string $participantHash Participant hash
@@ -294,8 +294,8 @@ class PollStateService
{
$stage = $slide->qaStage();
$own = $this->Responses->qaVotesOf($slide->id, $participantHash);
$max = (int)$slide->setting('max_upvotes');
$questions = (new QaService())->rankedQuestions($slide);
$max = $slide->qaBudget(count($questions));
if ($stage === Slide::QA_DISCUSS) {
$focusId = in_array($slide->focus_response_id, array_column($questions, 'id'), true)
? $slide->focus_response_id
@@ -309,7 +309,7 @@ class PollStateService
return [
'stage' => $stage,
'maxUpvotes' => $max,
'maxPerQuestion' => $slide->qaUpvotesPerQuestion(),
'maxPerQuestion' => $slide->qaUpvotesPerQuestion($max),
'votesLeft' => max(0, $max - count($own['upvotes'])),
'allowSelfUpvote' => (bool)$slide->setting('allow_self_upvote'),
'mine' => $own['questions'],
+2 -2
View File
@@ -15,8 +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 and `max_upvotes_per_question` per question (voting open).
* Skipped with `max_upvotes` = 0 (Slide::qaStages()).
* a budget of `upvote_share` percent of the questions each and `max_upvotes_per_question` per question (voting open).
* Skipped with `upvote_share` = 0 (Slide::qaStages()).
* - discuss: voting closed; the presenter steps through the questions, most upvoted
* first (`slides.focus_response_id` = question on screen)
*
+3 -2
View File
@@ -217,6 +217,7 @@ class ResultsService
{
$questions = (new QaService())->rankedQuestions($slide);
$upvotes = array_sum(array_column($questions, 'votes'));
$budget = $slide->qaBudget(count($questions));
if ($slide->qaStage() === Slide::QA_VOTE) {
// A live ranking on the stage would sway the vote: submission order, no counts until voting ends.
usort($questions, fn($a, $b) => $a['id'] <=> $b['id']);
@@ -232,8 +233,8 @@ class ResultsService
return [
'stage' => $slide->qaStage(),
'stages' => $slide->qaStages(),
'maxUpvotes' => (int)$slide->setting('max_upvotes'),
'maxPerQuestion' => $slide->qaUpvotesPerQuestion(),
'maxUpvotes' => $budget,
'maxPerQuestion' => $slide->qaUpvotesPerQuestion($budget),
'upvotes' => $upvotes,
'questions' => $questions,
'focusId' => $focusIndex === null ? null : $slide->focus_response_id,
+4 -4
View File
@@ -32,8 +32,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,
* at most `max_upvotes_per_question` if set; `max_upvotes` = 0 means no voting stage);
* a budget of `upvote_share` percent of the questions in total, freely distributed (several on one question are allowed,
* at most `max_upvotes_per_question` if set; `upvote_share` = 0 means no voting stage);
* `remove` withdraws one own upvote of the question. `vote=edit|delete` + `question_id`
* changes or deletes an own question, only while collecting (see editQuestion()).
* - ordering: `order` = all option ids, each exactly once, in the participant's order.
@@ -404,8 +404,8 @@ class VoteService
}
// 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();
$max = $slide->qaBudget($this->Responses->questionsOf($slide->id)->count());
$perQuestion = $slide->qaUpvotesPerQuestion($max);
if ($perQuestion < $max && count(array_keys($own['upvotes'], $id, true)) >= $perQuestion) {
throw new VoteException(__n(
'Du kannst jeder Frage nur eine Stimme geben.',