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
@@ -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'],
|
||||
|
||||
Reference in new issue
Block a user