diff --git a/AGENTS.md b/AGENTS.md index 8fbaa4a..e6ab449 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -55,7 +55,7 @@ src/ HomeController Startseite, Anlegen (/create), Bestätigung, Join, Recover AdminAppController Basis für /admin/{token}: löst Poll über Token-Hash auf (404 sonst), setzt Privacy-Header AdminController Dashboard, Settings, Present, State-JSON, Control, QR, CSV-Export, Token rotieren, Löschen - SlidesController Folien-CRUD, Antworten offener Fragen bzw. Skala-Auswertung (`answers`), Sortieren (Drag & Drop/Pfeiltasten via `reorder`, JSON; erfordert JS), Antworten zurücksetzen, + SlidesController Folien-CRUD, Antworten offener Fragen bzw. Auswertung von Skala/Umfrage (`answers`), Sortieren (Drag & Drop/Pfeiltasten via `reorder`, JSON; erfordert JS), Antworten zurücksetzen, YouTube-Link prüfen (`youtubeCheck`, JSON) ParticipateController /p/{code}: Ansicht, State-JSON, Abstimmen; anonymes Cookie plenario_pid EmbedController /e/{key}/{id}: eine Folie zum Einbetten (z. B. Canva), activate/heartbeat/pause/control @@ -92,7 +92,7 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures Mehrfachauswahl/mehrere Wörter = mehrere Zeilen mit gleichem `participant_hash`. Q&A: Frage = Zeile mit `text_value`, Upvote = Zeile mit `parent_id` (= id der Frage, FK mit CASCADE). Reihenfolge: eine Zeile pro Person, `text_value` = Options-ids kommagetrennt (`VoteService::parseOrder()`). - Skala: `numeric_value`, optional Kommentar in `text_value`. + Skala: `numeric_value`, optional Kommentar in `text_value`; Umfrage: Kommentar in `text_value` der ersten Zeile. ### Zentrale Regeln (nicht versehentlich brechen) @@ -169,15 +169,18 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures Presenter-/Embed-JSON (`results.answers` leer, nur `participants`) und nie bei Teilnehmenden (keine `results`, unabhängig von `show_results_to_participants`). Lesen nur in der Verwaltung (`slides:answers`, `ResultsService::answers()`) und im CSV-Export. -- **Internes Feedback** (Skala mit Setting `private`, ebenfalls `Slide::isPrivate()`): Presenter-/Embed-JSON enthält - `buckets: []` und `average: null` (nur `participants`), Teilnehmende bekommen keine `results`. Verteilung und - Durchschnitt nur in der Verwaltung (`slides:answers` → „Auswertung“, `ResultsService::scaleResults()`) und im CSV-Export. -- **Skala-Kommentare** (Setting `comments`, `Slide::hasComments()`): optionales Feld `comment`, gespeichert als `text_value` - in derselben Zeile wie `numeric_value` (ohne Setting wird es ignoriert). Öffentliche Skalen liefern im Presenter-/Embed-State - `results.comments` (Teilnehmende bekommen immer `[]`, `PollStateService::participantResults()`) - (älteste zuerst, je `id`, `text`, `value` – `value` nur mit Setting `comment_ratings`, sonst `null`); `Comments` in `plenario.js` blättert sie automatisch durch („Kommentar X von N“, - vor/zurück/Pause, Zustand übersteht das Neu-Rendern beim Polling). Bei internem Feedback bleibt `comments` leer, - lesbar nur in der Auswertung (`ResultsService::comments()`) und im CSV-Export. +- **Internes Feedback** (Skala und Umfrage mit Setting `private`, ebenfalls `Slide::isPrivate()`): Presenter-/Embed-JSON + enthält kein Ergebnis (Skala `buckets: []`/`average: null`, Umfrage `options: []`, nur `participants`), Teilnehmende + bekommen keine `results`. Ergebnis nur in der Verwaltung (`slides:answers` → „Auswertung“, + `ResultsService::scaleResults()` / `choiceResults()`) und im CSV-Export. +- **Kommentare** (Skala und Umfrage, Setting `comments`, `Slide::hasComments()`): optionales Feld `comment`, gespeichert als + `text_value` in der (bei Mehrfachauswahl: ersten) Zeile der Antwort, ohne Setting ignoriert (`VoteService::comment()`). + Öffentliche Folien liefern im Presenter-/Embed-State `results.comments` (älteste zuerst, je `id`, `text`, `value` – + `value` = Bewertung bzw. gewählte Antwort(en), nur mit Setting `comment_ratings`, sonst `null`). Teilnehmende bekommen + immer `[]` (`PollStateService::participantResults()`). `Comments` in `plenario.js` blättert sie automatisch durch + („Kommentar X von N“, vor/zurück/Pause, Zustand übersteht das Neu-Rendern beim Polling). Bei internem Feedback bleibt + `comments` leer, lesbar nur in der Auswertung (`ResultsService::comments()`) und im CSV-Export. + Formular-/Teilnehmerfelder: Elemente `feedback_settings` und `feedback_fields`. ### Neuen Folientyp hinzufügen (Checkliste) diff --git a/src/Controller/AdminController.php b/src/Controller/AdminController.php index f943153..8994d91 100644 --- a/src/Controller/AdminController.php +++ b/src/Controller/AdminController.php @@ -307,6 +307,8 @@ class AdminController extends AdminAppController $answer = match (true) { $slide->isOrder() => $this->orderAnswer($slide, $labels, (string)$response->text_value), $response->parent_id !== null => __('Upvote: {0}', $texts[$response->parent_id] ?? ''), + $response->slide_option_id !== null && $response->text_value !== null + => __('{0} (Kommentar: {1})', $labels[$response->slide_option_id] ?? '', $response->text_value), $response->slide_option_id !== null => $labels[$response->slide_option_id] ?? '', $response->text_value !== null && $response->numeric_value === VoteService::DOWNVOTE => __('Downvote: {0}', $response->text_value), diff --git a/src/Controller/SlidesController.php b/src/Controller/SlidesController.php index 0a4f6aa..4403e8f 100644 --- a/src/Controller/SlidesController.php +++ b/src/Controller/SlidesController.php @@ -164,7 +164,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 votes of a choice * (also anonymous/internal feedback, which is never shown on the stage). * * @param string $id Slide id @@ -173,13 +173,15 @@ 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, Slide::TYPE_CHOICE], true)) { throw new NotFoundException(__('Folie nicht gefunden.')); } $results = new ResultsService(); $this->set('slide', $slide); $this->set('answers', $slide->type === Slide::TYPE_OPEN ? $results->answers($slide) : []); $this->set('scale', $slide->type === Slide::TYPE_SCALE ? $results->scaleResults($slide) : null); + $this->set('choice', $slide->type === Slide::TYPE_CHOICE ? $results->choiceResults($slide) : null); + $this->set('participants', $this->Slides->Responses->countParticipants($slide->id)); $this->set('comments', $slide->hasComments() ? $results->comments($slide) : []); } diff --git a/src/Model/Entity/Slide.php b/src/Model/Entity/Slide.php index af61d4a..dded952 100644 --- a/src/Model/Entity/Slide.php +++ b/src/Model/Entity/Slide.php @@ -79,14 +79,15 @@ class Slide extends Entity * @var array> */ public const DEFAULT_SETTINGS = [ - self::TYPE_CHOICE => ['multiple' => false], + // private / comments / comment_ratings: see TYPE_SCALE (comment_ratings = show the chosen answer) + self::TYPE_CHOICE => ['multiple' => false, 'private' => false, 'comments' => false, 'comment_ratings' => 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) self::TYPE_OPEN => ['private' => false], // private: internal feedback, ratings only visible in the admin area (never on the stage) // comments: optional comment field, comments rotate in the presentation (unless private) - // comment_ratings: show the rating next to each comment in the presentation + // comment_ratings: show the rating (choice: the chosen answer) next to each comment in the presentation self::TYPE_SCALE => [ 'scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => '', 'private' => false, 'comments' => false, 'comment_ratings' => false, @@ -383,23 +384,24 @@ class Slide extends Entity } /** - * Open question or scale collecting anonymous/internal feedback: answers and ratings are + * Open question, scale or choice collecting anonymous/internal feedback: answers and results are * never shown on the presenter screen or to participants, only in the admin area and the CSV export. * * @return bool */ public function isPrivate(): bool { - return in_array($this->type, [self::TYPE_OPEN, self::TYPE_SCALE], true) && (bool)$this->setting('private'); + return in_array($this->type, [self::TYPE_OPEN, self::TYPE_SCALE, self::TYPE_CHOICE], true) + && (bool)$this->setting('private'); } /** - * Scale with an optional comment field for participants. + * Scale or choice 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_SCALE, self::TYPE_CHOICE], true) && (bool)$this->setting('comments'); } } diff --git a/src/Model/Table/SlidesTable.php b/src/Model/Table/SlidesTable.php index 756b7f5..84eb8ee 100644 --- a/src/Model/Table/SlidesTable.php +++ b/src/Model/Table/SlidesTable.php @@ -141,6 +141,9 @@ class SlidesTable extends Table switch ($type) { case Slide::TYPE_CHOICE: $settings['multiple'] = filter_var($input['multiple'] ?? false, FILTER_VALIDATE_BOOLEAN); + $settings['private'] = filter_var($input['private'] ?? false, FILTER_VALIDATE_BOOLEAN); + $settings['comments'] = filter_var($input['comments'] ?? false, FILTER_VALIDATE_BOOLEAN); + $settings['comment_ratings'] = filter_var($input['comment_ratings'] ?? 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 414334e..3a0798a 100644 --- a/src/Service/ResultsService.php +++ b/src/Service/ResultsService.php @@ -12,7 +12,8 @@ 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', 'private' (bool), 'comments' (like scale, + * 'value' = chosen answer(s) as text); private: 'options' and 'comments' empty, only via choiceResults() * - wordcloud: 'words' => [['key' (lower case), 'text', 'count', 'down', 'weight' (0..1)]] * (count = participants who named or upvoted the word, down = downvotes, * weight follows count) @@ -136,6 +137,22 @@ class ResultsService * @return array */ protected function choice(Slide $slide): array + { + if ($slide->isPrivate()) { + return ['options' => [], 'votes' => 0, 'private' => true, 'comments' => []]; + } + + return $this->choiceResults($slide) + ['private' => false, 'comments' => $this->publicComments($slide)]; + } + + /** + * Votes per option – also of private slides, so only for the admin area + * (never for the presenter screen or participants). + * + * @param \App\Model\Entity\Slide $slide Choice slide incl. slide_options + * @return array{options: list, votes: int} + */ + public function choiceResults(Slide $slide): array { $counts = $this->Responses->find() ->select(['slide_option_id', 'n' => $this->Responses->find()->func()->count('*')]) @@ -314,17 +331,7 @@ class ResultsService protected function scale(Slide $slide): array { if (!$slide->isPrivate()) { - $showRatings = (bool)$slide->setting('comment_ratings'); - $comments = []; - foreach ($slide->hasComments() ? $this->comments($slide, self::MAX_ANSWERS) : [] as $comment) { - $comments[] = [ - 'id' => $comment['id'], - 'text' => $comment['text'], - 'value' => $showRatings ? $comment['value'] : null, - ]; - } - - return $this->scaleResults($slide) + ['private' => false, 'comments' => $comments]; + return $this->scaleResults($slide) + ['private' => false, 'comments' => $this->publicComments($slide)]; } return [ @@ -340,29 +347,80 @@ class ResultsService } /** - * Comments of a scale with their rating, oldest first – also of private slides, - * so the unfiltered call is only for the admin area. + * Comments for the presentation: without date, the rating/answer only with the setting comment_ratings. * - * @param \App\Model\Entity\Slide $slide Scale slide + * @param \App\Model\Entity\Slide $slide Public scale or choice + * @return list + */ + protected function publicComments(Slide $slide): array + { + if (!$slide->hasComments()) { + return []; + } + $showRatings = (bool)$slide->setting('comment_ratings'); + $comments = []; + foreach ($this->comments($slide, self::MAX_ANSWERS) as $comment) { + $comments[] = [ + 'id' => $comment['id'], + 'text' => $comment['text'], + 'value' => $showRatings ? $comment['value'] : null, + ]; + } + + return $comments; + } + + /** + * Comments of a scale or choice with the rating (scale) or the chosen answers (choice), + * oldest first – also of private slides, so the unfiltered call is only for the admin area. + * + * @param \App\Model\Entity\Slide $slide Scale or choice slide (choice incl. slide_options) * @param int|null $limit Max. comments (the newest ones), null = all - * @return list + * @return list */ public function comments(Slide $slide, ?int $limit = null): array { + $isChoice = $slide->type === Slide::TYPE_CHOICE; $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]) + ->select(['id', 'text_value', 'numeric_value', 'participant_hash', 'created']) + ->where([ + 'slide_id' => $slide->id, + 'text_value IS NOT' => null, + ($isChoice ? 'slide_option_id' : 'numeric_value') . ' IS NOT' => null, + ]) ->orderBy(['id' => 'DESC']); if ($limit !== null) { $query->limit($limit); } + $rows = $query->all()->toList(); + + // Choice: all answers of the commenting participants (multiple choice = several rows). + $chosen = []; + if ($isChoice && $rows !== []) { + $labels = []; + foreach ($slide->slide_options ?? [] as $option) { + $labels[$option->id] = $option->label; + } + $answers = $this->Responses->find() + ->select(['participant_hash', 'slide_option_id']) + ->where([ + 'slide_id' => $slide->id, + 'slide_option_id IS NOT' => null, + 'participant_hash IN' => array_map(fn($row) => $row->participant_hash, $rows), + ]) + ->orderBy(['id' => 'ASC']) + ->all(); + foreach ($answers as $answer) { + $chosen[$answer->participant_hash][] = $labels[$answer->slide_option_id] ?? ''; + } + } $comments = []; - foreach ($query->all() as $row) { + foreach ($rows as $row) { $comments[] = [ 'id' => (int)$row->id, 'text' => (string)$row->text_value, - 'value' => (int)$row->numeric_value, + 'value' => $isChoice ? implode(', ', $chosen[$row->participant_hash] ?? []) : (int)$row->numeric_value, 'created' => $row->created->toIso8601String(), ]; } diff --git a/src/Service/VoteService.php b/src/Service/VoteService.php index 4656b9e..54aef36 100644 --- a/src/Service/VoteService.php +++ b/src/Service/VoteService.php @@ -142,7 +142,11 @@ class VoteService throw new VoteException(__('Bitte wähle nur eine Antwort aus.')); } - return array_map(fn(int $id) => ['slide_option_id' => $id], $selected); + $rows = array_map(fn(int $id) => ['slide_option_id' => $id, 'text_value' => null], $selected); + // The optional comment is stored once, in the first row of the participant. + $rows[0]['text_value'] = $this->comment($slide, $data); + + return $rows; } /** @@ -422,12 +426,24 @@ class VoteService if ($value === false || $value < $min || $value > $max) { throw new VoteException(__('Bitte wähle einen Wert zwischen {0} und {1}.', $min, $max)); } - // The optional comment is stored in the same row; ignored if the slide has no comment field. + // The optional comment is stored in the same row. + return [['numeric_value' => $value, 'text_value' => $this->comment($slide, $data)]]; + } + + /** + * Optional comment of a scale or choice; ignored if the slide has no comment field. + * + * @param \App\Model\Entity\Slide $slide Slide + * @param array $data Request data + * @return string|null Null if empty + */ + protected function comment(Slide $slide, array $data): ?string + { $comment = $slide->hasComments() ? trim((string)($data['comment'] ?? '')) : ''; if (mb_strlen($comment) > self::MAX_TEXT_LENGTH) { throw new VoteException(__('Der Kommentar darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH)); } - return [['numeric_value' => $value, 'text_value' => $comment === '' ? null : $comment]]; + return $comment === '' ? null : $comment; } } diff --git a/templates/Admin/index.php b/templates/Admin/index.php index 2072adb..741fe11 100644 --- a/templates/Admin/index.php +++ b/templates/Admin/index.php @@ -100,7 +100,7 @@ $url = fn(string $name, array $params = []) => ['_name' => $name, 'token' => $ad · is_voting_open) : ?>· hasComments()) : ?>💬 - isPrivate()) : ?>🔒 type === 'scale' ? __('intern') : __('anonym') ?> + isPrivate()) : ?>🔒 type === 'open' ? __('anonym') : __('intern') ?> question) ?> @@ -136,7 +136,7 @@ $url = fn(string $name, array $params = []) => ['_name' => $name, 'token' => $ad ]) ?> type === 'open' || $slide->isPrivate() || $slide->hasComments()) && $count > 0) : ?> - type === 'scale' ? __('Auswertung') : __('Antworten') ?> + type === 'open' ? __('Antworten') : __('Auswertung') ?> 0) : ?> diff --git a/templates/Participate/view.php b/templates/Participate/view.php index 9a9eade..0c8d445 100644 --- a/templates/Participate/view.php +++ b/templates/Participate/view.php @@ -217,7 +217,7 @@ $countdown = function (?int $remaining): string { type === 'choice') : $multiple = (bool)$slide->setting('multiple'); ?>

