Replace CSV export with PDF export
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>
This commit is contained in:
1 parent
5cd1be1cf8
commit
d1facd32a6
21 files changed
+1102
-110
No files matched your search
@@ -9,11 +9,13 @@ use App\Model\Table\ResponsesTable;
|
||||
use App\Model\Table\SlidesTable;
|
||||
use App\Service\PollStateService;
|
||||
use App\Service\QaService;
|
||||
use App\Service\ReportService;
|
||||
use App\Service\SlideControlService;
|
||||
use App\Service\VoteService;
|
||||
use App\View\PdfView;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Http\Exception\BadRequestException;
|
||||
use Cake\Http\Response;
|
||||
use Cake\I18n\DateTime;
|
||||
use Cake\Log\Log;
|
||||
use Cake\Routing\Router;
|
||||
use chillerlan\QRCode\Output\QRMarkupSVG;
|
||||
@@ -275,97 +277,27 @@ class AdminController extends AdminAppController
|
||||
}
|
||||
|
||||
/**
|
||||
* CSV export of all responses (semicolon separated, UTF-8 with BOM for Excel).
|
||||
* PDF report with the results of all interactive slides.
|
||||
* Query `internal=1` includes anonymous/internal feedback (complete version for the owner),
|
||||
* otherwise private slides only show their number of participants.
|
||||
*
|
||||
* @return \Cake\Http\Response
|
||||
*/
|
||||
public function export(): Response
|
||||
{
|
||||
$slides = $this->Slides->listForPoll($this->poll->id);
|
||||
$handle = fopen('php://temp', 'r+');
|
||||
assert($handle !== false);
|
||||
fwrite($handle, "\xEF\xBB\xBF");
|
||||
fputcsv($handle, ['Folie', 'Typ', 'Frage', 'Teilnehmer (anonym)', 'Antwort', 'Zeitpunkt'], ';', '"', '');
|
||||
$report = new ReportService();
|
||||
$hasInternal = $report->hasInternalContent($this->poll);
|
||||
$includeInternal = $hasInternal && $this->request->getQuery('internal') === '1';
|
||||
|
||||
foreach ($slides as $position => $slide) {
|
||||
if (!$slide->isInteractive()) {
|
||||
continue;
|
||||
}
|
||||
$labels = [];
|
||||
foreach ($slide->slide_options ?? [] as $option) {
|
||||
$labels[$option->id] = $slide->isQuiz()
|
||||
? __('{0} ({1})', $option->label, $option->is_correct ? __('richtig') : __('falsch'))
|
||||
: $option->label;
|
||||
}
|
||||
$responses = $this->Responses->find()
|
||||
->where(['slide_id' => $slide->id])
|
||||
->orderBy(['id' => 'ASC'])
|
||||
->all();
|
||||
$texts = [];
|
||||
foreach ($responses as $response) {
|
||||
$texts[$response->id] = (string)$response->text_value;
|
||||
$answer = match (true) {
|
||||
$slide->isOrder() => $this->orderAnswer($slide, $labels, (string)$response->text_value),
|
||||
$response->parent_id !== null => __('Upvote: {0}', $texts[$response->parent_id] ?? ''),
|
||||
$response->slide_option_id !== null => $labels[$response->slide_option_id] ?? '',
|
||||
$response->text_value !== null && $response->numeric_value === VoteService::DOWNVOTE
|
||||
=> __('Downvote: {0}', $response->text_value),
|
||||
$slide->type === Slide::TYPE_SCALE && $response->text_value !== null
|
||||
=> __('{0} (Kommentar: {1})', $response->numeric_value, $response->text_value),
|
||||
$response->numeric_value !== null => (string)$response->numeric_value,
|
||||
default => (string)$response->text_value,
|
||||
};
|
||||
fputcsv($handle, [
|
||||
$position + 1,
|
||||
$slide->typeLabel(),
|
||||
$slide->question,
|
||||
substr($response->participant_hash, 0, 8),
|
||||
$this->csvSafe($answer),
|
||||
$response->created->i18nFormat('yyyy-MM-dd HH:mm:ss'),
|
||||
], ';', '"', '');
|
||||
}
|
||||
}
|
||||
rewind($handle);
|
||||
$csv = (string)stream_get_contents($handle);
|
||||
fclose($handle);
|
||||
$this->viewBuilder()->setClassName(PdfView::class);
|
||||
$this->set([
|
||||
'sections' => $report->build($this->poll, $includeInternal),
|
||||
'includeInternal' => $includeInternal,
|
||||
'hasInternal' => $hasInternal,
|
||||
'exportedAt' => DateTime::now(),
|
||||
]);
|
||||
$filename = 'plenario-' . $this->poll->join_code . '-' . date('Ymd') . ($includeInternal ? '-intern' : '') . '.pdf';
|
||||
|
||||
$filename = 'plenario-' . $this->poll->join_code . '-' . date('Ymd') . '.csv';
|
||||
|
||||
return $this->protect($this->response)
|
||||
->withType('csv')
|
||||
->withCharset('UTF-8')
|
||||
->withDownload($filename)
|
||||
->withStringBody($csv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ordering answer for the CSV export, e.g. "A → C → B (falsch)".
|
||||
*
|
||||
* @param \App\Model\Entity\Slide $slide Ordering slide incl. slide_options
|
||||
* @param array<int, string> $labels Option labels by id
|
||||
* @param string $text Stored answer (option ids)
|
||||
* @return string
|
||||
*/
|
||||
protected function orderAnswer(Slide $slide, array $labels, string $text): string
|
||||
{
|
||||
$given = VoteService::parseOrder($text);
|
||||
$correct = array_map(fn($option) => $option->id, array_values($slide->slide_options ?? []));
|
||||
|
||||
return __(
|
||||
'{0} ({1})',
|
||||
implode(' → ', array_map(fn(int $id) => $labels[$id] ?? '?', $given)),
|
||||
$given === $correct ? __('richtig') : __('falsch'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevents CSV/formula injection in spreadsheet programs.
|
||||
*
|
||||
* @param string $value Cell value
|
||||
* @return string
|
||||
*/
|
||||
protected function csvSafe(string $value): string
|
||||
{
|
||||
return preg_match('/^[=+\-@\t\r]/', $value) ? "'" . $value : $value;
|
||||
return $this->render()->withDownload($filename);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user