Add optional comment field to multiple choice slides
CI / Deploy / PHP 8.4 (push) Successful in 1m11s
CI / Deploy / PHP 8.5 (push) Successful in 1m11s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped

Participants can justify their choice; the comment is stored once in the
first response row, rotates on the stage like scale comments and is listed
in the admin area and the PDF export.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-13 09:29:25 +01:00
1 parent 28bfd535f4
commit 61718ed7c6
14 files changed
+330 -29

No files matched your search

+21 -9
View File
@@ -12,7 +12,9 @@ use Cake\ORM\Locator\LocatorAwareTrait;
* rendered by webroot/js/plenario.js (presenter + participant views).
*
* Shape (always present): ['type' => string, 'participants' => int, ...]
* - choice: 'options' => [['id', 'label', 'count', 'percent']]
* - choice: 'options' => [['id', 'label', 'count', 'percent']], 'votes' (int),
* 'comments' => [['id', 'text', 'value' (always null)]] oldest first (only with the comment
* field, else []; presentation only, participants never get results of choice slides)
* - wordcloud: 'words' => [['key' (lower case), 'text', 'count', 'down', 'weight' (0..1)]]
* (count = participants who named or upvoted the word, down = downvotes,
* weight follows count)
@@ -78,7 +80,7 @@ class ResultsService
];
return $result + match ($slide->type) {
Slide::TYPE_CHOICE => $this->choice($slide),
Slide::TYPE_CHOICE => $this->choice($slide, $limit),
Slide::TYPE_WORDCLOUD => $this->wordcloud($slide),
Slide::TYPE_OPEN => $this->open($slide, $limit),
Slide::TYPE_SCALE => $this->scale($slide, $limit, $withRatings),
@@ -141,9 +143,10 @@ class ResultsService
/**
* @param \App\Model\Entity\Slide $slide Slide
* @param int|null $limit Max. comments (newest), null = all
* @return array<string, mixed>
*/
protected function choice(Slide $slide): array
protected function choice(Slide $slide, ?int $limit = null): array
{
$counts = $this->Responses->find()
->select(['slide_option_id', 'n' => $this->Responses->find()->func()->count('*')])
@@ -166,7 +169,13 @@ class ResultsService
];
}
return ['options' => $options, 'votes' => $total];
$comments = [];
foreach ($slide->hasComments() ? $this->comments($slide, $limit) : [] as $comment) {
// No option next to the comment: with several choices it would only be a guess anyway.
$comments[] = ['id' => $comment['id'], 'text' => $comment['text'], 'value' => null];
}
return ['options' => $options, 'votes' => $total, 'comments' => $comments];
}
/**
@@ -377,18 +386,21 @@ class ResultsService
}
/**
* Comments of a scale with their rating, oldest first; the unfiltered call
* Comments of a scale with their rating (choice: without value), oldest first; the unfiltered call
* (ratings regardless of comment_ratings) is only for the admin area.
*
* @param \App\Model\Entity\Slide $slide Scale slide
* @param \App\Model\Entity\Slide $slide Scale or choice slide
* @param int|null $limit Max. comments (the newest ones), null = all
* @return list<array{id: int, text: string, value: int, created: string}>
* @return list<array{id: int, text: string, value: int|null, created: string}>
*/
public function comments(Slide $slide, ?int $limit = null): array
{
// Scale: the rating row carries the comment. Choice: the comment sits in the first of the
// participant's rows, which always has an option.
$answered = $slide->type === Slide::TYPE_CHOICE ? 'slide_option_id IS NOT' : 'numeric_value IS NOT';
$query = $this->Responses->find()
->select(['id', 'text_value', 'numeric_value', 'created'])
->where(['slide_id' => $slide->id, 'text_value IS NOT' => null, 'numeric_value IS NOT' => null])
->where(['slide_id' => $slide->id, 'text_value IS NOT' => null, $answered => null])
->orderBy(['id' => 'DESC']);
if ($limit !== null) {
$query->limit($limit);
@@ -399,7 +411,7 @@ class ResultsService
$comments[] = [
'id' => (int)$row->id,
'text' => (string)$row->text_value,
'value' => (int)$row->numeric_value,
'value' => $row->numeric_value === null ? null : (int)$row->numeric_value,
'created' => $row->created->toIso8601String(),
];
}
+22 -9
View File
@@ -136,20 +136,20 @@ class VoteService
/**
* Whether the participant may change the text of their answer afterwards (e.g. to fix a typo):
* the answer of an open question and the comment of a scale with comment field.
* the answer of an open question and the comment of a choice or scale slide with comment field.
*
* @param \App\Model\Entity\Slide $slide Slide
* @return bool
*/
public static function hasEditableText(Slide $slide): bool
{
return $slide->type === Slide::TYPE_OPEN || ($slide->type === Slide::TYPE_SCALE && $slide->hasComments());
return $slide->type === Slide::TYPE_OPEN || $slide->hasComments();
}
/**
* Replaces the text of the participant's own answer as long as voting on the slide is open
* (see hasEditableText()). Open question: `text`, must not be empty. Scale: `comment`, the
* rating itself stays unchanged and an empty comment removes it. Same preconditions as submit().
* (see hasEditableText()). Open question: `text`, must not be empty. Choice/scale: `comment`, the
* selection or rating itself stays unchanged and an empty comment removes it. Same preconditions as submit().
*
* @param \App\Model\Entity\Poll $poll Poll
* @param \App\Model\Entity\Slide $slide Slide
@@ -167,13 +167,17 @@ class VoteService
$text = $slide->type === Slide::TYPE_OPEN ? $this->openRows($data)[0]['text_value'] : self::comment($data);
$this->Responses->getConnection()->transactional(function () use ($slide, $participantHash, $text): void {
$this->lockSlide($slide);
// A multiple choice answer has several rows; the comment always lives in the first one.
$row = $this->Responses->find()
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
->orderBy(['id' => 'ASC'])
->first();
if ($row === null) {
throw new VoteException($slide->type === Slide::TYPE_OPEN
? __('Du hast hier noch nicht geantwortet.')
: __('Bitte gib zuerst deine Bewertung ab.'));
throw new VoteException(match ($slide->type) {
Slide::TYPE_OPEN => __('Du hast hier noch nicht geantwortet.'),
Slide::TYPE_CHOICE => __('Bitte gib zuerst deine Auswahl ab.'),
default => __('Bitte gib zuerst deine Bewertung ab.'),
});
}
$row->text_value = $text;
$this->Responses->saveOrFail($row);
@@ -181,7 +185,7 @@ class VoteService
}
/**
* Text of the participant's own answer: open answer or scale comment (null: none or not answered yet).
* Text of the participant's own answer: open answer or choice/scale comment (null: none or not answered yet).
*
* @param \App\Model\Entity\Slide $slide Slide
* @param string $participantHash Participant hash
@@ -192,6 +196,7 @@ class VoteService
$row = $this->Responses->find()
->select(['text_value'])
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
->orderBy(['id' => 'ASC'])
->disableHydration()
->first();
@@ -281,7 +286,15 @@ class VoteService
throw new VoteException(__('Bitte wähle nur eine Antwort aus.'));
}
return array_map(fn(int $id) => ['slide_option_id' => $id], $selected);
// The optional comment goes into the first row only, so it is stored once per participant;
// counts group by option and ignore text_value.
$comment = $slide->hasComments() ? self::comment($data) : null;
return array_map(
fn(int $id, int $index) => ['slide_option_id' => $id, 'text_value' => $index === 0 ? $comment : null],
$selected,
array_keys($selected),
);
}
/**