-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
349 lines (307 loc) · 12.5 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
<?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/>.
/**
* Bebras question renderer class.
*
* @package qtype_bebras
* @subpackage bebras
* @copyright 2009 The Open University
* @copyright 2021 BWINF
* @author Manuel Gundlach
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Generates the output for bebras questions.
*
* @copyright 2009 The Open University
* @copyright 2021 BWINF
* @author Manuel Gundlach
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_bebras_renderer extends qtype_renderer {
/**
* Gives the code of the Bebras question's grader that creates a form
*
* The code returned creates what constitutes the basic part of the
* interaction of the user with the task
*
* @param string $grader The name of the grader (sans the .grader.php extension)
* @param ?string $currentanswer The most recent (encoded) answer given by the user
* @param ?string $currentscratch The most recent scratch value left by the user
* @param ?bool $cansave Whether the user can attempt to solve the question now
* @param ?array $args The arguments (from a JSON dump) that should be given to the grader
* @return string The (HTML) code that constitutes the basic interactive part of the question
*/
private function get_task_creator(
string $grader, ?string $currentanswer,
?string $currentscratch, ?bool $cansave,
?array $args
) {
$in = [
'answer' => $currentanswer,
'scratch' => $currentscratch,
'can_save' => $cansave
];
$mode = 'create_form';
$graderfile = 'graders/' . $grader . '.grader.php';
if (!include($graderfile)) {
die('Error: Grader missing');
}
return $out;
}
/**
* Encapsulates HTML code inside an iframe
*
* Returns code that encapsulates $srcdoc inside an iframe that can be accessed
* with the HTML id biber-iframe-$id.
*
* The iframe is automatically resized after loading using JS to fit the content
* inside. As a fallback, it is again resized 1s after loading as with some of
* the existing libraries, and unpredictably, content gets loaded after the fact.
*
* If $freeze is true, the user is kept from interacting with (clicking,
* dragging, etc. inside the) iframe.
*
* @param string $srcdoc The HTML code to be encapsulated in an iframe
* @param string $id A unique ID for the resulting iframe
* @param bool $freeze Whether interaction with the content should be allowed
* @return string The resulting iframe
*/
private function iframe_encapsulate(string $srcdoc, string $id, bool $freeze) {
$srcdoc = html_writer::tag('body', $srcdoc);
$biberiframeattributes = array(
'id' => 'biber-iframe-' . $id,
'srcdoc' => $srcdoc,
'frameborder' => '0',
'width' => '100%'
);
if ($freeze) {
$biberiframeattributes['style'] = 'pointer-events: none;';
}
$result = html_writer::tag('iframe', '', $biberiframeattributes);
$iframeresizer = 'function ifrresizer(event) {'
. 'let ifr = document.getElementById("biber-iframe-' . $id . '");'
. 'if(ifr.style.height != ifr.contentWindow.document.documentElement.scrollHeight+"px")'
. 'ifr.style.height = ifr.contentWindow.document.documentElement.scrollHeight+10+"px";'
. '}';
$iframeresizerloader = 'function(event) {'
. $iframeresizer
. 'let ifr = document.getElementById("biber-iframe-' . $id . '");'
. 'ifr.contentWindow.addEventListener("resize",ifrresizer);'
. 'ifrresizer();'
. 'setTimeout(ifrresizer, 1000);'
. '}';
$scr = 'window.addEventListener("load", ' . $iframeresizerloader . ');';
$result .= html_writer::script($scr);
return $result;
}
public function formulation_and_controls(
question_attempt $qa,
question_display_options $options
) {
$question = $qa->get_question();
$currentanswer = $qa->get_last_qt_var('answer');
$inputname = $qa->get_qt_field_name('answer');
// Remove scratch value from the answer and provide it separately.
$currentscratch = "";
if (strpos($currentanswer, "!!!!!")) {
$currentscratch = substr(
$currentanswer,
strpos($currentanswer, "!!!!!") + 5
);
$currentanswer = substr(
$currentanswer,
0,
strpos($currentanswer, "!!!!!")
);
}
$inputattributes = array(
'type' => 'text',
'name' => $inputname,
'value' => $currentanswer,
'id' => $inputname,
'class' => 'hidden',
'readonly' => 'readonly',
);
$feedbackimg = '';
if ($options->correctness) {
$answer = $question->get_matching_answer(array('answer' => $currentanswer));
if ($answer) {
$fraction = $answer->fraction;
} else {
$fraction = 0;
}
$inputattributes['class'] .= ' ' . $this->feedback_class($fraction);
$feedbackimg = $this->feedback_image($fraction);
}
$questiontext = $question->format_questiontext($qa);
list($grader, $in0, $in1) = $question->get_grader($questiontext);
$questiontext = substr($questiontext, 0, $in0) . substr($questiontext, $in1);
list($args, $in0, $in1) = $question->get_args($questiontext);
$questiontext = substr($questiontext, 0, $in0) . substr($questiontext, $in1);
// Replace {TASK-CREATOR} in question text with the task creator
// generated by the .grader.php file.
$questiontext = str_replace(
'{TASK-CREATOR}',
$this->get_task_creator(
$grader,
$currentanswer,
$currentanswer,
!($options->correctness),
$args
),
$questiontext
);
// This isn't pretty, but we have to replace @@PLUGINFILE@@/... in
// answers of 'Multiple Choice with Images' grader.
$questiontext = $question->format_text(
$questiontext,
$question->questiontextformat,
$qa,
'question',
'questiontext',
$question->id
);
$inputinplace = $inputattributes['id'];
$inputinplace = str_replace(':', '_', $inputinplace);
// Scratch input.
$scratchinputattributes = array(
'type' => 'text',
'name' => "scratch_" . $inputinplace,
'value' => $currentscratch,
'id' => "scratch_" . $inputinplace,
'class' => 'hidden',
'readonly' => 'readonly',
);
$input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg;
$input .= html_writer::empty_tag('input', $scratchinputattributes);
$srcdoc = $questiontext;
if (
!in_array(
$question->get_grader($questiontext)[0],
array("Open Integer", "Open Int 2019", "Open Question")
)
) {
$biberanswerfieldattributes = array(
'type' => 'hidden',
'name' => 'answer',
'value' => $currentanswer,
'id' => 'answer',
'readonly' => 'readonly',
);
$biberscratchfieldattributes = array(
'type' => 'hidden',
'name' => 'scratch',
'value' => $currentscratch,
'id' => 'scratch',
'readonly' => 'readonly',
);
$srcdoc .= html_writer::empty_tag('input', $biberanswerfieldattributes);
$srcdoc .= html_writer::empty_tag('input', $biberscratchfieldattributes);
}
$result = $this->iframe_encapsulate($srcdoc, $inputinplace, $options->correctness);
$result = html_writer::tag('div', $result, array('class' => 'qtext'));
$result .= html_writer::start_tag('div', array('class' => 'ablock form-inline hidden'));
$result .= html_writer::tag(
'span',
$input,
array('class' => 'answer'),
array('for' => $inputattributes['id'])
);
$result .= html_writer::end_tag('div');
if ($qa->get_state() == question_state::$invalid) {
$result .= html_writer::nonempty_tag(
'div',
$question->get_validation_error(array('answer' => $currentanswer)),
array('class' => 'validationerror')
);
}
// NOTE This JS code needs getAnswer (and getScratch) to be defined
// _globally_ in .grader.php. If it isn't already, e.g. because it's
// part of a Task object, define it like e.g.
// in 'Clickable SVG - Multi.grader.php'.
$answercatcher = 'function(event) {';
$answercatcher .= ' var refo = document.getElementById("responseform");'
. ' if(refo){'
. ' refo.addEventListener("submit", function() {'
. ' console.log("Inserting answer... ");'
. ' let ifrcW = document.getElementById("biber-iframe-' . $inputinplace . '").contentWindow;'
. ' let answerfield = document.getElementById("' . $inputattributes['id'] . '");'
. ' answerfield.value = ifrcW.getAnswer();'
. ' if(ifrcW.getScratch){'
. ' var scratch = ifrcW.getScratch();'
. ' if(scratch){'
. ' answerfield.value += "!!!!!" + scratch;'
. ' }'
. ' }'
. ' });'
. ' }'
. '}';
$scr = 'document.addEventListener("DOMContentLoaded", ' . $answercatcher . ');';
// Modified width of Moodle quiz layout.
$scr .= <<<EOD
if(!document.getElementById('css_for_bebras_width')){
var css = `
@media (min-width: 768px) {
#page-header {
max-width: 953.5px !important;
}
.secondary-navigation {
max-width: 953.5px !important;
}
.pagelayout-standard #page.drawers .main-inner,
body.limitedwidth #page.drawers .main-inner {
max-width: 953.5px !important;
}
}`;
var style = document.createElement('style');
document.head.appendChild(style);
style.type = 'text/css';
style.id = 'css_for_bebras_width';
style.appendChild(document.createTextNode(css));
}
EOD;
$result .= html_writer::script($scr);
return $result;
}
public function specific_feedback(question_attempt $qa) {
$question = $qa->get_question();
$answer = $question->get_matching_answer(array('answer' => $qa->get_last_qt_var('answer')));
if (!$answer || !$answer->feedback) {
return '';
}
return $question->format_text(
$answer->feedback, $answer->feedbackformat,
$qa,
'question',
'answerfeedback', $answer->id
);
}
public function correct_response(question_attempt $qa) {
return '';
}
protected function general_feedback(question_attempt $qa) {
$feedback = $qa->get_question()->format_generalfeedback($qa);
if (!$feedback) {
return '';
}
$ifrid = $qa->get_qt_field_name('answer');
$ifrid = str_replace(':', '_', $ifrid);
$ifrid .= '-explanation';
return $this->iframe_encapsulate($feedback, $ifrid, false);
}
}