2 // This file is part of Moodle - http://moodle.org/
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.
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 * Contains class core_tag_external
21 * @copyright 2015 Marina Glancy
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 require_once("$CFG->libdir/externallib.php");
28 require_once("$CFG->dirroot/webservice/externallib.php");
31 * Tags-related web services
34 * @copyright 2015 Marina Glancy
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class core_tag_external extends external_api {
40 * Parameters for function update_tags()
42 * @return external_function_parameters
44 public static function update_tags_parameters() {
45 return new external_function_parameters(
47 'tags' => new external_multiple_structure(
48 new external_single_structure(
50 'id' => new external_value(PARAM_INT, 'tag id'),
51 'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)',
53 'description' => new external_value(PARAM_RAW, 'tag description', VALUE_OPTIONAL),
54 'descriptionformat' => new external_value(PARAM_INT, 'tag description format', VALUE_OPTIONAL),
55 'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
56 'official' => new external_value(PARAM_INT,
57 '(deprecated, use isstandard) whether this flag is standard', VALUE_OPTIONAL),
58 'isstandard' => new external_value(PARAM_INT, 'whether this flag is standard', VALUE_OPTIONAL),
71 public static function update_tags($tags) {
72 global $CFG, $PAGE, $DB;
74 // Validate and normalize parameters.
75 $tags = self::validate_parameters(self::update_tags_parameters(), array('tags' => $tags));
77 $systemcontext = context_system::instance();
78 $canmanage = has_capability('moodle/tag:manage', $systemcontext);
79 $canedit = has_capability('moodle/tag:edit', $systemcontext);
82 if (empty($CFG->usetags)) {
83 throw new moodle_exception('tagsaredisabled', 'tag');
86 $renderer = $PAGE->get_renderer('core');
87 foreach ($tags['tags'] as $tag) {
89 if (array_key_exists('rawname', $tag)) {
90 $tag['rawname'] = clean_param($tag['rawname'], PARAM_TAG);
91 if (empty($tag['rawname'])) {
92 unset($tag['rawname']);
96 // User without manage capability can not change any fields except for descriptions.
97 $tag = array_intersect_key($tag, array('id' => 1,
98 'description' => 1, 'descriptionformat' => 1));
101 // User without edit capability can not change description.
102 $tag = array_diff_key($tag,
103 array('description' => 1, 'descriptionformat' => 1));
105 if (count($tag) <= 1) {
107 'item' => $tag['id'],
108 'warningcode' => 'nothingtoupdate',
109 'message' => get_string('nothingtoupdate', 'core_tag')
113 if (!$tagobject = core_tag_tag::get($tag['id'], '*')) {
115 'item' => $tag['id'],
116 'warningcode' => 'tagnotfound',
117 'message' => get_string('tagnotfound', 'error')
121 // First check if new tag name is allowed.
122 if (!empty($tag['rawname']) && ($existing = core_tag_tag::get_by_name($tagobject->tagcollid, $tag['rawname']))) {
123 if ($existing->id != $tag['id']) {
125 'item' => $tag['id'],
126 'warningcode' => 'namesalreadybeeingused',
127 'message' => get_string('namesalreadybeeingused', 'core_tag')
132 if (array_key_exists('official', $tag)) {
133 // Parameter 'official' deprecated and replaced with 'isstandard'.
134 $tag['isstandard'] = $tag['official'] ? 1 : 0;
135 unset($tag['official']);
137 if (isset($tag['flag'])) {
141 $tagobject->reset_flag();
147 $tagobject->update($tag);
150 return array('warnings' => $warnings);
154 * Return structure for update_tag()
156 * @return external_description
158 public static function update_tags_returns() {
159 return new external_single_structure(
161 'warnings' => new external_warnings()
167 * Parameters for function get_tags()
169 * @return external_function_parameters
171 public static function get_tags_parameters() {
172 return new external_function_parameters(
174 'tags' => new external_multiple_structure(
175 new external_single_structure(
177 'id' => new external_value(PARAM_INT, 'tag id'),
186 * Get tags by their ids
190 public static function get_tags($tags) {
191 global $CFG, $PAGE, $DB;
193 // Validate and normalize parameters.
194 $tags = self::validate_parameters(self::get_tags_parameters(), array('tags' => $tags));
196 $systemcontext = context_system::instance();
197 self::validate_context($systemcontext);
199 $canmanage = has_capability('moodle/tag:manage', $systemcontext);
200 $canedit = has_capability('moodle/tag:edit', $systemcontext);
205 if (empty($CFG->usetags)) {
206 throw new moodle_exception('tagsaredisabled', 'tag');
209 $renderer = $PAGE->get_renderer('core');
210 foreach ($tags['tags'] as $tag) {
212 if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) {
214 'item' => $tag['id'],
215 'warningcode' => 'tagnotfound',
216 'message' => get_string('tagnotfound', 'error')
220 $tagoutput = new \core_tag\output\tag($tagobject);
221 // Do not return some information to users without permissions.
222 $rv = $tagoutput->export_for_template($renderer);
225 unset($rv->isstandard);
226 unset($rv->official);
232 return array('tags' => $return, 'warnings' => $warnings);
236 * Return structure for get_tag()
238 * @return external_description
240 public static function get_tags_returns() {
241 return new external_single_structure(
243 'tags' => new external_multiple_structure( new external_single_structure(
245 'id' => new external_value(PARAM_INT, 'tag id'),
246 'tagcollid' => new external_value(PARAM_INT, 'tag collection id'),
247 'name' => new external_value(PARAM_TAG, 'name'),
248 'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)'),
249 'description' => new external_value(PARAM_RAW, 'tag description'),
250 'descriptionformat' => new external_format_value(PARAM_INT, 'tag description format'),
251 'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL),
252 'official' => new external_value(PARAM_INT,
253 'whether this flag is standard (deprecated, use isstandard)', VALUE_OPTIONAL),
254 'isstandard' => new external_value(PARAM_INT, 'whether this flag is standard', VALUE_OPTIONAL),
255 'viewurl' => new external_value(PARAM_URL, 'URL to view'),
256 ), 'information about one tag')
258 'warnings' => new external_warnings()
264 * Parameters for function get_tagindex()
266 * @return external_function_parameters
268 public static function get_tagindex_parameters() {
269 return new external_function_parameters(
271 'tagindex' => new external_single_structure(array(
272 'tag' => new external_value(PARAM_TAG, 'tag name'),
273 'tc' => new external_value(PARAM_INT, 'tag collection id'),
274 'ta' => new external_value(PARAM_INT, 'tag area id'),
275 'excl' => new external_value(PARAM_BOOL, 'exlusive mode for this tag area', VALUE_OPTIONAL, 0),
276 'from' => new external_value(PARAM_INT, 'context id where the link was displayed', VALUE_OPTIONAL, 0),
277 'ctx' => new external_value(PARAM_INT, 'context id where to search for items', VALUE_OPTIONAL, 0),
278 'rec' => new external_value(PARAM_INT, 'search in the context recursive', VALUE_OPTIONAL, 1),
279 'page' => new external_value(PARAM_INT, 'page number (0-based)', VALUE_OPTIONAL, 0),
286 * Get tags by their ids
288 * @param array $params
290 public static function get_tagindex($params) {
292 // Validate and normalize parameters.
293 $tagindex = self::validate_parameters(
294 self::get_tagindex_parameters(), array('tagindex' => $params));
295 $params = $tagindex['tagindex'] + array(
303 // Login to the course / module if applicable.
304 $context = $params['ctx'] ? context::instance_by_id($params['ctx']) : context_system::instance();
305 self::validate_context($context);
307 $tag = core_tag_tag::get_by_name($params['tc'], $params['tag'], '*', MUST_EXIST);
308 $tagareas = core_tag_collection::get_areas($params['tc']);
309 $tagindex = $tag->get_tag_index($tagareas[$params['ta']], $params['excl'], $params['from'],
310 $params['ctx'], $params['rec'], $params['page']);
311 $renderer = $PAGE->get_renderer('core');
312 return $tagindex->export_for_template($renderer);
316 * Return structure for get_tag()
318 * @return external_description
320 public static function get_tagindex_returns() {
321 return new external_single_structure(
323 'tagid' => new external_value(PARAM_INT, 'tag id'),
324 'ta' => new external_value(PARAM_INT, 'tag area id'),
325 'component' => new external_value(PARAM_COMPONENT, 'component'),
326 'itemtype' => new external_value(PARAM_NOTAGS, 'itemtype'),
327 'nextpageurl' => new external_value(PARAM_URL, 'URL for the next page', VALUE_OPTIONAL),
328 'prevpageurl' => new external_value(PARAM_URL, 'URL for the next page', VALUE_OPTIONAL),
329 'exclusiveurl' => new external_value(PARAM_URL, 'URL for exclusive link', VALUE_OPTIONAL),
330 'exclusivetext' => new external_value(PARAM_TEXT, 'text for exclusive link', VALUE_OPTIONAL),
331 'title' => new external_value(PARAM_RAW, 'title'),
332 'content' => new external_value(PARAM_RAW, 'title'),
333 'hascontent' => new external_value(PARAM_INT, 'whether the content is present'),
334 'anchor' => new external_value(PARAM_TEXT, 'name of anchor', VALUE_OPTIONAL),
341 * Parameters for function get_tagindex_per_area()
343 * @return external_function_parameters
346 public static function get_tagindex_per_area_parameters() {
347 return new external_function_parameters(
349 'tagindex' => new external_single_structure(array(
350 'id' => new external_value(PARAM_INT, 'tag id', VALUE_OPTIONAL, 0),
351 'tag' => new external_value(PARAM_TAG, 'tag name', VALUE_OPTIONAL, ''),
352 'tc' => new external_value(PARAM_INT, 'tag collection id', VALUE_OPTIONAL, 0),
353 'ta' => new external_value(PARAM_INT, 'tag area id', VALUE_OPTIONAL, 0),
354 'excl' => new external_value(PARAM_BOOL, 'exlusive mode for this tag area', VALUE_OPTIONAL, 0),
355 'from' => new external_value(PARAM_INT, 'context id where the link was displayed', VALUE_OPTIONAL, 0),
356 'ctx' => new external_value(PARAM_INT, 'context id where to search for items', VALUE_OPTIONAL, 0),
357 'rec' => new external_value(PARAM_INT, 'search in the context recursive', VALUE_OPTIONAL, 1),
358 'page' => new external_value(PARAM_INT, 'page number (0-based)', VALUE_OPTIONAL, 0),
365 * Returns the tag index per multiple areas if requested.
367 * @param array $params Tag index required information.
368 * @throws moodle_exception
371 public static function get_tagindex_per_area($params) {
373 // Validate and normalize parameters.
374 $tagindex = self::validate_parameters(
375 self::get_tagindex_per_area_parameters(), array('tagindex' => $params));
376 $params = $tagindex['tagindex'] + array( // Force defaults.
388 if (empty($CFG->usetags)) {
389 throw new moodle_exception('tagsaredisabled', 'tag');
392 if (!empty($params['tag'])) {
393 if (empty($params['tc'])) {
394 // Tag name specified but tag collection was not. Try to guess it.
395 $tags = core_tag_tag::guess_by_name($params['tag'], '*');
396 if (count($tags) > 1) {
397 // It is in more that one collection, do not display.
398 throw new moodle_exception('Tag is in more that one collection, please indicate one.');
399 } else if (count($tags) == 1) {
403 if (!$tag = core_tag_tag::get_by_name($params['tc'], $params['tag'], '*')) {
404 // Not found in collection.
405 throw new moodle_exception('notagsfound', 'tag');
408 } else if (!empty($params['id'])) {
409 $tag = core_tag_tag::get($params['id'], '*');
413 throw new moodle_exception('notagsfound', 'tag');
416 // Login to the course / module if applicable.
417 $context = !empty($params['ctx']) ? context::instance_by_id($params['ctx']) : context_system::instance();
418 self::validate_context($context);
420 $tag = core_tag_tag::get_by_name($params['tc'], $tag->name, '*', MUST_EXIST);
421 $tagareas = core_tag_collection::get_areas($params['tc']);
422 $tagareaid = $params['ta'];
425 // Find all areas in this collection and their items tagged with this tag.
427 $tagareas = array($tagareas[$tagareaid]);
429 if (!$tagareaid && count($tagareas) == 1) {
430 // Automatically set "exclusive" mode for tag collection with one tag area only.
434 $renderer = $PAGE->get_renderer('core');
436 foreach ($tagareas as $ta) {
437 $tagindex = $tag->get_tag_index($ta, $params['excl'], $params['from'], $params['ctx'], $params['rec'], $params['page']);
438 if (!empty($tagindex->hascontent)) {
439 $result[] = $tagindex->export_for_template($renderer);
446 * Return structure for get_tagindex_per_area
448 * @return external_description
451 public static function get_tagindex_per_area_returns() {
452 return new external_multiple_structure(
453 self::get_tagindex_returns()
458 * Returns description of get_tag_areas() parameters.
460 * @return external_function_parameters
463 public static function get_tag_areas_parameters() {
464 return new external_function_parameters(array());
468 * Retrieves existing tag areas.
470 * @return array an array of warnings and objects containing the plugin information
471 * @throws moodle_exception
474 public static function get_tag_areas() {
477 if (empty($CFG->usetags)) {
478 throw new moodle_exception('tagsaredisabled', 'tag');
481 $context = context_system::instance();
482 self::validate_context($context);
483 $PAGE->set_context($context); // Needed by internal APIs.
484 $output = $PAGE->get_renderer('core');
486 $areas = core_tag_area::get_areas();
487 $exportedareas = array();
488 foreach ($areas as $itemtype => $component) {
489 foreach ($component as $area) {
490 // Move optional fields not part of the DB table to otherdata.
492 if (isset($area->locked)) {
493 $locked = $area->locked;
494 unset($area->locked);
496 $exporter = new \core_tag\external\tag_area_exporter($area, array('locked' => $locked));
497 $exportedareas[] = $exporter->export($output);
502 'areas' => $exportedareas,
503 'warnings' => array(),
508 * Returns description of get_tag_areas() result value.
510 * @return external_description
513 public static function get_tag_areas_returns() {
514 return new external_single_structure(
516 'areas' => new external_multiple_structure(
517 \core_tag\external\tag_area_exporter::get_read_structure()
519 'warnings' => new external_warnings(),
525 * Returns description of get_tag_collections() parameters.
527 * @return external_function_parameters
530 public static function get_tag_collections_parameters() {
531 return new external_function_parameters(array());
535 * Retrieves existing tag collections.
537 * @return array an array of warnings and tag collections
538 * @throws moodle_exception
541 public static function get_tag_collections() {
544 if (empty($CFG->usetags)) {
545 throw new moodle_exception('tagsaredisabled', 'tag');
548 $context = context_system::instance();
549 self::validate_context($context);
550 $PAGE->set_context($context); // Needed by internal APIs.
551 $output = $PAGE->get_renderer('core');
553 $collections = core_tag_collection::get_collections();
554 $exportedcollections = array();
555 foreach ($collections as $collection) {
556 $exporter = new \core_tag\external\tag_collection_exporter($collection);
557 $exportedcollections[] = $exporter->export($output);
561 'collections' => $exportedcollections,
562 'warnings' => array(),
567 * Returns description of get_tag_collections() result value.
569 * @return external_description
572 public static function get_tag_collections_returns() {
573 return new external_single_structure(
575 'collections' => new external_multiple_structure(
576 \core_tag\external\tag_collection_exporter::get_read_structure()
578 'warnings' => new external_warnings(),
584 * Returns description of get_tag_cloud() parameters.
586 * @return external_function_parameters
589 public static function get_tag_cloud_parameters() {
590 return new external_function_parameters(
592 'tagcollid' => new external_value(PARAM_INT, 'Tag collection id.', VALUE_DEFAULT, 0),
593 'isstandard' => new external_value(PARAM_BOOL, 'Whether to return only standard tags.', VALUE_DEFAULT, false),
594 'limit' => new external_value(PARAM_INT, 'Maximum number of tags to retrieve.', VALUE_DEFAULT, 150),
595 'sort' => new external_value(PARAM_ALPHA, 'Sort order for display
596 (id, name, rawname, count, flag, isstandard, tagcollid).', VALUE_DEFAULT, 'name'),
597 'search' => new external_value(PARAM_RAW, 'Search string.', VALUE_DEFAULT, ''),
598 'fromctx' => new external_value(PARAM_INT, 'Context id where this tag cloud is displayed.', VALUE_DEFAULT, 0),
599 'ctx' => new external_value(PARAM_INT, 'Only retrieve tag instances in this context.', VALUE_DEFAULT, 0),
600 'rec' => new external_value(PARAM_INT, 'Retrieve tag instances in the $ctx context and it\'s children.',
607 * Retrieves a tag cloud for display.
609 * @param int $tagcollid tag collection id
610 * @param bool $isstandard return only standard tags
611 * @param int $limit maximum number of tags to retrieve, tags are sorted by the instance count
612 * descending here regardless of $sort parameter
613 * @param string $sort sort order for display, default 'name' - tags will be sorted after they are retrieved
614 * @param string $search search string
615 * @param int $fromctx context id where this tag cloud is displayed
616 * @param int $ctx only retrieve tag instances in this context
617 * @param int $rec retrieve tag instances in the $ctx context and it's children (default 1)
618 * @return array an array of warnings and tag cloud information and items
619 * @throws moodle_exception
622 public static function get_tag_cloud($tagcollid = 0, $isstandard = false, $limit = 150, $sort = 'name',
623 $search = '', $fromctx = 0, $ctx = 0, $rec = 1) {
626 $params = self::validate_parameters(self::get_tag_cloud_parameters(),
628 'tagcollid' => $tagcollid,
629 'isstandard' => $isstandard,
633 'fromctx' => $fromctx,
639 if (empty($CFG->usetags)) {
640 throw new moodle_exception('tagsaredisabled', 'tag');
643 $context = context_system::instance();
644 self::validate_context($context);
645 $PAGE->set_context($context); // Needed by internal APIs.
646 $output = $PAGE->get_renderer('core');
648 $tagcloud = core_tag_collection::get_tag_cloud($params['tagcollid'], $params['isstandard'], $params['limit'],
649 $params['sort'], $params['search'], $params['fromctx'], $params['ctx'], $params['rec']);
651 $result = $tagcloud->export_for_template($output);
652 $result->warnings = array();
654 return (array) $result;
658 * Returns description of get_tag_cloud() result value.
660 * @return external_description
663 public static function get_tag_cloud_returns() {
664 return new external_single_structure(
666 'tags' => new external_multiple_structure(
667 new external_single_structure(
669 'name' => new external_value(PARAM_TAG, 'Tag name.'),
670 'viewurl' => new external_value(PARAM_RAW, 'URL to view the tag index.'),
671 'flag' => new external_value(PARAM_BOOL, 'Whether the tag is flagged as inappropriate.',
673 'isstandard' => new external_value(PARAM_BOOL, 'Whether is a standard tag or not.', VALUE_OPTIONAL),
674 'count' => new external_value(PARAM_INT, 'Number of tag instances.', VALUE_OPTIONAL),
675 'size' => new external_value(PARAM_INT, 'Proportional size to display the tag.', VALUE_OPTIONAL),
679 'tagscount' => new external_value(PARAM_INT, 'Number of tags returned.'),
680 'totalcount' => new external_value(PARAM_INT, 'Total count of tags.'),
681 'warnings' => new external_warnings(),