Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

   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   * Base class for library handlers.
  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  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Base class for library handlers.
  31   *
  32   * If a new H5P libraries handler plugin has to be created, it has to define class
  33   * PLUGINNAME\local\library\handler that extends \core_h5p\local\library\handler.
  34   *
  35   * @package    core_h5p
  36   * @copyright  2019 Sara Arjona <sara@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  abstract class handler {
  40  
  41      /**
  42       * Get the current version of the H5P core library.
  43       *
  44       * @return string
  45       */
  46      abstract public static function get_h5p_version(): string;
  47  
  48      /**
  49       * Get the base path for the H5P Libraries.
  50       *
  51       * @return null|string
  52       */
  53      public static function get_h5p_library_base(): ?string {
  54          $h5pversion = static::get_h5p_version();
  55          return "/h5p/h5plib/v{$h5pversion}/joubel";
  56      }
  57  
  58      /**
  59       * Get the base path for the current H5P Core Library.
  60       *
  61       * @param string $filepath The path within the H5P root
  62       * @return null|string
  63       */
  64      public static function get_h5p_core_library_base(?string $filepath = null): ?string {
  65          return static::get_h5p_library_base() . "/core/{$filepath}";
  66      }
  67  
  68      /**
  69       * Get the base path for the current H5P Editor Library.
  70       *
  71       * @param null|string $filepath The path within the H5P root.
  72       * @return string Path to a file in the H5P Editor library.
  73       */
  74      public static function get_h5p_editor_library_base(?string $filepath = null): string {
  75          return static::get_h5p_library_base() . "/editor/{$filepath}";
  76      }
  77  
  78      /**
  79       * Register the H5P autoloader.
  80       */
  81      public static function register(): void {
  82          // Prepend H5P libraries in order to guarantee they are loaded first. Plugins using same libraries will need to use a
  83          // different namespace if they want to use a different version.
  84          spl_autoload_register([static::class, 'autoload'], true, true);
  85      }
  86  
  87      /**
  88       * SPL Autoloading function for H5P.
  89       *
  90       * @param string $classname The name of the class to load
  91       */
  92      public static function autoload($classname): void {
  93          global $CFG;
  94  
  95          $classes = static::get_class_list();
  96  
  97          if (isset($classes[$classname])) {
  98              if (file_exists($CFG->dirroot . static::get_h5p_core_library_base($classes[$classname]))) {
  99                  require_once($CFG->dirroot . static::get_h5p_core_library_base($classes[$classname]));
 100              } else {
 101                  require_once($CFG->dirroot . static::get_h5p_editor_library_base($classes[$classname]));
 102              }
 103          }
 104      }
 105  
 106      /**
 107       * Get a URL for the current H5P Core Library.
 108       *
 109       * @param string $filepath The path within the h5p root
 110       * @param array $params these params override current params or add new
 111       * @return null|\moodle_url
 112       */
 113      public static function get_h5p_core_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
 114          return new \moodle_url(static::get_h5p_core_library_base($filepath), $params);
 115      }
 116  
 117      /**
 118       * Get a URL for the current H5P Editor Library.
 119       *
 120       * @param string $filepath The path within the h5p root.
 121       * @param array $params These params override current params or add new.
 122       * @return null|\moodle_url The moodle_url to a file in the H5P Editor library.
 123       */
 124      public static function get_h5p_editor_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
 125          return new \moodle_url(static::get_h5p_editor_library_base($filepath), $params);
 126      }
 127  
 128      /**
 129       * Returns a localized string, if it exists in the h5plib plugin and the value it's different from the English version.
 130       *
 131       * @param string $identifier The key identifier for the localized string
 132       * @param string $language Language to get the localized string.
 133       * @return string|null The localized string or null if it doesn't exist in this H5P library plugin.
 134       */
 135      public static function get_h5p_string(string $identifier, string $language): ?string {
 136          $value = null;
 137          $h5pversion = static::get_h5p_version();
 138          $component = 'h5plib_v' . $h5pversion;
 139          // Composed code languages, such as 'Spanish, Mexican' are different in H5P and Moodle:
 140          // - In H5P, they use '-' to separate language from the country. For instance: es-mx.
 141          // - However, in Moodle, they have '_' instead of '-'. For instance: es_mx.
 142          $language = str_replace('-', '_', $language);
 143          if (get_string_manager()->string_exists($identifier, $component)) {
 144              $defaultmoodlelang = 'en';
 145              // In Moodle, all the English strings always will exist because they have to be declared in order to let users
 146              // to translate them. That's why, this method will only replace existing key if the value is different from
 147              // the English version and the current language is not English.
 148              $string = new \lang_string($identifier, $component);
 149              if ($language === $defaultmoodlelang || $string->out($language) !== $string->out($defaultmoodlelang)) {
 150                  $value = $string->out($language);
 151              }
 152          }
 153  
 154          return $value;
 155      }
 156  
 157      /**
 158       * Return the list of classes with their location within the joubel directory.
 159       *
 160       * @return array
 161       */
 162      protected static function get_class_list(): array {
 163          return [
 164              'Moodle\H5PCore' => 'h5p.classes.php',
 165              'Moodle\H5PFrameworkInterface' => 'h5p.classes.php',
 166              'Moodle\H5PContentValidator' => 'h5p.classes.php',
 167              'Moodle\H5PValidator' => 'h5p.classes.php',
 168              'Moodle\H5PStorage' => 'h5p.classes.php',
 169              'Moodle\H5PDevelopment' => 'h5p-development.class.php',
 170              'Moodle\H5PFileStorage' => 'h5p-file-storage.interface.php',
 171              'Moodle\H5PDefaultStorage' => 'h5p-default-storage.class.php',
 172              'Moodle\H5PMetadata' => 'h5p-metadata.class.php',
 173              'Moodle\H5peditor' => 'h5peditor.class.php',
 174              'Moodle\H5peditorStorage' => 'h5peditor-storage.interface.php',
 175              'Moodle\H5PEditorAjaxInterface' => 'h5peditor-ajax.interface.php',
 176              'Moodle\H5PEditorAjax' => 'h5peditor-ajax.class.php',
 177              'Moodle\H5peditorFile' => 'h5peditor-file.class.php',
 178          ];
 179      }
 180  }