PageRenderTime 25ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tag/classes/external.php

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