quiz-reviews.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Frontend Student's Quiz Review |
| 4 | * |
| 5 | * @package Tutor\Templates |
| 6 | * @subpackage Dashboard\Quiz_Attempts |
| 7 | * @author Themeum <support@themeum.com> |
| 8 | * @link https://themeum.com |
| 9 | * @since 1.4.0 |
| 10 | */ |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use TUTOR\Input; |
| 15 | use Tutor\Components\EmptyState; |
| 16 | use Tutor\Models\QuizModel; |
| 17 | |
| 18 | $attempt_id = Input::get( 'attempt_id', 0, Input::TYPE_INT ); |
| 19 | $attempt_data = tutor_utils()->get_attempt( $attempt_id ); |
| 20 | $user_id = tutor_utils()->avalue_dot( 'user_id', $attempt_data ); |
| 21 | $quiz_id = (int) tutor_utils()->avalue_dot( 'quiz_id', $attempt_data ); |
| 22 | $back_url = remove_query_arg( 'attempt_id' ); |
| 23 | $attempt_info = isset( $attempt_data->attempt_info ) ? maybe_unserialize( $attempt_data->attempt_info ) : array(); |
| 24 | $can_review = $attempt_id > 0 && tutor_utils()->can_user_manage( 'attempt', $attempt_id ); |
| 25 | |
| 26 | if ( ! $attempt_data || ! $can_review ) { |
| 27 | EmptyState::make() |
| 28 | ->title( __( 'Attempt not found or access permission denied', 'tutor' ) ) |
| 29 | ->icon( tutor_utils()->get_themed_svg( 'images/illustrations/quiz-empty.svg' ) ) |
| 30 | ->render(); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | $form_id = 'quiz-attempt-review-form'; |
| 35 | $form_default_values = array( |
| 36 | 'feedback' => tutor_utils()->count( $attempt_info ) && isset( $attempt_info['instructor_feedback'] ) ? $attempt_info['instructor_feedback'] : '', |
| 37 | ); |
| 38 | |
| 39 | $attempt_answers_map = array(); |
| 40 | $questions = QuizModel::get_quiz_answers_by_attempt_id( $attempt_id ); |
| 41 | |
| 42 | if ( is_array( $questions ) ) { |
| 43 | foreach ( $questions as $question ) { |
| 44 | $question_id = (int) ( $question->question_id ?? 0 ); |
| 45 | |
| 46 | if ( $question_id > 0 ) { |
| 47 | $attempt_answers_map[ $question_id ] = $question; |
| 48 | $answer_status = QuizModel::get_attempt_answer_status( $question ); |
| 49 | $form_default_values[ "review_statuses[{$question_id}]" ] = $answer_status; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | ?> |
| 54 | |
| 55 | <div class="wrap"> |
| 56 | |
| 57 | <?php if ( ! is_admin() ) : ?> |
| 58 | <form |
| 59 | id="<?php echo esc_attr( $form_id ); ?>" |
| 60 | x-data='(() => { |
| 61 | const form = tutorForm({ |
| 62 | id: "<?php echo esc_attr( $form_id ); ?>", |
| 63 | mode: "onSubmit", |
| 64 | defaultValues: <?php echo wp_json_encode( $form_default_values, JSON_HEX_APOS ); ?> |
| 65 | }); |
| 66 | const feedback = tutorQuizAttemptFeedback({ |
| 67 | attemptId: <?php echo esc_attr( $attempt_id ); ?>, |
| 68 | formId: "<?php echo esc_attr( $form_id ); ?>" |
| 69 | }); |
| 70 | |
| 71 | return { |
| 72 | ...form, |
| 73 | ...feedback, |
| 74 | init() { |
| 75 | form.init?.call(this); |
| 76 | feedback.init?.call(this); |
| 77 | }, |
| 78 | }; |
| 79 | })()' |
| 80 | x-bind="getFormBindings()" |
| 81 | @submit.prevent="handleSubmit((data) => handleSaveFeedback(data))($event)" |
| 82 | > |
| 83 | <?php endif; ?> |
| 84 | |
| 85 | <div class="tutor-quiz-attempt-details-wrapper "> |
| 86 | <?php |
| 87 | if ( is_admin() ) { |
| 88 | tutor_load_template_from_custom_path( |
| 89 | tutor()->path . '/views/quiz/attempt-details.php', |
| 90 | array( |
| 91 | 'attempt_id' => $attempt_id, |
| 92 | 'attempt_data' => $attempt_data, |
| 93 | 'user_id' => (int) $user_id, |
| 94 | 'context' => 'frontend-dashboard-students-attempts', |
| 95 | ) |
| 96 | ); |
| 97 | } else { |
| 98 | tutor_load_template( |
| 99 | 'shared.components.quiz.attempt-details', |
| 100 | array( |
| 101 | 'attempt_id' => $attempt_id, |
| 102 | 'attempt_data' => $attempt_data, |
| 103 | 'quiz_id' => $quiz_id, |
| 104 | 'user_id' => (int) $user_id, |
| 105 | 'back_url' => $back_url, |
| 106 | 'is_instructor_review' => true, |
| 107 | 'context' => 'frontend-dashboard-students-attempts', |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | ?> |
| 112 | </div> |
| 113 | |
| 114 | <?php |
| 115 | if ( is_admin() ) { |
| 116 | tutor_load_template_from_custom_path( |
| 117 | tutor()->path . 'views/quiz/instructor-feedback.php', |
| 118 | array( 'attempt_data' => $attempt_data ) |
| 119 | ); |
| 120 | } else { |
| 121 | tutor_load_template( |
| 122 | 'dashboard.quiz-attempts.quiz-attempts-feedback', |
| 123 | array( 'attempt_data' => $attempt_data ) |
| 124 | ); |
| 125 | } |
| 126 | ?> |
| 127 | |
| 128 | <?php if ( ! is_admin() ) : ?> |
| 129 | </form> |
| 130 | <?php endif; ?> |
| 131 | </div> |
| 132 |