The results export is now a PDF (Dompdf) with all interactive slides. By default private slides (anonymous/internal feedback) only show their number of participants; ?internal=1 creates the complete version incl. their answers, marked as confidential on every page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
4.0 KiB
PHP
117 lines
4.0 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Service;
|
||
|
||
use App\Model\Entity\Poll;
|
||
use App\Model\Entity\Slide;
|
||
use App\Model\Table\SlidesTable;
|
||
use Cake\ORM\Locator\LocatorAwareTrait;
|
||
|
||
/**
|
||
* Collects the results of all interactive slides of a poll for the PDF export.
|
||
*
|
||
* Without $includeInternal the report shows exactly what may be shown publicly: private slides
|
||
* (anonymous feedback, internal scales) only with their number of participants, like the presenter
|
||
* screen. With $includeInternal it also contains their answers, distribution and comments –
|
||
* that version is meant for the poll owner only.
|
||
*
|
||
* Per slide: ['slide' => 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<array{slide: \App\Model\Entity\Slide, number: int, internal: bool, withheld: bool, results: array<string, mixed>}>
|
||
*/
|
||
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<string, mixed>
|
||
*/
|
||
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;
|
||
}
|
||
}
|