From ad5223906ced19d9b0a7cc60b195cfe9aae6dfb1 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Wed, 11 Nov 2026 12:07:44 +0100 Subject: [PATCH] Let participants withdraw their own word cloud terms Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 3 ++ src/Service/PollStateService.php | 12 ++++- src/Service/VoteService.php | 36 +++++++++++++++ templates/Participate/view.php | 5 +++ tests/TestCase/Service/VoteServiceTest.php | 45 +++++++++++++++++++ webroot/css/plenario.css | 7 +++ webroot/js/plenario.js | 52 ++++++++++++++++++++-- 7 files changed, 154 insertions(+), 6 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 50d6c5f..89d3bb5 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -134,6 +134,9 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures Downvotes (`vote=down`, Setting `allow_downvotes`, nur zusammen mit Upvotes): Zeile mit `numeric_value = VoteService::DOWNVOTE` (-1), nur auf fremde, vorhandene Begriffe, ein Rating pro Begriff und Person. Wortgröße richtet sich nur nach den Upvotes (`count`), Downvotes stehen separat in `down`. + `vote=remove` nimmt den eigenen Eintrag zu einem Begriff zurück (genannt/Upvote oder Downvote, `VoteService::removeWord()`, + löscht nur Zeilen mit dem eigenen `participant_hash`); Tippfehler = entfernen und neu eingeben, kein Bearbeiten (die Zeile + ist zugleich die Stimme). Teilnehmende bekommen dafür `words.own` (eigene Begriffe wie eingegeben) und sehen sie als Chips mit „ד. - Quiz: Phasen `ready` → `running` → `finished` (`Slide::quizPhase()`). Neue Quiz-Folien starten geschlossen; erst der Presenter startet (`control` open/toggle → `Slide::setVotingOpen()` setzt Start + Deadline aus `time_limit`, erneutes Öffnen eines laufenden Quiz startet den Timer nicht neu). „Offen“ heißt immer diff --git a/src/Service/PollStateService.php b/src/Service/PollStateService.php index bc0d877..f4b87a9 100644 --- a/src/Service/PollStateService.php +++ b/src/Service/PollStateService.php @@ -321,8 +321,8 @@ class PollStateService * * @param \App\Model\Entity\Slide $slide Word cloud slide * @param string $participantHash Participant hash - * @return array{mine: list, down: list, allowUpvotes: bool, allowDownvotes: bool} - * `mine` (named/upvoted) and `down` are lower case word keys + * @return array{mine: list, down: list, own: list, allowUpvotes: bool, allowDownvotes: bool} + * `mine` (named/upvoted) and `down` are lower case word keys, `own` the same entries as typed */ public function wordState(Slide $slide, string $participantHash): array { @@ -330,10 +330,18 @@ class PollStateService $mine = array_values(array_unique(array_map(VoteService::wordKey(...), $votes['up']))); $down = array_values(array_unique(array_map(VoteService::wordKey(...), $votes['down']))); $allowUpvotes = (bool)$slide->setting('allow_upvotes'); + // Own entries as typed (first spelling per key), oldest first, so they can be withdrawn. + $own = []; + foreach (['up' => false, 'down' => true] as $kind => $isDown) { + foreach ($votes[$kind] as $text) { + $own[VoteService::wordKey($text)] ??= ['text' => $text, 'down' => $isDown]; + } + } return [ 'mine' => $mine, 'down' => $down, + 'own' => array_values($own), 'allowUpvotes' => $allowUpvotes, 'allowDownvotes' => $allowUpvotes && (bool)$slide->setting('allow_downvotes'), ]; diff --git a/src/Service/VoteService.php b/src/Service/VoteService.php index a7cbe4c..3fbf8b9 100644 --- a/src/Service/VoteService.php +++ b/src/Service/VoteService.php @@ -22,6 +22,7 @@ use Cake\Utility\Security; * exists in the cloud is an upvote – both are stored as the same row. * `vote=down` stores a downvote (numeric_value = DOWNVOTE) for an existing * word of someone else. Each participant rates a word only once (up or down). + * `vote=remove` withdraws the participant's own entry for a word (see removeWord()). * At most MAX_WORDS_PER_REQUEST terms per request and MAX_WORDS per participant and slide. * - checks and insert run in one transaction with the slide row locked (see lockSlide()), * so concurrent or repeated requests cannot store an answer twice. @@ -529,6 +530,11 @@ class VoteService if (($data['vote'] ?? null) === 'down') { return $this->downvoteRows($slide, self::stringField($data, 'word'), $existing, $downvoted); } + if (($data['vote'] ?? null) === 'remove') { + $this->removeWord($slide, $participantHash, self::stringField($data, 'word')); + + return []; + } $input = array_filter( array_merge((array)($data['words'] ?? []), (array)($data['word'] ?? [])), @@ -574,6 +580,36 @@ class VoteService return array_map(fn(string $word) => ['text_value' => $word], $words); } + /** + * Word cloud: withdraws the participant's own entry for a term – named/upvoted or downvoted – + * e.g. to fix a typo by entering it again. Only the participant's own rows are touched; the term + * stays in the cloud as long as others named it. + * + * @param \App\Model\Entity\Slide $slide Slide + * @param string $participantHash Participant hash + * @param string $word Word to withdraw + * @return void + * @throws \App\Service\VoteException + */ + protected function removeWord(Slide $slide, string $participantHash, string $word): void + { + $key = self::wordKey($word); + $ids = []; + $own = $this->Responses->find() + ->select(['id', 'text_value']) + ->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash, 'text_value IS NOT' => null]) + ->disableHydration(); + foreach ($own as $row) { + if (self::wordKey((string)$row['text_value']) === $key) { + $ids[] = $row['id']; + } + } + if ($key === '' || $ids === []) { + throw new VoteException(__('Diesen Begriff hast du nicht genannt.')); + } + $this->Responses->deleteAll(['id IN' => $ids]); + } + /** * @param \App\Model\Entity\Slide $slide Slide * @param string $word Word to downvote diff --git a/templates/Participate/view.php b/templates/Participate/view.php index 354db4f..3d20416 100644 --- a/templates/Participate/view.php +++ b/templates/Participate/view.php @@ -218,6 +218,11 @@ $countdown = function (?int $remaining): string {

Form->button(__('Begriff senden'), ['class' => 'button button--primary button--block']) ?> Form->end() ?> +

✓

isVotingOpen()) : diff --git a/tests/TestCase/Service/VoteServiceTest.php b/tests/TestCase/Service/VoteServiceTest.php index 701ad33..347e4a4 100644 --- a/tests/TestCase/Service/VoteServiceTest.php +++ b/tests/TestCase/Service/VoteServiceTest.php @@ -279,4 +279,49 @@ class VoteServiceTest extends TestCase $this->assertEquals($slide->modified, $this->slidesTable()->get($slide->id)->modified); $this->assertSame($slide->position, $this->slidesTable()->get($slide->id)->position); } + + public function testWordcloudOwnEntryCanBeRemoved(): void + { + [$poll] = $this->createPoll(); + $slide = $this->createSlide($poll, Slide::TYPE_WORDCLOUD); + $poll = $this->goLive($poll, $slide); + $service = new VoteService(); + $service->submit($poll, $slide, 'p1', ['word' => 'Kafee']); + $service->submit($poll, $slide, 'p1', ['word' => 'Tee']); + $service->submit($poll, $slide, 'p2', ['word' => 'tee']); + $service->submit($poll, $slide, 'p2', ['word' => 'Kafee', 'vote' => 'down']); + + $state = (new PollStateService())->wordState($slide, 'p1'); + $this->assertSame([['text' => 'Kafee', 'down' => false], ['text' => 'Tee', 'down' => false]], $state['own']); + + // Only the own row goes: "Tee" stays with the other upvote, "Kafee" keeps p2's downvote. + $service->submit($poll, $slide, 'p1', ['word' => 'TEE', 'vote' => 'remove']); + $service->submit($poll, $slide, 'p1', ['word' => 'Kafee', 'vote' => 'remove']); + $results = (new ResultsService())->forSlide($slide); + $this->assertSame([['tee', 1, 0]], array_map(fn($w) => [$w['text'], $w['count'], $w['down']], $results['words'])); + $this->assertSame([], (new PollStateService())->wordState($slide, 'p1')['own']); + $this->assertSame(2, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); + + // A downvote can be withdrawn too; afterwards the word can be named again. + $service->submit($poll, $slide, 'p2', ['word' => 'kafee', 'vote' => 'remove']); + $this->assertSame([['text' => 'tee', 'down' => false]], (new PollStateService())->wordState($slide, 'p2')['own']); + $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee']); + $this->assertSame(['kaffee'], (new PollStateService())->wordState($slide, 'p1')['mine']); + + // Removing again (or someone else's word) changes nothing. + foreach ([['p1', 'Tee'], ['p3', 'Kaffee'], ['p1', '']] as [$participant, $word]) { + try { + $service->submit($poll, $slide, $participant, ['word' => $word, 'vote' => 'remove']); + $this->fail('VoteException expected for ' . $participant . '/' . $word); + } catch (VoteException $e) { + $this->assertSame('Diesen Begriff hast du nicht genannt.', $e->getMessage()); + } + } + $this->assertSame(2, $this->fetchTable('Responses')->find()->where(['slide_id' => $slide->id])->count()); + + $this->slidesTable()->updateAll(['is_voting_open' => false], ['id' => $slide->id]); + $slide = $this->slidesTable()->get($slide->id); + $this->expectException(VoteException::class); + $service->submit($poll, $slide, 'p1', ['word' => 'Kaffee', 'vote' => 'remove']); + } } diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index 63930ff..679be68 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -336,6 +336,13 @@ textarea { resize: vertical; } .suggestion__meta { font-size: .8rem; color: var(--muted); white-space: nowrap; align-self: center; } .word-message { min-height: 1.4em; margin: 8px 0 0; font-size: .9rem; color: var(--ok); } .word-message.is-error { color: var(--danger); } +.own-words { margin-top: 20px; } +.own-words__heading { font-size: 1rem; margin: 0 0 4px; } +.own-words__list { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-wrap: wrap; gap: 8px; } +.own-words__item { display: inline-flex; align-items: center; gap: 2px; padding: 2px 2px 2px 12px; border-radius: 999px; background: var(--primary-soft); overflow-wrap: anywhere; } +.own-words__item.is-down { background: var(--surface-2); color: var(--muted); } +.own-words__remove { min-width: 36px; min-height: 36px; border: 0; border-radius: 999px; background: transparent; color: inherit; font-size: 1.2rem; line-height: 1; cursor: pointer; } +.own-words__remove:hover, .own-words__remove:focus-visible { background: var(--border); } .vote-form textarea { margin: 12px 0 16px; } .vote-form .button { margin-top: 12px; } .scale { display: flex; gap: 6px; margin: 20px 0 6px; } diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index 04bc8a0..4bb358e 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -1655,6 +1655,7 @@ function renderResults(state) { current = state; renderQa(state); + renderOwnWords(state); if (!card || !container) { return; } @@ -1702,14 +1703,17 @@ sending = true; var slideId = wordForm.querySelector('[name=slide_id]').value; var data = { slide_id: slideId, word: text }; - if (vote === 'down') { - data.vote = 'down'; + if (vote) { + data.vote = vote; } postJson(voteUrl, data).then(function (response) { - var success = vote === 'down' ? '✓ „' + text + '“ abgelehnt.' : '✓ „' + text + '“ ist angekommen.'; + var success = { + down: '✓ „' + text + '“ abgelehnt.', + remove: '✓ „' + text + '“ entfernt.' + }[vote] || '✓ „' + text + '“ ist angekommen.'; message.textContent = response.ok ? success : response.message; message.classList.toggle('is-error', !response.ok); - if (response.ok && vote !== 'down') { + if (response.ok && !vote) { input.value = ''; closeSuggestions(); } @@ -1722,6 +1726,46 @@ }); } + // Own terms with a remove button (fix a typo by removing and entering again). + var ownWords = root.querySelector('[data-own-words]'); + var ownWordsRendered = ''; + + function renderOwnWords(state) { + if (!ownWords || !state.words) { + return; + } + var own = state.words.own || []; + var key = JSON.stringify(own); + if (key === ownWordsRendered) { + return; + } + ownWordsRendered = key; + // Keep keyboard focus in the list after removing a term. + var hadFocus = ownWords.contains(document.activeElement); + var listNode = ownWords.querySelector('[data-own-words-list]'); + listNode.textContent = ''; + ownWords.hidden = own.length === 0; + own.forEach(function (word) { + var label = word.down ? 'Ablehnung von „' + word.text + '“ zurücknehmen' : '„' + word.text + '“ entfernen'; + var remove = el('button', { type: 'button', class: 'own-words__remove', 'aria-label': label, title: label }, [ + el('span', { 'aria-hidden': 'true', text: '×' }) + ]); + remove.addEventListener('click', function () { + sendWord(word.text, 'remove'); + }); + listNode.appendChild(el('li', { class: 'own-words__item' + (word.down ? ' is-down' : '') }, [ + el('span', { text: (word.down ? '− ' : '') + word.text }), + remove + ])); + }); + if (hadFocus) { + var next = listNode.querySelector('button') || input; + if (next) { + next.focus(); + } + } + } + function applyWordState(state) { if (state.version !== version || !state.words) { // Slide changed or voting closed.