Terminanmeldung mit Plätzen und Warteliste (0.31.0)
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

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Friederich Loheide 2026-09-27 12:37:28 +00:00
parent 33a76458af
commit b293d2e05a
139 changed files with 1030 additions and 336 deletions

View file

@ -98,6 +98,19 @@ class EventResource extends Resource
->automaticallyCropImagesToAspectRatio()
->maxSize(10 * 1024),
]),
Section::make('Anmeldung')
->columnSpan(1)
->schema([
Toggle::make('registration_enabled')->label('Anmeldung erforderlich')->live(),
TextInput::make('capacity')->label('Plätze')->helperText('Leer = unbegrenzt.')->numeric()->minValue(1)->maxValue(100000)
->visible(fn (Get $get) => $get('registration_enabled'))->live(),
Toggle::make('waitlist')->label('Warteliste')->default(true)
->visible(fn (Get $get) => $get('registration_enabled') && filled($get('capacity'))),
DateTimePicker::make('registration_deadline')->label('Anmeldeschluss')->seconds(false)
->visible(fn (Get $get) => $get('registration_enabled')),
Toggle::make('participants_public')->label('Angemeldete sehen, wer noch kommt')
->visible(fn (Get $get) => $get('registration_enabled')),
]),
]);
}

View file

@ -4,8 +4,15 @@ namespace Inhaus\Plugins\Events\Filament\Resources\Events\Pages;
use Filament\Resources\Pages\EditRecord;
use Inhaus\Plugins\Events\Filament\Resources\Events\EventResource;
use Inhaus\Plugins\Events\Registrations;
class EditEvent extends EditRecord
{
protected static string $resource = EventResource::class;
protected function afterSave(): void
{
// Mehr Plätze → Warteliste rückt nach
app(Registrations::class)->fillFromWaitlist($this->record);
}
}

View file

