Feature/edit scale comment #42

Merged
gnilebein merged 6 commits from feature/edit-scale-comment into main 2026-11-11 12:21:05 +01:00
6 changed files with 272 additions and 15 deletions
Showing only changes of commit 3a8962976e - Show all commits

No files matched your search

+4
View File
@@ -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`, (→/←, 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()`. damit Teilnehmerseiten nicht neu laden. Teilnehmende bekommen nie `results`, sondern `qa` aus `PollStateService::qaState()`.
Zurücksetzen startet wieder bei `collect`. 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), - 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). `autoplay`. Startzeit-Feld leer = Timecode aus dem Link (`t`/`start`), sonst gewinnt das Feld (`1:30`, `2` = Minuten).
+2
View File
@@ -138,6 +138,8 @@ class ParticipateController extends AppController
!$slide->isQa() => __('Danke für deine Antwort!'), !$slide->isQa() => __('Danke für deine Antwort!'),
$this->request->getData('vote') === 'up' => __('Deine Stimme ist angekommen.'), $this->request->getData('vote') === 'up' => __('Deine Stimme ist angekommen.'),
$this->request->getData('vote') === 'remove' => __('Deine Stimme wurde zurückgenommen.'), $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!'), default => __('Danke, deine Frage ist angekommen!'),
}; };
} catch (VoteException $e) { } catch (VoteException $e) {
+70 -14
View File
@@ -33,7 +33,8 @@ use Cake\Utility\Security;
* questions of others (own ones too with setting `allow_self_upvote`), at most * 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, * `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); * 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. * - 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()). * 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 * - 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; $vote = $data['vote'] ?? null;
$own = $this->Responses->qaVotesOf($slide->id, $participantHash); $own = $this->Responses->qaVotesOf($slide->id, $participantHash);
if ($vote === 'edit' || $vote === 'delete') {
$this->editQuestion($slide, $own['questions'], $vote, $data);
return [];
}
if ($vote === null) { if ($vote === null) {
if ($stage !== Slide::QA_COLLECT) { if ($stage !== Slide::QA_COLLECT) {
throw new VoteException(__('Es können keine neuen Fragen mehr eingereicht werden.')); throw new VoteException(__('Es können keine neuen Fragen mehr eingereicht werden.'));
} }
$text = trim((string)preg_replace('/\s+/u', ' ', self::stringField($data, 'question'))); $text = $this->qaQuestionText($slide, $data);
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.'));
}
}
if (count($own['questions']) >= self::MAX_QA_QUESTIONS) { if (count($own['questions']) >= self::MAX_QA_QUESTIONS) {
throw new VoteException(__('Du kannst höchstens {0} Fragen stellen.', 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]]; 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<string, mixed> $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<int> $ownQuestions Ids of the participant's questions
* @param string $action `edit` or `delete`
* @param array<string, mixed> $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 * Normalized key of a word cloud term: whitespace collapsed, trimmed, cut to
* MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison. * MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison.
+89
View File
@@ -297,4 +297,93 @@ class QaTest extends TestCase
$slide = $this->slidesTable()->get($slide->id); $slide = $this->slidesTable()->get($slide->id);
$this->assertSame([Slide::QA_COLLECT, true, null], [$slide->qaStage(), $slide->is_voting_open, $slide->focus_response_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'));
}
} }
+6
View File
@@ -496,6 +496,12 @@ textarea { resize: vertical; }
.qa-item.is-current { box-shadow: inset 0 0 0 2px var(--c1); } .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-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-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 { 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__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); } .qa-stepper.is-active .qa-stepper__count { color: var(--qa); }
+101 -1
View File
@@ -1385,6 +1385,9 @@
var qaForm = qaRoot ? qaRoot.querySelector('[data-qa-form]') : null; var qaForm = qaRoot ? qaRoot.querySelector('[data-qa-form]') : null;
var qaRendered = ''; var qaRendered = '';
var qaSending = false; 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) { function qaSay(text, isError) {
qaMessage.textContent = text; qaMessage.textContent = text;
@@ -1403,6 +1406,9 @@
window.location.reload(); window.location.reload();
return null; return null;
} }
if (response.ok && data.vote === 'edit') {
qaEditing = null;
}
renderQa(response.state); renderQa(response.state);
return response; return response;
}).catch(function () { }).catch(function () {
@@ -1419,6 +1425,10 @@
if (!qaList || !qa) { if (!qaList || !qa) {
return; return;
} }
qaLatest = state;
if (qaEditing !== null) {
return; // rendered once the edit is saved or cancelled
}
var key = JSON.stringify(qa); var key = JSON.stringify(qa);
if (key === qaRendered) { if (key === qaRendered) {
return; return;
@@ -1437,11 +1447,16 @@
} }
qaList.appendChild(el('ul', { class: 'qa-list' }, qa.questions.map(function (question) { qaList.appendChild(el('ul', { class: 'qa-list' }, qa.questions.map(function (question) {
var mine = qa.mine.indexOf(question.id) >= 0; 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 }), el('span', { class: 'qa-item__text', text: question.text }),
mine ? el('span', { class: 'qa-own', text: 'deine Frage' }) : null mine ? el('span', { class: 'qa-own', text: 'deine Frage' }) : null
]); ]);
if (mine) {
item.appendChild(qaOwnActions(item, question));
}
return item;
}))); })));
restoreQaFocus(focused);
return; return;
} }
@@ -1528,6 +1543,10 @@
}))); })));
} }
restoreQaFocus(focused);
}
function restoreQaFocus(focused) {
if (focused) { if (focused) {
var again = qaList.querySelector('[data-question-id="' + focused + '"]'); var again = qaList.querySelector('[data-question-id="' + focused + '"]');
if (again && !again.disabled) { 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) { if (qaForm) {
qaForm.addEventListener('submit', function (event) { qaForm.addEventListener('submit', function (event) {
event.preventDefault(); event.preventDefault();