Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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