PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.4
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.1.6.9.4, at templates/content-quiz/js.php

150 lines 4.5 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' => array_map( 'absint', array_values( $question_ids ) ),
101 'current_question' => absint( reset( $question_ids ) ),
102 'question_nav' => '',
103 'status' => '',
104 'attempts' => array(),
105 'answered' => $answered ? (object) $answered : new stdClass(),
106 'checked_questions' => array(),
107 'passing_grade' => $quiz->get_passing_grade(),
108 'negative_marking' => $quiz->get_negative_marking(),
109 'show_correct_review' => $show_correct_review,
110 'instant_check' => $quiz->get_instant_check(),
111 'retake_count' => absint( $quiz->get_retake_count() ),
112 'retaken' => 0,
113 'questions_per_page' => $quiz->get_pagination(),
114 'page_numbers' => get_post_meta( $quiz->get_id(), '_lp_pagination_numbers', true ) === 'yes',
115 'review_questions' => $quiz->get_review_questions(),
116 'support_options' => learn_press_get_question_support_answer_options(),
117 'duration' => $duration ? $duration->get() : false,
118 'crypto' => $crypto_js_aes,
119 'edit_permalink' => $editable ? get_edit_post_link( $quiz->get_id() ) : '',
120 'results' => array(),
121 'required_password' => post_password_required( $quiz->get_id() ),
122 'allow_retake' => $quiz->get_retake_count() == -1,
123 );
124
125 $js = array_merge( $js, $user_js );
126
127 if ( $total_question ) :
128 ?>
129 <div id="learn-press-quiz-app"></div>
130
131 <script>
132 document.addEventListener( 'DOMContentLoaded', () => {
133 if ( typeof LP !== 'undefined' ) {
134 LP.Hook.addAction('course-ready', () => {
135 LP.quiz.init(
136 '#learn-press-quiz-app',
137 <?php echo json_encode( $js ); ?>
138 )
139 });
140 }
141 });
142 </script>
143
144 <?php
145 else :
146 esc_html_e( 'You haven\'t any question!', 'learnpress' );
147 ?>
148
149 <?php endif; ?>
150