PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.0
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.0
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 4.2.0 All 139 releases
learnpress / templates / content-quiz / js.php

js.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.0, at templates/content-quiz/js.php

151 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 } else {
73 // Display quiz content.
74 echo '<div class="quiz-content">';
75 learn_press_echo_vuejs_write_on_php( $quiz->get_content() );
76 echo '</div>';
77 }
78
79 $questions = learn_press_rest_prepare_user_questions(
80 $question_ids,
81 array(
82 'instant_check' => $show_check,
83 'quiz_status' => $status,
84 'checked_questions' => $checked_questions,
85 'answered' => $answered,
86 'show_correct_review' => $show_correct_review,
87 'status' => $status,
88 )
89 );
90
91 $duration = $quiz->get_duration();
92
93 $js = array(
94 'course_id' => $course->get_id(),
95 'nonce' => wp_create_nonce( sprintf( 'user-quiz-%d', get_current_user_id() ) ),
96 'id' => $quiz->get_id(),
97 'title' => $quiz->get_title(),
98 'content' => '',
99 'questions' => $questions,
100 'question_ids' => $question_ids,
101 'number_questions_to_do' => $quiz->get_number_questions_to_do(),
102 'current_question' => absint( reset( $question_ids ) ),
103 'question_nav' => '',
104 'status' => '',
105 'attempts' => array(),
106 'answered' => $answered ? (object) $answered : new stdClass(),
107 'checked_questions' => array(),
108 'passing_grade' => $quiz->get_passing_grade(),
109 'negative_marking' => $quiz->get_negative_marking(),
110 'show_correct_review' => $show_correct_review,
111 'instant_check' => $quiz->get_instant_check(),
112 'retake_count' => absint( $quiz->get_retake_count() ),
113 'retaken' => 0,
114 'questions_per_page' => $quiz->get_pagination(),
115 'page_numbers' => get_post_meta( $quiz->get_id(), '_lp_pagination_numbers', true ) === 'yes',
116 'review_questions' => $quiz->get_review_questions(),
117 'support_options' => learn_press_get_question_support_answer_options(),
118 'duration' => $duration ? $duration->get() : false,
119 'crypto' => $crypto_js_aes,
120 'edit_permalink' => $editable ? get_edit_post_link( $quiz->get_id() ) : '',
121 'results' => array(),
122 'required_password' => post_password_required( $quiz->get_id() ),
123 'allow_retake' => $quiz->get_retake_count() == -1,
124 );
125
126 $js = array_merge( $js, $user_js );
127
128 if ( $total_question ) :
129 ?>
130 <div id="learn-press-quiz-app"></div>
131
132 <script>
133 document.addEventListener( 'DOMContentLoaded', () => {
134 if ( typeof LP !== 'undefined' ) {
135 LP.Hook.addAction('course-ready', () => {
136 LP.quiz.init(
137 '#learn-press-quiz-app',
138 <?php echo json_encode( $js ); ?>
139 )
140 });
141 }
142 });
143 </script>
144
145 <?php
146 else :
147 esc_html_e( 'You haven\'t any question!', 'learnpress' );
148 ?>
149
150 <?php endif; ?>
151