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]>
88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Policy;
|
|
|
|
use Unsupervised\Schedular\Val;
|
|
|
|
class PolicyAcceptance {
|
|
|
|
public const REG_ACCOUNT = 'account';
|
|
public const REG_LESSON = 'lesson';
|
|
public const REG_ENROLLMENT = 'enrollment';
|
|
|
|
/**
|
|
* Polymorphic registration targets an acceptance can attach to. For `account`
|
|
* the registration id is the WordPress user ID.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
public const VALID_REGISTRATION_TYPES = [ self::REG_ACCOUNT, self::REG_LESSON, self::REG_ENROLLMENT ];
|
|
|
|
public function __construct(
|
|
public readonly int $policyVersionId,
|
|
public readonly int $studentId,
|
|
public readonly string $registrationType,
|
|
public readonly int $registrationId,
|
|
/**
|
|
* Who actually clicked "I agree" — a child's guardian, or 0 meaning the
|
|
* student agreed for themselves. Zero rather than a copy of `studentId`
|
|
* so every acceptance recorded before guardian accounts existed keeps its
|
|
* original meaning.
|
|
*/
|
|
public readonly int $acceptedBy = 0,
|
|
public readonly ?string $ipAddress = null,
|
|
public readonly ?string $acceptedAt = null,
|
|
public readonly ?int $id = null,
|
|
) {}
|
|
|
|
public static function fromRow( \stdClass $row ): self {
|
|
return new self(
|
|
policyVersionId: Val::int( $row->policy_version_id ),
|
|
studentId: Val::int( $row->student_id ),
|
|
registrationType: Val::string( $row->registration_type ),
|
|
registrationId: Val::int( $row->registration_id ),
|
|
acceptedBy: Val::int( $row->accepted_by ?? 0 ),
|
|
ipAddress: Val::stringOrNull( $row->ip_address ),
|
|
acceptedAt: Val::stringOrNull( $row->accepted_at ),
|
|
id: Val::int( $row->id ),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Who this acceptance is legally attributable to: the recorded acceptor,
|
|
* falling back to the student. Callers go through here so the `0` default of a
|
|
* pre-guardian acceptance never leaks out as a user id.
|
|
*/
|
|
public function acceptorOrStudent(): int {
|
|
return $this->acceptedBy > 0 ? $this->acceptedBy : $this->studentId;
|
|
}
|
|
|
|
/**
|
|
* Whether someone other than the student agreed — a guardian accepting on a
|
|
* child's behalf. Drives the "accepted by" line on the admin screen, which is
|
|
* noise when they are the same person.
|
|
*/
|
|
public function acceptedOnBehalf(): bool {
|
|
return $this->acceptedBy > 0 && $this->acceptedBy !== $this->studentId;
|
|
}
|
|
|
|
/**
|
|
* Returns a plain array representation of the acceptance.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function toArray(): array {
|
|
return [
|
|
'id' => $this->id,
|
|
'policy_version_id' => $this->policyVersionId,
|
|
'student_id' => $this->studentId,
|
|
'accepted_by' => $this->acceptorOrStudent(),
|
|
'registration_type' => $this->registrationType,
|
|
'registration_id' => $this->registrationId,
|
|
'ip_address' => $this->ipAddress,
|
|
'accepted_at' => $this->acceptedAt,
|
|
];
|
|
}
|
|
}
|