1 |
efrain |
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 |
* \mod_hvp\editor_ajax class
|
|
|
19 |
*
|
|
|
20 |
* @package mod_hvp
|
|
|
21 |
* @copyright 2016 Joubel AS
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace mod_hvp;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
require_once(__DIR__ . '/../autoloader.php');
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Moodle's implementation of the H5P Editor Ajax interface.
|
|
|
33 |
* Makes it possible for the editor's core ajax functionality to communicate with the
|
|
|
34 |
* database used by Moodle.
|
|
|
35 |
*
|
|
|
36 |
* @package mod_hvp
|
|
|
37 |
* @copyright 2016 Joubel AS
|
|
|
38 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
39 |
*/
|
|
|
40 |
class editor_ajax implements \H5PEditorAjaxInterface {
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Gets latest library versions that exists locally
|
|
|
44 |
*
|
|
|
45 |
* @return array Latest version of all local libraries
|
|
|
46 |
* @throws \dml_exception
|
|
|
47 |
*/
|
|
|
48 |
// @codingStandardsIgnoreLine
|
|
|
49 |
public function getLatestLibraryVersions() {
|
|
|
50 |
global $DB;
|
|
|
51 |
|
|
|
52 |
$maxmajorversionsql = "
|
|
|
53 |
SELECT hl.machine_name, MAX(hl.major_version) AS major_version
|
|
|
54 |
FROM {hvp_libraries} hl
|
|
|
55 |
WHERE hl.runnable = 1
|
|
|
56 |
GROUP BY hl.machine_name";
|
|
|
57 |
|
|
|
58 |
$maxminorversionsql = "
|
|
|
59 |
SELECT hl2.machine_name, hl2.major_version, MAX(hl2.minor_version) AS minor_version
|
|
|
60 |
FROM ({$maxmajorversionsql}) hl1
|
|
|
61 |
JOIN {hvp_libraries} hl2
|
|
|
62 |
ON hl1.machine_name = hl2.machine_name
|
|
|
63 |
AND hl1.major_version = hl2.major_version
|
|
|
64 |
GROUP BY hl2.machine_name, hl2.major_version";
|
|
|
65 |
|
|
|
66 |
return $DB->get_records_sql("
|
|
|
67 |
SELECT hl4.id, hl4.machine_name, hl4.title, hl4.major_version,
|
|
|
68 |
hl4.minor_version, hl4.patch_version, hl4.has_icon, hl4.restricted
|
|
|
69 |
FROM {hvp_libraries} hl4
|
|
|
70 |
JOIN ({$maxminorversionsql}) hl3
|
|
|
71 |
ON hl4.machine_name = hl3.machine_name
|
|
|
72 |
AND hl4.major_version = hl3.major_version
|
|
|
73 |
AND hl4.minor_version = hl3.minor_version"
|
|
|
74 |
);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Get locally stored Content Type Cache. If machine name is provided
|
|
|
79 |
* it will only get the given content type from the cache
|
|
|
80 |
*
|
|
|
81 |
* @param null $machinename
|
|
|
82 |
*
|
|
|
83 |
* @return array|mixed|null|object Returns results from querying the database
|
|
|
84 |
* @throws \dml_exception
|
|
|
85 |
*/
|
|
|
86 |
// @codingStandardsIgnoreLine
|
|
|
87 |
public function getContentTypeCache($machinename = null) {
|
|
|
88 |
global $DB;
|
|
|
89 |
|
|
|
90 |
if ($machinename) {
|
|
|
91 |
return $DB->get_record_sql(
|
|
|
92 |
"SELECT id, is_recommended
|
|
|
93 |
FROM {hvp_libraries_hub_cache}
|
|
|
94 |
WHERE machine_name = ?",
|
|
|
95 |
array($machinename)
|
|
|
96 |
);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
return $DB->get_records("hvp_libraries_hub_cache");
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Gets recently used libraries for the current author
|
|
|
104 |
*
|
|
|
105 |
* @return array machine names. The first element in the array is the
|
|
|
106 |
* most recently used.
|
|
|
107 |
*/
|
|
|
108 |
// @codingStandardsIgnoreLine
|
|
|
109 |
public function getAuthorsRecentlyUsedLibraries() {
|
|
|
110 |
global $DB;
|
|
|
111 |
global $USER;
|
|
|
112 |
$recentlyused = array();
|
|
|
113 |
|
|
|
114 |
$results = $DB->get_records_sql(
|
|
|
115 |
"SELECT library_name, max(created_at) AS max_created_at
|
|
|
116 |
FROM {hvp_events}
|
|
|
117 |
WHERE type='content' AND sub_type = 'create' AND user_id = ?
|
|
|
118 |
GROUP BY library_name
|
|
|
119 |
ORDER BY max_created_at DESC", array($USER->id));
|
|
|
120 |
|
|
|
121 |
foreach ($results as $row) {
|
|
|
122 |
$recentlyused[] = $row->library_name;
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
return $recentlyused;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Checks if the provided token is valid for this endpoint
|
|
|
130 |
*
|
|
|
131 |
* @param string $token The token that will be validated for.
|
|
|
132 |
*
|
|
|
133 |
* @return bool True if successful validation
|
|
|
134 |
*/
|
|
|
135 |
// @codingStandardsIgnoreLine
|
|
|
136 |
public function validateEditorToken($token) {
|
|
|
137 |
return \H5PCore::validToken('editorajax', $token);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Get translations for a language for a list of libraries
|
|
|
142 |
*
|
|
|
143 |
* @param array $libraries An array of libraries, in the form "<machineName> <majorVersion>.<minorVersion>
|
|
|
144 |
* @param string $language_code
|
|
|
145 |
*
|
|
|
146 |
* @return array
|
|
|
147 |
* @throws \dml_exception
|
|
|
148 |
*/
|
|
|
149 |
public function getTranslations($libraries, $language_code) {
|
|
|
150 |
global $DB;
|
|
|
151 |
|
|
|
152 |
$translations = array();
|
|
|
153 |
|
|
|
154 |
foreach ($libraries as $library) {
|
|
|
155 |
$parsedLib = \H5PCore::libraryFromString($library);
|
|
|
156 |
|
|
|
157 |
$sql = "
|
|
|
158 |
SELECT language_json
|
|
|
159 |
FROM {hvp_libraries} lib
|
|
|
160 |
LEFT JOIN {hvp_libraries_languages} lang
|
|
|
161 |
ON lib.id = lang.library_id
|
|
|
162 |
WHERE lib.machine_name = :machine_name AND
|
|
|
163 |
lib.major_version = :major_version AND
|
|
|
164 |
lib.minor_version = :minor_version AND
|
|
|
165 |
lang.language_code = :language_code";
|
|
|
166 |
$translation = $DB->get_field_sql($sql, array(
|
|
|
167 |
'machine_name' => $parsedLib['machineName'],
|
|
|
168 |
'major_version' => $parsedLib['majorVersion'],
|
|
|
169 |
'minor_version' => $parsedLib['minorVersion'],
|
|
|
170 |
'language_code' => $language_code,
|
|
|
171 |
));
|
|
|
172 |
|
|
|
173 |
if ($translation !== false) {
|
|
|
174 |
$translations[$library] = $translation;
|
|
|
175 |
}
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
return $translations;
|
|
|
179 |
}
|
|
|
180 |
}
|