Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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   core_customfield
  21   * @copyright 2018 Marina Glancy
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  use core_external\external_api;
  26  
  27  defined('MOODLE_INTERNAL') || die;
  28  
  29  /**
  30   * Edit customfield elements inplace
  31   *
  32   * @param string $itemtype
  33   * @param int    $itemid
  34   * @param string $newvalue
  35   * @return \core\output\inplace_editable
  36   */
  37  function core_customfield_inplace_editable($itemtype, $itemid, $newvalue) {
  38      if ($itemtype === 'category') {
  39          $category = core_customfield\category_controller::create($itemid);
  40          $handler = $category->get_handler();
  41          external_api::validate_context($handler->get_configuration_context());
  42          if (!$handler->can_configure()) {
  43              throw new moodle_exception('nopermissionconfigure', 'core_customfield');
  44          }
  45          $newvalue = clean_param($newvalue, PARAM_TEXT);
  46          $handler->rename_category($category, $newvalue);
  47          return \core_customfield\api::get_category_inplace_editable($category, true);
  48      }
  49  }
  50  
  51  /**
  52   * Serve the files from the core_customfield file areas
  53   *
  54   * @param stdClass $course the course object
  55   * @param stdClass $cm the course module object
  56   * @param context $context the context
  57   * @param string $filearea the name of the file area
  58   * @param array $args extra arguments (itemid, path)
  59   * @param bool $forcedownload whether or not force download
  60   * @param array $options additional options affecting the file serving
  61   * @return bool false if the file not found, just send the file otherwise and do not return
  62   */
  63  function core_customfield_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
  64      if ($filearea !== 'description') {
  65          return false;
  66      }
  67  
  68      $itemid = array_shift($args);
  69      $filename = array_pop($args); // The last item in the $args array.
  70  
  71      $field = \core_customfield\field_controller::create($itemid);
  72      $handler = $field->get_handler();
  73      if ($handler->get_configuration_context()->id != $context->id) {
  74          return false;
  75      }
  76  
  77      // Retrieve the file from the Files API.
  78      $fs = get_file_storage();
  79      $file = $fs->get_file($context->id, 'core_customfield', $filearea, $itemid, '/', $filename);
  80      if (!$file) {
  81          return false; // The file does not exist.
  82      }
  83  
  84      // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
  85      send_stored_file($file, DAYSECS, 0, $forcedownload, $options);
  86  }