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 |
* Defines backup structure steps 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 |
* Define the complete hvp structure for backup, with file and id annotations
|
|
|
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 backup_hvp_activity_structure_step extends backup_activity_structure_step {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Defines backup element's structure
|
|
|
38 |
*
|
|
|
39 |
* @return backup_nested_element
|
|
|
40 |
* @throws base_element_struct_exception
|
|
|
41 |
* @throws base_step_exception
|
|
|
42 |
*/
|
|
|
43 |
protected function define_structure() {
|
|
|
44 |
|
|
|
45 |
// To know if we are including user info.
|
|
|
46 |
$userinfo = $this->get_setting_value('userinfo');
|
|
|
47 |
|
|
|
48 |
// Define each element separated.
|
|
|
49 |
$hvp = new backup_nested_element('hvp', array('id'), array(
|
|
|
50 |
'name',
|
|
|
51 |
'machine_name',
|
|
|
52 |
'major_version',
|
|
|
53 |
'minor_version',
|
|
|
54 |
'intro',
|
|
|
55 |
'introformat',
|
|
|
56 |
'json_content',
|
|
|
57 |
'embed_type',
|
|
|
58 |
'disable',
|
|
|
59 |
'content_type',
|
|
|
60 |
'source',
|
|
|
61 |
'year_from',
|
|
|
62 |
'year_to',
|
|
|
63 |
'license_version',
|
|
|
64 |
'changes',
|
|
|
65 |
'license_extras',
|
|
|
66 |
'author_comments',
|
|
|
67 |
'slug',
|
|
|
68 |
'timecreated',
|
|
|
69 |
'timemodified',
|
|
|
70 |
'authors',
|
|
|
71 |
'license',
|
|
|
72 |
'completionpass'
|
|
|
73 |
));
|
|
|
74 |
|
|
|
75 |
// User data.
|
|
|
76 |
$entries = new backup_nested_element('content_user_data');
|
|
|
77 |
$contentuserdata = new backup_nested_element('entry', array(
|
|
|
78 |
'user_id', // Annotated.
|
|
|
79 |
'sub_content_id'
|
|
|
80 |
), array(
|
|
|
81 |
'data_id',
|
|
|
82 |
'data',
|
|
|
83 |
'preloaded',
|
|
|
84 |
'delete_on_content_change',
|
|
|
85 |
));
|
|
|
86 |
|
|
|
87 |
// Build the tree.
|
|
|
88 |
|
|
|
89 |
$hvp->add_child($entries);
|
|
|
90 |
$entries->add_child($contentuserdata);
|
|
|
91 |
|
|
|
92 |
// Define sources.
|
|
|
93 |
|
|
|
94 |
// Uses library name and version instead of main_library_id.
|
|
|
95 |
$hvp->set_source_sql('
|
|
|
96 |
SELECT h.id,
|
|
|
97 |
hl.machine_name,
|
|
|
98 |
hl.major_version,
|
|
|
99 |
hl.minor_version,
|
|
|
100 |
h.name,
|
|
|
101 |
h.intro,
|
|
|
102 |
h.introformat,
|
|
|
103 |
h.json_content,
|
|
|
104 |
h.embed_type,
|
|
|
105 |
h.disable,
|
|
|
106 |
h.content_type,
|
|
|
107 |
h.slug,
|
|
|
108 |
h.timecreated,
|
|
|
109 |
h.timemodified,
|
|
|
110 |
h.authors,
|
|
|
111 |
h.source,
|
|
|
112 |
h.year_from,
|
|
|
113 |
h.year_to,
|
|
|
114 |
h.license_version,
|
|
|
115 |
h.changes,
|
|
|
116 |
h.license_extras,
|
|
|
117 |
h.author_comments,
|
|
|
118 |
h.license,
|
|
|
119 |
h.completionpass
|
|
|
120 |
FROM {hvp} h
|
|
|
121 |
JOIN {hvp_libraries} hl ON hl.id = h.main_library_id
|
|
|
122 |
WHERE h.id = ?', array(backup::VAR_ACTIVITYID));
|
|
|
123 |
|
|
|
124 |
// All the rest of elements only happen if we are including user info.
|
|
|
125 |
if ($userinfo) {
|
|
|
126 |
$contentuserdata->set_source_table('hvp_content_user_data', array('hvp_id' => backup::VAR_PARENTID));
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// Define id annotations.
|
|
|
130 |
$contentuserdata->annotate_ids('user', 'user_id');
|
|
|
131 |
// In an ideal world we would use the main_library_id and annotate that
|
|
|
132 |
// but since we cannot know the required dependencies of the content
|
|
|
133 |
// without parsing json_content and crawling the libraries_libraries
|
|
|
134 |
// (library dependencies) table it's much easier to just include all
|
|
|
135 |
// installed libraries.
|
|
|
136 |
|
|
|
137 |
// Define file annotations.
|
|
|
138 |
$hvp->annotate_files('mod_hvp', 'intro', null, null);
|
|
|
139 |
$hvp->annotate_files('mod_hvp', 'content', null, null);
|
|
|
140 |
|
|
|
141 |
// Return the root element (hvp), wrapped into standard activity structure.
|
|
|
142 |
return $this->prepare_activity_structure($hvp);
|
|
|
143 |
}
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
/**
|
|
|
147 |
* Backup h5p libraries.
|
|
|
148 |
*
|
|
|
149 |
* Structure step in charge of constructing the hvp_libraries.xml file for
|
|
|
150 |
* all the H5P libraries.
|
|
|
151 |
*
|
|
|
152 |
* @copyright 2018 Joubel AS <contact@joubel.com>
|
|
|
153 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
154 |
*/
|
|
|
155 |
class backup_hvp_libraries_structure_step extends backup_structure_step {
|
|
|
156 |
|
|
|
157 |
/**
|
|
|
158 |
* Determines if backup step should be executed
|
|
|
159 |
*
|
|
|
160 |
* @return bool
|
|
|
161 |
* @throws backup_step_exception
|
|
|
162 |
*/
|
|
|
163 |
protected function execute_condition() {
|
|
|
164 |
$fullpath = $this->task->get_taskbasepath();
|
|
|
165 |
if (empty($fullpath)) {
|
|
|
166 |
throw new backup_step_exception('backup_structure_step_undefined_fullpath');
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// Modify filename to use a globally shared file for all libraries.
|
|
|
170 |
$this->filename = "../{$this->filename}";
|
|
|
171 |
|
|
|
172 |
// Append the filename to the full path.
|
|
|
173 |
$fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
|
|
|
174 |
|
|
|
175 |
// Determine if already generated.
|
|
|
176 |
return !file_exists($fullpath);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* Defines the structure to be executed by this backup step
|
|
|
181 |
*
|
|
|
182 |
* @return backup_nested_element
|
|
|
183 |
* @throws base_element_struct_exception
|
|
|
184 |
* @throws dml_exception
|
|
|
185 |
*/
|
|
|
186 |
protected function define_structure() {
|
|
|
187 |
// Libraries.
|
|
|
188 |
$libraries = new backup_nested_element('hvp_libraries');
|
|
|
189 |
$library = new backup_nested_element('library', array('id'), array(
|
|
|
190 |
'title',
|
|
|
191 |
'machine_name',
|
|
|
192 |
'major_version',
|
|
|
193 |
'minor_version',
|
|
|
194 |
'patch_version',
|
|
|
195 |
'runnable',
|
|
|
196 |
'fullscreen',
|
|
|
197 |
'embed_types',
|
|
|
198 |
'preloaded_js',
|
|
|
199 |
'preloaded_css',
|
|
|
200 |
'drop_library_css',
|
|
|
201 |
'semantics',
|
|
|
202 |
'restricted',
|
|
|
203 |
'tutorial_url',
|
|
|
204 |
'add_to',
|
|
|
205 |
'metadata'
|
|
|
206 |
));
|
|
|
207 |
|
|
|
208 |
// Library translations.
|
|
|
209 |
$translations = new backup_nested_element('translations');
|
|
|
210 |
$translation = new backup_nested_element('translation', array(
|
|
|
211 |
'language_code'
|
|
|
212 |
), array(
|
|
|
213 |
'language_json'
|
|
|
214 |
));
|
|
|
215 |
|
|
|
216 |
// Library dependencies.
|
|
|
217 |
$dependencies = new backup_nested_element('dependencies');
|
|
|
218 |
$dependency = new backup_nested_element('dependency', array(
|
|
|
219 |
'required_library_id'
|
|
|
220 |
), array(
|
|
|
221 |
'dependency_type'
|
|
|
222 |
));
|
|
|
223 |
|
|
|
224 |
// Build the tree.
|
|
|
225 |
$libraries->add_child($library);
|
|
|
226 |
|
|
|
227 |
$library->add_child($translations);
|
|
|
228 |
$translations->add_child($translation);
|
|
|
229 |
|
|
|
230 |
$library->add_child($dependencies);
|
|
|
231 |
$dependencies->add_child($dependency);
|
|
|
232 |
|
|
|
233 |
// Define sources.
|
|
|
234 |
|
|
|
235 |
$library->set_source_table('hvp_libraries', array());
|
|
|
236 |
|
|
|
237 |
$translation->set_source_table('hvp_libraries_languages', array('library_id' => backup::VAR_PARENTID));
|
|
|
238 |
|
|
|
239 |
$dependency->set_source_table('hvp_libraries_libraries', array('library_id' => backup::VAR_PARENTID));
|
|
|
240 |
|
|
|
241 |
// Define file annotations.
|
|
|
242 |
$context = \context_system::instance();
|
|
|
243 |
$library->annotate_files('mod_hvp', 'libraries', null, $context->id);
|
|
|
244 |
|
|
|
245 |
// Return root element.
|
|
|
246 |
return $libraries;
|
|
|
247 |
}
|
|
|
248 |
}
|