-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.php
More file actions
598 lines (541 loc) · 24.4 KB
/
renderer.php
File metadata and controls
598 lines (541 loc) · 24.4 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<?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 core\di;
use core\output\html_writer;
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;
use qtype_questionpy\local\files\response_file_service;
use qtype_questionpy\local\files\static_file_service;
use qtype_questionpy\qpy_question_display_options;
use qtype_questionpy\utils;
/**
* 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' => get_string('render_error_message', 'qtype_questionpy'),
'info' => get_string('contact_administrator', 'qtype_questionpy'),
]);
}
/**
* 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. May be
* {@see qpy_question_display_options}.
* @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 {
/** @var array<string, int>|null $uploaddraftareas */
$uploaddraftareas = null;
$questiondivid = $qa->get_outer_question_div_unique_id();
$qpyresponseid = $questiondivid . '-qpy-response';
$formulationcb = function (qtype_questionpy_renderer $renderer)
use ($qa, $question, $options, $qpyresponseid, &$uploaddraftareas) {
$quirenderer = question_ui_renderer::render($question->ui->formulation, $question->ui->placeholders, $options, $qa);
$uploaddraftareas = $quirenderer->draftareas;
return $renderer->formulation_controls_feedback_in_iframe(
$qa,
$question->ui,
$quirenderer,
$options,
$qpyresponseid
);
};
$iframesrc = $this->get_iframe_document($options->context, $question, $formulationcb);
$iframeid = $questiondivid . '-iframe';
$qpyresponsename = $qa->get_field_prefix() . constants::QT_VAR_RESPONSE;
if (!$options->readonly) {
$this->page->requires->js_call_amd(
'qtype_questionpy/view_question',
'addIframeFormDataOnSubmit',
[$iframeid, $qpyresponsename]
);
}
$result = '';
if (
($options->qpyattemptdetailslink ?? question_display_options::VISIBLE) === question_display_options::VISIBLE
&& has_capability(constants::ROLE_VIEW_DETAILS, $options->context)
) {
$detailsurl = new moodle_url(
'/question/type/questionpy/attemptdetails.php',
['attemptid' => $qa->get_database_id()]
);
$result .= "<a class='qpy-details-link' href='{$detailsurl->out()}' target='_blank'>"
. get_string('attempt_detail_link', 'qtype_questionpy') . '</a>';
}
// When the user changes their answer within the iframe, the value of the following hidden input field gets updated.
// The quiz autosaver detects this modification and will save all answers.
// This hidden field must exist in the outer form for the autosaver to work properly as it relies on the
// `HTMLFormElement.elements` attribute.
$lastqpyresponse = s($qa->get_last_qt_var(constants::QT_VAR_RESPONSE) ?? '{}');
$result .= <<<EOA
<input type="hidden" name="{$qpyresponsename}" id="{$qpyresponseid}" value="{$lastqpyresponse}">
<iframe id="{$iframeid}" srcdoc="{$iframesrc}"></iframe>
EOA;
if ($uploaddraftareas === null) {
throw new coding_exception('$uploaddraftareas was not set');
}
if (count($uploaddraftareas) > 0) {
$result .= html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => $qa->get_field_prefix() . constants::FORM_DRAFT_AREAS,
'value' => json_encode($uploaddraftareas, JSON_FORCE_OBJECT),
]);
$combineddraftareaid = file_get_unused_draft_itemid();
$result .= html_writer::empty_tag('input', [
'type' => 'hidden',
'name' => $qa->get_field_prefix() . constants::QT_VAR_RESPONSE_FILES,
'value' => $combineddraftareaid,
]);
}
return $result;
} 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 $CFG, $PAGE, $OUTPUT;
$oldpage = $PAGE;
$oldoutput = $OUTPUT;
$oldclosingtags = $CFG->closingtags ?? '';
// 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 $this->get_package_css_links($question->ui->cssfiles, $question);
echo $iframecontents;
echo $OUTPUT->footer();
// The function $OUTPUT->footer stores the HTML closing tags in this global.
echo $CFG->closingtags ?? '';
} finally {
$PAGE = $oldpage;
$OUTPUT = $oldoutput;
$CFG->closingtags = $oldclosingtags;
}
// 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_ui_renderer $renderer Render result.
* @param question_display_options $options controls what should and should not be displayed.
* @param string $qpyresponseid
* @return string HTML fragment.
* @throws moodle_exception
*/
protected function formulation_controls_feedback_in_iframe(
question_attempt $qa, attempt_ui $ui, question_ui_renderer $renderer,
question_display_options $options, string $qpyresponseid
): string {
global $CFG;
$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',
[
$options->readonly,
$options->generalfeedback === question_display_options::VISIBLE,
$options->feedback === question_display_options::VISIBLE,
$options->rightanswer === question_display_options::VISIBLE,
$options->correctness === question_display_options::VISIBLE,
$qpyresponseid,
$roles,
utils::get_qpy_response($qa)->data ?? (object)[],
intval($CFG->branch),
]
);
$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);
}
}
/**
* Put together an HTML string of `<link rel="stylesheet"/>` elements that load the given CSS files.
*
* Call within the iframe.
*
* @param string[] $cssfiles HTTPS or QPy URLs
* @param qtype_questionpy_question $question
* @return string
* @throws coding_exception
*/
private function get_package_css_links(array $cssfiles, qtype_questionpy_question $question): string {
$sfs = di::get(static_file_service::class);
$elements = [];
foreach (array_unique($cssfiles) as $uri) {
if (str_starts_with($uri, 'qpy://static/')) {
$uri = $sfs->resolve_qpy_url(substr($uri, strlen('qpy://static')), $question);
} else if (str_starts_with($uri, 'qpy://')) {
debugging("Stylesheet URI '$uri' looks like a QPy-URI, but could not be parsed.");
continue;
} else if (!str_starts_with($uri, 'https://')) {
// Also accept arbitrary HTTPS URIs to support loading stylesheets from CDNs.
debugging("Stylesheet URI '$uri' does not use a supported scheme.");
continue;
}
$uri = s($uri);
$elements[] = "<link rel='stylesheet' href='$uri'/>";
}
return implode("\n", $elements);
}
/**
* 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 '';
}
/**
* Renders a read-only list of the response files belonging to the given field, with clickable download links.
*
* @param question_attempt $qa
* @param string $fieldname
* @param question_display_options $options
* @return string
* @throws coding_exception
*/
public function render_readonly_file_view(question_attempt $qa, string $fieldname, question_display_options $options): string {
// Loosely based on qtype_essay_renderer::files_read_only.
$allfiles = $qa->get_last_qt_files(constants::QT_VAR_RESPONSE_FILES, $options->context->id);
$result = html_writer::start_tag('ul', [
'class' => 'list-unstyled m-0',
]);
foreach (response_file_service::filter_combined_files_for_field($allfiles, $fieldname) as $filename => $file) {
$result .= html_writer::tag('li', html_writer::link(
url: $qa->get_response_file_url($file),
text: $this->output->pix_icon(
file_file_icon($file),
get_mimetype_description($file),
'moodle',
['class' => 'icon']
) . ' ' . s($filename),
));
}
$result .= html_writer::end_tag('ul');
return $result;
}
}