e61d99daed
CI / Coding Standards (pull_request) Successful in 51s
CI / PHPStan (pull_request) Successful in 1m0s
CI / Tests (PHP 8.1) (pull_request) Successful in 46s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 47s
CI / No Debug Code (pull_request) Successful in 3s
Implements #5: studio admin / instructors author intake questions scoped per offering; answers are stored against a lesson or group enrolment via a polymorphic registration reference. - src/Registration/: Question + Answer value objects, QuestionRepository and AnswerRepository, QuestionEndpoint (REST), QuestionController + templates/admin/questions.php (Offerings -> Questions submenu) - us_questions and us_question_answers tables in Schema.php - REST: public GET /offerings/{id}/questions; POST/PATCH/DELETE /questions gated by manage_questions + offering ownership (owner or studio admin) - Field types text/textarea/select/checkbox; select options stored as JSON - Wiring in Plugin, RestRegistrar, AdminMenu AnswerRepository is built now and consumed by the booking/enrolment flow in #3/#4. Tests: tests/Unit/Registration/ (19 tests). composer test (63 total), cs, and PHPStan level 6 all pass. Refs #5 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
58 lines
1.5 KiB
PHP
58 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Registration;
|
|
|
|
class AnswerRepository {
|
|
|
|
private string $table;
|
|
|
|
public function __construct( private \wpdb $db ) {
|
|
$this->table = $db->prefix . 'us_question_answers';
|
|
}
|
|
|
|
public function insert( Answer $answer ): int {
|
|
$this->db->insert(
|
|
$this->table,
|
|
[
|
|
'question_id' => $answer->questionId,
|
|
'registration_type' => $answer->registrationType,
|
|
'registration_id' => $answer->registrationId,
|
|
'student_id' => $answer->studentId,
|
|
'answer_value' => $answer->answerValue,
|
|
'created_at' => current_time( 'mysql' ),
|
|
],
|
|
[ '%d', '%s', '%d', '%d', '%s', '%s' ]
|
|
);
|
|
|
|
return $this->db->insert_id;
|
|
}
|
|
|
|
/**
|
|
* Persist a batch of answers for a single registration.
|
|
*
|
|
* @param list<Answer> $answers
|
|
* @return list<int> Inserted answer IDs.
|
|
*/
|
|
public function insertMany( array $answers ): array {
|
|
return array_map( fn( Answer $answer ): int => $this->insert( $answer ), $answers );
|
|
}
|
|
|
|
/**
|
|
* Find all answers attached to a registration (lesson or enrolment).
|
|
*
|
|
* @return list<Answer>
|
|
*/
|
|
public function findByRegistration( string $registrationType, int $registrationId ): array {
|
|
$rows = $this->db->get_results(
|
|
$this->db->prepare(
|
|
"SELECT * FROM {$this->table} WHERE registration_type = %s AND registration_id = %d ORDER BY id ASC",
|
|
$registrationType,
|
|
$registrationId
|
|
)
|
|
);
|
|
|
|
return array_map( Answer::fromRow( ... ), $rows ?? [] );
|
|
}
|
|
}
|