Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m33s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m25s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 6m7s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m54s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 9m9s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 10m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 10m12s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 1m26s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 9m48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m52s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
55 lines
2.4 KiB
PHP
55 lines
2.4 KiB
PHP
<?php
|
||
|
||
namespace Inhaus\Plugins\Umfragen;
|
||
|
||
use Inhaus\Plugins\Umfragen\Models\Poll;
|
||
|
||
/**
|
||
* Auswertung einer Umfrage je Frage.
|
||
*/
|
||
class Results
|
||
{
|
||
/**
|
||
* @return array{total: int, questions: list<array<string, mixed>>}
|
||
*/
|
||
public function for(Poll $poll, bool $withTexts): array
|
||
{
|
||
$answers = $poll->responses()->pluck('answers')->map(fn ($a) => is_array($a) ? $a : (json_decode((string) $a, true) ?: []));
|
||
$total = $answers->count();
|
||
|
||
return [
|
||
'total' => $total,
|
||
'questions' => collect($poll->questions)->map(function (array $question) use ($answers, $withTexts) {
|
||
$given = $answers->map(fn ($a) => $a[$question['id']] ?? null)->filter(fn ($v) => $v !== null && $v !== []);
|
||
$count = $given->count();
|
||
$result = ['id' => $question['id'], 'type' => $question['type'], 'label' => $question['label'], 'answered' => $count];
|
||
|
||
if (in_array($question['type'], ['single', 'multiple'], true)) {
|
||
$tally = $given->flatMap(fn ($v) => (array) $v)->countBy();
|
||
$result['options'] = collect($question['options'])->map(fn ($o) => [
|
||
'id' => $o['id'],
|
||
'label' => $o['label'],
|
||
'votes' => (int) ($tally[$o['id']] ?? 0),
|
||
'percent' => $count ? (int) round(($tally[$o['id']] ?? 0) / $count * 100) : 0,
|
||
])->all();
|
||
}
|
||
|
||
if ($question['type'] === 'scale') {
|
||
$tally = $given->countBy();
|
||
$result['scale'] = $question['scale'];
|
||
$result['min_label'] = $question['min_label'] ?? null;
|
||
$result['max_label'] = $question['max_label'] ?? null;
|
||
$result['average'] = $count ? round($given->avg(), 1) : null;
|
||
$result['distribution'] = collect(range(1, $question['scale']))->map(fn ($n) => ['value' => $n, 'votes' => (int) ($tally[$n] ?? 0)])->all();
|
||
}
|
||
|
||
if ($question['type'] === 'text') {
|
||
// Freitexte nur für die Redaktion – sie können Rückschlüsse auf Personen erlauben
|
||
$result['texts'] = $withTexts ? $given->values()->shuffle()->take(500)->values()->all() : null;
|
||
}
|
||
|
||
return $result;
|
||
})->all(),
|
||
];
|
||
}
|
||
}
|