MDL-51177 core: Ignore built files in stylelint
[moodle.git] / lib / external / externallib.php
blob6721d2af1400ff16450571188f3c7a0bf99b84ed
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/>.
18 /**
19 * external API for core library
21 * @package core_webservice
22 * @category external
23 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die;
29 require_once("$CFG->libdir/externallib.php");
31 /**
32 * Web service related functions
34 * @package core
35 * @category external
36 * @copyright 2012 Jerome Mouneyrac <jerome@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @since Moodle 2.4
40 class core_external extends external_api {
43 /**
44 * Format the received string parameters to be sent to the core get_string() function.
46 * @param array $stringparams
47 * @return object|string
48 * @since Moodle 2.4
50 public static function format_string_parameters($stringparams) {
51 // Check if there are some string params.
52 $strparams = new stdClass();
53 if (!empty($stringparams)) {
54 // There is only one string parameter.
55 if (count($stringparams) == 1) {
56 $stringparam = array_pop($stringparams);
57 if (isset($stringparam['name'])) {
58 $strparams->{$stringparam['name']} = $stringparam['value'];
59 } else {
60 // It is a not named string parameter.
61 $strparams = $stringparam['value'];
63 } else {
64 // There are more than one parameter.
65 foreach ($stringparams as $stringparam) {
67 // If a parameter is unnamed throw an exception
68 // unnamed param is only possible if one only param is sent.
69 if (empty($stringparam['name'])) {
70 throw new moodle_exception('unnamedstringparam', 'webservice');
73 $strparams->{$stringparam['name']} = $stringparam['value'];
77 return $strparams;
80 /**
81 * Returns description of get_string parameters
83 * @return external_function_parameters
84 * @since Moodle 2.4
86 public static function get_string_parameters() {
87 return new external_function_parameters(
88 array('stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
89 'component' => new external_value(PARAM_COMPONENT,'component', VALUE_DEFAULT, 'moodle'),
90 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
91 'stringparams' => new external_multiple_structure (
92 new external_single_structure(array(
93 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
94 - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
95 'value' => new external_value(PARAM_RAW,'param value'))),
96 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
103 * Return a core get_string() call
105 * @param string $identifier string identifier
106 * @param string $component string component
107 * @param array $stringparams the string params
108 * @return string
109 * @since Moodle 2.4
111 public static function get_string($stringid, $component = 'moodle', $lang = null, $stringparams = array()) {
112 $params = self::validate_parameters(self::get_string_parameters(),
113 array('stringid'=>$stringid, 'component' => $component, 'lang' => $lang, 'stringparams' => $stringparams));
115 $stringmanager = get_string_manager();
116 return $stringmanager->get_string($params['stringid'], $params['component'],
117 core_external::format_string_parameters($params['stringparams']), $params['lang']);
121 * Returns description of get_string() result value
123 * @return string
124 * @since Moodle 2.4
126 public static function get_string_returns() {
127 return new external_value(PARAM_RAW, 'translated string');
131 * Returns description of get_string parameters
133 * @return external_function_parameters
134 * @since Moodle 2.4
136 public static function get_strings_parameters() {
137 return new external_function_parameters(
138 array('strings' => new external_multiple_structure (
139 new external_single_structure (array(
140 'stringid' => new external_value(PARAM_STRINGID, 'string identifier'),
141 'component' => new external_value(PARAM_COMPONENT, 'component', VALUE_DEFAULT, 'moodle'),
142 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
143 'stringparams' => new external_multiple_structure (
144 new external_single_structure(array(
145 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name
146 - if the string expect only one $a parameter then don\'t send this field, just send the value.', VALUE_OPTIONAL),
147 'value' => new external_value(PARAM_RAW, 'param value'))),
148 'the definition of a string param (i.e. {$a->name})', VALUE_DEFAULT, array()
157 * Return multiple call to core get_string()
159 * @param array $strings strings to translate
160 * @return array
162 * @since Moodle 2.4
164 public static function get_strings($strings) {
165 $params = self::validate_parameters(self::get_strings_parameters(),
166 array('strings'=>$strings));
167 $stringmanager = get_string_manager();
169 $translatedstrings = array();
170 foreach($params['strings'] as $string) {
172 if (!empty($string['lang'])) {
173 $lang = $string['lang'];
174 } else {
175 $lang = current_language();
178 $translatedstrings[] = array(
179 'stringid' => $string['stringid'],
180 'component' => $string['component'],
181 'lang' => $lang,
182 'string' => $stringmanager->get_string($string['stringid'], $string['component'],
183 core_external::format_string_parameters($string['stringparams']), $lang));
186 return $translatedstrings;
190 * Returns description of get_string() result value
192 * @return array
193 * @since Moodle 2.4
195 public static function get_strings_returns() {
196 return new external_multiple_structure(
197 new external_single_structure(array(
198 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
199 'component' => new external_value(PARAM_COMPONENT, 'string component'),
200 'lang' => new external_value(PARAM_LANG, 'lang'),
201 'string' => new external_value(PARAM_RAW, 'translated string'))
206 * Returns description of get_user_dates parameters
208 * @return external_function_parameters
210 public static function get_user_dates_parameters() {
211 return new external_function_parameters(
213 'contextid' => new external_value(
214 PARAM_INT,
215 'Context ID. Either use this value, or level and instanceid.',
216 VALUE_DEFAULT,
219 'contextlevel' => new external_value(
220 PARAM_ALPHA,
221 'Context level. To be used with instanceid.',
222 VALUE_DEFAULT,
225 'instanceid' => new external_value(
226 PARAM_INT,
227 'Context instance ID. To be used with level',
228 VALUE_DEFAULT,
231 'timestamps' => new external_multiple_structure (
232 new external_single_structure (
234 'timestamp' => new external_value(PARAM_INT, 'unix timestamp'),
235 'format' => new external_value(PARAM_TEXT, 'format string'),
244 * Format an array of timestamps.
246 * @param int|null $contextid The contenxt id
247 * @param string|null $contextlevel The context level
248 * @param int|null $instanceid The instnace id for the context level
249 * @param array $timestamps Timestamps to format
250 * @return array
252 public static function get_user_dates($contextid, $contextlevel, $instanceid, $timestamps) {
253 $params = self::validate_parameters(
254 self::get_user_dates_parameters(),
256 'contextid' => $contextid,
257 'contextlevel' => $contextlevel,
258 'instanceid' => $instanceid,
259 'timestamps' => $timestamps,
263 $context = self::get_context_from_params($params);
264 self::validate_context($context);
266 $formatteddates = array_map(function($timestamp) {
267 return userdate($timestamp['timestamp'], $timestamp['format']);
268 }, $params['timestamps']);
270 return ['dates' => $formatteddates];
274 * Returns description of get_user_dates() result value
276 * @return array
278 public static function get_user_dates_returns() {
279 return new external_single_structure(
281 'dates' => new external_multiple_structure (
282 new external_value(PARAM_TEXT, 'formatted dates strings')
289 * Returns description of get_component_strings parameters
291 * @return external_function_parameters
292 * @since Moodle 2.4
294 public static function get_component_strings_parameters() {
295 return new external_function_parameters(
296 array('component' => new external_value(PARAM_COMPONENT, 'component'),
297 'lang' => new external_value(PARAM_LANG, 'lang', VALUE_DEFAULT, null),
303 * Return all lang strings of a component - call to core get_component_strings().
305 * @param string $component component name
306 * @return array
308 * @since Moodle 2.4
310 public static function get_component_strings($component, $lang = null) {
312 if (empty($lang)) {
313 $lang = current_language();
316 $params = self::validate_parameters(self::get_component_strings_parameters(),
317 array('component'=>$component, 'lang' => $lang));
319 $stringmanager = get_string_manager();
321 $wsstrings = array();
322 $componentstrings = $stringmanager->load_component_strings($params['component'], $params['lang']);
323 foreach($componentstrings as $stringid => $string) {
324 $wsstring = array();
325 $wsstring['stringid'] = $stringid;
326 $wsstring['string'] = $string;
327 $wsstrings[] = $wsstring;
330 return $wsstrings;
334 * Returns description of get_component_strings() result value
336 * @return array
337 * @since Moodle 2.4
339 public static function get_component_strings_returns() {
340 return new external_multiple_structure(
341 new external_single_structure(array(
342 'stringid' => new external_value(PARAM_STRINGID, 'string id'),
343 'string' => new external_value(PARAM_RAW, 'translated string'))
348 * Returns description of get_fragment parameters
350 * @return external_function_parameters
351 * @since Moodle 3.1
353 public static function get_fragment_parameters() {
354 return new external_function_parameters(
355 array(
356 'component' => new external_value(PARAM_COMPONENT, 'Component for the callback e.g. mod_assign'),
357 'callback' => new external_value(PARAM_ALPHANUMEXT, 'Name of the callback to execute'),
358 'contextid' => new external_value(PARAM_INT, 'Context ID that the fragment is from'),
359 'args' => new external_multiple_structure(
360 new external_single_structure(
361 array(
362 'name' => new external_value(PARAM_ALPHANUMEXT, 'param name'),
363 'value' => new external_value(PARAM_RAW, 'param value')
365 ), 'args for the callback are optional', VALUE_OPTIONAL
372 * Get a HTML fragment for inserting into something. Initial use is for inserting mforms into
373 * a page using AJAX.
374 * This web service is designed to be called only via AJAX and not directly.
375 * Callbacks that are called by this web service are responsible for doing the appropriate security checks
376 * to access the information returned. This only does minimal validation on the context.
378 * @param string $component Name of the component.
379 * @param string $callback Function callback name.
380 * @param int $contextid Context ID this fragment is in.
381 * @param array $args optional arguments for the callback.
382 * @return array HTML and JavaScript fragments for insertion into stuff.
383 * @since Moodle 3.1
385 public static function get_fragment($component, $callback, $contextid, $args = null) {
386 global $OUTPUT, $PAGE;
388 $params = self::validate_parameters(self::get_fragment_parameters(),
389 array(
390 'component' => $component,
391 'callback' => $callback,
392 'contextid' => $contextid,
393 'args' => $args
397 // Reformat arguments into something less unwieldy.
398 $arguments = array();
399 foreach ($params['args'] as $paramargument) {
400 $arguments[$paramargument['name']] = $paramargument['value'];
403 $context = context::instance_by_id($contextid);
404 self::validate_context($context);
405 $arguments['context'] = $context;
407 // Hack alert: Set a default URL to stop the annoying debug.
408 $PAGE->set_url('/');
409 // Hack alert: Forcing bootstrap_renderer to initiate moodle page.
410 $OUTPUT->header();
412 // Overwriting page_requirements_manager with the fragment one so only JS included from
413 // this point is returned to the user.
414 $PAGE->start_collecting_javascript_requirements();
415 $data = component_callback($params['component'], 'output_fragment_' . $params['callback'], array($arguments));
416 $jsfooter = $PAGE->requires->get_end_code();
417 $output = array('html' => $data, 'javascript' => $jsfooter);
418 return $output;
422 * Returns description of get_fragment() result value
424 * @return array
425 * @since Moodle 3.1
427 public static function get_fragment_returns() {
428 return new external_single_structure(
429 array(
430 'html' => new external_value(PARAM_RAW, 'HTML fragment.'),
431 'javascript' => new external_value(PARAM_RAW, 'JavaScript fragment')
437 * Parameters for function update_inplace_editable()
439 * @since Moodle 3.1
440 * @return external_function_parameters
442 public static function update_inplace_editable_parameters() {
443 return new external_function_parameters(
444 array(
445 'component' => new external_value(PARAM_COMPONENT, 'component responsible for the update', VALUE_REQUIRED),
446 'itemtype' => new external_value(PARAM_NOTAGS, 'type of the updated item inside the component', VALUE_REQUIRED),
447 'itemid' => new external_value(PARAM_RAW, 'identifier of the updated item', VALUE_REQUIRED),
448 'value' => new external_value(PARAM_RAW, 'new value', VALUE_REQUIRED),
453 * Update any component's editable value assuming that component implements necessary callback
455 * @since Moodle 3.1
456 * @param string $component
457 * @param string $itemtype
458 * @param string $itemid
459 * @param string $value
461 public static function update_inplace_editable($component, $itemtype, $itemid, $value) {
462 global $PAGE;
463 // Validate and normalize parameters.
464 $params = self::validate_parameters(self::update_inplace_editable_parameters(),
465 array('component' => $component, 'itemtype' => $itemtype, 'itemid' => $itemid, 'value' => $value));
466 if (!$functionname = component_callback_exists($component, 'inplace_editable')) {
467 throw new \moodle_exception('inplaceeditableerror');
469 $tmpl = component_callback($params['component'], 'inplace_editable',
470 array($params['itemtype'], $params['itemid'], $params['value']));
471 if (!$tmpl || !($tmpl instanceof \core\output\inplace_editable)) {
472 throw new \moodle_exception('inplaceeditableerror');
474 return $tmpl->export_for_template($PAGE->get_renderer('core'));
478 * Return structure for update_inplace_editable()
480 * @since Moodle 3.1
481 * @return external_description
483 public static function update_inplace_editable_returns() {
484 return new external_single_structure(
485 array(
486 'displayvalue' => new external_value(PARAM_RAW, 'display value (may contain link or other html tags)'),
487 'component' => new external_value(PARAM_NOTAGS, 'component responsible for the update', VALUE_OPTIONAL),
488 'itemtype' => new external_value(PARAM_NOTAGS, 'itemtype', VALUE_OPTIONAL),
489 'value' => new external_value(PARAM_RAW, 'value of the item as it is stored', VALUE_OPTIONAL),
490 'itemid' => new external_value(PARAM_RAW, 'identifier of the updated item', VALUE_OPTIONAL),
491 'edithint' => new external_value(PARAM_NOTAGS, 'hint for editing element', VALUE_OPTIONAL),
492 'editlabel' => new external_value(PARAM_NOTAGS, 'label for editing element', VALUE_OPTIONAL),
493 'type' => new external_value(PARAM_ALPHA, 'type of the element (text, toggle, select)', VALUE_OPTIONAL),
494 'options' => new external_value(PARAM_RAW, 'options of the element, format depends on type', VALUE_OPTIONAL),
495 'linkeverything' => new external_value(PARAM_INT, 'Should everything be wrapped in the edit link or link displayed separately', VALUE_OPTIONAL),
501 * Returns description of fetch_notifications() parameters.
503 * @return external_function_parameters
504 * @since Moodle 3.1
506 public static function fetch_notifications_parameters() {
507 return new external_function_parameters(
508 array(
509 'contextid' => new external_value(PARAM_INT, 'Context ID', VALUE_REQUIRED),
514 * Returns description of fetch_notifications() result value.
516 * @return external_description
517 * @since Moodle 3.1
519 public static function fetch_notifications_returns() {
520 return new external_multiple_structure(
521 new external_single_structure(
522 array(
523 'template' => new external_value(PARAM_RAW, 'Name of the template'),
524 'variables' => new external_single_structure(array(
525 'message' => new external_value(PARAM_RAW, 'HTML content of the Notification'),
526 'extraclasses' => new external_value(PARAM_RAW, 'Extra classes to provide to the tmeplate'),
527 'announce' => new external_value(PARAM_RAW, 'Whether to announce'),
528 'closebutton' => new external_value(PARAM_RAW, 'Whether to close'),
536 * Returns the list of notifications against the current session.
538 * @return array
539 * @since Moodle 3.1
541 public static function fetch_notifications($contextid) {
542 global $PAGE;
544 self::validate_parameters(self::fetch_notifications_parameters(), [
545 'contextid' => $contextid,
548 $context = \context::instance_by_id($contextid);
549 self::validate_context($context);
551 return \core\notification::fetch_as_array($PAGE->get_renderer('core'));