MDL-67814 core_h5p: implemented H5P Core/Editor library interfaces
[moodle.git] / h5p / ajax.php
blob88e8e17d6067237a9672dd8f49e4cd320fa91445
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.
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/>.
17 /**
18 * Responsible for handling AJAX requests related to H5P.
20 * @package core_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
25 use core_h5p\factory;
26 use core_h5p\framework;
28 define('AJAX_SCRIPT', true);
30 require(__DIR__ . '/../config.php');
31 require_once($CFG->libdir . '/filelib.php');
33 require_login();
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');
43 return;
46 $factory = new factory();
47 $editor = $factory->get_editor();
49 switch ($action) {
50 // Load list of libraries or details for library.
51 case 'libraries':
52 // Get parameters.
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);
59 if (!empty($name)) {
60 $editor->ajax->action(H5PEditorEndpoints::SINGLE_LIBRARY, $name,
61 $major, $minor, framework::get_language(), '', '', $language);
62 } else {
63 $editor->ajax->action(H5PEditorEndpoints::LIBRARIES);
66 break;
68 // Load content type cache list to display available libraries in hub.
69 case 'contenttypecache':
70 $editor->ajax->action(H5PEditorEndpoints::CONTENT_TYPE_CACHE);
71 break;
73 // Handle file upload through the editor.
74 case 'files':
75 $token = required_param('token', PARAM_RAW);
76 $contentid = required_param('contentId', PARAM_INT);
78 $editor->ajax->action(H5PEditorEndpoints::FILES, $token, $contentid);
79 break;
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);
86 break;
88 // Handle file upload through the editor.
89 case 'libraryupload':
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);
95 break;
97 // Get the $language libraries translations.
98 case 'translations':
99 $language = required_param('language', PARAM_RAW);
100 $editor->ajax->action(H5PEditorEndpoints::TRANSLATIONS, $language);
101 break;
103 // Handle filtering of parameters through AJAX.
104 case 'filter':
105 $token = required_param('token', PARAM_RAW);
106 $libraryparameters = required_param('libraryParameters', PARAM_RAW);
108 $editor->ajax->action(H5PEditorEndpoints::FILTER, $token, $libraryparameters);
109 break;
111 // Throw error if AJAX action is not handled.
112 default:
113 throw new coding_exception('Unhandled AJAX');
114 break;