Merge branch 'MDL-81399' of https://github.com/paulholden/moodle
[moodle.git] / customfield / lib.php
blob2bb99f4807d21eed3e96a94ea42d8a4e79a789bd
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 use core_external\external_api;
27 defined('MOODLE_INTERNAL') || die;
29 /**
30 * Edit customfield elements inplace
32 * @param string $itemtype
33 * @param int $itemid
34 * @param string $newvalue
35 * @return \core\output\inplace_editable
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');
45 $newvalue = clean_param($newvalue, PARAM_TEXT);
46 $handler->rename_category($category, $newvalue);
47 return \core_customfield\api::get_category_inplace_editable($category, true);
51 /**
52 * Serve the files from the core_customfield file areas
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
63 function core_customfield_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options=array()) {
64 if ($filearea !== 'description') {
65 return false;
68 $itemid = array_shift($args);
69 $filename = array_pop($args); // The last item in the $args array.
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;
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.
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);