fetchValue('SELECT COUNT(*) FROM `bookings` WHERE tour_id = ?', [$tourId]); } /** * true, wenn der Benutzer die Bildergalerie einer Tour sehen darf: * Admins immer, sonst nur Teilnehmer (vorhandene Buchung). */ public static function canAccessTourGallery(int $tourId, ?User $user): bool { if ($user === null || $tourId <= 0) { return false; } if ($user->isAdmin()) { return true; } return static::forUser($tourId, (int) $user->id) !== null; } public static function forUser(int $tourId, int $userId): ?static { $row = db()->fetchOne('SELECT * FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]); return $row === null ? null : static::fromRow($row); } public static function forTour(int $tourId): array { $rows = db()->fetchAll('SELECT * FROM `bookings` WHERE tour_id = ?', [$tourId]); return array_map(fn(array $row) => static::fromRow($row), $rows); } public static function deleteForUser(int $tourId, int $userId): void { db()->execute('DELETE FROM `bookings` WHERE tour_id = ? AND user_id = ?', [$tourId, $userId]); } /** Bucht einen Raum oder aktualisiert die Raumbuchung eines Benutzers. */ public static function joinRoom(int $tourId, int $userId, int $roomId): void { $booking = static::forUser($tourId, $userId); if ($booking !== null) { $booking->room_id = $roomId; $booking->save(); return; } static::create([ 'tour_id' => $tourId, 'user_id' => $userId, 'room_id' => $roomId, 'created_at' => now(), 'updated_at' => now(), ]); } }