| @@ -114,9 +114,9 @@ | ||
| 114 | 114 | $this->load(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | $this->_options = $args; |
| 118 | - $this->_init(); | |
| 118 | + //$this->_init(); | |
| 119 | 119 | // self::$_loaded ++; |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | public function add_support( $feature, $type = 'yes' ) { |
| @@ -392,9 +392,10 @@ | ||
| 392 | 392 | return $updated; |
| 393 | 393 | }*/ |
| 394 | 394 | |
| 395 | 395 | $this->empty_answers(); |
| 396 | - if ( $answer_options = $this->get_data( 'answer_options' ) ) { | |
| 396 | + $answer_options = $this->get_data( 'answer_options' ); | |
| 397 | + if ( $answer_options ) { | |
| 397 | 398 | $question_order = 1; |
| 398 | 399 | $query = "INSERT INTO {$wpdb->prefix}learnpress_question_answers(`question_id`, `order`) VALUES"; |
| 399 | 400 | foreach ( $answer_options as $answer_option ) { |
| 400 | 401 | if ( empty( $answer_option['title'] ) ) { |
| @@ -452,16 +453,18 @@ | ||
| 452 | 453 | * @param $field |
| 453 | 454 | * @param $is_saved |
| 454 | 455 | * |
| 455 | 456 | * @return string |
| 457 | + * @deprecated 4.1.7.3 | |
| 456 | 458 | */ |
| 457 | 459 | public function _filter_meta_box_meta( $meta, $field, $is_saved ) { |
| 458 | - if ( preg_match( '~\[question-content\]~', $field['id'] ) && $field['context'] == 'quiz-list-questions' ) { | |
| 460 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 461 | + /*if ( preg_match( '~\[question-content\]~', $field['id'] ) && $field['context'] == 'quiz-list-questions' ) { | |
| 459 | 462 | $post = get_post( $this->get_id() ); |
| 460 | 463 | $meta = $post->post_content; |
| 461 | 464 | } |
| 462 | 465 | |
| 463 | - return $meta; | |
| 466 | + return $meta;*/ | |
| 464 | 467 | } |
| 465 | 468 | |
| 466 | 469 | /** |
| 467 | 470 | * Set new type of question. |
| @@ -491,19 +494,21 @@ | ||
| 491 | 494 | /** |
| 492 | 495 | * Update ordering of question answers |
| 493 | 496 | * |
| 494 | 497 | * @param array $orders List of answers |
| 498 | + * @deprecated 4.1.7.3 | |
| 495 | 499 | */ |
| 496 | 500 | public function update_answer_orders( $orders ) { |
| 497 | - global $wpdb; | |
| 501 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 502 | + /*global $wpdb; | |
| 498 | 503 | $query = $wpdb->prepare( |
| 499 | 504 | " |
| 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` | |
| 505 | + SELECT qa.question_answer_id, qam2.meta_value as `name`, qam.meta_value as `value` | |
| 506 | + FROM {$wpdb->learnpress_question_answers} qa | |
| 507 | + INNER JOIN {$wpdb->learnpress_question_answermeta} qam ON qa.question_answer_id = qam.learnpress_question_answer_id AND qam.meta_key = %s | |
| 508 | + INNER JOIN {$wpdb->learnpress_question_answermeta} qam2 ON qa.question_answer_id = qam2.learnpress_question_answer_id AND qam2.meta_key = %s | |
| 509 | + WHERE qa.question_id = %d | |
| 510 | + ORDER BY `order` | |
| 506 | 511 | ", |
| 507 | 512 | 'value', |
| 508 | 513 | 'text', |
| 509 | 514 | $this->get_id() |
| @@ -509,11 +514,11 @@ | ||
| 509 | 514 | $this->get_id() |
| 510 | 515 | ); |
| 511 | 516 | if ( $answers = $wpdb->get_results( $query ) ) { |
| 512 | 517 | $query = " |
| 513 | - UPDATE {$wpdb->learnpress_question_answers} | |
| 514 | - SET `order` = CASE | |
| 515 | - "; | |
| 518 | + UPDATE {$wpdb->learnpress_question_answers} | |
| 519 | + SET `order` = CASE | |
| 520 | + "; | |
| 516 | 521 | for ( $order = 0, $n = sizeof( $orders ); $order < $n; $order ++ ) { |
| 517 | 522 | $found_answer = false; |
| 518 | 523 | foreach ( $answers as $answer ) { |
| 519 | 524 | if ( $answer->value == $orders[ $order ]['value'] && $answer->name == $orders[ $order ]['text'] ) { |
| @@ -527,9 +532,9 @@ | ||
| 527 | 532 | $query .= $wpdb->prepare( 'WHEN question_answer_id = %d THEN %d', $found_answer->question_answer_id, $order + 1 ) . "\n"; |
| 528 | 533 | } |
| 529 | 534 | $query .= sprintf( 'ELSE `order` END WHERE question_id = %d', $this->get_id() ); |
| 530 | 535 | $wpdb->query( $query ); |
| 531 | - } | |
| 536 | + }*/ | |
| 532 | 537 | } |
| 533 | 538 | |
| 534 | 539 | /** |
| 535 | 540 | * Return type of question. |
| @@ -549,9 +554,9 @@ | ||
| 549 | 554 | return learn_press_question_types()[ $this->get_type() ]; |
| 550 | 555 | } |
| 551 | 556 | |
| 552 | 557 | protected function _init() { |
| 553 | - add_filter( 'learn_press_question_answers', array( $this, '_get_default_answers' ), 10, 2 ); | |
| 558 | + //add_filter( 'learn_press_question_answers', array( $this, '_get_default_answers' ), 10, 2 ); | |
| 554 | 559 | } |
| 555 | 560 | |
| 556 | 561 | |
| 557 | 562 | /** |
| @@ -559,16 +564,17 @@ | ||
| 559 | 564 | * @param mixed $answers |
| 560 | 565 | * @param LP_Question $q |
| 561 | 566 | * |
| 562 | 567 | * @return array|bool |
| 568 | + * @deprecated 4.1.7.3 | |
| 563 | 569 | */ |
| 564 | - public function _get_default_answers( $answers = false, $q = null ) { | |
| 570 | + /*public function _get_default_answers( $answers = false, $q = null ) { | |
| 565 | 571 | if ( ! $answers && ( $q && $q->get_id() == $this->get_id() ) ) { |
| 566 | 572 | $answers = $this->get_default_answers(); |
| 567 | 573 | } |
| 568 | 574 | |
| 569 | 575 | return $answers; |
| 570 | - } | |
| 576 | + }*/ | |
| 571 | 577 | |
| 572 | 578 | /** |
| 573 | 579 | * Get default question answer. |
| 574 | 580 | * |
| @@ -616,34 +622,40 @@ | ||
| 616 | 622 | } |
| 617 | 623 | |
| 618 | 624 | |
| 619 | 625 | /** |
| 620 | - * @param string $field | |
| 621 | - * @param string $exclude | |
| 626 | + * Get question answers option. | |
| 622 | 627 | * |
| 623 | - * @return LP_Question_Answers | |
| 628 | + * @since 3.0.0 | |
| 629 | + * @version 4.0.1 | |
| 630 | + * @modify 4.1.7.3 by tungnx | |
| 631 | + * @return LP_Question_Answer_Option[] | |
| 624 | 632 | */ |
| 625 | - public function get_answers( $field = null, $exclude = null ) { | |
| 626 | - $answers = LP_Object_Cache::get( 'answer-options-' . $this->get_id(), 'learn-press/questions' ); | |
| 633 | + public function get_answers(): array { | |
| 634 | + $lp_question_cache = LP_Question_Cache::instance(); | |
| 635 | + $key_cache = "{$this->get_id()}/option_answers"; | |
| 636 | + //$answers = LP_Object_Cache::get( 'answer-options-' . $this->get_id(), 'learn-press/questions' ); | |
| 637 | + $answers_option = $lp_question_cache->get_cache( $key_cache ); | |
| 627 | 638 | |
| 628 | - if ( false === $answers ) { | |
| 629 | - $answers = $this->_curd->load_answer_options( $this->get_id() ); | |
| 639 | + if ( false === $answers_option ) { | |
| 640 | + $answers_option = $this->_curd->load_answer_options( $this->get_id() ); | |
| 630 | 641 | |
| 631 | - if ( ! $answers ) { | |
| 632 | - $answers = $this->get_default_answers(); | |
| 642 | + if ( ! $answers_option ) { | |
| 643 | + $answers_option = $this->get_default_answers(); | |
| 633 | 644 | } |
| 634 | 645 | |
| 635 | - LP_Object_Cache::set( 'answer-options-' . $this->get_id(), $answers, 'learn-press/questions' ); | |
| 636 | - }; | |
| 646 | + $lp_question_cache->set_cache( $key_cache, $answers_option ); | |
| 647 | + } | |
| 637 | 648 | |
| 638 | - if ( $answers ) { | |
| 639 | - $answers = new LP_Question_Answers( $this, $answers ); | |
| 649 | + require_once 'class-lp-question-answers.php'; | |
| 650 | + | |
| 651 | + $answers_option_tmp = []; | |
| 652 | + foreach ( $answers_option as $k => $answer ) { | |
| 653 | + $answer_option = new LP_Question_Answer_Option( $this, $answer ); | |
| 654 | + $answers_option_tmp[] = $answer_option; | |
| 640 | 655 | } |
| 641 | 656 | |
| 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() ); | |
| 657 | + return $answers_option_tmp; | |
| 646 | 658 | } |
| 647 | 659 | |
| 648 | 660 | /** |
| 649 | 661 | * Create default answers. |
| @@ -658,9 +670,9 @@ | ||
| 658 | 670 | foreach ( $answers as $index => $answer ) { |
| 659 | 671 | $answer = array( |
| 660 | 672 | 'question_id' => $this->get_id(), |
| 661 | 673 | 'title' => $answer['title'], |
| 662 | - 'value' => isset( $answer['value'] ) ? $answer['value'] : '', | |
| 674 | + 'value' => $answer['value'] ?? '', | |
| 663 | 675 | 'is_true' => ( $answer['is_true'] == 'yes' ) ? $answer['is_true'] : '', |
| 664 | 676 | 'order' => $index + 1, |
| 665 | 677 | ); |
| 666 | 678 | |
| @@ -698,9 +710,9 @@ | ||
| 698 | 710 | if ( $user && $quiz && $course ) { |
| 699 | 711 | $user_quiz = $user->get_quiz_data( $quiz->get_id(), $course->get_id() ); |
| 700 | 712 | if ( $user_quiz ) { |
| 701 | 713 | $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; | |
| 714 | + $show_correct = $user_quiz->is_completed() && $has_checked ? 'yes' : false; | |
| 703 | 715 | $answered = $user_quiz->get_question_answer( $this->get_id() ); |
| 704 | 716 | $this->set_answered( $answered ); |
| 705 | 717 | } |
| 706 | 718 | } |
| @@ -711,11 +723,13 @@ | ||
| 711 | 723 | /** |
| 712 | 724 | * Get question name. |
| 713 | 725 | * |
| 714 | 726 | * @return string |
| 727 | + * @deprecated 4.1.7.3 | |
| 715 | 728 | */ |
| 716 | 729 | public function get_name() { |
| 717 | - return isset( $this->_options['name'] ) ? $this->_options['name'] : ucfirst( | |
| 730 | + _deprecated_function( __METHOD__, '4.1.7.3' ); | |
| 731 | + /*return $this->_options['name'] ?? ucfirst( | |
| 718 | 732 | preg_replace_callback( |
| 719 | 733 | '!_([a-z])!', |
| 720 | 734 | array( |
| 721 | 735 | $this, |
| @@ -722,9 +736,9 @@ | ||
| 722 | 736 | 'sanitize_name_callback', |
| 723 | 737 | ), |
| 724 | 738 | $this->get_type() |
| 725 | 739 | ) |
| 726 | - ); | |
| 740 | + );*/ | |
| 727 | 741 | } |
| 728 | 742 | |
| 729 | 743 | /** |
| 730 | 744 | * @param $matches |
| @@ -818,14 +832,13 @@ | ||
| 818 | 832 | * @param mixed $answered |
| 819 | 833 | * |
| 820 | 834 | * @return bool |
| 821 | 835 | */ |
| 822 | - public function is_selected_option( $answer, $answered = false ) { | |
| 823 | - | |
| 836 | + public function is_selected_option( $answer, $answered = false ): bool { | |
| 824 | 837 | if ( is_array( $answered ) ) { |
| 825 | 838 | $is_selected = in_array( $answer['value'], $answered ); |
| 826 | 839 | } else { |
| 827 | - $is_selected = ( $answer['value'] . '' === $answered . '' ); | |
| 840 | + $is_selected = $answer['value'] === $answered; | |
| 828 | 841 | } |
| 829 | 842 | |
| 830 | 843 | return apply_filters( 'learn-press/question/is-selected-option', $is_selected, $answer, $answered, $this->get_id() ); |
| 831 | 844 | } |
| @@ -835,10 +848,11 @@ | ||
| 835 | 848 | * |
| 836 | 849 | * @param $answer |
| 837 | 850 | * @param $quiz_id |
| 838 | 851 | * @param null $user_id |
| 852 | + * @deprecated 4.2.7.3 | |
| 839 | 853 | */ |
| 840 | - public function save_user_answer( $answer, $quiz_id, $user_id = null ) { | |
| 854 | + /*public function save_user_answer( $answer, $quiz_id, $user_id = null ) { | |
| 841 | 855 | if ( $user_id ) { |
| 842 | 856 | $user = LP_User_Factory::get_user( $user_id ); |
| 843 | 857 | } else { |
| 844 | 858 | $user = learn_press_get_current_user(); |
| @@ -857,9 +871,9 @@ | ||
| 857 | 871 | $question_answers = apply_filters( 'learn_press_update_user_question_answers', $question_answers, $progress->history_id, $user_id, $this, $quiz_id ); |
| 858 | 872 | |
| 859 | 873 | // learn_press_update_user_quiz_meta( $progress->history_id, 'question_answers', $question_answers ); |
| 860 | 874 | } |
| 861 | - } | |
| 875 | + }*/ | |
| 862 | 876 | |
| 863 | 877 | /** |
| 864 | 878 | * Allow check question answer, default disable for True or False and Single choice, override by Multiple choice question. |
| 865 | 879 | * |