@ -7,6 +7,7 @@ use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Inhaus\Plugins\Events\Http\Resources\EventResource;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Events\Registrations;
/**
* Anstehende Termine für Apps – dieselben Zielgruppen wie im Portal.
@ -15,9 +16,14 @@ class EventApiController extends Controller
{
public function index(Request $request): JsonResponse
{
return response()->json(['data' => EventResource::collection(
Event::query()->upcoming()->visibleTo($request->user())->limit(200)->get()
)->resolve()]);
$registrations = app(Registrations::class);
// Mit eigenem Anmeldestatus (für „Angemeldet“ bzw. „Warteliste“ in der Liste)
return response()->json(['data' => Event::query()->upcoming()->visibleTo($request->user())->limit(200)->get()
->map(fn (Event $event) => [
...(new EventResource($event))->resolve(),
'registration' => $registrations->summary($event, $request->user()),
])->all()]);
}
public function show(Request $request, Event $event): JsonResponse
@ -29,6 +35,7 @@ class EventApiController extends Controller
'description_html' => $event->description_html,
'url' => route('events.show', $event),
'ics_url' => route('events.ics', $event),
'registration' => app(Registrations::class)->summary($event, $request->user()),
]]);
}
}

View file

@ -12,6 +12,8 @@ use Inertia\Response;
use Inhaus\Plugins\Events\Calendar;
use Inhaus\Plugins\Events\Http\Resources\EventResource;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Events\Models\EventRegistration;
use Inhaus\Plugins\Events\Registrations;
class EventController extends Controller
{
@ -27,16 +29,35 @@ class EventController extends Controller
]);
}
public function show(Event $event): Response
public function show(Event $event, Registrations $registrations): Response
{
abort_unless($event->is_published && $event->isVisibleTo(request()->user()), 404);
$user = request()->user();
abort_unless($event->is_published && $event->isVisibleTo($user), 404);
$canEdit = $user->can('update', $event);
$summary = $registrations->summary($event, $user);
return Inertia::render('events::Show', [
'registration' => $summary,
// Liste für die Terminpflege; alle anderen sehen – falls freigegeben – nur die Namen der Angemeldeten
'participants' => $summary && ($canEdit || ($event->participants_public && $summary['status'] === EventRegistration::CONFIRMED))
? $event->registrations()->with('user')->orderByRaw('status = ? DESC', [EventRegistration::CONFIRMED])->oldest()->oldest('id')->limit(1000)->get()
->filter(fn (EventRegistration $r) => $canEdit || $r->status === EventRegistration::CONFIRMED)
->map(fn (EventRegistration $r) => [
'id' => $r->id,
'status' => $r->status,
'name' => $r->user?->name,
'user_id' => $r->user_id,
'avatar_url' => $r->user?->avatarUrl(64),
'initials' => $r->user?->initials,
...$canEdit ? ['department' => $r->user?->department, 'note' => $r->note, 'date' => $r->created_at?->toIso8601String()] : [],
])->values()->all()
: null,
'event' => [
...(new EventResource($event))->resolve(),
'description_html' => $event->description_html,
],
'editUrl' => request()->user()->can('update', $event) ? route('events.edit', $event) : null,
'editUrl' => $canEdit ? route('events.edit', $event) : null,
'canManage' => $canEdit,
]);
}

View file

@ -16,6 +16,7 @@ use Illuminate\Validation\Rules\File;
use Inertia\Inertia;
use Inertia\Response;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Events\Registrations;
/**
* Termine im Portal pflegen („Redaktion → Termine“) – dasselbe wie im Backend.
@ -55,6 +56,7 @@ class EventEditorController extends Controller
$event->update($attributes);
Audiences::sync('event', $event->id, $audience);
app(Registrations::class)->fillFromWaitlist($event);
Audit::log('event.updated', ['event' => $event->id, 'title' => $event->title]);
return ($event->is_published ? to_route('events.show', $event) : to_route('editorial'))->with('status', 'Termin gespeichert.');
@ -95,6 +97,12 @@ class EventEditorController extends Controller
'image_path' => $event->image_path,
'image_url' => $event->imageUrl('large'),
'is_published' => $event->is_published,
'registration_enabled' => $event->registration_enabled,
'capacity' => $event->capacity,
'waitlist' => $event->waitlist,
'registration_deadline' => $event->registration_deadline?->format('Y-m-d\TH:i'),
'participants_public' => $event->participants_public,
'registrations' => $event->registrations()->count(),
'audience' => Audiences::get('event', $event->id),
'url' => route('events.show', $event),
] : null,
@ -117,11 +125,17 @@ class EventEditorController extends Controller
'description' => ['nullable', 'string', 'max:100000'],
'image_path' => ['nullable', 'string', 'max:500', 'regex:#^[A-Za-z0-9/_.-]+$#', 'not_regex:#\.\.#'],
'is_published' => ['boolean'],
'registration_enabled' => ['boolean'],
'capacity' => ['nullable', 'integer', 'min:1', 'max:100000'],
'waitlist' => ['boolean'],
'registration_deadline' => ['nullable', 'date'],
'participants_public' => ['boolean'],
'audience' => ['nullable', 'array'],
'audience.*' => ['array'],
'audience.*.*' => ['string', 'max:150'],
], ['ends_at.after_or_equal' => 'Das Ende darf nicht vor dem Beginn liegen.'], [
'title' => 'Titel', 'starts_at' => 'Beginn', 'ends_at' => 'Ende', 'location' => 'Ort', 'online_url' => 'Online-Link',
'capacity' => 'Plätze', 'registration_deadline' => 'Anmeldeschluss',
]);
$allDay = (bool) ($data['all_day'] ?? false);
@ -138,6 +152,11 @@ class EventEditorController extends Controller
'description' => filled($data['description'] ?? null) ? $data['description'] : null,
'image_path' => $data['image_path'] ?? null,
'is_published' => (bool) ($data['is_published'] ?? true),
'registration_enabled' => (bool) ($data['registration_enabled'] ?? false),
'capacity' => $data['capacity'] ?? null,
'waitlist' => (bool) ($data['waitlist'] ?? true),
'registration_deadline' => filled($data['registration_deadline'] ?? null) ? Carbon::parse($data['registration_deadline']) : null,
'participants_public' => (bool) ($data['participants_public'] ?? false),
], array_intersect_key((array) ($data['audience'] ?? []), array_flip(['role', 'department', 'location', 'exclude_role']))];
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace Inhaus\Plugins\Events\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Support\Csv;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Inhaus\Plugins\Events\Models\Event;
use Inhaus\Plugins\Events\Models\EventRegistration;
use Inhaus\Plugins\Events\Registrations;
use RuntimeException;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* An- und Abmelden zu Terminen – im Portal und über die App-Schnittstelle –
* sowie die Teilnehmendenliste für die Terminpflege.
*/
class RegistrationController extends Controller
{
public function __construct(private readonly Registrations $registrations) {}
public function store(Request $request, Event $event): RedirectResponse|JsonResponse
{
$this->ensureVisible($request, $event);
$data = $request->validate(['note' => ['nullable', 'string', 'max:500']]);
try {
$registration = $this->registrations->register($event, $request->user(), filled($data['note'] ?? null) ? trim($data['note']) : null);
} catch (RuntimeException $e) {
throw ValidationException::withMessages(['registration' => $e->getMessage()]);
}
return $this->respond($request, $event, $registration->status === EventRegistration::CONFIRMED
? 'Sie sind angemeldet.'
: 'Der Termin ist ausgebucht – Sie stehen auf der Warteliste.');
}
public function destroy(Request $request, Event $event): RedirectResponse|JsonResponse
{
$this->ensureVisible($request, $event);
if ($event->starts_at->isPast()) {
throw ValidationException::withMessages(['registration' => 'Der Termin hat bereits begonnen.']);
}
$this->registrations->cancel($event, $request->user());
return $this->respond($request, $event, 'Sie sind abgemeldet.');
}
/** Terminpflege: eine Anmeldung entfernen */
public function remove(Request $request, Event $event, EventRegistration $registration): RedirectResponse
{
Gate::authorize('update', $event);
abort_unless($registration->event_id === $event->id, 404);
$this->registrations->remove($registration);
return back()->with('status', 'Anmeldung entfernt.');
}
public function export(Request $request, Event $event): StreamedResponse
{
Gate::authorize('update', $event);
$rows = EventRegistration::query()->where('event_id', $event->id)->with('user')
->orderByRaw('status = ? DESC', [EventRegistration::CONFIRMED])->oldest()->get();
return response()->streamDownload(function () use ($rows) {
$out = fopen('php://output', 'w');
fwrite($out, "\xEF\xBB\xBF");
fputcsv($out, ['Name', 'E-Mail', 'Abteilung', 'Standort', 'Status', 'Anmerkung', 'Angemeldet am'], ';');
foreach ($rows as $row) {
fputcsv($out, array_map(Csv::cell(...), [
$row->user?->name, $row->user?->email, $row->user?->department, $row->user?->location,
$row->status === EventRegistration::CONFIRMED ? 'Angemeldet' : 'Warteliste',
$row->note, $row->created_at?->timezone(config('app.timezone'))->format('d.m.Y H:i'),
]), ';');
}
fclose($out);
}, 'teilnehmende-termin-'.$event->id.'.csv', ['Content-Type' => 'text/csv; charset=utf-8']);
}
private function ensureVisible(Request $request, Event $event): void
{
abort_unless($event->is_published && $event->isVisibleTo($request->user()), 404);
}
private function respond(Request $request, Event $event, string $message): RedirectResponse|JsonResponse
{
if ($request->expectsJson() && ! $request->header('X-Inertia')) {
return response()->json(['message' => $message, 'data' => $this->registrations->summary($event->fresh(), $request->user())]);
}
return back()->with('status', $message);
}
}

