-
Notifications
You must be signed in to change notification settings - Fork 2
/
question.php
327 lines (300 loc) · 12.7 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
// This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
use qtype_drawlines\line;
/**
* Draw lines question definition class.
*
* @package qtype_drawlines
* @copyright 2024 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_drawlines_question extends question_graded_automatically {
/** @var string feedback for any correct response. */
public string $correctfeedback;
/** @var int format of $correctfeedback. */
public int $correctfeedbackformat;
/** @var string feedback for any partially correct response. */
public string $partiallycorrectfeedback;
/** @var int format of $partiallycorrectfeedback. */
public int $partiallycorrectfeedbackformat;
/** @var string feedback for any incorrect response. */
public string $incorrectfeedback;
/** @var int format of $incorrectfeedback. */
public int $incorrectfeedbackformat;
/** @var int shows if the drag lines are misplaced. */
public $showmisplaced;
/** @var string 'allnone' (All-or-nothing) or 'partial' (Give partial credit) grading method. */
public string $grademethod;
/** @var line[], an array of line objects. */
public $lines;
/** @var int The number of lines. */
public $numberoflines;
#[\Override]
public function get_expected_data() {
$expecteddata = [];
foreach ($this->lines as $line) {
$expecteddata[$this->choice($line->number - 1)] = PARAM_RAW;
}
return $expecteddata;
}
#[\Override]
public function is_complete_response(array $response): bool {
// If there is no response return false.
if (empty($response)) {
return false;
}
// If there is no response for each line return false for all-or-nothing grading method.
if (count($response) < count($this->lines)) {
return false;
}
foreach ($this->lines as $key => $line) {
if (isset($response[$this->choice($key)]) &&
!line::are_response_coordinates_valid($response[$this->choice($key)], $line->type)) {
return false;
}
}
return true;
}
#[\Override]
public function get_correct_response() {
$response = [];
foreach ($this->lines as $key => $line) {
$response[$this->choice($key)] = line::get_coordinates($line->zonestart) . ' '
. line::get_coordinates($line->zoneend);
}
return $response;
}
#[\Override]
public function summarise_response(array $response): ?string {
$responsewords = [];
$answers = [];
foreach ($this->lines as $key => $line) {
if (array_key_exists($this->choice($key), $response) && $response[$this->choice($key)] != '') {
$coordinates = explode(' ', $response[$this->choice($key)]);
if ($line->type == 'lineinfinite' && count($coordinates) == 4) {
$coordinates = explode(' ', $response[$this->choice($key)]);
$answers[] = 'Line ' . $line->number . ': ' . $coordinates[1] . ' ' . $coordinates[2];
continue;
}
$answers[] = 'Line ' . $line->number . ': ' . $response[$this->choice($key)];
}
}
if (count($answers) > 0) {
$responsewords[] = implode(', ', $answers);
}
return implode('; ', $responsewords);
}
#[\Override]
public function is_gradable_response(array $response) {
foreach ($this->lines as $key => $line) {
if (array_key_exists($this->choice($key), $response)) {
return true;
}
}
return false;
}
#[\Override]
public function is_same_response(array $prevresponse, array $newresponse) {
foreach ($this->lines as $key => $line) {
$fieldname = $this->choice($key);
if (!question_utils::arrays_same_at_key_missing_is_blank($prevresponse, $newresponse, $fieldname)) {
return false;
}
}
return true;
}
#[\Override]
public function grade_response(array $response): array {
// Retrieve the number of right responses and the total number of responses.
if ($this->grademethod == 'partial') {
[$numright, $total] = $this->get_num_parts_right_grade_partialt($response);
} else {
[$numright, $total] = $this->get_num_parts_right_grade_allornone($response);
}
$fraction = $numright / $total;
return [$fraction, question_state::graded_state_for_fraction($fraction)];
}
/**
* Get the number of correct choices selected in the response, for 'Give partial credit' grade method.
*
* @param array $response The response list.
* @return array The array of number of correct lines (start, end or both points of lines).
*/
public function get_num_parts_right_grade_partialt(array $response): array {
if (!$response) {
return [0, 0];
}
$numpartright = 0;
foreach ($this->lines as $key => $line) {
if (array_key_exists($this->choice($key), $response) && $response[$this->choice($key)] !== '') {
$coords = explode(' ', $response[$this->choice($key)]);
if ($line->type == 'lineinfinite') {
if (count($coords) == 2) {
// Response with 2 coordinates (x1,y1 x2,y2).
if (line::is_item_positioned_correctly_on_axis(
$coords[0], $line->zonestart, $line->zoneend, 'start')) {
$numpartright++;
}
if (line::is_item_positioned_correctly_on_axis(
$coords[1], $line->zonestart, $line->zoneend, 'end')) {
$numpartright++;
}
} else {
// Response has 4 coordinates(x1,y1 x2,y2 x3,y3 x4,y4).
// Here we need to consider x2,y2 x3,y3 for calculation.
if (line::is_item_positioned_correctly_on_axis(
$coords[1], $line->zonestart, $line->zoneend, 'start')) {
$numpartright++;
}
if (line::is_item_positioned_correctly_on_axis(
$coords[2], $line->zonestart, $line->zoneend, 'end')) {
$numpartright++;
}
}
} else {
if (line::is_dragitem_in_the_right_place($coords[0], $line->zonestart)) {
$numpartright++;
}
if (line::is_dragitem_in_the_right_place($coords[1], $line->zoneend)) {
$numpartright++;
}
}
}
}
$total = count($this->lines) * 2;
return [$numpartright, $total];
}
/**
* Get the number of correct choices selected in the response, for All-or-nothing grade method.
*
* @param array $response The response list.
* @return array The array of number of correct lines (both start and end points).
*/
public function get_num_parts_right_grade_allornone(array $response): array {
if (!$response) {
return [0, 0];
}
$numright = 0;
foreach ($this->lines as $key => $line) {
if (array_key_exists($this->choice($key), $response) && $response[$this->choice($key)] !== '') {
$coords = explode(' ', $response[$this->choice($key)]);
if ($line->type == 'lineinfinite') {
if (count($coords) == 2) {
// Response with 2 coordinates (x1,y1 x2,y2 x3,y3 x4,y4).
$isstartrightplace = line::is_item_positioned_correctly_on_axis(
$coords[0], $line->zonestart, $line->zoneend, 'start'
);
$isendrightplace = line::is_item_positioned_correctly_on_axis(
$coords[1], $line->zonestart, $line->zoneend, 'end'
);
} else {
// Response has 4 coordinates(x1,y1 x2,y2 x3,y3 x4,y4).
// Here we need to consider x2,y2 x3,y3 for calculation.
$isstartrightplace = line::is_item_positioned_correctly_on_axis(
$coords[1], $line->zonestart, $line->zoneend, 'start'
);
$isendrightplace = line::is_item_positioned_correctly_on_axis(
$coords[2], $line->zonestart, $line->zoneend, 'end'
);
}
if ($isstartrightplace && $isendrightplace) {
$numright++;
}
} else {
if (line::is_dragitem_in_the_right_place($coords[0], $line->zonestart) &&
line::is_dragitem_in_the_right_place($coords[1], $line->zoneend)) {
$numright++;
}
}
}
}
$total = count($this->lines);
return [$numright, $total];
}
#[\Override]
public function check_file_access($qa, $options, $component, $filearea, $args, $forcedownload) {
if ($filearea === 'bgimage') {
$validfilearea = true;
} else {
$validfilearea = false;
}
if ($component === 'qtype_drawlines' && $validfilearea) {
$question = $qa->get_question(false);
$itemid = reset($args);
return $itemid == $question->id;
} else {
return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
}
}
#[\Override]
public function get_validation_error(array $response): string {
if ($this->is_complete_response($response)) {
return '';
}
return get_string('pleasedragalllines', 'qtype_drawlines');
}
#[\Override]
public function classify_response(array $response) {
$classifiedresponse = [];
foreach ($this->lines as $key => $line) {
if (array_key_exists($this->choice($key), $response) && $response[$this->choice($key)] !== '') {
if ($this->grademethod == 'partial') {
$fraction = 0.5;
} else {
$fraction = 1;
}
$classifiedresponse[$key] = new question_classified_response(
$line->number,
'Line ' . $line->number . ': ' . $response[$this->choice($key)],
$fraction);
} else {
$classifiedresponse[$key] = question_classified_response::no_response();
}
}
return $classifiedresponse;
}
/**
* Work out a final grade for this attempt, taking into account
* all the tries the student made and return the grade value.
*
* @param array $responses the response for each try. Each element of this
* array is a response array, as would be passed to {@link grade_response()}.
* There may be between 1 and $totaltries responses.
*
* @param int $totaltries The maximum number of tries allowed.
*
* @return float the fraction that should be awarded for this
* sequence of response.
*/
public function compute_final_grade(array $responses, int $totaltries): float {
// TODO: To incorporate the question penalty for interactive with multiple tries behaviour.
$grade = 0;
foreach ($responses as $response) {
[$fraction, $state] = $this->grade_response($response);
$grade += $fraction;
}
return $grade;
}
/**
* Get a choice identifier
*
* @param int $choice stem number
* @return string the question-type variable name.
*/
public function choice($choice) {
return 'c' . $choice;
}
}