From affa31a05db7e6dabeb7c349f9f2cebdc0889e82 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Sun, 20 Aug 2023 10:26:51 +0200 Subject: [PATCH] Add stat for agegroups --- classes/Jsonfile.php | 4 ++-- jobs/ProcessSubmitJob.php | 14 +++++++++++--- models/Event.php | 4 ++-- models/participant/columns.yaml | 3 +++ 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/classes/Jsonfile.php b/classes/Jsonfile.php index ec77374..633c928 100644 --- a/classes/Jsonfile.php +++ b/classes/Jsonfile.php @@ -32,8 +32,8 @@ class Jsonfile extends CmsCompoundObject return json_decode($this->content, true); } - public function getVar(string $var) + public function getVar(string $var, $default = null) { - return array_get($this->markup(), $var); + return array_get($this->markup(), $var, $default); } } diff --git a/jobs/ProcessSubmitJob.php b/jobs/ProcessSubmitJob.php index b22f3dc..3edb6e1 100644 --- a/jobs/ProcessSubmitJob.php +++ b/jobs/ProcessSubmitJob.php @@ -102,9 +102,17 @@ class ProcessSubmitJob implements ShouldQueue private function addStat(Spreadsheet $s): Spreadsheet { - $groupBy = $this->event->loadConfig('groupBy'); - $stat = Participant::where('event_id', $this->event->id)->get()->groupBy(fn ($p) => data_get($p->payload, $groupBy))->map(fn ($p) => $p->count())->map(fn ($key, $value) => [$value, $key])->values(); - $s->sheet('Statistik', $stat->toArray()); + $payload = collect([]); + foreach ($this->event->loadConfig('stat') as $stat) { + $stat = Participant::where('event_id', $this->event->id) + ->get() + ->groupBy(fn ($p) => data_get($p->payload, $stat)) + ->mapWithKeys(fn ($participants, $groupKey) => [$groupKey => [$groupKey, $participants->count()]]) + ->values(); + $payload = $payload->merge($stat); + } + + $s->sheet('Statistik', $payload->toArray()); return $s; } diff --git a/models/Event.php b/models/Event.php index e0a3600..5e47e27 100644 --- a/models/Event.php +++ b/models/Event.php @@ -84,12 +84,12 @@ class Event extends Model return static::pluck('name', 'id')->toArray(); } - public function loadConfig($v) + public function loadConfig($v, $default = null) { $file = $this->slug.'.json'; throw_unless(Jsonfile::find($file), Exception::class, 'Datei '.$file.' nicht gefunden.'); - return Jsonfile::find($file)->getVar($v); + return Jsonfile::find($file)->getVar($v, $default); } public function transformInputs($input) diff --git a/models/participant/columns.yaml b/models/participant/columns.yaml index 4e1a24f..30e9896 100644 --- a/models/participant/columns.yaml +++ b/models/participant/columns.yaml @@ -3,6 +3,9 @@ # =================================== columns: + id: + label: ID + firstname: label: Vorname