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

+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),
);
}
/**