Files
mareike/app/Repositories/EventParticipantRepository.php
2026-04-11 22:17:38 +02:00

151 lines
5.5 KiB
PHP

<?php
namespace App\Repositories;
use App\Enumerations\EatingHabit;
use App\Models\Event;
use App\Models\EventParticipant;
use Illuminate\Http\Request;
class EventParticipantRepository {
public function getForList(Event $event, Request $request, bool $signedOffParticipants = false) : array {
$participants = [];
if (!$signedOffParticipants) {
$allParticipants = $event->participants()->whereNull('unregistered_at');
} else {
$allParticipants = $event->participants()->whereNotNull('unregistered_at');
}
foreach ($allParticipants->orderBy('lastname')->orderBy('firstname')->get() as $participant) {
$participants[] = $participant->toResource()->toArray($request);
};
return $participants;
}
public function getByIdentifier(string $identifier, EventRepository $eventRepository, bool $withPermissionCheck = true) : ?EventParticipant {
$participant = EventParticipant::where('identifier', $identifier)->first();
if ($participant === null) {
return null;
}
if ($withPermissionCheck) {
$event = $eventRepository->getById($participant->event_id);
if ($event === null) {
return null;
}
}
return $participant;
}
public function groupByLocalGroup(Event $event, Request $request) : array {
$allParticipants = $this->getForList($event, $request);
$participants = [];
foreach ($allParticipants as $participant) {
$participants[$participant['localgroup']][] = $participant;
}
return $participants;
}
public function groupByParticipationType(Event $event, Request $request) : array {
$allParticipants = $this->getForList($event, $request);
$participants = [];
foreach ($allParticipants as $participant) {
$participants[$participant['participationType']][] = $participant;
}
return $participants;
}
public function getSignedOffParticipants(Event $event, Request $request) : array {
$allParticipants = $this->getForList($event, $request, true);
$participants = [];
foreach ($allParticipants as $participant) {
$participants[$participant['participationType']][] = $participant;
}
return $participants;
}
public function getParticipantsWithIntolerances(Event $event, Request $request) : array {
$participants = [];
foreach ($event->participants()->whereNotNull('intolerances')->whereNot('intolerances' , '=', '')->get() as $participant) {
$participants[] = $participant->toResource()->toArray($request);
};
return $participants;
}
public function getKitchenOverview(Event $event) : array {
$data = [];
$participants = $event->participants()->get();
for ($cur_date = $event->start_date; $cur_date <= $event->end_date; $cur_date->modify('+1 day')) {
$dateKey = $cur_date->format('d.m.Y');
$data[$dateKey] = [
'1' => [],
'2' => [],
'3' => [],
];
foreach (EatingHabit::all() as $eatingHabit) {
$data[$dateKey]['1'][$eatingHabit->slug] = 0;
$data[$dateKey]['2'][$eatingHabit->slug] = 0;
$data[$dateKey]['3'][$eatingHabit->slug] = 0;
}
foreach ($participants as $participant) {
$eatingHabitSlug = $participant->eating_habit;
if ($eatingHabitSlug === null) {
continue;
}
$anreise = $participant->arrival_date;
$abreise = $participant->departure_date;
if ($anreise->getTimestamp() === $cur_date->getTimestamp()) {
switch ((int)$participant->arrival_eating) {
case 1:
$data[$dateKey]['3'][$eatingHabitSlug]++;
break;
case 2:
$data[$dateKey]['2'][$eatingHabitSlug]++;
$data[$dateKey]['3'][$eatingHabitSlug]++;
break;
case 3:
$data[$dateKey]['1'][$eatingHabitSlug]++;
$data[$dateKey]['2'][$eatingHabitSlug]++;
$data[$dateKey]['3'][$eatingHabitSlug]++;
break;
}
} elseif ($abreise->getTimestamp() === $cur_date->getTimestamp()) {
switch ((int)$participant->departure_eating) {
case 1:
$data[$dateKey]['1'][$eatingHabitSlug]++;
break;
case 2:
$data[$dateKey]['1'][$eatingHabitSlug]++;
$data[$dateKey]['2'][$eatingHabitSlug]++;
break;
case 3:
$data[$dateKey]['1'][$eatingHabitSlug]++;
$data[$dateKey]['2'][$eatingHabitSlug]++;
$data[$dateKey]['3'][$eatingHabitSlug]++;
break;
}
} else {
$data[$dateKey]['1'][$eatingHabitSlug]++;
$data[$dateKey]['2'][$eatingHabitSlug]++;
$data[$dateKey]['3'][$eatingHabitSlug]++;
}
}
}
return $data;
}
}