A parent registers once and manages lessons for one or more children, who need no login of their own. A child is a real wp_users row with the student role but no usable login — so student_id keeps meaning "a WordPress user" on every table, and booking, credits, policies and enrolments work unchanged. A us_guardians link table maps guardian to child. The signup form gains a parent/guardian tick that reveals a block per child, with the account-signup questions asked per child rather than per guardian — they describe the student, not the account holder. Signup policies are recorded once per child with the guardian as the acceptor, which is the record that actually means something. A family that half-creates is rolled back entirely rather than leaving a guardian who cannot re-register. The booking and enrolment forms gain a "Who is this for?" picker listing children first, so the default selection is never the parent — booking for the wrong child is correctable, quietly billing a parent for their kid's lesson is not. POST /bookings and POST /enrollments take an optional student_id honoured only for that child's guardian; anything else is a 403. That check is the authorisation boundary of the feature. Payments and credits gain a payer: the charge names the child it was for and the guardian who owes it, so per-child reporting is unchanged while notices, receipts and the payment step reach the parent. Credit is held by the payer, so one child's cancellation can settle a sibling's charge, and the daily billing scan sends a guardian one notice covering every child. Closes #132 Co-Authored-By: Claude Opus 5 <[email protected]>
126 lines
3.9 KiB
PHP
126 lines
3.9 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.
|
|
*
|
|
* `$acceptedBy` is who actually agreed, when that is not the student — a
|
|
* guardian booking for a child. It defaults to 0, read back as "the student
|
|
* agreed for themselves".
|
|
*
|
|
* @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, int $acceptedBy = 0 ): 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,
|
|
acceptedBy: $acceptedBy > 0 ? $acceptedBy : $studentId,
|
|
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;
|
|
}
|
|
}
|