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 * Responsible for handling AJAX requests related to H5P.
21 * @copyright 2020 Victor Deniz <victor@moodle.com>, based on code by Joubel AS
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 use core_h5p\framework
;
28 define('AJAX_SCRIPT', true);
30 require(__DIR__
. '/../config.php');
31 require_once($CFG->libdir
. '/filelib.php');
35 $action = required_param('action', PARAM_ALPHA
);
36 $contextid = required_param('contextId', PARAM_INT
);
38 $context = context
::instance_by_id($contextid);
40 if (!has_capability('moodle/h5p:updatelibraries', $context)) {
41 H5PCore
::ajaxError(get_string('nopermissiontoedit', 'h5p'));
42 header('HTTP/1.1 403 Forbidden');
46 $factory = new factory();
47 $editor = $factory->get_editor();
50 // Load list of libraries or details for library.
53 $name = optional_param('machineName', '', PARAM_TEXT
);
54 $major = optional_param('majorVersion', 0, PARAM_INT
);
55 $minor = optional_param('minorVersion', 0, PARAM_INT
);
57 $language = optional_param('default-language', null, PARAM_ALPHA
);
60 $editor->ajax
->action(H5PEditorEndpoints
::SINGLE_LIBRARY
, $name,
61 $major, $minor, framework
::get_language(), '', '', $language);
63 $editor->ajax
->action(H5PEditorEndpoints
::LIBRARIES
);
68 // Load content type cache list to display available libraries in hub.
69 case 'contenttypecache':
70 $editor->ajax
->action(H5PEditorEndpoints
::CONTENT_TYPE_CACHE
);
73 // Handle file upload through the editor.
75 $token = required_param('token', PARAM_RAW
);
76 $contentid = required_param('contentId', PARAM_INT
);
78 $editor->ajax
->action(H5PEditorEndpoints
::FILES
, $token, $contentid);
81 // Install libraries from H5P and retrieve content json.
82 case 'libraryinstall':
83 $token = required_param('token', PARAM_RAW
);
84 $machinename = required_param('id', PARAM_TEXT
);
85 $editor->ajax
->action(H5PEditorEndpoints
::LIBRARY_INSTALL
, $token, $machinename);
88 // Handle file upload through the editor.
90 $token = required_param('token', PARAM_RAW
);
92 $uploadpath = $_FILES['h5p']['tmp_name'];
93 $contentid = optional_param('contentId', 0, PARAM_INT
);
94 $editor->ajax
->action(H5PEditorEndpoints
::LIBRARY_UPLOAD
, $token, $uploadpath, $contentid);
97 // Get the $language libraries translations.
99 $language = required_param('language', PARAM_RAW
);
100 $editor->ajax
->action(H5PEditorEndpoints
::TRANSLATIONS
, $language);
103 // Handle filtering of parameters through AJAX.
105 $token = required_param('token', PARAM_RAW
);
106 $libraryparameters = required_param('libraryParameters', PARAM_RAW
);
108 $editor->ajax
->action(H5PEditorEndpoints
::FILTER
, $token, $libraryparameters);
111 // Throw error if AJAX action is not handled.
113 throw new coding_exception('Unhandled AJAX');