| 1 |
<?php |
| 2 |
/** |
| 3 |
* LP_Question_True_Or_False |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Templates |
| 7 |
* @version 3.0.0 |
| 8 |
* @extends LP_Question |
| 9 |
*/ |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit(); |
| 12 |
|
| 13 |
if ( ! class_exists( 'LP_Question_True_Or_False' ) ) { |
| 14 |
|
| 15 |
/** |
| 16 |
* Class LP_Question_True_Or_False |
| 17 |
*/ |
| 18 |
class LP_Question_True_Or_False extends LP_Question { |
| 19 |
/** |
| 20 |
* Type of this question. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
protected $_question_type = 'true_or_false'; |
| 25 |
|
| 26 |
/** |
| 27 |
* LP_Question_True_Or_False constructor. |
| 28 |
* |
| 29 |
* @param null $the_question |
| 30 |
* @param null $args |
| 31 |
* |
| 32 |
* @throws Exception |
| 33 |
*/ |
| 34 |
public function __construct( $the_question = null, $args = null ) { |
| 35 |
parent::__construct( $the_question, $args ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get true or false default answers. |
| 40 |
* |
| 41 |
* @return array |
| 42 |
*/ |
| 43 |
public function get_default_answers() { |
| 44 |
$answers = array( |
| 45 |
array( |
| 46 |
'question_answer_id' => - 1, |
| 47 |
'is_true' => 'yes', |
| 48 |
'value' => 'true', |
| 49 |
'title' => esc_html__( 'True', 'learnpress' ), |
| 50 |
), |
| 51 |
array( |
| 52 |
'question_answer_id' => - 2, |
| 53 |
'is_true' => 'no', |
| 54 |
'value' => 'false', |
| 55 |
'title' => esc_html__( 'False', 'learnpress' ), |
| 56 |
), |
| 57 |
); |
| 58 |
|
| 59 |
return $answers; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Check user answer. |
| 64 |
* |
| 65 |
* @param null $user_answer |
| 66 |
* |
| 67 |
* @return array |
| 68 |
*/ |
| 69 |
public function check( $user_answer = null ) { |
| 70 |
$return = parent::check(); |
| 71 |
$answers = $this->get_answers(); |
| 72 |
|
| 73 |
if ( $answers ) { |
| 74 |
foreach ( $answers as $key => $option ) { |
| 75 |
if ( ( $option['is_true'] == 'yes' ) && ( $option['value'] == $user_answer ) ) { |
| 76 |
$return['correct'] = true; |
| 77 |
$return['mark'] = floatval( $this->get_mark() ); |
| 78 |
break; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $return; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|