← All changes
|
inc/question/class-lp-question-fill-in-blanks.php
+32
-68
4.1.7
→
4.4.10
View file →
| @@ -92,62 +92,8 @@ | ||
| 92 | 92 | |
| 93 | 93 | return $output; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - /** | |
| 97 | - * @editor tungnnx | |
| 98 | - * @modify 4.1.4 comment - not use - replaced to "match_shortcode_api" function | |
| 99 | - */ | |
| 100 | - /* | |
| 101 | - public function match_shortcode( $content, $answer_id, $show_answer = false, $answered = '' ) { | |
| 102 | - if ( ! empty( $content ) ) { | |
| 103 | - preg_match_all( | |
| 104 | - '/' . get_shortcode_regex( array( 'fib' ) ) . '/', | |
| 105 | - $content, | |
| 106 | - $all_shortcode, | |
| 107 | - PREG_SET_ORDER | |
| 108 | - ); | |
| 109 | - | |
| 110 | - if ( ! empty( $all_shortcode ) ) { | |
| 111 | - foreach ( $all_shortcode as $shortcode ) { | |
| 112 | - $atts = shortcode_parse_atts( $shortcode[0] ); | |
| 113 | - | |
| 114 | - if ( empty( $atts['id'] ) ) { | |
| 115 | - $ida = explode( '=', str_replace( ']', '', $atts[1] ) ); | |
| 116 | - $ids = isset( $ida[1] ) ? str_replace( '"', '', $ida[1] ) : ''; | |
| 117 | - } else { | |
| 118 | - $ids = $atts['id']; | |
| 119 | - } | |
| 120 | - | |
| 121 | - $fill = ! empty( $atts['fill'] ) ? $atts['fill'] : ''; | |
| 122 | - | |
| 123 | - if ( $show_answer ) { | |
| 124 | - $answer = isset( $answered[ $ids ] ) ? $answered[ $ids ] : ''; | |
| 125 | - | |
| 126 | - $is_correct = ''; | |
| 127 | - | |
| 128 | - if ( ! empty( $answer ) ) { | |
| 129 | - $blanks = learn_press_get_question_answer_meta( $answer_id, '_blanks', true ); | |
| 130 | - | |
| 131 | - if ( ! empty( $blanks ) ) { | |
| 132 | - $is_correct = $this->check_answer( $blanks[ $ids ], $answer ) ? 'correct' : 'fail'; | |
| 133 | - } | |
| 134 | - } | |
| 135 | - | |
| 136 | - $answer_html = ( ! empty( $answer ) && $is_correct === 'fail' ) ? '<span class="lp-fib-answered__answer">' . $answer . '</span> → ' : ''; | |
| 137 | - $new_str = '<span class="lp-fib-answered ' . $is_correct . '" id="' . esc_attr( $ids ) . '">' . $answer_html . '<span class="lp-fib-answered__fill">' . $fill . '</span></span>'; | |
| 138 | - } else { | |
| 139 | - $new_str = '<div class="lp-fib-input" style="display: inline-block; width: auto;"><input type="text" data-id="' . esc_attr( $ids ) . '" value="" /></div>'; | |
| 140 | - } | |
| 141 | - | |
| 142 | - $content = str_replace( $shortcode[0], $new_str, $content ); | |
| 143 | - } | |
| 144 | - } | |
| 145 | - } | |
| 146 | - | |
| 147 | - return $content; | |
| 148 | - }*/ | |
| 149 | - | |
| 150 | 96 | public function match_shortcode_api( $content, $answer_id, $show_answer = false, $answered = '' ) { |
| 151 | 97 | if ( ! empty( $content ) ) { |
| 152 | 98 | preg_match_all( |
| 153 | 99 | '/' . get_shortcode_regex( array( 'fib' ) ) . '/', |
| @@ -273,14 +219,31 @@ | ||
| 273 | 219 | |
| 274 | 220 | return $answers; |
| 275 | 221 | } |
| 276 | 222 | |
| 223 | + /** | |
| 224 | + * Clear answer meta of the question | |
| 225 | + * | |
| 226 | + * @param $question_id | |
| 227 | + * | |
| 228 | + * @since 4.0.0 | |
| 229 | + * @version 1.0.1 | |
| 230 | + */ | |
| 277 | 231 | public function clear_question_answer_meta( $question_id ) { |
| 278 | - $question = LP_Question::get_question( $question_id ); | |
| 279 | - $answers = $question->get_answers(); | |
| 232 | + try { | |
| 233 | + $question = LP_Question::get_question( $question_id ); | |
| 234 | + $answers = $question->get_answers(); | |
| 280 | 235 | |
| 281 | - foreach ( $answers as $answer ) { | |
| 282 | - learn_press_delete_question_answer_meta( $answer->get_id(), '_blanks', '', true ); | |
| 236 | + foreach ( $answers as $answer ) { | |
| 237 | + $answer_id = $answer->get_id(); | |
| 238 | + $lp_db = LP_Database::getInstance(); | |
| 239 | + $filter = new LP_Question_Answermeta_Filter(); | |
| 240 | + $filter->collection = $lp_db->tb_lp_question_answermeta; | |
| 241 | + $filter->where[] = $lp_db->wpdb->prepare( "AND learnpress_question_answer_id = %s", $answer_id ); | |
| 242 | + $lp_db->delete_execute( $filter ); | |
| 243 | + } | |
| 244 | + } catch ( Throwable $e ) { | |
| 245 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 283 | 246 | } |
| 284 | 247 | } |
| 285 | 248 | |
| 286 | 249 | /** |
| @@ -400,16 +363,12 @@ | ||
| 400 | 363 | public function check( $user_answer = null ) { |
| 401 | 364 | $return = parent::check(); |
| 402 | 365 | settype( $user_answer, 'array' ); |
| 403 | 366 | $answers = $this->get_answers(); |
| 404 | - $blanks = array(); | |
| 405 | 367 | |
| 406 | 368 | if ( $answers ) { |
| 407 | - /** | |
| 408 | - * @var LP_Question_Answer_Option $answer | |
| 409 | - */ | |
| 410 | - foreach ( $answers as $key => $answer ) { | |
| 411 | - $blanks = $answer->get_meta( '_blanks' ); | |
| 369 | + foreach ( $answers as $key => $option ) { | |
| 370 | + $blanks = $option->get_meta( '_blanks' ); | |
| 412 | 371 | $return['blanks'] = array(); |
| 413 | 372 | $return['answered'] = array(); |
| 414 | 373 | |
| 415 | 374 | if ( ! empty( $blanks ) ) { |
| @@ -439,17 +398,20 @@ | ||
| 439 | 398 | $return['mark'] = $this->get_mark(); |
| 440 | 399 | } |
| 441 | 400 | } |
| 442 | 401 | } |
| 402 | + } | |
| 443 | 403 | |
| 444 | - return $return; | |
| 445 | - } | |
| 404 | + $question = $this; | |
| 405 | + $data = compact( 'user_answer', 'answers', 'question' ); | |
| 406 | + | |
| 407 | + return apply_filters( 'learn-press/question/fill-in-blank/check', $return, $data ); | |
| 446 | 408 | } |
| 447 | 409 | |
| 448 | 410 | /** |
| 449 | 411 | * Check answer fill in blank |
| 450 | 412 | * |
| 451 | - * @param array $blank | |
| 413 | + * @param array $blank | |
| 452 | 414 | * @param $user_fill |
| 453 | 415 | * |
| 454 | 416 | * @return bool |
| 455 | 417 | */ |
| @@ -494,9 +456,11 @@ | ||
| 494 | 456 | $blank_correct = strcasecmp( mb_strtolower( $user_fill, 'UTF-8' ), mb_strtolower( $fill, 'UTF-8' ) ) == 0; |
| 495 | 457 | } |
| 496 | 458 | } |
| 497 | 459 | |
| 498 | - return $blank_correct; | |
| 460 | + $data = compact( 'blank', 'user_fill' ); | |
| 461 | + | |
| 462 | + return apply_filters( 'learn-press/question/fill-in-blank/check_answer', $blank_correct, $data ); | |
| 499 | 463 | } |
| 500 | 464 | } |
| 501 | 465 | } |
| 502 | 466 | |