forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
externallib.php
296 lines (269 loc) · 10.6 KB
/
externallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External interface library for customfields component
*
* @package core_customfield
* @copyright 2018 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . "/externallib.php");
/**
* Class core_customfield_external
*
* @copyright 2018 David Matamoros <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_customfield_external extends external_api {
/**
* Parameters for delete_field
*
* @return external_function_parameters
*/
public static function delete_field_parameters() {
return new external_function_parameters(
array('id' => new external_value(PARAM_INT, 'Custom field ID to delete', VALUE_REQUIRED))
);
}
/**
* Delete custom field function
*
* @param int $id
*/
public static function delete_field($id) {
$params = self::validate_parameters(self::delete_field_parameters(), ['id' => $id]);
$record = \core_customfield\field_controller::create($params['id']);
$handler = $record->get_handler();
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
$handler->delete_field_configuration($record);
}
/**
* Return for delete_field
*/
public static function delete_field_returns() {
}
/**
* Parameters for reload template function
*
* @return external_function_parameters
*/
public static function reload_template_parameters() {
return new external_function_parameters(
array(
'component' => new external_value(PARAM_COMPONENT, 'component', VALUE_REQUIRED),
'area' => new external_value(PARAM_ALPHANUMEXT, 'area', VALUE_REQUIRED),
'itemid' => new external_value(PARAM_INT, 'itemid', VALUE_REQUIRED)
)
);
}
/**
* Reload template function
*
* @param string $component
* @param string $area
* @param int $itemid
* @return array|object|stdClass
*/
public static function reload_template($component, $area, $itemid) {
global $PAGE;
$params = self::validate_parameters(self::reload_template_parameters(),
['component' => $component, 'area' => $area, 'itemid' => $itemid]);
$PAGE->set_context(context_system::instance());
$handler = \core_customfield\handler::get_handler($params['component'], $params['area'], $params['itemid']);
self::validate_context($handler->get_configuration_context());
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
$output = $PAGE->get_renderer('core_customfield');
$outputpage = new \core_customfield\output\management($handler);
return $outputpage->export_for_template($output);
}
/**
* Ajax returns on reload template.
*
* @return external_single_structure
*/
public static function reload_template_returns() {
return new external_single_structure(
array(
'component' => new external_value(PARAM_COMPONENT, 'component'),
'area' => new external_value(PARAM_ALPHANUMEXT, 'area'),
'itemid' => new external_value(PARAM_INT, 'itemid'),
'usescategories' => new external_value(PARAM_INT, 'view has categories'),
'categories' => new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'id'),
'nameeditable' => new external_value(PARAM_RAW, 'inplace editable name'),
'addfieldmenu' => new external_value(PARAM_RAW, 'addfieldmenu'),
'fields' => new external_multiple_structure(
new external_single_structure(
array(
'name' => new external_value(PARAM_NOTAGS, 'name'),
'shortname' => new external_value(PARAM_NOTAGS, 'shortname'),
'type' => new external_value(PARAM_NOTAGS, 'type'),
'editfieldurl' => new external_value(PARAM_URL, 'edit field url'),
'id' => new external_value(PARAM_INT, 'id'),
)
)
, '', VALUE_OPTIONAL),
)
)
),
)
);
}
/**
* Parameters for delete category
*
* @return external_function_parameters
*/
public static function delete_category_parameters() {
return new external_function_parameters(
array('id' => new external_value(PARAM_INT, 'category ID to delete', VALUE_REQUIRED))
);
}
/**
* Delete category function
*
* @param int $id
*/
public static function delete_category($id) {
$category = core_customfield\category_controller::create($id);
$handler = $category->get_handler();
self::validate_context($handler->get_configuration_context());
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
$handler->delete_category($category);
}
/**
* Return for delete category
*/
public static function delete_category_returns() {
}
/**
* Parameters for create category
*
* @return external_function_parameters
*/
public static function create_category_parameters() {
return new external_function_parameters(
array(
'component' => new external_value(PARAM_COMPONENT, 'component', VALUE_REQUIRED),
'area' => new external_value(PARAM_ALPHANUMEXT, 'area', VALUE_REQUIRED),
'itemid' => new external_value(PARAM_INT, 'itemid', VALUE_REQUIRED)
)
);
}
/**
* Create category function
*
* @param string $component
* @param string $area
* @param int $itemid
* @return mixed
*/
public static function create_category($component, $area, $itemid) {
$params = self::validate_parameters(self::create_category_parameters(),
['component' => $component, 'area' => $area, 'itemid' => $itemid]);
$handler = \core_customfield\handler::get_handler($params['component'], $params['area'], $params['itemid']);
self::validate_context($handler->get_configuration_context());
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
return $handler->create_category();
}
/**
* Return for create category
*/
public static function create_category_returns() {
return new external_value(PARAM_INT, 'Id of the category');
}
/**
* Parameters for move field.
*
* @return external_function_parameters
*/
public static function move_field_parameters() {
return new external_function_parameters(
['id' => new external_value(PARAM_INT, 'Id of the field to move', VALUE_REQUIRED),
'categoryid' => new external_value(PARAM_INT, 'New parent category id', VALUE_REQUIRED),
'beforeid' => new external_value(PARAM_INT, 'Id of the field before which it needs to be moved',
VALUE_DEFAULT, 0)]
);
}
/**
* Move/reorder field. Move a field to another category and/or change sortorder of fields
*
* @param int $id field id
* @param int $categoryid
* @param int $beforeid
*/
public static function move_field($id, $categoryid, $beforeid) {
$params = self::validate_parameters(self::move_field_parameters(),
['id' => $id, 'categoryid' => $categoryid, 'beforeid' => $beforeid]);
$field = \core_customfield\field_controller::create($params['id']);
$handler = $field->get_handler();
self::validate_context($handler->get_configuration_context());
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
$handler->move_field($field, $params['categoryid'], $params['beforeid']);
}
/**
* Return for move field
*/
public static function move_field_returns() {
}
/**
* Return for move category
*
* @return external_function_parameters
*/
public static function move_category_parameters() {
return new external_function_parameters(
['id' => new external_value(PARAM_INT, 'Category ID to move', VALUE_REQUIRED),
'beforeid' => new external_value(PARAM_INT, 'Id of the category before which it needs to be moved',
VALUE_DEFAULT, 0)]
);
}
/**
* Reorder categories. Move category to the new position
*
* @param int $id category id
* @param int $beforeid
*/
public static function move_category(int $id, int $beforeid) {
$params = self::validate_parameters(self::move_category_parameters(),
['id' => $id, 'beforeid' => $beforeid]);
$category = core_customfield\category_controller::create($id);
$handler = $category->get_handler();
self::validate_context($handler->get_configuration_context());
if (!$handler->can_configure()) {
throw new moodle_exception('nopermissionconfigure', 'core_customfield');
}
$handler->move_category($category, $params['beforeid']);
}
/**
* Return for move category
*/
public static function move_category_returns() {
}
}