PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.2.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.2.1
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
learnpress / inc / question / class-lp-question-answers.php

class-lp-question-answers.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.2.1, at inc/question/class-lp-question-answers.php

555 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class LP_Question_Answers
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 3.0.0
8 */
9
10 defined( 'ABSPATH' ) || exit();
11
12 if ( ! class_exists( 'LP_Question_Answers' ) ) {
13
14 /**
15 * Class LP_Question_Answers
16 *
17 * @deprecated 4.1.7.3
18 */
19 class LP_Question_Answers {
20 /**
21 * Answers
22 *
23 * @var array
24 */
25 protected $_answers = array();
26
27 /**
28 * Current position of answers.
29 *
30 * @var int
31 */
32 protected $_position = 0;
33
34 /**
35 * @var bool
36 */
37 protected $_randomize_options = false;
38
39 /**
40 * Original positions of answers (maybe useful later).
41 *
42 * @var bool
43 */
44 protected $_origin_positions = false;
45
46 /**
47 * @var LP_Question
48 */
49 protected $_question = null;
50
51 /**
52 * LP_Question_Answers constructor.
53 *
54 * @param LP_Question $question
55 * @param mixed $raw
56 *
57 * @param $raw
58 */
59 public function __construct( $question, $raw ) {
60 $this->_question = $question;
61 $this->_init( $raw );
62 }
63
64 /**
65 * Set option randomize answer options.
66 *
67 * @param bool $randomize
68 */
69 public function set_randomize_options( $randomize ) {
70 $this->_randomize_options = (bool) $randomize;
71 }
72
73 /**
74 * Get randomize options.
75 *
76 * @return bool
77 */
78 public function get_randomize_options() {
79 return $this->_randomize_options;
80 }
81
82 /**
83 * Init answer options.
84 *
85 * @param array $raw
86 */
87 protected function _init( $raw ) {
88 if ( ! $raw ) {
89 return;
90 }
91
92 foreach ( $raw as $data ) {
93 $key = isset( $data['question_answer_id'] ) ? $data['question_answer_id'] : 0;
94 $answer = new LP_Question_Answer_Option( $this->_question, $data );
95 $this->_answers[ $key ] = $answer;
96 }
97
98 // Keep origin positions
99 $this->_origin_positions = array_keys( $this->_answers );
100
101 // shuffle
102 if ( $this->_randomize_options ) {
103 $this->_shuffle();
104 }
105 }
106
107 /**
108 * @return array
109 */
110 public function get_answers(): array {
111 return $this->_answers;
112 }
113
114 /**
115 * Set new option for an answer
116 *
117 * @param int|array $id
118 * @param mixed $options
119 */
120 public function set_answer_option( $id, $options = '' ) {
121 if ( is_array( $id ) && func_num_args() == 1 ) {
122 $options = $id;
123 foreach ( $options as $id => $opts ) {
124 $this->set_answer_option( $id, $opts );
125 }
126 } else {
127 if ( ! is_a( $options, 'LP_Question_Answer_Option' ) ) {
128 $options = new LP_Question_Answer_Option( $this->_question, $options );
129 }
130 $this->_answers[ $id ] = $options;
131 }
132 }
133
134 public function remove_answer_option( $id ) {
135 if ( isset( $this->_answers[ $id ] ) ) {
136 unset( $this->_answers[ $id ] );
137
138 return true;
139 }
140
141 return false;
142 }
143
144 public function clear_answer_options() {
145 $this->_answers = array();
146
147 return true;
148 }
149
150 /**
151 * @return LP_Question
152 */
153 public function get_question() {
154 return $this->_question;
155 }
156
157 /**
158 * @return int
159 */
160 public function get_question_id() {
161 return $this->_question->get_id();
162 }
163
164 public function get_ids() {
165 return array_keys( $this->_answers );
166 }
167
168 /**
169 * Is exists an answer option with passed offset?
170 *
171 * @param string|int $offset
172 *
173 * @return bool
174 */
175 public function offsetExists( $offset ) {
176 return array_key_exists( $offset, $this->_answers );
177 }
178
179 /**
180 * Get answer option by offset.
181 *
182 * @param mixed $offset
183 *
184 * @return bool|mixed
185 */
186 public function offsetGet( $offset ) {
187 return $this->offsetExists( $offset ) ? $this->_answers[ $offset ] : false;
188 }
189
190 /**
191 * Set answer option by offset.
192 *
193 * @param int|string $offset
194 * @param mixed $value
195 */
196 public function offsetSet( $offset, $value ) {
197 $this->_answers[ $offset ] = $value;
198 }
199
200 /**
201 * Unset answer option by offset.
202 *
203 * @param int|string $offset
204 */
205 public function offsetUnset( $offset ) {
206 if ( $this->offsetExists( $offset ) ) {
207 unset( $this->_answers[ $offset ] );
208 }
209 }
210
211 /**
212 * Reset current position of answer options.
213 */
214 public function rewind() {
215 $this->_position = 0;
216 if ( $this->_randomize_options ) {
217 $this->_shuffle();
218 }
219 }
220
221 /**
222 * @return mixed
223 */
224 public function current() {
225 $values = array_values( $this->_answers );
226
227 return $values[ $this->_position ];
228 }
229
230 /**
231 * @return mixed
232 */
233 public function key() {
234 $keys = array_keys( $this->_answers );
235
236 return $keys[ $this->_position ];// $this->_position;
237 }
238
239 /**
240 * Nex question.
241 */
242 public function next() {
243 ++ $this->_position;
244 }
245
246 /**
247 * @return bool
248 */
249 public function valid() {
250 $values = array_values( $this->_answers );
251
252 return isset( $values[ $this->_position ] );
253 }
254
255 /**
256 * Shuffle options
257 */
258 protected function _shuffle() {
259 LP_Helper::shuffle_assoc( $this->_answers );
260 }
261
262 public function get_class( $more = '' ) {
263 $classes = array( 'answer-options' );
264 if ( $more && is_string( $more ) ) {
265 $more = explode( ' ', $more );
266 }
267
268 if ( $more && is_array( $more ) ) {
269 $classes = array_merge( $classes, $more );
270 }
271 if ( $this->get_question()->show_correct_answers() === 'yes' ) {
272 $classes[] = 'disabled';
273 }
274
275 // sanitize unwanted classes
276 $classes = LP_Helper::sanitize_array( $classes );
277
278 return apply_filters( 'learn-press/question/answer-options/classes', $classes, $this );
279 }
280
281 /**
282 * Return an answer from a position.
283 *
284 * @param int $at
285 *
286 * @return LP_Question_Answer_Option|mixed
287 */
288 public function get_answer_at( $at ) {
289 $position = 0;
290 foreach ( $this->_answers as $answer ) {
291 if ( $position == $at ) {
292 return $answer;
293 }
294 $position ++;
295 }
296
297 return false;
298 }
299
300 /**
301 * @param string $more
302 * @deprecated 4.1.7.3
303 */
304 public function answers_class( $more = '' ) {
305 $classes = $this->get_class( $more );
306 echo 'class="' . join( ' ', $classes ) . '"';
307 }
308 }
309 }
310
311 if ( ! class_exists( 'LP_Question_Answer_Option' ) ) {
312 /**
313 * Class LP_Question_Answer_Option
314 *
315 * @since 3.0.0
316 */
317 class LP_Question_Answer_Option {
318 /**
319 * Option data
320 *
321 * @var array
322 */
323 protected $_data = null;
324
325 /**
326 * @var LP_Question
327 */
328 protected $_question = null;
329
330 /**
331 * LP_Question_Answer_Option constructor.
332 *
333 * @param LP_Question $question
334 * @param mixed $data
335 */
336 public function __construct( $question, $data ) {
337 $this->_data = $data;
338 $this->_question = $question;
339 }
340
341 /**
342 * Get option title.
343 *
344 * @param string $context
345 *
346 * @return string
347 */
348 public function get_title( $context = '' ) {
349 $title = array_key_exists( 'title', $this->_data ) ? $this->_data['title'] : '';
350 if ( $context == 'display' ) {
351 $title = do_shortcode( $title );
352 }
353
354 return apply_filters( 'learn-press/question/option-title', $title, $this );
355 }
356
357 /**
358 * Get option value.
359 *
360 * @return string
361 */
362 public function get_value() {
363 $value = array_key_exists( 'value', $this->_data ) ? $this->_data['value'] : '';
364
365 return apply_filters( 'learn-press/question/option-value', $value, $this );
366 }
367
368 /**
369 * Return true if option is TRUE
370 *
371 * @return bool
372 */
373 public function is_true(): bool {
374 return apply_filters( 'learn-press/question/option-is-true', array_key_exists( 'is_true', $this->_data ) && $this->_data['is_true'] === 'yes', $this );
375 }
376
377 public function get_id() {
378 return $this->_data['question_answer_id'] ?? 0;
379 }
380
381 /**
382 * CSS class for option
383 *
384 * @param string $more
385 *
386 * @return array
387 */
388 public function get_class( $more = '' ) {
389 $classes = array( 'answer-option' );
390 if ( $more && is_string( $more ) ) {
391 $more = explode( ' ', $more );
392 }
393
394 if ( $more && is_array( $more ) ) {
395 $classes = array_merge( $classes, $more );
396 }
397 $is_checked = $this->is_checked();
398 $is_true = $this->is_true();
399
400 if ( $this->get_question()->show_correct_answers() === 'yes' ) {
401
402 if ( $is_true ) {
403 $classes[] = 'answer-correct';
404 }
405
406 if ( $is_checked ) {
407 $classes[] = 'answer-selected';
408 }
409
410 if ( $is_checked && $is_true ) {
411 $classes[] = 'answered-correct';
412 } elseif ( $is_checked && ! $is_true ) {
413 $classes[] = 'answered-wrong';
414 } elseif ( ! $is_checked && $is_true ) {
415 $classes[] = 'answered-wrong';
416 }
417 }
418 /*elseif ( learn_press_is_review_questions() ) {
419 if ( $is_checked && $is_true ) {
420 $classes[] = 'answered-correct';
421 } elseif ( $is_checked && ! $is_true ) {
422 $classes[] = 'answered-wrong';
423 }
424 }*/
425
426 // sanitize unwanted classes
427 $classes = LP_Helper::sanitize_array( $classes );
428
429 return apply_filters( 'learn-press/question/answer-option/classes', $classes, $this );
430 }
431
432 /**
433 * @param bool $echo
434 *
435 * @return string
436 */
437 public function checked( $echo = true ) {
438 return checked( $this->is_checked(), true, $echo );
439 }
440
441 /**
442 * @param bool $echo
443 *
444 * @return string
445 */
446 public function disabled( $echo = true ) {
447 $q = $this->_question;
448 $disabled = ( $q->show_correct_answers() === 'yes' ) || ( $q->disable_answers() === 'yes' );
449
450 return disabled( $disabled, true, $echo );
451 }
452
453 /**
454 * @return array|mixed
455 */
456 public function get_answered() {
457 return $this->get_question()->get_answered();
458 }
459
460 /**
461 * @return bool
462 */
463 public function is_checked() {
464 if ( false === $this->get_answered() ) {
465 if ( $this->get_question()->show_correct_answers() === 'yes' && $this->is_true() ) {
466
467 return true;
468 }
469
470 return false;
471 }
472
473 return in_array( $this->get_value(), (array) $this->get_answered() );
474 }
475
476
477 /**
478 * @return bool|LP_Question
479 */
480 public function get_question() {
481 return $this->_question;
482 }
483
484 /**
485 * @return int
486 */
487 public function get_question_id() {
488 return $this->_question->get_id();
489 }
490
491 /**
492 * Print class attribute
493 *
494 * @param string $more
495 */
496 public function option_class( $more = '' ) {
497 echo 'class="' . join( ' ', $this->get_class( $more ) ) . '"';
498 }
499
500 public function get_data() {
501 return $this->_data;
502 }
503
504 public function get_meta( $key, $single = true ) {
505 return learn_press_get_question_answer_meta( $this->get_id(), $key, $single );
506 }
507
508 public function __toString() {
509 return (string) $this->_data['title'];
510 }
511
512 /**
513 * Backward compatibility for accessing property as array.
514 *
515 * @param mixed $offset
516 *
517 * @return bool
518 */
519 public function offsetExists( $offset ) {
520 return array_key_exists( $offset, $this->_data );
521 }
522
523 /**
524 * Backward compatibility for get property as array.
525 *
526 * @param mixed $offset
527 *
528 * @return mixed
529 */
530 public function offsetGet( $offset ) {
531 return $this->offsetExists( $offset ) ? $this->_data[ $offset ] : false;
532 }
533
534 /**
535 * Backward compatibility for set property as array.
536 *
537 * @param mixed $offset
538 */
539 public function offsetSet( $offset, $value ) {
540 $this->_data[ $offset ] = $value;
541 }
542
543 /**
544 * Backward compatibility for unset property as array.
545 *
546 * @param mixed $offset
547 */
548 public function offsetUnset( $offset ) {
549 if ( $this->offsetExists( $offset ) ) {
550 unset( $this->_data[ $offset ] );
551 }
552 }
553 }
554 }
555