Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * Callbacks
  19   *
  20   * @package   customfield_textarea
  21   * @copyright 2018 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  /**
  28   * Serve the files from the customfield_textarea file areas
  29   *
  30   * @param stdClass $course the course object
  31   * @param stdClass $cm the course module object
  32   * @param context $context the context
  33   * @param string $filearea the name of the file area
  34   * @param array $args extra arguments (itemid, path)
  35   * @param bool $forcedownload whether or not force download
  36   * @param array $options additional options affecting the file serving
  37   * @return bool false if the file not found, just send the file otherwise and do not return
  38   */
  39  function customfield_textarea_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  40      global $DB;
  41  
  42      $itemid = array_shift($args);
  43      if ($filearea === 'value') {
  44          // Value of the data, itemid = id in data table.
  45          $datarecord = $DB->get_record(\core_customfield\data::TABLE, ['id' => $itemid], '*', MUST_EXIST);
  46          $field = \core_customfield\field_controller::create($datarecord->fieldid);
  47          $data = \core_customfield\data_controller::create(0, $datarecord, $field);
  48          $handler = $field->get_handler();
  49          if ($field->get('type') !== 'textarea' || !$handler->can_view($field, $data->get('instanceid'))
  50                  || $data->get_context()->id != $context->id) {
  51              send_file_not_found();
  52          }
  53      } else if ($filearea === 'defaultvalue') {
  54          // Default value of the field, itemid = id in the field table.
  55          $field = \core_customfield\field_controller::create($itemid);
  56          $handler = $field->get_handler();
  57          if ($field->get('type') !== 'textarea' || $handler->get_configuration_context()->id != $context->id) {
  58              send_file_not_found();
  59          }
  60      } else {
  61          send_file_not_found();
  62      }
  63  
  64      $filename = array_pop($args); // The last item in the $args array.
  65      $filepath = '/' . ($args ? implode('/', $args) . '/' : '');
  66  
  67      // Retrieve the file from the Files API.
  68      $fs = get_file_storage();
  69      $file = $fs->get_file($context->id, 'customfield_textarea', $filearea, $itemid, $filepath, $filename);
  70      if (!$file) {
  71          send_file_not_found();
  72      }
  73  
  74      // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
  75      send_file($file, 86400, 0, $forcedownload, $options);
  76  }