questions
3 days ago
question-header.php
3 days ago
question.php
1 month ago
questions-sidebar.php
3 days ago
review-answers.php
1 month ago
summary.php
1 month ago
question-header.php
170 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Attempt details question header. |
| 4 | * |
| 5 | * @package Tutor\Templates |
| 6 | * @subpackage LearningArea\Quiz\AttemptDetails |
| 7 | * @since 4.0.0 |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use TUTOR\Quiz; |
| 13 | use TUTOR\Icon; |
| 14 | use Tutor\Components\Badge; |
| 15 | use Tutor\Components\SvgIcon; |
| 16 | |
| 17 | /** |
| 18 | * Build Alpine.js attribute expressions for a reactive review-status badge. |
| 19 | * |
| 20 | * @param string $review_field_name The form field name, e.g. "review_statuses[42]". |
| 21 | * |
| 22 | * @return array{ x_text: string, class_expr: string } |
| 23 | */ |
| 24 | $build_badge_attrs = function ( string $review_field_name ): array { |
| 25 | $label_map = wp_json_encode( |
| 26 | array( |
| 27 | 'pending' => __( 'Pending', 'tutor' ), |
| 28 | 'correct' => __( 'Correct', 'tutor' ), |
| 29 | 'incorrect' => __( 'Incorrect', 'tutor' ), |
| 30 | ) |
| 31 | ); |
| 32 | |
| 33 | $variant_map = wp_json_encode( |
| 34 | array( |
| 35 | 'pending' => Badge::WARNING, |
| 36 | 'correct' => Badge::SUCCESS, |
| 37 | 'incorrect' => Badge::ERROR, |
| 38 | ) |
| 39 | ); |
| 40 | |
| 41 | $field = esc_attr( $review_field_name ); |
| 42 | |
| 43 | return array( |
| 44 | 'x_text' => "({$label_map})[watch('{$field}')] ?? ''", |
| 45 | 'class_expr' => "'tutor-badge tutor-badge-rounded tutor-badge-' + (({$variant_map})[watch('{$field}')] ?? 'info')", |
| 46 | ); |
| 47 | }; |
| 48 | |
| 49 | $index = (int) ( $index ?? 1 ); |
| 50 | $question_title = (string) ( $question_title ?? '' ); |
| 51 | $question_description = (string) ( $question_description ?? '' ); |
| 52 | $status_badges = isset( $status_badges ) && is_array( $status_badges ) ? $status_badges : array(); |
| 53 | $question = isset( $question ) && is_object( $question ) ? $question : null; |
| 54 | $answer_status = (string) ( $answer_status ?? '' ); |
| 55 | $attempt_id = (int) ( $attempt_id ?? 0 ); |
| 56 | $attempt_answer_id = (int) ( $attempt_answer_id ?? 0 ); |
| 57 | $is_instructor_review = ! empty( $is_instructor_review ); |
| 58 | $review_field_name = (string) ( $review_field_name ?? '' ); |
| 59 | ?> |
| 60 | |
| 61 | <div class="tutor-quiz-question-header"> |
| 62 | <div class="tutor-quiz-question-number"> |
| 63 | <?php echo esc_html( $index ); ?> |
| 64 | </div> |
| 65 | |
| 66 | <div class="tutor-quiz-question-title"> |
| 67 | <?php echo esc_html( Quiz::sanitize_quiz_content( $question_title ?? '' ) ); ?> |
| 68 | |
| 69 | <?php if ( ! empty( $question_description ) ) : ?> |
| 70 | <?php |
| 71 | $description = apply_filters( 'tutor_filter_quiz_question_description', wp_unslash( (string) ( $question_description ?? '' ) ) ); |
| 72 | if ( $description ) { |
| 73 | $markup = "<div class='tutor-p2 tutor-text-secondary'>{$description}</div>"; |
| 74 | if ( function_exists( 'tutor' ) && tutor()->has_pro ) { |
| 75 | do_action( 'tutor_quiz_question_desc_render', $markup, $question ); |
| 76 | } else { |
| 77 | echo wp_kses_post( $markup ); |
| 78 | } |
| 79 | } |
| 80 | ?> |
| 81 | <?php endif; ?> |
| 82 | </div> |
| 83 | |
| 84 | <?php if ( ! empty( $status_badges ) || ( $is_instructor_review && $attempt_id ) ) : ?> |
| 85 | <div class="tutor-quiz-question-header-actions"> |
| 86 | <?php if ( ! empty( $status_badges ) ) : ?> |
| 87 | <div class="tutor-quiz-question-header-status"> |
| 88 | <?php foreach ( $status_badges as $badge ) : ?> |
| 89 | <?php |
| 90 | $badge_status = (string) ( $badge['status'] ?? '' ); |
| 91 | |
| 92 | if ( $badge_status && $is_instructor_review ) : |
| 93 | $badge_attrs = $build_badge_attrs( $review_field_name ); |
| 94 | |
| 95 | Badge::make() |
| 96 | ->rounded() |
| 97 | ->attr( 'x-text', $badge_attrs['x_text'] ) |
| 98 | ->attr( ':class', $badge_attrs['class_expr'] ) |
| 99 | ->render(); |
| 100 | else : |
| 101 | $badge_label = (string) ( $badge['label'] ?? '' ); |
| 102 | $badge_variant = (string) ( $badge['variant'] ?? '' ); |
| 103 | |
| 104 | if ( '' === $badge_label || '' === $badge_variant ) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | Badge::make() |
| 109 | ->label( $badge_label ) |
| 110 | ->variant( $badge_variant ) |
| 111 | ->rounded() |
| 112 | ->render(); |
| 113 | endif; |
| 114 | ?> |
| 115 | <?php endforeach; ?> |
| 116 | </div> |
| 117 | <?php endif; ?> |
| 118 | |
| 119 | <?php if ( $is_instructor_review && $attempt_id && $review_field_name ) : ?> |
| 120 | <div class="tutor-quiz-question-header-divider" aria-hidden="true"></div> |
| 121 | |
| 122 | <div class="tutor-quiz-question-review-actions"> |
| 123 | <input |
| 124 | type="hidden" |
| 125 | name="<?php echo esc_attr( $review_field_name ); ?>" |
| 126 | value="<?php echo esc_attr( $answer_status ); ?>" |
| 127 | x-bind="register('<?php echo esc_attr( $review_field_name ); ?>')" |
| 128 | /> |
| 129 | |
| 130 | <label |
| 131 | class="tutor-quiz-question-review-action" |
| 132 | data-review-status="correct" |
| 133 | title="<?php esc_attr_e( 'Mark as correct', 'tutor' ); ?>" |
| 134 | @click="setValue('<?php echo esc_attr( $review_field_name ); ?>', 'correct', { shouldDirty: true })" |
| 135 | > |
| 136 | <input |
| 137 | class="tutor-quiz-question-review-input" |
| 138 | type="radio" |
| 139 | name="<?php echo esc_attr( $review_field_name ); ?>" |
| 140 | value="correct" |
| 141 | :checked="watch('<?php echo esc_attr( $review_field_name ); ?>') === 'correct'" |
| 142 | tabindex="-1" |
| 143 | aria-hidden="true" |
| 144 | /> |
| 145 | <?php SvgIcon::make()->name( Icon::CHECK_2 )->size( 20 )->render(); ?> |
| 146 | </label> |
| 147 | |
| 148 | <label |
| 149 | class="tutor-quiz-question-review-action" |
| 150 | data-review-status="incorrect" |
| 151 | title="<?php esc_attr_e( 'Mark as incorrect', 'tutor' ); ?>" |
| 152 | @click="setValue('<?php echo esc_attr( $review_field_name ); ?>', 'incorrect', { shouldDirty: true })" |
| 153 | > |
| 154 | <input |
| 155 | class="tutor-quiz-question-review-input" |
| 156 | type="radio" |
| 157 | name="<?php echo esc_attr( $review_field_name ); ?>" |
| 158 | value="incorrect" |
| 159 | :checked="watch('<?php echo esc_attr( $review_field_name ); ?>') === 'incorrect'" |
| 160 | tabindex="-1" |
| 161 | aria-hidden="true" |
| 162 | /> |
| 163 | <?php SvgIcon::make()->name( Icon::CROSS )->size( 20 )->render(); ?> |
| 164 | </label> |
| 165 | </div> |
| 166 | <?php endif; ?> |
| 167 | </div> |
| 168 | <?php endif; ?> |
| 169 | </div> |
| 170 |