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 39 and 400] [Versions 400 and 402] [Versions 400 and 403]

Class \core_h5p\file_storage.

Copyright: 2019 Victor Deniz <victor@moodle.com>, base on code by Joubel AS
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 873 lines (30 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: file_storage  - X-Ref

Class to handle storage and export of H5P Content.

__construct()   X-Ref
Initial setup for file_storage.


saveLibrary($library)   X-Ref
Stores a H5P library in the Moodle filesystem.

param: array $library Library properties.

saveContent($source, $content)   X-Ref
Store the content folder.

param: string $source Path on file system to content directory.
param: array $content Content properties

deleteContent($content)   X-Ref
Remove content folder.

param: array $content Content properties

cloneContent($id, $newid)   X-Ref
Creates a stored copy of the content folder.

param: string $id Identifier of content to clone.
param: int $newid The cloned content's identifier

getTmpPath()   X-Ref
Get path to a new unique tmp folder.
Please note this needs to not be a directory.

return: string Path

exportContent($id, $target)   X-Ref
Fetch content folder and save in target directory.

param: int $id Content identifier
param: string $target Where the content folder will be saved

exportLibrary($library, $target)   X-Ref
Fetch library folder and save in target directory.

param: array $library Library properties
param: string $target Where the library folder will be saved

saveExport($source, $filename)   X-Ref
Save export in file system

param: string $source Path on file system to temporary export file.
param: string $filename Name of export file.

deleteExport($filename)   X-Ref
Removes given export file

param: string $filename filename of the export to delete.

hasExport($filename)   X-Ref
Check if the given export file exists

return: boolean True if the export file exists.
param: string $filename The export file to check.

cacheAssets(&$files, $key)   X-Ref
Will concatenate all JavaScrips and Stylesheets into two files in order
to improve page performance.

param: array $files A set of all the assets required for content to display
param: string $key Hashed key for cached asset

getCachedAssets($key)   X-Ref
Will check if there are cache assets available for content.

return: array
param: string $key Hashed key for cached asset

deleteCachedAssets($keys)   X-Ref
Remove the aggregated cache files.

param: array $keys The hash keys of removed files

getContent($filepath)   X-Ref
Read file content of given file and then return it.

return: string contents
param: string $filepath

saveFile($file, $contentid)   X-Ref
Save files uploaded through the editor.

return: int The id of the saved file.
param: H5peditorFile $file
param: int $contentid

cloneContentFile($file, $fromid, $tocontent)   X-Ref
Copy a file from another content or editor tmp dir.
Used when copy pasting content in H5P.

return: void
param: string $file path + name
param: string|int $fromid Content ID or 'editor' string
param: \stdClass $tocontent Target Content

moveContentDirectory($source, $contentid = null)   X-Ref
Copy content from one directory to another.
Defaults to cloning content from the current temporary upload folder to the editor path.

param: string $source path to source directory
param: string $contentid Id of content

get_icon_url(int $itemid, string $machinename, int $majorversion, int $minorversion)   X-Ref
Get the file URL or given library and then return it.

return: string url or false if the file doesn't exist
param: int $itemid
param: string $machinename
param: int $majorversion
param: int $minorversion

getContentFile($file, $content)   X-Ref
Checks to see if an H5P content has the given file.

return: int|null File ID or NULL if not found
param: string $file File path and name.
param: int $content Content id.

removeContentFile($file, $contentid)   X-Ref
Remove content files that are no longer used.

Used when saving content.

return: void
param: string $file File path and name.
param: int $contentid Content id.

hasWriteAccess()   X-Ref
Check if server setup has write permission to
the required folders

return: bool True if server has the proper write access

hasPresave($libraryname, $developmentpath = null)   X-Ref
Check if the library has a presave.js in the root folder

return: bool
param: string $libraryname
param: string $developmentpath

getUpgradeScript($machinename, $majorversion, $minorversion)   X-Ref
Check if upgrades script exist for library.

return: string Relative path
param: string $machinename
param: int $majorversion
param: int $minorversion

saveFileFromZip($path, $file, $stream)   X-Ref
Store the given stream into the given file.

return: bool|int
param: string $path
param: string $file
param: resource $stream

delete_library(array $library)   X-Ref
Deletes a library from the file system.

param: array $library Library details

delete_directory(int $contextid, string $component, string $filearea, int $itemid)   X-Ref
Remove an H5P directory from the filesystem.

param: int $contextid context ID
param: string $component component
param: string $filearea file area or all areas in context if not specified
param: int $itemid item ID or all files if not specified

copy_directory(string $source, array $options)   X-Ref
Copy an H5P directory from the temporary directory into the file system.

param: string $source  Temporary location for files.
param: array  $options File system information.

export_file_tree(string $target, int $contextid, string $filearea, string $filepath, int $itemid = 0)   X-Ref
Copies files from storage to temporary folder.

param: string $target Path to temporary folder
param: int $contextid context where the files are found
param: string $filearea file area
param: string $filepath file path
param: int $itemid Optional item ID

concatenate_files(array $assets, string $type, \context $context)   X-Ref
Adds all files of a type into one file.

return: string All of the file content in one string.
param: array    $assets  A list of files.
param: string   $type    The type of files in assets. Either 'scripts' or 'styles'
param: \context $context Context

get_export_file(string $filename)   X-Ref
No description

get_file_elements_from_filepath(string $filepath)   X-Ref
Converts a relative system file path into Moodle File API elements.

return: array File information.
param: string $filepath The system filepath to get information from.

get_itemid_for_file(string $filearea, string $filepath, string $filename)   X-Ref
Returns the item id given the other necessary variables.

return: mixed the specified value false if not found.
param: string $filearea The file area.
param: string $filepath The file path.
param: string $filename The file name.

get_file(string $filearea, int $itemid, string $file)   X-Ref
Helper to make it easy to load content files.

return: stored_file|null
param: string $filearea File area where the file is saved.
param: int $itemid Content instance or content id.
param: string $file File path and name.

move_file(string $sourcefile, int $contentid)   X-Ref
Move a single file

return: void
param: string $sourcefile Path to source file
param: int $contentid Content id or 0 if the file is in the editor file area