MDL-67814 core_h5p: implemented H5P Core/Editor library interfaces
[moodle.git] / h5p / classes / local / library / handler.php
blobca59674934be754591f38743ba9b37e5c228652c
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.
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/>.
17 /**
18 * Base class for library handlers.
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
25 namespace core_h5p\local\library;
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Base class for library handlers.
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.
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
39 abstract class handler {
41 /**
42 * Get the current version of the H5P core library.
44 * @return string
46 abstract public static function get_h5p_version(): string;
48 /**
49 * Get the base path for the H5P Libraries.
51 * @return null|string
53 public static function get_h5p_library_base(): ?string {
54 $h5pversion = static::get_h5p_version();
55 return "/h5p/h5plib/v{$h5pversion}/joubel";
58 /**
59 * Get the base path for the current H5P Core Library.
61 * @param string $filepath The path within the H5P root
62 * @return null|string
64 public static function get_h5p_core_library_base(?string $filepath = null): ?string {
65 return static::get_h5p_library_base() . "/core/{$filepath}";
68 /**
69 * Get the base path for the current H5P Editor Library.
71 * @param null|string $filepath The path within the H5P root.
72 * @return string Path to a file in the H5P Editor library.
74 public static function get_h5p_editor_library_base(?string $filepath = null): string {
75 return static::get_h5p_library_base() . "/editor/{$filepath}";
78 /**
79 * Register the H5P autoloader.
81 public static function register(): void {
82 spl_autoload_register([static::class, 'autoload']);
85 /**
86 * SPL Autoloading function for H5P.
88 * @param string $classname The name of the class to load
90 public static function autoload($classname): void {
91 global $CFG;
93 $classes = static::get_class_list();
95 if (isset($classes[$classname])) {
96 if (file_exists($CFG->dirroot . static::get_h5p_core_library_base($classes[$classname]))) {
97 require_once($CFG->dirroot . static::get_h5p_core_library_base($classes[$classname]));
98 } else {
99 require_once($CFG->dirroot . static::get_h5p_editor_library_base($classes[$classname]));
105 * Get a URL for the current H5P Core Library.
107 * @param string $filepath The path within the h5p root
108 * @param array $params these params override current params or add new
109 * @return null|\moodle_url
111 public static function get_h5p_core_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
112 return new \moodle_url(static::get_h5p_core_library_base($filepath), $params);
116 * Get a URL for the current H5P Editor Library.
118 * @param string $filepath The path within the h5p root.
119 * @param array $params These params override current params or add new.
120 * @return null|\moodle_url The moodle_url to a file in the H5P Editor library.
122 public static function get_h5p_editor_library_url(?string $filepath = null, ?array $params = null): ?\moodle_url {
123 return new \moodle_url(static::get_h5p_editor_library_base($filepath), $params);
127 * Return the list of classes with their location within the joubel directory.
129 * @return array
131 protected static function get_class_list(): array {
132 return [
133 'H5PCore' => 'h5p.classes.php',
134 'H5PFrameworkInterface' => 'h5p.classes.php',
135 'H5PContentValidator' => 'h5p.classes.php',
136 'H5PValidator' => 'h5p.classes.php',
137 'H5PStorage' => 'h5p.classes.php',
138 'H5PDevelopment' => 'h5p-development.class.php',
139 'H5PFileStorage' => 'h5p-file-storage.interface.php',
140 'H5PMetadata' => 'h5p-metadata.class.php',
141 'H5peditor' => 'h5peditor.class.php',
142 'H5peditorStorage' => 'h5peditor-storage.interface.php',
143 'H5PEditorAjaxInterface' => 'h5peditor-ajax.interface.php',
144 'H5PEditorAjax' => 'h5peditor-ajax.class.php',
145 'H5peditorFile' => 'h5peditor-file.class.php',