-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
458 lines (414 loc) · 18.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
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
// This file is part of the QuestionPy Moodle plugin - https://questionpy.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/>.
/**
* QuestionPy renderer class
*
* @package qtype_questionpy
* @copyright 2022 Martin Gauk, TU Berlin, innoCampus - www.questionpy.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use qtype_questionpy\constants;
use qtype_questionpy\local\api\attempt_ui;
use qtype_questionpy\local\api\feedback_type;
use qtype_questionpy\local\api\js_module_call;
use qtype_questionpy\local\attempt_ui\question_ui_renderer;
/**
* Generates the output for QuestionPy questions.
*
* @copyright 2022 Martin Gauk, TU Berlin, innoCampus - www.questionpy.org
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_questionpy_renderer extends qtype_renderer {
/**
* Return any HTML that needs to be included in the page's <head> when this
* question is used.
* @param question_attempt $qa the question attempt that will be displayed on the page.
* @return string HTML fragment.
*/
public function head_code(question_attempt $qa) {
return '';
}
/**
* Renders a message informing the user about an error.
*
* @return string
* @throws \core\exception\moodle_exception
*/
private function render_error(): string {
return $this->output->render_from_template('qtype_questionpy/render_error', [
'message' => 'There was an error attempting to view the question.',
'info' => 'Please contact an administrator.',
]);
}
/**
* Generate the display of the formulation part of the question. This is the
* area that contains the question text, and the controls for students to
* input their answers. Some question types also embed bits of feedback, for
* example ticks and crosses, in this area.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should not be displayed.
* @return string HTML fragment.
* @throws moodle_exception
*/
public function formulation_and_controls(question_attempt $qa, question_display_options $options): string {
$question = $qa->get_question();
assert($question instanceof qtype_questionpy_question);
if ($question->errorduringload) {
// This should already have been logged in qtype_questionpy_question.
return $this->render_error();
}
try {
$questiondivid = $qa->get_outer_question_div_unique_id();
$autosavehintid = $questiondivid . '-autosave';
$formulationcb = function (qtype_questionpy_renderer $renderer) use ($qa, $question, $options, $autosavehintid) {
return $renderer->formulation_controls_feedback_in_iframe($qa, $question->ui, $options, $autosavehintid);
};
$iframesrc = $this->get_iframe_document($options->context, $question, $formulationcb);
// A hidden input field is used to tell the quiz autosaver that the user changed their question answer
// in the iframe. The value is increased by one every time. The autosaver detects this modification and will
// save all answers.
$iframeid = $questiondivid . '-iframe';
$autosavehintname = 'qpy-autosave-' . $questiondivid;
$this->page->requires->js_call_amd(
'qtype_questionpy/view_question',
'addIframeFormDataOnSubmit',
[$iframeid, $qa->get_field_prefix() . constants::QT_VAR_RESPONSE]
);
return <<<EOA
<input type="hidden" name="{$autosavehintname}" id="{$autosavehintid}" value="0">
<iframe id="{$iframeid}" srcdoc="{$iframesrc}"></iframe>
EOA;
} catch (Throwable $t) {
global $USER;
// Trigger error event.
$params = [
'context' => $this->page->context,
'relateduserid' => $USER->id,
'other' => [
'questionid' => $question->id,
'questionattemptid' => $qa->get_database_id(),
'errormessage' => $t->getMessage(),
],
];
$event = \qtype_questionpy\event\viewing_attempt_failed::create($params);
$event->trigger();
debugging($event->get_description(), backtrace: $t->getTrace());
return $this->render_error();
}
}
/**
* Generate the HTML document that is put into an iframe.
*
* A new moodle_page object is created (and temporarily swapped with $PAGE) to get a usual Moodle page with the
* embedded layout. $contentcb is called to get the actual content. A callback is used here because it
* needs to be called on a new renderer instance that is connected with the temporary moodle_page.
*
* @param context $context
* @param qtype_questionpy_question $question
* @param callable $contentcb Callback to get the main content that should be part of the iframe.
* @return string HTML document already encoded with htmlspecialchars to put in iframe srcdoc
* @throws \core\exception\coding_exception
*/
protected function get_iframe_document(context $context, qtype_questionpy_question $question, callable $contentcb): string {
// We know what we are doing here. We are touching these globals on purpose.
// phpcs:disable moodle.PHP.ForbiddenGlobalUse.BadGlobal
global $PAGE, $OUTPUT;
$oldpage = $PAGE;
$oldoutput = $OUTPUT;
// Initialize output buffer.
// We do this to ensure that any echo, var_dump, etc. statements are included in the iframe contents.
ob_start();
try {
$classname = get_class($oldpage); // The class of $PAGE may be customized using $CFG->moodlepageclass.
/** @var moodle_page $PAGE */
$PAGE = new $classname();
/** @var \core\output\core_renderer $OUTPUT */
$OUTPUT = new bootstrap_renderer(); // Class bootstrap_renderer will initialize $OUTPUT on first use.
$PAGE->set_context($context);
$PAGE->set_pagelayout('embedded');
$PAGE->set_pagetype($oldpage->pagetype);
$PAGE->set_url($oldpage->url);
$PAGE->add_body_class('questionpy-iframe-body');
// Get a new instance of this renderer for the new $PAGE object to render the iframe contents.
// This is necessary so that any JS/CSS requirements get added to the new page's page_requirements_manager.
/** @var self $qpyrenderer */
$qpyrenderer = $PAGE->get_renderer('qtype_questionpy');
// Render iframe contents before the header is printed to allow CSS to be added to the page header.
$iframecontents = $contentcb($qpyrenderer);
// Write iframe source into the output buffer.
echo $OUTPUT->header();
echo $this->get_iframe_js_before();
echo $this->get_iframe_js_importmap($question);
echo $iframecontents;
echo $OUTPUT->footer();
} finally {
$PAGE = $oldpage;
$OUTPUT = $oldoutput;
}
// phpcs:enable
$iframesrc = ob_get_clean();
return htmlspecialchars($iframesrc);
}
/**
* Gather the formulation/controls and feedbacks of a question attempt.
*
* Prepares everything in order to display the question in an iframe.
*
* @param question_attempt $qa the question attempt to display.
* @param attempt_ui $ui
* @param question_display_options $options controls what should and should not be displayed.
* @param string $autosavehintinputid
* @return string HTML fragment.
* @throws moodle_exception
*/
protected function formulation_controls_feedback_in_iframe(question_attempt $qa, attempt_ui $ui,
question_display_options $options, string $autosavehintinputid): string {
$renderer = question_ui_renderer::render($ui->formulation, $ui->placeholders, $options, $qa);
$warningshtml = '';
if ($renderer->warnings) {
global $USER;
$isstudent = $qa->get_step(0)->get_user_id() === $USER->id;
$warningshtml .= $this->output->render_from_template('qtype_questionpy/render_warnings', [
'warnings' => $renderer->warnings,
'should_use_list' => count($renderer->warnings) > 1,
'should_show_hint_contact_teachers' => $isstudent,
]);
}
$feedback = html_writer::nonempty_tag(
'div',
$this->feedback_in_iframe($qa, $options),
['class' => 'outcome clearfix']
);
$roles = $renderer->get_user_roles();
$this->page->requires->js_call_amd(
'qtype_questionpy/view_question',
'init',
[$autosavehintinputid, $roles]
);
$this->add_package_js_calls($ui->javascriptcalls, $roles, $options);
return $this->render_from_template('qtype_questionpy/iframe_question_content', [
'question_html' => $renderer->html,
'feedback_html' => $feedback,
'warnings_html' => $warningshtml,
]);
}
/**
* Filter the JavaScript calls requested by the package and call the functions.
*
* @param js_module_call[] $jscalls
* @param string[] $roles names of qpy user roles
* @param question_display_options $options
* @return void
*/
protected function add_package_js_calls(array $jscalls, array $roles, question_display_options $options): void {
$calls = [];
foreach ($jscalls as $call) {
// If there are role/feedback conditions, both have to be matched.
if ($call->ifrole && !in_array(strtolower($call->ifrole), $roles)) {
continue;
}
if (
$call->iffeedbacktype && !(
($call->iffeedbacktype == feedback_type::general_feedback->value && $options->generalfeedback)
|| ($call->iffeedbacktype == feedback_type::specific_feedback->value && $options->feedback)
|| ($call->iffeedbacktype == feedback_type::right_answer->value && $options->rightanswer)
)
) {
continue;
}
if ($call->data !== null) {
try {
// Decode and encode the data again to be sure that it is a properly escaped JSON (no XSS in script tag).
$decodedjson = json_decode(
$call->data,
associative: false,
depth: 64,
flags: JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE
);
$jsondata = json_encode($decodedjson);
} catch (JsonException | ValueError $e) {
debugging(
'qtype_questionpy: Error decoding JSON data from package: ' . $e->getMessage(),
DEBUG_DEVELOPER,
backtrace: $e->getTrace()
);
$calls[] = "window.console.error('There was an error (on the server side) decoding the JSON data from " .
"the package ({$call->module} -> {$call->function} will not be called).');";
continue;
}
} else {
$jsondata = 'undefined';
}
$calls[] = <<<EOF
M.util.js_pending("qtype_questionpy/package/{$call->module}");
import("{$call->module}").then(module => {
const data = {$jsondata};
module["{$call->function}"](attempt, data);
M.util.js_complete("qtype_questionpy/package/{$call->module}");
});
EOF;
}
if ($calls) {
$imploded = implode("\n", $calls);
$inlinejs = <<<EOD
M.util.js_pending('qtype_questionpy/view_question');
require(['qtype_questionpy/view_question'], (view) => {
const attempt = view.getAttempt()
{$imploded}
M.util.js_complete('qtype_questionpy/view_question');
});
EOD;
// We are not using js_call_amd here, because (a) it is not possible to call the ES6 import function from
// amd/src (it is transpiled to use RequireJS) and (b) js_call_amd has a quite low character limit for the params.
$this->page->requires->js_amd_inline($inlinejs);
}
}
/**
* JavaScript within the iframe that is executed before the formulation part.
*
* The iframe should quickly resize its height to its scroll height to make it a seamless part of the page.
*
* @return string
*/
protected function get_iframe_js_before(): string {
return <<<'END'
<script>
"use strict";
(function () {
// Add <base target='_blank'> tag to head so links are opened in a new tab.
const base = document.createElement('base');
base.target = '_blank';
document.getElementsByTagName('head')[0].appendChild(base);
// Resize iframe when content height changes.
const resize = function() {
if (window.frameElement) {
window.frameElement.style.height = document.body.scrollHeight + 'px';
window.frameElement.style.width = '100%';
}
};
resize();
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(document.body);
})();
</script>
END;
}
/**
* Generate an importmap script element
*
* @param qtype_questionpy_question $question
* @return string
*/
protected function get_iframe_js_importmap(qtype_questionpy_question $question): string {
$importmap = [
'imports' => [],
];
foreach ($question->packagedependencies as $dependency) {
$path = "@{$dependency->namespace}/{$dependency->shortname}/";
$mapsto = \moodle_url::make_pluginfile_url(
$question->contextid,
'qtype_questionpy',
'static',
null,
"/{$question->packagehash}/{$dependency->namespace}/{$dependency->shortname}/js/",
''
);
$importmap['imports'][$path] = $mapsto->out();
}
$importmapjson = json_encode($importmap, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
return <<<"EOD"
<script type="importmap">
$importmapjson
</script>
EOD;
}
/**
* Generate the display of the outcome part of the question.
*
* We reimplement this method instead of overriding the more specific methods {@see specific_feedback()},
* {@see general_feedback()} and {@see correct_response()} because those aren't passed the
* {@see question_display_options}.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should not be displayed.
* @return string HTML fragment.
* @throws coding_exception
*/
protected function feedback_in_iframe(question_attempt $qa, question_display_options $options): string {
$question = $qa->get_question();
assert($question instanceof qtype_questionpy_question);
$output = '';
$hint = null;
if ($options->feedback && !is_null($question->ui->specificfeedback)) {
$renderer = question_ui_renderer::render($question->ui->specificfeedback, $question->ui->placeholders, $options, $qa);
$output .= html_writer::nonempty_tag(
'div',
$renderer->html,
['class' => 'specificfeedback', 'id' => 'qpy-specific-feedback']
);
$hint = $qa->get_applicable_hint();
}
if ($options->numpartscorrect) {
$output .= html_writer::nonempty_tag('div', $this->num_parts_correct($qa), ['class' => 'numpartscorrect']);
}
if ($hint) {
$output .= $this->hint($qa, $hint);
}
if ($options->generalfeedback && !is_null($question->ui->generalfeedback)) {
$renderer = question_ui_renderer::render($question->ui->generalfeedback, $question->ui->placeholders, $options, $qa);
$output .= html_writer::nonempty_tag(
'div',
$renderer->html,
['class' => 'generalfeedback', 'id' => 'qpy-general-feedback']
);
}
if ($options->rightanswer && !is_null($question->ui->rightanswer)) {
$renderer = question_ui_renderer::render($question->ui->rightanswer, $question->ui->placeholders, $options, $qa);
$output .= html_writer::nonempty_tag(
'div',
$renderer->html,
['class' => 'rightanswer', 'id' => 'qpy-right-answer']
);
}
if ($output) {
// Copied from \core_question_renderer::question.
$output = html_writer::tag(
'h4',
get_string('feedback', 'question'),
['class' => 'accesshide']
) . $output;
}
return $output;
}
/**
* Generate the display of the outcome part of the question. This is the
* area that contains the various forms of feedback. This function generates
* the content of this area belonging to the question type.
*
* Subclasses will normally want to override the more specific methods
* {specific_feedback()}, {general_feedback()} and {correct_response()}
* that this method calls.
*
* @param question_attempt $qa the question attempt to display.
* @param question_display_options $options controls what should and should not be displayed.
* @return string HTML fragment.
*/
public function feedback(question_attempt $qa, question_display_options $options) {
// We display all feedbacks in the iframe together with the formulation.
return '';
}
}