CI / Build Plugin Zip (pull_request) Has been skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 45s
CI / PHPStan (pull_request) Successful in 2m48s
CI / Tests (PHP 8.1) (pull_request) Successful in 41s
CI / No Debug Code (pull_request) Failing after 2s
CI / Coding Standards (pull_request) Successful in 52s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m36s
Availability windows were stored and served as a single bookable row, so a 9:00 AM-4:00 PM window showed to students as one giant slot and booking it consumed the whole day; past and multi-day windows also leaked into the booking page as nonsense entries. - Split windows into consecutive lesson-length slots on save (REST and admin form); each chunk is independently bookable and weekly recurrence creates a series per chunk so "reserve this time weekly" holds the same hour each week - Reject windows spanning multiple days or shorter than the lesson length (400 invalid_window) - Never return slots whose start has passed from GET /availability - Migrate pre-split rows: Plugin::boot re-runs the Installer on version change and AvailabilityRepository::splitOversizedWindows() rewrites unbooked same-day oversized windows in place - Display all times in 12-hour AM/PM form (booking page, wp-admin lists, editor previews) - Add a List | Week view toggle to the student booking page and the instructor availability page, with previous/next-week navigation honouring the site's start_of_week option (new WeekCalendar helper) Co-Authored-By: Claude Fable 5 <[email protected]>
107 lines
3.3 KiB
PHP
107 lines
3.3 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
namespace Unsupervised\Schedular\Availability;
|
||
|
||
use Unsupervised\Schedular\Val;
|
||
|
||
class AvailabilitySlot {
|
||
|
||
public function __construct(
|
||
public readonly int $instructorId,
|
||
public readonly string $startDt,
|
||
public readonly string $endDt,
|
||
public readonly int $durationMinutes = 60,
|
||
public readonly ?int $offeringId = null,
|
||
public readonly bool $isBooked = false,
|
||
public readonly ?int $recurrenceGroup = null,
|
||
public readonly ?int $id = null,
|
||
) {}
|
||
|
||
/**
|
||
* Normalise a submitted slot datetime to canonical `Y-m-d H:i:s`, or null when
|
||
* it is not a real datetime. Accepts the HTML `datetime-local` form
|
||
* (`Y-m-d\TH:i`, optionally with seconds) and the canonical form (optionally
|
||
* without seconds). Anything else — including strings PHP would "helpfully"
|
||
* coerce — is rejected so garbage never reaches the DATETIME column or throws
|
||
* inside the weekly-series date arithmetic.
|
||
*/
|
||
public static function normalizeDateTime( string $value ): ?string {
|
||
foreach ( [ 'Y-m-d H:i:s', 'Y-m-d H:i', 'Y-m-d\TH:i:s', 'Y-m-d\TH:i' ] as $format ) {
|
||
$dt = \DateTimeImmutable::createFromFormat( '!' . $format, $value );
|
||
if ( false !== $dt && $dt->format( $format ) === $value ) {
|
||
return $dt->format( 'Y-m-d H:i:s' );
|
||
}
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* Split this window into consecutive lesson-length slots: 09:00–16:00 with
|
||
* 60-minute lessons yields seven bookable slots. A trailing remainder shorter
|
||
* than the lesson length is dropped, and an empty list is returned when the
|
||
* window cannot fit a single lesson.
|
||
*
|
||
* @return list<self>
|
||
*/
|
||
public function splitByDuration(): array {
|
||
if ( $this->durationMinutes <= 0 ) {
|
||
return [];
|
||
}
|
||
|
||
$end = new \DateTimeImmutable( $this->endDt );
|
||
$step = new \DateInterval( 'PT' . $this->durationMinutes . 'M' );
|
||
|
||
$cursor = new \DateTimeImmutable( $this->startDt );
|
||
$chunkEnd = $cursor->add( $step );
|
||
|
||
$slots = [];
|
||
while ( $chunkEnd <= $end ) {
|
||
$slots[] = new self(
|
||
instructorId: $this->instructorId,
|
||
startDt: $cursor->format( 'Y-m-d H:i:s' ),
|
||
endDt: $chunkEnd->format( 'Y-m-d H:i:s' ),
|
||
durationMinutes: $this->durationMinutes,
|
||
offeringId: $this->offeringId,
|
||
);
|
||
|
||
$cursor = $chunkEnd;
|
||
$chunkEnd = $cursor->add( $step );
|
||
}
|
||
|
||
return $slots;
|
||
}
|
||
|
||
public static function fromRow( \stdClass $row ): self {
|
||
return new self(
|
||
instructorId: Val::int( $row->instructor_id ),
|
||
startDt: Val::string( $row->start_dt ),
|
||
endDt: Val::string( $row->end_dt ),
|
||
durationMinutes: Val::int( $row->duration_minutes ),
|
||
offeringId: Val::intOrNull( $row->offering_id ),
|
||
isBooked: Val::bool( $row->is_booked ),
|
||
recurrenceGroup: Val::intOrNull( $row->recurrence_group ),
|
||
id: Val::int( $row->id ),
|
||
);
|
||
}
|
||
|
||
/**
|
||
* Returns a plain array representation of the slot.
|
||
*
|
||
* @return array<string, mixed>
|
||
*/
|
||
public function toArray(): array {
|
||
return [
|
||
'id' => $this->id,
|
||
'instructor_id' => $this->instructorId,
|
||
'offering_id' => $this->offeringId,
|
||
'start_dt' => $this->startDt,
|
||
'end_dt' => $this->endDt,
|
||
'duration_minutes' => $this->durationMinutes,
|
||
'is_booked' => $this->isBooked,
|
||
'recurrence_group' => $this->recurrenceGroup,
|
||
];
|
||
}
|
||
}
|