attempt-details.php
166 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shared quiz attempt details summary view. |
| 4 | * |
| 5 | * @package Tutor\Templates |
| 6 | * @subpackage Shared |
| 7 | * @author Themeum <support@themeum.com> |
| 8 | * @link https://themeum.com |
| 9 | * @since 4.0.0 |
| 10 | */ |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use TUTOR\Icon; |
| 15 | use TUTOR\Quiz_Attempts_List; |
| 16 | use TUTOR\Input; |
| 17 | use Tutor\Components\EmptyState; |
| 18 | use Tutor\Models\QuizModel; |
| 19 | use Tutor\Components\Button; |
| 20 | use Tutor\Components\Constants\Size; |
| 21 | use Tutor\Components\Constants\Variant; |
| 22 | |
| 23 | global $tutor_current_post; |
| 24 | |
| 25 | $is_quiz_details_hidden = Quiz_Attempts_List::is_attempt_details_hidden(); |
| 26 | |
| 27 | if ( $is_quiz_details_hidden ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $quiz_id = (int) ( $quiz_id ?? ( $tutor_current_post->ID ?? 0 ) ); |
| 32 | $user_id = (int) ( $user_id ?? get_current_user_id() ); |
| 33 | $attempt_id = (int) ( $attempt_id ?? Input::get( 'attempt_id', 0, Input::TYPE_INT ) ); |
| 34 | $attempt_data = $attempt_data ?? null; |
| 35 | $back_url = $back_url ?? get_permalink( $quiz_id ); |
| 36 | $context = (string) ( $context ?? '' ); |
| 37 | $is_instructor_review = ! empty( $is_instructor_review ); |
| 38 | |
| 39 | if ( $attempt_id > 0 && ! $attempt_data ) { |
| 40 | $attempt_data = tutor_utils()->get_attempt( $attempt_id ); |
| 41 | } |
| 42 | |
| 43 | $render_attempt_not_found = static function ( string $title ) { |
| 44 | EmptyState::make() |
| 45 | ->title( $title ) |
| 46 | ->icon( tutor_utils()->get_themed_svg( 'images/illustrations/quiz-empty.svg' ) ) |
| 47 | ->render(); |
| 48 | }; |
| 49 | |
| 50 | if ( ! $attempt_data || empty( $attempt_data->attempt_id ) ) { |
| 51 | $render_attempt_not_found( __( 'Attempt not found', 'tutor' ) ); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if ( $is_instructor_review ) { |
| 56 | if ( ! tutor_utils()->can_user_manage( 'attempt', (int) $attempt_data->attempt_id ) ) { |
| 57 | $render_attempt_not_found( __( 'Attempt not found or access permission denied', 'tutor' ) ); |
| 58 | return; |
| 59 | } |
| 60 | } elseif ( $user_id > 0 && (int) $attempt_data->user_id !== $user_id ) { |
| 61 | $render_attempt_not_found( __( 'Attempt not found or access permission denied', 'tutor' ) ); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if ( ! $quiz_id ) { |
| 66 | $quiz_id = (int) ( $attempt_data->quiz_id ?? 0 ); |
| 67 | } |
| 68 | |
| 69 | $questions = QuizModel::get_quiz_answers_by_attempt_id( (int) $attempt_data->attempt_id ); |
| 70 | $questions = QuizModel::filter_attempt_answers_for_details( $questions, $is_instructor_review ); |
| 71 | |
| 72 | $course_contents = tutor_utils()->get_course_prev_next_contents_by_id( $quiz_id ); |
| 73 | ?> |
| 74 | <div class="tutor-quiz-summary-page"> |
| 75 | <div class="tutor-quiz-summary-header"> |
| 76 | <div class="tutor-quiz-summary-header-inner"> |
| 77 | <div class="tutor-flex tutor-items-center tutor-gap-4"> |
| 78 | <?php |
| 79 | Button::make() |
| 80 | ->icon( Icon::ARROW_LEFT_2 ) |
| 81 | ->flip_rtl() |
| 82 | ->tag( 'a' ) |
| 83 | ->attr( 'href', $back_url ) |
| 84 | ->variant( Variant::GHOST ) |
| 85 | ->size( Size::X_SMALL ) |
| 86 | ->icon_only() |
| 87 | ->render(); |
| 88 | ?> |
| 89 | <h5 class="tutor-h5 tutor-font-semibold"><?php esc_html_e( 'Quiz Summary', 'tutor' ); ?></h5> |
| 90 | </div> |
| 91 | |
| 92 | <?php |
| 93 | Button::make() |
| 94 | ->icon( Icon::CROSS ) |
| 95 | ->tag( 'a' ) |
| 96 | ->attr( 'href', $back_url ) |
| 97 | ->attr( 'type', 'button' ) |
| 98 | ->variant( Variant::GHOST ) |
| 99 | ->size( Size::X_SMALL ) |
| 100 | ->icon_only() |
| 101 | ->render(); |
| 102 | ?> |
| 103 | </div> |
| 104 | </div> |
| 105 | |
| 106 | <div class="tutor-surface-l1"> |
| 107 | <?php |
| 108 | tutor_load_template( |
| 109 | 'shared.components.quiz.attempt-details.summary', |
| 110 | array( |
| 111 | 'attempt_data' => $attempt_data, |
| 112 | 'answers' => $questions, // $questions holds quiz answers data, mapped to 'answers' key for summary template |
| 113 | 'is_instructor_review' => $is_instructor_review, |
| 114 | ) |
| 115 | ); |
| 116 | ?> |
| 117 | </div> |
| 118 | <?php if ( tutor_utils()->count( $questions ) ) : ?> |
| 119 | <div class="tutor-quiz-summary-body"> |
| 120 | <?php |
| 121 | tutor_load_template( |
| 122 | 'shared.components.quiz.attempt-details.questions-sidebar', |
| 123 | array( |
| 124 | 'quiz_id' => $quiz_id, |
| 125 | 'attempt_data' => $attempt_data, |
| 126 | 'questions' => $questions, |
| 127 | 'is_instructor_review' => $is_instructor_review, |
| 128 | ) |
| 129 | ); |
| 130 | ?> |
| 131 | <div class="tutor-quiz-summary-content"> |
| 132 | <h3 class="tutor-h3 tutor-sm-text-h5 tutor-text-subdued tutor-mb-10 tutor-sm-mb-5"> |
| 133 | <?php esc_html_e( 'Review your answers', 'tutor' ); ?> |
| 134 | </h3> |
| 135 | <?php |
| 136 | tutor_load_template( |
| 137 | 'shared.components.quiz.attempt-details.review-answers', |
| 138 | array( |
| 139 | 'questions' => is_array( $questions ) ? $questions : array(), |
| 140 | 'attempt_data' => $attempt_data, |
| 141 | 'back_url' => $back_url, |
| 142 | 'context' => $context, |
| 143 | 'is_instructor_review' => $is_instructor_review, |
| 144 | ) |
| 145 | ); |
| 146 | ?> |
| 147 | </div> |
| 148 | </div> |
| 149 | <?php endif; ?> |
| 150 | <?php if ( ! empty( $is_learning_area ) && $course_contents->next_id ) : ?> |
| 151 | <div class="tutor-quiz-summary-footer"> |
| 152 | <div class="tutor-quiz-summary-footer-inner"> |
| 153 | <?php |
| 154 | Button::make() |
| 155 | ->tag( 'a' ) |
| 156 | ->label( __( 'Continue Lesson', 'tutor' ) ) |
| 157 | ->variant( Variant::PRIMARY ) |
| 158 | ->size( Size::LARGE ) |
| 159 | ->attr( 'href', esc_url( get_the_permalink( $course_contents->next_id ) ) ) |
| 160 | ->render(); |
| 161 | ?> |
| 162 | </div> |
| 163 | </div> |
| 164 | <?php endif; ?> |
| 165 | </div> |
| 166 |