Share repeated controller and service code
- AppController: joinUrl(), adminUrlFor(), setPollInterval() and protect() instead of copies in Admin-, Home-, Participate- and EmbedController. - PollMailer::sendSafely() replaces two try/catch blocks around mail sending. - Slide::optionIds() replaces five identical array_map() calls. - PollStateService: one pollPayload() for presenter and embed; the vote action passes the already loaded slide instead of loading it again. - ResultsService::forSlide() takes the answer/comment limit and the rating flag, so ReportService no longer queries answers, comments and the scale distribution twice and the export loads the slides only once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ad537b9b34
commit
3e44ded107
14 files changed
+189
-137
No files matched your search
@@ -94,13 +94,7 @@ class PollStateService
|
||||
}
|
||||
|
||||
return [
|
||||
'poll' => [
|
||||
'title' => $poll->title,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $poll->isPaused(),
|
||||
],
|
||||
'poll' => $this->pollPayload($poll),
|
||||
'slides' => array_map(fn(Slide $s) => [
|
||||
'id' => $s->id,
|
||||
'type' => $s->type,
|
||||
@@ -127,13 +121,7 @@ class PollStateService
|
||||
$slide = $this->Slides->closeIfExpired($slide);
|
||||
|
||||
return [
|
||||
'poll' => [
|
||||
'title' => $poll->title,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $poll->isPaused(),
|
||||
],
|
||||
'poll' => $this->pollPayload($poll),
|
||||
'isLive' => $poll->current_slide_id === $slide->id,
|
||||
'index' => null,
|
||||
'count' => null,
|
||||
@@ -145,11 +133,17 @@ class PollStateService
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param string $participantHash Participant hash
|
||||
* @param \App\Model\Entity\Slide|false|null $current Current slide if already loaded via currentSlide()
|
||||
* in this request (null = none is live), false = load it
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forParticipant(Poll $poll, string $participantHash): array
|
||||
public function forParticipant(Poll $poll, string $participantHash, Slide|false|null $current = false): array
|
||||
{
|
||||
$slide = $poll->is_active ? $this->currentSlide($poll) : null;
|
||||
$slide = match (true) {
|
||||
!$poll->is_active => null,
|
||||
$current === false => $this->currentSlide($poll),
|
||||
default => $current,
|
||||
};
|
||||
$words = $slide?->type === Slide::TYPE_WORDCLOUD ? $this->wordState($slide, $participantHash) : null;
|
||||
$quiz = $slide?->isQuiz() ? $this->quizState($slide, $participantHash) : null;
|
||||
$qa = $slide?->isQa() ? $this->qaState($slide, $participantHash) : null;
|
||||
@@ -233,7 +227,7 @@ class PollStateService
|
||||
->first();
|
||||
$answer = $response === null ? null : VoteService::parseOrder($response->text_value);
|
||||
}
|
||||
$correct = array_map(fn($option) => $option->id, array_values($slide->slide_options ?? []));
|
||||
$correct = $slide->optionIds();
|
||||
|
||||
return [
|
||||
'revealed' => $revealed,
|
||||
@@ -343,6 +337,23 @@ class PollStateService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll part of the presenter and embed state.
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @return array{title: string, joinCode: string, joinCodeFormatted: string, isActive: bool, isPaused: bool}
|
||||
*/
|
||||
protected function pollPayload(Poll $poll): array
|
||||
{
|
||||
return [
|
||||
'title' => $poll->title,
|
||||
'joinCode' => $poll->join_code,
|
||||
'joinCodeFormatted' => $poll->formattedJoinCode(),
|
||||
'isActive' => $poll->is_active,
|
||||
'isPaused' => $poll->isPaused(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @return array<string, mixed>
|
||||
|
||||
@@ -43,13 +43,17 @@ class ReportService
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param bool $includeInternal Include answers of private slides
|
||||
* @param bool $includeInternal Include answers of private slides (ignored without private slides,
|
||||
* so the public and the complete version are identical then)
|
||||
* @return list<array{slide: \App\Model\Entity\Slide, number: int, internal: bool, withheld: bool, results: array<string, mixed>}>
|
||||
*/
|
||||
public function build(Poll $poll, bool $includeInternal): array
|
||||
{
|
||||
$slides = $this->Slides->listForPoll($poll->id);
|
||||
$includeInternal = $includeInternal
|
||||
&& array_filter($slides, fn(Slide $slide) => $slide->isInteractive() && $slide->isPrivate()) !== [];
|
||||
$sections = [];
|
||||
foreach ($this->Slides->listForPoll($poll->id) as $index => $slide) {
|
||||
foreach ($slides as $index => $slide) {
|
||||
if (!$slide->isInteractive()) {
|
||||
continue;
|
||||
}
|
||||
@@ -67,20 +71,15 @@ class ReportService
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the poll has slides whose answers are not public.
|
||||
* Whether a report built by build() contains slides whose answers are not public
|
||||
* (i.e. whether a complete, internal version exists at all).
|
||||
*
|
||||
* @param \App\Model\Entity\Poll $poll Poll
|
||||
* @param list<array{internal: bool}> $sections Result of build()
|
||||
* @return bool
|
||||
*/
|
||||
public function hasInternalContent(Poll $poll): bool
|
||||
public static function hasInternal(array $sections): bool
|
||||
{
|
||||
foreach ($this->Slides->listForPoll($poll->id) as $slide) {
|
||||
if ($slide->isInteractive() && $slide->isPrivate()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return in_array(true, array_column($sections, 'internal'), true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,29 +89,12 @@ class ReportService
|
||||
*/
|
||||
protected function resultsFor(Slide $slide, bool $includeInternal): array
|
||||
{
|
||||
$results = $this->results->forSlide($slide);
|
||||
$mayRead = !$slide->isPrivate() || $includeInternal;
|
||||
if (!$mayRead) {
|
||||
return ResultsService::withoutContents($results);
|
||||
if ($slide->isPrivate() && !$includeInternal) {
|
||||
return ResultsService::withoutContents($this->results->forSlide($slide));
|
||||
}
|
||||
|
||||
if ($slide->type === Slide::TYPE_OPEN) {
|
||||
$results['answers'] = $this->results->answers($slide);
|
||||
}
|
||||
if ($slide->type === Slide::TYPE_SCALE) {
|
||||
// Ratings next to comments: complete version always, public one only with the setting.
|
||||
$showRatings = $includeInternal || (bool)$slide->setting('comment_ratings');
|
||||
$comments = [];
|
||||
foreach ($slide->hasComments() ? $this->results->comments($slide) : [] as $comment) {
|
||||
$comments[] = [
|
||||
'id' => $comment['id'],
|
||||
'text' => $comment['text'],
|
||||
'value' => $showRatings ? $comment['value'] : null,
|
||||
];
|
||||
}
|
||||
$results = $this->results->scaleResults($slide) + ['comments' => $comments] + $results;
|
||||
}
|
||||
|
||||
return $results;
|
||||
// All answers and comments. Ratings next to comments: complete version always,
|
||||
// public one only with the setting comment_ratings.
|
||||
return $this->results->forSlide($slide, null, $includeInternal);
|
||||
}
|
||||
}
|
||||
@@ -61,9 +61,12 @@ class ResultsService
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide incl. slide_options
|
||||
* @param int|null $limit Max. answers of open questions / comments of scales (newest), null = all
|
||||
* @param bool $withRatings Ratings next to scale comments regardless of the setting comment_ratings
|
||||
* (complete PDF export only)
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function forSlide(Slide $slide): array
|
||||
public function forSlide(Slide $slide, ?int $limit = self::MAX_ANSWERS, bool $withRatings = false): array
|
||||
{
|
||||
$result = [
|
||||
'type' => $slide->type,
|
||||
@@ -73,8 +76,8 @@ class ResultsService
|
||||
return $result + match ($slide->type) {
|
||||
Slide::TYPE_CHOICE => $this->choice($slide),
|
||||
Slide::TYPE_WORDCLOUD => $this->wordcloud($slide),
|
||||
Slide::TYPE_OPEN => $this->open($slide),
|
||||
Slide::TYPE_SCALE => $this->scale($slide),
|
||||
Slide::TYPE_OPEN => $this->open($slide, $limit),
|
||||
Slide::TYPE_SCALE => $this->scale($slide, $limit, $withRatings),
|
||||
Slide::TYPE_QUIZ => $this->quiz($slide),
|
||||
Slide::TYPE_QA => $this->qa($slide),
|
||||
Slide::TYPE_ORDER => $this->order($slide),
|
||||
@@ -98,7 +101,7 @@ class ResultsService
|
||||
];
|
||||
}
|
||||
|
||||
$correct = array_map(fn($option) => $option->id, array_values($slide->slide_options ?? []));
|
||||
$correct = $slide->optionIds();
|
||||
$placed = array_fill_keys($correct, 0);
|
||||
$correctCount = 0;
|
||||
$answers = $this->Responses->find()
|
||||
@@ -272,13 +275,14 @@ class ResultsService
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param int|null $limit Max. answers, null = all
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function open(Slide $slide): array
|
||||
protected function open(Slide $slide, ?int $limit): array
|
||||
{
|
||||
return [
|
||||
'private' => $slide->isPrivate(),
|
||||
'answers' => $this->answers($slide, self::MAX_ANSWERS),
|
||||
'answers' => $this->answers($slide, $limit),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -309,13 +313,15 @@ class ResultsService
|
||||
|
||||
/**
|
||||
* @param \App\Model\Entity\Slide $slide Slide
|
||||
* @param int|null $limit Max. comments, null = all
|
||||
* @param bool $withRatings Ratings regardless of the setting comment_ratings
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function scale(Slide $slide): array
|
||||
protected function scale(Slide $slide, ?int $limit, bool $withRatings): array
|
||||
{
|
||||
$showRatings = (bool)$slide->setting('comment_ratings');
|
||||
$showRatings = $withRatings || (bool)$slide->setting('comment_ratings');
|
||||
$comments = [];
|
||||
foreach ($slide->hasComments() ? $this->comments($slide, self::MAX_ANSWERS) : [] as $comment) {
|
||||
foreach ($slide->hasComments() ? $this->comments($slide, $limit) : [] as $comment) {
|
||||
$comments[] = [
|
||||
'id' => $comment['id'],
|
||||
'text' => $comment['text'],
|
||||
|
||||
@@ -183,7 +183,7 @@ class VoteService
|
||||
{
|
||||
$selected = $data['option_ids'] ?? $data['option_id'] ?? [];
|
||||
$selected = array_values(array_unique(array_map('intval', (array)$selected)));
|
||||
$validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []);
|
||||
$validIds = $slide->optionIds();
|
||||
$selected = array_values(array_intersect($selected, $validIds));
|
||||
|
||||
if ($selected === []) {
|
||||
@@ -205,7 +205,7 @@ class VoteService
|
||||
{
|
||||
$selected = $data['option_id'] ?? null;
|
||||
$id = is_scalar($selected) ? filter_var($selected, FILTER_VALIDATE_INT) : false;
|
||||
$validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []);
|
||||
$validIds = $slide->optionIds();
|
||||
if ($id === false || !in_array($id, $validIds, true)) {
|
||||
throw new VoteException(__('Bitte wähle eine Antwort aus.'));
|
||||
}
|
||||
@@ -229,7 +229,7 @@ class VoteService
|
||||
}
|
||||
$given[] = $id;
|
||||
}
|
||||
$validIds = array_map(fn($option) => $option->id, $slide->slide_options ?? []);
|
||||
$validIds = $slide->optionIds();
|
||||
$sortedGiven = $given;
|
||||
sort($sortedGiven);
|
||||
sort($validIds);
|
||||
|
||||
Reference in new issue
Block a user