-
+
isPrivate() ? ' aria-describedby="private-hint"' : '' ?>> slide_options as $option) : ?>
+ element('feedback_fields', ['slide' => $slide, 'placeholder' => __('Möchtest du deine Wahl begründen?')]) ?> type === 'open') : ?> - - isPrivate()) : ?> -

- 🔒 -

- + element('feedback_fields', ['slide' => $slide, 'placeholder' => __('Möchtest du deine Bewertung begründen?')]) ?> Form->button(__('Absenden'), ['class' => 'button button--primary button--block']) ?> diff --git a/templates/Slides/answers.php b/templates/Slides/answers.php index 9ba03b3..2f2c536 100644 --- a/templates/Slides/answers.php +++ b/templates/Slides/answers.php @@ -8,7 +8,9 @@ * @var string $adminToken * @var \App\Model\Entity\Slide $slide * @var list $answers - * @var array|null $scale Distribution of a scale slide, null for open questions + * @var array|null $scale Distribution of a scale slide, null otherwise + * @var array|null $choice Votes of a choice slide, null otherwise + * @var int $participants Participants who answered * @var list $comments Comments of a scale, oldest first */ use Cake\I18n\DateTime; @@ -22,21 +24,46 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) :

typeLabel()) ?> - isPrivate()) : ?>· 🔒 + isPrivate()) : ?>· 🔒 type === 'open' ? __('Anonymes Feedback') : __('Internes Feedback') ?>

