1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* True-false question definition class.
|
|
|
19 |
*
|
|
|
20 |
* @package qtype
|
|
|
21 |
* @subpackage truefalse
|
|
|
22 |
* @copyright 2009 The Open University
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
require_once($CFG->dirroot . '/question/type/questionbase.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Represents a true-false question.
|
|
|
33 |
*
|
|
|
34 |
* @copyright 2009 The Open University
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
*/
|
|
|
37 |
class qtype_truefalse_question extends question_graded_automatically {
|
|
|
38 |
public $rightanswer;
|
|
|
39 |
public $truefeedback;
|
|
|
40 |
public $falsefeedback;
|
|
|
41 |
public $trueanswerid;
|
|
|
42 |
public $falseanswerid;
|
|
|
43 |
|
|
|
44 |
/** @var int the format of the true feedback. */
|
|
|
45 |
public $truefeedbackformat;
|
|
|
46 |
|
|
|
47 |
/** @var int the format of the false feedback. */
|
|
|
48 |
public $falsefeedbackformat;
|
|
|
49 |
|
|
|
50 |
/** @var bool true to show the standard instruction, otherwise hide it. */
|
|
|
51 |
public $showstandardinstruction;
|
|
|
52 |
|
|
|
53 |
public function get_expected_data() {
|
|
|
54 |
return array('answer' => PARAM_INT);
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
public function get_correct_response() {
|
|
|
58 |
return array('answer' => (int) $this->rightanswer);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public function summarise_response(array $response) {
|
|
|
62 |
if (!array_key_exists('answer', $response)) {
|
|
|
63 |
return null;
|
|
|
64 |
} else if ($response['answer']) {
|
|
|
65 |
return get_string('true', 'qtype_truefalse');
|
|
|
66 |
} else {
|
|
|
67 |
return get_string('false', 'qtype_truefalse');
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function un_summarise_response(string $summary) {
|
|
|
72 |
if ($summary === get_string('true', 'qtype_truefalse')) {
|
|
|
73 |
return ['answer' => '1'];
|
|
|
74 |
} else if ($summary === get_string('false', 'qtype_truefalse')) {
|
|
|
75 |
return ['answer' => '0'];
|
|
|
76 |
} else {
|
|
|
77 |
return [];
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function classify_response(array $response) {
|
|
|
82 |
if (!array_key_exists('answer', $response)) {
|
|
|
83 |
return array($this->id => question_classified_response::no_response());
|
|
|
84 |
}
|
|
|
85 |
list($fraction) = $this->grade_response($response);
|
|
|
86 |
if ($response['answer']) {
|
|
|
87 |
return array($this->id => new question_classified_response(1,
|
|
|
88 |
get_string('true', 'qtype_truefalse'), $fraction));
|
|
|
89 |
} else {
|
|
|
90 |
return array($this->id => new question_classified_response(0,
|
|
|
91 |
get_string('false', 'qtype_truefalse'), $fraction));
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
public function is_complete_response(array $response) {
|
|
|
96 |
return array_key_exists('answer', $response);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
public function get_validation_error(array $response) {
|
|
|
100 |
if ($this->is_gradable_response($response)) {
|
|
|
101 |
return '';
|
|
|
102 |
}
|
|
|
103 |
return get_string('pleaseselectananswer', 'qtype_truefalse');
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public function is_same_response(array $prevresponse, array $newresponse) {
|
|
|
107 |
return question_utils::arrays_same_at_key_missing_is_blank(
|
|
|
108 |
$prevresponse, $newresponse, 'answer');
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
public function grade_response(array $response) {
|
|
|
112 |
if ($this->rightanswer == true && $response['answer'] == true) {
|
|
|
113 |
$fraction = 1;
|
|
|
114 |
} else if ($this->rightanswer == false && $response['answer'] == false) {
|
|
|
115 |
$fraction = 1;
|
|
|
116 |
} else {
|
|
|
117 |
$fraction = 0;
|
|
|
118 |
}
|
|
|
119 |
return array($fraction, question_state::graded_state_for_fraction($fraction));
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
|
|
|
123 |
if ($component == 'question' && $filearea == 'answerfeedback') {
|
|
|
124 |
$answerid = reset($args); // Itemid is answer id.
|
|
|
125 |
$response = $qa->get_last_qt_var('answer', '');
|
|
|
126 |
return $options->feedback && (
|
|
|
127 |
($answerid == $this->trueanswerid && $response) ||
|
|
|
128 |
($answerid == $this->falseanswerid && $response !== ''));
|
|
|
129 |
|
|
|
130 |
} else {
|
|
|
131 |
return parent::check_file_access($qa, $options, $component, $filearea,
|
|
|
132 |
$args, $forcedownload);
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Return the question settings that define this question as structured data.
|
|
|
138 |
*
|
|
|
139 |
* @param question_attempt $qa the current attempt for which we are exporting the settings.
|
|
|
140 |
* @param question_display_options $options the question display options which say which aspects of the question
|
|
|
141 |
* should be visible.
|
|
|
142 |
* @return mixed structure representing the question settings. In web services, this will be JSON-encoded.
|
|
|
143 |
*/
|
|
|
144 |
public function get_question_definition_for_external_rendering(question_attempt $qa, question_display_options $options) {
|
|
|
145 |
// No need to return anything, external clients do not need additional information for rendering this question type.
|
|
|
146 |
return null;
|
|
|
147 |
}
|
|
|
148 |
}
|