| @@ -24,9 +24,9 @@ | ||
| 24 | 24 | * LP_Question_CURD constructor. |
| 25 | 25 | */ |
| 26 | 26 | public function __construct() { |
| 27 | 27 | $this->_error_messages = array( |
| 28 | - 'QUESTION_NOT_EXISTS' => __( 'Question does not exist.', 'learnpress' ), | |
| 28 | + 'QUESTION_NOT_EXISTS' => __( 'The question does not exist.', 'learnpress' ), | |
| 29 | 29 | ); |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /** |
| @@ -84,16 +84,16 @@ | ||
| 84 | 84 | |
| 85 | 85 | $question = LP_Question::get_question( $question_id, array( 'type' => $args['type'] ) ); |
| 86 | 86 | $question->set_type( $args['type'] ); |
| 87 | 87 | |
| 88 | + // add question to quiz | |
| 89 | + if ( ! empty( $args['quiz_id'] ) ) { | |
| 90 | + $quiz_curd = new LP_Quiz_CURD(); | |
| 91 | + $quiz_curd->add_question( $args['quiz_id'], $question_id, $args['order'] ); | |
| 92 | + } | |
| 93 | + | |
| 88 | 94 | if ( $args['create_answers'] ) { |
| 89 | 95 | $question->create_default_answers(); |
| 90 | - | |
| 91 | - // add question to quiz | |
| 92 | - if ( ! empty( $args['quiz_id'] ) ) { | |
| 93 | - $quiz_curd = new LP_Quiz_CURD(); | |
| 94 | - $quiz_curd->add_question( $args['quiz_id'], $question_id, $args['order'] ); | |
| 95 | - } | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | do_action( 'learn-press/after-create-question', $question ); |
| 99 | 99 | |
| @@ -146,9 +146,9 @@ | ||
| 146 | 146 | * @return mixed|WP_Error |
| 147 | 147 | */ |
| 148 | 148 | public function duplicate( &$question_id, $args = array() ) { |
| 149 | 149 | if ( ! $question_id ) { |
| 150 | - return new WP_Error( 'lp/question/curd/duplicate/err', 'Op! ID not found' ); | |
| 150 | + return new WP_Error( 'lp/question/curd/duplicate/err', 'Oops! ID not found' ); | |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | if ( learn_press_get_post_type( $question_id ) != LP_QUESTION_CPT ) { |
| 154 | 154 | return new WP_Error( 'lp/question/curd/duplicate/err', 'Op! The question does not exist' ); |
| @@ -165,9 +165,9 @@ | ||
| 165 | 165 | // duplicate question |
| 166 | 166 | $new_question_id = learn_press_duplicate_post( $question_id, array( 'post_status' => 'publish' ) ); |
| 167 | 167 | |
| 168 | 168 | if ( ! $new_question_id || is_wp_error( $new_question_id ) ) { |
| 169 | - return new WP_Error( 'lp/question/curd/duplicate/err', 'Sorry! Failed to duplicate question!' ); | |
| 169 | + return new WP_Error( 'lp/question/curd/duplicate/err', 'Sorry! Failed to duplicate the question!' ); | |
| 170 | 170 | } else { |
| 171 | 171 | |
| 172 | 172 | // init new question |
| 173 | 173 | $new_question = LP_Question::get_question( $new_question_id ); |
| @@ -204,9 +204,9 @@ | ||
| 204 | 204 | try { |
| 205 | 205 | // Get all answer of question |
| 206 | 206 | $filter_get_answer_options = new LP_Question_Answers_Filter(); |
| 207 | 207 | $filter_get_answer_options->question_ids = [ $question_id ]; |
| 208 | - $answer_options = $lp_question_answers_db->get_question_asnwers( $filter_get_answer_options ); | |
| 208 | + $answer_options = $lp_question_answers_db->get_question_answers( $filter_get_answer_options ); | |
| 209 | 209 | |
| 210 | 210 | if ( $answer_options ) { |
| 211 | 211 | foreach ( $answer_options as $answer_option ) { |
| 212 | 212 | $question_answer_id = $answer_option->question_answer_id; |
| @@ -232,9 +232,9 @@ | ||
| 232 | 232 | $filter_question_answer_id = new LP_Question_Answers_Filter(); |
| 233 | 233 | $filter_question_answer_id->only_fields = [ 'MAX(question_answer_id)' ]; |
| 234 | 234 | $filter_question_answer_id->question_ids = [ $question_id_clone ]; |
| 235 | 235 | $filter_question_answer_id->return_string_query = true; |
| 236 | - $question_answer_id_query = $lp_question_answers_db->get_question_asnwers( $filter_question_answer_id ); | |
| 236 | + $question_answer_id_query = $lp_question_answers_db->get_question_answers( $filter_question_answer_id ); | |
| 237 | 237 | $question_answer_id_new = (int) $lp_question_answers_db->wpdb->get_var( $question_answer_id_query ); |
| 238 | 238 | |
| 239 | 239 | if ( ! $question_answer_id_new ) { |
| 240 | 240 | throw new Exception( __( 'Failed to duplicate answer', 'learnpress' ) ); |
| @@ -282,14 +282,10 @@ | ||
| 282 | 282 | if ( ! $id || ! in_array( learn_press_get_post_type( $id ), array( 'revision', LP_QUESTION_CPT ) ) ) { |
| 283 | 283 | throw new Exception( sprintf( __( 'Invalid question with ID "%d".', 'learnpress' ), $id ) ); |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - $question->set_data_via_methods( | |
| 287 | - array( | |
| 288 | - 'explanation' => get_post_meta( $id, '_lp_explanation', true ), | |
| 289 | - 'hint' => get_post_meta( $id, '_lp_hint', true ), | |
| 290 | - ) | |
| 291 | - ); | |
| 286 | + $question->set_explanation( get_post_meta( $id, '_lp_explanation', true ) ); | |
| 287 | + $question->set_hint( get_post_meta( $id, '_lp_hint', true ) ); | |
| 292 | 288 | // $this->_load_answer_options( $question ); |
| 293 | 289 | $this->_load_meta( $question ); |
| 294 | 290 | |
| 295 | 291 | return true; |
| @@ -305,13 +301,9 @@ | ||
| 305 | 301 | } |
| 306 | 302 | // $question->set_type( $type ); |
| 307 | 303 | |
| 308 | 304 | $mark = $this->_get_question_mark( $question->get_id() ); |
| 309 | - $question->set_data_via_methods( | |
| 310 | - array( | |
| 311 | - 'mark' => $mark, | |
| 312 | - ) | |
| 313 | - ); | |
| 305 | + $question->set_mark( $mark ); | |
| 314 | 306 | } |
| 315 | 307 | |
| 316 | 308 | public function _get_question_mark( $question_id ) { |
| 317 | 309 | |
| @@ -357,24 +349,26 @@ | ||
| 357 | 349 | * |
| 358 | 350 | * @param $question LP_Question |
| 359 | 351 | * @param $new_type |
| 360 | 352 | * |
| 361 | - * @return bool|int|LP_Question | |
| 353 | + * @return false|void | |
| 354 | + * @since 3.0.0 | |
| 355 | + * @version 1.0.1 | |
| 362 | 356 | */ |
| 363 | - public function change_question_type( $question, $new_type ) { | |
| 357 | + public function change_question_type( &$question, $new_type ) { | |
| 364 | 358 | if ( learn_press_get_post_type( $question->get_id() ) != LP_QUESTION_CPT ) { |
| 365 | 359 | return false; |
| 366 | 360 | } |
| 367 | 361 | |
| 368 | 362 | $question_id = $question->get_id(); |
| 369 | - $old_type = $question->get_type(); | |
| 370 | 363 | |
| 371 | - /*if ( $old_type == $new_type ) { | |
| 372 | - return false; | |
| 373 | - }*/ | |
| 364 | + // If not new Question or not change type return | |
| 365 | + $old_type = get_post_meta( $question_id, '_lp_type', true ); | |
| 366 | + if ( ! empty( $old_type ) && $old_type === $new_type ) { | |
| 367 | + return; | |
| 368 | + } | |
| 374 | 369 | |
| 375 | 370 | $answer_options = $question->get_data( 'answer_options' ); |
| 376 | - | |
| 377 | 371 | update_post_meta( $question_id, '_lp_type', $new_type ); |
| 378 | 372 | $question->set_type( $new_type ); |
| 379 | 373 | |
| 380 | 374 | $new_question = LP_Question::get_question( $question_id, array( 'force' => true ) ); |
| @@ -405,12 +399,10 @@ | ||
| 405 | 399 | |
| 406 | 400 | LP_Object_Cache::set( 'answer-options-' . $question_id, $answer_options, 'learn-press/questions' ); |
| 407 | 401 | $new_question->set_data( 'answer_options', $answer_options ); |
| 408 | 402 | |
| 409 | - return $new_question; | |
| 403 | + $question = $new_question; | |
| 410 | 404 | } |
| 411 | - | |
| 412 | - return false; | |
| 413 | 405 | } |
| 414 | 406 | |
| 415 | 407 | /** |
| 416 | 408 | * Update answer title |
| @@ -432,10 +424,10 @@ | ||
| 432 | 424 | 'data' => apply_filters( |
| 433 | 425 | 'learn-press/question/update-answer-data', |
| 434 | 426 | array( |
| 435 | 427 | 'title' => $answer['title'], |
| 436 | - 'value' => isset( $answer['value'] ) ? $answer['value'] : '', | |
| 437 | - 'is_true' => isset( $answer['is_true'] ) ? $answer['is_true'] : '', | |
| 428 | + 'value' => $answer['value'] ?? '', | |
| 429 | + 'is_true' => $answer['is_true'] ?? '', | |
| 438 | 430 | ) |
| 439 | 431 | ), |
| 440 | 432 | 'where' => array( |
| 441 | 433 | 'question_answer_id' => $answer['question_answer_id'], |
| @@ -454,15 +446,15 @@ | ||
| 454 | 446 | // Update for Fill in Blanks. |
| 455 | 447 | if ( ! empty( $answer['blanks'] ) ) { |
| 456 | 448 | $blanks = $answer['blanks']; |
| 457 | 449 | |
| 458 | - if ( is_array( $blanks ) ) { | |
| 450 | + /*if ( is_array( $blanks ) ) { | |
| 459 | 451 | $question = LP_Question::get_question( $question_id ); |
| 460 | 452 | |
| 461 | 453 | foreach ( $blanks as $id => $blank ) { |
| 462 | 454 | $question->_blanks[ $blank['id'] ] = $blank; |
| 463 | 455 | } |
| 464 | - } | |
| 456 | + }*/ | |
| 465 | 457 | |
| 466 | 458 | learn_press_update_question_answer_meta( $answer['question_answer_id'], '_blanks', $blanks ); |
| 467 | 459 | } |
| 468 | 460 | |
| @@ -612,29 +604,23 @@ | ||
| 612 | 604 | $answers = $question->get_data( 'answer_options' ); |
| 613 | 605 | |
| 614 | 606 | global $wpdb; |
| 615 | 607 | |
| 616 | - // delete all answer in question | |
| 617 | - if ( $force ) { | |
| 618 | - $delete = $wpdb->delete( | |
| 619 | - $wpdb->learnpress_question_answers, | |
| 620 | - array( 'question_id' => $question_id ) | |
| 621 | - ); | |
| 608 | + // Delete answer meta | |
| 609 | + $wpdb->delete( | |
| 610 | + $wpdb->learnpress_question_answermeta, | |
| 611 | + array( 'learnpress_question_answer_id' => $answer_id ) | |
| 612 | + ); | |
| 622 | 613 | |
| 623 | - if ( $delete ) { | |
| 624 | - $question->set_data( 'answer_options', '' ); | |
| 625 | - } | |
| 626 | - } else { | |
| 627 | - $delete = $wpdb->delete( | |
| 628 | - $wpdb->learnpress_question_answers, | |
| 629 | - array( 'question_answer_id' => $answer_id ) | |
| 630 | - ); | |
| 631 | - if ( $delete ) { | |
| 632 | - unset( $answers[ $answer_id ] ); | |
| 633 | - $question->set_data( 'answer_options', $answers ); | |
| 614 | + $delete = $wpdb->delete( | |
| 615 | + $wpdb->learnpress_question_answers, | |
| 616 | + array( 'question_answer_id' => $answer_id ) | |
| 617 | + ); | |
| 618 | + if ( $delete ) { | |
| 619 | + unset( $answers[ $answer_id ] ); | |
| 620 | + $question->set_data( 'answer_options', $answers ); | |
| 634 | 621 | |
| 635 | - $this->sort_answers( $question_id, array_keys( $answers ) ); | |
| 636 | - } | |
| 622 | + $this->sort_answers( $question_id, array_keys( $answers ) ); | |
| 637 | 623 | } |
| 638 | 624 | |
| 639 | 625 | return $delete; |
| 640 | 626 | } |
| @@ -967,44 +953,8 @@ | ||
| 967 | 953 | return $answer_options; |
| 968 | 954 | } |
| 969 | 955 | |
| 970 | 956 | /** |
| 971 | - * Load answer options for the question from database. | |
| 972 | - * Load from cache if data is already loaded into cache. | |
| 973 | - * Otherwise, load from database and put to cache. | |
| 974 | - * | |
| 975 | - * @param $question LP_Question | |
| 976 | - * @depecated 4.1.7 | |
| 977 | - */ | |
| 978 | - /*protected function _load_answer_options( &$question ) { | |
| 979 | - $id = $question->get_id(); | |
| 980 | - $answer_options = LP_Object_Cache::get( 'answer-options-' . $id, 'lp-questions' ); | |
| 981 | - | |
| 982 | - if ( false === $answer_options ) { | |
| 983 | - global $wpdb; | |
| 984 | - $query = $wpdb->prepare( | |
| 985 | - " | |
| 986 | - SELECT * | |
| 987 | - FROM {$wpdb->prefix}learnpress_question_answers | |
| 988 | - WHERE question_id = %d | |
| 989 | - ORDER BY `order` ASC | |
| 990 | - ", | |
| 991 | - $id | |
| 992 | - ); | |
| 993 | - | |
| 994 | - $answer_options = $this->load_answer_options( $question->get_id() ); | |
| 995 | - } | |
| 996 | - $answer_options = apply_filters( 'learn-press/question/load-answer-options', $answer_options, $id ); | |
| 997 | - | |
| 998 | - if ( ! empty( $answer_options['question_answer_id'] ) && $answer_options['question_answer_id'] > 0 ) { | |
| 999 | - $this->_load_answer_option_meta( $answer_options ); | |
| 1000 | - } | |
| 1001 | - LP_Object_Cache::set( 'answer-options-' . $id, $answer_options, 'lp-questions' ); | |
| 1002 | - | |
| 1003 | - $question->set_data( 'answer_options', $answer_options ); | |
| 1004 | - }*/ | |
| 1005 | - | |
| 1006 | - /** | |
| 1007 | 957 | * Load question answers |
| 1008 | 958 | * |
| 1009 | 959 | * @updated 3.1.0 |
| 1010 | 960 | * |
| @@ -1035,65 +985,7 @@ | ||
| 1035 | 985 | LP_Object_Cache::set( 'question-' . $question_id, $answer_options, 'question-answers' ); |
| 1036 | 986 | } |
| 1037 | 987 | |
| 1038 | 988 | return $answer_options; |
| 1039 | - } | |
| 1040 | - | |
| 1041 | - /** | |
| 1042 | - * Load meta data for answer options. | |
| 1043 | - * | |
| 1044 | - * @param array $answer_options | |
| 1045 | - * | |
| 1046 | - * @return mixed; | |
| 1047 | - * @depecated 4.1.7 | |
| 1048 | - */ | |
| 1049 | - /*protected function _load_answer_option_meta( &$answer_options ) { | |
| 1050 | - if ( ! $answer_options ) { | |
| 1051 | - return false; | |
| 1052 | - } | |
| 1053 | - | |
| 1054 | - $answer_option_ids = wp_list_pluck( $answer_options, 'question_answer_id' ); | |
| 1055 | - $format = array_fill( 0, sizeof( $answer_option_ids ), '%d' ); | |
| 1056 | - $query = $wpdb->prepare( | |
| 1057 | - " | |
| 1058 | - SELECT * | |
| 1059 | - FROM {$wpdb->prefix}learnpress_question_answermeta | |
| 1060 | - WHERE learnpress_question_answer_id IN(" . join( ', ', $format ) . ') | |
| 1061 | - ', | |
| 1062 | - $answer_option_ids | |
| 1063 | - ); | |
| 1064 | - | |
| 1065 | - $metas = $wpdb->get_results( $query ); | |
| 1066 | - | |
| 1067 | - if ( $metas ) { | |
| 1068 | - foreach ( $metas as $meta ) { | |
| 1069 | - $key = $meta->meta_key; | |
| 1070 | - $option_key = $meta->learnpress_question_answer_id; | |
| 1071 | - if ( ! empty( $answer_options[ $option_key ] ) ) { | |
| 1072 | - if ( $key == 'checked' ) { | |
| 1073 | - $key = 'is_true'; | |
| 1074 | - } | |
| 1075 | - $answer_options[ $option_key ][ $key ] = $meta->meta_value; | |
| 1076 | - } | |
| 1077 | - } | |
| 1078 | - } | |
| 1079 | - | |
| 1080 | - return true; | |
| 1081 | - }*/ | |
| 1082 | - | |
| 1083 | - public function add_meta( &$object, $meta ) { | |
| 1084 | - // TODO: Implement add_meta() method. | |
| 1085 | - } | |
| 1086 | - | |
| 1087 | - public function delete_meta( &$object, $meta ) { | |
| 1088 | - // TODO: Implement delete_meta() method. | |
| 1089 | - } | |
| 1090 | - | |
| 1091 | - public function read_meta( &$object ) { | |
| 1092 | - // TODO: Implement read_meta() method. | |
| 1093 | - } | |
| 1094 | - | |
| 1095 | - public function update_meta( &$object, $meta ) { | |
| 1096 | - // TODO: Implement update_meta() method. | |
| 1097 | 989 | } |
| 1098 | 990 | } |
| 1099 | 991 | } |