Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

Contains API class for the H5P area.

Copyright: 2020 Sara Arjona <sara@moodle.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 829 lines (36 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: api  - X-Ref

Contains API class for the H5P area.

delete_library(factory $factory, \stdClass $library)   X-Ref
Delete a library and also all the libraries depending on it and the H5P contents using it. For the H5P content, only the
database entries in {h5p} are removed (the .h5p files are not removed in order to let users to deploy them again).

param: factory   $factory The H5P factory.
param: \stdClass $library The library to delete.

get_dependent_libraries(int $libraryid)   X-Ref
Get all the libraries using a defined library.

return: array  List of libraryid with all the libraries required by a defined library.
param: int    $libraryid The library to get its dependencies.

get_library(int $libraryid)   X-Ref
Get a library from an identifier.

return: \stdClass The library object having the library identifier defined.
param: int    $libraryid The library identifier.

get_library_details(array $params, bool $configurable, string $fields = '')   X-Ref
Returns a library as an object with properties that correspond to the fetched row's field names.

return: \stdClass|null An object with one attribute for each field name in $fields param.
param: array $params An associative array with the values of the machinename, majorversion and minorversion fields.
param: bool $configurable A library that has semantics so it can be configured in the editor.
param: string $fields Library attributes to retrieve.

get_contenttype_libraries(?string $fields = '')   X-Ref
Get all the H5P content type libraries versions.

return: array An array with an object for each content type library installed.
param: string|null $fields Library fields to return.

get_content_from_pluginfile_url(string $url, bool $preventredirect = true,bool $skipcapcheck = false)   X-Ref
Get the H5P DB instance id for a H5P pluginfile URL. If it doesn't exist, it's not created.

return: array of [file, stdClass|false]:
param: string $url H5P pluginfile URL.
param: bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
param: bool $skipcapcheck Whether capabilities should be checked or not to get the pluginfile URL because sometimes they

get_original_content_from_pluginfile_url(string $url, bool $preventredirect = true,bool $skipcapcheck = false)   X-Ref
Get the original file and H5P DB instance for a given H5P pluginfile URL. If it doesn't exist, it's not created.
If the file has been added as a reference, this method will return the original linked file.

return: array of [\stored_file|false, \stdClass|false, \stored_file|false]:
param: string $url H5P pluginfile URL.
param: bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions.
param: bool $skipcapcheck Whether capabilities should be checked or not to get the pluginfile URL because sometimes they

can_edit_content(\stored_file $file)   X-Ref
Check if the user can edit an H5P file. It will return true in the following situations:
- The user is the author of the file.
- The component is different from user (i.e. private files).
- If the component is contentbank, the user can edit this file (calling the ContentBank API).
- If the component is mod_xxx or block_xxx, the user has the addinstance capability.
- If the component implements the can_edit_content in the h5p\canedit class and the callback to this method returns true.

return: boolean Whether the user can edit or not the given file.
param: \stored_file $file The H5P file to check.

create_content_from_pluginfile_url(string $url, \stdClass $config, factory $factory,\stdClass &$messages, bool $preventredirect = true, bool $skipcapcheck = false)   X-Ref
Create, if it doesn't exist, the H5P DB instance id for a H5P pluginfile URL. If it exists:
- If the content is not the same, remove the existing content and re-deploy the H5P content again.
- If the content is the same, returns the H5P identifier.

return: array of [file, h5pid]:
param: string $url H5P pluginfile URL.
param: stdClass $config Configuration for H5P buttons.
param: factory $factory The \core_h5p\factory object
param: stdClass $messages The error, exception and info messages, raised while preparing and running an H5P content.
param: bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions
param: bool $skipcapcheck Whether capabilities should be checked or not to get the pluginfile URL because sometimes they

delete_content(\stdClass $content, factory $factory)   X-Ref
Delete an H5P package.

param: stdClass $content The H5P package to delete with, at least content['id].
param: factory $factory The \core_h5p\factory object

delete_content_from_pluginfile_url(string $url, factory $factory)   X-Ref
Delete an H5P package deployed from the defined $url.

param: string $url pluginfile URL of the H5P package to delete.
param: factory $factory The \core_h5p\factory object

can_access_pluginfile_hash(string $url, bool $preventredirect = true)   X-Ref
If user can access pathnamehash from an H5P internal URL.

return: bool if user can access pluginfile hash.
param: string $url H5P pluginfile URL poiting to an H5P file.
param: bool $preventredirect Set to true in scripts that can not redirect (CLI, RSS feeds, etc.), throws exceptions

get_pluginfile_hash(string $url)   X-Ref
Get the pathnamehash from an H5P internal URL.

return: string|false pathnamehash for the file in the internal URL.
param: string $url H5P pluginfile URL poiting to an H5P file.

get_content_from_pathnamehash(string $pathnamehash)   X-Ref
Returns the H5P content object corresponding to an H5P content file.

return: null|\stdClass H5P content object or null if not found.
param: string $pathnamehash The pathnamehash of the file associated to an H5P content.

get_export_info_from_context_id(int $contextid,factory $factory,string $component,string $filearea)   X-Ref
Return the H5P export information file when the file has been deployed.
Otherwise, return null if H5P file:
i) has not been deployed.
ii) has changed the content.

The information returned will be:
- filename, filepath, mimetype, filesize, timemodified and fileurl.

return: array|null Return file info otherwise null.
param: int $contextid ContextId of the H5P activity.
param: factory $factory The \core_h5p\factory object.
param: string $component component
param: string $filearea file area

set_library_enabled(int $libraryid, bool $isenabled)   X-Ref
Enable or disable a library.

param: int $libraryid The id of the library to enable/disable.
param: bool $isenabled True if the library should be enabled; false otherwise.

is_library_enabled(\stdClass $librarydata)   X-Ref
Check whether a library is enabled or not. When machinename is passed, it will return false if any of the versions
for this machinename is disabled.
If the library doesn't exist, it will return true.

return: bool
param: \stdClass $librarydata Supported fields for library: 'id' and 'machichename'.

is_valid_package(\stored_file $file, bool $onlyupdatelibs, bool $skipcontent = false,?factory $factory = null, bool $deletefiletree = true)   X-Ref
Check whether an H5P package is valid or not.

return: bool True if the H5P file is valid (expected format, valid libraries...); false otherwise.
param: \stored_file $file The file with the H5P content.
param: bool $onlyupdatelibs Whether new libraries can be installed or only the existing ones can be updated
param: bool $skipcontent Should the content be skipped (so only the libraries will be saved)?
param: factory|null $factory The \core_h5p\factory object
param: bool $deletefiletree Should the temporary files be deleted before returning?