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 <[email protected]>
121 lines
3.6 KiB
PHP
121 lines
3.6 KiB
PHP
<?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;
|
|
}
|
|
}
|