Loheide-eu-mitarbeiterplatt.../plugins-core/events/src/Registrations.php
Friederich Loheide b293d2e05a
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 5m10s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 5m16s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m42s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m29s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m35s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m36s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m27s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m19s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 44s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m10s
Terminanmeldung mit Plätzen und Warteliste (0.31.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 12:37:28 +00:00

152 lines
5.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Inhaus\Plugins\Events;
use App\Models\User;
use App\Notifications\PortalNotification;
use Illuminate\Support\Facades\DB;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Events\Models\EventRegistration;
use RuntimeException;
/**
* Anmeldungen zu Terminen: feste Plätze, danach Warteliste. Wird ein Platz frei,
* rückt die erste Person der Warteliste nach und bekommt Bescheid.
*/
class Registrations
{
public function isOpen(Event $event): bool
{
return $event->registration_enabled && $event->is_published
&& $event->starts_at->isFuture()
&& ($event->registration_deadline === null || $event->registration_deadline->isFuture());
}
/**
* @throws RuntimeException wenn keine Anmeldung möglich ist
*/
public function register(Event $event, User $user, ?string $note = null): EventRegistration
{
if (! $this->isOpen($event)) {
throw new RuntimeException('Für diesen Termin ist keine Anmeldung (mehr) möglich.');
}
return DB::transaction(function () use ($event, $user, $note) {
// Sperre auf den Termin, damit bei gleichzeitigen Anmeldungen niemand über die Grenze rutscht
$event = Event::query()->lockForUpdate()->findOrFail($event->id);
$existing = EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first();
if ($existing) {
$existing->update(['note' => $note]);
return $existing;
}
$status = $this->hasSpace($event) ? EventRegistration::CONFIRMED : ($event->waitlist ? EventRegistration::WAITLIST : null);
if ($status === null) {
throw new RuntimeException('Der Termin ist leider ausgebucht.');
}
return EventRegistration::query()->create(['event_id' => $event->id, 'user_id' => $user->id, 'status' => $status, 'note' => $note]);
});
}
public function cancel(Event $event, User $user): void
{
$this->remove(EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first());
}
public function remove(?EventRegistration $registration): void
{
if (! $registration) {
return;
}
DB::transaction(function () use ($registration) {
Event::query()->lockForUpdate()->find($registration->event_id);
$registration->delete();
});
$this->fillFromWaitlist($registration->event);
}
/**
* Freie Plätze an die Warteliste vergeben (nach Abmeldung oder mehr Plätzen).
*/
public function fillFromWaitlist(Event $event): void
{
$promoted = DB::transaction(function () use ($event) {
$event = Event::query()->lockForUpdate()->findOrFail($event->id);
$promoted = collect();
while ($this->hasSpace($event)) {
$next = EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::WAITLIST)->oldest()->oldest('id')->first();
if (! $next) {
break;
}
$next->update(['status' => EventRegistration::CONFIRMED]);
$promoted->push($next);
}
return $promoted;
});
if ($event->starts_at->isPast()) {
return;
}
foreach ($promoted as $registration) {
$registration->user?->notify(new PortalNotification(
'Sie sind nachgerückt: '.$event->title,
'Ein Platz ist frei geworden – Ihre Anmeldung ist jetzt bestätigt.',
route('events.show', $event),
'calendar-check',
));
}
}
public function confirmedCount(Event $event): int
{
return EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::CONFIRMED)->count();
}
/**
* Stand für die Anzeige: eigene Anmeldung, Plätze, Warteliste.
*
* @return array<string, mixed>|null
*/
public function summary(Event $event, User $user): ?array
{
if (! $event->registration_enabled) {
return null;
}
$mine = EventRegistration::query()->where('event_id', $event->id)->where('user_id', $user->id)->first();
$confirmed = $this->confirmedCount($event);
$waiting = EventRegistration::query()->where('event_id', $event->id)->where('status', EventRegistration::WAITLIST);
return [
'enabled' => true,
'open' => $this->isOpen($event),
'deadline' => $event->registration_deadline?->toIso8601String(),
'capacity' => $event->capacity,
'confirmed' => $confirmed,
'free' => $event->capacity === null ? null : max(0, $event->capacity - $confirmed),
'waitlist' => $event->waitlist,
'waiting' => (clone $waiting)->count(),
'status' => $mine?->status,
'note' => $mine?->note,
'position' => $mine?->status === EventRegistration::WAITLIST
? (clone $waiting)->where(fn ($q) => $q->where('created_at', '<', $mine->created_at)->orWhere(fn ($q) => $q->where('created_at', $mine->created_at)->where('id', '<', $mine->id)))->count() + 1
: null,
];
}
private function hasSpace(Event $event): bool
{
return $event->capacity === null || $this->confirmedCount($event) < $event->capacity;
}
}