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 / 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.6.9.4, at inc/admin/editor/class-lp-admin-editor-quiz.php

634 lines 14.8 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
179 // update hidden questions in quiz meta
180 $quiz = LP_Quiz::get_quiz( $quiz_id );
181 $hidden_questions = $quiz->get_questions();
182
183 if ( $hidden_questions ) {
184 unset( $hidden_questions[ $new_question->get_id() ] );
185 $hidden_questions = array_keys( $hidden_questions );
186 }
187
188 update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions );
189
190 // get new question data
191 $this->result = $this->get_question_data_to_quiz_editor( $new_question, true, array( 'open' => true ) );
192
193 if ( isset( $question['id'] ) ) {
194 $this->result['temp_id'] = $question['id'];
195 }
196
197 return true;
198 }
199
200 return false;
201 }
202
203 /**
204 * @param array $args
205 *
206 * @return bool
207 */
208 public function sort_questions( $args = array() ) {
209 $order = ! empty( $args['order'] ) ? $args['order'] : false;
210 $order = json_decode( wp_unslash( $order ), true );
211
212 if ( ! $order ) {
213 return false;
214 }
215
216 $this->result = $this->quiz_curd->sort_questions( $order );
217
218 return true;
219 }
220
221 /**
222 * @param array $args
223 *
224 * @return bool
225 */
226 public function update_question_title( $args = array() ) {
227 $question = ! empty( $args['question'] ) ? $args['question'] : false;
228 $question = json_decode( wp_unslash( $question ), true );
229
230 if ( ! $question ) {
231 return false;
232 }
233
234 wp_update_post(
235 array(
236 'ID' => $question['id'],
237 'post_title' => $question['title'],
238 )
239 );
240
241 $this->result['status'] = true;
242
243 return true;
244 }
245
246 /**
247 * @param array $args
248 *
249 * @return bool
250 */
251 public function change_question_type( $args = array() ) {
252 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
253 $type = ! empty( $args['question_type'] ) ? $args['question_type'] : false;
254
255 if ( ! ( $question_id || $type ) ) {
256 return false;
257 }
258
259 $question = LP_Question::get_question( $question_id );
260
261 $question = $this->question_curd->change_question_type( $question, $type );
262
263 $this->result = $this->get_question_data_to_quiz_editor( $question, true );
264
265 return true;
266 }
267
268 /**
269 * @param array $args
270 *
271 * @return bool
272 */
273 public function clone_question( $args = array() ) {
274 $question = ! empty( $args['question'] ) ? $args['question'] : false;
275 $question = json_decode( wp_unslash( $question ), true );
276
277 if ( ! $question ) {
278 return false;
279 }
280
281 // duplicate question
282 $new_question_id = $this->question_curd->duplicate( $question['id'], array( 'post_status' => 'publish' ) );
283
284 if ( ! is_wp_error( $new_question_id ) ) {
285
286 // add question to hidden questions in quiz meta
287 $hidden_questions = get_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', true );
288 if ( ! $hidden_questions ) {
289 $hidden_questions = array();
290 }
291 $hidden_questions[] = $new_question_id;// add question to hidden questions in quiz meta
292 update_post_meta( $this->quiz->get_id(), '_lp_hidden_questions', $hidden_questions );
293
294 // add question to quiz
295 $this->quiz_curd->add_question( $this->quiz->get_id(), $new_question_id );
296
297 $this->result = $this->get_question_data_to_quiz_editor( $new_question_id );
298
299 return true;
300 }
301
302 return false;
303 }
304
305 /**
306 * @param array $args
307 *
308 * @return bool
309 */
310 public function remove_question( $args = array() ) {
311 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
312
313 if ( ! $question_id ) {
314 return false;
315 }
316
317 $this->result = $this->quiz_curd->remove_questions( $this->quiz->get_id(), $question_id );
318
319 return true;
320 }
321
322 /**
323 * @param array $args
324 *
325 * @return bool
326 */
327 public function delete_question( $args = array() ) {
328 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
329
330 if ( ! $question_id ) {
331 return false;
332 }
333
334 $this->result = wp_trash_post( $question_id );
335
336 return true;
337 }
338
339 /**
340 * @param array $args
341 *
342 * @return bool
343 */
344 public function sort_question_answers( $args = array() ) {
345 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
346
347 $order = ! empty( $args['order'] ) ? $args['order'] : false;
348 $order = json_decode( wp_unslash( $order ), true );
349
350 if ( ! ( $question_id && $order ) ) {
351 return false;
352 }
353
354 // sort answer
355 $this->result = $this->question_curd->sort_answers( $question_id, $order );
356
357 return true;
358 }
359
360 /**
361 * @param array $args
362 *
363 * @return bool
364 */
365 public function update_question_answer_title( $args = array() ) {
366 // question id
367 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
368
369 // answers
370 $answer = ! empty( $args['answer'] ) ? $args['answer'] : false;
371 $answer = json_decode( wp_unslash( $answer ), true );
372
373 if ( ! ( $question_id && $answer ) ) {
374 return false;
375 }
376
377 // update answer title
378 $this->result = $this->question_curd->update_answer_title( $question_id, $answer );
379
380 return true;
381 }
382
383 /**
384 * @param array $args
385 *
386 * @return bool
387 */
388 public function change_question_correct_answer( $args = array() ) {
389 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
390
391 // correct answer
392 $correct = ! empty( $args['correct'] ) ? $args['correct'] : false;
393 $correct = json_decode( wp_unslash( $correct ), true );
394
395 if ( ! ( $question_id && $correct ) ) {
396 return false;
397 }
398
399 $question = LP_Question::get_question( $question_id );
400 // update correct answer, get new question
401 $question = $this->question_curd->change_correct_answer( $question, $correct );
402
403 $this->result = $this->get_question_data_to_quiz_editor( $question, true );
404
405 return true;
406 }
407
408 /**
409 * @param array $args
410 *
411 * @return bool
412 */
413 public function delete_question_answer( $args = array() ) {
414 $question_id = LP_Helper::sanitize_params_submitted( $_POST['question_id'] ?? 0 );
415 $answer_id = LP_Helper::sanitize_params_submitted( $_POST['answer_id'] ?? 0 );
416
417 if ( ! $question_id || ! $answer_id ) {
418 return false;
419 }
420
421 $this->result = $this->question_curd->delete_answer( $question_id, $answer_id );
422
423 return true;
424 }
425
426 /**
427 * @param array $args
428 *
429 * @return bool
430 */
431 public function new_question_answer( $args = array() ) {
432 $question_id = ! empty( $args['question_id'] ) ? $args['question_id'] : false;
433
434 if ( ! $question_id ) {
435 return false;
436 }
437
438 $answer = LP_Question::get_default_answer();
439 $new_answer_id = $this->question_curd->new_answer( $question_id, $answer );
440 $question = LP_Question::get_question( $question_id );
441
442 if ( $new_answer_id ) {
443 $this->result = array_merge(
444 $answer,
445 array(
446 'temp_id' => isset( $args['question_answer_id'] ) ? $args['question_answer_id'] : 0,
447 'question_answer_id' => $new_answer_id,
448 'question_id' => $question_id,
449 'order' => count( $question->get_data( 'answer_options' ) ),
450 )
451 );
452
453 return true;
454 }
455
456 return false;
457 }
458
459 public function update_quiz_questions_hidden( $args = array() ) {
460 $id = $args['id'];
461 $questions = isset( $args['hidden'] ) ? $args['hidden'] : false;
462
463 if ( $questions ) {
464 update_post_meta( $id, '_hidden_questions_settings', $questions );
465 } else {
466 delete_post_meta( $id, '_hidden_questions_settings' );
467 }
468
469 return true;
470 }
471
472 /**
473 * @param array $args
474 *
475 * @return bool
476 */
477 public function update_question_content( $args = array() ) {
478 $question = ! empty( $args['question'] ) ? $args['question'] : false;
479 $question = json_decode( wp_unslash( $question ), true );
480
481 if ( ! $question ) {
482 return false;
483 }
484
485 wp_update_post(
486 array(
487 'ID' => $question['id'],
488 'post_content' => $question['settings']['content'],
489 )
490 );
491
492 $this->result['status'] = true;
493
494 return true;
495 }
496
497 /**
498 * @param array $args
499 *
500 * @return bool
501 */
502 public function update_question_meta( $args = array() ) {
503 $question = ! empty( $args['question'] ) ? $args['question'] : false;
504 $question = json_decode( wp_unslash( $question ), true );
505
506 $meta_key = ! empty( $args['meta_key'] ) ? $args['meta_key'] : false;
507
508 if ( ! ( $question && $meta_key ) ) {
509 return false;
510 }
511
512 update_post_meta( $question['id'], '_lp_' . $meta_key, $question['settings'][ $meta_key ] );
513
514 $this->result['status'] = true;
515
516 return true;
517 }
518
519 /**
520 * @param array $args
521 *
522 * @return bool
523 */
524 public function search_items( $args = array() ) {
525 $query = ! empty( $args['query'] ) ? $args['query'] : '';
526 $page = ! empty( $args['page'] ) ? intval( $args['page'] ) : 1;
527 $exclude = ! empty( $args['exclude'] ) ? intval( $args['exclude'] ) : '';
528
529 if ( $exclude ) {
530 $exclude = json_decode( $exclude, true );
531 }
532
533 $ids_exclude = array();
534 if ( is_array( $exclude ) ) {
535 foreach ( $exclude as $item ) {
536 $ids_exclude[] = $item['id'];
537 }
538 }
539
540 $search = new LP_Modal_Search_Items(
541 array(
542 'type' => 'lp_question',
543 'context' => 'quiz',
544 'context_id' => $this->quiz->get_id(),
545 'term' => $query,
546 'limit' => apply_filters( 'learn-press/quiz-editor/choose-items-limit', 10 ),
547 'paged' => $page,
548 'exclude' => $ids_exclude,
549 )
550 );
551
552 $ids_item = $search->get_items();
553
554 $items = array();
555 foreach ( $ids_item as $id ) {
556 $post = get_post( $id );
557
558 $items[] = array(
559 'id' => $post->ID,
560 'title' => $post->post_title,
561 'type' => $post->post_type,
562 );
563 }
564
565 $this->result = array(
566 'items' => $items,
567 'pagination' => $search->get_pagination( false ),
568 );
569
570 return true;
571 }
572
573 /**
574 * @param array $args
575 *
576 * @return bool
577 */
578 public function add_questions_to_quiz( $args = array() ) {
579 $questions = LP_Helper::sanitize_params_submitted( $_POST['items'] ?? '' );
580
581 if ( ! $questions ) {
582 return false;
583 }
584
585 $questions = json_decode( wp_unslash( $questions ), true );
586
587 $quiz_id = $this->quiz->get_id();
588
589 if ( get_post_status( $quiz_id ) == 'auto-draft' ) {
590 $draft_quiz = ! empty( $args['draft_quiz'] ) ? $args['draft_quiz'] : '';
591 $draft_quiz = (array) ( json_decode( wp_unslash( $draft_quiz ), '' ) );
592
593 $quiz_args = array(
594 'id' => $quiz_id,
595 'title' => $draft_quiz['title'],
596 'content' => $draft_quiz['content'],
597 'status' => 'draft',
598 );
599
600 $quiz_id = $this->quiz_curd->create( $quiz_args );
601 }
602
603 if ( ! isset( $quiz_id ) ) {
604 $this->result = new WP_Error( __( 'Quiz creation failed.', 'learnpress' ) );
605
606 return false;
607 }
608
609 if ( $questions ) {
610 $hidden_questions = get_post_meta( $quiz_id, '_lp_hidden_questions', true );
611
612 if ( ! $hidden_questions ) {
613 $hidden_questions = array();
614 }
615
616 foreach ( $questions as $key => $question ) {
617 $hidden_questions[] = $question['id'];
618 $this->quiz_curd->add_question( $quiz_id, $question['id'] );
619 }
620
621 $hidden_questions = array_unique( $hidden_questions );
622
623 update_post_meta( $quiz_id, '_lp_hidden_questions', $hidden_questions );
624
625 LP_Object_Cache::flush();
626 $this->result = $this->quiz->quiz_editor_get_questions();
627
628 return true;
629 }
630
631 return false;
632 }
633 }
634