MDL-68665 assignfeedback_editpdf: Improve caching on stamps
authorJamie Stamp <jamie.stamp@catalyst-eu.net>
Thu, 1 Oct 2020 15:59:42 +0000 (16:59 +0100)
committerJamie Stamp <jamie.stamp@catalyst-eu.net>
Thu, 1 Oct 2020 15:59:42 +0000 (16:59 +0100)
mod/assign/feedback/editpdf/lib.php
mod/assign/feedback/editpdf/locallib.php

index 143acff..7e1d143 100644 (file)
@@ -47,7 +47,24 @@ function assignfeedback_editpdf_pluginfile($course,
 
     require_once($CFG->dirroot . '/mod/assign/locallib.php');
 
-    if ($context->contextlevel == CONTEXT_MODULE) {
+    if ($filearea === 'stamps' && $context->contextlevel === CONTEXT_SYSTEM) {
+
+        $itemid = (int)array_shift($args);
+
+        $relativepath = implode('/', $args);
+
+        $fullpath = "/{$context->id}/assignfeedback_editpdf/$filearea/$itemid/$relativepath";
+
+        $fs = get_file_storage();
+        if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
+            return false;
+        }
+
+        $options['cacheability'] = 'public';
+        $options['immutable'] = true;
+
+        send_stored_file($file, 0, 0, true, $options);
+    } else if ($context->contextlevel == CONTEXT_MODULE) {
 
         require_login($course, false, $cm);
         $itemid = (int)array_shift($args);
index 03f9f26..2f039b2 100644 (file)
@@ -69,6 +69,7 @@ class assign_feedback_editpdf extends assign_feedback_plugin {
                                                                  $attempt);
 
         $stampfiles = array();
+        $systemfiles = array();
         $fs = get_file_storage();
         $syscontext = context_system::instance();
 
@@ -82,13 +83,17 @@ class assign_feedback_editpdf extends assign_feedback_plugin {
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 if ($filename !== '.') {
+                    $systemfiles[] = $filename;
+
+                    $existingfile = $fs->file_exists(
+                        $this->assignment->get_context()->id,
+                        'assignfeedback_editpdf',
+                        'stamps',
+                        $grade->id,
+                        '/',
+                        $file->get_filename()
+                    );
 
-                    $existingfile = $fs->get_file($this->assignment->get_context()->id,
-                                                  'assignfeedback_editpdf',
-                                                  'stamps',
-                                                  $grade->id,
-                                                  '/',
-                                                  $file->get_filename());
                     if (!$existingfile) {
                         $newrecord = new stdClass();
                         $newrecord->contextid = $this->assignment->get_context()->id;
@@ -109,13 +114,24 @@ class assign_feedback_editpdf extends assign_feedback_plugin {
             foreach ($files as $file) {
                 $filename = $file->get_filename();
                 if ($filename !== '.') {
-                    $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id,
-                                                   'assignfeedback_editpdf',
-                                                   'stamps',
-                                                   $grade->id,
-                                                   '/',
-                                                   $file->get_filename(),
-                                                   false);
+
+                    // Check to see if the file exists in system context.
+                    $insystemfiles = in_array($filename, $systemfiles);
+
+                    // If stamp is available in the system context, use that copy.
+                    // If not then fall back to file saved in the files table.
+                    $context = $insystemfiles ? $syscontext->id : $this->assignment->get_context()->id;
+                    $itemid = $insystemfiles ? 0 : $grade->id;
+
+                    $url = moodle_url::make_pluginfile_url(
+                        $context,
+                        'assignfeedback_editpdf',
+                        'stamps',
+                        $itemid,
+                        '/',
+                        $file->get_filename(),
+                        false
+                    );
                     array_push($stampfiles, $url->out());
                 }
             }