weekly release 3.9dev+
[moodle.git] / h5p / ajax.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 * Responsible for handling AJAX requests related to H5P.
19 *
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
23 */
24
25use core_h5p\factory;
26use core_h5p\framework;
27
28define('AJAX_SCRIPT', true);
29
30require(__DIR__ . '/../config.php');
31require_once($CFG->libdir . '/filelib.php');
32
33require_login();
34
35$action = required_param('action', PARAM_ALPHA);
36$contextid = required_param('contextId', PARAM_INT);
37
38$context = context::instance_by_id($contextid);
39
40if (!has_capability('moodle/h5p:updatelibraries', $context)) {
41 H5PCore::ajaxError(get_string('nopermissiontoedit', 'h5p'));
42 header('HTTP/1.1 403 Forbidden');
43 return;
44}
45
46$factory = new factory();
47$editor = $factory->get_editor();
48
49switch ($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);
56
57 $language = optional_param('default-language', null, PARAM_ALPHA);
58
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);
64 }
65
66 break;
67
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;
72
73 // Handle file upload through the editor.
74 case 'files':
75 $token = required_param('token', PARAM_RAW);
76 $contentid = required_param('contentId', PARAM_INT);
77
78 $editor->ajax->action(H5PEditorEndpoints::FILES, $token, $contentid);
79 break;
80
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;
87
88 // Handle file upload through the editor.
89 case 'libraryupload':
90 $token = required_param('token', PARAM_RAW);
91
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;
96
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;
102
103 // Handle filtering of parameters through AJAX.
104 case 'filter':
105 $token = required_param('token', PARAM_RAW);
106 $libraryparameters = required_param('libraryParameters', PARAM_RAW);
107
108 $editor->ajax->action(H5PEditorEndpoints::FILTER, $token, $libraryparameters);
109 break;
110
111 // Throw error if AJAX action is not handled.
112 default:
113 throw new coding_exception('Unhandled AJAX');
114 break;
115}