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   * Tour helper.
  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;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Tour helper.
  31   *
  32   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class helper {
  36  
  37      /**
  38       * @var MOVE_UP
  39       */
  40      const MOVE_UP = -1;
  41  
  42      /**
  43       * @var MOVE_DOWN
  44       */
  45      const MOVE_DOWN = 1;
  46  
  47      /**
  48       * @var boolean Has it been bootstrapped?
  49       */
  50      private static $bootstrapped = false;
  51  
  52      /**
  53       * Get the link to edit the step.
  54       *
  55       * If no stepid is specified, then a link to create a new step is provided. The $targettype must be specified in this case.
  56       *
  57       * @param   int     $tourid     The tour that the step belongs to.
  58       * @param   int     $stepid     The step ID.
  59       * @param   int     $targettype The type of step.
  60       *
  61       * @return moodle_url
  62       */
  63      public static function get_edit_step_link($tourid, $stepid = null, $targettype = null) {
  64          $link = new \moodle_url('/admin/tool/usertours/configure.php');
  65  
  66          if ($stepid) {
  67              $link->param('action', manager::ACTION_EDITSTEP);
  68              $link->param('id', $stepid);
  69          } else {
  70              $link->param('action', manager::ACTION_NEWSTEP);
  71              $link->param('tourid', $tourid);
  72          }
  73  
  74          return $link;
  75      }
  76  
  77      /**
  78       * Get the link to move the tour.
  79       *
  80       * @param   int     $tourid     The tour ID.
  81       * @param   int     $direction  The direction to move in
  82       *
  83       * @return moodle_url
  84       */
  85      public static function get_move_tour_link($tourid, $direction = self::MOVE_DOWN) {
  86          $link = new \moodle_url('/admin/tool/usertours/configure.php');
  87  
  88          $link->param('action', manager::ACTION_MOVETOUR);
  89          $link->param('id', $tourid);
  90          $link->param('direction', $direction);
  91          $link->param('sesskey', sesskey());
  92  
  93          return $link;
  94      }
  95  
  96      /**
  97       * Get the link to move the step.
  98       *
  99       * @param   int     $stepid     The step ID.
 100       * @param   int     $direction  The direction to move in
 101       *
 102       * @return moodle_url
 103       */
 104      public static function get_move_step_link($stepid, $direction = self::MOVE_DOWN) {
 105          $link = new \moodle_url('/admin/tool/usertours/configure.php');
 106  
 107          $link->param('action', manager::ACTION_MOVESTEP);
 108          $link->param('id', $stepid);
 109          $link->param('direction', $direction);
 110          $link->param('sesskey', sesskey());
 111  
 112          return $link;
 113      }
 114  
 115      /**
 116       * Get the link ot create a new step.
 117       *
 118       * @param   int         $tourid     The ID of the tour to attach this step to.
 119       * @param   int         $targettype The type of target.
 120       *
 121       * @return  moodle_url              The required URL.
 122       */
 123      public static function get_new_step_link($tourid, $targettype = null) {
 124          $link = new \moodle_url('/admin/tool/usertours/configure.php');
 125          $link->param('action', manager::ACTION_NEWSTEP);
 126          $link->param('tourid', $tourid);
 127          $link->param('targettype', $targettype);
 128  
 129          return $link;
 130      }
 131  
 132      /**
 133       * Get the link used to view the tour.
 134       *
 135       * @param   int         $tourid     The ID of the tour to display.
 136       * @return  moodle_url              The URL.
 137       */
 138      public static function get_view_tour_link($tourid) {
 139          return new \moodle_url('/admin/tool/usertours/configure.php', [
 140                  'id'        => $tourid,
 141                  'action'    => manager::ACTION_VIEWTOUR,
 142              ]);
 143      }
 144  
 145      /**
 146       * Get the link used to reset the tour state for all users.
 147       *
 148       * @param   int         $tourid     The ID of the tour to display.
 149       * @return  moodle_url              The URL.
 150       */
 151      public static function get_reset_tour_for_all_link($tourid) {
 152          return new \moodle_url('/admin/tool/usertours/configure.php', [
 153                  'id'        => $tourid,
 154                  'action'    => manager::ACTION_RESETFORALL,
 155                  'sesskey'   => sesskey(),
 156              ]);
 157      }
 158  
 159      /**
 160       * Get the link used to edit the tour.
 161       *
 162       * @param   int         $tourid     The ID of the tour to edit.
 163       * @return  moodle_url              The URL.
 164       */
 165      public static function get_edit_tour_link($tourid = null) {
 166          $link = new \moodle_url('/admin/tool/usertours/configure.php');
 167  
 168          if ($tourid) {
 169              $link->param('action', manager::ACTION_EDITTOUR);
 170              $link->param('id', $tourid);
 171          } else {
 172              $link->param('action', manager::ACTION_NEWTOUR);
 173          }
 174  
 175          return $link;
 176      }
 177  
 178      /**
 179       * Get the link used to import the tour.
 180       *
 181       * @return  moodle_url              The URL.
 182       */
 183      public static function get_import_tour_link() {
 184          $link = new \moodle_url('/admin/tool/usertours/configure.php', [
 185                  'action'    => manager::ACTION_IMPORTTOUR,
 186              ]);
 187  
 188          return $link;
 189      }
 190  
 191      /**
 192       * Get the link used to export the tour.
 193       *
 194       * @param   int         $tourid     The ID of the tour to export.
 195       * @return  moodle_url              The URL.
 196       */
 197      public static function get_export_tour_link($tourid) {
 198          $link = new \moodle_url('/admin/tool/usertours/configure.php', [
 199                  'action'    => manager::ACTION_EXPORTTOUR,
 200                  'id'        => $tourid,
 201              ]);
 202  
 203          return $link;
 204      }
 205  
 206      /**
 207       * Get the link used to duplicate the tour.
 208       *
 209       * @param   int         $tourid     The ID of the tour to duplicate.
 210       * @return  moodle_url              The URL.
 211       */
 212      public static function get_duplicate_tour_link($tourid) {
 213          $link = new \moodle_url('/admin/tool/usertours/configure.php', [
 214                  'action'    => manager::ACTION_DUPLICATETOUR,
 215                  'id'        => $tourid,
 216          ]);
 217  
 218          return $link;
 219      }
 220  
 221      /**
 222       * Get the link used to delete the tour.
 223       *
 224       * @param   int         $tourid     The ID of the tour to delete.
 225       * @return  moodle_url              The URL.
 226       */
 227      public static function get_delete_tour_link($tourid) {
 228          return new \moodle_url('/admin/tool/usertours/configure.php', [
 229                  'id'        => $tourid,
 230                  'action'    => manager::ACTION_DELETETOUR,
 231                  'sesskey'   => sesskey(),
 232              ]);
 233      }
 234  
 235      /**
 236       * Get the link for listing tours.
 237       *
 238       * @return  moodle_url              The URL.
 239       */
 240      public static function get_list_tour_link() {
 241          $link = new \moodle_url('/admin/tool/usertours/configure.php');
 242          $link->param('action', manager::ACTION_LISTTOURS);
 243  
 244          return $link;
 245      }
 246  
 247      /**
 248       * Get a filler icon for display in the actions column of a table.
 249       *
 250       * @param   string      $url            The URL for the icon.
 251       * @param   string      $icon           The icon identifier.
 252       * @param   string      $alt            The alt text for the icon.
 253       * @param   string      $iconcomponent  The icon component.
 254       * @param   array       $options        Display options.
 255       * @return  string
 256       */
 257      public static function format_icon_link($url, $icon, $alt, $iconcomponent = 'moodle', $options = array()) {
 258          global $OUTPUT;
 259  
 260          return $OUTPUT->action_icon(
 261                  $url,
 262                  new \pix_icon($icon, $alt, $iconcomponent, [
 263                          'title' => $alt,
 264                      ]),
 265                  null,
 266                  $options
 267                  );
 268  
 269      }
 270  
 271      /**
 272       * Get a filler icon for display in the actions column of a table.
 273       *
 274       * @param   array       $options        Display options.
 275       * @return  string
 276       */
 277      public static function get_filler_icon($options = array()) {
 278          global $OUTPUT;
 279  
 280          return \html_writer::span(
 281              $OUTPUT->pix_icon('t/filler', '', 'tool_usertours', $options),
 282              'action-icon'
 283          );
 284      }
 285  
 286      /**
 287       * Get the link for deleting steps.
 288       *
 289       * @param   int         $stepid     The ID of the step to display.
 290       * @return  moodle_url              The URL.
 291       */
 292      public static function get_delete_step_link($stepid) {
 293          return new \moodle_url('/admin/tool/usertours/configure.php', [
 294                  'action'    => manager::ACTION_DELETESTEP,
 295                  'id'        => $stepid,
 296                  'sesskey'   => sesskey(),
 297              ]);
 298      }
 299  
 300      /**
 301       * Render the inplace editable used to edit the tour name.
 302       *
 303       * @param   tour        $tour       The tour to edit.
 304       * @return  string
 305       */
 306      public static function render_tourname_inplace_editable(tour $tour) {
 307          return new \core\output\inplace_editable(
 308                  'tool_usertours',
 309                  'tourname',
 310                  $tour->get_id(),
 311                  true,
 312                  \html_writer::link(
 313                      $tour->get_view_link(),
 314                      $tour->get_name()
 315                  ),
 316                  $tour->get_name()
 317              );
 318      }
 319  
 320      /**
 321       * Render the inplace editable used to edit the tour description.
 322       *
 323       * @param   tour        $tour       The tour to edit.
 324       * @return  string
 325       */
 326      public static function render_tourdescription_inplace_editable(tour $tour) {
 327          return new \core\output\inplace_editable(
 328                  'tool_usertours',
 329                  'tourdescription',
 330                  $tour->get_id(),
 331                  true,
 332                  $tour->get_description(),
 333                  $tour->get_description()
 334              );
 335      }
 336  
 337      /**
 338       * Render the inplace editable used to edit the tour enable state.
 339       *
 340       * @param   tour        $tour       The tour to edit.
 341       * @return  string
 342       */
 343      public static function render_tourenabled_inplace_editable(tour $tour) {
 344          global $OUTPUT;
 345  
 346          if ($tour->is_enabled()) {
 347              $icon = 't/hide';
 348              $alt = get_string('disable');
 349              $value = 1;
 350          } else {
 351              $icon = 't/show';
 352              $alt = get_string('enable');
 353              $value = 0;
 354          }
 355  
 356          $editable = new \core\output\inplace_editable(
 357                  'tool_usertours',
 358                  'tourenabled',
 359                  $tour->get_id(),
 360                  true,
 361                  $OUTPUT->pix_icon($icon, $alt, 'moodle', [
 362                          'title' => $alt,
 363                      ]),
 364                  $value
 365              );
 366  
 367          $editable->set_type_toggle();
 368          return $editable;
 369      }
 370  
 371      /**
 372       * Render the inplace editable used to edit the step name.
 373       *
 374       * @param   step        $step       The step to edit.
 375       * @return  string
 376       */
 377      public static function render_stepname_inplace_editable(step $step) {
 378          $title = format_text(step::get_string_from_input($step->get_title()), FORMAT_HTML);
 379  
 380          return new \core\output\inplace_editable(
 381                  'tool_usertours',
 382                  'stepname',
 383                  $step->get_id(),
 384                  true,
 385                  \html_writer::link(
 386                      $step->get_edit_link(),
 387                      $title
 388                  ),
 389                  $step->get_title()
 390              );
 391      }
 392  
 393      /**
 394       * Get all of the tours.
 395       *
 396       * @return  stdClass[]
 397       */
 398      public static function get_tours() {
 399          global $DB;
 400  
 401          $tours = $DB->get_records('tool_usertours_tours', array(), 'sortorder ASC');
 402          $return = [];
 403          foreach ($tours as $tour) {
 404              $return[$tour->id] = tour::load_from_record($tour);
 405          }
 406          return $return;
 407      }
 408  
 409      /**
 410       * Get the specified tour.
 411       *
 412       * @param   int         $tourid     The tour that the step belongs to.
 413       * @return  stdClass
 414       */
 415      public static function get_tour($tourid) {
 416          return tour::instance($tourid);
 417      }
 418  
 419      /**
 420       * Fetch the tour with the specified sortorder.
 421       *
 422       * @param   int         $sortorder  The sortorder of the tour.
 423       * @return  tour
 424       */
 425      public static function get_tour_from_sortorder($sortorder) {
 426          global $DB;
 427  
 428          $tour = $DB->get_record('tool_usertours_tours', array('sortorder' => $sortorder));
 429          return tour::load_from_record($tour);
 430      }
 431  
 432      /**
 433       * Return the count of all tours.
 434       *
 435       * @return  int
 436       */
 437      public static function count_tours() {
 438          global $DB;
 439  
 440          return $DB->count_records('tool_usertours_tours');
 441      }
 442  
 443      /**
 444       * Reset the sortorder for all tours.
 445       */
 446      public static function reset_tour_sortorder() {
 447          global $DB;
 448          $tours = $DB->get_records('tool_usertours_tours', null, 'sortorder ASC, pathmatch DESC', 'id, sortorder');
 449  
 450          $index = 0;
 451          foreach ($tours as $tour) {
 452              if ($tour->sortorder != $index) {
 453                  $DB->set_field('tool_usertours_tours', 'sortorder', $index, array('id' => $tour->id));
 454              }
 455              $index++;
 456          }
 457  
 458          // Notify the cache that a tour has changed.
 459          // Tours are only stored in the cache if there are steps.
 460          // If there step count has changed for some reason, this will change the potential cache results.
 461          cache::notify_tour_change();
 462      }
 463  
 464  
 465      /**
 466       * Get all of the steps in the tour.
 467       *
 468       * @param   int         $tourid     The tour that the step belongs to.
 469       * @return  stdClass[]
 470       */
 471      public static function get_steps($tourid) {
 472          $steps = cache::get_stepdata($tourid);
 473  
 474          $return = [];
 475          foreach ($steps as $step) {
 476              $return[$step->id] = step::load_from_record($step);
 477          }
 478          return $return;
 479      }
 480  
 481      /**
 482       * Fetch the specified step.
 483       *
 484       * @param   int         $stepid     The id of the step to fetch.
 485       * @return  step
 486       */
 487      public static function get_step($stepid) {
 488          return step::instance($stepid);
 489      }
 490  
 491      /**
 492       * Fetch the step with the specified sortorder.
 493       *
 494       * @param   int         $tourid     The tour that the step belongs to.
 495       * @param   int         $sortorder  The sortorder of the step.
 496       * @return  step
 497       */
 498      public static function get_step_from_sortorder($tourid, $sortorder) {
 499          global $DB;
 500  
 501          $step = $DB->get_record('tool_usertours_steps', array('tourid' => $tourid, 'sortorder' => $sortorder));
 502          return step::load_from_record($step);
 503      }
 504  
 505      /**
 506       * Handle addition of the tour into the current page.
 507       */
 508      public static function bootstrap() {
 509          global $PAGE;
 510  
 511          if (!isloggedin() || isguestuser()) {
 512              return;
 513          }
 514  
 515          if (in_array($PAGE->pagelayout, ['maintenance', 'print', 'redirect'])) {
 516              // Do not try to show user tours inside iframe, in maintenance mode,
 517              // when printing, or during redirects.
 518              return;
 519          }
 520  
 521          if (self::$bootstrapped) {
 522              return;
 523          }
 524          self::$bootstrapped = true;
 525  
 526          if ($tour = manager::get_current_tour()) {
 527              $PAGE->requires->js_call_amd('tool_usertours/usertours', 'init', [
 528                      $tour->get_id(),
 529                      $tour->should_show_for_user(),
 530                      $PAGE->context->id,
 531                  ]);
 532          }
 533      }
 534  
 535      /**
 536       * Add the reset link to the current page.
 537       */
 538      public static function bootstrap_reset() {
 539          if (manager::get_current_tour()) {
 540              echo \html_writer::link('', get_string('resettouronpage', 'tool_usertours'), [
 541                      'data-action'   => 'tool_usertours/resetpagetour',
 542                  ]);
 543          }
 544      }
 545  
 546      /**
 547       * Get a list of all possible filters.
 548       *
 549       * @return  array
 550       */
 551      public static function get_all_filters() {
 552          $filters = \core_component::get_component_classes_in_namespace('tool_usertours', 'local\filter');
 553          $filters = array_keys($filters);
 554  
 555          $filters = array_filter($filters, function($filterclass) {
 556              $rc = new \ReflectionClass($filterclass);
 557              return $rc->isInstantiable();
 558          });
 559  
 560          return $filters;
 561      }
 562  }