Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file contains the version information for the comments feedback plugin
  19   *
  20   * @package assignfeedback_editpdf
  21   * @copyright  2012 Davo Smith
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/assign/locallib.php');
  29  
  30  /**
  31   * Serves assignment feedback and other files.
  32   *
  33   * @param mixed $course course or id of the course
  34   * @param mixed $cm course module or id of the course module
  35   * @param context $context
  36   * @param string $filearea
  37   * @param array $args
  38   * @param bool $forcedownload
  39   * @param array $options - List of options affecting file serving.
  40   * @return bool false if file not found, does not return if found - just send the file
  41   */
  42  function assignfeedback_editpdf_pluginfile(
  43      $course,
  44      $cm,
  45      context $context,
  46      $filearea,
  47      $args,
  48      $forcedownload,
  49      array $options = array()
  50  ) {
  51      global $DB;
  52      if ($filearea === 'systemstamps') {
  53  
  54          if ($context->contextlevel !== CONTEXT_SYSTEM) {
  55              return false;
  56          }
  57  
  58          $filename = array_pop($args);
  59          $filepath = '/' . implode('/', $args) . '/';
  60  
  61          $fs = get_file_storage();
  62          $file = $fs->get_file($context->id, 'assignfeedback_editpdf', $filearea, 0, $filepath, $filename);
  63          if (!$file) {
  64              return false;
  65          }
  66  
  67          $options['cacheability'] = 'public';
  68          $options['immutable'] = true;
  69  
  70          send_stored_file($file, null, 0, false, $options);
  71      }
  72  
  73      if ($context->contextlevel == CONTEXT_MODULE) {
  74  
  75          require_login($course, false, $cm);
  76          $itemid = (int)array_shift($args);
  77  
  78          $assign = new assign($context, $cm, $course);
  79  
  80          $record = $DB->get_record('assign_grades', array('id' => $itemid), 'userid,assignment', MUST_EXIST);
  81          $userid = $record->userid;
  82          if ($assign->get_instance()->id != $record->assignment) {
  83              return false;
  84          }
  85  
  86          // Rely on mod_assign checking permissions.
  87          if (!$assign->can_view_submission($userid)) {
  88              return false;
  89          }
  90  
  91          $relativepath = implode('/', $args);
  92  
  93          $fullpath = "/{$context->id}/assignfeedback_editpdf/$filearea/$itemid/$relativepath";
  94  
  95          $fs = get_file_storage();
  96          if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
  97              return false;
  98          }
  99          // Download MUST be forced - security!
 100          send_stored_file($file, 0, 0, true, $options);// Check if we want to retrieve the stamps.
 101      }
 102  
 103  }
 104  
 105  /**
 106   * Files API hook to remove stale conversion records.
 107   *
 108   * When a file is update, its contenthash will change, but its ID
 109   * remains the same. The document converter API records source file
 110   * IDs and destination file IDs. When a file is updated, the document
 111   * converter API has no way of knowing that the content of the file
 112   * has changed, so it just serves the previously stored destination
 113   * file.
 114   *
 115   * In this hook we check if the contenthash has changed, and if it has
 116   * we delete the existing conversion so that a new one will be created.
 117   *
 118   * @param stdClass $file The updated file record.
 119   * @param stdClass $filepreupdate The file record pre-update.
 120   */
 121  function assignfeedback_editpdf_after_file_updated(stdClass $file, stdClass $filepreupdate) {
 122      $contenthashchanged = $file->contenthash !== $filepreupdate->contenthash;
 123      if ($contenthashchanged && $file->component == 'assignsubmission_file' && $file->filearea == 'submission_files') {
 124          $fs = get_file_storage();
 125          $file = $fs->get_file_by_id($file->id);
 126          $conversions = \core_files\conversion::get_conversions_for_file($file, 'pdf');
 127  
 128          foreach ($conversions as $conversion) {
 129              if ($conversion->get('id')) {
 130                  $conversion->delete();
 131              }
 132          }
 133      }
 134  }