| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template for printing js code used for Quiz. |
| 4 |
* Call from hook 'learn-press/content-item-summary/lp_quiz' |
| 5 |
* |
| 6 |
* @author ThimPress |
| 7 |
* @package LearnPress/Templates |
| 8 |
* @version 4.0.2 |
| 9 |
*/ |
| 10 |
|
| 11 |
use LearnPress\Models\CourseModel; |
| 12 |
use LearnPress\Models\QuizPostModel; |
| 13 |
use LearnPress\Models\UserItems\UserQuizModel; |
| 14 |
use LearnPress\Models\UserModel; |
| 15 |
|
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
global $lpCourseModel; |
| 19 |
$courseModel = $lpCourseModel; |
| 20 |
if ( ! $courseModel instanceof CourseModel ) { |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$userModel = UserModel::find( get_current_user_id(), true ); |
| 25 |
|
| 26 |
$quiz = LP_Global::course_item_quiz(); |
| 27 |
if ( ! $quiz ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
$quizPostModel = QuizPostModel::find( $quiz->get_id(), true ); |
| 32 |
if ( ! $quizPostModel instanceof QuizPostModel ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$total_question = $quizPostModel->count_questions(); |
| 37 |
$questions = array(); |
| 38 |
$show_check = $quizPostModel->has_instant_check(); |
| 39 |
$show_correct_review = $quizPostModel->has_show_correct_review(); |
| 40 |
$question_ids = $quizPostModel->get_question_ids(); |
| 41 |
$user_js = array(); |
| 42 |
|
| 43 |
$answered = array(); |
| 44 |
$status = ''; |
| 45 |
$checked_questions = array(); |
| 46 |
|
| 47 |
$crypto_js_aes = false; |
| 48 |
$user = learn_press_get_current_user(); |
| 49 |
$editable = $user->is_admin() || get_post_field( $user->is_author_of( $courseModel->get_id() ) ); |
| 50 |
$max_retrying = learn_press_get_quiz_max_retrying( $quiz->get_id(), $courseModel->get_id() ); |
| 51 |
$quiz_results = null; |
| 52 |
$userQuizModel = null; |
| 53 |
|
| 54 |
if ( $userModel ) { |
| 55 |
$userQuizModel = UserQuizModel::find_user_item( |
| 56 |
$userModel->get_id(), |
| 57 |
$quiz->get_id(), |
| 58 |
LP_QUIZ_CPT, |
| 59 |
$courseModel->get_id(), |
| 60 |
LP_COURSE_CPT, |
| 61 |
true |
| 62 |
); |
| 63 |
|
| 64 |
if ( $userQuizModel instanceof UserQuizModel ) { |
| 65 |
$status = $userQuizModel->get_status(); |
| 66 |
$quiz_results = $userQuizModel->get_result(); |
| 67 |
$checked_questions = $userQuizModel->get_checked_questions(); |
| 68 |
|
| 69 |
$start_time_obj = new LP_Datetime( $userQuizModel->get_start_time() ); |
| 70 |
$start_time_timestamp = $start_time_obj->getTimestamp(); |
| 71 |
|
| 72 |
$user_js = array( |
| 73 |
'status' => $status, |
| 74 |
'attempts' => $userQuizModel->get_history(), |
| 75 |
'checked_questions' => $checked_questions, |
| 76 |
'start_time' => $userQuizModel->get_start_time(), |
| 77 |
'time_spend' => time() - $start_time_timestamp, |
| 78 |
'retaken' => $userQuizModel->get_retaken_count(), |
| 79 |
'total_time' => $userQuizModel->get_time_remaining(), |
| 80 |
'results' => $quiz_results, |
| 81 |
); |
| 82 |
|
| 83 |
$answered = $quiz_results['questions']; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! $userQuizModel ) { |
| 88 |
// Display quiz content. |
| 89 |
echo '<div class="quiz-content">'; |
| 90 |
global $post; |
| 91 |
$original_post = $post; |
| 92 |
$post = get_post( $quizPostModel->get_id() ); |
| 93 |
setup_postdata( $post ); |
| 94 |
|
| 95 |
echo $quizPostModel->get_the_content(); |
| 96 |
|
| 97 |
$post = $original_post; |
| 98 |
setup_postdata( $post ); |
| 99 |
echo '</div>'; |
| 100 |
} |
| 101 |
|
| 102 |
$questions = learn_press_rest_prepare_user_questions( |
| 103 |
$question_ids, |
| 104 |
array( |
| 105 |
'instant_check' => $show_check, |
| 106 |
'quiz_status' => $status, |
| 107 |
'checked_questions' => $checked_questions, |
| 108 |
'answered' => $answered, |
| 109 |
'show_correct_review' => $show_correct_review, |
| 110 |
'status' => $status, |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
$duration = $quiz->get_duration(); |
| 115 |
|
| 116 |
$js = array( |
| 117 |
'course_id' => $courseModel->get_id(), |
| 118 |
'nonce' => wp_create_nonce( sprintf( 'user-quiz-%d', get_current_user_id() ) ), |
| 119 |
'id' => $quizPostModel->get_id(), |
| 120 |
'title' => $quizPostModel->get_the_title(), |
| 121 |
'content' => '', |
| 122 |
'questions' => $questions, |
| 123 |
'question_ids' => $question_ids, |
| 124 |
'number_questions_to_do' => $quiz->get_number_questions_to_do(), |
| 125 |
'current_question' => absint( reset( $question_ids ) ), |
| 126 |
'question_nav' => '', |
| 127 |
'status' => '', |
| 128 |
'attempts' => array(), |
| 129 |
'answered' => $answered ? (object) $answered : new stdClass(), |
| 130 |
'checked_questions' => array(), |
| 131 |
'passing_grade' => $quizPostModel->get_passing_grade(), |
| 132 |
'negative_marking' => $quizPostModel->has_negative_marking(), |
| 133 |
'show_correct_review' => $show_correct_review, |
| 134 |
'instant_check' => $quizPostModel->has_instant_check(), |
| 135 |
'retake_count' => absint( $quizPostModel->get_retake_count() ), |
| 136 |
'retaken' => 0, |
| 137 |
'questions_per_page' => $quiz->get_pagination(), |
| 138 |
'page_numbers' => get_post_meta( $quiz->get_id(), '_lp_pagination_numbers', true ) === 'yes', |
| 139 |
'review_questions' => $quizPostModel->get_meta_value_by_key( QuizPostModel::META_KEY_REVIEW, 'yes' ) === 'yes', |
| 140 |
'support_options' => learn_press_get_question_support_answer_options(), |
| 141 |
'duration' => $duration ? $duration->get() : false, |
| 142 |
'crypto' => $crypto_js_aes, |
| 143 |
'edit_permalink' => $editable ? $quizPostModel->get_edit_link() : '', |
| 144 |
'results' => array(), |
| 145 |
'required_password' => post_password_required( $quiz->get_id() ), |
| 146 |
'allow_retake' => $quizPostModel->get_retake_count() == - 1, |
| 147 |
); |
| 148 |
|
| 149 |
global $post; |
| 150 |
$original_post = $post; |
| 151 |
$post = get_post( $quizPostModel->get_id() ); |
| 152 |
setup_postdata( $post ); |
| 153 |
|
| 154 |
$js['quiz_description'] = $quizPostModel->get_the_content(); |
| 155 |
|
| 156 |
$post = $original_post; |
| 157 |
setup_postdata( $post ); |
| 158 |
|
| 159 |
$js = array_merge( $js, $user_js ); |
| 160 |
|
| 161 |
// To show data debug. |
| 162 |
LP_Helper::print_inline_script_tag( 'lp_quiz_js_data', [ 'data' => $js ] ); |
| 163 |
|
| 164 |
if ( $total_question ) { |
| 165 |
?> |
| 166 |
<div id="learn-press-quiz-app"></div> |
| 167 |
|
| 168 |
<script> |
| 169 |
document.addEventListener('DOMContentLoaded', () => { |
| 170 |
if (typeof LP !== 'undefined') { |
| 171 |
LP.quiz.init( |
| 172 |
'#learn-press-quiz-app', |
| 173 |
<?php echo json_encode( $js ); ?> |
| 174 |
) |
| 175 |
} |
| 176 |
}); |
| 177 |
</script> |
| 178 |
<?php |
| 179 |
} else { |
| 180 |
esc_html_e( 'You haven\'t any question!', 'learnpress' ); |
| 181 |
} |
| 182 |
|