-
Notifications
You must be signed in to change notification settings - Fork 3
/
externallib.php
393 lines (344 loc) · 14.1 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?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/>.
/**
* @package mod_customlabel
* @category mod
* @author Valery Fremaux <valery.fremaux@gmail.com>
* @copyright 2016 Valery Fremaux (http://www.mylearningfactory.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->dirroot.'/lib/externallib.php');
require_once($CFG->dirroot.'/mod/customlabel/lib.php');
require_once($CFG->dirroot.'/mod/customlabel/locallib.php');
class mod_customlabel_external extends external_api {
// Get content.
/**
* checks some common parameters
*
* @return external_function_parameters
*/
public static function validate_element_parameters($parameters, $input) {
global $DB;
$params = self::validate_parameters($parameters, $input);
switch ($input['cidsource']) {
case 'idnumber':
if (!$cid = $DB->get_field('course_modules', 'instance', array('idnumber' => $input['cid']))) {
throw new moodle_exception('Course module not found');
}
break;
case 'id':
$cid = $input['cid'];
break;
default:
throw new moodle_exception('Invalid id source for element');
}
if (!$DB->record_exists('customlabel', array('id' => $cid))) {
throw new moodle_exception('Course element instance not found');
}
return $params;
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_content_parameters() {
global $CFG;
$desc = 'source for the course element id, can be either \'id\', or \'idnumber\'';
return new external_function_parameters(
array(
'cidsource' => new external_value(PARAM_ALPHA, $desc),
'cid' => new external_value(PARAM_TEXT, 'Element id'),
'lang' => new external_value(PARAM_TEXT, 'The output language', VALUE_DEFAULT, $CFG->lang),
)
);
}
/**
* Get a customlabel internal content
*
* @param string $cidsource the source field for the course identifier.
* @param string $cid the courseid id. If 0, will get all the certificates of the site
*
* @return external_description
*/
public static function get_content($cidsource, $cid, $lang) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::get_content($cidsource, $cid, $lang);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function get_content_returns() {
return new external_value(PARAM_RAW, 'The course element content');
}
// Get attribute.
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_attribute_parameters() {
$desc = 'source for the course element id, can be either \'id\', or \'idnumber\'';
return new external_function_parameters(
array(
'cidsource' => new external_value(PARAM_ALPHA, $desc),
'cid' => new external_value(PARAM_TEXT, 'Element id'),
'attributekey' => new external_value(PARAM_TEXT, 'Attribute key id'),
)
);
}
/**
* Get one or all attributes from a course
*
* @param string $cidsource the field for the course element identifier. 'idnumber'
* addresses the course module idnumber, while id
* addresses the element instance record.
* @param string $cid the courseid id. If 0, will get all the certificates of the site
* @param string $attributekey the attribute key. If 0, will get all the attributes of the element
*
* @return external_description
*/
public static function get_attribute($cidsource, $cid, $attributekey) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::get_attribute($cidsource, $cid, $attributekey);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function get_attribute_returns() {
return new external_value(PARAM_RAW, 'The course element attribute value');
}
// Set attribute.
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function set_attribute_parameters() {
$desc = 'source for the course element id, can be either \'id\', or \'idnumber\'';
return new external_function_parameters(
array(
'cidsource' => new external_value(PARAM_ALPHA, $desc),
'cid' => new external_value(PARAM_TEXT, 'Element id'),
'attributekey' => new external_value(PARAM_TEXT, 'Attribute id id'),
'value' => new external_value(PARAM_RAW, 'Attribute value'),
)
);
}
/**
* Sets a particular attribute value in a course element. Will not
* refresh the content cache. You will have to call refresh for commiting.
*
* @param string $cidsource the source field for the course element identifier.
* @param string $cid the course element id
* @param string $attributeid the attribute id. If 0, will get all the attributes of the element
* @param string $value the new value
*
* @return external_description
*/
public static function set_attribute($cidsource, $cid, $attributekey, $value) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::set_attribute($cidsource, $cid, $attributekey, $value);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function set_attribute_returns() {
return new external_value(PARAM_BOOL, 'The success status');
}
// Refresh.
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function refresh_parameters() {
$desc = 'source for the course element id, can be either \'id\', or \'idnumber\'';
return new external_function_parameters(
array(
'cidsource' => new external_value(PARAM_ALPHA, $desc),
'cid' => new external_value(PARAM_TEXT, 'Element id'),
)
);
}
/**
* Refreshes the content cache.
*
* @param string $cidsource the source field for the course element identifier.
* @param string $cid the course element id
*
* @return external_description
*/
public static function refresh($cidsource, $cid) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::refresh($cidsource, $cid);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function refresh_returns() {
return new external_value(PARAM_BOOL, 'The success status');
}
// Get Metadata Domain.
public static function validate_mtd_parameters($parameters, $input) {
global $DB;
$config = get_config('customlabel');
$result = self::validate_parameters($parameters, $input);
switch ($result['domain']->type) {
case 'id':
if (!$DB->record_exists($config->classification_type_table, array('id' => $result['domain']->id))) {
throw new moodle_exception('Domain type missing by id');
}
$result['domainid'] = $result['domain']->id;
break;
case 'code': {
$params = array('code' => $result['domain']->id);
if (!$type = $DB->record_exists($config->classification_type_table, $params, 'id,id')) {
throw new moodle_exception('Domain type missing by code '.$result['domain']->id);
}
$result['domainid'] = $type->id;
break;
}
case 'name': {
$params = array('name' => $result['domain']->id);
if (!$type = $DB->record_exists($config->classification_type_table, $params, 'id,id')) {
throw new moodle_exception('Domain type missing by name '.$result['domain']->id);
}
$result['domainid'] = $type->id;
break;
}
default;
throw new moodle_exception('Bad domain type');
}
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
*/
public static function get_mtd_domain_parameters() {
return new external_function_parameters(
array(
'domain' => new external_single_structure(
array(
'type' => new external_value(PARAM_TEXT, 'Identifier field, can be "id", "name" or "code"'),
'id' => new external_value(PARAM_TEXT, 'Domain identifier'),
)
)
)
);
}
/**
* Get the complete set of values for a metadata domain.
*
* @param string $cidsource the source field for the course element identifier.
* @param string $cid the course element id
*
* @return external_description
*/
public static function get_mtd_domain($domain) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::get_mtd_domain($domain);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
/**
* Returns description of method result value
*
* @return external_description
*/
public static function get_mtd_domain_returns() {
return new external_multiple_structure(
new external_single_structure(
array(
'code' => new external_value(PARAM_TEXT, 'Domain value code'),
'value' => new external_value(PARAM_TEXT, 'Domain value label'),
'id' => new external_value(PARAM_INT, 'Internal ID for classification reference'),
)
)
);
}
public static function add_instance_parameters() {
$desc = 'Source for the course id, can be either \'id\', or \'idnumber\', or \'shortname\'';
return new external_function_parameters(
array(
'cidsource' => new external_value(PARAM_ALPHA, $desc),
'cid' => new external_value(PARAM_TEXT, 'Course identifier'),
'sectionnum' => new external_value(PARAM_NUMERIC, 'Section/page num'),
'position' => new external_value(PARAM_NUMERIC, 'Position in section/page sequence, 0 (end) or -1 (start)'),
'labeltype' => new external_value(PARAM_NUMERIC, 'Label type name'),
'idnumber' => new external_value(PARAM_NUMERIC, 'Idnumber value for associated course module'),
)
);
}
public static function add_instance($cidsource, $cid, $sectionnum, $position, $labeltype, $idnumber) {
global $CFG;
if (customlabel_supports_feature('api/ws')) {
include_once($CFG->dirroot.'/mod/customlabel/pro/externallib.php');
return mod_customlabel_external_extended::add_instance($cidsource, $cid, $sectionnum, $position, $labeltype, $idnumber);
} else {
throw new moodle_exception("Call to \"pro\" version WS additional implementation cannot be satisfied");
}
}
public static function add_instance_returns() {
return new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'Course module id'),
'idnumber' => new external_value(PARAM_TEXT, 'Course module idnumber'),
'attributes' => new external_multiple_structure(
new external_single_structure(
[
'name' => new external_value(PARAM_TEXT, 'Label attribute name'),
'type' => new external_value(PARAM_TEXT, 'Label attribute type'),
]
)
)
)
);
}
}