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 |
* Restore structure step for both hvp content and hvp libraries
|
|
|
19 |
*
|
|
|
20 |
* @package mod_hvp
|
|
|
21 |
* @category backup
|
|
|
22 |
* @copyright 2016 Joubel AS <contact@joubel.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
defined('MOODLE_INTERNAL') || die();
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Structure step to restore one H5P activity
|
|
|
30 |
*
|
|
|
31 |
* @copyright 2018 Joubel AS <contact@joubel.com>
|
|
|
32 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
class restore_hvp_activity_structure_step extends restore_activity_structure_step {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Defines restore element's structure
|
|
|
38 |
*
|
|
|
39 |
* @return array
|
|
|
40 |
* @throws base_step_exception
|
|
|
41 |
*/
|
|
|
42 |
protected function define_structure() {
|
|
|
43 |
$paths = array();
|
|
|
44 |
$userinfo = $this->get_setting_value('userinfo');
|
|
|
45 |
|
|
|
46 |
// Restore activities.
|
|
|
47 |
$paths[] = new restore_path_element('hvp', '/activity/hvp');
|
|
|
48 |
|
|
|
49 |
if ($userinfo) {
|
|
|
50 |
// Restore content state.
|
|
|
51 |
$paths[] = new restore_path_element('content_user_data', '/activity/hvp/content_user_data/entry');
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
// Return the paths wrapped into standard activity structure.
|
|
|
55 |
return $this->prepare_activity_structure($paths);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Process H5P, inserting the record into the database.
|
|
|
60 |
*
|
|
|
61 |
* @param $data
|
|
|
62 |
*
|
|
|
63 |
* @throws base_step_exception
|
|
|
64 |
* @throws dml_exception
|
|
|
65 |
*/
|
|
|
66 |
protected function process_hvp($data) {
|
|
|
67 |
global $DB;
|
|
|
68 |
|
|
|
69 |
$data = (object) $data;
|
|
|
70 |
$data->course = $this->get_courseid();
|
|
|
71 |
$data->main_library_id = \restore_hvp_libraries_structure_step::get_library_id($data);
|
|
|
72 |
|
|
|
73 |
$data->timecreated = $this->apply_date_offset($data->timecreated);
|
|
|
74 |
$data->timemodified = $this->apply_date_offset($data->timemodified);
|
|
|
75 |
|
|
|
76 |
// Insert the hvp record.
|
|
|
77 |
$newitemid = $DB->insert_record('hvp', $data);
|
|
|
78 |
// Immediately after inserting "activity" record, call this.
|
|
|
79 |
$this->apply_activity_instance($newitemid);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Process and inserts content user data.
|
|
|
84 |
*
|
|
|
85 |
* @param $data
|
|
|
86 |
*
|
|
|
87 |
* @throws dml_exception
|
|
|
88 |
*/
|
|
|
89 |
protected function process_content_user_data($data) {
|
|
|
90 |
global $DB;
|
|
|
91 |
|
|
|
92 |
$data = (object) $data;
|
|
|
93 |
$data->user_id = $this->get_mappingid('user', $data->user_id);
|
|
|
94 |
$data->hvp_id = $this->get_new_parentid('hvp');
|
|
|
95 |
|
|
|
96 |
$DB->insert_record('hvp_content_user_data', $data);
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Additional work that needs to be done after steps executions.
|
|
|
101 |
*/
|
|
|
102 |
protected function after_execute() {
|
|
|
103 |
// Add files for intro field.
|
|
|
104 |
$this->add_related_files('mod_hvp', 'intro', null);
|
|
|
105 |
|
|
|
106 |
// Add hvp related files.
|
|
|
107 |
$this->add_related_files('mod_hvp', 'content', 'hvp');
|
|
|
108 |
}
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Structure step to restore H5P libraries
|
|
|
113 |
*
|
|
|
114 |
* @copyright 2018 Joubel AS <contact@joubel.com>
|
|
|
115 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
116 |
*/
|
|
|
117 |
class restore_hvp_libraries_structure_step extends restore_activity_structure_step {
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Determines if library should be restored.
|
|
|
121 |
*
|
|
|
122 |
* @return bool
|
|
|
123 |
* @throws backup_step_exception
|
|
|
124 |
*/
|
|
|
125 |
protected function execute_condition() {
|
|
|
126 |
static $librariesrestored;
|
|
|
127 |
|
|
|
128 |
// Prevent this step from running more than once
|
|
|
129 |
// since all hvp_libraries.xml files are the same.
|
|
|
130 |
if (!empty($librariesrestored)) {
|
|
|
131 |
return false;
|
|
|
132 |
}
|
|
|
133 |
$librariesrestored = true;
|
|
|
134 |
|
|
|
135 |
// Get full path to activity backup location.
|
|
|
136 |
$fullpath = $this->task->get_taskbasepath();
|
|
|
137 |
if (empty($fullpath)) {
|
|
|
138 |
throw new backup_step_exception('backup_structure_step_undefined_fullpath');
|
|
|
139 |
}
|
|
|
140 |
$fullpath = rtrim($fullpath, '/');
|
|
|
141 |
|
|
|
142 |
// Check for the activity's local hvp_libraries.xml file.
|
|
|
143 |
if (file_exists("{$fullpath}/{$this->filename}")) {
|
|
|
144 |
// Use that.
|
|
|
145 |
return true;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
// Look for a global hvp_libraries.xml file.
|
|
|
149 |
if (file_exists("{$fullpath}/../{$this->filename}")) {
|
|
|
150 |
// Use it.
|
|
|
151 |
$this->filename = "../{$this->filename}";
|
|
|
152 |
return true;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
// Not able to find a hvp_libraries.xml, skip restore and let the admin
|
|
|
156 |
// be responsible for providing the approperiate libraries.
|
|
|
157 |
// (Could also be using Import which doesn't need libraries).
|
|
|
158 |
return false;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/**
|
|
|
162 |
* Defines how library should be restored.
|
|
|
163 |
*
|
|
|
164 |
* @return array
|
|
|
165 |
*/
|
|
|
166 |
protected function define_structure() {
|
|
|
167 |
$paths = array();
|
|
|
168 |
|
|
|
169 |
// Restore libraries first.
|
|
|
170 |
$paths[] = new restore_path_element('hvp_library', '/hvp_libraries/library');
|
|
|
171 |
|
|
|
172 |
// Add translations.
|
|
|
173 |
$paths[] = new restore_path_element('hvp_library_translation', '/hvp_libraries/library/translations/translation');
|
|
|
174 |
|
|
|
175 |
// ... and dependencies.
|
|
|
176 |
$paths[] = new restore_path_element('hvp_library_dependency', '/hvp_libraries/library/dependencies/dependency');
|
|
|
177 |
|
|
|
178 |
// Return the paths wrapped into standard activity structure.
|
|
|
179 |
return $this->prepare_activity_structure($paths);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Process and insert library record.
|
|
|
184 |
*
|
|
|
185 |
* @param $data
|
|
|
186 |
*
|
|
|
187 |
* @throws dml_exception
|
|
|
188 |
* @throws restore_step_exception
|
|
|
189 |
*/
|
|
|
190 |
protected function process_hvp_library($data) {
|
|
|
191 |
global $DB;
|
|
|
192 |
|
|
|
193 |
$data = (object)$data;
|
|
|
194 |
$oldid = $data->id;
|
|
|
195 |
unset($data->id);
|
|
|
196 |
|
|
|
197 |
$libraryid = self::get_library_id($data);
|
|
|
198 |
if (!$libraryid) {
|
|
|
199 |
// There is no updating of libraries. If an older patch version exists
|
|
|
200 |
// on the site that one will be used instead of the new one in the backup.
|
|
|
201 |
// This is due to the default behavior when files are restored in Moodle.
|
|
|
202 |
|
|
|
203 |
// Restore library.
|
|
|
204 |
$libraryid = $DB->insert_record('hvp_libraries', $data);
|
|
|
205 |
|
|
|
206 |
// Update libraries cache.
|
|
|
207 |
self::get_library_id($data, $libraryid);
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
// Keep track of libraries for translations and dependencies.
|
|
|
211 |
$this->set_mapping('hvp_library', $oldid, $libraryid);
|
|
|
212 |
|
|
|
213 |
// Update any dependencies that require this library.
|
|
|
214 |
$this->update_missing_dependencies($oldid, $libraryid);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Process and inserts translations for library.
|
|
|
219 |
*
|
|
|
220 |
* @param $data
|
|
|
221 |
*
|
|
|
222 |
* @throws dml_exception
|
|
|
223 |
*/
|
|
|
224 |
protected function process_hvp_library_translation($data) {
|
|
|
225 |
global $DB;
|
|
|
226 |
|
|
|
227 |
$data = (object) $data;
|
|
|
228 |
$data->library_id = $this->get_new_parentid('hvp_library');
|
|
|
229 |
|
|
|
230 |
// Check that translations doesn't exists.
|
|
|
231 |
$translation = $DB->get_record_sql(
|
|
|
232 |
'SELECT id
|
|
|
233 |
FROM {hvp_libraries_languages}
|
|
|
234 |
WHERE library_id = ?
|
|
|
235 |
AND language_code = ?',
|
|
|
236 |
array($data->library_id,
|
|
|
237 |
$data->language_code)
|
|
|
238 |
);
|
|
|
239 |
|
|
|
240 |
if (empty($translation)) {
|
|
|
241 |
// Only restore translations if library has been restored.
|
|
|
242 |
$DB->insert_record('hvp_libraries_languages', $data);
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Process and inserts library dependencies.
|
|
|
248 |
*
|
|
|
249 |
* @param $data
|
|
|
250 |
*
|
|
|
251 |
* @throws dml_exception
|
|
|
252 |
*/
|
|
|
253 |
protected function process_hvp_library_dependency($data) {
|
|
|
254 |
global $DB;
|
|
|
255 |
|
|
|
256 |
$data = (object) $data;
|
|
|
257 |
$data->library_id = $this->get_new_parentid('hvp_library');
|
|
|
258 |
|
|
|
259 |
$newlibraryid = $this->get_mappingid('hvp_library', $data->required_library_id);
|
|
|
260 |
if ($newlibraryid) {
|
|
|
261 |
$data->required_library_id = $newlibraryid;
|
|
|
262 |
|
|
|
263 |
// Check that the dependency doesn't exists.
|
|
|
264 |
$dependency = $DB->get_record_sql(
|
|
|
265 |
'SELECT id
|
|
|
266 |
FROM {hvp_libraries_libraries}
|
|
|
267 |
WHERE library_id = ?
|
|
|
268 |
AND required_library_id = ?',
|
|
|
269 |
[
|
|
|
270 |
$data->library_id,
|
|
|
271 |
$data->required_library_id,
|
|
|
272 |
]
|
|
|
273 |
);
|
|
|
274 |
if (empty($dependency)) {
|
|
|
275 |
$DB->insert_record('hvp_libraries_libraries', $data);
|
|
|
276 |
}
|
|
|
277 |
} else {
|
|
|
278 |
// The required dependency hasn't been restored yet. We need to add this dependency later.
|
|
|
279 |
$this->update_missing_dependencies($data->required_library_id, null, $data);
|
|
|
280 |
}
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* Additional work that is executed after library restoration steps.
|
|
|
285 |
*
|
|
|
286 |
* @throws dml_exception
|
|
|
287 |
*/
|
|
|
288 |
protected function after_execute() {
|
|
|
289 |
// Add files for libraries.
|
|
|
290 |
$context = \context_system::instance();
|
|
|
291 |
$this->add_related_files('mod_hvp', 'libraries', null, $context->id);
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
/**
|
|
|
295 |
* Cache to reduce queries.
|
|
|
296 |
*
|
|
|
297 |
* @param $library
|
|
|
298 |
* @param null $set
|
|
|
299 |
*
|
|
|
300 |
* @return mixed
|
|
|
301 |
* @throws dml_exception
|
|
|
302 |
*/
|
|
|
303 |
public static function get_library_id(&$library, $set = null) {
|
|
|
304 |
static $keytoid;
|
|
|
305 |
global $DB;
|
|
|
306 |
|
|
|
307 |
$key = $library->machine_name . ' ' . $library->major_version . '.' . $library->minor_version;
|
|
|
308 |
if (is_null($keytoid)) {
|
|
|
309 |
$keytoid = array();
|
|
|
310 |
}
|
|
|
311 |
if ($set !== null) {
|
|
|
312 |
$keytoid[$key] = $set;
|
|
|
313 |
} else if (!isset($keytoid[$key])) {
|
|
|
314 |
$lib = $DB->get_record_sql(
|
|
|
315 |
'SELECT id
|
|
|
316 |
FROM {hvp_libraries}
|
|
|
317 |
WHERE machine_name = ?
|
|
|
318 |
AND major_version = ?
|
|
|
319 |
AND minor_version = ?',
|
|
|
320 |
array($library->machine_name,
|
|
|
321 |
$library->major_version,
|
|
|
322 |
$library->minor_version)
|
|
|
323 |
);
|
|
|
324 |
|
|
|
325 |
// Non existing = false.
|
|
|
326 |
$keytoid[$key] = (empty($lib) ? false : $lib->id);
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
return $keytoid[$key];
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
/**
|
|
|
333 |
* Keep track of missing dependencies since libraries aren't inserted
|
|
|
334 |
* in any special order
|
|
|
335 |
*
|
|
|
336 |
* @param $oldid
|
|
|
337 |
* @param $newid
|
|
|
338 |
* @param null $setmissing
|
|
|
339 |
*
|
|
|
340 |
* @throws dml_exception
|
|
|
341 |
*/
|
|
|
342 |
private function update_missing_dependencies($oldid, $newid, $setmissing = null) {
|
|
|
343 |
static $missingdeps;
|
|
|
344 |
global $DB;
|
|
|
345 |
|
|
|
346 |
if (is_null($missingdeps)) {
|
|
|
347 |
$missingdeps = array();
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
if ($setmissing !== null) {
|
|
|
351 |
$missingdeps[$oldid][] = $setmissing;
|
|
|
352 |
} else if (isset($missingdeps[$oldid])) {
|
|
|
353 |
foreach ($missingdeps[$oldid] as $missingdep) {
|
|
|
354 |
$missingdep->required_library_id = $newid;
|
|
|
355 |
|
|
|
356 |
// Check that the dependency doesn't exists.
|
|
|
357 |
$dependency = $DB->get_record_sql(
|
|
|
358 |
'SELECT id
|
|
|
359 |
FROM {hvp_libraries_libraries}
|
|
|
360 |
WHERE library_id = ?
|
|
|
361 |
AND required_library_id = ?',
|
|
|
362 |
array($missingdep->library_id,
|
|
|
363 |
$missingdep->required_library_id)
|
|
|
364 |
);
|
|
|
365 |
if (empty($dependency)) {
|
|
|
366 |
$DB->insert_record('hvp_libraries_libraries', $missingdep);
|
|
|
367 |
}
|
|
|
368 |
}
|
|
|
369 |
unset($missingdeps[$oldid]);
|
|
|
370 |
}
|
|
|
371 |
}
|
|
|
372 |
}
|