Let participants edit their scale comment while voting is open
Participants can fix typos in, add or remove their own comment until the voting closes. The rating itself stays unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd488e0b81
commit
d518b7a37e
6 files changed
+244
-20
No files matched your search
@@ -86,8 +86,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)
|
||||
: null;
|
||||
|
||||
$this->set(compact('poll', 'slide', 'state', 'participantHash'));
|
||||
$this->set(compact('poll', 'slide', 'state', 'participantHash', 'ownComment'));
|
||||
$this->setPollInterval();
|
||||
}
|
||||
|
||||
@@ -153,6 +156,38 @@ class ParticipateController extends AppController
|
||||
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the own comment on a scale slide while voting is open (e.g. to fix a typo).
|
||||
*
|
||||
* @param string $code Join code
|
||||
* @return \Cake\Http\Response|null
|
||||
*/
|
||||
public function comment(string $code): ?Response
|
||||
{
|
||||
$this->request->allowMethod(['post']);
|
||||
$poll = $this->loadPoll($code);
|
||||
$slide = (new PollStateService())->currentSlide($poll);
|
||||
|
||||
try {
|
||||
if (!$this->throttle('vote', 'Plenario.throttle.votesPerMinute', 60)) {
|
||||
throw new VoteException(__('Zu viele Antworten in kurzer Zeit. Bitte warte einen Moment und versuche es dann noch einmal.'));
|
||||
}
|
||||
if ($slide === null || $slide->id !== (int)$this->request->getData('slide_id')) {
|
||||
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
||||
}
|
||||
$data = (array)$this->request->getData();
|
||||
(new VoteService())->editComment($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.'));
|
||||
} catch (VoteException $e) {
|
||||
$this->Flash->error($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->redirect(['_name' => 'participate', 'code' => $poll->join_code]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $code Join code
|
||||
* @return \App\Model\Entity\Poll
|
||||
|
||||
+96
-19
@@ -36,6 +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.
|
||||
*/
|
||||
class VoteService
|
||||
{
|
||||
@@ -91,23 +93,7 @@ class VoteService
|
||||
*/
|
||||
public function submit(Poll $poll, Slide $slide, string $participantHash, array $data): void
|
||||
{
|
||||
if (!$poll->is_active) {
|
||||
throw new VoteException(__('Diese Umfrage ist derzeit geschlossen.'));
|
||||
}
|
||||
if ($slide->poll_id !== $poll->id || $poll->current_slide_id !== $slide->id) {
|
||||
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
||||
}
|
||||
if (!$slide->isInteractive()) {
|
||||
throw new VoteException(__('Auf dieser Folie kann nicht abgestimmt werden.'));
|
||||
}
|
||||
if (!$slide->isVotingOpen()) {
|
||||
throw new VoteException(match (true) {
|
||||
$slide->isQuiz() && $slide->voting_started_at === null => __('Das Quiz hat noch nicht begonnen.'),
|
||||
$slide->isQuiz() => __('Die Zeit für dieses Quiz ist abgelaufen.'),
|
||||
$slide->isQa() => __('Die Fragerunde läuft – Fragen und Stimmen sind nicht mehr möglich.'),
|
||||
default => __('Die Abstimmung zu dieser Frage ist geschlossen.'),
|
||||
});
|
||||
}
|
||||
$this->assertOpen($poll, $slide);
|
||||
// Checks (answered already, upvote budget, duplicates) and the insert run as one unit,
|
||||
// so a double click or a retried request cannot store an answer twice.
|
||||
$this->Responses->getConnection()->transactional(function () use ($slide, $participantHash, $data): void {
|
||||
@@ -140,6 +126,85 @@ 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().
|
||||
*
|
||||
* @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`)
|
||||
* @return void
|
||||
* @throws \App\Service\VoteException
|
||||
*/
|
||||
public function editComment(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.'));
|
||||
}
|
||||
$comment = self::comment($data);
|
||||
$this->Responses->getConnection()->transactional(function () use ($slide, $participantHash, $comment): 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.'));
|
||||
}
|
||||
$row->text_value = $comment;
|
||||
$this->Responses->saveOrFail($row);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The participant's own comment on a scale slide (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
|
||||
{
|
||||
$row = $this->Responses->find()
|
||||
->select(['text_value'])
|
||||
->where(['slide_id' => $slide->id, 'participant_hash' => $participantHash])
|
||||
->disableHydration()
|
||||
->first();
|
||||
|
||||
return $row['text_value'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll active, slide live and interactive, voting open.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return void
|
||||
* @throws \App\Service\VoteException
|
||||
*/
|
||||
protected function assertOpen(Poll $poll, Slide $slide): void
|
||||
{
|
||||
if (!$poll->is_active) {
|
||||
throw new VoteException(__('Diese Umfrage ist derzeit geschlossen.'));
|
||||
}
|
||||
if ($slide->poll_id !== $poll->id || $poll->current_slide_id !== $slide->id) {
|
||||
throw new VoteException(__('Diese Frage ist nicht mehr aktiv.'));
|
||||
}
|
||||
if (!$slide->isInteractive()) {
|
||||
throw new VoteException(__('Auf dieser Folie kann nicht abgestimmt werden.'));
|
||||
}
|
||||
if (!$slide->isVotingOpen()) {
|
||||
throw new VoteException(match (true) {
|
||||
$slide->isQuiz() && $slide->voting_started_at === null => __('Das Quiz hat noch nicht begonnen.'),
|
||||
$slide->isQuiz() => __('Die Zeit für dieses Quiz ist abgelaufen.'),
|
||||
$slide->isQa() => __('Die Fragerunde läuft – Fragen und Stimmen sind nicht mehr möglich.'),
|
||||
default => __('Die Abstimmung zu dieser Frage ist geschlossen.'),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialises answers to one slide for the rest of the transaction.
|
||||
*
|
||||
@@ -497,11 +562,23 @@ class VoteService
|
||||
throw new VoteException(__('Bitte wähle einen Wert zwischen {0} und {1}.', $min, $max));
|
||||
}
|
||||
// The optional comment is stored in the same row; ignored if the slide has no comment field.
|
||||
$comment = $slide->hasComments() ? trim(self::stringField($data, 'comment')) : '';
|
||||
return [['numeric_value' => $value, 'text_value' => $slide->hasComments() ? self::comment($data) : null]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional scale comment from the request: trimmed, empty = null.
|
||||
*
|
||||
* @param array<string, mixed> $data Request data
|
||||
* @return string|null
|
||||
* @throws \App\Service\VoteException
|
||||
*/
|
||||
protected static function comment(array $data): ?string
|
||||
{
|
||||
$comment = trim(self::stringField($data, 'comment'));
|
||||
if (mb_strlen($comment) > self::MAX_TEXT_LENGTH) {
|
||||
throw new VoteException(__('Der Kommentar darf maximal {0} Zeichen lang sein.', self::MAX_TEXT_LENGTH));
|
||||
}
|
||||
|
||||
return [['numeric_value' => $value, 'text_value' => $comment === '' ? null : $comment]];
|
||||
return $comment === '' ? null : $comment;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user