question) ?>

+ +

isPrivate()) : ?> -

+

type === 'open' + ? __('Diese Antworten werden weder in der Präsentation noch den Teilnehmenden angezeigt.') + : __('Dieses Ergebnis wird weder in der Präsentation noch den Teilnehmenden angezeigt.') ?>

- + + +

+ + + + + + + + + + + + + + + +
+ + Number->format((float)$option['percent'], ['places' => 0, 'precision' => 1, 'locale' => 'de_DE'])) ?> % +
+ +

@@ -69,22 +96,6 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) : - hasComments()) : ?> -

- -

- -
    - -
  1. - - -
  2. - -
- -

@@ -98,4 +109,21 @@ $count = $scale !== null ? array_sum(array_column($scale['buckets'], 'count')) : + + hasComments()) : ?> +

+ +

+ +
    + +
  1. + + +
  2. + +
+ +
diff --git a/templates/Slides/form.php b/templates/Slides/form.php index cd65a0e..4aaecaf 100644 --- a/templates/Slides/form.php +++ b/templates/Slides/form.php @@ -129,6 +129,10 @@ $optionsError = $slide->getError('options_text'); 'label' => __('Mehrfachauswahl erlauben'), 'checked' => (bool)$settings['multiple'], ]) ?> + element('feedback_settings', [ + 'settings' => $settings, + 'ratingLabel' => __('Gewählte Antwort beim Kommentar in der Präsentation anzeigen'), + ]) ?> type === 'open') : ?> Form->control('settings.private', [ 'type' => 'checkbox', @@ -166,23 +170,10 @@ $optionsError = $slide->getError('options_text'); 'placeholder' => __('z. B. sehr'), 'value' => $settings['max_label'], ]) ?>
- Form->control('settings.comments', [ - 'type' => 'checkbox', - 'label' => __('Kommentarfeld anzeigen (Teilnehmende können ihre Bewertung optional begründen)'), - 'checked' => (bool)$settings['comments'], + element('feedback_settings', [ + 'settings' => $settings, + 'ratingLabel' => __('Bewertung beim Kommentar in der Präsentation anzeigen'), ]) ?> -

- Form->control('settings.comment_ratings', [ - 'type' => 'checkbox', - 'label' => __('Bewertung beim Kommentar in der Präsentation anzeigen'), - 'checked' => (bool)$settings['comment_ratings'], - ]) ?> - Form->control('settings.private', [ - 'type' => 'checkbox', - 'label' => __('Internes Feedback: Ergebnis nicht öffentlich anzeigen'), - 'checked' => (bool)$settings['private'], - ]) ?> -

type === 'quiz') : $quizError = $slide->getError('quiz_options'); $rows = $quizOptions; diff --git a/templates/element/feedback_fields.php b/templates/element/feedback_fields.php new file mode 100644 index 0000000..8d5913f --- /dev/null +++ b/templates/element/feedback_fields.php @@ -0,0 +1,19 @@ + +hasComments()) : ?> + + + +isPrivate()) : ?> +

