Slide, 'number' => int (position in the poll, 1-based), * 'internal' => bool (private slide), 'withheld' => bool (private and left out), * 'results' => ResultsService::forSlide() shape, where * open: 'answers' contains all texts (not only the newest ones), and * scale: 'comments' contains all comments, 'value' only in the complete version or with comment_ratings]. */ class ReportService { use LocatorAwareTrait; protected SlidesTable $Slides; protected ResultsService $results; /** * Constructor */ public function __construct() { /** @var \App\Model\Table\SlidesTable $slides */ $slides = $this->fetchTable('Slides'); $this->Slides = $slides; $this->results = new ResultsService(); } /** * @param \App\Model\Entity\Poll $poll Poll * @param bool $includeInternal Include answers of private slides * @return list}> */ public function build(Poll $poll, bool $includeInternal): array { $sections = []; foreach ($this->Slides->listForPoll($poll->id) as $index => $slide) { if (!$slide->isInteractive()) { continue; } $internal = $slide->isPrivate(); $sections[] = [ 'slide' => $slide, 'number' => $index + 1, 'internal' => $internal, 'withheld' => $internal && !$includeInternal, 'results' => $this->resultsFor($slide, $includeInternal), ]; } return $sections; } /** * Whether the poll has slides whose answers are not public. * * @param \App\Model\Entity\Poll $poll Poll * @return bool */ public function hasInternalContent(Poll $poll): bool { foreach ($this->Slides->listForPoll($poll->id) as $slide) { if ($slide->isInteractive() && $slide->isPrivate()) { return true; } } return false; } /** * @param \App\Model\Entity\Slide $slide Slide incl. slide_options * @param bool $includeInternal Include answers of private slides * @return array */ protected function resultsFor(Slide $slide, bool $includeInternal): array { // Public shape first: it never contains private data. $results = $this->results->forSlide($slide); $mayRead = !$slide->isPrivate() || $includeInternal; if ($slide->type === Slide::TYPE_OPEN && $mayRead) { $results['answers'] = $this->results->answers($slide); } if ($slide->type === Slide::TYPE_SCALE && $mayRead) { // 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; } }