Skip to content

Commit 2f2341e

Browse files
committed
feat: replace PLUGINFILE-URLs in editor text with QPy-URLs
Closes: #237
1 parent 76c41c5 commit 2f2341e

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

question.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,32 @@ private function prepare_responses_for_server(array $responseqtdata): array {
518518

519519
$editors = [];
520520
foreach ($raweditors as $editorname => $editordata) {
521+
$text = $editordata->text;
521522
$filemetas = [];
523+
522524
if (isset($filesbyfield[$editorname])) {
525+
$filenamestorefs = [];
526+
523527
foreach ($filesbyfield[$editorname] as $filename => $file) {
524-
$filemetas[] = file_metadata::from_stored_file($file, overridename: $filename);
528+
$filemetas[] = $filemeta = file_metadata::from_stored_file($file, overridename: $filename);
529+
$filenamestorefs[$filename] = $filemeta->fileref;
530+
}
531+
532+
// Filenames may be prefixes of each other, so we replace the longest ones first.
533+
uksort($filenamestorefs, fn($a, $b) => strlen($b) - strlen($a));
534+
foreach ($filenamestorefs as $filename => $fileref) {
535+
$text = str_replace('@@PLUGINFILE@@/' . $filename, 'qpy://response/' . $fileref, $text);
525536
}
526537
}
527538

528-
// TODO: Turn @@PLUGINFILE@@-links into QPy-URLs?
539+
if (str_contains($text, '@@PLUGINFILE@@')) {
540+
debugging('Editor text still contains @@PLUGINFILE@@-placeholders after replacement.');
541+
$brokenfile = (new moodle_url('/brokenfile.php'))->out();
542+
$text = str_replace('@@PLUGINFILE@@', $brokenfile, $text);
543+
}
529544

530545
$editors[$editorname] = new wysiwyg_editor_data(
531-
text: $editordata->text,
546+
text: $text,
532547
textformat: $editordata->format,
533548
files: $filemetas,
534549
);

0 commit comments

Comments
 (0)