forked from gbateson/moodle-qtype_ordering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
434 lines (369 loc) · 19.3 KB
/
renderer.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?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/>.
/**
* ORDERING question renderer class.
* (originally based on the TRUE-FALSE renderer)
*
* @package qtype
* @subpackage ordering
* @copyright 2013 Gordon Bateson ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/** Prevent direct access to this script */
defined('MOODLE_INTERNAL') || die();
if (! class_exists('qtype_with_combined_feedback_renderer')) { // Moodle 2.0
require_once($CFG->dirroot.'/question/type/ordering/legacy/rendererbase.php');
}
/**
* Generates the output for ORDERING questions
*
* @copyright 2013 Gordon Bateson
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_ordering_renderer extends qtype_with_combined_feedback_renderer {
protected $correctinfo = null;
protected $currentinfo = null;
protected $itemscores = array();
protected $all_correct = null;
public function formulation_and_controls(question_attempt $qa, question_display_options $options) {
global $CFG, $DB;
$question = $qa->get_question();
$response = $qa->get_last_qt_data();
$question->update_current_response($response);
$currentresponse = $question->currentresponse;
$correctresponse = $question->correctresponse;
// generate fieldnames and ids
// response_fieldname : 1_response_319
// response_name : q27:1_response_319
// response_id : id_q27_1_response_319
// sortable_id : id_sortable_q27_1_response_319
$response_fieldname = $question->get_response_fieldname();
$response_name = $qa->get_qt_field_name($response_fieldname);
$response_id = 'id_'.preg_replace('/[^a-zA-Z0-9]+/', '_', $response_name);
$sortable_id = 'id_sortable_'.$question->id;
$ablock_id = 'id_ablock_'.$question->id;
switch ($question->options->layouttype) {
case qtype_ordering_question::LAYOUT_VERTICAL : $axis = 'y'; break;
case qtype_ordering_question::LAYOUT_HORIZONTAL : $axis = ''; break;
default: $axis = '';
}
$result = '';
if ($options->readonly || $options->correctness) {
// don't allow items to be dragged and dropped
} else {
$script = "\n";
$script .= "//<![CDATA[\n";
$script .= "if (window.$) {\n";
$script .= " $(function() {\n";
$script .= " $('#$sortable_id').sortable({\n";
$script .= " axis: '$axis',\n";
$script .= " containment: '#$ablock_id',\n";
$script .= " opacity: 0.6,\n";
$script .= " update: function(event, ui) {\n";
$script .= " var ItemsOrder = $(this).sortable('toArray').toString();\n";
$script .= " $('#$response_id').attr('value', ItemsOrder);\n";
$script .= " }\n";
$script .= " });\n";
$script .= " $('#$sortable_id').disableSelection();\n";
$script .= " });\n";
$script .= " $(document).ready(function() {\n";
$script .= " var ItemsOrder = $('#$sortable_id').sortable('toArray').toString();\n";
$script .= " $('#$response_id').attr('value', ItemsOrder);\n";
$script .= " });\n";
$script .= "}\n";
$script .= "//]]>\n";
$result .= html_writer::tag('script', $script, array('type' => 'text/javascript'));
}
$result .= html_writer::tag('div', $question->format_questiontext($qa), array('class' => 'qtext'));
$printeditems = false;
if (count($currentresponse)) {
// set layout class
$layoutclass = $question->get_ordering_layoutclass();
// generate ordering items
foreach ($currentresponse as $position => $answerid) {
if (! array_key_exists($answerid, $question->answers)) {
continue; // shouldn't happen !!
}
if (! array_key_exists($position, $correctresponse)) {
continue; // shouldn't happen !!
}
if ($printeditems==false) {
$printeditems = true;
$result .= html_writer::start_tag('div', array('class' => 'ablock', 'id' => $ablock_id));
$result .= html_writer::start_tag('div', array('class' => 'answer ordering'));
$result .= html_writer::start_tag('ul', array('class' => 'sortablelist', 'id' => $sortable_id));
}
// set the CSS class and correctness img for this response
if ($options->correctness) {
$score = $this->get_ordering_item_score($question, $position, $answerid);
list($score, $maxscore, $fraction, $percent, $class, $img) = $score;
} else {
$class = 'sortableitem';
$img = '';
}
$class = "$class $layoutclass";
// the original "id" revealed the correct order of the answers
// because $answer->fraction holds the correct order number
// $id = 'ordering_item_'.$answerid.'_'.intval($question->answers[$answerid]->fraction);
$answer = $question->answers[$answerid];
$answer->answer = $question->format_text($answer->answer, $answer->answerformat, $qa, 'question', 'answer', $answerid);
$params = array('class' => $class, 'id' => $answer->md5key);
$result .= html_writer::tag('li', $img.$answer->answer, $params);
}
}
if ($printeditems) {
$result .= html_writer::end_tag('ul');
$result .= html_writer::end_tag('div'); // answer
$result .= html_writer::end_tag('div'); // ablock
$result .= html_writer::empty_tag('input', array('type' => 'hidden',
'name' => $response_name,
'id' => $response_id,
'value' => ''));
// the following DIV is not necessary if we use "overflow: auto;" on the "answer" DIV
//$result .= html_writer::tag('div', '', array('style' => 'clear:both;'));
}
return $result;
}
public function specific_feedback(question_attempt $qa) {
if ($feedback = $this->combined_feedback($qa)) {
$feedback = html_writer::tag('p', $feedback);
}
$gradingtype = '';
$gradedetails = '';
$scoredetails = '';
// if required, add explanation of grade calculation
if ($step = $qa->get_last_step()) {
$state = $step->get_state();
if ($state=='gradedpartial' || $state=='gradedwrong') {
$plugin = 'qtype_ordering';
$question = $qa->get_question();
// fetch grading type
$gradingtype = $question->options->gradingtype;
$gradingtype = qtype_ordering_question::get_grading_types($gradingtype);
// format grading type, e.g. Grading type: Relative to next item, excluding last item
if ($gradingtype) {
$gradingtype = get_string('gradingtype', $plugin).': '.$gradingtype;
$gradingtype = html_writer::tag('p', $gradingtype, array('class' => 'gradingtype'));
}
// fetch grade details and score details
if ($currentresponse = $question->currentresponse) {
$totalscore = 0;
$totalmaxscore = 0;
$layoutclass = $question->get_ordering_layoutclass();
$params = array('class' => $layoutclass);
$scoredetails .= html_writer::tag('p', get_string('scoredetails', $plugin));
$scoredetails .= html_writer::start_tag('ol', array('class' => 'scoredetails'));
// format scoredetails, e.g. 1 /2 = 50%, for each item
foreach ($currentresponse as $position => $answerid) {
if (array_key_exists($answerid, $question->answers)) {
$answer = $question->answers[$answerid];
$score = $this->get_ordering_item_score($question, $position, $answerid);
list($score, $maxscore, $fraction, $percent, $class, $img) = $score;
if ($maxscore===null) {
$score = get_string('noscore', $plugin);
} else {
$totalscore += $score;
$totalmaxscore += $maxscore;
$score = "$score / $maxscore = $percent%";
}
$scoredetails .= html_writer::tag('li', $score, $params);
} else {
// Oops, the md5 key is not recognized !!
// Maybe the password salt has changed
// because we restored to a new server?
}
}
$scoredetails .= html_writer::end_tag('ol');
if ($totalmaxscore==0) {
$scoredetails = ''; // ALL_OR_NOTHING
} else {
// format gradedetails, e.g. 4 /6 = 67%
if ($totalscore==0) {
$gradedetails = 0;
} else {
$gradedetails = round(100 * $totalscore / $totalmaxscore, 0);
}
$gradedetails = "$totalscore / $totalmaxscore = $gradedetails%";
$gradedetails = get_string('gradedetails', $plugin).': '.$gradedetails;
$gradedetails = html_writer::tag('p', $gradedetails, array('class' => 'gradedetails'));
}
}
}
}
return $feedback.$gradingtype.$gradedetails.$scoredetails;
}
public function correct_response(question_attempt $qa) {
global $DB;
$output = '';
$showcorrect = false;
$question = $qa->get_question();
if (empty($question->correctresponse)) {
$output .= html_writer::tag('p', get_string('noresponsedetails', 'qtype_ordering'));
} else {
if ($step = $qa->get_last_step()) {
switch ($step->get_state()) {
case 'gradedright' : $showcorrect = false; break;
case 'gradedpartial': $showcorrect = true; break;
case 'gradedwrong' : $showcorrect = true; break;
}
}
}
if ($showcorrect) {
$layoutclass = $question->get_ordering_layoutclass();
$output .= html_writer::tag('p', get_string('correctorder', 'qtype_ordering'));
$output .= html_writer::start_tag('ol', array('class' => 'correctorder'));
$correctresponse = $question->correctresponse;
foreach ($correctresponse as $position => $answerid) {
$answer = $question->answers[$answerid];
$output .= html_writer::tag('li', $answer->answer, array('class' => $layoutclass));
}
$output .= html_writer::end_tag('ol');
}
return $output;
}
/////////////////////////////////////
// custom methods
/////////////////////////////////////
protected function get_response_info($question) {
$gradingtype = $question->options->gradingtype;
switch ($gradingtype) {
case qtype_ordering_question::GRADING_ALL_OR_NOTHING:
case qtype_ordering_question::GRADING_ABSOLUTE_POSITION:
$this->correctinfo = $question->correctresponse;
$this->currentinfo = $question->currentresponse;
break;
case qtype_ordering_question::GRADING_RELATIVE_NEXT_EXCLUDE_LAST:
case qtype_ordering_question::GRADING_RELATIVE_NEXT_INCLUDE_LAST:
$this->correctinfo = $question->get_next_answerids($question->correctresponse, $gradingtype==2);
$this->currentinfo = $question->get_next_answerids($question->currentresponse, $gradingtype==2);
break;
case qtype_ordering_question::GRADING_RELATIVE_ONE_PREVIOUS_AND_NEXT:
case qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT:
$this->correctinfo = $question->get_previous_and_next_answerids($question->correctresponse, $gradingtype==4);
$this->currentinfo = $question->get_previous_and_next_answerids($question->currentresponse, $gradingtype==4);
break;
case qtype_ordering_question::GRADING_LONGEST_ORDERED_SUBSET:
case qtype_ordering_question::GRADING_LONGEST_CONTIGUOUS_SUBSET:
$this->correctinfo = $question->correctresponse;
$this->currentinfo = $question->currentresponse;
$subset = $question->get_ordered_subset($gradingtype==6);
foreach ($this->currentinfo as $position => $answerid) {
if (array_search($position, $subset)===false) {
$this->currentinfo[$position] = 0;
} else {
$this->currentinfo[$position] = 1;
}
}
break;
}
}
protected function get_ordering_item_score($question, $position, $answerid) {
if (! isset($this->itemscores[$position])) {
if ($this->correctinfo===null || $this->currentinfo===null) {
$this->get_response_info($question);
}
$correctinfo = $this->correctinfo;
$currentinfo = $this->currentinfo;
$score = 0; // actual score for this item
$maxscore = null; // max score for this item
$fraction = 0.0; // $score / $maxscore
$percent = 0; // 100 * $fraction
$class = ''; // CSS class
$img = ''; // icon to show correctness
switch ($question->options->gradingtype) {
case qtype_ordering_question::GRADING_ALL_OR_NOTHING:
if ($this->is_all_correct()) {
$score = 1;
$maxscore = 1;
}
break;
case qtype_ordering_question::GRADING_ABSOLUTE_POSITION:
if (isset($correctinfo[$position])) {
if ($correctinfo[$position]==$answerid) {
$score = 1;
}
$maxscore = 1;
}
break;
case qtype_ordering_question::GRADING_RELATIVE_NEXT_EXCLUDE_LAST:
case qtype_ordering_question::GRADING_RELATIVE_NEXT_INCLUDE_LAST:
if (isset($correctinfo[$answerid])) {
if (isset($currentinfo[$answerid]) && $currentinfo[$answerid]==$correctinfo[$answerid]) {
$score = 1;
}
$maxscore = 1;
}
break;
case qtype_ordering_question::GRADING_RELATIVE_ONE_PREVIOUS_AND_NEXT:
case qtype_ordering_question::GRADING_RELATIVE_ALL_PREVIOUS_AND_NEXT:
if (isset($correctinfo[$answerid])) {
$maxscore = 0;
$prev = $correctinfo[$answerid]->prev;
$maxscore += count($prev);
$prev = array_intersect($prev, $currentinfo[$answerid]->prev);
$score += count($prev);
$next = $correctinfo[$answerid]->next;
$maxscore += count($next);
$next = array_intersect($next, $currentinfo[$answerid]->next);
$score += count($next);
}
break;
case qtype_ordering_question::GRADING_LONGEST_ORDERED_SUBSET:
case qtype_ordering_question::GRADING_LONGEST_CONTIGUOUS_SUBSET:
if (isset($correctinfo[$position])) {
if (isset($currentinfo[$position])) {
$score = $currentinfo[$position];
}
$maxscore = 1;
}
break;
}
if ($maxscore===null) {
// an unscored item is either an illegal item
// or last item of RELATIVE_NEXT_EXCLUDE_LAST
// or an item in an incorrect ALL_OR_NOTHING
// or an item from an unrecognized grading type
$class = 'unscored';
} else {
if ($maxscore==0) {
$fraction = 0.0;
$percent = 0;
} else {
$fraction = ($score / $maxscore);
$percent = round(100 * $fraction, 0);
}
switch (true) {
case ($fraction > 0.999999): $class = 'correct'; break;
case ($fraction < 0.000001): $class = 'incorrect'; break;
case ($fraction >= 0.66): $class = 'partial66'; break;
case ($fraction >= 0.33): $class = 'partial33'; break;
default: $class = 'partial00'; break;
}
$img = $this->feedback_image($fraction);
}
$score = array($score, $maxscore, $fraction, $percent, $class, $img);
$this->itemscores[$position] = $score;
}
return $this->itemscores[$position];
}
protected function is_all_correct() {
if ($this->all_correct===null) {
// use "==" to determine if the two "info" arrays are identical
$this->all_correct = ($this->correctinfo==$this->currentinfo);
}
return $this->all_correct;
}
}