| 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 |
/** |
| 263 |
* @deprecated 4.2.7.6 |
| 264 |
*/ |
| 265 |
/*public function get_class( $more = '' ) { |
| 266 |
$classes = array( 'answer-options' ); |
| 267 |
if ( $more && is_string( $more ) ) { |
| 268 |
$more = explode( ' ', $more ); |
| 269 |
} |
| 270 |
|
| 271 |
if ( $more && is_array( $more ) ) { |
| 272 |
$classes = array_merge( $classes, $more ); |
| 273 |
} |
| 274 |
if ( $this->get_question()->show_correct_answers() === 'yes' ) { |
| 275 |
$classes[] = 'disabled'; |
| 276 |
} |
| 277 |
|
| 278 |
// sanitize unwanted classes |
| 279 |
$classes = LP_Helper::sanitize_array( $classes ); |
| 280 |
|
| 281 |
return apply_filters( 'learn-press/question/answer-options/classes', $classes, $this ); |
| 282 |
}*/ |
| 283 |
|
| 284 |
/** |
| 285 |
* Return an answer from a position. |
| 286 |
* |
| 287 |
* @param int $at |
| 288 |
* |
| 289 |
* @return LP_Question_Answer_Option|mixed |
| 290 |
*/ |
| 291 |
public function get_answer_at( $at ) { |
| 292 |
$position = 0; |
| 293 |
foreach ( $this->_answers as $answer ) { |
| 294 |
if ( $position == $at ) { |
| 295 |
return $answer; |
| 296 |
} |
| 297 |
$position ++; |
| 298 |
} |
| 299 |
|
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @param string $more |
| 305 |
* @deprecated 4.1.7.3 |
| 306 |
*/ |
| 307 |
/*public function answers_class( $more = '' ) { |
| 308 |
$classes = $this->get_class( $more ); |
| 309 |
echo 'class="' . join( ' ', $classes ) . '"'; |
| 310 |
}*/ |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
if ( ! class_exists( 'LP_Question_Answer_Option' ) ) { |
| 315 |
/** |
| 316 |
* Class LP_Question_Answer_Option |
| 317 |
* |
| 318 |
* @since 3.0.0 |
| 319 |
*/ |
| 320 |
class LP_Question_Answer_Option { |
| 321 |
/** |
| 322 |
* Option data |
| 323 |
* |
| 324 |
* @var array |
| 325 |
*/ |
| 326 |
protected $_data = null; |
| 327 |
|
| 328 |
/** |
| 329 |
* @var LP_Question |
| 330 |
*/ |
| 331 |
protected $_question = null; |
| 332 |
|
| 333 |
/** |
| 334 |
* LP_Question_Answer_Option constructor. |
| 335 |
* |
| 336 |
* @param LP_Question $question |
| 337 |
* @param mixed $data |
| 338 |
*/ |
| 339 |
public function __construct( $question, $data ) { |
| 340 |
$this->_data = $data; |
| 341 |
$this->_question = $question; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Get option title. |
| 346 |
* |
| 347 |
* @param string $context |
| 348 |
* |
| 349 |
* @return string |
| 350 |
*/ |
| 351 |
public function get_title( $context = '' ) { |
| 352 |
$title = array_key_exists( 'title', $this->_data ) ? $this->_data['title'] : ''; |
| 353 |
if ( $context == 'display' ) { |
| 354 |
$title = do_shortcode( $title ); |
| 355 |
} |
| 356 |
|
| 357 |
return apply_filters( 'learn-press/question/option-title', $title, $this ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Get option value. |
| 362 |
* |
| 363 |
* @return string |
| 364 |
*/ |
| 365 |
public function get_value() { |
| 366 |
$value = array_key_exists( 'value', $this->_data ) ? $this->_data['value'] : ''; |
| 367 |
|
| 368 |
return apply_filters( 'learn-press/question/option-value', $value, $this ); |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Return true if option is TRUE |
| 373 |
* |
| 374 |
* @return bool |
| 375 |
*/ |
| 376 |
public function is_true(): bool { |
| 377 |
return apply_filters( 'learn-press/question/option-is-true', array_key_exists( 'is_true', $this->_data ) && $this->_data['is_true'] === 'yes', $this ); |
| 378 |
} |
| 379 |
|
| 380 |
public function get_id() { |
| 381 |
return $this->_data['question_answer_id'] ?? 0; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* CSS class for option |
| 386 |
* |
| 387 |
* @param string $more |
| 388 |
* |
| 389 |
* @return array |
| 390 |
* @deprecated 4.2.7.6 |
| 391 |
*/ |
| 392 |
// public function get_class( $more = '' ) { |
| 393 |
// $classes = array( 'answer-option' ); |
| 394 |
// if ( $more && is_string( $more ) ) { |
| 395 |
// $more = explode( ' ', $more ); |
| 396 |
// } |
| 397 |
// |
| 398 |
// if ( $more && is_array( $more ) ) { |
| 399 |
// $classes = array_merge( $classes, $more ); |
| 400 |
// } |
| 401 |
// $is_checked = $this->is_checked(); |
| 402 |
// $is_true = $this->is_true(); |
| 403 |
// |
| 404 |
// if ( $this->get_question()->show_correct_answers() === 'yes' ) { |
| 405 |
// |
| 406 |
// if ( $is_true ) { |
| 407 |
// $classes[] = 'answer-correct'; |
| 408 |
// } |
| 409 |
// |
| 410 |
// if ( $is_checked ) { |
| 411 |
// $classes[] = 'answer-selected'; |
| 412 |
// } |
| 413 |
// |
| 414 |
// if ( $is_checked && $is_true ) { |
| 415 |
// $classes[] = 'answered-correct'; |
| 416 |
// } elseif ( $is_checked && ! $is_true ) { |
| 417 |
// $classes[] = 'answered-wrong'; |
| 418 |
// } elseif ( ! $is_checked && $is_true ) { |
| 419 |
// $classes[] = 'answered-wrong'; |
| 420 |
// } |
| 421 |
// } |
| 422 |
// /*elseif ( learn_press_is_review_questions() ) { |
| 423 |
// if ( $is_checked && $is_true ) { |
| 424 |
// $classes[] = 'answered-correct'; |
| 425 |
// } elseif ( $is_checked && ! $is_true ) { |
| 426 |
// $classes[] = 'answered-wrong'; |
| 427 |
// } |
| 428 |
// }*/ |
| 429 |
// |
| 430 |
// // sanitize unwanted classes |
| 431 |
// $classes = LP_Helper::sanitize_array( $classes ); |
| 432 |
// |
| 433 |
// return apply_filters( 'learn-press/question/answer-option/classes', $classes, $this ); |
| 434 |
// } |
| 435 |
|
| 436 |
/** |
| 437 |
* @param bool $echo |
| 438 |
* |
| 439 |
* @return string |
| 440 |
*/ |
| 441 |
public function checked( $echo = true ) { |
| 442 |
return checked( $this->is_checked(), true, $echo ); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* @param bool $echo |
| 447 |
* |
| 448 |
* @return string |
| 449 |
*/ |
| 450 |
public function disabled( $echo = true ) { |
| 451 |
$q = $this->_question; |
| 452 |
$disabled = ( $q->show_correct_answers() === 'yes' ) || ( $q->disable_answers() === 'yes' ); |
| 453 |
|
| 454 |
return disabled( $disabled, true, $echo ); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* @return array|mixed |
| 459 |
*/ |
| 460 |
public function get_answered() { |
| 461 |
return $this->get_question()->get_answered(); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* @return bool |
| 466 |
*/ |
| 467 |
public function is_checked() { |
| 468 |
if ( false === $this->get_answered() ) { |
| 469 |
if ( $this->get_question()->show_correct_answers() === 'yes' && $this->is_true() ) { |
| 470 |
|
| 471 |
return true; |
| 472 |
} |
| 473 |
|
| 474 |
return false; |
| 475 |
} |
| 476 |
|
| 477 |
return in_array( $this->get_value(), (array) $this->get_answered() ); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
/** |
| 482 |
* @return bool|LP_Question |
| 483 |
*/ |
| 484 |
public function get_question() { |
| 485 |
return $this->_question; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* @return int |
| 490 |
*/ |
| 491 |
public function get_question_id() { |
| 492 |
return $this->_question->get_id(); |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Print class attribute |
| 497 |
* |
| 498 |
* @param string $more |
| 499 |
* @deprecated 4.2.7.6 |
| 500 |
*/ |
| 501 |
/*public function option_class( $more = '' ) { |
| 502 |
echo 'class="' . join( ' ', $this->get_class( $more ) ) . '"'; |
| 503 |
}*/ |
| 504 |
|
| 505 |
public function get_data() { |
| 506 |
return $this->_data; |
| 507 |
} |
| 508 |
|
| 509 |
public function get_meta( $key, $single = true ) { |
| 510 |
return learn_press_get_question_answer_meta( $this->get_id(), $key, $single ); |
| 511 |
} |
| 512 |
|
| 513 |
public function __toString() { |
| 514 |
return (string) $this->_data['title']; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Backward compatibility for accessing property as array. |
| 519 |
* |
| 520 |
* @param mixed $offset |
| 521 |
* |
| 522 |
* @return bool |
| 523 |
*/ |
| 524 |
public function offsetExists( $offset ) { |
| 525 |
return array_key_exists( $offset, $this->_data ); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Backward compatibility for get property as array. |
| 530 |
* |
| 531 |
* @param mixed $offset |
| 532 |
* |
| 533 |
* @return mixed |
| 534 |
*/ |
| 535 |
public function offsetGet( $offset ) { |
| 536 |
return $this->offsetExists( $offset ) ? $this->_data[ $offset ] : false; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Backward compatibility for set property as array. |
| 541 |
* |
| 542 |
* @param mixed $offset |
| 543 |
*/ |
| 544 |
public function offsetSet( $offset, $value ) { |
| 545 |
$this->_data[ $offset ] = $value; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Backward compatibility for unset property as array. |
| 550 |
* |
| 551 |
* @param mixed $offset |
| 552 |
*/ |
| 553 |
public function offsetUnset( $offset ) { |
| 554 |
if ( $this->offsetExists( $offset ) ) { |
| 555 |
unset( $this->_data[ $offset ] ); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
|