Add ordering slide type
Participants bring shuffled items into the correct order and submit it once. The admin enters the items in the correct order and can reorder them by drag & drop or arrow keys. Closing the voting reveals the solution: how many got it completely right and how often each item was placed correctly; participants see whether their order was right. The drag & drop mechanics of the slide list are extracted into a reusable makeSortable() helper. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4eed568a64
commit
918e077734
17 files changed
+922
-95
No files matched your search
@@ -22,7 +22,8 @@ use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
* Quiz slides reveal results (and the participant's own correctness) only
|
||||
* once the quiz is finished – then always, independent of
|
||||
* `show_results_to_participants`. Timed quizzes are closed lazily here
|
||||
* when their deadline has passed.
|
||||
* when their deadline has passed. Ordering slides work the same way: solution,
|
||||
* counts and the participant's correctness (`order`) only once voting is closed.
|
||||
*
|
||||
* Q&A slides have their own participant view (`qa`, see qaState()), never `results`.
|
||||
* Private open questions (anonymous feedback) never show results to participants.
|
||||
@@ -153,6 +154,7 @@ class PollStateService
|
||||
$words = $slide?->type === Slide::TYPE_WORDCLOUD ? $this->wordState($slide, $participantHash) : null;
|
||||
$quiz = $slide?->isQuiz() ? $this->quizState($slide, $participantHash) : null;
|
||||
$qa = $slide?->isQa() ? $this->qaState($slide, $participantHash) : null;
|
||||
$order = $slide?->isOrder() ? $this->orderState($slide, $participantHash) : null;
|
||||
$hasResponded = match (true) {
|
||||
$words !== null => $words['mine'] !== [] || $words['down'] !== [],
|
||||
$qa !== null => $qa['mine'] !== [] || $qa['upvoted'] !== [],
|
||||
@@ -164,8 +166,8 @@ class PollStateService
|
||||
&& $slide->isInteractive()
|
||||
&& $qa === null
|
||||
&& !$slide->isPrivate()
|
||||
&& ($quiz !== null
|
||||
? $quiz['phase'] === Slide::QUIZ_FINISHED
|
||||
&& ($quiz !== null || $order !== null
|
||||
? ($quiz['phase'] ?? null) === Slide::QUIZ_FINISHED || ($order['revealed'] ?? false)
|
||||
: ($poll->show_results_to_participants && ($hasResponded || !$isVotingOpen))
|
||||
|| ($words !== null && $words['allowUpvotes'] && $isVotingOpen));
|
||||
|
||||
@@ -189,6 +191,35 @@ class PollStateService
|
||||
'words' => $words,
|
||||
'quiz' => $quiz,
|
||||
'qa' => $qa,
|
||||
'order' => $order,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordering progress of one participant. Own answer and correctness only once
|
||||
* revealed (voting closed); before that the state must not hint at the solution.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Ordering slide incl. slide_options
|
||||
* @param string $participantHash Participant hash
|
||||
* @return array{revealed: bool, answer: list<int>|null, isCorrect: bool|null}
|
||||
*/
|
||||
public function orderState(Slide $slide, string $participantHash): array
|
||||
{
|
||||
$revealed = $slide->isOrderRevealed();
|
||||
$answer = null;
|
||||
if ($revealed) {
|
||||
$response = $this->Responses->find()
|
||||
->select(['text_value'])
|
||||
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
|
||||
->first();
|
||||
$answer = $response === null ? null : VoteService::parseOrder($response->text_value);
|
||||
}
|
||||
$correct = array_map(fn($option) => $option->id, array_values($slide->slide_options ?? []));
|
||||
|
||||
return [
|
||||
'revealed' => $revealed,
|
||||
'answer' => $answer,
|
||||
'isCorrect' => $answer === null ? null : $answer === $correct,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,11 @@ use Cake\ORM\Locator\LocatorAwareTrait;
|
||||
* once finished additionally per option 'count', 'percent', 'correct' (bool)
|
||||
* and 'correctCount' (participants with a correct answer).
|
||||
* Counts and correct answers are NEVER part of the result before the quiz is finished.
|
||||
* - order: 'revealed' (bool, = voting closed); while not revealed 'items' => [['id', 'label']]
|
||||
* in a fixed shuffled order (never the solution); once revealed 'items' in the correct
|
||||
* 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),
|
||||
* 'questions' => [['id', 'text', 'votes']] most upvoted first (ties: oldest first),
|
||||
* 'focusId' (question being discussed|null), 'focusIndex' (0-based position in questions|null).
|
||||
@@ -66,10 +71,61 @@ class ResultsService
|
||||
Slide::TYPE_SCALE => $this->scale($slide),
|
||||
Slide::TYPE_QUIZ => $this->quiz($slide),
|
||||
Slide::TYPE_QA => $this->qa($slide),
|
||||
Slide::TYPE_ORDER => $this->order($slide),
|
||||
default => [],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function order(Slide $slide): array
|
||||
{
|
||||
if (!$slide->isOrderRevealed()) {
|
||||
return [
|
||||
'revealed' => false,
|
||||
'items' => array_map(fn($option) => [
|
||||
'id' => $option->id,
|
||||
'label' => $option->label,
|
||||
], $slide->shuffledOptions('slide:' . $slide->id)),
|
||||
];
|
||||
}
|
||||
|
||||
$correct = array_map(fn($option) => $option->id, array_values($slide->slide_options ?? []));
|
||||
$placed = array_fill_keys($correct, 0);
|
||||
$correctCount = 0;
|
||||
$answers = $this->Responses->find()
|
||||
->select(['text_value'])
|
||||
->where(['slide_id' => $slide->id, 'text_value IS NOT' => null])
|
||||
->disableHydration()
|
||||
->all();
|
||||
foreach ($answers as $row) {
|
||||
$given = VoteService::parseOrder($row['text_value']);
|
||||
if ($given === $correct) {
|
||||
$correctCount++;
|
||||
}
|
||||
foreach ($correct as $index => $id) {
|
||||
if (($given[$index] ?? null) === $id) {
|
||||
$placed[$id]++;
|
||||
}
|
||||
}
|
||||
}
|
||||
$total = count($answers);
|
||||
$items = [];
|
||||
foreach (array_values($slide->slide_options ?? []) as $index => $option) {
|
||||
$items[] = [
|
||||
'id' => $option->id,
|
||||
'label' => $option->label,
|
||||
'position' => $index + 1,
|
||||
'placed' => $placed[$option->id],
|
||||
'percent' => $total > 0 ? round($placed[$option->id] / $total * 100, 1) : 0,
|
||||
];
|
||||
}
|
||||
|
||||
return ['revealed' => true, 'items' => $items, 'correctCount' => $correctCount];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return array<string, mixed>
|
||||
|
||||
@@ -28,6 +28,8 @@ use Cake\Utility\Security;
|
||||
* only once per slide. `vote=up|remove` + `question_id` only while voting: upvote
|
||||
* questions of others, at most `max_upvotes` in total, freely distributed (several
|
||||
* on one question are allowed); `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()).
|
||||
*/
|
||||
class VoteService
|
||||
{
|
||||
@@ -105,6 +107,7 @@ class VoteService
|
||||
Slide::TYPE_SCALE => $this->scaleRows($slide, $data),
|
||||
Slide::TYPE_QUIZ => $this->quizRows($slide, $data),
|
||||
Slide::TYPE_QA => $this->qaRows($slide, $participantHash, $data),
|
||||
Slide::TYPE_ORDER => $this->orderRows($slide, $data),
|
||||
default => throw new VoteException(__('Unbekannter Folientyp.')),
|
||||
};
|
||||
|
||||
@@ -159,6 +162,51 @@ class VoteService
|
||||
return [['slide_option_id' => $id]];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param array<string, mixed> $data Request data
|
||||
* @return list<array<string, mixed>>
|
||||
*/
|
||||
protected function orderRows(Slide $slide, array $data): array
|
||||
{
|
||||
$given = [];
|
||||
foreach ((array)($data['order'] ?? []) as $id) {
|
||||
$id = is_scalar($id) ? filter_var($id, FILTER_VALIDATE_INT) : false;
|
||||
if ($id === false) {
|
||||
$given = [];
|
||||
break;
|
||||
}
|
||||
$given[] = $id;
|
||||
}
|
||||
$validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []);
|
||||
$sortedGiven = $given;
|
||||
sort($sortedGiven);
|
||||
sort($validIds);
|
||||
if ($validIds === [] || $sortedGiven !== $validIds) {
|
||||
throw new VoteException(__('Bitte bring alle Einträge in eine Reihenfolge und sende sie erneut.'));
|
||||
}
|
||||
|
||||
return [['text_value' => implode(',', $given)]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Option ids of a stored ordering answer (`responses.text_value`), in the participant's order.
|
||||
*
|
||||
* @param string|null $text Stored answer
|
||||
* @return list<int>
|
||||
*/
|
||||
public static function parseOrder(?string $text): array
|
||||
{
|
||||
$ids = [];
|
||||
foreach (explode(',', (string)$text) as $id) {
|
||||
if (ctype_digit(trim($id))) {
|
||||
$ids[] = (int)$id;
|
||||
}
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Q&A: a new question while collecting, an upvote (or its withdrawal) while voting.
|
||||
* Withdrawing deletes one upvote row and returns no new rows.
|
||||
|
||||
Reference in new issue
Block a user