Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * Target class.
  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   * Target class.
  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 target {
  36  
  37      /**
  38       * @var TARGET_SELECTOR The target is a CSS selector.
  39       */
  40      const TARGET_SELECTOR = 0;
  41  
  42      /**
  43       * @var TARGET_BLOCK The target is a block.
  44       */
  45      const TARGET_BLOCK = 1;
  46  
  47      /**
  48       * @var TARGET_UNATTACHED The target is unattached to any specific node.
  49       */
  50      const TARGET_UNATTACHED = 2;
  51  
  52      /**
  53       * @var     array   $mapping    The list of target type to target name.
  54       */
  55      protected static $mapping = [
  56          self::TARGET_BLOCK      => 'block',
  57          self::TARGET_SELECTOR   => 'selector',
  58          self::TARGET_UNATTACHED => 'unattached',
  59      ];
  60  
  61      /**
  62       * Return the name of the class for this target type.
  63       *
  64       * @param   int     $type       The type of target.
  65       * @return  string              The class name.
  66       */
  67      public static function get_classname($type) {
  68          $targettype = self::$mapping[self::get_target_constant($type)];
  69          return "\\tool_usertours\\local\\target\\{$targettype}";
  70      }
  71  
  72      /**
  73       * Return the instance of the class for this target.
  74       *
  75       * @param   step    $step       The step.
  76       * @return  target              The target instance.
  77       */
  78      public static function get_target_type(step $step) {
  79          if (!isset(self::$mapping[$step->get_targettype()])) {
  80              throw new \moodle_exception('Unknown Target type');
  81          }
  82  
  83          $targettype = self::$mapping[$step->get_targettype()];
  84          return "\\tool_usertours\\local\\target\\{$targettype}";
  85      }
  86  
  87      /**
  88       * Return the constant used to describe this target.
  89       *
  90       * @param   string  $type       The type of the target.
  91       * @return  int                 The constant for this target.
  92       */
  93      public static function get_target_constant($type) {
  94          return array_search($type, self::$mapping);
  95      }
  96  
  97      /**
  98       * Return the constant used to describe this class.
  99       *
 100       * @param   string  $classname  The fully-qualified class name of the target
 101       * @return  int                 The constant for this target.
 102       */
 103      public static function get_target_constant_for_class($classname) {
 104          $rc = new \ReflectionClass($classname);
 105  
 106          return self::get_target_constant($rc->getShortName());
 107      }
 108  
 109      /**
 110       * Return the instance of the class for this target.
 111       *
 112       * @param   step    $step       The step.
 113       * @return  target              The target instance.
 114       */
 115      public static function get_target_instance(step $step) {
 116          $targetclass = self::get_target_type($step);
 117          return new $targetclass($step);
 118      }
 119  
 120      /**
 121       * Return the complete lits of target types.
 122       *
 123       * @return  array
 124       */
 125      public static function get_target_types() {
 126          return self::$mapping;
 127      }
 128  }