3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * True-false question definition class.
22 * @package qtype_truefalse
23 * @copyright 2009 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 * Represents a true-false question.
31 * @copyright 2009 The Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class qtype_truefalse_question extends question_graded_automatically {
37 public $falsefeedback;
39 public $falseanswerid;
41 public function get_expected_data() {
42 return array('answer' => PARAM_INTEGER);
45 public function get_correct_response() {
46 return array('answer' => (int) $this->rightanswer);
49 public function summarise_response(array $response) {
50 if (!array_key_exists('answer', $response)) {
52 } else if ($response['answer']) {
53 return get_string('true', 'qtype_truefalse');
55 return get_string('false', 'qtype_truefalse');
59 public function classify_response(array $response) {
60 if (!array_key_exists('answer', $response)) {
61 return array($this->id => question_classified_response::no_response());
63 list($fraction) = $this->grade_response($response);
64 if ($response['answer']) {
65 return array($this->id => new question_classified_response(1,
66 get_string('true', 'qtype_truefalse'), $fraction));
68 return array($this->id => new question_classified_response(0,
69 get_string('false', 'qtype_truefalse'), $fraction));
73 public function is_complete_response(array $response) {
74 return array_key_exists('answer', $response);
77 public function get_validation_error(array $response) {
78 if ($this->is_gradable_response($response)) {
81 return get_string('pleaseselectananswer', 'qtype_truefalse');
84 public function is_same_response(array $prevresponse, array $newresponse) {
85 return question_utils::arrays_same_at_key_missing_is_blank(
86 $prevresponse, $newresponse, 'answer');
89 public function grade_response(array $response) {
90 if ($this->rightanswer == true && $response['answer'] == true) {
92 } else if ($this->rightanswer == false && $response['answer'] == false) {
97 return array($fraction, question_state::graded_state_for_fraction($fraction));
100 public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
101 if ($component == 'question' && $filearea == 'answerfeedback') {
102 $answerid = reset($args); // itemid is answer id.
103 $response = $qa->get_last_qt_var('answer', '');
104 return $options->feedback && (
105 ($answerid == $this->trueanswerid && $response) ||
106 ($answerid == $this->falseanswerid && $response !== ''));
109 return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);