| @@ -9,8 +9,11 @@ | ||
| 9 | 9 | |
| 10 | 10 | /** |
| 11 | 11 | * Prevent loading this file directly |
| 12 | 12 | */ |
| 13 | + | |
| 14 | +use LearnPress\Helpers\Template; | |
| 15 | + | |
| 13 | 16 | defined( 'ABSPATH' ) || exit(); |
| 14 | 17 | |
| 15 | 18 | if ( ! class_exists( 'LP_Question' ) ) { |
| 16 | 19 | |
| @@ -114,9 +117,9 @@ | ||
| 114 | 117 | $this->load(); |
| 115 | 118 | } |
| 116 | 119 | |
| 117 | 120 | $this->_options = $args; |
| 118 | - $this->_init(); | |
| 121 | + //$this->_init(); | |
| 119 | 122 | // self::$_loaded ++; |
| 120 | 123 | } |
| 121 | 124 | |
| 122 | 125 | public function add_support( $feature, $type = 'yes' ) { |
| @@ -129,9 +132,13 @@ | ||
| 129 | 132 | |
| 130 | 133 | return LP_Global::object_is_support_feature( $this->object_type . '.' . $this->get_type(), $feature, $type ); |
| 131 | 134 | }*/ |
| 132 | 135 | |
| 136 | + /** | |
| 137 | + * @deprecated 4.1.7.3 | |
| 138 | + */ | |
| 133 | 139 | public function get_supports() { |
| 140 | + _deprecated_function( __FUNCTION__, '4.1.7.3' ); | |
| 134 | 141 | if ( empty( LP_Global::$object_support_features ) ) { |
| 135 | 142 | return false; |
| 136 | 143 | } |
| 137 | 144 | |
| @@ -137,12 +144,8 @@ | ||
| 137 | 144 | |
| 138 | 145 | return LP_Global::get_object_supports( $this->object_type . '.' . $this->get_type() ); |
| 139 | 146 | } |
| 140 | 147 | |
| 141 | - public static function get_type_support_answer_options() { | |
| 142 | - | |
| 143 | - } | |
| 144 | - | |
| 145 | 148 | /** |
| 146 | 149 | * Load data for question |
| 147 | 150 | * |
| 148 | 151 | * @throws Exception |
| @@ -326,9 +329,11 @@ | ||
| 326 | 329 | /** |
| 327 | 330 | * @return mixed |
| 328 | 331 | */ |
| 329 | 332 | public function get_explanation() { |
| 330 | - return apply_filters( 'learn-press/question/explanation', do_shortcode( $this->get_data( 'explanation' ) ), $this->get_id() ); | |
| 333 | + $explanation = apply_filters( 'learn-press/question/explanation', do_shortcode( $this->get_data( 'explanation' ) ), $this->get_id() ); | |
| 334 | + | |
| 335 | + return Template::sanitize_html_content( (string) $explanation ); | |
| 331 | 336 | } |
| 332 | 337 | |
| 333 | 338 | /** |
| 334 | 339 | * @param string $hint |
| @@ -340,9 +345,11 @@ | ||
| 340 | 345 | /** |
| 341 | 346 | * @return mixed |
| 342 | 347 | */ |
| 343 | 348 | public function get_hint() { |
| 344 | - return apply_filters( 'learn-press/question/hint', do_shortcode( $this->get_data( 'hint' ) ), $this->get_id() ); | |
| 349 | + $hint = apply_filters( 'learn-press/question/hint', do_shortcode( $this->get_data( 'hint' ) ), $this->get_id() ); | |
| 350 | + | |
| 351 | + return Template::sanitize_html_content( (string) $hint ); | |
| 345 | 352 | } |
| 346 | 353 | |
| 347 | 354 | /** |
| 348 | 355 | * Get all type of questions |
| @@ -392,9 +399,10 @@ | ||
| 392 | 399 | return $updated; |
| 393 | 400 | }*/ |
| 394 | 401 | |
| 395 | 402 | $this->empty_answers(); |
| 396 | - if ( $answer_options = $this->get_data( 'answer_options' ) ) { | |
| 403 | + $answer_options = $this->get_data( 'answer_options' ); | |
| 404 | + if ( $answer_options ) { | |
| 397 | 405 | $question_order = 1; |
| 398 | 406 | $query = "INSERT INTO {$wpdb->prefix}learnpress_question_answers(`question_id`, `order`) VALUES"; |
| 399 | 407 | foreach ( $answer_options as $answer_option ) { |
| 400 | 408 | if ( empty( $answer_option['title'] ) ) { |
| @@ -452,16 +460,18 @@ | ||
| 452 | 460 | * @param $field |
| 453 | 461 | * @param $is_saved |
| 454 | 462 | * |
| 455 | 463 | * @return string |
| 464 | + * @deprecated 4.1.7.3 | |
| 456 | 465 | */ |
| 457 | 466 | public function _filter_meta_box_meta( $meta, $field, $is_saved ) { |
| 458 | - if ( preg_match( '~\[question-content\]~', $field['id'] ) && $field['context'] == 'quiz-list-questions' ) { | |
| 467 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 468 | + /*if ( preg_match( '~\[question-content\]~', $field['id'] ) && $field['context'] == 'quiz-list-questions' ) { | |
| 459 | 469 | $post = get_post( $this->get_id() ); |
| 460 | 470 | $meta = $post->post_content; |
| 461 | 471 | } |
| 462 | 472 | |
| 463 | - return $meta; | |
| 473 | + return $meta;*/ | |
| 464 | 474 | } |
| 465 | 475 | |
| 466 | 476 | /** |
| 467 | 477 | * Set new type of question. |
| @@ -491,19 +501,21 @@ | ||
| 491 | 501 | /** |
| 492 | 502 | * Update ordering of question answers |
| 493 | 503 | * |
| 494 | 504 | * @param array $orders List of answers |
| 505 | + * @deprecated 4.1.7.3 | |
| 495 | 506 | */ |
| 496 | 507 | public function update_answer_orders( $orders ) { |
| 497 | - global $wpdb; | |
| 508 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 509 | + /*global $wpdb; | |
| 498 | 510 | $query = $wpdb->prepare( |
| 499 | 511 | " |
| 500 | - SELECT qa.question_answer_id, qam2.meta_value as `name`, qam.meta_value as `value` | |
| 501 | - FROM {$wpdb->learnpress_question_answers} qa | |
| 502 | - INNER JOIN {$wpdb->learnpress_question_answermeta} qam ON qa.question_answer_id = qam.learnpress_question_answer_id AND qam.meta_key = %s | |
| 503 | - INNER JOIN {$wpdb->learnpress_question_answermeta} qam2 ON qa.question_answer_id = qam2.learnpress_question_answer_id AND qam2.meta_key = %s | |
| 504 | - WHERE qa.question_id = %d | |
| 505 | - ORDER BY `order` | |
| 512 | + SELECT qa.question_answer_id, qam2.meta_value as `name`, qam.meta_value as `value` | |
| 513 | + FROM {$wpdb->learnpress_question_answers} qa | |
| 514 | + INNER JOIN {$wpdb->learnpress_question_answermeta} qam ON qa.question_answer_id = qam.learnpress_question_answer_id AND qam.meta_key = %s | |
| 515 | + INNER JOIN {$wpdb->learnpress_question_answermeta} qam2 ON qa.question_answer_id = qam2.learnpress_question_answer_id AND qam2.meta_key = %s | |
| 516 | + WHERE qa.question_id = %d | |
| 517 | + ORDER BY `order` | |
| 506 | 518 | ", |
| 507 | 519 | 'value', |
| 508 | 520 | 'text', |
| 509 | 521 | $this->get_id() |
| @@ -509,27 +521,27 @@ | ||
| 509 | 521 | $this->get_id() |
| 510 | 522 | ); |
| 511 | 523 | if ( $answers = $wpdb->get_results( $query ) ) { |
| 512 | 524 | $query = " |
| 513 | - UPDATE {$wpdb->learnpress_question_answers} | |
| 514 | - SET `order` = CASE | |
| 515 | - "; | |
| 516 | - for ( $order = 0, $n = sizeof( $orders ); $order < $n; $order ++ ) { | |
| 517 | - $found_answer = false; | |
| 518 | - foreach ( $answers as $answer ) { | |
| 519 | - if ( $answer->value == $orders[ $order ]['value'] && $answer->name == $orders[ $order ]['text'] ) { | |
| 520 | - $found_answer = $answer; | |
| 521 | - break; | |
| 522 | - } | |
| 525 | + UPDATE {$wpdb->learnpress_question_answers} | |
| 526 | + SET `order` = CASE | |
| 527 | + "; | |
| 528 | + for ( $order = 0, $n = sizeof( $orders ); $order < $n; $order ++ ) { | |
| 529 | + $found_answer = false; | |
| 530 | + foreach ( $answers as $answer ) { | |
| 531 | + if ( $answer->value == $orders[ $order ]['value'] && $answer->name == $orders[ $order ]['text'] ) { | |
| 532 | + $found_answer = $answer; | |
| 533 | + break; | |
| 523 | 534 | } |
| 524 | - if ( $found_answer === false ) { | |
| 525 | - continue; | |
| 526 | - } | |
| 527 | - $query .= $wpdb->prepare( 'WHEN question_answer_id = %d THEN %d', $found_answer->question_answer_id, $order + 1 ) . "\n"; | |
| 528 | 535 | } |
| 529 | - $query .= sprintf( 'ELSE `order` END WHERE question_id = %d', $this->get_id() ); | |
| 530 | - $wpdb->query( $query ); | |
| 536 | + if ( $found_answer === false ) { | |
| 537 | + continue; | |
| 538 | + } | |
| 539 | + $query .= $wpdb->prepare( 'WHEN question_answer_id = %d THEN %d', $found_answer->question_answer_id, $order + 1 ) . "\n"; | |
| 531 | 540 | } |
| 541 | + $query .= sprintf( 'ELSE `order` END WHERE question_id = %d', $this->get_id() ); | |
| 542 | + $wpdb->query( $query ); | |
| 543 | + }*/ | |
| 532 | 544 | } |
| 533 | 545 | |
| 534 | 546 | /** |
| 535 | 547 | * Return type of question. |
| @@ -549,9 +561,9 @@ | ||
| 549 | 561 | return learn_press_question_types()[ $this->get_type() ]; |
| 550 | 562 | } |
| 551 | 563 | |
| 552 | 564 | protected function _init() { |
| 553 | - add_filter( 'learn_press_question_answers', array( $this, '_get_default_answers' ), 10, 2 ); | |
| 565 | + //add_filter( 'learn_press_question_answers', array( $this, '_get_default_answers' ), 10, 2 ); | |
| 554 | 566 | } |
| 555 | 567 | |
| 556 | 568 | |
| 557 | 569 | /** |
| @@ -559,16 +571,17 @@ | ||
| 559 | 571 | * @param mixed $answers |
| 560 | 572 | * @param LP_Question $q |
| 561 | 573 | * |
| 562 | 574 | * @return array|bool |
| 575 | + * @deprecated 4.1.7.3 | |
| 563 | 576 | */ |
| 564 | - public function _get_default_answers( $answers = false, $q = null ) { | |
| 577 | + /*public function _get_default_answers( $answers = false, $q = null ) { | |
| 565 | 578 | if ( ! $answers && ( $q && $q->get_id() == $this->get_id() ) ) { |
| 566 | 579 | $answers = $this->get_default_answers(); |
| 567 | 580 | } |
| 568 | 581 | |
| 569 | 582 | return $answers; |
| 570 | - } | |
| 583 | + }*/ | |
| 571 | 584 | |
| 572 | 585 | /** |
| 573 | 586 | * Get default question answer. |
| 574 | 587 | * |
| @@ -616,34 +629,40 @@ | ||
| 616 | 629 | } |
| 617 | 630 | |
| 618 | 631 | |
| 619 | 632 | /** |
| 620 | - * @param string $field | |
| 621 | - * @param string $exclude | |
| 633 | + * Get question answers option. | |
| 622 | 634 | * |
| 623 | - * @return LP_Question_Answers | |
| 635 | + * @since 3.0.0 | |
| 636 | + * @version 4.0.1 | |
| 637 | + * @modify 4.1.7.3 by tungnx | |
| 638 | + * @return LP_Question_Answer_Option[] | |
| 624 | 639 | */ |
| 625 | - public function get_answers( $field = null, $exclude = null ) { | |
| 626 | - $answers = LP_Object_Cache::get( 'answer-options-' . $this->get_id(), 'learn-press/questions' ); | |
| 640 | + public function get_answers(): array { | |
| 641 | + $lp_question_cache = LP_Question_Cache::instance(); | |
| 642 | + $key_cache = "{$this->get_id()}/option_answers"; | |
| 643 | + //$answers = LP_Object_Cache::get( 'answer-options-' . $this->get_id(), 'learn-press/questions' ); | |
| 644 | + $answers_option = $lp_question_cache->get_cache( $key_cache ); | |
| 627 | 645 | |
| 628 | - if ( false === $answers ) { | |
| 629 | - $answers = $this->_curd->load_answer_options( $this->get_id() ); | |
| 646 | + if ( false === $answers_option ) { | |
| 647 | + $answers_option = $this->_curd->load_answer_options( $this->get_id() ); | |
| 630 | 648 | |
| 631 | - if ( ! $answers ) { | |
| 632 | - $answers = $this->get_default_answers(); | |
| 649 | + if ( ! $answers_option ) { | |
| 650 | + $answers_option = $this->get_default_answers(); | |
| 633 | 651 | } |
| 634 | 652 | |
| 635 | - LP_Object_Cache::set( 'answer-options-' . $this->get_id(), $answers, 'learn-press/questions' ); | |
| 636 | - }; | |
| 653 | + $lp_question_cache->set_cache( $key_cache, $answers_option ); | |
| 654 | + } | |
| 637 | 655 | |
| 638 | - if ( $answers ) { | |
| 639 | - $answers = new LP_Question_Answers( $this, $answers ); | |
| 656 | + require_once 'class-lp-question-answers.php'; | |
| 657 | + | |
| 658 | + $answers_option_tmp = []; | |
| 659 | + foreach ( $answers_option as $k => $answer ) { | |
| 660 | + $answer_option = new LP_Question_Answer_Option( $this, $answer ); | |
| 661 | + $answers_option_tmp[] = $answer_option; | |
| 640 | 662 | } |
| 641 | 663 | |
| 642 | - // @deprecated | |
| 643 | - $answers = apply_filters( 'learn_press_question_answers', $answers, $this ); | |
| 644 | - | |
| 645 | - return apply_filters( 'learn-press/questions/answers', $answers, $this->get_id() ); | |
| 664 | + return $answers_option_tmp; | |
| 646 | 665 | } |
| 647 | 666 | |
| 648 | 667 | /** |
| 649 | 668 | * Create default answers. |
| @@ -658,9 +677,9 @@ | ||
| 658 | 677 | foreach ( $answers as $index => $answer ) { |
| 659 | 678 | $answer = array( |
| 660 | 679 | 'question_id' => $this->get_id(), |
| 661 | 680 | 'title' => $answer['title'], |
| 662 | - 'value' => isset( $answer['value'] ) ? $answer['value'] : '', | |
| 681 | + 'value' => $answer['value'] ?? '', | |
| 663 | 682 | 'is_true' => ( $answer['is_true'] == 'yes' ) ? $answer['is_true'] : '', |
| 664 | 683 | 'order' => $index + 1, |
| 665 | 684 | ); |
| 666 | 685 | |
| @@ -698,9 +717,9 @@ | ||
| 698 | 717 | if ( $user && $quiz && $course ) { |
| 699 | 718 | $user_quiz = $user->get_quiz_data( $quiz->get_id(), $course->get_id() ); |
| 700 | 719 | if ( $user_quiz ) { |
| 701 | 720 | $has_checked = $user->has_checked_answer( $this->get_id(), $quiz->get_id(), $course->get_id() ); |
| 702 | - $show_correct = $user_quiz->is_completed() && ( $has_checked || $quiz->get_show_result() ) ? 'yes' : false; | |
| 721 | + $show_correct = $user_quiz->is_completed() && $has_checked ? 'yes' : false; | |
| 703 | 722 | $answered = $user_quiz->get_question_answer( $this->get_id() ); |
| 704 | 723 | $this->set_answered( $answered ); |
| 705 | 724 | } |
| 706 | 725 | } |
| @@ -711,11 +730,13 @@ | ||
| 711 | 730 | /** |
| 712 | 731 | * Get question name. |
| 713 | 732 | * |
| 714 | 733 | * @return string |
| 734 | + * @deprecated 4.1.7.3 | |
| 715 | 735 | */ |
| 716 | 736 | public function get_name() { |
| 717 | - return isset( $this->_options['name'] ) ? $this->_options['name'] : ucfirst( | |
| 737 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 738 | + /*return $this->_options['name'] ?? ucfirst( | |
| 718 | 739 | preg_replace_callback( |
| 719 | 740 | '!_([a-z])!', |
| 720 | 741 | array( |
| 721 | 742 | $this, |
| @@ -722,9 +743,9 @@ | ||
| 722 | 743 | 'sanitize_name_callback', |
| 723 | 744 | ), |
| 724 | 745 | $this->get_type() |
| 725 | 746 | ) |
| 726 | - ); | |
| 747 | + );*/ | |
| 727 | 748 | } |
| 728 | 749 | |
| 729 | 750 | /** |
| 730 | 751 | * @param $matches |
| @@ -818,14 +839,13 @@ | ||
| 818 | 839 | * @param mixed $answered |
| 819 | 840 | * |
| 820 | 841 | * @return bool |
| 821 | 842 | */ |
| 822 | - public function is_selected_option( $answer, $answered = false ) { | |
| 823 | - | |
| 843 | + public function is_selected_option( $answer, $answered = false ): bool { | |
| 824 | 844 | if ( is_array( $answered ) ) { |
| 825 | 845 | $is_selected = in_array( $answer['value'], $answered ); |
| 826 | 846 | } else { |
| 827 | - $is_selected = ( $answer['value'] . '' === $answered . '' ); | |
| 847 | + $is_selected = $answer['value'] === $answered; | |
| 828 | 848 | } |
| 829 | 849 | |
| 830 | 850 | return apply_filters( 'learn-press/question/is-selected-option', $is_selected, $answer, $answered, $this->get_id() ); |
| 831 | 851 | } |
| @@ -835,10 +855,11 @@ | ||
| 835 | 855 | * |
| 836 | 856 | * @param $answer |
| 837 | 857 | * @param $quiz_id |
| 838 | 858 | * @param null $user_id |
| 859 | + * @deprecated 4.2.7.3 | |
| 839 | 860 | */ |
| 840 | - public function save_user_answer( $answer, $quiz_id, $user_id = null ) { | |
| 861 | + /*public function save_user_answer( $answer, $quiz_id, $user_id = null ) { | |
| 841 | 862 | if ( $user_id ) { |
| 842 | 863 | $user = LP_User_Factory::get_user( $user_id ); |
| 843 | 864 | } else { |
| 844 | 865 | $user = learn_press_get_current_user(); |
| @@ -857,9 +878,9 @@ | ||
| 857 | 878 | $question_answers = apply_filters( 'learn_press_update_user_question_answers', $question_answers, $progress->history_id, $user_id, $this, $quiz_id ); |
| 858 | 879 | |
| 859 | 880 | // learn_press_update_user_quiz_meta( $progress->history_id, 'question_answers', $question_answers ); |
| 860 | 881 | } |
| 861 | - } | |
| 882 | + }*/ | |
| 862 | 883 | |
| 863 | 884 | /** |
| 864 | 885 | * Allow check question answer, default disable for True or False and Single choice, override by Multiple choice question. |
| 865 | 886 | * |