PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.2
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.2
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 / inc / quiz / lp-quiz-functions.php

lp-quiz-functions.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.2, at inc/quiz/lp-quiz-functions.php

346 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Common functions to manipulate with the quiz.
4 *
5 * @since 3.0.0
6 */
7
8 /**
9 * Prevent loading this file directly
10 */
11 defined( 'ABSPATH' ) || exit();
12
13 /**
14 * Get quiz from anything is passed
15 *
16 * @param mixed $the_quiz
17 *
18 * @return bool|LP_Quiz
19 */
20 function learn_press_get_quiz( $the_quiz ) {
21 return LP_Quiz::get_quiz( $the_quiz );
22 }
23
24 /**
25 * Get question from anything is passed
26 *
27 * @param mixed $the_question
28 *
29 * @return bool|LP_Question
30 */
31 function learn_press_get_question( $the_question ) {
32 return LP_Question::get_question( $the_question );
33 }
34
35 function learn_press_add_question_answer_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
36 return add_metadata( 'learnpress_question_answer', $item_id, $meta_key, $meta_value, $prev_value );
37 }
38
39 function learn_press_update_question_answer_meta( $item_id, $meta_key, $meta_value, $prev_value = '' ) {
40 return update_metadata( 'learnpress_question_answer', $item_id, $meta_key, $meta_value, $prev_value );
41 }
42
43 function learn_press_delete_question_answer_meta( $item_id, $meta_key, $meta_value, $delete_all = false ) {
44 return delete_metadata( 'learnpress_question_answer', $item_id, $meta_key, $meta_value, $delete_all );
45 }
46
47 function learn_press_get_question_answer_meta( $item_id, $meta_key, $single = true ) {
48 return get_metadata( 'learnpress_question_answer', $item_id, $meta_key, $single );
49 }
50
51
52 /**
53 * Get all questions of a quiz
54 *
55 * @author TuNN
56 *
57 * @param int $quiz_id The ID of a quiz to get all questions
58 * @param boolean $only_ids return an array of questions with IDs only or as post objects
59 *
60 * @return array|null
61 */
62 function learn_press_get_quiz_questions( $quiz_id = null, $only_ids = true ) {
63 if ( ! $quiz_id ) {
64 $quiz_id = get_the_ID();
65 }
66
67 return ( $quiz = LP_Quiz::get_quiz( $quiz_id ) ) ? $quiz->get_questions() : false;
68 }
69
70 /**
71 * Check to see if user not passed the ID of quiz try to get it from global
72 * Only works in single quiz page
73 *
74 * @param $id
75 *
76 * @return mixed
77 */
78 function learn_press_get_quiz_id( $id ) {
79 if ( ! $id ) {
80 global $quiz;
81 $id = $quiz->id;
82 }
83
84 return $id;
85 }
86
87 /**
88 * Check if user has completed a quiz or not
89 *
90 * @author TuNN
91 *
92 * @param int $user_id The ID of user need to check
93 * @param int $quiz_id The ID of quiz need to check
94 *
95 * @return boolean
96 * @depecated 4.1.6.9
97 */
98 /*function learn_press_user_has_completed_quiz( $user_id = null, $quiz_id = null ) {
99 $user = learn_press_get_user( $user_id );
100
101 if ( $user ) {
102 return $user->has_completed_quiz( $quiz_id );
103 }
104
105 return false;
106 }*/
107
108 /**
109 * get the time remaining of a quiz has started by an user
110 *
111 * @param null $user_id
112 * @param null $quiz_id
113 *
114 * @return int
115 */
116 function learn_press_get_quiz_time_remaining( $user_id = null, $quiz_id = null ) {
117 global $quiz;
118 if ( ! $user_id ) {
119 $user_id = get_current_user_id();
120 }
121 $quiz_id = $quiz_id ? $quiz_id : ( $quiz ? $quiz->id : 0 );
122 if ( ! $quiz_id ) {
123 return 0;
124 }
125 $meta = get_user_meta( $user_id, '_lpr_quiz_start_time', true );
126 $quiz_duration = get_post_meta( $quiz_id, '_lpr_duration', true );
127 $time_remaining = $quiz_duration * 60;
128 $quiz_start_time = ! empty( $meta[ $quiz_id ] ) ? $meta[ $quiz_id ] : 0;
129 $quiz_start_time = apply_filters( 'learn_press_user_quiz_start_time', $quiz_start_time, $quiz_id, $user_id );
130
131 if ( $quiz_duration && learn_press_user_has_started_quiz( $user_id, $quiz_id ) ) {
132 $quiz_duration *= 60;
133 $now = current_time( 'timestamp' );
134
135 if ( $now < $quiz_start_time + $quiz_duration ) {
136 $time_remaining = $quiz_start_time + $quiz_duration - $now;
137 } else {
138 $time_remaining = 0;
139 }
140 }
141
142 return apply_filters( 'learn_press_get_quiz_time_remaining', $time_remaining, $user_id, $quiz_id );
143 }
144
145
146 /**
147 * Check if user has started a quiz or not.
148 *
149 * @param null $user_id
150 * @param null $quiz_id
151 *
152 * @return bool|mixed
153 * @throws Exception
154 */
155 function learn_press_user_has_started_quiz( $user_id = null, $quiz_id = null ) {
156 $user = $user_id ? learn_press_get_user( $user_id ) : learn_press_get_current_user();
157
158 return $user ? $user->has_started_quiz( $quiz_id ) : false;
159 }
160
161 /**
162 * Get current status of a quiz for user
163 * Status:
164 * - null => User does not start quiz
165 * - Started => User has started quiz
166 * - Completed => User has finished quiz
167 *
168 * @param $quiz_id
169 * @param bool|false $user_id
170 *
171 * @return string
172 */
173 function learn_press_get_user_quiz_status( $quiz_id, $user_id = false ) {
174 $user = $user_id ? learn_press_get_user( $user_id ) : learn_press_get_current_user();
175
176 return $user ? $user->get_quiz_status( $quiz_id ) : '';
177 }
178
179 function learn_press_get_quizzes( $user_id = 0, &$args = array() ) {
180 // TODO: get all quizzes by user
181 }
182
183 /**
184 * Add new type that LP question can support
185 *
186 * @param string|array $types
187 * @param string|array $supports
188 */
189 function learn_press_add_question_type_support( $types, $supports ) {
190 if ( empty( $GLOBALS['learn_press_question_type_support'] ) ) {
191 $GLOBALS['learn_press_question_type_support'] = array();
192 }
193 $_supports = $GLOBALS['learn_press_question_type_support'];
194 settype( $types, 'array' );
195 settype( $supports, 'array' );
196
197 $types = array_filter( $types );
198 $supports = array_filter( $supports );
199
200 if ( $types ) {
201 foreach ( $types as $type ) {
202 if ( ! $type ) {
203 continue;
204 }
205 if ( empty( $_supports[ $type ] ) ) {
206 $_supports[ $type ] = array();
207 }
208 if ( $supports ) {
209 foreach ( $supports as $s ) {
210 $_supports[ $type ][] = $s;
211 }
212 }
213 $_supports[ $type ] = array_filter( $_supports[ $type ] );
214 }
215 }
216 $GLOBALS['learn_press_question_type_support'] = $_supports;
217 }
218
219 /**
220 * @param string $type
221 *
222 * @return array|mixed
223 */
224 function learn_press_get_question_type_support( $type = '' ) {
225 $supports = ! empty( $GLOBALS['learn_press_question_type_support'] ) ? $GLOBALS['learn_press_question_type_support'] : array();
226
227 return $type && ! empty( $supports[ $type ] ) ? $supports[ $type ] : $supports;
228 }
229
230 /**
231 * Check if a type question is supported.
232 *
233 * @param string $type
234 *
235 * @return bool
236 */
237 function learn_press_is_support_question_type( $type ) {
238
239 if ( is_array( $type ) ) {
240 $type = reset( $type );
241 }
242
243 $supports = learn_press_get_question_type_support();
244
245 // New type is supported?
246 if ( $supports && ! array_key_exists( $type, $supports ) ) {
247 return false;
248 }
249
250 return true;
251 }
252
253 function learn_press_question_type_support( $type, $features ) {
254 settype( $features, 'array' );
255 $features = array_filter( $features );
256 $supports = learn_press_get_question_type_support( $type );
257 $has_support = true;
258
259 if ( $features ) {
260 foreach ( $features as $feature ) {
261 $has_support = $has_support && in_array( $feature, $supports );
262 }
263 }
264
265 return $has_support;
266 }
267
268 function learn_press_default_question_type_support() {
269 learn_press_add_question_type_support(
270 array(
271 'multi_choice',
272 'single_choice',
273 'true_or_false',
274 'fill_in_blanks',
275 ),
276 'check-answer'
277 );
278 }
279
280 add_action( 'plugins_loaded', 'learn_press_default_question_type_support' );
281
282
283 if ( ! function_exists( 'learn_press_quiz_get_questions_order' ) ) {
284 /**
285 * Get question order in quiz.
286 *
287 * @since 3.0.0
288 *
289 * @param array $questions
290 *
291 * @return array
292 */
293 function learn_press_quiz_get_questions_order( $questions = array() ) {
294 if ( ! $questions ) {
295 return array();
296 }
297
298 global $wpdb;
299
300 $ids = $orders = array();
301
302 foreach ( $questions as $id => $question ) {
303 $ids[] = $id;
304 }
305
306 $order = $wpdb->get_results( "SELECT q.question_id AS q_id, q.question_order AS q_order FROM $wpdb->learnpress_quiz_questions AS q", ARRAY_A );
307
308 if ( $order ) {
309 foreach ( $order as $id => $_order ) {
310 $orders[ $_order['q_id'] ] = $_order['q_order'];
311 }
312 }
313
314 return $orders;
315 }
316 }
317
318 /**
319 * @return bool
320 * @deprecated 4.1.6.1
321 * We will remove on version 4.1.7, you can use "get_status" function on the class LP_User_Item_Quiz to replace
322 */
323 function learn_press_is_review_questions() {
324 _deprecated_function( __FUNCTION__, '4.1.6.1' );
325
326 $item = LP_Global::course_item();
327 $user = learn_press_get_current_user();
328
329 if ( $item && $user ) {
330 $course = learn_press_get_course();
331
332 if ( ! $course ) {
333 return false;
334 }
335
336 $quiz_data = $user->get_item_data( $item->get_id(), $course->get_id() );
337
338 return $quiz_data && $quiz_data->is_review_questions();
339 }
340
341 return false;
342 }
343
344
345
346