MDL-50783 Ajax: Configure how to call a webservice through db/service.php
[moodle.git] / tag / classes / external.php
CommitLineData
8e355853
MG
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.
13//
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/>.
16
17/**
18 * Contains class core_tag_external
19 *
20 * @package core_tag
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once("$CFG->libdir/externallib.php");
26require_once("$CFG->dirroot/webservice/externallib.php");
27
28/**
29 * Tags-related web services
30 *
31 * @package core_tag
32 * @copyright 2015 Marina Glancy
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class core_tag_external extends external_api {
36
37 /**
38 * Parameters for function update_tags()
39 *
40 * @return external_function_parameters
41 */
42 public static function update_tags_parameters() {
43 return new external_function_parameters(
44 array(
45 'tags' => new external_multiple_structure(
46 new external_single_structure(
47 array(
48 'id' => new external_value(PARAM_INT, 'tag id'),
49 'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)',
50 VALUE_OPTIONAL),
51 'description' => new external_value(PARAM_RAW, 'tag description', VALUE_OPTIONAL),
52 'descriptionformat' => new external_value(PARAM_INT, 'tag description format', VALUE_OPTIONAL),
53 'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
54 'official' => new external_value(PARAM_INT, 'whether this flag is official', VALUE_OPTIONAL),
55 )
56 )
57 )
58 )
59 );
60 }
61
8e355853
MG
62 /**
63 * Update tags
64 *
65 * @param array $tags
66 */
67 public static function update_tags($tags) {
68 global $CFG, $PAGE, $DB;
69 require_once($CFG->dirroot.'/tag/lib.php');
70
71 // Validate and normalize parameters.
72 $tags = self::validate_parameters(self::update_tags_parameters(), array('tags' => $tags));
73
74 $systemcontext = context_system::instance();
75 $canmanage = has_capability('moodle/tag:manage', $systemcontext);
76 $canedit = has_capability('moodle/tag:edit', $systemcontext);
8e355853
MG
77 $warnings = array();
78
79 if (empty($CFG->usetags)) {
80 throw new moodle_exception('tagsaredisabled', 'tag');
81 }
82
83 $renderer = $PAGE->get_renderer('core');
84 foreach ($tags['tags'] as $tag) {
85 $tag = (array)$tag;
86 if (array_key_exists('rawname', $tag)) {
87 $tag['rawname'] = clean_param($tag['rawname'], PARAM_TAG);
88 if (empty($tag['rawname'])) {
89 unset($tag['rawname']);
90 } else {
91 $tag['name'] = core_text::strtolower($tag['rawname']);
92 }
93 }
94 if (!$canmanage) {
95 // User without manage capability can not change any fields except for descriptions.
96 $tag = array_intersect_key($tag, array('id' => 1,
97 'description' => 1, 'descriptionformat' => 1));
98 }
99 if (!$canedit) {
100 // User without edit capability can not change description.
101 $tag = array_diff_key($tag,
102 array('description' => 1, 'descriptionformat' => 1));
103 }
104 if (count($tag) <= 1) {
ef4c23cc
MG
105 $warnings[] = array(
106 'item' => $tag['id'],
107 'warningcode' => 'nothingtoupdate',
108 'message' => get_string('nothingtoupdate', 'core_tag')
109 );
8e355853
MG
110 continue;
111 }
112 if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
113 $warnings[] = array(
114 'item' => $tag['id'],
115 'warningcode' => 'tagnotfound',
116 'message' => get_string('tagnotfound', 'error')
117 );
118 continue;
119 }
120 // First check if new tag name is allowed.
121 if (!empty($tag['name']) && ($existing = $DB->get_record('tag', array('name' => $tag['name']), 'id'))) {
122 if ($existing->id != $tag['id']) {
123 $warnings[] = array(
124 'item' => $tag['id'],
125 'warningcode' => 'namesalreadybeeingused',
126 'message' => get_string('namesalreadybeeingused', 'core_tag')
127 );
128 continue;
129 }
130 }
131 if (array_key_exists('official', $tag)) {
132 $tag['tagtype'] = $tag['official'] ? 'official' : 'default';
133 unset($tag['official']);
134 }
135 $tag['timemodified'] = time();
136 $DB->update_record('tag', $tag);
137
138 foreach ($tag as $key => $value) {
139 $tagobject->$key = $value;
140 }
141
142 $event = \core\event\tag_updated::create(array(
143 'objectid' => $tagobject->id,
144 'relateduserid' => $tagobject->userid,
145 'context' => context_system::instance(),
146 'other' => array(
147 'name' => $tagobject->name,
148 'rawname' => $tagobject->rawname
149 )
150 ));
151 $event->trigger();
8e355853 152 }
ef4c23cc 153 return array('warnings' => $warnings);
8e355853
MG
154 }
155
156 /**
157 * Return structure for update_tag()
158 *
159 * @return external_description
160 */
161 public static function update_tags_returns() {
ef4c23cc
MG
162 return new external_single_structure(
163 array(
164 'warnings' => new external_warnings()
165 )
166 );
167 }
168
169 /**
170 * Parameters for function get_tags()
171 *
172 * @return external_function_parameters
173 */
174 public static function get_tags_parameters() {
175 return new external_function_parameters(
176 array(
177 'tags' => new external_multiple_structure(
178 new external_single_structure(
179 array(
180 'id' => new external_value(PARAM_INT, 'tag id'),
181 )
182 )
183 )
184 )
185 );
186 }
187
ef4c23cc
MG
188 /**
189 * Get tags by their ids
190 *
191 * @param array $tags
192 */
193 public static function get_tags($tags) {
194 global $CFG, $PAGE, $DB;
195 require_once($CFG->dirroot.'/tag/lib.php');
196
197 // Validate and normalize parameters.
198 $tags = self::validate_parameters(self::get_tags_parameters(), array('tags' => $tags));
199
200 require_login(null, false, null, false, true);
201
202 $systemcontext = context_system::instance();
203 $canmanage = has_capability('moodle/tag:manage', $systemcontext);
204 $canedit = has_capability('moodle/tag:edit', $systemcontext);
205
206 $return = array();
207 $warnings = array();
208
209 if (empty($CFG->usetags)) {
210 throw new moodle_exception('tagsaredisabled', 'tag');
211 }
212
213 $renderer = $PAGE->get_renderer('core');
214 foreach ($tags['tags'] as $tag) {
215 $tag = (array)$tag;
216 if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
217 $warnings[] = array(
218 'item' => $tag['id'],
219 'warningcode' => 'tagnotfound',
220 'message' => get_string('tagnotfound', 'error')
221 );
222 continue;
223 }
224 $tagoutput = new \core_tag\output\tag($tagobject);
225 // Do not return some information to users without permissions.
226 $rv = $tagoutput->export_for_template($renderer);
227 if (!$canmanage) {
228 if (!$canedit) {
229 unset($rv->official);
230 }
231 unset($rv->flag);
232 unset($rv->changetypeurl);
233 unset($rv->changeflagurl);
234 }
235 $return[] = $rv;
236 }
237 return array('tags' => $return, 'warnings' => $warnings);
238 }
239
240 /**
241 * Return structure for get_tag()
242 *
243 * @return external_description
244 */
245 public static function get_tags_returns() {
8e355853
MG
246 return new external_single_structure(
247 array(
248 'tags' => new external_multiple_structure( new external_single_structure(
249 array(
250 'id' => new external_value(PARAM_INT, 'tag id'),
251 'name' => new external_value(PARAM_TAG, 'name'),
252 'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)'),
253 'description' => new external_value(PARAM_RAW, 'tag description'),
ef4c23cc
MG
254 'descriptionformat' => new external_format_value(PARAM_INT, 'tag description format'),
255 'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
256 'official' => new external_value(PARAM_INT, 'whether this flag is official', VALUE_OPTIONAL),
8e355853 257 'viewurl' => new external_value(PARAM_URL, 'URL to view'),
ef4c23cc
MG
258 'changetypeurl' => new external_value(PARAM_URL, 'URL to change type (official or not)', VALUE_OPTIONAL),
259 'changeflagurl' => new external_value(PARAM_URL, 'URL to set or reset flag', VALUE_OPTIONAL),
260 ), 'information about one tag')
8e355853
MG
261 ),
262 'warnings' => new external_warnings()
263 )
264 );
265 }
ba224fb4 266}