Add lesson booking registration flow (offering, questions, policies)
CI / Coding Standards (pull_request) Successful in 1m51s
CI / PHPStan (pull_request) Successful in 2m17s
CI / Tests (PHP 8.1) (pull_request) Successful in 2m24s
CI / No Debug Code (pull_request) Successful in 2s
CI / Tests (PHP 8.2) (pull_request) Successful in 42s
CI / Tests (PHP 8.3) (pull_request) Successful in 47s
CI / Build Plugin Zip (pull_request) Has been skipped

Implements #3: students register for a private lesson by picking a slot,
answering the offering's intake questions, and accepting booking-scoped
policies. Payment is a clean seam for #7 (lessons land pending; payment_id
null; instructor confirms via PATCH /bookings/{id}/status).

- Schema: us_lessons += offering_id, recurrence, series_id, payment_id.
- Lesson: new fields + recurrence constants.
- BookingRepository::insertSeries() builds a weekly series sharing a
  series_id; AvailabilityRepository::findUnbookedInGroup() reserves a group.
- RegistrationGate (src/Registration/): validate + record intake answers and
  booking-scoped policy acceptances. Reused by group enrolment (#4).
- BookingEndpoint::book(): offering_id, recurrence, answers,
  accepted_policy_version_ids; single or weekly; records answers/acceptances
  (type lesson).
- GET /policies?scope=booking filter.
- Front-end booking.js: slot -> questions + policies -> submit.
- Wiring: RegistrationGate built in Plugin, passed via RestRegistrar.
- Test-only WP_Error stub in tests/bootstrap.php for gate testing.

Refs #3

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 11:25:30 -03:00
parent d0dddd9075
commit 6d163e5d0e
15 changed files with 649 additions and 68 deletions
@@ -141,6 +141,22 @@ class AvailabilityRepository {
return array_map( AvailabilitySlot::fromRow( ... ), $rows ?? [] );
}
/**
* Unbooked slots that belong to a weekly-recurring group, ordered by start.
*
* @return list<AvailabilitySlot>
*/
public function findUnbookedInGroup( int $recurrenceGroup ): array {
$rows = $this->db->get_results(
$this->db->prepare(
"SELECT * FROM {$this->table} WHERE recurrence_group = %d AND is_booked = 0 ORDER BY start_dt ASC",
$recurrenceGroup
)
);
return array_map( AvailabilitySlot::fromRow( ... ), $rows ?? [] );
}
public function findById( int $id ): ?AvailabilitySlot {
$row = $this->db->get_row(
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
+86 -8
View File
@@ -5,12 +5,17 @@ namespace Unsupervised\Schedular\Booking;
use Unsupervised\Schedular\Availability\AvailabilityRepository;
use Unsupervised\Schedular\Auth\RoleManager;
use Unsupervised\Schedular\Offering\OfferingRepository;
use Unsupervised\Schedular\Policy\PolicyAcceptance;
use Unsupervised\Schedular\Registration\RegistrationGate;
class BookingEndpoint {
public function __construct(
private AvailabilityRepository $availability,
private BookingRepository $bookings,
private OfferingRepository $offerings,
private RegistrationGate $gate,
) {}
public function registerRoutes( string $route_namespace ): void {
@@ -28,12 +33,28 @@ class BookingEndpoint {
'callback' => [ $this, 'book' ],
'permission_callback' => [ $this, 'canBook' ],
'args' => [
'slot_id' => [
'slot_id' => [
'type' => 'integer',
'required' => true,
'sanitize_callback' => 'absint',
],
'notes' => [
'offering_id' => [
'type' => 'integer',
'default' => 0,
],
'recurrence' => [
'type' => 'string',
'default' => 'single',
],
'answers' => [
'type' => 'object',
'default' => [],
],
'accepted_policy_version_ids' => [
'type' => 'array',
'default' => [],
],
'notes' => [
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_textarea_field',
@@ -84,26 +105,83 @@ class BookingEndpoint {
return new \WP_Error( 'slot_taken', __( 'This slot is already booked.', 'unsupervised-schedular' ), [ 'status' => 409 ] );
}
$notes = (string) $request->get_param( 'notes' );
$lesson = new Lesson(
$offeringId = absint( $request->get_param( 'offering_id' ) );
if ( 0 === $offeringId ) {
$offeringId = (int) ( $slot->offeringId ?? 0 );
}
if ( $offeringId > 0 && null === $this->offerings->findById( $offeringId ) ) {
return new \WP_Error( 'invalid_offering', __( 'Offering not found.', 'unsupervised-schedular' ), [ 'status' => 400 ] );
}
$answers = $this->answers( $request );
$acceptedVersionIds = array_map( 'absint', (array) $request->get_param( 'accepted_policy_version_ids' ) );
$gateError = $this->gate->validate( $offeringId, $answers, $acceptedVersionIds );
if ( $gateError instanceof \WP_Error ) {
return $gateError;
}
$studentId = get_current_user_id();
$notes = (string) $request->get_param( 'notes' );
$recurrence = Lesson::RECURRENCE_WEEKLY === $request->get_param( 'recurrence' )
? Lesson::RECURRENCE_WEEKLY
: Lesson::RECURRENCE_SINGLE;
$template = new Lesson(
slotId: $slotId,
studentId: get_current_user_id(),
studentId: $studentId,
instructorId: $slot->instructorId,
offeringId: $offeringId > 0 ? $offeringId : null,
recurrence: $recurrence,
notes: '' !== $notes ? $notes : null,
);
$id = $this->bookings->insert( $lesson );
$this->availability->markBooked( $slotId );
// Weekly reservation across the slot's recurring group; otherwise a single lesson.
if ( Lesson::RECURRENCE_WEEKLY === $recurrence && null !== $slot->recurrenceGroup ) {
$slotIds = array_map( static fn( $s ): int => (int) $s->id, $this->availability->findUnbookedInGroup( $slot->recurrenceGroup ) );
$ids = $this->bookings->insertSeries( $template, $slotIds );
foreach ( $slotIds as $reservedSlotId ) {
$this->availability->markBooked( $reservedSlotId );
}
$anchorId = $ids[0] ?? 0;
} else {
$anchorId = $this->bookings->insert( $template );
$this->availability->markBooked( $slotId );
$ids = [ $anchorId ];
}
$this->gate->record( PolicyAcceptance::REG_LESSON, $anchorId, $studentId, $offeringId, $answers, $acceptedVersionIds, $this->clientIp() );
return new \WP_REST_Response(
[
'id' => $id,
'ids' => $ids,
'status' => Lesson::STATUS_PENDING,
],
201
);
}
/**
* Extract a question_id => value map from the request.
*
* @return array<int, string>
*/
private function answers( \WP_REST_Request $request ): array {
$out = [];
foreach ( (array) $request->get_param( 'answers' ) as $questionId => $value ) {
$out[ (int) $questionId ] = sanitize_text_field( (string) $value );
}
return $out;
}
private function clientIp(): ?string {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- IP stored verbatim for audit.
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) );
return '' !== $ip ? $ip : null;
}
public function updateStatus( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
$id = absint( $request->get_param( 'id' ) );
$lesson = $this->bookings->findById( $id );
+51 -1
View File
@@ -16,18 +16,68 @@ class BookingRepository {
$this->table,
[
'slot_id' => $lesson->slotId,
'offering_id' => $lesson->offeringId,
'student_id' => $lesson->studentId,
'instructor_id' => $lesson->instructorId,
'recurrence' => $lesson->recurrence,
'series_id' => $lesson->seriesId,
'status' => $lesson->status,
'payment_id' => $lesson->paymentId,
'notes' => $lesson->notes,
'created_at' => current_time( 'mysql' ),
],
[ '%d', '%d', '%d', '%s', '%s', '%s' ]
[ '%d', '%d', '%d', '%d', '%s', '%d', '%s', '%d', '%s', '%s' ]
);
return $this->db->insert_id;
}
/**
* Create a weekly lesson series — one lesson per slot, all sharing a
* `series_id` (the id of the first lesson row).
*
* @param list<int> $slotIds Availability slot IDs to reserve, in order.
* @return list<int> Inserted lesson IDs.
*/
public function insertSeries( Lesson $template, array $slotIds ): array {
$ids = [];
$seriesId = 0;
foreach ( $slotIds as $slotId ) {
$id = $this->insert(
new Lesson(
slotId: $slotId,
studentId: $template->studentId,
instructorId: $template->instructorId,
offeringId: $template->offeringId,
recurrence: Lesson::RECURRENCE_WEEKLY,
seriesId: $seriesId > 0 ? $seriesId : null,
status: $template->status,
notes: $template->notes,
)
);
if ( 0 === $seriesId ) {
$seriesId = $id;
$this->setSeriesId( $id, $seriesId );
}
$ids[] = $id;
}
return $ids;
}
private function setSeriesId( int $id, int $seriesId ): void {
$this->db->update(
$this->table,
[ 'series_id' => $seriesId ],
[ 'id' => $id ],
[ '%d' ],
[ '%d' ]
);
}
public function findById( int $id ): ?Lesson {
$row = $this->db->get_row(
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
+22
View File
@@ -16,11 +16,25 @@ class Lesson {
*/
public const VALID_STATUSES = [ self::STATUS_PENDING, self::STATUS_CONFIRMED, self::STATUS_CANCELLED ];
public const RECURRENCE_SINGLE = 'single';
public const RECURRENCE_WEEKLY = 'weekly';
/**
* All valid recurrence values.
*
* @var list<string>
*/
public const VALID_RECURRENCES = [ self::RECURRENCE_SINGLE, self::RECURRENCE_WEEKLY ];
public function __construct(
public readonly int $slotId,
public readonly int $studentId,
public readonly int $instructorId,
public readonly ?int $offeringId = null,
public readonly string $recurrence = self::RECURRENCE_SINGLE,
public readonly ?int $seriesId = null,
public readonly string $status = self::STATUS_PENDING,
public readonly ?int $paymentId = null,
public readonly ?string $notes = null,
public readonly ?int $id = null,
) {}
@@ -30,7 +44,11 @@ class Lesson {
slotId: (int) $row->slot_id,
studentId: (int) $row->student_id,
instructorId: (int) $row->instructor_id,
offeringId: null !== $row->offering_id ? (int) $row->offering_id : null,
recurrence: $row->recurrence,
seriesId: null !== $row->series_id ? (int) $row->series_id : null,
status: $row->status,
paymentId: null !== $row->payment_id ? (int) $row->payment_id : null,
notes: $row->notes,
id: (int) $row->id,
);
@@ -45,9 +63,13 @@ class Lesson {
return [
'id' => $this->id,
'slot_id' => $this->slotId,
'offering_id' => $this->offeringId,
'student_id' => $this->studentId,
'instructor_id' => $this->instructorId,
'recurrence' => $this->recurrence,
'series_id' => $this->seriesId,
'status' => $this->status,
'payment_id' => $this->paymentId,
'notes' => $this->notes,
];
}
+14 -10
View File
@@ -12,7 +12,9 @@ use Unsupervised\Schedular\Policy\AcceptanceRepository;
use Unsupervised\Schedular\Policy\PolicyRepository;
use Unsupervised\Schedular\Policy\PolicyService;
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
use Unsupervised\Schedular\Registration\AnswerRepository;
use Unsupervised\Schedular\Registration\QuestionRepository;
use Unsupervised\Schedular\Registration\RegistrationGate;
class Plugin {
@@ -20,19 +22,21 @@ class Plugin {
load_plugin_textdomain( 'unsupervised-schedular', false, dirname( plugin_basename( USC_PLUGIN_FILE ) ) . '/languages' );
global $wpdb;
$availability = new AvailabilityRepository( $wpdb );
$bookings = new BookingRepository( $wpdb );
$offerings = new OfferingRepository( $wpdb );
$questions = new QuestionRepository( $wpdb );
$policies = new PolicyRepository( $wpdb );
$policyVersions = new PolicyVersionRepository( $wpdb );
$policyService = new PolicyService( $policies, $policyVersions );
$acceptances = new AcceptanceRepository( $wpdb );
$invites = new InviteRepository( $wpdb );
$availability = new AvailabilityRepository( $wpdb );
$bookings = new BookingRepository( $wpdb );
$offerings = new OfferingRepository( $wpdb );
$questions = new QuestionRepository( $wpdb );
$answers = new AnswerRepository( $wpdb );
$policies = new PolicyRepository( $wpdb );
$policyVersions = new PolicyVersionRepository( $wpdb );
$policyService = new PolicyService( $policies, $policyVersions );
$acceptances = new AcceptanceRepository( $wpdb );
$invites = new InviteRepository( $wpdb );
$registrationGate = new RegistrationGate( $questions, $answers, $policies, $policyVersions, $acceptances );
( new RoleManager() )->register();
( new AdminMenu( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService, $invites ) )->register();
( new RestRegistrar( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService ) )->register();
( new RestRegistrar( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService, $registrationGate ) )->register();
( new ShortcodeRegistrar( $invites, $policies, $policyVersions, $acceptances ) )->register();
}
}
+10 -3
View File
@@ -69,12 +69,19 @@ class PolicyEndpoint {
}
/**
* Public: the current published version of every policy (the registration gate).
* Public: the current published version of every policy (the registration
* gate). Pass `?scope=signup|booking` to limit to that gate (includes
* `both`-scoped policies).
*/
public function index(): \WP_REST_Response {
public function index( \WP_REST_Request $request ): \WP_REST_Response {
$scope = (string) $request->get_param( 'scope' );
$policies = in_array( $scope, [ Policy::SCOPE_SIGNUP, Policy::SCOPE_BOOKING ], true )
? $this->policies->findForScope( $scope )
: $this->policies->findAll();
$out = [];
foreach ( $this->policies->findAll() as $policy ) {
foreach ( $policies as $policy ) {
if ( null === $policy->currentVersionId ) {
continue;
}
+120
View File
@@ -0,0 +1,120 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Registration;
use Unsupervised\Schedular\Policy\AcceptanceRepository;
use Unsupervised\Schedular\Policy\Policy;
use Unsupervised\Schedular\Policy\PolicyAcceptance;
use Unsupervised\Schedular\Policy\PolicyRepository;
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
/**
* Shared registration gate: validates and records the intake-question answers
* and booking-scoped policy acceptances common to lesson bookings and group
* enrolments.
*/
class RegistrationGate {
public function __construct(
private QuestionRepository $questions,
private AnswerRepository $answers,
private PolicyRepository $policies,
private PolicyVersionRepository $versions,
private AcceptanceRepository $acceptances,
) {}
/**
* Validate that required questions are answered and all booking-scoped
* published policies are accepted. Returns null when the gate passes.
*
* @param array<int, string> $answers question_id => answer value
* @param list<int> $acceptedVersionIds Accepted policy version IDs
*/
public function validate( int $offeringId, array $answers, array $acceptedVersionIds ): ?\WP_Error {
foreach ( $this->questions->findByOffering( $offeringId, true ) as $question ) {
if ( $question->isRequired && '' === trim( (string) ( $answers[ (int) $question->id ] ?? '' ) ) ) {
return new \WP_Error(
'missing_answer',
__( 'Please answer all required questions.', 'unsupervised-schedular' ),
[ 'status' => 400 ]
);
}
}
foreach ( $this->requiredPolicyVersionIds() as $versionId ) {
if ( ! in_array( $versionId, $acceptedVersionIds, true ) ) {
return new \WP_Error(
'policy_required',
__( 'You must accept all required policies.', 'unsupervised-schedular' ),
[ 'status' => 400 ]
);
}
}
return null;
}
/**
* Persist answers and policy acceptances for a created registration.
*
* @param array<int, string> $answers question_id => answer value
* @param list<int> $acceptedVersionIds Accepted policy version IDs
*/
public function record( string $registrationType, int $registrationId, int $studentId, int $offeringId, array $answers, array $acceptedVersionIds, ?string $ipAddress = null ): void {
foreach ( $this->questions->findByOffering( $offeringId, true ) as $question ) {
$value = (string) ( $answers[ (int) $question->id ] ?? '' );
if ( '' === $value ) {
continue;
}
$this->answers->insert(
new Answer(
questionId: (int) $question->id,
registrationType: $registrationType,
registrationId: $registrationId,
studentId: $studentId,
answerValue: $value,
)
);
}
foreach ( $this->requiredPolicyVersionIds() as $versionId ) {
if ( ! in_array( $versionId, $acceptedVersionIds, true ) ) {
continue;
}
$this->acceptances->insert(
new PolicyAcceptance(
policyVersionId: $versionId,
studentId: $studentId,
registrationType: $registrationType,
registrationId: $registrationId,
ipAddress: $ipAddress,
)
);
}
}
/**
* Current published version IDs of every booking-scoped policy.
*
* @return list<int>
*/
private function requiredPolicyVersionIds(): array {
$ids = [];
foreach ( $this->policies->findForScope( Policy::SCOPE_BOOKING ) as $policy ) {
if ( null === $policy->currentVersionId ) {
continue;
}
$version = $this->versions->findById( $policy->currentVersionId );
if ( null !== $version && $version->isPublished() ) {
$ids[] = (int) $version->id;
}
}
return $ids;
}
}
+3 -2
View File
@@ -15,6 +15,7 @@ use Unsupervised\Schedular\Policy\PolicyService;
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
use Unsupervised\Schedular\Registration\QuestionEndpoint;
use Unsupervised\Schedular\Registration\QuestionRepository;
use Unsupervised\Schedular\Registration\RegistrationGate;
class RestRegistrar {
@@ -26,9 +27,9 @@ class RestRegistrar {
private QuestionEndpoint $questionEndpoint;
private PolicyEndpoint $policyEndpoint;
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions, PolicyRepository $policies, PolicyVersionRepository $policyVersions, PolicyService $policyService ) {
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions, PolicyRepository $policies, PolicyVersionRepository $policyVersions, PolicyService $policyService, RegistrationGate $gate ) {
$this->availabilityEndpoint = new AvailabilityEndpoint( $availability );
$this->bookingEndpoint = new BookingEndpoint( $availability, $bookings );
$this->bookingEndpoint = new BookingEndpoint( $availability, $bookings, $offerings, $gate );
$this->offeringEndpoint = new OfferingEndpoint( $offerings );
$this->questionEndpoint = new QuestionEndpoint( $questions, $offerings );
$this->policyEndpoint = new PolicyEndpoint( $policies, $policyVersions, $policyService );
+7 -1
View File
@@ -32,15 +32,21 @@ class Schema {
"CREATE TABLE {$prefix}us_lessons (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
slot_id BIGINT UNSIGNED NOT NULL,
offering_id BIGINT UNSIGNED DEFAULT NULL,
student_id BIGINT UNSIGNED NOT NULL,
instructor_id BIGINT UNSIGNED NOT NULL,
recurrence VARCHAR(10) NOT NULL DEFAULT 'single',
series_id BIGINT UNSIGNED DEFAULT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
payment_id BIGINT UNSIGNED DEFAULT NULL,
notes TEXT,
created_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY slot_id (slot_id),
KEY offering_id (offering_id),
KEY student_id (student_id),
KEY instructor_id (instructor_id)
KEY instructor_id (instructor_id),
KEY series_id (series_id)
) {$charset};",
"CREATE TABLE {$prefix}us_offerings (