From 3a8962976e35ab368fb007c2743b2d0ac54127b2 Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Wed, 11 Nov 2026 12:02:59 +0100 Subject: [PATCH] Let participants edit or delete their Q&A questions while collecting Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 4 + src/Controller/ParticipateController.php | 2 + src/Service/VoteService.php | 84 +++++++++++++++---- tests/TestCase/Service/QaTest.php | 89 ++++++++++++++++++++ webroot/css/plenario.css | 6 ++ webroot/js/plenario.js | 102 ++++++++++++++++++++++- 6 files changed, 272 insertions(+), 15 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 9cd6d23..50d6c5f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -167,6 +167,10 @@ tests/ PHPUnit; PlenarioTestTrait erzeugt Testdaten, Fixtures (→/←, Klicker) zuerst durch die Fragen, erst danach durch die Folien. Fokuswechsel per `updateAll` ohne `modified`, damit Teilnehmerseiten nicht neu laden. Teilnehmende bekommen nie `results`, sondern `qa` aus `PollStateService::qaState()`. Zurücksetzen startet wieder bei `collect`. + Eigene Fragen bearbeiten/löschen (`vote=edit|delete` + `question_id`, `VoteService::editQuestion()`) nur in `collect`: + danach könnten Upvotes bzw. die Besprechung an einem anderen Text hängen. Bearbeiten prüft Dubletten ohne die eigene Frage, + Löschen entfernt auch übrig gebliebene Upvotes (Presenter von `vote` zurück nach `collect`). Im JS hält `qaEditing` das + Neu-Rendern beim Polling an, solange eine Frage bearbeitet wird. - YouTube-Folie (`youtube`): nicht interaktiv (wie `content`). Settings `url` (wie eingegeben), `video_id`, `start` (Sekunden), `autoplay`. Startzeit-Feld leer = Timecode aus dem Link (`t`/`start`), sonst gewinnt das Feld (`1:30`, `2` = Minuten). diff --git a/src/Controller/ParticipateController.php b/src/Controller/ParticipateController.php index f16ed21..8da85f2 100644 --- a/src/Controller/ParticipateController.php +++ b/src/Controller/ParticipateController.php @@ -138,6 +138,8 @@ class ParticipateController extends AppController !$slide->isQa() => __('Danke für deine Antwort!'), $this->request->getData('vote') === 'up' => __('Deine Stimme ist angekommen.'), $this->request->getData('vote') === 'remove' => __('Deine Stimme wurde zurückgenommen.'), + $this->request->getData('vote') === 'edit' => __('Deine Frage wurde geändert.'), + $this->request->getData('vote') === 'delete' => __('Deine Frage wurde gelöscht.'), default => __('Danke, deine Frage ist angekommen!'), }; } catch (VoteException $e) { diff --git a/src/Service/VoteService.php b/src/Service/VoteService.php index 6de31bd..a7cbe4c 100644 --- a/src/Service/VoteService.php +++ b/src/Service/VoteService.php @@ -33,7 +33,8 @@ use Cake\Utility\Security; * questions of others (own ones too with setting `allow_self_upvote`), at most * `max_upvotes` in total, freely distributed (several on one question are allowed, * at most `max_upvotes_per_question` if set; `max_upvotes` = 0 means no voting stage); - * `remove` withdraws one own upvote of the question. + * `remove` withdraws one own upvote of the question. `vote=edit|delete` + `question_id` + * changes or deletes an own question, only while collecting (see editQuestion()). * - ordering: `order` = all option ids, each exactly once, in the participant's order. * Stored as one row with the ids comma separated in `text_value` (see parseOrder()). * - open question and scale with comment field: the own answer text or comment can be changed @@ -353,23 +354,16 @@ class VoteService $vote = $data['vote'] ?? null; $own = $this->Responses->qaVotesOf($slide->id, $participantHash); + if ($vote === 'edit' || $vote === 'delete') { + $this->editQuestion($slide, $own['questions'], $vote, $data); + + return []; + } if ($vote === null) { if ($stage !== Slide::QA_COLLECT) { throw new VoteException(__('Es können keine neuen Fragen mehr eingereicht werden.')); } - $text = trim((string)preg_replace('/\s+/u', ' ', self::stringField($data, 'question'))); - if ($text === '') { - throw new VoteException(__('Bitte gib eine Frage ein.')); - } - if (mb_strlen($text) > self::MAX_TEXT_LENGTH) { - throw new VoteException(__('Die Frage darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH)); - } - $key = mb_strtolower($text); - foreach ($this->Responses->questionsOf($slide->id)->select(['text_value'])->disableHydration() as $row) { - if (mb_strtolower((string)$row['text_value']) === $key) { - throw new VoteException(__('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.')); - } - } + $text = $this->qaQuestionText($slide, $data); if (count($own['questions']) >= self::MAX_QA_QUESTIONS) { throw new VoteException(__('Du kannst höchstens {0} Fragen stellen.', self::MAX_QA_QUESTIONS)); } @@ -431,6 +425,68 @@ class VoteService return [['parent_id' => $id]]; } + /** + * Q&A: validated question text from `question` (whitespace collapsed), unique on the slide + * (case-insensitive; `$except` = id of the question being edited). + * + * @param \App\Model\Entity\Slide $slide Slide + * @param array $data Request data + * @param int|null $except Question id to ignore in the duplicate check + * @return string + * @throws \App\Service\VoteException + */ + protected function qaQuestionText(Slide $slide, array $data, ?int $except = null): string + { + $text = trim((string)preg_replace('/\s+/u', ' ', self::stringField($data, 'question'))); + if ($text === '') { + throw new VoteException(__('Bitte gib eine Frage ein.')); + } + if (mb_strlen($text) > self::MAX_TEXT_LENGTH) { + throw new VoteException(__('Die Frage darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH)); + } + $key = mb_strtolower($text); + $others = $this->Responses->questionsOf($slide->id)->select(['id', 'text_value'])->disableHydration(); + foreach ($others as $row) { + if ($row['id'] !== $except && mb_strtolower((string)$row['text_value']) === $key) { + throw new VoteException(__('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.')); + } + } + + return $text; + } + + /** + * Q&A: changes (`edit`, new text in `question`) or deletes (`delete`) an own question, + * only while collecting – later the question may already carry upvotes or be discussed. + * Deleting also removes its upvotes (left over if the presenter stepped back from voting). + * + * @param \App\Model\Entity\Slide $slide Slide + * @param list $ownQuestions Ids of the participant's questions + * @param string $action `edit` or `delete` + * @param array $data Request data (`question_id`, `question`) + * @return void + * @throws \App\Service\VoteException + */ + protected function editQuestion(Slide $slide, array $ownQuestions, string $action, array $data): void + { + if ($slide->qaStage() !== Slide::QA_COLLECT) { + throw new VoteException(__('Fragen können nur geändert werden, solange sie gesammelt werden.')); + } + $id = is_scalar($data['question_id'] ?? null) ? filter_var($data['question_id'], FILTER_VALIDATE_INT) : false; + if ($id === false || !in_array($id, $ownQuestions, true)) { + throw new VoteException(__('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.')); + } + $question = $this->Responses->get($id); + if ($action === 'delete') { + $this->Responses->deleteAll(['slide_id' => $slide->id, 'parent_id' => $id]); + $this->Responses->deleteOrFail($question); + + return; + } + $question->text_value = $this->qaQuestionText($slide, $data, $id); + $this->Responses->saveOrFail($question); + } + /** * Normalized key of a word cloud term: whitespace collapsed, trimmed, cut to * MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison. diff --git a/tests/TestCase/Service/QaTest.php b/tests/TestCase/Service/QaTest.php index 409735c..10f2f1a 100644 --- a/tests/TestCase/Service/QaTest.php +++ b/tests/TestCase/Service/QaTest.php @@ -297,4 +297,93 @@ class QaTest extends TestCase $slide = $this->slidesTable()->get($slide->id); $this->assertSame([Slide::QA_COLLECT, true, null], [$slide->qaStage(), $slide->is_voting_open, $slide->focus_response_id]); } + + public function testOwnQuestionCanBeEditedWhileCollecting(): void + { + [$poll, $slide] = $this->createQa(); + $votes = new VoteService(); + $mine = $this->ask($poll, $slide, 'p1', 'Wan gehts los?'); + $other = $this->ask($poll, $slide, 'p2', 'Gibt es Kaffee?'); + + $votes->submit($poll, $slide, 'p1', ['vote' => 'edit', 'question_id' => $mine, 'question' => ' Wann geht es los? ']); + $responses = $this->fetchTable('Responses'); + $this->assertSame('Wann geht es los?', $responses->get($mine)->get('text_value')); + // Repeating the same edit is harmless, case-only changes of the own question are fine. + $votes->submit($poll, $slide, 'p1', ['vote' => 'edit', 'question_id' => $mine, 'question' => 'WANN geht es los?']); + $this->assertSame(2, $responses->find()->where(['slide_id' => $slide->id])->count()); + + $this->assertVoteFails('Diese Frage wurde schon gestellt. Du kannst sie gleich beim Abstimmen unterstützen.', fn() => $votes->submit( + $poll, + $slide, + 'p1', + ['vote' => 'edit', 'question_id' => $mine, 'question' => 'gibt es kaffee?'], + )); + $this->assertVoteFails('Bitte gib eine Frage ein.', fn() => $votes->submit( + $poll, + $slide, + 'p1', + ['vote' => 'edit', 'question_id' => $mine, 'question' => ' '], + )); + $this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit( + $poll, + $slide, + 'p1', + ['vote' => 'edit', 'question_id' => $other, 'question' => 'Gekapert'], + )); + $this->assertSame('Gibt es Kaffee?', $responses->get($other)->get('text_value')); + } + + public function testOwnQuestionCanBeDeletedWhileCollecting(): void + { + [$poll, $slide] = $this->createQa(); + $votes = new VoteService(); + $mine = $this->ask($poll, $slide, 'p1', 'Weg damit'); + $other = $this->ask($poll, $slide, 'p2', 'Bleibt'); + + $this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit( + $poll, + $slide, + 'p1', + ['vote' => 'delete', 'question_id' => $other], + )); + + // Upvotes left over from a voting stage the presenter stepped back from go with it. + $slide = $this->stage($slide, Slide::QA_VOTE); + $this->vote($poll, $slide, 'p2', $mine); + $slide = $this->stage($slide, Slide::QA_COLLECT); + + $votes->submit($poll, $slide, 'p1', ['vote' => 'delete', 'question_id' => $mine]); + $responses = $this->fetchTable('Responses'); + $this->assertSame([$other], $responses->find()->where(['slide_id' => $slide->id])->all()->extract('id')->toList()); + $state = (new PollStateService())->qaState($slide, 'p1'); + $this->assertSame([], $state['mine']); + + // Deleting again is answered with a message, nothing else happens. + $this->assertVoteFails('Diese Frage gibt es nicht mehr oder sie stammt nicht von dir.', fn() => $votes->submit( + $poll, + $slide, + 'p1', + ['vote' => 'delete', 'question_id' => $mine], + )); + } + + public function testQuestionsAreFixedOnceVotingStarts(): void + { + [$poll, $slide] = $this->createQa(); + $votes = new VoteService(); + $mine = $this->ask($poll, $slide, 'p1', 'Feste Frage'); + + foreach ([Slide::QA_VOTE, Slide::QA_DISCUSS] as $stage) { + $slide = $this->stage($slide, $stage); + $poll = $this->fetchTable('Polls')->get($poll->id); + foreach (['edit', 'delete'] as $action) { + try { + $votes->submit($poll, $slide, 'p1', ['vote' => $action, 'question_id' => $mine, 'question' => 'Geändert']); + $this->fail('VoteException expected'); + } catch (VoteException) { + } + } + } + $this->assertSame('Feste Frage', $this->fetchTable('Responses')->get($mine)->get('text_value')); + } } diff --git a/webroot/css/plenario.css b/webroot/css/plenario.css index f2130a8..63930ff 100644 --- a/webroot/css/plenario.css +++ b/webroot/css/plenario.css @@ -496,6 +496,12 @@ textarea { resize: vertical; } .qa-item.is-current { box-shadow: inset 0 0 0 2px var(--c1); } .qa-votes { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; color: var(--qa); position: relative; } .qa-own { font-size: .85rem; color: var(--muted); white-space: nowrap; } +.qa-item:has(.qa-own-actions) { flex-wrap: wrap; } +.qa-own-actions { flex: none; display: inline-flex; gap: 4px; margin-left: auto; } +.qa-own-actions .button { min-height: 36px; } +.qa-item.is-editing { display: block; } +.qa-edit textarea { width: 100%; } +.qa-edit__actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; } .qa-stepper { flex: none; display: inline-flex; align-items: center; gap: 6px; } .qa-stepper__count { min-width: 3.2em; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--muted); } .qa-stepper.is-active .qa-stepper__count { color: var(--qa); } diff --git a/webroot/js/plenario.js b/webroot/js/plenario.js index c8d97ec..04bc8a0 100644 --- a/webroot/js/plenario.js +++ b/webroot/js/plenario.js @@ -1385,6 +1385,9 @@ var qaForm = qaRoot ? qaRoot.querySelector('[data-qa-form]') : null; var qaRendered = ''; var qaSending = false; + // Own question being edited: polling must not re-render the list (and wipe the draft) meanwhile. + var qaEditing = null; + var qaLatest = null; function qaSay(text, isError) { qaMessage.textContent = text; @@ -1403,6 +1406,9 @@ window.location.reload(); return null; } + if (response.ok && data.vote === 'edit') { + qaEditing = null; + } renderQa(response.state); return response; }).catch(function () { @@ -1419,6 +1425,10 @@ if (!qaList || !qa) { return; } + qaLatest = state; + if (qaEditing !== null) { + return; // rendered once the edit is saved or cancelled + } var key = JSON.stringify(qa); if (key === qaRendered) { return; @@ -1437,11 +1447,16 @@ } qaList.appendChild(el('ul', { class: 'qa-list' }, qa.questions.map(function (question) { var mine = qa.mine.indexOf(question.id) >= 0; - return el('li', { class: 'qa-item' + (mine ? ' is-mine' : '') }, [ + var item = el('li', { class: 'qa-item' + (mine ? ' is-mine' : '') }, [ el('span', { class: 'qa-item__text', text: question.text }), mine ? el('span', { class: 'qa-own', text: 'deine Frage' }) : null ]); + if (mine) { + item.appendChild(qaOwnActions(item, question)); + } + return item; }))); + restoreQaFocus(focused); return; } @@ -1528,6 +1543,10 @@ }))); } + restoreQaFocus(focused); + } + + function restoreQaFocus(focused) { if (focused) { var again = qaList.querySelector('[data-question-id="' + focused + '"]'); if (again && !again.disabled) { @@ -1536,6 +1555,87 @@ } } + /** Ends editing and shows the latest state, focus back on the question's edit button. */ + function qaStopEditing(id) { + qaEditing = null; + qaRendered = ''; + renderQa(qaLatest); + restoreQaFocus(id + '-edit'); + } + + /** + * Collect stage: edit and delete buttons for an own question. Editing swaps the item + * content for a small form in place. + */ + function qaOwnActions(item, question) { + var button = function (action, text, extraClass) { + return el('button', { + type: 'button', + class: 'button button--small button--ghost' + (extraClass || ''), + 'aria-label': text + ': ' + question.text, + 'data-question-id': question.id + '-' + action, + text: text + }); + }; + var edit = button('edit', 'Bearbeiten'); + var remove = button('delete', 'Löschen', ' button--danger'); + + edit.addEventListener('click', function () { + qaEditing = question.id; + qaSay('', false); + var field = el('textarea', { name: 'question', rows: '3', maxlength: '500', required: '', 'aria-label': 'Deine Frage bearbeiten' }); + field.value = question.text; + var form = el('form', { class: 'qa-edit' }, [ + field, + el('div', { class: 'qa-edit__actions' }, [ + el('button', { type: 'submit', class: 'button button--small button--primary', text: 'Speichern' }), + el('button', { type: 'button', class: 'button button--small', 'data-qa-cancel': '', text: 'Abbrechen' }) + ]) + ]); + form.addEventListener('submit', function (event) { + event.preventDefault(); + var text = field.value.trim(); + if (!text) { + qaSay('Bitte gib eine Frage ein.', true); + field.focus(); + return; + } + if (text === question.text) { + qaStopEditing(question.id); + return; + } + sendQa({ vote: 'edit', question_id: question.id, question: text }).then(function (response) { + if (response && response.ok) { + restoreQaFocus(question.id + '-edit'); + } else { + field.focus(); + } + }); + }); + form.querySelector('[data-qa-cancel]').addEventListener('click', function () { + qaStopEditing(question.id); + }); + field.addEventListener('keydown', function (event) { + if (event.key === 'Escape') { + qaStopEditing(question.id); + } + }); + item.textContent = ''; + item.classList.add('is-editing'); + item.appendChild(form); + field.focus(); + field.setSelectionRange(field.value.length, field.value.length); + }); + + remove.addEventListener('click', function () { + if (window.confirm('Deine Frage wirklich löschen?\n\n„' + question.text + '“')) { + sendQa({ vote: 'delete', question_id: question.id }); + } + }); + + return el('span', { class: 'qa-own-actions' }, [edit, remove]); + } + if (qaForm) { qaForm.addEventListener('submit', function (event) { event.preventDefault();