PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.7.3.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.7.3.1
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 / admin / editor / class-lp-admin-editor-quiz.php

class-lp-admin-editor-quiz.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.7.3.1, at inc/admin/editor/class-lp-admin-editor-quiz.php

636 lines 14.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class LP_Admin_Editor_Quiz
5 *
6 * @since 3.0.2
7 */
8 class LP_Admin_Editor_Quiz extends LP_Admin_Editor {
9
10 /**
11 * @var LP_Quiz_CURD
12 */
13 protected $quiz_curd = null;
14
15 /**
16 * @var LP_Question_CURD
17 */
18 protected $question_curd = null;
19
20 /**
21 * @var LP_Quiz
22 */
23 protected $quiz = null;
24
25 /**
26 * LP_Admin_Editor_Quiz constructor.
27 */
28 public function __construct() {
29 }
30
31 /**
32 * Do the action depending on ajax calls with params
33 *
34 * @return bool|mixed|WP_Error
35 */
36 public function dispatch() {
37 check_ajax_referer( 'learnpress_admin_quiz_editor', 'nonce' );
38 $args = wp_parse_args(
39 $_REQUEST,
40 array(
41 'id' => false,
42 'type' => '',
43 )
44 );
45
46 // get quiz
47 $quiz_id = $args['id'];
48 $quiz = learn_press_get_quiz( $quiz_id );
49
50 if ( ! $quiz ) {
51 return new WP_Error( 'INVALID_QUIZ', __( 'Invalid quiz', 'learnpress' ) );
52 }
53
54 $this->quiz = $quiz;
55 $this->quiz_curd = new LP_Quiz_CURD();
56 $this->question_curd = new LP_Question_CURD();
57 $this->result = array( 'status' => false );
58
59 $this->call( $args['type'], array( $args ) );
60
61 return $this->get_result();
62 }
63
64 /**
65 * Get question data in admin quiz editor.
66 *
67 * @since 3.0.0
68 *
69 * @param $question
70 * @param $object | if true, input in question object, do not need init LP_Question::get_question()
71 * @param array $args
72 *
73 * @return array
74 */
75 public function get_question_data_to_quiz_editor( $question, $object = false, $args = array() ) {
76
77 if ( ! $object || ! $question ) {
78 if ( get_post_type( $question ) !== LP_QUESTION_CPT ) {
79 return array();
80 }
81
82 // get question
83 $question = LP_Question::get_question( $question );
84 }
85
86 // question id
87 $question_id = $question->get_id();
88 // question answer
89 $answer_options = $question->get_data( 'answer_options' );
90 $answer = array();
91 foreach ( $answer_options as $answer_option ) {
92 if ( ! isset( $answer[ $answer_option['question_answer_id'] ] ) ) {
93 $answer[ $answer_option['question_answer_id'] ] = $answer_option;
94 }
95 }
96 $answers = array_values( $answer );
97 $data = wp_parse_args(
98 $args,
99 array(
100 'id' => $question_id,
101 'open' => false,
102 'title' => get_the_title( $question_id ),
103 'type' => array(
104 'key' => $question->get_type(),
105 'label' => $question->get_type_label(),
106 ),
107 'answers' => $answers,
108 'settings' => array(
109 'mark' => get_post_meta( $question_id, '_lp_mark', true ),
110 'explanation' => get_post_meta( $question_id, '_lp_explanation', true ),
111 'hint' => get_post_meta( $question_id, '_lp_hint', true ),
112 ),
113 'order' => count( $answers ),
114 )
115 );
116
117 return $data;
118 }
119
120 /**
121 * @param array $args
122 *
123 * @return bool
124 */
125 public function hidden_questions( $args = array() ) {
126 $hidden = ! empty( $args['hidden'] ) ? $args['hidden'] : false;
127 update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden );
128
129 return true;
130 }
131
132 /**
133 * @param array $args
134 *
135 * @return bool
136 */
137 public function new_question( $args = array() ) {
138 $question = ! empty( $args['question'] ) ? $args['question'] : false;
139 $question = json_decode( wp_unslash( $question ), true );
140
141 if ( ! $question ) {
142 return false;
143 }
144
145 $quiz_id = $this->quiz->get_id();
146
147 // draft quiz
148 if ( get_post_status( $quiz_id ) == 'auto-draft' ) {
149
150 $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : '';
151 $draft_quiz = (array) ( json_decode( wp_unslash( $draft_quiz ), '' ) );
152
153 $quiz_args = array(
154 'id' => $this->quiz->get_id(),
155 'title' => $draft_quiz['title'] ? $draft_quiz['title'] : __( 'New Quiz', 'learnpress' ),
156 'content' => $draft_quiz['content'],
157 'status' => 'draft',
158 );
159
160 $quiz_id = $this->quiz_curd->create( $quiz_args );
161 }
162
163 if ( ! isset( $quiz_id ) ) {
164 $this->result = new WP_Error( 'CREATE_QUIZ_FAILED', __( 'Quiz creation failed.', 'learnpress' ) );
165
166 return false;
167 }
168
169 $args = array(
170 'quiz_id' => $quiz_id,
171 'title' => $question['title'],
172 'type' => $question['type'],
173 );
174
175 $new_question = $this->question_curd->create( $args );
176
177 if ( ! is_wp_error( $new_question ) ) {
178 // update hidden questions in quiz meta
179 $quiz = LP_Quiz::get_quiz( $quiz_id );
180 $hidden_questions = $quiz->get_question_ids();
181
182 if ( $hidden_questions ) {
183 $index = array_search( $new_question->get_id(), $hidden_questions );
184 if ( $index !== false ) {
185 unset( $hidden_questions[ $index ] );
186 }
187 //$hidden_questions = array_keys( $hidden_questions );
188 }
189
190 update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions );
191
192 // get new question data
193 $this->result = $this->get_question_data_to_quiz_editor( $new_question, true, array( 'open' => true ) );
194
195 if ( isset( $question['id'] ) ) {
196 $this->result['temp_id'] = $question['id'];
197 }
198
199 return true;
200 }
201
202 return false;
203 }
204
205 /**
206 * @param array $args
207 *
208 * @return bool
209 */
210 public function sort_questions( $args = array() ) {
211 $order = ! empty( $args['order'] ) ? $args['order'] : false;
212 $order = json_decode( wp_unslash( $order ), true );
213
214 if ( ! $order ) {
215 return false;
216 }
217
218 $this->result = $this->quiz_curd->sort_questions( $order );
219
220 return true;
221 }
222
223 /**
224 * @param array $args
225 *
226 * @return bool
227 */
228 public function update_question_title( $args = array() ) {
229 $question = ! empty( $args['question'] ) ? $args['question'] : false;
230 $question = json_decode( wp_unslash( $question ), true );
231
232 if ( ! $question ) {
233 return false;
234 }
235
236 wp_update_post(
237 array(
238 'ID' => $question['id'],
239 'post_title' => $question['title'],
240 )
241 );
242
243 $this->result['status'] = true;
244
245 return true;
246 }
247
248 /**
249 * @param array $args
250 *
251 * @return bool
252 */
253 public function change_question_type( $args = array() ) {
254 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
255 $type = ! empty( $args['question_type'] ) ? $args['question_type'] : false;
256
257 if ( ! ( $question_id || $type ) ) {
258 return false;
259 }
260
261 $question = LP_Question::get_question( $question_id );
262
263 $question = $this->question_curd->change_question_type( $question, $type );
264
265 $this->result = $this->get_question_data_to_quiz_editor( $question, true );
266
267 return true;
268 }
269
270 /**
271 * @param array $args
272 *
273 * @return bool
274 */
275 public function clone_question( $args = array() ) {
276 $question = ! empty( $args['question'] ) ? $args['question'] : false;
277 $question = json_decode( wp_unslash( $question ), true );
278
279 if ( ! $question ) {
280 return false;
281 }
282
283 // duplicate question
284 $new_question_id = $this->question_curd->duplicate( $question['id'], array( 'post_status' => 'publish' ) );
285
286 if ( ! is_wp_error( $new_question_id ) ) {
287
288 // add question to hidden questions in quiz meta
289 $hidden_questions = get_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', true );
290 if ( ! $hidden_questions ) {
291 $hidden_questions = array();
292 }
293 $hidden_questions[] = $new_question_id;// add question to hidden questions in quiz meta
294 update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden_questions );
295
296 // add question to quiz
297 $this->quiz_curd->add_question( $this->quiz->get_id(), $new_question_id );
298
299 $this->result = $this->get_question_data_to_quiz_editor( $new_question_id );
300
301 return true;
302 }
303
304 return false;
305 }
306
307 /**
308 * @param array $args
309 *
310 * @return bool
311 */
312 public function remove_question( $args = array() ) {
313 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
314
315 if ( ! $question_id ) {
316 return false;
317 }
318
319 $this->result = $this->quiz_curd->remove_questions( $this->quiz->get_id(), $question_id );
320
321 return true;
322 }
323
324 /**
325 * @param array $args
326 *
327 * @return bool
328 */
329 public function delete_question( $args = array() ) {
330 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
331
332 if ( ! $question_id ) {
333 return false;
334 }
335
336 $this->result = wp_trash_post( $question_id );
337
338 return true;
339 }
340
341 /**
342 * @param array $args
343 *
344 * @return bool
345 */
346 public function sort_question_answers( $args = array() ) {
347 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
348
349 $order = ! empty( $args['order'] ) ? $args['order'] : false;
350 $order = json_decode( wp_unslash( $order ), true );
351
352 if ( ! ( $question_id && $order ) ) {
353 return false;
354 }
355
356 // sort answer
357 $this->result = $this->question_curd->sort_answers( $question_id, $order );
358
359 return true;
360 }
361
362 /**
363 * @param array $args
364 *
365 * @return bool
366 */
367 public function update_question_answer_title( $args = array() ) {
368 // question id
369 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
370
371 // answers
372 $answer = ! empty( $args['answer'] ) ? $args['answer'] : false;
373 $answer = json_decode( wp_unslash( $answer ), true );
374
375 if ( ! ( $question_id && $answer ) ) {
376 return false;
377 }
378
379 // update answer title
380 $this->result = $this->question_curd->update_answer_title( $question_id, $answer );
381
382 return true;
383 }
384
385 /**
386 * @param array $args
387 *
388 * @return bool
389 */
390 public function change_question_correct_answer( $args = array() ) {
391 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
392
393 // correct answer
394 $correct = ! empty( $args['correct'] ) ? $args['correct'] : false;
395 $correct = json_decode( wp_unslash( $correct ), true );
396
397 if ( ! ( $question_id && $correct ) ) {
398 return false;
399 }
400
401 $question = LP_Question::get_question( $question_id );
402 // update correct answer, get new question
403 $question = $this->question_curd->change_correct_answer( $question, $correct );
404
405 $this->result = $this->get_question_data_to_quiz_editor( $question, true );
406
407 return true;
408 }
409
410 /**
411 * @param array $args
412 *
413 * @return bool
414 */
415 public function delete_question_answer( $args = array() ) {
416 $question_id = LP_Helper::sanitize_params_submitted( $_POST['question_id'] ?? 0 );
417 $answer_id = LP_Helper::sanitize_params_submitted( $_POST['answer_id'] ?? 0 );
418
419 if ( ! $question_id || ! $answer_id ) {
420 return false;
421 }
422
423 $this->result = $this->question_curd->delete_answer( $question_id, $answer_id );
424
425 return true;
426 }
427
428 /**
429 * @param array $args
430 *
431 * @return bool
432 */
433 public function new_question_answer( $args = array() ) {
434 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
435
436 if ( ! $question_id ) {
437 return false;
438 }
439
440 $answer = LP_Question::get_default_answer();
441 $new_answer_id = $this->question_curd->new_answer( $question_id, $answer );
442 $question = LP_Question::get_question( $question_id );
443
444 if ( $new_answer_id ) {
445 $this->result = array_merge(
446 $answer,
447 array(
448 'temp_id' => isset( $args['question_answer_id'] ) ? $args['question_answer_id'] : 0,
449 'question_answer_id' => $new_answer_id,
450 'question_id' => $question_id,
451 'order' => count( $question->get_data( 'answer_options' ) ),
452 )
453 );
454
455 return true;
456 }
457
458 return false;
459 }
460
461 public function update_quiz_questions_hidden( $args = array() ) {
462 $id = $args['id'];
463 $questions = isset( $args['hidden'] ) ? $args['hidden'] : false;
464
465 if ( $questions ) {
466 update_post_meta( $id, '_hidden_questions_settings', $questions );
467 } else {
468 delete_post_meta( $id, '_hidden_questions_settings' );
469 }
470
471 return true;
472 }
473
474 /**
475 * @param array $args
476 *
477 * @return bool
478 */
479 public function update_question_content( $args = array() ) {
480 $question = ! empty( $args['question'] ) ? $args['question'] : false;
481 $question = json_decode( wp_unslash( $question ), true );
482
483 if ( ! $question ) {
484 return false;
485 }
486
487 wp_update_post(
488 array(
489 'ID' => $question['id'],
490 'post_content' => $question['settings']['content'],
491 )
492 );
493
494 $this->result['status'] = true;
495
496 return true;
497 }
498
499 /**
500 * @param array $args
501 *
502 * @return bool
503 */
504 public function update_question_meta( $args = array() ) {
505 $question = ! empty( $args['question'] ) ? $args['question'] : false;
506 $question = json_decode( wp_unslash( $question ), true );
507
508 $meta_key = ! empty( $args['meta_key'] ) ? $args['meta_key'] : false;
509
510 if ( ! ( $question && $meta_key ) ) {
511 return false;
512 }
513
514 update_post_meta( $question['id'], '_lp_' . $meta_key, $question['settings'][ $meta_key ] );
515
516 $this->result['status'] = true;
517
518 return true;
519 }
520
521 /**
522 * @param array $args
523 *
524 * @return bool
525 */
526 public function search_items( $args = array() ) {
527 $query = ! empty( $args['query'] ) ? $args['query'] : '';
528 $page = ! empty( $args['page'] ) ? intval( $args['page'] ) : 1;
529 $exclude = ! empty( $args['exclude'] ) ? intval( $args['exclude'] ) : '';
530
531 if ( $exclude ) {
532 $exclude = json_decode( $exclude, true );
533 }
534
535 $ids_exclude = array();
536 if ( is_array( $exclude ) ) {
537 foreach ( $exclude as $item ) {
538 $ids_exclude[] = $item['id'];
539 }
540 }
541
542 $search = new LP_Modal_Search_Items(
543 array(
544 'type' => 'lp_question',
545 'context' => 'quiz',
546 'context_id' => $this->quiz->get_id(),
547 'term' => $query,
548 'limit' => apply_filters( 'learn-press/quiz-editor/choose-items-limit', 10 ),
549 'paged' => $page,
550 'exclude' => $ids_exclude,
551 )
552 );
553
554 $ids_item = $search->get_items();
555
556 $items = array();
557 foreach ( $ids_item as $id ) {
558 $post = get_post( $id );
559
560 $items[] = array(
561 'id' => $post->ID,
562 'title' => $post->post_title,
563 'type' => $post->post_type,
564 );
565 }
566
567 $this->result = array(
568 'items' => $items,
569 'pagination' => $search->get_pagination( false ),
570 );
571
572 return true;
573 }
574
575 /**
576 * @param array $args
577 *
578 * @return bool
579 */
580 public function add_questions_to_quiz( $args = array() ) {
581 $questions = LP_Helper::sanitize_params_submitted( $_POST['items'] ?? '' );
582
583 if ( ! $questions ) {
584 return false;
585 }
586
587 $questions = json_decode( wp_unslash( $questions ), true );
588
589 $quiz_id = $this->quiz->get_id();
590
591 if ( get_post_status( $quiz_id ) == 'auto-draft' ) {
592 $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : '';
593 $draft_quiz = (array) ( json_decode( wp_unslash( $draft_quiz ), '' ) );
594
595 $quiz_args = array(
596 'id' => $quiz_id,
597 'title' => $draft_quiz['title'],
598 'content' => $draft_quiz['content'],
599 'status' => 'draft',
600 );
601
602 $quiz_id = $this->quiz_curd->create( $quiz_args );
603 }
604
605 if ( ! isset( $quiz_id ) ) {
606 $this->result = new WP_Error( __( 'Quiz creation failed.', 'learnpress' ) );
607
608 return false;
609 }
610
611 if ( $questions ) {
612 $hidden_questions = get_post_meta( $quiz_id, '_lp_hidden_questions', true );
613
614 if ( ! $hidden_questions ) {
615 $hidden_questions = array();
616 }
617
618 foreach ( $questions as $key => $question ) {
619 $hidden_questions[] = $question['id'];
620 $this->quiz_curd->add_question( $quiz_id, $question['id'] );
621 }
622
623 $hidden_questions = array_unique( $hidden_questions );
624
625 update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions );
626
627 LP_Object_Cache::flush();
628 $this->result = $this->quiz->quiz_editor_get_questions();
629
630 return true;
631 }
632
633 return false;
634 }
635 }
636