2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Class \core_h5p\editor_framework
21 * @copyright 2020 Victor Deniz <victor@moodle.com>, base on code by Joubel AS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 use Moodle\H5peditorStorage
;
31 * Moodle's implementation of the H5P Editor storage interface.
33 * Makes it possible for the editor's core library to communicate with the
34 * database used by Moodle.
37 * @copyright 2020 Victor Deniz <victor@moodle.com>, base on code by Joubel AS
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class editor_framework
implements H5peditorStorage
{
43 * Retrieve library language file from file storage. Note that parent languages will also be checked until a matching
44 * record is found (e.g. "de_kids" -> "de_du" -> "de")
50 * @return stdClass|bool Translation record if available, false otherwise
52 private function get_language_record(string $name, int $major, int $minor, string $lang) {
56 file_storage
::COMPONENT
,
57 file_storage
::LIBRARY_FILEAREA
,
59 $sqllike = $DB->sql_like('f.filepath', '?');
60 $params[] = '%language%';
62 $sql = "SELECT hl.id, f.pathnamehash
63 FROM {h5p_libraries} hl
65 ON hl.id = f.itemid AND f.component = ? AND f.filearea = ? AND $sqllike
66 WHERE ((hl.machinename = ? AND hl.majorversion = ? AND hl.minorversion = ?)
68 ORDER BY hl.patchversion DESC";
73 $params[] = $lang.'.json';
75 // Add translations, based initially on the given H5P language code. If missing then recurse language dependencies
76 // until we find a matching H5P language file.
77 $result = $DB->get_record_sql($sql, $params);
78 if ($result === false) {
80 // Normalise Moodle language using underscore, as opposed to H5P which uses dash.
81 $moodlelanguage = str_replace('-', '_', $lang);
83 $dependencies = get_string_manager()->get_language_dependencies($moodlelanguage);
85 // If current language has a dependency, then request it.
86 if (count($dependencies) > 1) {
87 $parentlanguage = get_html_lang_attribute_value($dependencies[count($dependencies) - 2]);
88 $result = $this->get_language_record($name, $major, $minor, $parentlanguage);
96 * Load language file(JSON).
97 * Used to translate the editor fields(title, description etc.)
99 * @param string $name The machine readable name of the library(content type)
100 * @param int $major Major part of version number
101 * @param int $minor Minor part of version number
102 * @param string $lang Language code
104 * @return string|boolean Translation in JSON format if available, false otherwise
106 public function getLanguage($name, $major, $minor, $lang) {
108 // Check if this information has been saved previously into the cache.
109 $langcache = \cache
::make('core', 'h5p_content_type_translations');
110 $library = new stdClass();
111 $library->machinename
= $name;
112 $library->majorversion
= $major;
113 $library->minorversion
= $minor;
114 $librarykey = helper
::get_cache_librarykey(core
::record_to_string($library));
115 $cachekey = "{$librarykey}/{$lang}";
116 $translation = $langcache->get($cachekey);
118 if ($translation !== false) {
119 // When there is no translation we store it in the cache as `null`.
120 // This API requires it be returned as `false`.
121 if ($translation === null) {
128 // Get the language file for this library.
129 $result = $this->get_language_record($name, $major, $minor, $lang);
130 if (empty($result)) {
131 // Save the fact that there is no translation into the cache.
132 // The cache API cannot handle setting a literal `false` value so conver to `null` instead.
133 $langcache->set($cachekey, null);
138 // Save translation into the cache, and return its content.
139 $fs = get_file_storage();
140 $file = $fs->get_file_by_hash($result->pathnamehash
);
141 $translation = $file->get_content();
143 $langcache->set($cachekey, $translation);
149 * Load a list of available language codes.
151 * Until translations is implemented, only returns the "en" language.
153 * @param string $machinename The machine readable name of the library(content type)
154 * @param int $major Major part of version number
155 * @param int $minor Minor part of version number
157 * @return array List of possible language codes
159 public function getAvailableLanguages($machinename, $major, $minor): array {
162 // Check if this information has been saved previously into the cache.
163 $langcache = \cache
::make('core', 'h5p_content_type_translations');
164 $library = new stdClass();
165 $library->machinename
= $machinename;
166 $library->majorversion
= $major;
167 $library->minorversion
= $minor;
168 $librarykey = helper
::get_cache_librarykey(core
::record_to_string($library));
169 $languages = $langcache->get($librarykey);
171 // This contains a list of all of the available languages for the library.
175 // Get the language files for this library.
177 file_storage
::COMPONENT
,
178 file_storage
::LIBRARY_FILEAREA
,
180 $filepathsqllike = $DB->sql_like('f.filepath', '?');
181 $params[] = '%language%';
182 $filenamesqllike = $DB->sql_like('f.filename', '?');
183 $params[] = '%.json';
185 $sql = "SELECT DISTINCT f.filename
186 FROM {h5p_libraries} hl
188 ON hl.id = f.itemid AND f.component = ? AND f.filearea = ?
189 AND $filepathsqllike AND $filenamesqllike
190 WHERE hl.machinename = ? AND hl.majorversion = ? AND hl.minorversion = ?";
191 $params[] = $machinename;
198 $results = $DB->get_recordset_sql($sql, $params);
199 if ($results->valid()) {
200 // Extract the code language from the JS language files.
201 foreach ($results as $result) {
202 if (!empty($result->filename
)) {
203 $lang = substr($result->filename
, 0, -5);
204 $languages[$lang] = $languages;
209 // Semantics is 'en' by default. It has to be added always.
210 if (!array_key_exists($defaultcode, $languages)) {
211 $languages = array_keys($languages);
212 array_unshift($languages, $defaultcode);
217 'machinename' => $machinename,
218 'majorversion' => $major,
219 'minorversion' => $minor,
221 if ($DB->record_exists('h5p_libraries', $params)) {
222 // If the library exists (but it doesn't contain any language file), at least defaultcode should be returned.
223 $languages[] = $defaultcode;
227 // Save available languages into the cache.
228 $langcache->set($librarykey, $languages);
234 * "Callback" for mark the given file as a permanent file.
236 * Used when saving content that has new uploaded files.
240 public function keepFile($fileid): void
{
241 // Temporal files will be removed on a task when they are in the "editor" file area and and are at least one day older.
245 * Return libraries details.
248 * 1. No input, will list all the available content types.
249 * 2. Libraries supported are specified, load additional data and verify
250 * that the content types are available. Used by e.g. the Presentation Tool
251 * Editor that already knows which content types are supported in its
254 * @param array $libraries List of library names + version to load info for.
256 * @return array List of all libraries loaded.
258 public function getLibraries($libraries = null): ?
array {
260 if ($libraries !== null) {
261 // Get details for the specified libraries.
263 $fields = 'title, runnable, metadatasettings, example, tutorial';
265 foreach ($libraries as $library) {
267 'machinename' => $library->name
,
268 'majorversion' => $library->majorVersion
,
269 'minorversion' => $library->minorVersion
272 $details = api
::get_library_details($params, true, $fields);
275 $library->title
= $details->title
;
276 $library->runnable
= $details->runnable
;
277 $library->metadataSettings
= json_decode($details->metadatasettings ??
'');
278 $library->example
= $details->example
;
279 $library->tutorial
= $details->tutorial
;
280 $librariesin[] = $library;
284 $fields = 'id, machinename as name, title, majorversion, minorversion, metadatasettings, example, tutorial';
285 $librariesin = api
::get_contenttype_libraries($fields);
292 * Allow for other plugins to decide which styles and scripts are attached.
294 * This is useful for adding and/or modifying the functionality and look of
297 * @param array $files List of files as objects with path and version as properties.
298 * @param array $libraries List of libraries indexed by machineName with objects as values. The objects have majorVersion and
299 * minorVersion as properties.
301 public function alterLibraryFiles(&$files, $libraries): void
{
304 // Refactor dependency list.
306 foreach ($libraries as $dependency) {
307 $librarylist[$dependency['machineName']] = [
308 'majorVersion' => $dependency['majorVersion'],
309 'minorVersion' => $dependency['minorVersion']
313 $renderer = $PAGE->get_renderer('core_h5p');
315 $embedtype = 'editor';
316 $renderer->h5p_alter_scripts($files['scripts'], $librarylist, $embedtype);
317 $renderer->h5p_alter_styles($files['styles'], $librarylist, $embedtype);
321 * Saves a file or moves it temporarily.
323 * This is often necessary in order to validate and store uploaded or fetched H5Ps.
325 * @param string $data Uri of data that should be saved as a temporary file.
326 * @param bool $movefile Can be set to TRUE to move the data instead of saving it.
328 * @return bool|object Returns false if saving failed or an object with path
329 * of the directory and file that is temporarily saved.
331 public static function saveFileTemporarily($data, $movefile = false) {
332 // This is to be implemented when the Hub client is used to upload libraries.
337 * Marks a file for later cleanup.
339 * Useful when files are not instantly cleaned up. E.g. for files that are uploaded through the editor.
341 * @param int $file Id of file that should be cleaned up
342 * @param int|null $contentid Content id of file
344 public static function markFileForCleanup($file, $contentid = null): ?
int {
345 // Temporal files will be removed on a task when they are in the "editor" file area and and are at least one day older.
350 * Clean up temporary files
352 * @param string $filepath Path to file or directory
354 public static function removeTemporarilySavedFiles($filepath): void
{
355 // This is to be implemented when the Hub client is used to upload libraries.