forked from iucc/h5p-moodle-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.php
345 lines (305 loc) · 9.91 KB
/
ajax.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?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/>.
/**
* Responsible for handling AJAX requests related to H5P.
*
* @package mod_hvp
* @copyright 2016 Joubel AS <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/filelib.php');
require_once("locallib.php");
require_login();
$action = required_param('action', PARAM_ALPHA);
switch($action) {
/*
* Handle user data reporting
*
* Type: HTTP POST
*
* Parameters:
* - content_id
* - data_type
* - sub_content_id
*/
case 'contentsuserdata':
\mod_hvp\content_user_data::handle_ajax();
break;
/*
* Handle restricting H5P libraries
*
* Type: HTTP GET
*
* Parameters:
* - library_id
* - restrict (0 or 1)
* - token
*/
case 'restrictlibrary':
// Check permissions.
$context = \context_system::instance();
if (!has_capability('mod/hvp:restrictlibraries', $context)) {
\H5PCore::ajaxError(get_string('nopermissiontorestrict', 'hvp'));
http_response_code(403);
break;
}
$libraryid = required_param('library_id', PARAM_INT);
$restrict = required_param('restrict', PARAM_INT);
if (!\H5PCore::validToken('library_' . $libraryid, required_param('token', PARAM_RAW))) {
\H5PCore::ajaxError(get_string('invalidtoken', 'hvp'));
exit;
}
hvp_restrict_library($libraryid, $restrict);
header('Cache-Control: no-cache');
header('Content-Type: application/json');
echo json_encode(array(
'url' => (new moodle_url('/mod/hvp/ajax.php', array(
'action' => 'restrict_library',
'token' => \H5PCore::createToken('library_' . $libraryid),
'restrict' => ($restrict === '1' ? 0 : 1),
'library_id' => $libraryid
)))->out(false)));
break;
/*
* Collecting data needed by H5P content upgrade
*
* Type: HTTP GET
*
* Parameters:
* - library (Format: /<machine-name>/<major-version>/<minor-version>)
*/
case 'getlibrarydataforupgrade':
// Check permissions.
$context = \context_system::instance();
if (!has_capability('mod/hvp:updatelibraries', $context)) {
\H5PCore::ajaxError(get_string('nopermissiontoupgrade', 'hvp'));
http_response_code(403);
break;
}
$library = required_param('library', PARAM_TEXT);
$library = explode('/', substr($library, 1));
if (count($library) !== 3) {
http_response_code(422);
return;
}
$library = hvp_get_library_upgrade_info($library[0], $library[1], $library[2]);
header('Cache-Control: no-cache');
header('Content-Type: application/json');
print json_encode($library);
break;
/*
* Saving upgraded content, and returning next batch to process
*
* Type: HTTP POST
*
* Parameters:
* - library_id
*/
case 'libraryupgradeprogress':
// Check upgrade permissions.
$context = \context_system::instance();
if (!has_capability('mod/hvp:updatelibraries', $context)) {
\H5PCore::ajaxError(get_string('nopermissiontoupgrade', 'hvp'));
http_response_code(403);
break;
}
// Because of a confirmed bug in PHP, filter_input(INPUT_SERVER, ...)
// will return null on some versions of FCGI/PHP (5.4 and probably
// older versions as well), ref. https://bugs.php.net/bug.php?id=49184.
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$libraryid = required_param('library_id', PARAM_INT);
$out = hvp_content_upgrade_progress($libraryid);
header('Cache-Control: no-cache');
header('Content-Type: application/json');
print json_encode($out);
} else {
// Only allow POST.
http_response_code(405);
}
break;
/*
* Handle set finished / storing grades
*
* Type: HTTP GET
*
* Parameters:
* - contentId
* - score
* - maxScore
*/
case 'setfinished':
\mod_hvp\user_grades::handle_ajax();
break;
/*
* Saves a dynamically graded grade to the gradebook
*
* Type: HTTP POST
*
* Parameters:
* - subcontent_id
* - score
*/
case 'updatesubcontentscore':
\mod_hvp\user_grades::handle_dynamic_grading();
break;
/*
* Returns a grade
*
* Type: HTTP GET
*
* Parameters:
* - subcontent_id
*/
case 'getsubcontentscore':
\mod_hvp\user_grades::return_subcontent_grade();
break;
/*
* Provide data for results view
*
* Type: HTTP GET
*
* Parameters:
* int content_id
* int offset
* int limit
* int sortBy
* int sortDir
* string[] filters
*/
case 'results':
$results = new \mod_hvp\results();
$results->print_results();
break;
/*
* Load list of libraries or details for library.
*
* Parameters:
* string machineName
* int majorVersion
* int minorVersion
*/
case 'libraries':
if (!\mod_hvp\framework::has_editor_access('nopermissiontoviewcontenttypes')) {
break;
}
// Get parameters.
$name = optional_param('machineName', '', PARAM_TEXT);
$major = optional_param('majorVersion', 0, PARAM_INT);
$minor = optional_param('minorVersion', 0, PARAM_INT);
$editor = \mod_hvp\framework::instance('editor');
$language = optional_param('default-language', null, PARAM_RAW);
if (!empty($name)) {
$editor->ajax->action(H5PEditorEndpoints::SINGLE_LIBRARY, $name,
$major, $minor, \mod_hvp\framework::get_language(), '', '', $language);
new \mod_hvp\event(
'library', null,
null, null,
$name, $major . '.' . $minor
);
} else {
$editor->ajax->action(H5PEditorEndpoints::LIBRARIES);
}
break;
/*
* Load content type cache list to display available libraries in hub
*/
case 'contenttypecache':
if (!\mod_hvp\framework::has_editor_access('nopermissiontoviewcontenttypes')) {
break;
}
$editor = \mod_hvp\framework::instance('editor');
$editor->ajax->action(H5PEditorEndpoints::CONTENT_TYPE_CACHE);
break;
/*
* Handle file upload through the editor.
*
* Parameters:
* int contentId
* int contextId
*/
case 'files':
$token = required_param('token', PARAM_RAW);
$contentid = required_param('contentId', PARAM_INT);
if (!\mod_hvp\framework::has_editor_access('nopermissiontouploadfiles')) {
break;
}
$editor = \mod_hvp\framework::instance('editor');
$editor->ajax->action(H5PEditorEndpoints::FILES, $token, $contentid);
break;
/*
* Handle file upload through the editor.
*
* Parameters:
* raw token
* raw contentTypeUrl
*/
case 'libraryinstall':
$token = required_param('token', PARAM_RAW);
$machinename = required_param('id', PARAM_TEXT);
$editor = \mod_hvp\framework::instance('editor');
$editor->ajax->action(H5PEditorEndpoints::LIBRARY_INSTALL, $token, $machinename);
break;
/*
* Install libraries from h5p and retrieve content json
*
* Parameters:
* file h5p
*/
case 'libraryupload':
$token = required_param('token', PARAM_RAW);
if (!\mod_hvp\framework::has_editor_access('nopermissiontouploadcontent')) {
break;
}
$editor = \mod_hvp\framework::instance('editor');
$uploadpath = $_FILES['h5p']['tmp_name'];
$contentid = optional_param('contentId', 0, PARAM_INT);
$editor->ajax->action(H5PEditorEndpoints::LIBRARY_UPLOAD, $token, $uploadpath, $contentid);
break;
/*
* Record xAPI result from view
*/
case 'xapiresult':
\mod_hvp\xapi_result::handle_ajax();
break;
case 'translations':
if (!\mod_hvp\framework::has_editor_access('nopermissiontogettranslations')) {
break;
}
$language = required_param('language', PARAM_RAW);
$editor = \mod_hvp\framework::instance('editor');
$editor->ajax->action(H5PEditorEndpoints::TRANSLATIONS, $language);
break;
/*
* Handle filtering of parameters through AJAX.
*/
case 'filter':
$token = required_param('token', PARAM_RAW);
$libraryparameters = required_param('libraryParameters', PARAM_RAW);
if (!\mod_hvp\framework::has_editor_access('nopermissiontouploadfiles')) {
break;
}
$editor = \mod_hvp\framework::instance('editor');
$editor->ajax->action(H5PEditorEndpoints::FILTER, $token, $libraryparameters);
break;
/*
* Throw error if AJAX isnt handeled
*/
default:
throw new coding_exception('Unhandled AJAX');
break;
}