Merge branch 'MDL-69251-311' of https://github.com/snake/moodle into MOODLE_311_STABLE
[moodle.git] / customfield / lib.php
blobb3ec2d66adf9a71d11068d0bdcf99e6ec0a19779
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.
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/>.
17 /**
18 * Callbacks
20 * @package core_customfield
21 * @copyright 2018 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die;
27 /**
28 * Edit customfield elements inplace
30 * @param string $itemtype
31 * @param int $itemid
32 * @param string $newvalue
33 * @return \core\output\inplace_editable
35 function core_customfield_inplace_editable($itemtype, $itemid, $newvalue) {
36 if ($itemtype === 'category') {
37 $category = core_customfield\category_controller::create($itemid);
38 $handler = $category->get_handler();
39 \external_api::validate_context($handler->get_configuration_context());
40 if (!$handler->can_configure()) {
41 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
43 $newvalue = clean_param($newvalue, PARAM_NOTAGS);
44 $handler->rename_category($category, $newvalue);
45 return \core_customfield\api::get_category_inplace_editable($category, true);
49 /**
50 * Serve the files from the core_customfield file areas
52 * @param stdClass $course the course object
53 * @param stdClass $cm the course module object
54 * @param context $context the context
55 * @param string $filearea the name of the file area
56 * @param array $args extra arguments (itemid, path)
57 * @param bool $forcedownload whether or not force download
58 * @param array $options additional options affecting the file serving
59 * @return bool false if the file not found, just send the file otherwise and do not return
61 function core_customfield_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
62 if ($filearea !== 'description') {
63 return false;
66 $itemid = array_shift($args);
67 $filename = array_pop($args); // The last item in the $args array.
69 $field = \core_customfield\field_controller::create($itemid);
70 $handler = $field->get_handler();
71 if ($handler->get_configuration_context()->id != $context->id) {
72 return false;
75 // Retrieve the file from the Files API.
76 $fs = get_file_storage();
77 $file = $fs->get_file($context->id, 'core_customfield', $filearea, $itemid, '/', $filename);
78 if (!$file) {
79 return false; // The file does not exist.
82 // We can now send the file back to the browser - in this case with a cache lifetime of 1 day and no filtering.
83 // From Moodle 2.3, use send_stored_file instead.
84 send_file($file, 86400, 0, $forcedownload, $options);