-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.php
283 lines (262 loc) · 9.45 KB
/
question.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Question definition class for multichoicegrid.
*
* @package qtype_multichoicegrid
* @copyright 2021 Laurent David <laurent@call-learning.fr>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// For a complete list of base question classes please examine the file
// /question/type/questionbase.php.
//
// Make sure to implement all the abstract methods of the base class.
/**
* Class that represents a multichoicegrid question.
*/
class qtype_multichoicegrid_question extends question_graded_automatically {
/**
* @var array $answers
*/
public $answers = null;
/**
* Returns data to be included in the form submission.
*
* @return array|string.
*/
public function get_expected_data() {
$data = [];
foreach (array_keys($this->answers) as $key) {
$data['answer' . $key] = PARAM_INT;
}
return $data;
}
/**
* Returns the data that would need to be submitted to get a correct answer.
*
* @return array|null Null if it is not possible to compute a correct response.
*/
public function get_correct_response() {
$response = array();
foreach ($this->answers as $key => $answer) {
$response[$this->field($key)] = $answer->answer;
}
return $response;
}
/**
* returns string of place key value prepended with p, i.e. p0 or p1 etc
*
* @param int $key stem number
* @return string the question-type variable name.
*/
public function field($key) {
return 'answer' . $key;
}
/**
* Checks whether the user is allowed to be served a particular file.
*
* @param question_attempt $qa The question attempt being displayed.
* @param question_display_options $options The options that control display of the question.
* @param string $component The name of the component we are serving files for.
* @param string $filearea The name of the file area.
* @param array $args the Remaining bits of the file path.
* @param bool $forcedownload Whether the user must be forced to download the file.
* @return bool True if the user can access this file.
*/
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
$isdocument = $component == 'qtype_multichoicegrid' && $filearea == 'document';
$isaudio = $component == 'qtype_multichoicegrid' && $filearea == 'audio';
$isadocumentfromthisquestion = \qtype_multichoicegrid\multichoicegrid_docs::record_exists_select(
'questionid = :questionid AND id = :id', array('questionid' => $this->id, 'id' => $args[0])
);
return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload)
|| ($isdocument || $isaudio) && $isadocumentfromthisquestion;
}
/**
* Is the reponse complete ?
*
* @param array $responses
* @return bool
*/
public function is_complete_response(array $responses) {
$iscomplete = true;
foreach (array_keys($this->answers) as $answerkey) {
$fieldname = $this->field($answerkey);
$iscomplete = $iscomplete && isset($responses[$fieldname]);
$iscomplete = $iscomplete && trim($responses[$fieldname] != "");
}
return $iscomplete;
}
/**
* Is it the same response ?
*
* @param array $prevresponse
* @param array $newresponse
* @return bool
*/
public function is_same_response(array $prevresponse, array $newresponse) {
$same = true;
foreach ($this->answers as $answerkey => $answerinfo) {
$aprevresponse = $this->get_response_value($answerkey, $prevresponse);
$anewresponse = $this->get_response_value($answerkey, $newresponse);
$same = $same && $aprevresponse == $anewresponse;
}
return $same;
}
/**
* Get response value from key
*
* @param string $answerkey
* @param array $responses
* @return mixed|null
*/
protected function get_response_value($answerkey, $responses) {
$fieldname = $this->field($answerkey);
return empty($responses[$fieldname]) ? null : $responses[$fieldname];
}
/**
* Summarise response
*
* @param array $response
* @return string
* @throws coding_exception
*/
public function summarise_response(array $response) {
$textresponses = [];
$index = 1;
foreach ($this->answers as $answerkey => $answerinfo) {
$currentresponse = $this->get_response_value($answerkey, $response);
if (is_null($currentresponse)) {
continue;
}
$answertypetext = get_string('option:' . $currentresponse, 'qtype_multichoicegrid');
$textresponses[] = "{$index} -> $answertypetext";
$index++;
}
return implode(', ', $textresponses);
}
/**
* Get validation error
*
* @param array $response
* @return lang_string|string
* @throws coding_exception
*/
public function get_validation_error(array $response) {
if ($this->is_gradable_response($response)) {
return '';
}
return get_string('pleasechoseatleastananswer', 'qtype_multichoicegrid');
}
/**
* A question is gradable if at least one answer
*
* @param array $response
* @return boolean
*/
public function is_gradable_response(array $response) {
foreach (array_keys($this->answers) as $answerkey) {
if (!empty($response[$this->field($answerkey)])) {
return true;
}
}
return false;
}
/**
* Grade response
* @param array $responses
* @return array
*/
public function grade_response(array $responses) {
$totalscore = 0.0;
foreach ($this->answers as $answerkey => $answerinfo) {
$currentresponse = $this->get_response_value($answerkey, $responses);
if (is_null($currentresponse)) {
continue;
}
$isrightvalue = ($currentresponse == $answerinfo->answer) ? 1 : 0;
$totalscore += $isrightvalue;
}
$fraction = $totalscore / count($this->answers);
return array($fraction, question_state::graded_state_for_fraction($fraction));
}
/**
* Given a response, reset the parts that are wrong.
*
* @param array $response a response
* @return array a cleaned up response with the wrong bits reset.
*/
public function clear_wrong_from_response(array $response) {
foreach ($this->answers as $answerkey => $answerinfo) {
$currentresponse = $this->get_response_value($answerkey, $response);
if (is_null($currentresponse)) {
continue;
}
$isrightvalue = ($currentresponse == $answerinfo->answer) ? 1 : 0;
if (!$isrightvalue) {
$fieldname = $this->field($answerkey);
unset($response[$fieldname]);
}
}
return $response;
}
/**
* Return the number of subparts of this response that are right.
*
* @param array $response a response
* @return array with two elements, the number of correct subparts, and
* the total number of subparts.
*/
public function get_num_parts_right(array $response) {
$rightcount = 0;
foreach ($this->answers as $answerkey => $answerinfo) {
$currentresponse = $this->get_response_value($answerkey, $response);
if (is_null($currentresponse)) {
continue;
}
$rightcount += ($currentresponse == $answerinfo->answer) ? 1 : 0;
}
return array($rightcount, count($this->answers));
}
/**
* Compute final grade
*
* @param array $responses
* @param int $totaltries
* @return float|int
*/
public function compute_final_grade($responses, $totaltries) {
$totalscore = 0;
foreach ($this->answers as $answerkey => $answerinfo) {
$lastwrongindex = -1;
$finallyright = false;
foreach ($responses as $i => $response) {
$currentresponse = $this->get_response_value($answerkey, $response);
if (($currentresponse != $answerinfo->answer)) {
$lastwrongindex = $i;
$finallyright = false;
} else {
$finallyright = true;
}
}
if ($finallyright) {
$totalscore += max(0, 1 - ($lastwrongindex + 1) * $this->penalty);
}
}
return $totalscore / count($this->answers);
}
}