Files
unsupervised-scheduler/src/Policy/AcceptanceRepository.php
T
thatguygriffandClaude Opus 5 b772e1811e Let parents register once and book for their children
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]>
2026-07-29 16:07:52 -03:00

90 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Policy;
class AcceptanceRepository {
private string $table;
public function __construct( private \wpdb $db ) {
$this->table = $db->prefix . 'us_policy_acceptances';
}
public function insert( PolicyAcceptance $acceptance ): int {
$this->db->insert(
$this->table,
[
'policy_version_id' => $acceptance->policyVersionId,
'student_id' => $acceptance->studentId,
'accepted_by' => $acceptance->acceptorOrStudent(),
'registration_type' => $acceptance->registrationType,
'registration_id' => $acceptance->registrationId,
'ip_address' => $acceptance->ipAddress,
'accepted_at' => current_time( 'mysql' ),
],
[ '%d', '%d', '%d', '%s', '%d', '%s', '%s' ]
);
return $this->db->insert_id;
}
/**
* Persist a batch of acceptances for a single registration.
*
* @param list<PolicyAcceptance> $acceptances
* @return list<int> Inserted acceptance IDs.
*/
public function insertMany( array $acceptances ): array {
return array_map( fn( PolicyAcceptance $a ): int => $this->insert( $a ), $acceptances );
}
/**
* Backfill `accepted_by` on acceptances recorded before guardian accounts
* existed, where the student always agreed for themselves. Run once from the
* installer so the acceptor is a real user id on every row.
*/
public function backfillAcceptedBy(): void {
$sql = $this->db->prepare( 'UPDATE %i SET accepted_by = student_id WHERE accepted_by = 0', $this->table );
if ( null !== $sql ) {
$this->db->query( $sql );
}
}
/**
* Find all acceptances attached to a registration (lesson or enrolment).
*
* @return list<PolicyAcceptance>
*/
public function findByRegistration( string $registrationType, int $registrationId ): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE registration_type = %s AND registration_id = %d ORDER BY id ASC',
$this->table,
$registrationType,
$registrationId
)
);
return array_map( PolicyAcceptance::fromRow( ... ), $rows ?? [] );
}
/**
* Find every acceptance a student has recorded, newest first.
*
* @return list<PolicyAcceptance>
*/
public function findByStudent( int $studentId ): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE student_id = %d ORDER BY accepted_at DESC, id DESC',
$this->table,
$studentId
)
);
return array_map( PolicyAcceptance::fromRow( ... ), $rows ?? [] );
}
}