Add Registration Questions domain (per-offering intake forms)
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
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>
This commit is contained in:
+15
-1
@@ -10,17 +10,21 @@ use Unsupervised\Schedular\Booking\BookingRepository;
|
||||
use Unsupervised\Schedular\Booking\LessonController;
|
||||
use Unsupervised\Schedular\Offering\OfferingController;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
use Unsupervised\Schedular\Registration\QuestionController;
|
||||
use Unsupervised\Schedular\Registration\QuestionRepository;
|
||||
|
||||
class AdminMenu {
|
||||
|
||||
private AvailabilityController $availabilityController;
|
||||
private LessonController $lessonController;
|
||||
private OfferingController $offeringController;
|
||||
private QuestionController $questionController;
|
||||
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings ) {
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions ) {
|
||||
$this->availabilityController = new AvailabilityController( $availability );
|
||||
$this->lessonController = new LessonController( $bookings );
|
||||
$this->offeringController = new OfferingController( $offerings );
|
||||
$this->questionController = new QuestionController( $questions, $offerings );
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
@@ -61,6 +65,16 @@ class AdminMenu {
|
||||
33
|
||||
);
|
||||
|
||||
// Studio admin / instructor: manage per-offering intake questions.
|
||||
add_submenu_page(
|
||||
'us-offerings',
|
||||
__( 'Questions', 'unsupervised-schedular' ),
|
||||
__( 'Questions', 'unsupervised-schedular' ),
|
||||
RoleManager::CAP_MANAGE_QUESTIONS,
|
||||
'us-questions',
|
||||
[ $this->questionController, 'renderPage' ]
|
||||
);
|
||||
|
||||
// Instructor: view their upcoming lessons.
|
||||
add_menu_page(
|
||||
__( 'My Lessons', 'unsupervised-schedular' ),
|
||||
|
||||
+4
-2
@@ -7,6 +7,7 @@ use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
||||
use Unsupervised\Schedular\Booking\BookingRepository;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
use Unsupervised\Schedular\Registration\QuestionRepository;
|
||||
|
||||
class Plugin {
|
||||
|
||||
@@ -17,10 +18,11 @@ class Plugin {
|
||||
$availability = new AvailabilityRepository( $wpdb );
|
||||
$bookings = new BookingRepository( $wpdb );
|
||||
$offerings = new OfferingRepository( $wpdb );
|
||||
$questions = new QuestionRepository( $wpdb );
|
||||
|
||||
( new RoleManager() )->register();
|
||||
( new AdminMenu( $availability, $bookings, $offerings ) )->register();
|
||||
( new RestRegistrar( $availability, $bookings, $offerings ) )->register();
|
||||
( new AdminMenu( $availability, $bookings, $offerings, $questions ) )->register();
|
||||
( new RestRegistrar( $availability, $bookings, $offerings, $questions ) )->register();
|
||||
( new ShortcodeRegistrar() )->register();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Registration;
|
||||
|
||||
class Answer {
|
||||
|
||||
public const REG_LESSON = 'lesson';
|
||||
public const REG_ENROLLMENT = 'enrollment';
|
||||
|
||||
/**
|
||||
* Polymorphic registration targets an answer can attach to.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const VALID_REGISTRATION_TYPES = [ self::REG_LESSON, self::REG_ENROLLMENT ];
|
||||
|
||||
public function __construct(
|
||||
public readonly int $questionId,
|
||||
public readonly string $registrationType,
|
||||
public readonly int $registrationId,
|
||||
public readonly int $studentId,
|
||||
public readonly ?string $answerValue = null,
|
||||
public readonly ?int $id = null,
|
||||
) {}
|
||||
|
||||
public static function fromRow( object $row ): self {
|
||||
return new self(
|
||||
questionId: (int) $row->question_id,
|
||||
registrationType: $row->registration_type,
|
||||
registrationId: (int) $row->registration_id,
|
||||
studentId: (int) $row->student_id,
|
||||
answerValue: $row->answer_value,
|
||||
id: (int) $row->id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain array representation of the answer.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'question_id' => $this->questionId,
|
||||
'registration_type' => $this->registrationType,
|
||||
'registration_id' => $this->registrationId,
|
||||
'student_id' => $this->studentId,
|
||||
'answer_value' => $this->answerValue,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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 ?? [] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Registration;
|
||||
|
||||
class Question {
|
||||
|
||||
public const FIELD_TEXT = 'text';
|
||||
public const FIELD_TEXTAREA = 'textarea';
|
||||
public const FIELD_SELECT = 'select';
|
||||
public const FIELD_CHECKBOX = 'checkbox';
|
||||
|
||||
/**
|
||||
* All valid field types.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const VALID_FIELD_TYPES = [
|
||||
self::FIELD_TEXT,
|
||||
self::FIELD_TEXTAREA,
|
||||
self::FIELD_SELECT,
|
||||
self::FIELD_CHECKBOX,
|
||||
];
|
||||
|
||||
/**
|
||||
* Build an intake question value object.
|
||||
*
|
||||
* @param list<string>|null $options Choices for a `select` field.
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $offeringId,
|
||||
public readonly string $label,
|
||||
public readonly string $fieldType = self::FIELD_TEXT,
|
||||
public readonly ?array $options = null,
|
||||
public readonly bool $isRequired = false,
|
||||
public readonly int $sortOrder = 0,
|
||||
public readonly bool $isActive = true,
|
||||
public readonly ?int $id = null,
|
||||
) {}
|
||||
|
||||
public static function fromRow( object $row ): self {
|
||||
$options = null;
|
||||
if ( null !== $row->options && '' !== $row->options ) {
|
||||
$decoded = json_decode( (string) $row->options, true );
|
||||
$options = is_array( $decoded ) ? array_values( array_map( 'strval', $decoded ) ) : null;
|
||||
}
|
||||
|
||||
return new self(
|
||||
offeringId: (int) $row->offering_id,
|
||||
label: $row->label,
|
||||
fieldType: $row->field_type,
|
||||
options: $options,
|
||||
isRequired: (bool) $row->is_required,
|
||||
sortOrder: (int) $row->sort_order,
|
||||
isActive: (bool) $row->is_active,
|
||||
id: (int) $row->id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain array representation of the question.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'offering_id' => $this->offeringId,
|
||||
'label' => $this->label,
|
||||
'field_type' => $this->fieldType,
|
||||
'options' => $this->options,
|
||||
'is_required' => $this->isRequired,
|
||||
'sort_order' => $this->sortOrder,
|
||||
'is_active' => $this->isActive,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Registration;
|
||||
|
||||
use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Offering\Offering;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
|
||||
class QuestionController {
|
||||
|
||||
public function __construct(
|
||||
private QuestionRepository $questions,
|
||||
private OfferingRepository $offerings,
|
||||
) {}
|
||||
|
||||
public function renderPage(): void {
|
||||
if ( ! current_user_can( RoleManager::CAP_MANAGE_QUESTIONS ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to manage questions.', 'unsupervised-schedular' ) );
|
||||
}
|
||||
|
||||
$userId = get_current_user_id();
|
||||
$manageAll = current_user_can( RoleManager::CAP_MANAGE_INSTRUCTORS );
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only offering selector.
|
||||
$offeringId = absint( $_GET['offering_id'] ?? 0 );
|
||||
$offeringList = $manageAll ? $this->offerings->findAll() : $this->offerings->findAll( $userId );
|
||||
$selectedOffering = $offeringId > 0 ? $this->offerings->findById( $offeringId ) : null;
|
||||
|
||||
if ( null !== $selectedOffering && ! $this->canManageOffering( $selectedOffering, $userId, $manageAll ) ) {
|
||||
$selectedOffering = null;
|
||||
}
|
||||
|
||||
$questions = null;
|
||||
if ( null !== $selectedOffering ) {
|
||||
if ( isset( $_POST['usc_action'] ) && check_admin_referer( 'usc_question_action' ) ) {
|
||||
$this->handleFormAction( $selectedOffering );
|
||||
}
|
||||
|
||||
$questions = $this->questions->findByOffering( (int) $selectedOffering->id );
|
||||
}
|
||||
|
||||
include USC_PLUGIN_DIR . 'templates/admin/questions.php';
|
||||
}
|
||||
|
||||
private function handleFormAction( Offering $offering ): void {
|
||||
// Nonce is verified by the caller (renderPage) before this method runs.
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
$action = sanitize_key( wp_unslash( $_POST['usc_action'] ?? '' ) );
|
||||
|
||||
if ( 'add' === $action ) {
|
||||
$this->addQuestion( (int) $offering->id );
|
||||
}
|
||||
|
||||
if ( 'delete' === $action ) {
|
||||
$questionId = absint( $_POST['question_id'] ?? 0 );
|
||||
if ( $questionId > 0 ) {
|
||||
$question = $this->questions->findById( $questionId );
|
||||
if ( $question && $question->offeringId === (int) $offering->id ) {
|
||||
$this->questions->delete( $questionId );
|
||||
}
|
||||
}
|
||||
}
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
}
|
||||
|
||||
private function addQuestion( int $offeringId ): void {
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
$label = sanitize_text_field( wp_unslash( $_POST['label'] ?? '' ) );
|
||||
$fieldType = sanitize_key( wp_unslash( $_POST['field_type'] ?? Question::FIELD_TEXT ) );
|
||||
|
||||
if ( '' === $label || ! in_array( $fieldType, Question::VALID_FIELD_TYPES, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->questions->insert(
|
||||
new Question(
|
||||
offeringId: $offeringId,
|
||||
label: $label,
|
||||
fieldType: $fieldType,
|
||||
options: $this->parseOptions( sanitize_textarea_field( wp_unslash( $_POST['options'] ?? '' ) ) ),
|
||||
isRequired: isset( $_POST['is_required'] ),
|
||||
sortOrder: absint( $_POST['sort_order'] ?? 0 ),
|
||||
)
|
||||
);
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
}
|
||||
|
||||
private function canManageOffering( Offering $offering, int $userId, bool $manageAll ): bool {
|
||||
return $manageAll || $offering->instructorId === $userId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a newline-separated textarea into a list of option strings.
|
||||
*
|
||||
* @return list<string>|null
|
||||
*/
|
||||
private function parseOptions( string $raw ): ?array {
|
||||
$lines = preg_split( '/\r\n|\r|\n/', $raw );
|
||||
$options = array_values(
|
||||
array_filter(
|
||||
array_map(
|
||||
static fn( string $line ): string => sanitize_text_field( trim( $line ) ),
|
||||
false === $lines ? [] : $lines
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return [] === $options ? null : $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Registration;
|
||||
|
||||
use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
|
||||
class QuestionEndpoint {
|
||||
|
||||
public function __construct(
|
||||
private QuestionRepository $questions,
|
||||
private OfferingRepository $offerings,
|
||||
) {}
|
||||
|
||||
public function registerRoutes( string $route_namespace ): void {
|
||||
register_rest_route(
|
||||
$route_namespace,
|
||||
'/offerings/(?P<id>\d+)/questions',
|
||||
[
|
||||
[
|
||||
'methods' => \WP_REST_Server::READABLE,
|
||||
'callback' => [ $this, 'index' ],
|
||||
'permission_callback' => '__return_true',
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$route_namespace,
|
||||
'/questions',
|
||||
[
|
||||
[
|
||||
'methods' => \WP_REST_Server::CREATABLE,
|
||||
'callback' => [ $this, 'create' ],
|
||||
'permission_callback' => [ $this, 'canManage' ],
|
||||
],
|
||||
]
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$route_namespace,
|
||||
'/questions/(?P<id>\d+)',
|
||||
[
|
||||
[
|
||||
'methods' => \WP_REST_Server::EDITABLE,
|
||||
'callback' => [ $this, 'update' ],
|
||||
'permission_callback' => [ $this, 'canManage' ],
|
||||
],
|
||||
[
|
||||
'methods' => \WP_REST_Server::DELETABLE,
|
||||
'callback' => [ $this, 'delete' ],
|
||||
'permission_callback' => [ $this, 'canManage' ],
|
||||
],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function index( \WP_REST_Request $request ): \WP_REST_Response {
|
||||
$questions = $this->questions->findByOffering( absint( $request->get_param( 'id' ) ), activeOnly: true );
|
||||
|
||||
return new \WP_REST_Response( array_map( fn( Question $q ) => $q->toArray(), $questions ), 200 );
|
||||
}
|
||||
|
||||
public function create( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
|
||||
$offeringId = absint( $request->get_param( 'offering_id' ) );
|
||||
$ownerCheck = $this->requireOfferingOwner( $offeringId );
|
||||
if ( $ownerCheck instanceof \WP_Error ) {
|
||||
return $ownerCheck;
|
||||
}
|
||||
|
||||
$label = sanitize_text_field( (string) $request->get_param( 'label' ) );
|
||||
if ( '' === $label ) {
|
||||
return $this->invalid( __( 'A question label is required.', 'unsupervised-schedular' ) );
|
||||
}
|
||||
|
||||
$fieldType = (string) ( $request->get_param( 'field_type' ) ?? Question::FIELD_TEXT );
|
||||
if ( ! in_array( $fieldType, Question::VALID_FIELD_TYPES, true ) ) {
|
||||
return $this->invalid( __( 'Invalid field type.', 'unsupervised-schedular' ) );
|
||||
}
|
||||
|
||||
$question = new Question(
|
||||
offeringId: $offeringId,
|
||||
label: $label,
|
||||
fieldType: $fieldType,
|
||||
options: $this->sanitizeOptions( $request->get_param( 'options' ) ),
|
||||
isRequired: (bool) $request->get_param( 'is_required' ),
|
||||
sortOrder: (int) $request->get_param( 'sort_order' ),
|
||||
isActive: null === $request->get_param( 'is_active' ) ? true : (bool) $request->get_param( 'is_active' ),
|
||||
);
|
||||
|
||||
$id = $this->questions->insert( $question );
|
||||
|
||||
return new \WP_REST_Response( [ 'id' => $id ], 201 );
|
||||
}
|
||||
|
||||
public function update( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
|
||||
$id = absint( $request->get_param( 'id' ) );
|
||||
$existing = $this->questions->findById( $id );
|
||||
|
||||
if ( null === $existing ) {
|
||||
return new \WP_Error( 'not_found', __( 'Question not found.', 'unsupervised-schedular' ), [ 'status' => 404 ] );
|
||||
}
|
||||
|
||||
$ownerCheck = $this->requireOfferingOwner( $existing->offeringId );
|
||||
if ( $ownerCheck instanceof \WP_Error ) {
|
||||
return $ownerCheck;
|
||||
}
|
||||
|
||||
$fieldType = $request->has_param( 'field_type' ) ? (string) $request->get_param( 'field_type' ) : $existing->fieldType;
|
||||
if ( ! in_array( $fieldType, Question::VALID_FIELD_TYPES, true ) ) {
|
||||
return $this->invalid( __( 'Invalid field type.', 'unsupervised-schedular' ) );
|
||||
}
|
||||
|
||||
$question = new Question(
|
||||
offeringId: $existing->offeringId,
|
||||
label: $request->has_param( 'label' ) ? sanitize_text_field( (string) $request->get_param( 'label' ) ) : $existing->label,
|
||||
fieldType: $fieldType,
|
||||
options: $request->has_param( 'options' ) ? $this->sanitizeOptions( $request->get_param( 'options' ) ) : $existing->options,
|
||||
isRequired: $request->has_param( 'is_required' ) ? (bool) $request->get_param( 'is_required' ) : $existing->isRequired,
|
||||
sortOrder: $request->has_param( 'sort_order' ) ? (int) $request->get_param( 'sort_order' ) : $existing->sortOrder,
|
||||
isActive: $request->has_param( 'is_active' ) ? (bool) $request->get_param( 'is_active' ) : $existing->isActive,
|
||||
id: $id,
|
||||
);
|
||||
|
||||
$this->questions->update( $id, $question );
|
||||
|
||||
return new \WP_REST_Response( $question->toArray(), 200 );
|
||||
}
|
||||
|
||||
public function delete( \WP_REST_Request $request ): \WP_REST_Response|\WP_Error {
|
||||
$id = absint( $request->get_param( 'id' ) );
|
||||
$existing = $this->questions->findById( $id );
|
||||
|
||||
if ( null === $existing ) {
|
||||
return new \WP_Error( 'not_found', __( 'Question not found.', 'unsupervised-schedular' ), [ 'status' => 404 ] );
|
||||
}
|
||||
|
||||
$ownerCheck = $this->requireOfferingOwner( $existing->offeringId );
|
||||
if ( $ownerCheck instanceof \WP_Error ) {
|
||||
return $ownerCheck;
|
||||
}
|
||||
|
||||
$this->questions->delete( $id );
|
||||
|
||||
return new \WP_REST_Response( null, 204 );
|
||||
}
|
||||
|
||||
public function canManage(): bool {
|
||||
return is_user_logged_in() && current_user_can( RoleManager::CAP_MANAGE_QUESTIONS );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the offering exists and the caller owns it (or is a studio admin).
|
||||
*/
|
||||
private function requireOfferingOwner( int $offeringId ): ?\WP_Error {
|
||||
$offering = $this->offerings->findById( $offeringId );
|
||||
|
||||
if ( null === $offering ) {
|
||||
return new \WP_Error( 'not_found', __( 'Offering not found.', 'unsupervised-schedular' ), [ 'status' => 404 ] );
|
||||
}
|
||||
|
||||
$ownsOrManagesAll = get_current_user_id() === $offering->instructorId
|
||||
|| current_user_can( RoleManager::CAP_MANAGE_INSTRUCTORS );
|
||||
|
||||
if ( ! $ownsOrManagesAll ) {
|
||||
return new \WP_Error( 'forbidden', __( 'You cannot manage questions for this offering.', 'unsupervised-schedular' ), [ 'status' => 403 ] );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalise a submitted options array into a clean list of strings.
|
||||
*
|
||||
* @return list<string>|null
|
||||
*/
|
||||
private function sanitizeOptions( mixed $value ): ?array {
|
||||
if ( ! is_array( $value ) || [] === $value ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$options = array_values(
|
||||
array_filter(
|
||||
array_map(
|
||||
static fn( $option ): string => sanitize_text_field( (string) $option ),
|
||||
$value
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
return [] === $options ? null : $options;
|
||||
}
|
||||
|
||||
private function invalid( string $message ): \WP_Error {
|
||||
return new \WP_Error( 'invalid_question', $message, [ 'status' => 400 ] );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Registration;
|
||||
|
||||
class QuestionRepository {
|
||||
|
||||
private string $table;
|
||||
|
||||
public function __construct( private \wpdb $db ) {
|
||||
$this->table = $db->prefix . 'us_questions';
|
||||
}
|
||||
|
||||
public function insert( Question $question ): int {
|
||||
$this->db->insert(
|
||||
$this->table,
|
||||
$this->columns( $question ) + [ 'created_at' => current_time( 'mysql' ) ],
|
||||
[ '%d', '%s', '%s', '%s', '%d', '%d', '%d', '%s' ]
|
||||
);
|
||||
|
||||
return $this->db->insert_id;
|
||||
}
|
||||
|
||||
public function update( int $id, Question $question ): bool {
|
||||
return false !== $this->db->update(
|
||||
$this->table,
|
||||
$this->columns( $question ),
|
||||
[ 'id' => $id ],
|
||||
[ '%d', '%s', '%s', '%s', '%d', '%d', '%d' ],
|
||||
[ '%d' ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Column values shared by insert and update (excludes created_at).
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function columns( Question $question ): array {
|
||||
return [
|
||||
'offering_id' => $question->offeringId,
|
||||
'label' => $question->label,
|
||||
'field_type' => $question->fieldType,
|
||||
'options' => null === $question->options ? null : (string) wp_json_encode( $question->options ),
|
||||
'is_required' => $question->isRequired ? 1 : 0,
|
||||
'sort_order' => $question->sortOrder,
|
||||
'is_active' => $question->isActive ? 1 : 0,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Find questions for an offering, ordered for display.
|
||||
*
|
||||
* @return list<Question>
|
||||
*/
|
||||
public function findByOffering( int $offeringId, bool $activeOnly = false ): array {
|
||||
$sql = "SELECT * FROM {$this->table} WHERE offering_id = %d";
|
||||
$params = [ $offeringId ];
|
||||
|
||||
if ( $activeOnly ) {
|
||||
$sql .= ' AND is_active = %d';
|
||||
$params[] = 1;
|
||||
}
|
||||
|
||||
$sql .= ' ORDER BY sort_order ASC, id ASC';
|
||||
|
||||
$rows = $this->db->get_results( $this->db->prepare( $sql, $params ) );
|
||||
|
||||
return array_map( Question::fromRow( ... ), $rows ?? [] );
|
||||
}
|
||||
|
||||
public function findById( int $id ): ?Question {
|
||||
$row = $this->db->get_row(
|
||||
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id )
|
||||
);
|
||||
|
||||
return $row ? Question::fromRow( $row ) : null;
|
||||
}
|
||||
|
||||
public function delete( int $id ): bool {
|
||||
return (bool) $this->db->delete(
|
||||
$this->table,
|
||||
[ 'id' => $id ],
|
||||
[ '%d' ]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,8 @@ use Unsupervised\Schedular\Booking\BookingEndpoint;
|
||||
use Unsupervised\Schedular\Booking\BookingRepository;
|
||||
use Unsupervised\Schedular\Offering\OfferingEndpoint;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
use Unsupervised\Schedular\Registration\QuestionEndpoint;
|
||||
use Unsupervised\Schedular\Registration\QuestionRepository;
|
||||
|
||||
class RestRegistrar {
|
||||
|
||||
@@ -17,11 +19,13 @@ class RestRegistrar {
|
||||
private AvailabilityEndpoint $availabilityEndpoint;
|
||||
private BookingEndpoint $bookingEndpoint;
|
||||
private OfferingEndpoint $offeringEndpoint;
|
||||
private QuestionEndpoint $questionEndpoint;
|
||||
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings ) {
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions ) {
|
||||
$this->availabilityEndpoint = new AvailabilityEndpoint( $availability );
|
||||
$this->bookingEndpoint = new BookingEndpoint( $availability, $bookings );
|
||||
$this->offeringEndpoint = new OfferingEndpoint( $offerings );
|
||||
$this->questionEndpoint = new QuestionEndpoint( $questions, $offerings );
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
@@ -32,5 +36,6 @@ class RestRegistrar {
|
||||
$this->availabilityEndpoint->registerRoutes( self::NAMESPACE );
|
||||
$this->bookingEndpoint->registerRoutes( self::NAMESPACE );
|
||||
$this->offeringEndpoint->registerRoutes( self::NAMESPACE );
|
||||
$this->questionEndpoint->registerRoutes( self::NAMESPACE );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,35 @@ class Schema {
|
||||
KEY kind (kind),
|
||||
KEY is_active (is_active)
|
||||
) {$charset};",
|
||||
|
||||
"CREATE TABLE {$prefix}us_questions (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
offering_id BIGINT UNSIGNED NOT NULL,
|
||||
label VARCHAR(255) NOT NULL,
|
||||
field_type VARCHAR(20) NOT NULL DEFAULT 'text',
|
||||
options TEXT,
|
||||
is_required TINYINT(1) NOT NULL DEFAULT 0,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
is_active TINYINT(1) NOT NULL DEFAULT 1,
|
||||
created_at DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY offering_id (offering_id),
|
||||
KEY is_active (is_active)
|
||||
) {$charset};",
|
||||
|
||||
"CREATE TABLE {$prefix}us_question_answers (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
question_id BIGINT UNSIGNED NOT NULL,
|
||||
registration_type VARCHAR(20) NOT NULL,
|
||||
registration_id BIGINT UNSIGNED NOT NULL,
|
||||
student_id BIGINT UNSIGNED NOT NULL,
|
||||
answer_value TEXT,
|
||||
created_at DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY question_id (question_id),
|
||||
KEY registration (registration_type, registration_id),
|
||||
KEY student_id (student_id)
|
||||
) {$charset};",
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user