PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9
4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 All 140 releases
← All changes | templates/content-quiz/js.php +116 -79 4.1.6.9 → 4.4.9 View file →
@@ -1,75 +1,103 @@
1 1 <?php
2 2 /**
3 3 * Template for printing js code used for Quiz.
4 + * Call from hook 'learn-press/content-item-summary/lp_quiz'
4 5 *
5 6 * @author ThimPress
6 7 * @package LearnPress/Templates
7 - * @version 4.0.0
8 + * @version 4.0.2
8 9 */
9 10
11 +use LearnPress\Models\CourseModel;
12 +use LearnPress\Models\QuizPostModel;
13 +use LearnPress\Models\UserItems\UserQuizModel;
14 +use LearnPress\Models\UserModel;
15 +
10 16 defined( 'ABSPATH' ) || exit;
11 17
12 -$user = learn_press_get_current_user();
13 -$course = learn_press_get_course();
14 -if ( ! $course ) {
18 +global $lpCourseModel;
19 +$courseModel = $lpCourseModel;
20 +if ( ! $courseModel instanceof CourseModel ) {
15 21 return;
16 22 }
17 23
24 +$userModel = UserModel::find( get_current_user_id(), true );
25 +
18 26 $quiz = LP_Global::course_item_quiz();
19 27 if ( ! $quiz ) {
20 28 return;
21 29 }
22 30
23 -$total_question = $quiz->count_questions();
31 +$quizPostModel = QuizPostModel::find( $quiz->get_id(), true );
32 +if ( ! $quizPostModel instanceof QuizPostModel ) {
33 + return;
34 +}
35 +
36 +$total_question = $quizPostModel->count_questions();
24 37 $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();
38 +$show_check = $quizPostModel->has_instant_check();
39 +$show_correct_review = $quizPostModel->has_show_correct_review();
40 +$question_ids = $quizPostModel->get_question_ids();
28 41 $user_js = array();
29 42
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 43 $answered = array();
37 44 $status = '';
38 45 $checked_questions = array();
39 46
40 47 $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() );
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() );
43 51 $quiz_results = null;
52 +$userQuizModel = null;
44 53
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 - }
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 + );
50 63
51 - if ( ! $quiz_results ) {
52 - $quiz_results = $user_quiz->get_result();
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'];
53 84 }
85 +}
54 86
55 - $checked_questions = $user_quiz->get_checked_questions();
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 );
56 94
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 - );
95 + echo $quizPostModel->get_the_content();
64 96
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 - }
97 + $post = $original_post;
98 + setup_postdata( $post );
99 + echo '</div>';
72 100 }
73 101
74 102 $questions = learn_press_rest_prepare_user_questions(
75 103 $question_ids,
@@ -85,60 +113,69 @@
85 113
86 114 $duration = $quiz->get_duration();
87 115
88 116 $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,
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,
118 147 );
119 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 +
120 159 $js = array_merge( $js, $user_js );
121 160
122 -if ( $total_question ) :
161 +// To show data debug.
162 +LP_Helper::print_inline_script_tag( 'lp_quiz_js_data', [ 'data' => $js ] );
163 +
164 +if ( $total_question ) {
123 165 ?>
124 166 <div id="learn-press-quiz-app"></div>
125 167
126 168 <script>
127 - document.addEventListener( 'DOMContentLoaded', () => {
128 - if ( typeof LP !== 'undefined' ) {
129 - LP.Hook.addAction('course-ready', () => {
169 + document.addEventListener('DOMContentLoaded', () => {
170 + if (typeof LP !== 'undefined') {
130 171 LP.quiz.init(
131 172 '#learn-press-quiz-app',
132 173 <?php echo json_encode( $js ); ?>
133 174 )
134 - });
135 - }
136 - });
175 + }
176 + });
137 177 </script>
138 -
139 178 <?php
140 -else :
179 +} else {
141 180 esc_html_e( 'You haven\'t any question!', 'learnpress' );
142 - ?>
143 -
144 -<?php endif; ?>
181 +}