Merge branch 'wip-MDL-61424-master' of git://github.com/marinaglancy/moodle
[moodle.git] / filter / classes / external.php
blobed6218f976304167ce305ab0bb4860412430ffdd
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 * This is the external API for the filter component.
20 * @package core_filters
21 * @copyright 2017 Juan Leyva
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_filters;
26 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->libdir . '/externallib.php');
29 require_once($CFG->libdir . '/filterlib.php');
31 use external_api;
32 use external_function_parameters;
33 use external_value;
34 use external_single_structure;
35 use external_multiple_structure;
36 use external_warnings;
37 use Exception;
39 /**
40 * This is the external API for the filter component.
42 * @copyright 2017 Juan Leyva
43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 class external extends external_api {
47 /**
48 * Returns description of get_available_in_context() parameters.
50 * @return external_function_parameters
51 * @since Moodle 3.4
53 public static function get_available_in_context_parameters() {
54 return new external_function_parameters (
55 array(
56 'contexts' => new external_multiple_structure(
57 new external_single_structure(
58 array(
59 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level where the filters are:
60 (coursecat, course, module)'),
61 'instanceid' => new external_value(PARAM_INT, 'The instance id of item associated with the context.')
63 ), 'The list of contexts to check.'
69 /**
70 * Returns the filters available in the given contexts.
72 * @param array $contexts the list of contexts to check
73 * @return array with the filters information and warnings
74 * @since Moodle 3.4
76 public static function get_available_in_context($contexts) {
77 $params = self::validate_parameters(self::get_available_in_context_parameters(), array('contexts' => $contexts));
78 $filters = $warnings = array();
80 foreach ($params['contexts'] as $contextinfo) {
81 try {
82 $context = self::get_context_from_params($contextinfo);
83 self::validate_context($context);
84 $contextinfo['contextid'] = $context->id;
85 } catch (Exception $e) {
86 $warnings[] = array(
87 'item' => 'context',
88 'itemid' => $context['instanceid'],
89 'warningcode' => $e->getCode(),
90 'message' => $e->getMessage(),
92 continue;
94 $contextfilters = filter_get_available_in_context($context);
96 foreach ($contextfilters as $filter) {
97 $filters[] = array_merge($contextinfo, (array) $filter);
101 return array(
102 'filters' => $filters,
103 'warnings' => $warnings,
108 * Returns description of get_available_in_context() result value.
110 * @return external_single_structure
111 * @since Moodle 3.4
113 public static function get_available_in_context_returns() {
114 return new external_single_structure(
115 array(
116 'filters' => new external_multiple_structure(
117 new external_single_structure(
118 array(
119 'contextlevel' => new external_value(PARAM_ALPHA, 'The context level where the filters are:
120 (coursecat, course, module).'),
121 'instanceid' => new external_value(PARAM_INT, 'The instance id of item associated with the context.'),
122 'contextid' => new external_value(PARAM_INT, 'The context id.'),
123 'filter' => new external_value(PARAM_PLUGIN, 'Filter plugin name.'),
124 'localstate' => new external_value(PARAM_INT, 'Filter state: 1 for on, -1 for off, 0 if inherit.'),
125 'inheritedstate' => new external_value(PARAM_INT, '1 or 0 to use when localstate is set to inherit.'),
128 'Available filters'
130 'warnings' => new external_warnings(),