on-demand release 3.9dev+
[moodle.git] / h5p / classes / editor_framework.php
CommitLineData
6da050d7
VDF
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 * Class \core_h5p\editor_framework
19 *
20 * @package core_h5p
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
23 */
24
25namespace core_h5p;
26
27use H5peditorStorage;
28use stdClass;
29
30/**
31 * Moodle's implementation of the H5P Editor storage interface.
32 *
33 * Makes it possible for the editor's core library to communicate with the
34 * database used by Moodle.
35 *
36 * @package core_h5p
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
39 */
40class editor_framework implements H5peditorStorage {
41
42 /**
43 * Load language file(JSON).
44 * Used to translate the editor fields(title, description etc.)
45 *
46 * @param string $name The machine readable name of the library(content type)
47 * @param int $major Major part of version number
48 * @param int $minor Minor part of version number
49 * @param string $lang Language code
50 *
51 * @return string|boolean Translation in JSON format if available, false otherwise
52 */
53 public function getLanguage($name, $major, $minor, $lang) {
54 // To be implemented when translations are introduced.
55 return false;
56 }
57
58 /**
59 * Load a list of available language codes.
60 *
61 * Until translations is implemented, only returns the "en" language.
62 *
63 * @param string $machinename The machine readable name of the library(content type)
64 * @param int $major Major part of version number
65 * @param int $minor Minor part of version number
66 *
67 * @return array List of possible language codes
68 */
69 public function getAvailableLanguages($machinename, $major, $minor): array {
70 $defaultcode = 'en';
71 $codes = [];
72
73 // Semantics is 'en' by default.
74 array_unshift($codes, $defaultcode);
75
76 return $codes;
77 }
78
79 /**
80 * "Callback" for mark the given file as a permanent file.
81 *
82 * Used when saving content that has new uploaded files.
83 *
84 * @param int $fileid
85 */
86 public function keepFile($fileid): void {
87 // Temporal files will be removed on a task when they are in the "editor" file area and and are at least one day older.
88 }
89
90 /**
91 * Return libraries details.
92 *
93 * Two use cases:
94 * 1. No input, will list all the available content types.
95 * 2. Libraries supported are specified, load additional data and verify
96 * that the content types are available. Used by e.g. the Presentation Tool
97 * Editor that already knows which content types are supported in its
98 * slides.
99 *
100 * @param array $libraries List of library names + version to load info for.
101 *
102 * @return array List of all libraries loaded.
103 */
104 public function getLibraries($libraries = null): ?array {
105
106 if ($libraries !== null) {
107 // Get details for the specified libraries.
108 $librariesin = [];
109 $fields = 'title, runnable';
110
111 foreach ($libraries as $library) {
112 $params = [
113 'machinename' => $library->name,
114 'majorversion' => $library->majorVersion,
115 'minorversion' => $library->minorVersion
116 ];
117
118 $details = api::get_library_details($params, true, $fields);
119
120 if ($details) {
121 $library->title = $details->title;
122 $library->runnable = $details->runnable;
123 $librariesin[] = $library;
124 }
125 }
126 } else {
127 $fields = 'id, machinename as name, title, majorversion, minorversion';
128 $librariesin = api::get_contenttype_libraries($fields);
129 }
130
131 return $librariesin;
132 }
133
134 /**
135 * Allow for other plugins to decide which styles and scripts are attached.
136 *
137 * This is useful for adding and/or modifying the functionality and look of
138 * the content types.
139 *
140 * @param array $files List of files as objects with path and version as properties.
141 * @param array $libraries List of libraries indexed by machineName with objects as values. The objects have majorVersion and
142 * minorVersion as properties.
143 */
144 public function alterLibraryFiles(&$files, $libraries): void {
145 // This is to be implemented when the renderer is used.
146 }
147
148 /**
149 * Saves a file or moves it temporarily.
150 *
151 * This is often necessary in order to validate and store uploaded or fetched H5Ps.
152 *
153 * @param string $data Uri of data that should be saved as a temporary file.
154 * @param bool $movefile Can be set to TRUE to move the data instead of saving it.
155 *
156 * @return bool|object Returns false if saving failed or an object with path
157 * of the directory and file that is temporarily saved.
158 */
159 public static function saveFileTemporarily($data, $movefile = false) {
160 // This is to be implemented when the Hub client is used to upload libraries.
161 return false;
162 }
163
164 /**
165 * Marks a file for later cleanup.
166 *
167 * Useful when files are not instantly cleaned up. E.g. for files that are uploaded through the editor.
168 *
169 * @param int $file Id of file that should be cleaned up
170 * @param int|null $contentid Content id of file
171 */
172 public static function markFileForCleanup($file, $contentid = null): ?int {
173 // Temporal files will be removed on a task when they are in the "editor" file area and and are at least one day older.
174 return null;
175 }
176
177 /**
178 * Clean up temporary files
179 *
180 * @param string $filepath Path to file or directory
181 */
182 public static function removeTemporarilySavedFiles($filepath): void {
183 // This is to be implemented when the Hub client is used to upload libraries.
184 }
185}