Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 core 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; 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 require_once("$CFG->libdir/filelib.php"); 30 31 use Moodle\H5PCore; 32 use Moodle\H5PFrameworkInterface; 33 use Moodle\H5PHubEndpoints; 34 use stdClass; 35 use moodle_url; 36 use core_h5p\local\library\autoloader; 37 38 /** 39 * H5P core class, containing functions and storage shared by the other H5P classes. 40 * 41 * @package core_h5p 42 * @copyright 2019 Sara Arjona <sara@moodle.com> 43 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 44 */ 45 class core extends H5PCore { 46 47 /** @var array The array containing all the present libraries */ 48 protected $libraries; 49 50 /** 51 * Constructor for core_h5p/core. 52 * 53 * @param H5PFrameworkInterface $framework The frameworks implementation of the H5PFrameworkInterface 54 * @param string|H5PFileStorage $path The H5P file storage directory or class 55 * @param string $url The URL to the file storage directory 56 * @param string $language The language code. Defaults to english 57 * @param boolean $export Whether export is enabled 58 */ 59 public function __construct(H5PFrameworkInterface $framework, $path, string $url, string $language = 'en', 60 bool $export = false) { 61 62 parent::__construct($framework, $path, $url, $language, $export); 63 64 // Aggregate the assets by default. 65 $this->aggregateAssets = true; 66 } 67 68 /** 69 * Get the path to the dependency. 70 * 71 * @param array $dependency An array containing the information of the requested dependency library 72 * @return string The path to the dependency library 73 */ 74 protected function getDependencyPath(array $dependency): string { 75 $library = $this->find_library($dependency); 76 77 return "libraries/{$library->id}/{$library->machinename}-{$library->majorversion}.{$library->minorversion}"; 78 } 79 80 /** 81 * Get the paths to the content dependencies. 82 * 83 * @param int $id The H5P content ID 84 * @return array An array containing the path of each content dependency 85 */ 86 public function get_dependency_roots(int $id): array { 87 $roots = []; 88 $dependencies = $this->h5pF->loadContentDependencies($id); 89 $context = \context_system::instance(); 90 foreach ($dependencies as $dependency) { 91 $library = $this->find_library($dependency); 92 $roots[self::libraryToString($dependency, true)] = (moodle_url::make_pluginfile_url( 93 $context->id, 94 'core_h5p', 95 'libraries', 96 $library->id, 97 "/" . self::libraryToString($dependency, true), 98 '' 99 ))->out(false); 100 } 101 102 return $roots; 103 } 104 105 /** 106 * Get a particular dependency library. 107 * 108 * @param array $dependency An array containing information of the dependency library 109 * @return stdClass|null The library object if the library dependency exists, null otherwise 110 */ 111 protected function find_library(array $dependency): ?\stdClass { 112 global $DB; 113 if (null === $this->libraries) { 114 $this->libraries = $DB->get_records('h5p_libraries'); 115 } 116 117 $major = $dependency['majorVersion']; 118 $minor = $dependency['minorVersion']; 119 $patch = $dependency['patchVersion']; 120 121 foreach ($this->libraries as $library) { 122 if ($library->machinename !== $dependency['machineName']) { 123 continue; 124 } 125 126 if ($library->majorversion != $major) { 127 continue; 128 } 129 if ($library->minorversion != $minor) { 130 continue; 131 } 132 if ($library->patchversion != $patch) { 133 continue; 134 } 135 136 return $library; 137 } 138 139 return null; 140 } 141 142 /** 143 * Get the list of JS scripts to include on the page. 144 * 145 * @return array The array containg urls of the core JavaScript files 146 */ 147 public static function get_scripts(): array { 148 global $PAGE; 149 150 $jsrev = $PAGE->requires->get_jsrev(); 151 $urls = []; 152 foreach (self::$scripts as $script) { 153 $urls[] = autoloader::get_h5p_core_library_url($script, [ 154 'ver' => $jsrev, 155 ]); 156 } 157 $urls[] = new moodle_url("/h5p/js/h5p_overrides.js", [ 158 'ver' => $jsrev, 159 ]); 160 161 return $urls; 162 } 163 164 /** 165 * Fetch and install the latest H5P content types libraries from the official H5P repository. 166 * If the latest version of a content type library is present in the system, nothing is done for that content type. 167 * 168 * @return stdClass 169 */ 170 public function fetch_latest_content_types(): ?\stdClass { 171 172 $contenttypes = $this->get_latest_content_types(); 173 if (!empty($contenttypes->error)) { 174 return $contenttypes; 175 } 176 177 $typesinstalled = []; 178 179 $factory = new factory(); 180 $framework = $factory->get_framework(); 181 182 foreach ($contenttypes->contentTypes as $type) { 183 // Don't fetch content types if any of the versions is disabled. 184 $librarydata = (object) ['machinename' => $type->id]; 185 if (!api::is_library_enabled($librarydata)) { 186 continue; 187 } 188 // Don't fetch content types that require a higher H5P core API version. 189 if (!$this->is_required_core_api($type->coreApiVersionNeeded)) { 190 continue; 191 } 192 193 $library = [ 194 'machineName' => $type->id, 195 'majorVersion' => $type->version->major, 196 'minorVersion' => $type->version->minor, 197 'patchVersion' => $type->version->patch, 198 ]; 199 // Add example and tutorial to the library, to store this information too. 200 if (isset($type->example)) { 201 $library['example'] = $type->example; 202 } 203 if (isset($type->tutorial)) { 204 $library['tutorial'] = $type->tutorial; 205 } 206 207 $shoulddownload = true; 208 if ($framework->getLibraryId($type->id, $type->version->major, $type->version->minor)) { 209 if (!$framework->isPatchedLibrary($library)) { 210 $shoulddownload = false; 211 } 212 } 213 214 if ($shoulddownload) { 215 $installed['id'] = $this->fetch_content_type($library); 216 if ($installed['id']) { 217 $installed['name'] = H5PCore::libraryToString($library); 218 $typesinstalled[] = $installed; 219 } 220 } 221 } 222 223 $result = new stdClass(); 224 $result->error = ''; 225 $result->typesinstalled = $typesinstalled; 226 227 return $result; 228 } 229 230 /** 231 * Given an H5P content type machine name, fetch and install the required library from the official H5P repository. 232 * 233 * @param array $library Library machineName, majorversion and minorversion. 234 * @return int|null Returns the id of the content type library installed, null otherwise. 235 */ 236 public function fetch_content_type(array $library): ?int { 237 global $DB; 238 239 $factory = new factory(); 240 241 $fs = get_file_storage(); 242 243 // Delete any existing file, if it was not deleted during a previous download. 244 $existing = $fs->get_file( 245 (\context_system::instance())->id, 246 'core_h5p', 247 'library_sources', 248 0, 249 '/', 250 $library['machineName'] 251 ); 252 if ($existing) { 253 $existing->delete(); 254 } 255 256 // Download the latest content type from the H5P official repository. 257 $file = $fs->create_file_from_url( 258 (object) [ 259 'component' => 'core_h5p', 260 'filearea' => 'library_sources', 261 'itemid' => 0, 262 'contextid' => (\context_system::instance())->id, 263 'filepath' => '/', 264 'filename' => $library['machineName'], 265 ], 266 $this->get_api_endpoint($library['machineName']), 267 null, 268 true 269 ); 270 271 if (!$file) { 272 return null; 273 } 274 275 helper::save_h5p($factory, $file, (object) [], false, true); 276 277 $file->delete(); 278 279 $librarykey = static::libraryToString($library); 280 281 $libraryjson = $factory->get_storage()->h5pC->librariesJsonData[$librarykey]; 282 if (!array_key_exists('libraryId', $libraryjson)) { 283 return null; 284 } 285 286 $libraryid = $libraryjson['libraryId']; 287 288 // Update example and tutorial (if any of them are defined in $library). 289 $params = ['id' => $libraryid]; 290 if (array_key_exists('example', $library)) { 291 $params['example'] = $library['example']; 292 } 293 if (array_key_exists('tutorial', $library)) { 294 $params['tutorial'] = $library['tutorial']; 295 } 296 if (count($params) > 1) { 297 $DB->update_record('h5p_libraries', $params); 298 } 299 300 return $libraryid; 301 } 302 303 /** 304 * Get H5P endpoints. 305 * 306 * If $endpoint = 'content' and $library is null, moodle_url is the endpoint of the latest version of the H5P content 307 * types; however, if $library is the machine name of a content type, moodle_url is the endpoint to download the content type. 308 * The SITES endpoint ($endpoint = 'site') may be use to get a site UUID or send site data. 309 * 310 * @param string|null $library The machineName of the library whose endpoint is requested. 311 * @param string $endpoint The endpoint required. Valid values: "site", "content". 312 * @return moodle_url The endpoint moodle_url object. 313 */ 314 public function get_api_endpoint(?string $library = null, string $endpoint = 'content'): moodle_url { 315 if ($endpoint == 'site') { 316 $h5purl = H5PHubEndpoints::createURL(H5PHubEndpoints::SITES ); 317 } else if ($endpoint == 'content') { 318 $h5purl = H5PHubEndpoints::createURL(H5PHubEndpoints::CONTENT_TYPES ) . $library; 319 } 320 321 return new moodle_url($h5purl); 322 } 323 324 /** 325 * Get the latest version of the H5P content types available in the official repository. 326 * 327 * @return stdClass An object with 2 properties: 328 * - string error: error message when there is any problem, empty otherwise 329 * - array contentTypes: an object for each H5P content type with its information 330 */ 331 public function get_latest_content_types(): \stdClass { 332 global $CFG; 333 334 $siteuuid = $this->get_site_uuid() ?? md5($CFG->wwwroot); 335 $postdata = ['uuid' => $siteuuid]; 336 337 // Get the latest content-types json. 338 $endpoint = $this->get_api_endpoint(); 339 $request = download_file_content($endpoint, null, $postdata, true); 340 341 if (!empty($request->error) || $request->status != '200' || empty($request->results)) { 342 if (empty($request->error)) { 343 $request->error = get_string('fetchtypesfailure', 'core_h5p'); 344 } 345 return $request; 346 } 347 348 $contenttypes = json_decode($request->results); 349 $contenttypes->error = ''; 350 351 return $contenttypes; 352 } 353 354 /** 355 * Get the site UUID. If site UUID is not defined, try to register the site. 356 * 357 * return $string The site UUID, null if it is not set. 358 */ 359 public function get_site_uuid(): ?string { 360 // Check if the site_uuid is already set. 361 $siteuuid = get_config('core_h5p', 'site_uuid'); 362 363 if (!$siteuuid) { 364 $siteuuid = $this->register_site(); 365 } 366 367 return $siteuuid; 368 } 369 370 /** 371 * Get H5P generated site UUID. 372 * 373 * return ?string Returns H5P generated site UUID, null if can't get it. 374 */ 375 private function register_site(): ?string { 376 $endpoint = $this->get_api_endpoint(null, 'site'); 377 $siteuuid = download_file_content($endpoint, null, ''); 378 379 // Successful UUID retrieval from H5P. 380 if ($siteuuid) { 381 $json = json_decode($siteuuid); 382 if (isset($json->uuid)) { 383 set_config('site_uuid', $json->uuid, 'core_h5p'); 384 return $json->uuid; 385 } 386 } 387 388 return null; 389 } 390 391 /** 392 * Checks that the required H5P core API version or higher is installed. 393 * 394 * @param stdClass $coreapi Object with properties major and minor for the core API version required. 395 * @return bool True if the required H5P core API version is installed. False if not. 396 */ 397 public function is_required_core_api($coreapi): bool { 398 if (isset($coreapi) && !empty($coreapi)) { 399 if (($coreapi->major > H5PCore::$coreApi['majorVersion']) || 400 (($coreapi->major == H5PCore::$coreApi['majorVersion']) && ($coreapi->minor > H5PCore::$coreApi['minorVersion']))) { 401 return false; 402 } 403 } 404 return true; 405 } 406 407 /** 408 * Get the library string from a DB library record. 409 * 410 * @param stdClass $record The DB library record. 411 * @param bool $foldername If true, use hyphen instead of space in returned string. 412 * @return string The string name on the form {machineName} {majorVersion}.{minorVersion}. 413 */ 414 public static function record_to_string(stdClass $record, bool $foldername = false): string { 415 return static::libraryToString([ 416 'machineName' => $record->machinename, 417 'majorVersion' => $record->majorversion, 418 'minorVersion' => $record->minorversion, 419 ], $foldername); 420 } 421 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body