| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template for printing js code used for Quiz. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Templates |
| 7 |
* @version 4.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
defined( 'ABSPATH' ) || exit; |
| 11 |
|
| 12 |
$user = learn_press_get_current_user(); |
| 13 |
$course = learn_press_get_course(); |
| 14 |
if ( ! $course ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
$quiz = LP_Global::course_item_quiz(); |
| 19 |
if ( ! $quiz ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$total_question = $quiz->count_questions(); |
| 24 |
$questions = array(); |
| 25 |
$show_check = $quiz->get_instant_check(); |
| 26 |
$show_correct_review = $quiz->get_show_correct_review(); |
| 27 |
$question_ids = $quiz->get_question_ids(); |
| 28 |
$user_js = array(); |
| 29 |
|
| 30 |
|
| 31 |
$user_course = $user->get_course_data( $course->get_id() ); |
| 32 |
/** |
| 33 |
* @var LP_User_Item_Quiz $user_quiz |
| 34 |
*/ |
| 35 |
$user_quiz = $user_course ? $user_course->get_item( $quiz->get_id() ) : false; |
| 36 |
$answered = array(); |
| 37 |
$status = ''; |
| 38 |
$checked_questions = array(); |
| 39 |
|
| 40 |
$crypto_js_aes = false; |
| 41 |
$editable = $user->is_admin() || get_post_field( $user->is_author_of( $course->get_id() ) ); |
| 42 |
$max_retrying = learn_press_get_quiz_max_retrying( $quiz->get_id(), $course->get_id() ); |
| 43 |
$quiz_results = null; |
| 44 |
|
| 45 |
if ( $user_quiz ) { |
| 46 |
$status = $user_quiz->get_status(); |
| 47 |
if ( LP_ITEM_STARTED === $status ) { |
| 48 |
$quiz_results = LP_User_Items_Result_DB::instance()->get_result( $user_quiz->get_user_item_id() ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! $quiz_results ) { |
| 52 |
$quiz_results = $user_quiz->get_result(); |
| 53 |
} |
| 54 |
|
| 55 |
$checked_questions = $user_quiz->get_checked_questions(); |
| 56 |
|
| 57 |
$user_js = array( |
| 58 |
'status' => $status, |
| 59 |
'attempts' => $user_quiz->get_attempts(), |
| 60 |
'checked_questions' => $checked_questions, |
| 61 |
'start_time' => $user_quiz->get_start_time()->toSql( false ), |
| 62 |
'retaken' => absint( $user_quiz->get_retaken_count() ), |
| 63 |
); |
| 64 |
|
| 65 |
$time_remaining = $user_quiz->get_timestamp_remaining(); |
| 66 |
$user_js['total_time'] = $time_remaining; |
| 67 |
|
| 68 |
if ( $quiz_results ) { |
| 69 |
$user_js['results'] = $quiz_results; |
| 70 |
$answered = $quiz_results['questions']; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$questions = learn_press_rest_prepare_user_questions( |
| 75 |
$question_ids, |
| 76 |
array( |
| 77 |
'instant_check' => $show_check, |
| 78 |
'quiz_status' => $status, |
| 79 |
'checked_questions' => $checked_questions, |
| 80 |
'answered' => $answered, |
| 81 |
'show_correct_review' => $show_correct_review, |
| 82 |
'status' => $status, |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
$duration = $quiz->get_duration(); |
| 87 |
|
| 88 |
$js = array( |
| 89 |
'course_id' => $course->get_id(), |
| 90 |
'nonce' => wp_create_nonce( sprintf( 'user-quiz-%d', get_current_user_id() ) ), |
| 91 |
'id' => $quiz->get_id(), |
| 92 |
'title' => $quiz->get_title(), |
| 93 |
'content' => $quiz->get_content(), |
| 94 |
'questions' => $questions, |
| 95 |
'question_ids' => array_map( 'absint', array_values( $question_ids ) ), |
| 96 |
'current_question' => absint( reset( $question_ids ) ), |
| 97 |
'question_nav' => '', |
| 98 |
'status' => '', |
| 99 |
'attempts' => array(), |
| 100 |
'answered' => $answered ? (object) $answered : new stdClass(), |
| 101 |
'checked_questions' => array(), |
| 102 |
'passing_grade' => $quiz->get_passing_grade(), |
| 103 |
'negative_marking' => $quiz->get_negative_marking(), |
| 104 |
'show_correct_review' => $show_correct_review, |
| 105 |
'instant_check' => $quiz->get_instant_check(), |
| 106 |
'retake_count' => absint( $quiz->get_retake_count() ), |
| 107 |
'retaken' => 0, |
| 108 |
'questions_per_page' => $quiz->get_pagination(), |
| 109 |
'page_numbers' => get_post_meta( $quiz->get_id(), '_lp_pagination_numbers', true ) === 'yes', |
| 110 |
'review_questions' => $quiz->get_review_questions(), |
| 111 |
'support_options' => learn_press_get_question_support_answer_options(), |
| 112 |
'duration' => $duration ? $duration->get() : false, |
| 113 |
'crypto' => $crypto_js_aes, |
| 114 |
'edit_permalink' => $editable ? get_edit_post_link( $quiz->get_id() ) : '', |
| 115 |
'results' => array(), |
| 116 |
'required_password' => post_password_required( $quiz->get_id() ), |
| 117 |
'allow_retake' => $quiz->get_retake_count() == -1, |
| 118 |
); |
| 119 |
|
| 120 |
$js = array_merge( $js, $user_js ); |
| 121 |
|
| 122 |
if ( $total_question ) : |
| 123 |
?> |
| 124 |
<div id="learn-press-quiz-app"></div> |
| 125 |
|
| 126 |
<script> |
| 127 |
document.addEventListener( 'DOMContentLoaded', () => { |
| 128 |
if ( typeof LP !== 'undefined' ) { |
| 129 |
LP.Hook.addAction('course-ready', () => { |
| 130 |
LP.quiz.init( |
| 131 |
'#learn-press-quiz-app', |
| 132 |
<?php echo json_encode( $js ); ?> |
| 133 |
) |
| 134 |
}); |
| 135 |
} |
| 136 |
}); |
| 137 |
</script> |
| 138 |
|
| 139 |
<?php |
| 140 |
else : |
| 141 |
esc_html_e( 'You haven\'t any question!', 'learnpress' ); |
| 142 |
?> |
| 143 |
|
| 144 |
<?php endif; ?> |
| 145 |
|