From bc0c1100c44a3ad431d61e55c539d4c6ba00f567 Mon Sep 17 00:00:00 2001 From: Tungnx Date: Fri, 29 Mar 2024 16:29:20 +0700 Subject: [PATCH] = 4.2.6.4 = ~ Fixed: error romove question_anwsermeta when create new post question. --- .../editor/class-lp-admin-editor-question.php | 2 +- .../editor/class-lp-admin-editor-quiz.php | 2 +- inc/curds/class-lp-question-curd.php | 20 ++++++------- .../class-lp-question-fill-in-blanks.php | 29 +++++++++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/inc/admin/editor/class-lp-admin-editor-question.php b/inc/admin/editor/class-lp-admin-editor-question.php index 6d045fc7a..3a7208df4 100644 --- a/inc/admin/editor/class-lp-admin-editor-question.php +++ b/inc/admin/editor/class-lp-admin-editor-question.php @@ -151,7 +151,7 @@ public function change_question_type( $args = array() ) { } if ( isset( $question ) ) { - $question = $this->question_curd->change_question_type( $question, $type ); + $this->question_curd->change_question_type( $question, $type ); $this->result = $this->get_question_data_to_question_editor( $question, true ); return true; diff --git a/inc/admin/editor/class-lp-admin-editor-quiz.php b/inc/admin/editor/class-lp-admin-editor-quiz.php index 78166fc56..b3fa96cda 100644 --- a/inc/admin/editor/class-lp-admin-editor-quiz.php +++ b/inc/admin/editor/class-lp-admin-editor-quiz.php @@ -261,7 +261,7 @@ public function change_question_type( $args = array() ) { $question = LP_Question::get_question( $question_id ); - $question = $this->question_curd->change_question_type( $question, $type ); + $this->question_curd->change_question_type( $question, $type ); $this->result = $this->get_question_data_to_quiz_editor( $question, true ); diff --git a/inc/curds/class-lp-question-curd.php b/inc/curds/class-lp-question-curd.php index 975cb6db8..ac1520882 100644 --- a/inc/curds/class-lp-question-curd.php +++ b/inc/curds/class-lp-question-curd.php @@ -358,22 +358,24 @@ public function get_quiz( $question_id ) { * @param $question LP_Question * @param $new_type * - * @return bool|int|LP_Question + * @return false|void + * @since 3.0.0 + * @version 1.0.1 */ - public function change_question_type( $question, $new_type ) { + public function change_question_type( &$question, $new_type ) { if ( learn_press_get_post_type( $question->get_id() ) != LP_QUESTION_CPT ) { return false; } $question_id = $question->get_id(); - $old_type = $question->get_type(); - /*if ( $old_type == $new_type ) { - return false; - }*/ + // If not new Question or not change type return + $old_type = get_post_meta( $question_id, '_lp_type', true ); + if ( ! empty( $old_type ) && $old_type === $new_type ) { + return; + } $answer_options = $question->get_data( 'answer_options' ); - update_post_meta( $question_id, '_lp_type', $new_type ); $question->set_type( $new_type ); @@ -406,10 +408,8 @@ public function change_question_type( $question, $new_type ) { LP_Object_Cache::set( 'answer-options-' . $question_id, $answer_options, 'learn-press/questions' ); $new_question->set_data( 'answer_options', $answer_options ); - return $new_question; + $question = $new_question; } - - return false; } /** diff --git a/inc/question/class-lp-question-fill-in-blanks.php b/inc/question/class-lp-question-fill-in-blanks.php index 73ca14372..2247d35e1 100644 --- a/inc/question/class-lp-question-fill-in-blanks.php +++ b/inc/question/class-lp-question-fill-in-blanks.php @@ -220,12 +220,29 @@ public function admin_editor_question_answers( $answers, $question_id, $quiz_id return $answers; } + /** + * Clear answer meta of the question + * + * @param $question_id + * + * @since 4.0.0 + * @version 1.0.1 + */ public function clear_question_answer_meta( $question_id ) { - $question = LP_Question::get_question( $question_id ); - $answers = $question->get_answers(); - - foreach ( $answers as $answer ) { - learn_press_delete_question_answer_meta( $answer->get_id(), '_blanks', '', true ); + try { + $question = LP_Question::get_question( $question_id ); + $answers = $question->get_answers(); + + foreach ( $answers as $answer ) { + $answer_id = $answer->get_id(); + $lp_db = LP_Database::getInstance(); + $filter = new LP_Question_Answermeta_Filter(); + $filter->collection = $lp_db->tb_lp_question_answermeta; + $filter->where[] = $lp_db->wpdb->prepare( "AND learnpress_question_answer_id = %s", $answer_id ); + $lp_db->delete_execute( $filter ); + } + } catch ( Throwable $e ) { + error_log( __METHOD__ . ': ' . $e->getMessage() ); } } @@ -393,7 +410,7 @@ public function check( $user_answer = null ) { /** * Check answer fill in blank * - * @param array $blank + * @param array $blank * @param $user_fill * * @return bool