+ 🔒 +

+ diff --git a/templates/element/feedback_settings.php b/templates/element/feedback_settings.php new file mode 100644 index 0000000..44074df --- /dev/null +++ b/templates/element/feedback_settings.php @@ -0,0 +1,26 @@ + $settings Current settings + * @var string $ratingLabel Label of the setting comment_ratings + */ +?> +Form->control('settings.comments', [ + 'type' => 'checkbox', + 'label' => __('Kommentarfeld anzeigen (Teilnehmende können ihre Antwort optional begründen)'), + 'checked' => (bool)$settings['comments'], +]) ?> +

+Form->control('settings.comment_ratings', [ + 'type' => 'checkbox', + 'label' => $ratingLabel, + 'checked' => (bool)$settings['comment_ratings'], +]) ?> +Form->control('settings.private', [ + 'type' => 'checkbox', + 'label' => __('Internes Feedback: Ergebnis nicht öffentlich anzeigen'), + 'checked' => (bool)$settings['private'], +]) ?> +

diff --git a/tests/TestCase/Controller/AdminControllerTest.php b/tests/TestCase/Controller/AdminControllerTest.php index a21d4da..c96a644 100644 --- a/tests/TestCase/Controller/AdminControllerTest.php +++ b/tests/TestCase/Controller/AdminControllerTest.php @@ -84,7 +84,10 @@ class AdminControllerTest extends TestCase $slide = $this->slidesTable()->find()->contain(['SlideOptions'])->firstOrFail(); $this->assertSame($poll->id, $slide->poll_id); $this->assertSame(['Pizza', 'Pasta'], array_map(fn($o) => $o->label, $slide->slide_options)); - $this->assertSame(['multiple' => true], $slide->settings); + $this->assertSame( + ['multiple' => true, 'private' => false, 'comments' => false, 'comment_ratings' => false], + $slide->settings, + ); $this->assertSame(1, $slide->position); } diff --git a/tests/TestCase/Controller/ChoiceFeedbackTest.php b/tests/TestCase/Controller/ChoiceFeedbackTest.php new file mode 100644 index 0000000..7bc6ac5 --- /dev/null +++ b/tests/TestCase/Controller/ChoiceFeedbackTest.php @@ -0,0 +1,207 @@ + + */ + protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; + + protected function setUp(): void + { + parent::setUp(); + $this->enableCsrfToken(); + } + + /** + * Choice "Ja/Nein/Vielleicht" (multiple) with two votes and one comment. + * + * @param array $settings Settings + * @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide} + */ + protected function choiceWithVotes(array $settings): array + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['settings' => $settings + ['multiple' => true]], ['Ja', 'Nein', 'Vielleicht']); + $poll = $this->goLive($poll, $slide); + [$yes, $no, $maybe] = array_map(fn($option) => $option->id, $slide->slide_options); + $votes = new VoteService(); + $votes->submit($poll, $slide, $this->hash('a', $poll), ['option_ids' => [$yes, $maybe], 'comment' => 'Weil ']); + $votes->submit($poll, $slide, $this->hash('b', $poll), ['option_ids' => [$no]]); + + return [$poll, $token, $slide]; + } + + protected function hash(string $char, Poll $poll): string + { + return VoteService::participantHash(str_repeat($char, 32), $poll->id); + } + + /** + * @return array + */ + protected function presenterResults(string $token): array + { + $this->get("/admin/$token/state"); + + return json_decode((string)$this->_response->getBody(), true)['results']; + } + + public function testSettingsAreSanitized(): void + { + $settings = SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, ['private' => '1', 'comments' => '1', 'comment_ratings' => '1']); + $this->assertTrue($settings['private']); + $this->assertTrue($settings['comments']); + $this->assertTrue($settings['comment_ratings']); + + $settings = SlidesTable::sanitizeSettings(Slide::TYPE_CHOICE, []); + $this->assertFalse($settings['private']); + $this->assertFalse($settings['comments']); + $this->assertFalse($settings['comment_ratings']); + + [$poll] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_CHOICE, ['settings' => ['multiple' => false]]); + $this->assertFalse($slide->isPrivate()); + $this->assertFalse($slide->hasComments()); + } + + public function testFormOffersFeedbackSettings(): void + { + [, $token] = $this->createPoll(); + + $this->get("/admin/$token/slides/add?type=choice"); + $this->assertResponseOk(); + $this->assertResponseContains('name="settings[comments]"'); + $this->assertResponseContains('Gewählte Antwort beim Kommentar'); + $this->assertResponseContains('name="settings[private]"'); + + $this->get("/admin/$token/slides/add?type=scale"); + $this->assertResponseOk(); + $this->assertResponseContains('Bewertung beim Kommentar'); + } + + public function testCommentIsStoredOnceForMultipleChoice(): void + { + [, , $slide] = $this->choiceWithVotes(['comments' => true]); + + $texts = $this->slidesTable()->Responses->find() + ->where(['slide_id' => $slide->id, 'text_value IS NOT' => null]) + ->all() + ->extract('text_value') + ->toList(); + $this->assertSame(['Weil '], $texts); + } + + public function testCommentIsIgnoredWithoutCommentField(): void + { + [, , $slide] = $this->choiceWithVotes([]); + + $this->assertFalse($this->slidesTable()->Responses->exists(['slide_id' => $slide->id, 'text_value IS NOT' => null])); + } + + public function testParticipantPageShowsCommentFieldAndHint(): void + { + [$poll, , $slide] = $this->choiceWithVotes(['comments' => true, 'private' => true]); + $this->slidesTable()->Responses->deleteAll(['slide_id' => $slide->id]); + + $this->get('/p/' . $poll->join_code); + $this->assertResponseContains('name="comment"'); + $this->assertResponseContains('Internes Feedback'); + } + + public function testPresenterGetsCommentsWithoutAnswerByDefault(): void + { + [, $token] = $this->choiceWithVotes(['comments' => true]); + + $results = $this->presenterResults($token); + + $this->assertFalse($results['private']); + $this->assertSame([1, 1, 1], array_column($results['options'], 'count')); + $this->assertSame(['Weil '], array_column($results['comments'], 'text')); + $this->assertSame([null], array_column($results['comments'], 'value')); + } + + public function testChosenAnswersNextToCommentAreOptional(): void + { + [, $token] = $this->choiceWithVotes(['comments' => true, 'comment_ratings' => true]); + + $this->assertSame(['Ja, Vielleicht'], array_column($this->presenterResults($token)['comments'], 'value')); + } + + public function testParticipantsNeverSeeComments(): void + { + [$poll] = $this->choiceWithVotes(['comments' => true]); + $this->cookie('plenario_pid', str_repeat('a', 32)); + + $this->get('/p/' . $poll->join_code . '/state'); + $state = json_decode((string)$this->_response->getBody(), true); + + $this->assertNotEmpty($state['results']['options']); + $this->assertSame([], $state['results']['comments']); + $this->assertResponseNotContains('darum'); + } + + public function testPrivateChoiceShowsOnlyTheCount(): void + { + [$poll, $token, $slide] = $this->choiceWithVotes(['private' => true, 'comments' => true]); + + $results = $this->presenterResults($token); + $this->assertTrue($results['private']); + $this->assertSame([], $results['options']); + $this->assertSame([], $results['comments']); + $this->assertSame(2, $results['participants']); + $this->assertResponseNotContains('darum'); + + $key = $this->pollsTable()->ensureEmbedKey($poll); + $this->get("/e/$key/{$slide->id}/state"); + $this->assertResponseNotContains('darum'); + + $this->cookie('plenario_pid', str_repeat('a', 32)); + $this->get('/p/' . $poll->join_code . '/state'); + $this->assertNull(json_decode((string)$this->_response->getBody(), true)['results']); + } + + public function testAdminEvaluationShowsVotesAndComments(): void + { + [, $token, $slide] = $this->choiceWithVotes(['private' => true, 'comments' => true]); + + $this->get("/admin/$token"); + $this->assertResponseContains("/slides/{$slide->id}/answers"); + $this->assertResponseContains('intern'); + + $this->get("/admin/$token/slides/{$slide->id}/answers"); + $this->assertResponseOk(); + $this->assertResponseContains('2 Personen haben abgestimmt'); + $this->assertResponseContains('Internes Feedback'); + $this->assertResponseContains('1 Kommentar'); + $this->assertResponseContains('Weil <darum>'); + $this->assertResponseContains('Antwort: Ja, Vielleicht'); + } + + public function testExportContainsComment(): void + { + [, $token] = $this->choiceWithVotes(['comments' => true]); + + $this->get("/admin/$token/export.csv"); + + $this->assertResponseContains('Ja (Kommentar: Weil )'); + $this->assertResponseContains(';Vielleicht;'); + } +} diff --git a/tests/TestCase/Controller/PrivateFeedbackTest.php b/tests/TestCase/Controller/PrivateFeedbackTest.php index 607c943..07b5207 100644 --- a/tests/TestCase/Controller/PrivateFeedbackTest.php +++ b/tests/TestCase/Controller/PrivateFeedbackTest.php @@ -131,14 +131,14 @@ class PrivateFeedbackTest extends TestCase $this->assertHeader('Referrer-Policy', 'no-referrer'); } - public function testAnswersPageOnlyForOwnOpenSlides(): void + public function testAnswersPageOnlyForOwnSlidesWithEvaluation(): void { [$poll, $token] = $this->createPoll(); - $choice = $this->createSlide($poll); + $quiz = $this->createSlide($poll, Slide::TYPE_QUIZ); [$other] = $this->createPoll('Andere'); $foreign = $this->createSlide($other, Slide::TYPE_OPEN); - $this->get("/admin/$token/slides/{$choice->id}/answers"); + $this->get("/admin/$token/slides/{$quiz->id}/answers"); $this->assertResponseCode(404); $this->get("/admin/$token/slides/{$foreign->id}/answers"); $this->assertResponseCode(404); diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index bf7b20b..f5e459c 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -513,7 +513,7 @@ button.qa-steps__step:hover:not(:disabled) { color: var(--text); border-color: v .answers-page { list-style: none; padding: 0; margin: 0; } .answers-page li { padding: 12px 16px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 8px; background: var(--surface); white-space: pre-line; overflow-wrap: anywhere; } .answers-page time { display: block; color: var(--muted); font-size: .8rem; margin-top: 4px; } -.scale-comment { display: block; margin-top: 16px; font-weight: 600; } +.comment-label { display: block; margin-top: 16px; font-weight: 600; } .comments { margin-top: 20px; padding: 16px 20px; background: var(--surface-2); border-radius: 14px; } .comments__counter { margin: 0 0 6px; font-size: .85em; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-variant-numeric: tabular-nums; } .comments__text { margin: 0; font-size: 1.25em; font-weight: 600; white-space: pre-line; overflow-wrap: anywhere; } diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index 37b44ba..fb1ee07 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -303,8 +303,14 @@ timer: null, items: [], node: null, + valuePrefix: '', - render: function (items) { + /** + * @param {Array} items [{id, text, value|null}] + * @param {string} valuePrefix e.g. 'Bewertung: ' or 'Antwort: ' + */ + render: function (items, valuePrefix) { + Comments.valuePrefix = valuePrefix; // A different first comment means a different slide (or a reset): start over. var key = String(items[0].id); if (key !== Comments.key) { @@ -387,7 +393,7 @@ 'Kommentar ' + (Comments.index + 1) + ' von ' + Comments.items.length; text.textContent = item.text; var value = node.querySelector('[data-comment-value]'); - value.textContent = item.value === null || item.value === undefined ? '' : 'Bewertung: ' + item.value; + value.textContent = item.value === null || item.value === undefined ? '' : Comments.valuePrefix + item.value; value.hidden = value.textContent === ''; // Only announce changes the viewer triggered, not every automatic rotation. node.setAttribute('aria-live', Comments.paused ? 'polite' : 'off'); @@ -409,6 +415,24 @@ // ------------------------------------------------------------------ results + /** + * Anonymous/internal feedback: only the number of answers reaches the stage. + */ + function privateSummary(n, singular, plural) { + return el('div', { class: 'private-summary' }, [ + el('p', { class: 'private-summary__count', text: String(n) }), + el('p', { text: (n === 1 ? singular : plural) + ' eingegangen' }), + el('p', { class: 'private-summary__hint', text: '🔒 Ergebnis wird nicht öffentlich angezeigt.' }) + ]); + } + + function withComments(node, results, valuePrefix) { + if (!results.comments || !results.comments.length) { + return node; + } + return el('div', {}, [node, Comments.render(results.comments, valuePrefix)]); + } + var Results = { /** * @param {Object} [options] renderer specific; wordcloud: {mine: [keys], onUpvote: fn(word)|null} @@ -441,6 +465,13 @@ }, choice: function (results) { + if (results.private) { + return privateSummary(results.participants, 'Stimme', 'Stimmen'); + } + return withComments(Results.choiceBars(results), results, 'Antwort: '); + }, + + choiceBars: function (results) { var max = Math.max.apply(null, results.options.map(function (o) { return o.count; }).concat([1])); return el('div', { class: 'bars' }, results.options.map(function (option, index) { return el('div', { class: 'bar bar--c' + (index % 6) }, [ @@ -455,13 +486,7 @@ scale: function (results) { if (results.private) { - // Internal feedback: the ratings never reach the stage, only the count. - var n = results.participants; - return el('div', { class: 'private-summary' }, [ - el('p', { class: 'private-summary__count', text: String(n) }), - el('p', { text: (n === 1 ? 'Bewertung' : 'Bewertungen') + ' eingegangen' }), - el('p', { class: 'private-summary__hint', text: '🔒 Ergebnis wird nicht öffentlich angezeigt.' }) - ]); + return privateSummary(results.participants, 'Bewertung', 'Bewertungen'); } var max = Math.max.apply(null, results.buckets.map(function (b) { return b.count; }).concat([1])); var total = results.buckets.reduce(function (sum, b) { return sum + b.count; }, 0); @@ -509,7 +534,7 @@ ]), gauge ]); - var comments = results.comments && results.comments.length ? Comments.render(results.comments) : null; + var comments = results.comments && results.comments.length ? Comments.render(results.comments, 'Bewertung: ') : null; return el('div', { class: 'scale-results' }, [average, columns, labels, comments]); },