View file

@ -26,6 +26,7 @@ class EventResource extends JsonResource
'all_day' => $this->all_day,
'image_url' => $this->imageUrl('large'),
'thumb_url' => $this->imageUrl('thumb'),
'registration_enabled' => (bool) $this->registration_enabled,
];
}
}

View file

@ -11,11 +11,13 @@ use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Inhaus\Plugins\Events\Database\Factories\EventFactory;
#[Fillable([
'author_id', 'title', 'description', 'location', 'online_url',
'starts_at', 'ends_at', 'all_day', 'image_path', 'is_published',
'registration_enabled', 'capacity', 'waitlist', 'registration_deadline', 'participants_public',
])]
class Event extends Model
{
@ -54,6 +56,11 @@ class Event extends Model
'ends_at' => 'datetime',
'all_day' => 'boolean',
'is_published' => 'boolean',
'registration_enabled' => 'boolean',
'capacity' => 'integer',
'waitlist' => 'boolean',
'registration_deadline' => 'datetime',
'participants_public' => 'boolean',
];
}
@ -65,6 +72,14 @@ class Event extends Model
return $this->belongsTo(User::class);
}
/**
* @return HasMany<EventRegistration, $this>
*/
public function registrations(): HasMany
{
return $this->hasMany(EventRegistration::class);
}
/**
* Veröffentlichte Termine, die noch nicht vorbei sind (laufende eingeschlossen).
*

View file

@ -0,0 +1,32 @@
<?php
namespace Inhaus\Plugins\Events\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable(['event_id', 'user_id', 'status', 'note'])]
class EventRegistration extends Model
{
public const CONFIRMED = 'confirmed';
public const WAITLIST = 'waitlist';
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* @return BelongsTo<Event, $this>
*/
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
}

View file

@ -0,0 +1,152 @@
<?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;
}
}