2 // This file is part of Moodle - http://moodle.org/
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.
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 use core_external\external_api
;
18 use core_external\external_function_parameters
;
19 use core_external\external_multiple_structure
;
20 use core_external\external_single_structure
;
21 use core_external\external_value
;
24 * External interface library for customfields component
26 * @package core_customfield
27 * @copyright 2018 David Matamoros <davidmc@moodle.com>
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 class core_customfield_external
extends external_api
{
33 * Parameters for delete_field
35 * @return external_function_parameters
37 public static function delete_field_parameters() {
38 return new external_function_parameters(
39 array('id' => new external_value(PARAM_INT
, 'Custom field ID to delete', VALUE_REQUIRED
))
44 * Delete custom field function
48 public static function delete_field($id) {
49 $params = self
::validate_parameters(self
::delete_field_parameters(), ['id' => $id]);
51 $record = \core_customfield\field_controller
::create($params['id']);
52 $handler = $record->get_handler();
53 if (!$handler->can_configure()) {
54 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
56 $handler->delete_field_configuration($record);
60 * Return for delete_field
62 public static function delete_field_returns() {
66 * Parameters for reload template function
68 * @return external_function_parameters
70 public static function reload_template_parameters() {
71 return new external_function_parameters(
73 'component' => new external_value(PARAM_COMPONENT
, 'component', VALUE_REQUIRED
),
74 'area' => new external_value(PARAM_ALPHANUMEXT
, 'area', VALUE_REQUIRED
),
75 'itemid' => new external_value(PARAM_INT
, 'itemid', VALUE_REQUIRED
)
81 * Reload template function
83 * @param string $component
86 * @return array|object|stdClass
88 public static function reload_template($component, $area, $itemid) {
91 $params = self
::validate_parameters(self
::reload_template_parameters(),
92 ['component' => $component, 'area' => $area, 'itemid' => $itemid]);
94 $PAGE->set_context(context_system
::instance());
95 $handler = \core_customfield\handler
::get_handler($params['component'], $params['area'], $params['itemid']);
96 self
::validate_context($handler->get_configuration_context());
97 if (!$handler->can_configure()) {
98 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
100 $output = $PAGE->get_renderer('core_customfield');
101 $outputpage = new \core_customfield\output\
management($handler);
102 return $outputpage->export_for_template($output);
106 * Ajax returns on reload template.
108 * @return external_single_structure
110 public static function reload_template_returns() {
111 return new external_single_structure(
113 'component' => new external_value(PARAM_COMPONENT
, 'component'),
114 'area' => new external_value(PARAM_ALPHANUMEXT
, 'area'),
115 'itemid' => new external_value(PARAM_INT
, 'itemid'),
116 'usescategories' => new external_value(PARAM_BOOL
, 'view has categories'),
117 'categories' => new external_multiple_structure(
118 new external_single_structure(
120 'id' => new external_value(PARAM_INT
, 'id'),
121 'nameeditable' => new external_value(PARAM_RAW
, 'inplace editable name'),
122 'addfieldmenu' => new external_value(PARAM_RAW
, 'addfieldmenu'),
123 'fields' => new external_multiple_structure(
124 new external_single_structure(
126 'name' => new external_value(PARAM_NOTAGS
, 'name'),
127 'shortname' => new external_value(PARAM_NOTAGS
, 'shortname'),
128 'type' => new external_value(PARAM_NOTAGS
, 'type'),
129 'id' => new external_value(PARAM_INT
, 'id'),
132 , '', VALUE_OPTIONAL
),
141 * Parameters for delete category
143 * @return external_function_parameters
145 public static function delete_category_parameters() {
146 return new external_function_parameters(
147 array('id' => new external_value(PARAM_INT
, 'category ID to delete', VALUE_REQUIRED
))
152 * Delete category function
156 public static function delete_category($id) {
157 $category = core_customfield\category_controller
::create($id);
158 $handler = $category->get_handler();
159 self
::validate_context($handler->get_configuration_context());
160 if (!$handler->can_configure()) {
161 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
163 $handler->delete_category($category);
167 * Return for delete category
169 public static function delete_category_returns() {
174 * Parameters for create category
176 * @return external_function_parameters
178 public static function create_category_parameters() {
179 return new external_function_parameters(
181 'component' => new external_value(PARAM_COMPONENT
, 'component', VALUE_REQUIRED
),
182 'area' => new external_value(PARAM_ALPHANUMEXT
, 'area', VALUE_REQUIRED
),
183 'itemid' => new external_value(PARAM_INT
, 'itemid', VALUE_REQUIRED
)
189 * Create category function
191 * @param string $component
192 * @param string $area
196 public static function create_category($component, $area, $itemid) {
197 $params = self
::validate_parameters(self
::create_category_parameters(),
198 ['component' => $component, 'area' => $area, 'itemid' => $itemid]);
200 $handler = \core_customfield\handler
::get_handler($params['component'], $params['area'], $params['itemid']);
201 self
::validate_context($handler->get_configuration_context());
202 if (!$handler->can_configure()) {
203 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
205 return $handler->create_category();
209 * Return for create category
211 public static function create_category_returns() {
212 return new external_value(PARAM_INT
, 'Id of the category');
216 * Parameters for move field.
218 * @return external_function_parameters
220 public static function move_field_parameters() {
221 return new external_function_parameters(
222 ['id' => new external_value(PARAM_INT
, 'Id of the field to move', VALUE_REQUIRED
),
223 'categoryid' => new external_value(PARAM_INT
, 'New parent category id', VALUE_REQUIRED
),
224 'beforeid' => new external_value(PARAM_INT
, 'Id of the field before which it needs to be moved',
230 * Move/reorder field. Move a field to another category and/or change sortorder of fields
232 * @param int $id field id
233 * @param int $categoryid
234 * @param int $beforeid
236 public static function move_field($id, $categoryid, $beforeid) {
237 $params = self
::validate_parameters(self
::move_field_parameters(),
238 ['id' => $id, 'categoryid' => $categoryid, 'beforeid' => $beforeid]);
239 $field = \core_customfield\field_controller
::create($params['id']);
240 $handler = $field->get_handler();
241 self
::validate_context($handler->get_configuration_context());
242 if (!$handler->can_configure()) {
243 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
245 $handler->move_field($field, $params['categoryid'], $params['beforeid']);
249 * Return for move field
251 public static function move_field_returns() {
255 * Return for move category
257 * @return external_function_parameters
259 public static function move_category_parameters() {
260 return new external_function_parameters(
261 ['id' => new external_value(PARAM_INT
, 'Category ID to move', VALUE_REQUIRED
),
262 'beforeid' => new external_value(PARAM_INT
, 'Id of the category before which it needs to be moved',
268 * Reorder categories. Move category to the new position
270 * @param int $id category id
271 * @param int $beforeid
273 public static function move_category(int $id, int $beforeid) {
274 $params = self
::validate_parameters(self
::move_category_parameters(),
275 ['id' => $id, 'beforeid' => $beforeid]);
276 $category = core_customfield\category_controller
::create($id);
277 $handler = $category->get_handler();
278 self
::validate_context($handler->get_configuration_context());
279 if (!$handler->can_configure()) {
280 throw new moodle_exception('nopermissionconfigure', 'core_customfield');
282 $handler->move_category($category, $params['beforeid']);
286 * Return for move category
288 public static function move_category_returns() {