Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * H5P autoloader management class.
  19   *
  20   * @package    core_h5p
  21   * @copyright  2019 Sara Arjona <sara@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_h5p\local\library;
  26  
  27  /**
  28   * H5P autoloader management class.
  29   *
  30   * @package    core_h5p
  31   * @copyright  2019 Sara Arjona <sara@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class autoloader {
  35  
  36      /**
  37       * Returns the list of plugins that can work as H5P library handlers (have class PLUGINNAME\local\library\handler)
  38       * @return array with the format: pluginname => class
  39       */
  40      public static function get_all_handlers(): array {
  41          $handlers = [];
  42          $plugins = \core_component::get_plugin_list_with_class('h5plib', 'local\library\handler') +
  43              \core_component::get_plugin_list_with_class('h5plib', 'local_library_handler');
  44          // Allow plugins to have the class either with namespace or without (useful for unittest).
  45          foreach ($plugins as $pname => $class) {
  46              $handlers[$pname] = $class;
  47          }
  48  
  49          return $handlers;
  50      }
  51  
  52      /**
  53       * Returns the default H5P library handler class.
  54       *
  55       * @return string|null H5P library handler class
  56       */
  57      public static function get_default_handler(): ?string {
  58          $default = null;
  59          $handlers = self::get_all_handlers();
  60          if (!empty($handlers)) {
  61              // The default handler will be the first value in the list.
  62              $default = array_shift($handlers);
  63          }
  64  
  65          return $default;
  66      }
  67  
  68      /**
  69       * Returns the default H5P library handler.
  70       *
  71       * @return string|null H5P library handler
  72       */
  73      public static function get_default_handler_library(): ?string {
  74          $default = null;
  75          $handlers = self::get_all_handlers();
  76          if (!empty($handlers)) {
  77              // The default handler will be the first in the list.
  78              $keys = array_keys($handlers);
  79              $default = array_shift($keys);
  80          }
  81  
  82          return $default;
  83      }
  84  
  85      /**
  86       * Returns the current H5P library handler class.
  87       *
  88       * @return string H5P library handler class
  89       * @throws \moodle_exception
  90       */
  91      public static function get_handler_classname(): string {
  92          global $CFG;
  93  
  94          $handlers = self::get_all_handlers();
  95          if (!empty($CFG->h5plibraryhandler)) {
  96              if (isset($handlers[$CFG->h5plibraryhandler])) {
  97                  return $handlers[$CFG->h5plibraryhandler];
  98              }
  99          }
 100  
 101          // If no handler has been defined, return the default one.
 102          $defaulthandler = self::get_default_handler();
 103          if (empty($defaulthandler)) {
 104              // If there is no default handler, throw an exception.
 105              throw new \moodle_exception('noh5plibhandlerdefined', 'core_h5p');
 106          }
 107  
 108          return $defaulthandler;
 109      }
 110  
 111      /**
 112       * Get the current version of the H5P core library.
 113       *
 114       * @return string
 115       */
 116      public static function get_h5p_version(): string {
 117          return component_class_callback(self::get_handler_classname(), 'get_h5p_version', []);
 118      }
 119  
 120      /**
 121       * Get a URL for the current H5P Core Library.
 122       *
 123       * @param string $filepath The path within the h5p root
 124       * @param array $params these params override current params or add new
 125       * @return null|moodle_url
 126       */
 127      public static function get_h5p_core_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
 128          return component_class_callback(self::get_handler_classname(), 'get_h5p_core_library_url', [$filepath, $params]);
 129      }
 130  
 131      /**
 132       * Get a URL for the current H5P Editor Library.
 133       *
 134       * @param string $filepath The path within the h5p root.
 135       * @param array $params These params override current params or add new.
 136       * @return null|\moodle_url The moodle_url instance to a file in the H5P Editor library.
 137       */
 138      public static function get_h5p_editor_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
 139          return component_class_callback(self::get_handler_classname(), 'get_h5p_editor_library_url', [$filepath, $params]);
 140      }
 141  
 142      /**
 143       * Get the base path for the current H5P Editor Library.
 144       *
 145       * @param string $filepath The path within the h5p root.
 146       * @return string  Path to a file in the H5P Editor library.
 147       */
 148      public static function get_h5p_editor_library_base(?string $filepath = null): string {
 149          return component_class_callback(self::get_handler_classname(), 'get_h5p_editor_library_base', [$filepath]);
 150      }
 151  
 152      /**
 153       * Returns a localized string, if it exists in the h5plib plugin and the value it's different from the English version.
 154       *
 155       * @param string $identifier The key identifier for the localized string
 156       * @param string $language Language to get the localized string.
 157       * @return string|null The localized string or null if it doesn't exist in this H5P library plugin.
 158       */
 159      public static function get_h5p_string(string $identifier, string $language): ?string {
 160          return component_class_callback(self::get_handler_classname(), 'get_h5p_string', [$identifier, $language]);
 161      }
 162  
 163      /**
 164       * Register the H5P autoloader.
 165       */
 166      public static function register(): void {
 167          component_class_callback(self::get_handler_classname(), 'register', []);
 168      }
 169  }