Let participants edit their open answer while voting is open
Generalises the comment edit to /p/{code}/edit: open answers can be
corrected (not emptied), scale comments as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e6f52a0449
commit
51d1be8695
7 files changed
+135
-48
No files matched your search
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Model\Entity\Poll;
|
||||
use App\Model\Entity\Slide;
|
||||
use App\Model\Table\PollsTable;
|
||||
use App\Service\PollStateService;
|
||||
use App\Service\VoteException;
|
||||
@@ -86,11 +87,11 @@ class ParticipateController extends AppController
|
||||
$slide = $poll->is_active ? $stateService->currentSlide($poll) : null;
|
||||
$participantHash = $this->participantHash($poll);
|
||||
$state = $stateService->forParticipant($poll, $participantHash, $slide);
|
||||
$ownComment = $slide?->hasComments() && $state['hasResponded']
|
||||
? (new VoteService())->ownComment($slide, $participantHash)
|
||||
$ownText = $slide !== null && VoteService::hasEditableText($slide) && $state['hasResponded']
|
||||
? (new VoteService())->ownText($slide, $participantHash)
|
||||
: null;
|
||||
|
||||
$this->set(compact('poll', 'slide', 'state', 'participantHash', 'ownComment'));
|
||||
$this->set(compact('poll', 'slide', 'state', 'participantHash', 'ownText'));
|
||||
$this->setPollInterval();
|
||||
}
|
||||
|
||||
@@ -157,12 +158,13 @@ class ParticipateController extends AppController
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the own comment on a scale slide while voting is open (e.g. to fix a typo).
|
||||
* Replaces the own answer text (open question) or comment (scale) while voting is open,
|
||||
* e.g. to fix a typo. See VoteService::editText().
|
||||
*
|
||||
* @param string $code Join code
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function comment(string $code): ?Response
|
||||
public function edit(string $code): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$poll = $this->loadPoll($code);
|
||||
@@ -176,11 +178,13 @@ class ParticipateController extends AppController
|
||||
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
||||
}
|
||||
$data = (array)$this->request->getData();
|
||||
(new VoteService())->editComment($poll, $slide, $this->participantHash($poll), $data);
|
||||
(new VoteService())->editText($poll, $slide, $this->participantHash($poll), $data);
|
||||
$comment = $data['comment'] ?? '';
|
||||
$this->Flash->success(!is_scalar($comment) || trim((string)$comment) === ''
|
||||
? __('Dein Kommentar wurde entfernt.')
|
||||
: __('Dein Kommentar wurde gespeichert.'));
|
||||
$this->Flash->success(match (true) {
|
||||
$slide->type === Slide::TYPE_OPEN => __('Deine Antwort wurde geändert.'),
|
||||
!is_scalar($comment) || trim((string)$comment) === '' => __('Dein Kommentar wurde entfernt.'),
|
||||
default => __('Dein Kommentar wurde gespeichert.'),
|
||||
});
|
||||
} catch (VoteException $e) {
|
||||
$this->Flash->error($e->getMessage());
|
||||
}
|
||||
|
||||
+29
-15
@@ -36,8 +36,8 @@ use Cake\Utility\Security;
|
||||
* `remove` withdraws one own upvote of the question.
|
||||
* - 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()).
|
||||
* - scale with comment field: the own comment can be changed while voting is open
|
||||
* (editComment()), the rating cannot.
|
||||
* - open question and scale with comment field: the own answer text or comment can be changed
|
||||
* while voting is open (editText()), a scale rating cannot.
|
||||
*/
|
||||
class VoteService
|
||||
{
|
||||
@@ -127,45 +127,59 @@ class VoteService
|
||||
}
|
||||
|
||||
/**
|
||||
* Scale with comment field: replaces the participant's own comment (e.g. to fix a typo)
|
||||
* as long as voting on the slide is open. The rating itself stays unchanged; an empty
|
||||
* comment removes it. Same preconditions as submit().
|
||||
* Whether the participant may change the text of their answer afterwards (e.g. to fix a typo):
|
||||
* the answer of an open question and the comment of a scale with comment field.
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return bool
|
||||
*/
|
||||
public static function hasEditableText(Slide $slide): bool
|
||||
{
|
||||
return $slide->type === Slide::TYPE_OPEN || ($slide->type === Slide::TYPE_SCALE && $slide->hasComments());
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the text of the participant's own answer as long as voting on the slide is open
|
||||
* (see hasEditableText()). Open question: `text`, must not be empty. Scale: `comment`, the
|
||||
* rating itself stays unchanged and an empty comment removes it. Same preconditions as submit().
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param string $participantHash Participant hash
|
||||
* @param array<string, mixed> $data Request data (`comment`)
|
||||
* @param array<string, mixed> $data Request data (`text` or `comment`)
|
||||
* @return void
|
||||
* @throws \App\Service\VoteException
|
||||
*/
|
||||
public function editComment(Poll $poll, Slide $slide, string $participantHash, array $data): void
|
||||
public function editText(Poll $poll, Slide $slide, string $participantHash, array $data): void
|
||||
{
|
||||
$this->assertOpen($poll, $slide);
|
||||
if (!$slide->hasComments()) {
|
||||
throw new VoteException(__('Bei dieser Frage gibt es keine Kommentare.'));
|
||||
if (!self::hasEditableText($slide)) {
|
||||
throw new VoteException(__('Diese Antwort kann nicht mehr geändert werden.'));
|
||||
}
|
||||
$comment = self::comment($data);
|
||||
$this->Responses->getConnection()->transactional(function () use ($slide, $participantHash, $comment): void {
|
||||
$text = $slide->type === Slide::TYPE_OPEN ? $this->openRows($data)[0]['text_value'] : self::comment($data);
|
||||
$this->Responses->getConnection()->transactional(function () use ($slide, $participantHash, $text): void {
|
||||
$this->lockSlide($slide);
|
||||
$row = $this->Responses->find()
|
||||
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
|
||||
->first();
|
||||
if ($row === null) {
|
||||
throw new VoteException(__('Bitte gib zuerst deine Bewertung ab.'));
|
||||
throw new VoteException($slide->type === Slide::TYPE_OPEN
|
||||
? __('Du hast hier noch nicht geantwortet.')
|
||||
: __('Bitte gib zuerst deine Bewertung ab.'));
|
||||
}
|
||||
$row->text_value = $comment;
|
||||
$row->text_value = $text;
|
||||
$this->Responses->saveOrFail($row);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The participant's own comment on a scale slide (null: none or not answered yet).
|
||||
* Text of the participant's own answer: open answer or scale comment (null: none or not answered yet).
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param string $participantHash Participant hash
|
||||
* @return string|null
|
||||
*/
|
||||
public function ownComment(Slide $slide, string $participantHash): ?string
|
||||
public function ownText(Slide $slide, string $participantHash): ?string
|
||||
{
|
||||
$row = $this->Responses->find()
|
||||
->select(['text_value'])
|
||||
|
||||
Reference in new issue
Block a user