| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Question_Multi_Choice |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 3.0.0 |
| 8 |
* @extend LP_Question |
| 9 |
*/ |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit(); |
| 12 |
|
| 13 |
if ( ! class_exists( 'LP_Question_Multi_Choice' ) ) { |
| 14 |
/** |
| 15 |
* Class LP_Question_Multi_Choice |
| 16 |
*/ |
| 17 |
class LP_Question_Multi_Choice extends LP_Question { |
| 18 |
|
| 19 |
/** |
| 20 |
* Type of this question. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $_question_type = 'multi_choice'; |
| 25 |
|
| 26 |
/** |
| 27 |
* LP_Question_Multi_Choice constructor. |
| 28 |
* |
| 29 |
* @param null $the_question |
| 30 |
* @param null $options |
| 31 |
* |
| 32 |
* @throws Exception |
| 33 |
*/ |
| 34 |
public function __construct( $the_question = null, $options = null ) { |
| 35 |
parent::__construct( $the_question, $options ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Allow check answer. |
| 40 |
* |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
public function can_check_answer() { |
| 44 |
return true; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Check user answer. |
| 49 |
* |
| 50 |
* @param null $user_answer |
| 51 |
* |
| 52 |
* @return array |
| 53 |
*/ |
| 54 |
public function check( $user_answer = null ) { |
| 55 |
$return = parent::check(); |
| 56 |
settype( $user_answer, 'array' ); |
| 57 |
$answers = $this->get_answers(); |
| 58 |
|
| 59 |
if ( $answers ) { |
| 60 |
$correct = true; |
| 61 |
foreach ( $answers as $key => $option ) { |
| 62 |
$data = $option->get_data(); |
| 63 |
$selected = $this->is_selected_option( $data, $user_answer ); |
| 64 |
|
| 65 |
if ( $selected && ! $option->is_true() ) { |
| 66 |
$correct = false; |
| 67 |
} elseif ( ! $selected && $option->is_true() ) { |
| 68 |
$correct = false; |
| 69 |
} |
| 70 |
|
| 71 |
// Only one option is selected wrong will wrong the answer. |
| 72 |
if ( ! $correct ) { |
| 73 |
break; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if ( $correct ) { |
| 78 |
$return = array( |
| 79 |
'correct' => true, |
| 80 |
'mark' => floatval( $this->get_mark() ), |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
return $return; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|