Skip to content

Commit 796802b

Browse files
committed
feat: turn PLUGINFILE-URLs into draftfile.php URLs during render
1 parent aab8f43 commit 796802b

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

classes/local/attempt_ui/qpy_rich_text_editor.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use DOMNode;
2424
use file_exception;
2525
use moodle_exception;
26+
use moodle_url;
2627
use MoodleQuickForm_editor;
2728
use qtype_questionpy\local\files\response_file_service;
2829
use qtype_questionpy\local\files\validatable_upload_limits;
@@ -52,6 +53,8 @@ class qpy_rich_text_editor implements custom_xhtml_element {
5253
* Trivial private constructor. Use {@see from_element()}.
5354
* @param DOMElement $element
5455
* @param string $name
56+
* @param bool $required
57+
* @param string|null $default
5558
*/
5659
private function __construct(
5760
/** @var DOMElement */
@@ -60,6 +63,8 @@ private function __construct(
6063
public readonly string $name,
6164
/** @var bool */
6265
public readonly bool $required,
66+
/** @var string */
67+
public readonly ?string $default,
6368
) {
6469
}
6570

@@ -107,7 +112,9 @@ public static function from_element(DOMElement $element): ?static {
107112

108113
$required = $element->hasAttribute('required');
109114

110-
return new static($element, $name, $required);
115+
$default = $element->hasAttribute('default') ? $element->getAttribute('default') : null;
116+
117+
return new static($element, $name, $required, $default);
111118
}
112119

113120
/**
@@ -122,20 +129,25 @@ public static function from_element(DOMElement $element): ?static {
122129
* @throws stored_file_creation_exception
123130
*/
124131
public function render(question_attempt $qa, question_ui_renderer $renderer): DOMNode {
132+
// TODO: Maybe separate readonly view?
125133
$limits = $this->get_limits_in($renderer->options->context);
126134

127135
$alleditorsdata = utils::get_qpy_editors_data($qa);
128136
$mydata = $alleditorsdata[$this->name] ?? null;
129137

130138
$options = [
131-
'context' => $renderer->options->context,
139+
'context' => $renderer->options->context,
132140
];
133141
$values = [
134-
'text' => $mydata !== null ? $mydata->text : '',
135-
'format' => $mydata !== null ? $mydata->format : FORMAT_HTML,
142+
'text' => $this->default === null ? '' : s($this->default),
143+
'format' => $mydata !== null ? $mydata->format : FORMAT_HTML,
136144
];
137145
if ($limits->maxfiles === 0) {
138146
$options['enable_filemanagement'] = false;
147+
148+
if ($mydata !== null) {
149+
$values['text'] = $mydata->text;
150+
}
139151
} else {
140152
$combinedfilearea = $renderer->prepare_combined_draft_area($qa);
141153
$rfs = di::get(response_file_service::class);
@@ -151,6 +163,14 @@ public function render(question_attempt $qa, question_ui_renderer $renderer): DO
151163
$options['areamaxbytes'] = $limits->areamaxbytes;
152164

153165
$values['itemid'] = $splitdraftitemid;
166+
167+
if ($mydata !== null) {
168+
// Replace the @@PLUGINFILE@@ placeholders with the correct draftfile-URL prefix.
169+
// The inverse is done in JS for want of a better place.
170+
$prefix = moodle_url::make_draftfile_url($splitdraftitemid, '/', '');
171+
assert(str_ends_with($prefix, '/'));
172+
$values['text'] = str_replace('@@PLUGINFILE@@/', $prefix, $mydata->text);
173+
}
154174
}
155175

156176
// This will be used by the JS code to separately handle the editor data.

0 commit comments

Comments
 (0)