Let participants edit or delete their Q&A questions while collecting
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51d1be8695
commit
3a8962976e
6 files changed
+272
-15
No files matched your search
+70
-14
@@ -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<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
|
||||
* MAX_WORD_LENGTH. Use wordKey() for case-insensitive comparison.
|
||||
|
||||
Reference in new issue
Block a user