From 367ff8548df92c112374663395c3dd3ea1c51090 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Sat, 7 Nov 2026 16:28:47 +0100 Subject: [PATCH] Add anonymous feedback option for open questions Open questions can be marked private: the presenter screen and embeds only show the number of answers, participants never see results, and the texts are readable on a new admin answers page and in the CSV. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 7 +- config/routes.php | 2 + src/Controller/SlidesController.php | 17 ++ src/Model/Entity/Slide.php | 14 +- src/Model/Table/SlidesTable.php | 3 + src/Service/PollStateService.php | 2 + src/Service/ResultsService.php | 38 +++-- templates/Admin/index.php | 4 + templates/Participate/view.php | 6 + templates/Slides/answers.php | 44 +++++ templates/Slides/form.php | 7 + .../Controller/PrivateFeedbackTest.php | 160 ++++++++++++++++++ webroot/css/plenario.css | 9 + webroot/js/plenario.js | 9 + 14 files changed, 310 insertions(+), 12 deletions(-) create mode 100644 templates/Slides/answers.php create mode 100644 tests/TestCase/Controller/PrivateFeedbackTest.php diff --git a/AGENTS.md b/AGENTS.md index b172d4a..c322ba3 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, Sortieren (Drag & Drop/Pfeiltasten via `reorder`, JSON; erfordert JS), Antworten zurücksetzen, + SlidesController Folien-CRUD, Antworten offener Fragen (`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 @@ -153,6 +153,11 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures ist die Berechtigung, siehe `Application::middleware()`) und erlauben Framing per CSP `frame-ancestors` (`EMBED_FRAME_ANCESTORS`); alle anderen Seiten bleiben `X-Frame-Options: SAMEORIGIN`. +- **Anonymes Feedback** (offene Frage mit Setting `private`, `Slide::isPrivate()`): Antworttexte erscheinen **nie** im + 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. + ### Neuen Folientyp hinzufügen (Checkliste) 1. Konstante + Label + Beschreibung + `DEFAULT_SETTINGS` in `src/Model/Entity/Slide.php` diff --git a/config/routes.php b/config/routes.php index 1775391..3df5ccb 100644 --- a/config/routes.php +++ b/config/routes.php @@ -81,6 +81,8 @@ return function (RouteBuilder $routes): void { ->setPatterns($token)->setMethods(['POST']); $admin->connect('/slides/youtube-check', 'Slides::youtubeCheck', ['_name' => 'slides:youtube-check']) ->setPatterns($token)->setMethods(['POST']); + $admin->connect('/slides/{id}/answers', 'Slides::answers', ['_name' => 'slides:answers']) + ->setPatterns($token)->setPass(['id']); $admin->connect('/slides/{id}/reset', 'Slides::reset', ['_name' => 'slides:reset']) ->setPatterns($token)->setPass(['id'])->setMethods(['POST']); }); diff --git a/src/Controller/SlidesController.php b/src/Controller/SlidesController.php index 6d6139a..dfe32de 100644 --- a/src/Controller/SlidesController.php +++ b/src/Controller/SlidesController.php @@ -6,6 +6,7 @@ namespace App\Controller; use App\Model\Entity\Slide; use App\Model\Table\SlidesTable; use App\Service\QaService; +use App\Service\ResultsService; use App\Service\YouTubeService; use Cake\Http\Exception\NotFoundException; use Cake\Http\Response; @@ -158,6 +159,22 @@ class SlidesController extends AdminAppController return $this->redirect($this->adminUrl()); } + /** + * All answers of an open question (also anonymous feedback, which is never shown on the stage). + * + * @param string $id Slide id + * @return void + */ + public function answers(string $id): void + { + $slide = $this->loadSlide($id); + if ($slide->type !== Slide::TYPE_OPEN) { + throw new NotFoundException(__('Folie nicht gefunden.')); + } + $this->set('slide', $slide); + $this->set('answers', (new ResultsService())->answers($slide)); + } + /** * Loads a slide that belongs to the current poll (404 otherwise). * diff --git a/src/Model/Entity/Slide.php b/src/Model/Entity/Slide.php index 6a2c1cb..9c8cd1d 100644 --- a/src/Model/Entity/Slide.php +++ b/src/Model/Entity/Slide.php @@ -81,7 +81,8 @@ class Slide extends Entity self::TYPE_CHOICE => ['multiple' => false], // allow_downvotes only takes effect together with allow_upvotes self::TYPE_WORDCLOUD => ['allow_upvotes' => true, 'allow_downvotes' => true], - self::TYPE_OPEN => [], + // private: anonymous feedback, answers only visible in the admin area (never on the stage) + self::TYPE_OPEN => ['private' => false], self::TYPE_SCALE => ['scale_min' => 1, 'scale_max' => 5, 'min_label' => '', 'max_label' => ''], self::TYPE_CONTENT => [], // time_limit in seconds, 0 = no limit (presenter ends the quiz) @@ -327,4 +328,15 @@ class Slide extends Entity { return $this->getSettings()[$key] ?? null; } + + /** + * Open question collecting anonymous feedback: answers 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 $this->type === self::TYPE_OPEN && (bool)$this->setting('private'); + } } diff --git a/src/Model/Table/SlidesTable.php b/src/Model/Table/SlidesTable.php index 5acad51..464e1d6 100644 --- a/src/Model/Table/SlidesTable.php +++ b/src/Model/Table/SlidesTable.php @@ -145,6 +145,9 @@ class SlidesTable extends Table $settings['allow_upvotes'] = filter_var($input['allow_upvotes'] ?? true, FILTER_VALIDATE_BOOLEAN); $settings['allow_downvotes'] = filter_var($input['allow_downvotes'] ?? true, FILTER_VALIDATE_BOOLEAN); break; + case Slide::TYPE_OPEN: + $settings['private'] = filter_var($input['private'] ?? false, FILTER_VALIDATE_BOOLEAN); + break; case Slide::TYPE_SCALE: $min = max(0, min(1, (int)($input['scale_min'] ?? 1))); $max = max(2, min(10, (int)($input['scale_max'] ?? 5))); diff --git a/src/Service/PollStateService.php b/src/Service/PollStateService.php index 3f183f6..898f309 100644 --- a/src/Service/PollStateService.php +++ b/src/Service/PollStateService.php @@ -25,6 +25,7 @@ use Cake\ORM\Locator\LocatorAwareTrait; * when their deadline has passed. * * Q&A slides have their own participant view (`qa`, see qaState()), never `results`. + * Private open questions (anonymous feedback) never show results to participants. */ class PollStateService { @@ -162,6 +163,7 @@ class PollStateService $showResults = $slide !== null && $slide->isInteractive() && $qa === null + && !$slide->isPrivate() && ($quiz !== null ? $quiz['phase'] === Slide::QUIZ_FINISHED : ($poll->show_results_to_participants && ($hasResponded || !$isVotingOpen)) diff --git a/src/Service/ResultsService.php b/src/Service/ResultsService.php index 7d118da..52e53a3 100644 --- a/src/Service/ResultsService.php +++ b/src/Service/ResultsService.php @@ -16,7 +16,8 @@ use Cake\ORM\Locator\LocatorAwareTrait; * - wordcloud: 'words' => [['key' (lower case), 'text', 'count', 'down', 'weight' (0..1)]] * (count = participants who named or upvoted the word, down = downvotes, * weight follows count) - * - open: 'answers' => [['text', 'created']] newest first + * - open: 'answers' => [['text', 'created']] newest first, 'private' (bool); + * private (anonymous feedback): 'answers' is always empty, texts only via answers() * - scale: 'buckets' => [['value', 'count', 'percent']], 'average' => float|null, * 'min', 'max', 'minLabel', 'maxLabel' * - quiz: 'phase' (ready|running|finished), 'timeLimit' (s, 0 = manual), 'remaining' (s|null), @@ -213,19 +214,36 @@ class ResultsService */ protected function open(Slide $slide): array { - $rows = $this->Responses->find() + return [ + 'private' => $slide->isPrivate(), + 'answers' => $slide->isPrivate() ? [] : $this->answers($slide, self::MAX_ANSWERS), + ]; + } + + /** + * Texts of an open question, newest first – also of private slides, + * so only for the admin area (never for the presenter screen or participants). + * + * @param \App\Model\Entity\Slide $slide Open question + * @param int|null $limit Max. answers, null = all + * @return list + */ + public function answers(Slide $slide, ?int $limit = null): array + { + $query = $this->Responses->find() ->select(['text_value', 'created']) ->where(['slide_id' => $slide->id, 'text_value IS NOT' => null]) - ->orderBy(['id' => 'DESC']) - ->limit(self::MAX_ANSWERS) - ->all(); - - $answers = []; - foreach ($rows as $row) { - $answers[] = ['text' => $row->text_value, 'created' => $row->created->toIso8601String()]; + ->orderBy(['id' => 'DESC']); + if ($limit !== null) { + $query->limit($limit); } - return ['answers' => $answers]; + $answers = []; + foreach ($query->all() as $row) { + $answers[] = ['text' => (string)$row->text_value, 'created' => $row->created->toIso8601String()]; + } + + return $answers; } /** diff --git a/templates/Admin/index.php b/templates/Admin/index.php index d1672d6..b4c04f4 100644 --- a/templates/Admin/index.php +++ b/templates/Admin/index.php @@ -97,6 +97,7 @@ $url = fn(string $name, array $params = []) => ['_name' => $name, 'token' => $ad 'discuss' => __('Fragen werden besprochen'), ][$slide->qaStage()]) ?> · setting('max_upvotes'), (int)$slide->setting('max_upvotes')) ?> is_voting_open) : ?>· + isPrivate()) : ?>🔒 question) ?> @@ -126,6 +127,9 @@ $url = fn(string $name, array $params = []) => ['_name' => $name, 'token' => $ad 'title' => __('Diese Folie jetzt den Teilnehmenden zeigen'), ]) ?> + type === 'open' && $count > 0) : ?> + + 0) : ?> Form->postLink(__('Zurücksetzen'), $url('slides:reset', ['id' => $slide->id]), [ diff --git a/templates/Participate/view.php b/templates/Participate/view.php index 4f21cb7..f04cc1c 100644 --- a/templates/Participate/view.php +++ b/templates/Participate/view.php @@ -190,7 +190,13 @@ $countdown = function (?int $remaining): string { type === 'open') : ?> + isPrivate()) : ?> +

+ 🔒 +

+ type === 'scale') : ?>
diff --git a/templates/Slides/answers.php b/templates/Slides/answers.php new file mode 100644 index 0000000..f379d3f --- /dev/null +++ b/templates/Slides/answers.php @@ -0,0 +1,44 @@ + $answers + */ +use Cake\I18n\DateTime; + +$this->assign('title', $slide->question); +$this->assign('noindex', '1'); +$count = count($answers); +?> +

←

+ +
+

+ typeLabel()) ?> + isPrivate()) : ?>· 🔒 +

+

question) ?>

+

+ isPrivate()) : ?> +

+ + + +

+ +
    + +
  1. + + +
  2. + +
+ +
diff --git a/templates/Slides/form.php b/templates/Slides/form.php index 5702741..b77559a 100644 --- a/templates/Slides/form.php +++ b/templates/Slides/form.php @@ -128,6 +128,13 @@ $optionsError = $slide->getError('options_text'); 'label' => __('Mehrfachauswahl erlauben'), 'checked' => (bool)$settings['multiple'], ]) ?> + type === 'open') : ?> + Form->control('settings.private', [ + 'type' => 'checkbox', + 'label' => __('Anonymes Feedback: Antworten nicht öffentlich anzeigen'), + 'checked' => (bool)$settings['private'], + ]) ?> +

type === 'wordcloud') : ?> Form->control('settings.allow_upvotes', [ 'type' => 'checkbox', diff --git a/tests/TestCase/Controller/PrivateFeedbackTest.php b/tests/TestCase/Controller/PrivateFeedbackTest.php new file mode 100644 index 0000000..c215ece --- /dev/null +++ b/tests/TestCase/Controller/PrivateFeedbackTest.php @@ -0,0 +1,160 @@ + + */ + protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses']; + + protected function setUp(): void + { + parent::setUp(); + $this->enableCsrfToken(); + } + + /** + * @return array{0: \App\Model\Entity\Poll, 1: string, 2: \App\Model\Entity\Slide} + */ + protected function privateSlideWithAnswer(string $text = 'Geheimes '): array + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_OPEN, ['settings' => ['private' => true]]); + $poll = $this->goLive($poll, $slide); + (new VoteService())->submit($poll, $slide, VoteService::participantHash(str_repeat('a', 32), $poll->id), ['text' => $text]); + + return [$poll, $token, $slide]; + } + + protected function participantState(Poll $poll): array + { + $this->get('/p/' . $poll->join_code . '/state'); + + return json_decode((string)$this->_response->getBody(), true); + } + + public function testSettingIsSanitized(): void + { + $this->assertTrue(SlidesTable::sanitizeSettings(Slide::TYPE_OPEN, ['private' => '1'])['private']); + $this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_OPEN, ['private' => '0'])['private']); + $this->assertFalse(SlidesTable::sanitizeSettings(Slide::TYPE_OPEN, [])['private']); + } + + public function testExistingOpenSlidesStayPublic(): void + { + [$poll] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_OPEN, ['settings' => []]); + + $this->assertFalse($slide->isPrivate()); + } + + public function testFormSavesPrivateSetting(): void + { + [$poll, $token] = $this->createPoll(); + $this->post("/admin/$token/slides/add?type=open", [ + 'type' => 'open', + 'question' => 'Was können wir besser machen?', + 'settings' => ['private' => '1'], + ]); + + $slide = $this->slidesTable()->find()->where(['poll_id' => $poll->id])->firstOrFail(); + $this->assertTrue($slide->isPrivate()); + } + + public function testPresenterSeesOnlyTheCount(): void + { + [, $token] = $this->privateSlideWithAnswer(); + + $this->get("/admin/$token/state"); + $state = json_decode((string)$this->_response->getBody(), true); + + $this->assertTrue($state['results']['private']); + $this->assertSame([], $state['results']['answers']); + $this->assertSame(1, $state['results']['participants']); + $this->assertResponseNotContains('Geheimes'); + } + + public function testParticipantsNeverSeeAnswers(): void + { + [$poll] = $this->privateSlideWithAnswer(); + $this->assertTrue($poll->show_results_to_participants); + + $this->assertNull($this->participantState($poll)['results']); + $this->get('/p/' . $poll->join_code); + $this->assertResponseContains('Anonymes Feedback'); + $this->assertResponseNotContains('Geheimes'); + + // Not even after answering themselves. + $this->post('/p/' . $poll->join_code . '/vote', ['slide_id' => $poll->current_slide_id, 'text' => 'Meins']); + $this->assertNull($this->participantState($poll)['results']); + $this->assertResponseNotContains('Geheimes'); + } + + public function testEmbedSeesOnlyTheCount(): void + { + [$poll, , $slide] = $this->privateSlideWithAnswer(); + $key = $this->pollsTable()->ensureEmbedKey($poll); + + $this->get("/e/$key/{$slide->id}/state"); + + $this->assertResponseOk(); + $this->assertResponseNotContains('Geheimes'); + } + + public function testAdminReadsAnswers(): void + { + [, $token, $slide] = $this->privateSlideWithAnswer(); + + $this->get("/admin/$token"); + $this->assertResponseContains("/slides/{$slide->id}/answers"); + + $this->get("/admin/$token/slides/{$slide->id}/answers"); + $this->assertResponseOk(); + $this->assertResponseContains('Geheimes <Feedback>'); + $this->assertHeader('Referrer-Policy', 'no-referrer'); + } + + public function testAnswersPageOnlyForOwnOpenSlides(): void + { + [$poll, $token] = $this->createPoll(); + $choice = $this->createSlide($poll); + [$other] = $this->createPoll('Andere'); + $foreign = $this->createSlide($other, Slide::TYPE_OPEN); + + $this->get("/admin/$token/slides/{$choice->id}/answers"); + $this->assertResponseCode(404); + $this->get("/admin/$token/slides/{$foreign->id}/answers"); + $this->assertResponseCode(404); + } + + public function testPublicOpenQuestionStillShowsAnswers(): void + { + [$poll, $token] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_OPEN); + $poll = $this->goLive($poll, $slide); + (new VoteService())->submit($poll, $slide, VoteService::participantHash(str_repeat('b', 32), $poll->id), ['text' => 'Offen']); + + $this->get("/admin/$token/state"); + $state = json_decode((string)$this->_response->getBody(), true); + + $this->assertFalse($state['results']['private']); + $this->assertSame('Offen', $state['results']['answers'][0]['text']); + } +} diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index 85e6654..8bcc9fb 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -456,6 +456,15 @@ button.qa-steps__step { cursor: pointer; } button.qa-steps__step:hover:not(:disabled) { color: var(--text); border-color: var(--text); } .qa-steps .is-current .qa-steps__step { background: var(--qa); border-color: var(--qa); color: var(--surface); font-weight: 700; cursor: default; } +.private-summary { text-align: center; } +.private-summary p { margin: 0 0 .3em; } +.private-summary__count { font-size: 3em; font-weight: 800; line-height: 1; color: var(--c2); font-variant-numeric: tabular-nums; } +.private-summary__hint { color: var(--muted); font-size: .85em; } +.private-hint { margin-top: 8px; } +.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; } + /* ---------------------------------------------------------------- presenter */ /* Dark by default (beamer); the presenter's own switch can make it light (data-theme="light"). */ body.present { diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index f0b1289..3c1602d 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -499,6 +499,15 @@ }, open: function (results) { + if (results.private) { + // Anonymous feedback: the texts 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 ? 'anonyme Antwort' : 'anonyme Antworten') + ' eingegangen' }), + el('p', { class: 'private-summary__hint', text: '🔒 Antworten werden nicht öffentlich angezeigt.' }) + ]); + } return el('ul', { class: 'answers' }, results.answers.map(function (answer) { return el('li', { class: 'answer', text: answer.text }); }));