From 61718ed7c674ee650e8ff140b51c43e788ef2969 Mon Sep 17 00:00:00 2001
From: Patrick Niebeling
Date: Fri, 13 Nov 2026 09:29:25 +0100
Subject: [PATCH] Add optional comment field to multiple choice slides
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
---
AGENTS.md | 6 +
src/Controller/SlidesController.php | 4 +-
src/Model/Entity/Slide.php | 8 +-
src/Model/Table/SlidesTable.php | 1 +
src/Service/ResultsService.php | 30 ++-
src/Service/VoteService.php | 31 ++-
templates/Admin/export.php | 12 +
templates/Admin/index.php | 6 +-
templates/Participate/view.php | 9 +-
templates/Slides/answers.php | 25 +-
templates/Slides/form.php | 6 +
.../Controller/AdminControllerTest.php | 2 +-
.../Controller/ChoiceCommentsTest.php | 213 ++++++++++++++++++
webroot/js/plenario.js | 6 +-
14 files changed, 330 insertions(+), 29 deletions(-)
create mode 100644 tests/TestCase/Controller/ChoiceCommentsTest.php
diff --git a/AGENTS.md b/AGENTS.md
index 3e9da51..e1eb237 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -248,6 +248,12 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures
vor/zurück/Pause, Laufbalken bis zum Wechsel (`Comments.progress()`, setzt nach dem Neu-Rendern per negativem
`animation-delay` fort, bei Pause unsichtbar), Zustand übersteht das Neu-Rendern beim Polling; die Box ist so hoch wie der längste Kommentar – alle liegen unsichtbar in derselben Grid-Zelle –, damit die zentrierte Bühne beim Wechsel nicht springt; höchstens `--comment-max` ≈ 5 Zeilen, längere Kommentare setzt `Comments.fit()` kleiner statt sie abzuschneiden; die Bewertung steht über dem Text, damit sie an fester Stelle bleibt; der Fortschritt läuft als Rahmen der Box: `Comments.progressBorder()`, SVG-Rechteck mit `pathLength` 100, zwei Linien ab gegenüberliegenden Ecken). Bei internem Feedback erscheinen sie erst,
wenn der Presenter die Ergebnisse einblendet.
+- **Kommentare bei Multiple Choice** (Setting `comments`, gleiches `Slide::hasComments()` wie bei Skalen): optionales Feld `comment`,
+ gespeichert als `text_value` **nur in der ersten Zeile** der Person (bei Mehrfachauswahl gibt es mehrere Zeilen; Zählungen
+ gruppieren nach `slide_option_id` und ignorieren `text_value`). Bearbeiten/Anlegen/Leeren wie bei Skalen über `participate:edit`
+ (`editText()` und `ownText()` lesen die Zeile mit der kleinsten id). Presenter/Embed bekommen `results.comments` (`value` immer
+ `null`, keine Zuordnung zur Option), `Results.choice` zeigt sie mit `Comments` unter den Balken; Teilnehmende nie.
+ Volle Liste: `slides:answers` („Kommentare“), PDF-Export unter der Tabelle. Tests: `ChoiceCommentsTest.php`.
- **Sterne-Bewertung** (Skala mit Setting `style = stars`, `Slide::isStarRating()`, Standard `numbers`): immer 1 bis `scale_max`
Sterne (`SlidesTable::sanitizeSettings()` erzwingt `scale_min = 1`), gespeichert wie jede Skala als `numeric_value`.
Teilnehmende wählen Sterne (Radios in DOM-Reihenfolge 1..n, Füllung per CSS `:has()`), der State enthält `results.stars`;
diff --git a/src/Controller/SlidesController.php b/src/Controller/SlidesController.php
index 1d35254..6c99227 100644
--- a/src/Controller/SlidesController.php
+++ b/src/Controller/SlidesController.php
@@ -201,7 +201,7 @@ class SlidesController extends AdminAppController
}
/**
- * All answers of an open question or the ratings of a scale
+ * All answers of an open question, the ratings of a scale or the comments of a choice slide
* (also anonymous/internal feedback, which is never shown on the stage).
*
* @param string $id Slide id
@@ -210,7 +210,7 @@ class SlidesController extends AdminAppController
public function answers(string $id): void
{
$slide = $this->loadSlide($id);
- if (!in_array($slide->type, [Slide::TYPE_OPEN, Slide::TYPE_SCALE], true)) {
+ if (!in_array($slide->type, [Slide::TYPE_OPEN, Slide::TYPE_SCALE], true) && !$slide->hasComments()) {
throw new NotFoundException(__('Folie nicht gefunden.'));
}
$results = new ResultsService();
diff --git a/src/Model/Entity/Slide.php b/src/Model/Entity/Slide.php
index 1bf2276..19f85a3 100644
--- a/src/Model/Entity/Slide.php
+++ b/src/Model/Entity/Slide.php
@@ -101,7 +101,9 @@ class Slide extends Entity
* @var array>
*/
public const DEFAULT_SETTINGS = [
- self::TYPE_CHOICE => ['multiple' => false],
+ // comments: optional comment field (stored in text_value of the participant's first row),
+ // comments rotate in the presentation like those of a scale
+ self::TYPE_CHOICE => ['multiple' => false, 'comments' => false],
// allow_downvotes only takes effect together with allow_upvotes
self::TYPE_WORDCLOUD => ['allow_upvotes' => true, 'allow_downvotes' => true],
// private: anonymous feedback, answers only visible in the admin area (never on the stage)
@@ -500,13 +502,13 @@ class Slide extends Entity
}
/**
- * Scale with an optional comment field for participants.
+ * Multiple choice or scale with an optional comment field for participants.
*
* @return bool
*/
public function hasComments(): bool
{
- return $this->type === self::TYPE_SCALE && (bool)$this->setting('comments');
+ return in_array($this->type, [self::TYPE_CHOICE, self::TYPE_SCALE], true) && (bool)$this->setting('comments');
}
/**
diff --git a/src/Model/Table/SlidesTable.php b/src/Model/Table/SlidesTable.php
index 6dde09e..96e1ce4 100644
--- a/src/Model/Table/SlidesTable.php
+++ b/src/Model/Table/SlidesTable.php
@@ -142,6 +142,7 @@ class SlidesTable extends Table
switch ($type) {
case Slide::TYPE_CHOICE:
$settings['multiple'] = filter_var($input['multiple'] ?? false, FILTER_VALIDATE_BOOLEAN);
+ $settings['comments'] = filter_var($input['comments'] ?? false, FILTER_VALIDATE_BOOLEAN);
break;
case Slide::TYPE_WORDCLOUD:
$settings['allow_upvotes'] = filter_var($input['allow_upvotes'] ?? true, FILTER_VALIDATE_BOOLEAN);
diff --git a/src/Service/ResultsService.php b/src/Service/ResultsService.php
index 5bedc67..e978c71 100644
--- a/src/Service/ResultsService.php
+++ b/src/Service/ResultsService.php
@@ -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
*/
- 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
+ * @return list
*/
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(),
];
}
diff --git a/src/Service/VoteService.php b/src/Service/VoteService.php
index 2fac4b8..07831e0 100644
--- a/src/Service/VoteService.php
+++ b/src/Service/VoteService.php
@@ -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),
+ );
}
/**
diff --git a/templates/Admin/export.php b/templates/Admin/export.php
index bd8aefa..ec5d516 100644
--- a/templates/Admin/export.php
+++ b/templates/Admin/export.php
@@ -103,6 +103,18 @@ $bar = function (float $percent, bool $ok = false): string {
+ hasComments()) : ?>
+ = __n('{0} Kommentar', '{0} Kommentare', count($r['comments']), count($r['comments'])) ?>
+
+ = __('Keine Kommentare.') ?>
+
+
+
+ - = nl2br(h($comment['text'])) ?>
+
+
+
+
type === 'quiz') : ?>
diff --git a/templates/Admin/index.php b/templates/Admin/index.php
index 85f3739..e71ab97 100644
--- a/templates/Admin/index.php
+++ b/templates/Admin/index.php
@@ -143,7 +143,11 @@ $hasInternal = array_filter($slides, fn($slide) => !$slide->is_hidden && $slide-
]) ?>
type === 'open' || $slide->isPrivate() || $slide->hasComments()) && $count > 0) :
- $answersLabel = $slide->type === 'scale' ? __('Auswertung') : __('Antworten'); ?>
+ $answersLabel = match ($slide->type) {
+ 'scale' => __('Auswertung'),
+ 'choice' => __('Kommentare'),
+ default => __('Antworten'),
+ }; ?>
📊
diff --git a/templates/Participate/view.php b/templates/Participate/view.php
index 11abae7..5e6f43b 100644
--- a/templates/Participate/view.php
+++ b/templates/Participate/view.php
@@ -266,7 +266,9 @@ $countdown = function (?int $remaining): string {
aria-describedby="edit-text-hint">= h((string)$ownText) ?>
= $isOpen
? __('Möglich, solange die Abstimmung läuft.')
- : __('Möglich, solange die Abstimmung läuft. Ein leeres Feld entfernt deinen Kommentar, deine Bewertung bleibt.') ?>
+ : ($slide->type === 'choice'
+ ? __('Möglich, solange die Abstimmung läuft. Ein leeres Feld entfernt deinen Kommentar, deine Auswahl bleibt.')
+ : __('Möglich, solange die Abstimmung läuft. Ein leeres Feld entfernt deinen Kommentar, deine Bewertung bleibt.')) ?>
= $this->Form->button($isOpen ? __('Antwort speichern') : __('Kommentar speichern'), ['class' => 'button button--primary button--block']) ?>
= $this->Form->end() ?>
@@ -290,6 +292,11 @@ $countdown = function (?int $remaining): string {
+ hasComments()) : ?>
+
+
+
type === 'open') : ?>