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