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 $acceptances * @return list 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 */ 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 */ 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 ?? [] ); } }