[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 25 require_once("$CFG->libdir/externallib.php"); 26 require_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 */ 35 class 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, 55 '(deprecated, use isstandard) whether this flag is standard', VALUE_OPTIONAL), 56 'isstandard' => new external_value(PARAM_INT, 'whether this flag is standard', VALUE_OPTIONAL), 57 ) 58 ) 59 ) 60 ) 61 ); 62 } 63 64 /** 65 * Update tags 66 * 67 * @param array $tags 68 */ 69 public static function update_tags($tags) { 70 global $CFG, $PAGE, $DB; 71 72 // Validate and normalize parameters. 73 $tags = self::validate_parameters(self::update_tags_parameters(), array('tags' => $tags)); 74 75 $systemcontext = context_system::instance(); 76 $canmanage = has_capability('moodle/tag:manage', $systemcontext); 77 $canedit = has_capability('moodle/tag:edit', $systemcontext); 78 $warnings = array(); 79 80 if (empty($CFG->usetags)) { 81 throw new moodle_exception('tagsaredisabled', 'tag'); 82 } 83 84 $renderer = $PAGE->get_renderer('core'); 85 foreach ($tags['tags'] as $tag) { 86 $tag = (array)$tag; 87 if (array_key_exists('rawname', $tag)) { 88 $tag['rawname'] = clean_param($tag['rawname'], PARAM_TAG); 89 if (empty($tag['rawname'])) { 90 unset($tag['rawname']); 91 } 92 } 93 if (!$canmanage) { 94 // User without manage capability can not change any fields except for descriptions. 95 $tag = array_intersect_key($tag, array('id' => 1, 96 'description' => 1, 'descriptionformat' => 1)); 97 } 98 if (!$canedit) { 99 // User without edit capability can not change description. 100 $tag = array_diff_key($tag, 101 array('description' => 1, 'descriptionformat' => 1)); 102 } 103 if (count($tag) <= 1) { 104 $warnings[] = array( 105 'item' => $tag['id'], 106 'warningcode' => 'nothingtoupdate', 107 'message' => get_string('nothingtoupdate', 'core_tag') 108 ); 109 continue; 110 } 111 if (!$tagobject = core_tag_tag::get($tag['id'], '*')) { 112 $warnings[] = array( 113 'item' => $tag['id'], 114 'warningcode' => 'tagnotfound', 115 'message' => get_string('tagnotfound', 'error') 116 ); 117 continue; 118 } 119 // First check if new tag name is allowed. 120 if (!empty($tag['rawname']) && ($existing = core_tag_tag::get_by_name($tagobject->tagcollid, $tag['rawname']))) { 121 if ($existing->id != $tag['id']) { 122 $warnings[] = array( 123 'item' => $tag['id'], 124 'warningcode' => 'namesalreadybeeingused', 125 'message' => get_string('namesalreadybeeingused', 'core_tag') 126 ); 127 continue; 128 } 129 } 130 if (array_key_exists('official', $tag)) { 131 // Parameter 'official' deprecated and replaced with 'isstandard'. 132 $tag['isstandard'] = $tag['official'] ? 1 : 0; 133 unset($tag['official']); 134 } 135 if (isset($tag['flag'])) { 136 if ($tag['flag']) { 137 $tagobject->flag(); 138 } else { 139 $tagobject->reset_flag(); 140 } 141 unset($tag['flag']); 142 } 143 unset($tag['id']); 144 if (count($tag)) { 145 $tagobject->update($tag); 146 } 147 } 148 return array('warnings' => $warnings); 149 } 150 151 /** 152 * Return structure for update_tag() 153 * 154 * @return external_description 155 */ 156 public static function update_tags_returns() { 157 return new external_single_structure( 158 array( 159 'warnings' => new external_warnings() 160 ) 161 ); 162 } 163 164 /** 165 * Parameters for function get_tags() 166 * 167 * @return external_function_parameters 168 */ 169 public static function get_tags_parameters() { 170 return new external_function_parameters( 171 array( 172 'tags' => new external_multiple_structure( 173 new external_single_structure( 174 array( 175 'id' => new external_value(PARAM_INT, 'tag id'), 176 ) 177 ) 178 ) 179 ) 180 ); 181 } 182 183 /** 184 * Get tags by their ids 185 * 186 * @param array $tags 187 */ 188 public static function get_tags($tags) { 189 global $CFG, $PAGE, $DB; 190 191 // Validate and normalize parameters. 192 $tags = self::validate_parameters(self::get_tags_parameters(), array('tags' => $tags)); 193 194 $systemcontext = context_system::instance(); 195 self::validate_context($systemcontext); 196 197 $canmanage = has_capability('moodle/tag:manage', $systemcontext); 198 $canedit = has_capability('moodle/tag:edit', $systemcontext); 199 200 $return = array(); 201 $warnings = array(); 202 203 if (empty($CFG->usetags)) { 204 throw new moodle_exception('tagsaredisabled', 'tag'); 205 } 206 207 $renderer = $PAGE->get_renderer('core'); 208 foreach ($tags['tags'] as $tag) { 209 $tag = (array)$tag; 210 if (!$tagobject = $DB->get_record('tag', array('id' => $tag['id']))) { 211 $warnings[] = array( 212 'item' => $tag['id'], 213 'warningcode' => 'tagnotfound', 214 'message' => get_string('tagnotfound', 'error') 215 ); 216 continue; 217 } 218 $tagoutput = new \core_tag\output\tag($tagobject); 219 // Do not return some information to users without permissions. 220 $rv = $tagoutput->export_for_template($renderer); 221 if (!$canmanage) { 222 if (!$canedit) { 223 unset($rv->isstandard); 224 unset($rv->official); 225 } 226 unset($rv->flag); 227 } 228 $return[] = $rv; 229 } 230 return array('tags' => $return, 'warnings' => $warnings); 231 } 232 233 /** 234 * Return structure for get_tag() 235 * 236 * @return external_description 237 */ 238 public static function get_tags_returns() { 239 return new external_single_structure( 240 array( 241 'tags' => new external_multiple_structure( new external_single_structure( 242 array( 243 'id' => new external_value(PARAM_INT, 'tag id'), 244 'tagcollid' => new external_value(PARAM_INT, 'tag collection id'), 245 'name' => new external_value(PARAM_TAG, 'name'), 246 'rawname' => new external_value(PARAM_RAW, 'tag raw name (may contain capital letters)'), 247 'description' => new external_value(PARAM_RAW, 'tag description'), 248 'descriptionformat' => new external_format_value(PARAM_INT, 'tag description format'), 249 'flag' => new external_value(PARAM_INT, 'flag', VALUE_OPTIONAL), 250 'official' => new external_value(PARAM_INT, 251 'whether this flag is standard (deprecated, use isstandard)', VALUE_OPTIONAL), 252 'isstandard' => new external_value(PARAM_INT, 'whether this flag is standard', VALUE_OPTIONAL), 253 'viewurl' => new external_value(PARAM_URL, 'URL to view'), 254 ), 'information about one tag') 255 ), 256 'warnings' => new external_warnings() 257 ) 258 ); 259 } 260 261 /** 262 * Parameters for function get_tagindex() 263 * 264 * @return external_function_parameters 265 */ 266 public static function get_tagindex_parameters() { 267 return new external_function_parameters( 268 array( 269 'tagindex' => new external_single_structure(array( 270 'tag' => new external_value(PARAM_TAG, 'tag name'), 271 'tc' => new external_value(PARAM_INT, 'tag collection id'), 272 'ta' => new external_value(PARAM_INT, 'tag area id'), 273 'excl' => new external_value(PARAM_BOOL, 'exlusive mode for this tag area', VALUE_OPTIONAL, 0), 274 'from' => new external_value(PARAM_INT, 'context id where the link was displayed', VALUE_OPTIONAL, 0), 275 'ctx' => new external_value(PARAM_INT, 'context id where to search for items', VALUE_OPTIONAL, 0), 276 'rec' => new external_value(PARAM_INT, 'search in the context recursive', VALUE_OPTIONAL, 1), 277 'page' => new external_value(PARAM_INT, 'page number (0-based)', VALUE_OPTIONAL, 0), 278 ), 'parameters') 279 ) 280 ); 281 } 282 283 /** 284 * Get tags by their ids 285 * 286 * @param array $params 287 */ 288 public static function get_tagindex($params) { 289 global $PAGE; 290 // Validate and normalize parameters. 291 $tagindex = self::validate_parameters( 292 self::get_tagindex_parameters(), array('tagindex' => $params)); 293 $params = $tagindex['tagindex'] + array( 294 'excl' => 0, 295 'from' => 0, 296 'ctx' => 0, 297 'rec' => 1, 298 'page' => 0 299 ); 300 301 // Login to the course / module if applicable. 302 $context = $params['ctx'] ? context::instance_by_id($params['ctx']) : context_system::instance(); 303 self::validate_context($context); 304 305 $tag = core_tag_tag::get_by_name($params['tc'], $params['tag'], '*', MUST_EXIST); 306 $tagareas = core_tag_collection::get_areas($params['tc']); 307 $tagindex = $tag->get_tag_index($tagareas[$params['ta']], $params['excl'], $params['from'], 308 $params['ctx'], $params['rec'], $params['page']); 309 $renderer = $PAGE->get_renderer('core'); 310 return $tagindex->export_for_template($renderer); 311 } 312 313 /** 314 * Return structure for get_tag() 315 * 316 * @return external_description 317 */ 318 public static function get_tagindex_returns() { 319 return new external_single_structure( 320 array( 321 'tagid' => new external_value(PARAM_INT, 'tag id'), 322 'ta' => new external_value(PARAM_INT, 'tag area id'), 323 'component' => new external_value(PARAM_COMPONENT, 'component'), 324 'itemtype' => new external_value(PARAM_NOTAGS, 'itemtype'), 325 'nextpageurl' => new external_value(PARAM_URL, 'URL for the next page', VALUE_OPTIONAL), 326 'prevpageurl' => new external_value(PARAM_URL, 'URL for the next page', VALUE_OPTIONAL), 327 'exclusiveurl' => new external_value(PARAM_URL, 'URL for exclusive link', VALUE_OPTIONAL), 328 'exclusivetext' => new external_value(PARAM_TEXT, 'text for exclusive link', VALUE_OPTIONAL), 329 'title' => new external_value(PARAM_RAW, 'title'), 330 'content' => new external_value(PARAM_RAW, 'title'), 331 'hascontent' => new external_value(PARAM_INT, 'whether the content is present'), 332 'anchor' => new external_value(PARAM_TEXT, 'name of anchor', VALUE_OPTIONAL), 333 ), 'tag index' 334 ); 335 } 336 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |