PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.10
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.10
4.4.10 4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 All 142 releases
← All changes | inc/question/class-lp-question-fill-in-blanks.php +35 -72 4.1.6.9.2 → 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> &rarr; ' : '';
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' ) ) . '/',
@@ -266,9 +212,9 @@
266 212 $answer['question_answer_id'],
267 213 '_blanks',
268 214 true
269 215 );
270 - $answers[ $k ]['blanks'] = $blanks ? array_values( $blanks ) : array();
216 + $answers[ $k ]['blanks'] = ! empty( $blanks ) ? array_values( $blanks ) : array();
271 217 }
272 218 }
273 219
274 220 return $answers;
@@ -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,22 +363,17 @@
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' );
412 - $total_field_input = count( $blanks );
413 -
369 + foreach ( $answers as $key => $option ) {
370 + $blanks = $option->get_meta( '_blanks' );
414 371 $return['blanks'] = array();
415 372 $return['answered'] = array();
416 373
417 - if ( $blanks ) {
374 + if ( ! empty( $blanks ) ) {
375 + $total_field_input = count( $blanks );
418 376 $total_answer_correct = 0;
419 377
420 378 foreach ( $user_answer as $answer_id => $answer_value ) {
421 379 $answer_id = trim( $answer_id );
@@ -440,17 +398,20 @@
440 398 $return['mark'] = $this->get_mark();
441 399 }
442 400 }
443 401 }
402 + }
444 403
445 - return $return;
446 - }
404 + $question = $this;
405 + $data = compact( 'user_answer', 'answers', 'question' );
406 +
407 + return apply_filters( 'learn-press/question/fill-in-blank/check', $return, $data );
447 408 }
448 409
449 410 /**
450 411 * Check answer fill in blank
451 412 *
452 - * @param array $blank
413 + * @param array $blank
453 414 * @param $user_fill
454 415 *
455 416 * @return bool
456 417 */
@@ -495,9 +456,11 @@
495 456 $blank_correct = strcasecmp( mb_strtolower( $user_fill, 'UTF-8' ), mb_strtolower( $fill, 'UTF-8' ) ) == 0;
496 457 }
497 458 }
498 459
499 - 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 );
500 463 }
501 464 }
502 465 }
503 466