PageRenderTime 24ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/admin/tool/usertours/classes/external/tour.php

https://bitbucket.org/moodle/moodle
PHP | 333 lines | 187 code | 38 blank | 108 comment | 5 complexity | 2eadd8708c49068b11956abdfd287f7c MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. * Web Service functions for steps.
  18. *
  19. * @package tool_usertours
  20. * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. namespace tool_usertours\external;
  24. defined('MOODLE_INTERNAL') || die();
  25. use external_api;
  26. use external_function_parameters;
  27. use external_single_structure;
  28. use external_multiple_structure;
  29. use external_value;
  30. use tool_usertours\tour as tourinstance;
  31. use tool_usertours\step;
  32. /**
  33. * Web Service functions for steps.
  34. *
  35. * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
  36. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37. */
  38. class tour extends external_api {
  39. /**
  40. * Fetch the tour configuration for the specified tour.
  41. *
  42. * @param int $tourid The ID of the tour to fetch.
  43. * @param int $context The Context ID of the current page.
  44. * @param string $pageurl The path of the current page.
  45. * @return array As described in fetch_and_start_tour_returns
  46. */
  47. public static function fetch_and_start_tour($tourid, $context, $pageurl) {
  48. global $PAGE;
  49. $params = self::validate_parameters(self::fetch_and_start_tour_parameters(), [
  50. 'tourid' => $tourid,
  51. 'context' => $context,
  52. 'pageurl' => $pageurl,
  53. ]);
  54. $context = \context_helper::instance_by_id($params['context']);
  55. self::validate_context($context);
  56. $tour = tourinstance::instance($params['tourid']);
  57. if (!$tour->should_show_for_user()) {
  58. return [];
  59. }
  60. $touroutput = new \tool_usertours\output\tour($tour);
  61. \tool_usertours\event\tour_started::create([
  62. 'contextid' => $context->id,
  63. 'objectid' => $tour->get_id(),
  64. 'other' => [
  65. 'pageurl' => $params['pageurl'],
  66. ],
  67. ])->trigger();
  68. return [
  69. 'tourconfig' => $touroutput->export_for_template($PAGE->get_renderer('core')),
  70. ];
  71. }
  72. /**
  73. * The parameters for fetch_and_start_tour.
  74. *
  75. * @return external_function_parameters
  76. */
  77. public static function fetch_and_start_tour_parameters() {
  78. return new external_function_parameters([
  79. 'tourid' => new external_value(PARAM_INT, 'Tour ID'),
  80. 'context' => new external_value(PARAM_INT, 'Context ID'),
  81. 'pageurl' => new external_value(PARAM_URL, 'Page URL'),
  82. ]);
  83. }
  84. /**
  85. * The return configuration for fetch_and_start_tour.
  86. *
  87. * @return external_single_structure
  88. */
  89. public static function fetch_and_start_tour_returns() {
  90. return new external_single_structure([
  91. 'tourconfig' => new external_single_structure([
  92. 'name' => new external_value(PARAM_RAW, 'Tour Name'),
  93. 'steps' => new external_multiple_structure(self::step_structure_returns()),
  94. 'endtourlabel' => new external_value(PARAM_RAW, 'Label of the end tour button'),
  95. 'displaystepnumbers' => new external_value(PARAM_BOOL, 'display step number'),
  96. ], 'Tour config', VALUE_OPTIONAL)
  97. ]);
  98. }
  99. /**
  100. * Reset the specified tour for the current user.
  101. *
  102. * @param int $tourid The ID of the tour.
  103. * @param int $context The Context ID of the current page.
  104. * @param string $pageurl The path of the current page requesting the reset.
  105. * @return array As described in reset_tour_returns
  106. */
  107. public static function reset_tour($tourid, $context, $pageurl) {
  108. $params = self::validate_parameters(self::reset_tour_parameters(), [
  109. 'tourid' => $tourid,
  110. 'context' => $context,
  111. 'pageurl' => $pageurl,
  112. ]);
  113. $context = \context_helper::instance_by_id($params['context']);
  114. self::validate_context($context);
  115. $tour = tourinstance::instance($params['tourid']);
  116. $tour->request_user_reset();
  117. $result = [];
  118. $matchingtours = \tool_usertours\manager::get_matching_tours(new \moodle_url($params['pageurl']));
  119. foreach ($matchingtours as $match) {
  120. if ($tour->get_id() === $match->get_id()) {
  121. $result['startTour'] = $tour->get_id();
  122. \tool_usertours\event\tour_reset::create([
  123. 'contextid' => $context->id,
  124. 'objectid' => $params['tourid'],
  125. 'other' => [
  126. 'pageurl' => $params['pageurl'],
  127. ],
  128. ])->trigger();
  129. break;
  130. }
  131. }
  132. return $result;
  133. }
  134. /**
  135. * The parameters for reset_tour.
  136. *
  137. * @return external_function_parameters
  138. */
  139. public static function reset_tour_parameters() {
  140. return new external_function_parameters([
  141. 'tourid' => new external_value(PARAM_INT, 'Tour ID'),
  142. 'context' => new external_value(PARAM_INT, 'Context ID'),
  143. 'pageurl' => new external_value(PARAM_URL, 'Current page location'),
  144. ]);
  145. }
  146. /**
  147. * The return configuration for reset_tour.
  148. *
  149. * @return external_single_structure
  150. */
  151. public static function reset_tour_returns() {
  152. return new external_single_structure([
  153. 'startTour' => new external_value(PARAM_INT, 'Tour ID', VALUE_OPTIONAL),
  154. ]);
  155. }
  156. /**
  157. * Mark the specified tour as completed for the current user.
  158. *
  159. * @param int $tourid The ID of the tour.
  160. * @param int $context The Context ID of the current page.
  161. * @param string $pageurl The path of the current page.
  162. * @param int $stepid The step id
  163. * @param int $stepindex The step index
  164. * @return array As described in complete_tour_returns
  165. */
  166. public static function complete_tour($tourid, $context, $pageurl, $stepid, $stepindex) {
  167. $params = self::validate_parameters(self::complete_tour_parameters(), [
  168. 'tourid' => $tourid,
  169. 'context' => $context,
  170. 'pageurl' => $pageurl,
  171. 'stepid' => $stepid,
  172. 'stepindex' => $stepindex,
  173. ]);
  174. $context = \context_helper::instance_by_id($params['context']);
  175. self::validate_context($context);
  176. $tour = tourinstance::instance($params['tourid']);
  177. $tour->mark_user_completed();
  178. \tool_usertours\event\tour_ended::create([
  179. 'contextid' => $context->id,
  180. 'objectid' => $params['tourid'],
  181. 'other' => [
  182. 'pageurl' => $params['pageurl'],
  183. 'stepid' => $params['stepid'],
  184. 'stepindex' => $params['stepindex'],
  185. ],
  186. ])->trigger();
  187. return [];
  188. }
  189. /**
  190. * The parameters for complete_tour.
  191. *
  192. * @return external_function_parameters
  193. */
  194. public static function complete_tour_parameters() {
  195. return new external_function_parameters([
  196. 'tourid' => new external_value(PARAM_INT, 'Tour ID'),
  197. 'context' => new external_value(PARAM_INT, 'Context ID'),
  198. 'pageurl' => new external_value(PARAM_LOCALURL, 'Page URL'),
  199. 'stepid' => new external_value(PARAM_INT, 'Step ID'),
  200. 'stepindex' => new external_value(PARAM_INT, 'Step Number'),
  201. ]);
  202. }
  203. /**
  204. * The return configuration for complete_tour.
  205. *
  206. * @return external_single_structure
  207. */
  208. public static function complete_tour_returns() {
  209. return new external_single_structure([]);
  210. }
  211. /**
  212. * Mark the specified toru step as shown for the current user.
  213. *
  214. * @param int $tourid The ID of the tour.
  215. * @param int $context The Context ID of the current page.
  216. * @param string $pageurl The path of the current page.
  217. * @param int $stepid The step id
  218. * @param int $stepindex The step index
  219. * @return array As described in complete_tour_returns
  220. */
  221. public static function step_shown($tourid, $context, $pageurl, $stepid, $stepindex) {
  222. $params = self::validate_parameters(self::step_shown_parameters(), [
  223. 'tourid' => $tourid,
  224. 'context' => $context,
  225. 'pageurl' => $pageurl,
  226. 'stepid' => $stepid,
  227. 'stepindex' => $stepindex,
  228. ]);
  229. $context = \context_helper::instance_by_id($params['context']);
  230. self::validate_context($context);
  231. $step = step::instance($params['stepid']);
  232. if ($step->get_tourid() != $params['tourid']) {
  233. throw new \moodle_exception('Incorrect tour specified.');
  234. }
  235. \tool_usertours\event\step_shown::create([
  236. 'contextid' => $context->id,
  237. 'objectid' => $params['stepid'],
  238. 'other' => [
  239. 'pageurl' => $params['pageurl'],
  240. 'tourid' => $params['tourid'],
  241. 'stepindex' => $params['stepindex'],
  242. ],
  243. ])->trigger();
  244. return [];
  245. }
  246. /**
  247. * The parameters for step_shown.
  248. *
  249. * @return external_function_parameters
  250. */
  251. public static function step_shown_parameters() {
  252. return new external_function_parameters([
  253. 'tourid' => new external_value(PARAM_INT, 'Tour ID'),
  254. 'context' => new external_value(PARAM_INT, 'Context ID'),
  255. 'pageurl' => new external_value(PARAM_URL, 'Page URL'),
  256. 'stepid' => new external_value(PARAM_INT, 'Step ID'),
  257. 'stepindex' => new external_value(PARAM_INT, 'Step Number'),
  258. ]);
  259. }
  260. /**
  261. * The return configuration for step_shown.
  262. *
  263. * @return external_single_structure
  264. */
  265. public static function step_shown_returns() {
  266. return new external_single_structure([]);
  267. }
  268. /**
  269. * The standard return structure for a step.
  270. *
  271. * @return external_multiple_structure
  272. */
  273. public static function step_structure_returns() {
  274. return new external_single_structure([
  275. 'title' => new external_value(PARAM_RAW,
  276. 'Step Title'),
  277. 'content' => new external_value(PARAM_RAW,
  278. 'Step Content'),
  279. 'element' => new external_value(PARAM_TEXT,
  280. 'Step Target'),
  281. 'placement' => new external_value(PARAM_TEXT,
  282. 'Step Placement'),
  283. 'delay' => new external_value(PARAM_INT,
  284. 'Delay before showing the step (ms)', VALUE_OPTIONAL),
  285. 'backdrop' => new external_value(PARAM_BOOL,
  286. 'Whether a backdrop should be used', VALUE_OPTIONAL),
  287. 'reflex' => new external_value(PARAM_BOOL,
  288. 'Whether to move to the next step when the target element is clicked', VALUE_OPTIONAL),
  289. 'orphan' => new external_value(PARAM_BOOL,
  290. 'Whether to display the step even if it could not be found', VALUE_OPTIONAL),
  291. 'stepid' => new external_value(PARAM_INT,
  292. 'The actual ID of the step', VALUE_OPTIONAL),
  293. ]);
  294. }
  295. }