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
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user