forked from h5p/moodle-mod_hvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocallib.php
308 lines (272 loc) · 9.87 KB
/
locallib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Internal library of functions for module hvp
*
* All the hvp specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod_hvp
* @copyright 2016 Joubel AS <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Get array with settings for hvp core
*
* @return array Settings
*/
function hvp_get_core_settings() {
global $USER, $CFG, $COURSE;
$basePath = $CFG->sessioncookiepath;
$ajaxPath = $basePath . 'mod/hvp/ajax.php?action=';
$system_context = \context_system::instance();
$course_context = \context_course::instance($COURSE->id);
$settings = array(
'baseUrl' => $basePath,
'url' => "{$basePath}pluginfile.php/{$course_context->id}/mod_hvp",
'libraryUrl' => "{$basePath}pluginfile.php/{$system_context->id}/mod_hvp/libraries",
'postUserStatistics' => TRUE,
'ajax' => array(
'setFinished' => $ajaxPath . 'set_finished',
'contentUserData' => $ajaxPath . 'contents_user_data&content_id=:contentId&data_type=:dataType&sub_content_id=:subContentId'
),
'saveFreq' => get_config('mod_hvp', 'enable_save_content_state') ? get_config('mod_hvp', 'content_state_frequency') : FALSE,
'siteUrl' => $CFG->wwwroot,
'l10n' => array(
'H5P' => array(
'fullscreen' => get_string('fullscreen', 'hvp'),
'disableFullscreen' => get_string('disablefullscreen', 'hvp'),
'download' => get_string('download', 'hvp'),
'copyrights' => get_string('copyright', 'hvp'),
'copyrightInformation' => get_string('copyright', 'hvp'),
'close' => get_string('close', 'hvp'),
'title' => get_string('title', 'hvp'),
'author' => get_string('author', 'hvp'),
'year' => get_string('year', 'hvp'),
'source' => get_string('source', 'hvp'),
'license' => get_string('license', 'hvp'),
'thumbnail' => get_string('thumbnail', 'hvp'),
'noCopyrights' => get_string('nocopyright', 'hvp'),
'downloadDescription' => get_string('downloadtitle', 'hvp'),
'copyrightsDescription' => get_string('copyrighttitle', 'hvp'),
'h5pDescription' => get_string('h5ptitle', 'hvp'),
'contentChanged' => get_string('contentchanged', 'hvp'),
'startingOver' => get_string('startingover', 'hvp')
)
),
'user' => array(
'name' => $USER->firstname . ' ' . $USER->lastname,
'mail' => $USER->email
)
);
return $settings;
}
/**
* Get assets (scripts and styles) for hvp core.
*
* @return array
*/
function hvp_get_core_assets() {
global $CFG, $PAGE;
// Get core settings
$settings = hvp_get_core_settings();
$settings['core'] = array(
'styles' => array(),
'scripts' => array()
);
$settings['loadedJs'] = array();
$settings['loadedCss'] = array();
// Make sure files are reloaded for each plugin update
$cache_buster = hvp_get_cache_buster();
// Use relative URL to support both http and https.
$lib_url = $CFG->httpswwwroot . '/mod/hvp/library/';
$rel_path = '/' . preg_replace('/^[^:]+:\/\/[^\/]+\//', '', $lib_url);
// Add core stylesheets
foreach (H5PCore::$styles as $style) {
$settings['core']['styles'][] = $rel_path . $style . $cache_buster;
$PAGE->requires->css(new moodle_url($lib_url . $style . $cache_buster));
}
// Add core JavaScript
foreach (H5PCore::$scripts as $script) {
$settings['core']['scripts'][] = $rel_path . $script . $cache_buster;
$PAGE->requires->js(new moodle_url($lib_url . $script . $cache_buster), true);
}
return $settings;
}
/**
* Add core JS and CSS to page.
*
* @param moodle_page $page
* @param moodle_url|string $lib_url
* @param array|null $settings
* @throws \coding_exception
*/
function hvp_admin_add_generic_css_and_js($page, $lib_url, $settings = NULL) {
foreach (H5PCore::$adminScripts as $script) {
$page->requires->js(new moodle_url($lib_url . $script . hvp_get_cache_buster()), true);
}
if ($settings === NULL) {
$settings = array();
}
$settings['containerSelector'] = '#h5p-admin-container';
$settings['l10n'] = array(
'NA' => get_string('notapplicable', 'hvp'),
'viewLibrary' => '',
'deleteLibrary' => '',
'upgradeLibrary' => get_string('upgradelibrarycontent', 'hvp')
);
$page->requires->data_for_js('H5PAdminIntegration', $settings, true);
$page->requires->css(new moodle_url($lib_url . 'styles/h5p.css' . hvp_get_cache_buster()));
$page->requires->css(new moodle_url($lib_url . 'styles/h5p-admin.css' . hvp_get_cache_buster()));
// Add settings:
$page->requires->data_for_js('h5p', hvp_get_core_settings(), true);
}
/**
* Get a query string with the plugin version number to include at the end
* of URLs. This is used to force the browser to reload the asset when the
* plugin is updated.
*
* @return string
*/
function hvp_get_cache_buster() {
return '?ver=' . get_config('mod_hvp', 'version');
}
/**
* Get a new H5P security token.
*
* @param string $key
* @return string
*/
function hvp_get_token($key) {
return $_SESSION['h5p_' . $key] = uniqid('h5p-');
}
/**
* Verifiy a given H5P security token.
*
* @param string $key
* @param string $token
* @return string
*/
function hvp_verify_token($key, $token) {
return $_SESSION['h5p_' . $key] === $token;
}
/**
* Restrict access to a given content type.
*
* @param int $library_id
* @param bool $restrict
*/
function hvp_restrict_library($library_id, $restrict) {
global $DB;
$DB->update_record('hvp_libraries', (object) array(
'id' => $library_id,
'restricted' => $restrict ? 1 : 0
));
}
/**
* Handle content upgrade progress
*
* @method hvp_content_upgrade_progress
* @param int $library_id
* @return object An object including the json content for the H5P instances
* (maximum 40) that should be upgraded.
*/
function hvp_content_upgrade_progress($library_id) {
global $DB;
$to_library_id = filter_input(INPUT_POST, 'libraryId');
// Verify security token
if (!hvp_verify_token('content_upgrade', filter_input(INPUT_POST, 'token'))) {
print get_string('upgradeinvalidetoken', 'hvp');
return;
}
// Get the library we're upgrading to
$to_library = $DB->get_record('hvp_libraries', array(
'id' => $to_library_id
));
if (!$to_library) {
print get_string('upgradelibrarymissing', 'hvp');
return;
}
// Prepare response
$out = new stdClass();
$out->params = array();
$out->token = hvp_get_token('content_upgrade');
// Prepare our interface
$interface = \mod_hvp\framework::instance('interface');
// Get updated params
$params = filter_input(INPUT_POST, 'params');
if ($params !== NULL) {
// Update params.
$params = json_decode($params);
foreach ($params as $id => $param) {
$DB->update_record('hvp', (object) array(
'id' => $id,
'main_library_id' => $to_library->id,
'json_content' => $param,
'filtered' => ''
));
}
}
// Get number of contents for this library
$out->left = $interface->getNumContent($library_id);
if ($out->left) {
// Find the 40 first contents using this library version and add to params
$results = $DB->get_records_sql(
"SELECT id, json_content as params
FROM {hvp}
WHERE main_library_id = ?
ORDER BY name ASC", array($library_id), 0 , 40
);
foreach ($results as $content) {
$out->params[$content->id] = $content->params;
}
}
return $out;
}
/**
* Gets the information needed when content is upgraded
*
* @method hvp_get_library_upgrade_info
* @param string $name
* @param int $major
* @param int $minor
* @return object Library metadata including name, version, semantics and path
* to upgrade script
*/
function hvp_get_library_upgrade_info($name, $major, $minor) {
global $CFG;
$library = (object) array(
'name' => $name,
'version' => (object) array(
'major' => $major,
'minor' => $minor
)
);
$core = \mod_hvp\framework::instance();
$library->semantics = $core->loadLibrarySemantics($library->name, $library->version->major, $library->version->minor);
if ($library->semantics === NULL) {
http_response_code(404);
return;
}
$context = \context_system::instance();
$libraryFolderName = "{$library->name}-{$library->version->major}.{$library->version->minor}";
if (\mod_hvp\file_storage::fileExists($context->id, 'libraries', '/' . $libraryFolderName . '/', 'upgrades.js')) {
$basePath = $CFG->sessioncookiepath;
$library->upgradesScript = "{$basePath}pluginfile.php/{$context->id}/mod_hvp/libraries/{$libraryFolderName}/upgrades.js";
}
return $library;
}