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

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 <[email protected]>
This commit is contained in:
2026-06-05 11:11:06 -03:00
co-authored by Claude Opus 4.8
parent 5b6cc4e89b
commit e61d99daed
15 changed files with 1141 additions and 4 deletions
+111
View File
@@ -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;
}
}