| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
// This file is part of Moodle - http://moodle.org/
|
|
|
4 |
//
|
|
|
5 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
6 |
// it under the terms of the GNU General Public License as published by
|
|
|
7 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
8 |
// (at your option) any later version.
|
|
|
9 |
//
|
|
|
10 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
13 |
// GNU General Public License for more details.
|
|
|
14 |
//
|
|
|
15 |
// You should have received a copy of the GNU General Public License
|
|
|
16 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Defines backup_section_task class
|
|
|
20 |
*
|
|
|
21 |
* @package core_backup
|
|
|
22 |
* @subpackage moodle2
|
|
|
23 |
* @category backup
|
|
|
24 |
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
|
|
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
26 |
*/
|
|
|
27 |
|
|
|
28 |
defined('MOODLE_INTERNAL') || die();
|
|
|
29 |
|
|
|
30 |
/**
|
|
|
31 |
* section task that provides all the properties and common steps to be performed
|
|
|
32 |
* when one section is being backup
|
|
|
33 |
*
|
|
|
34 |
* TODO: Finish phpdocs
|
|
|
35 |
*/
|
|
|
36 |
class backup_section_task extends backup_task {
|
|
|
37 |
|
|
|
38 |
protected $sectionid;
|
|
|
39 |
|
|
|
40 |
/**
|
| 1441 |
ariadna |
41 |
* @var stdClass $section The database section object.
|
|
|
42 |
*/
|
|
|
43 |
protected stdClass $section;
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @var int|null $delegatedcmid the course module that is delegating this section (if any)
|
|
|
47 |
*/
|
|
|
48 |
protected ?int $delegatedcmid = null;
|
|
|
49 |
|
|
|
50 |
/**
|
| 1 |
efrain |
51 |
* Constructor - instantiates one object of this class
|
|
|
52 |
*/
|
|
|
53 |
public function __construct($name, $sectionid, $plan = null) {
|
|
|
54 |
global $DB;
|
|
|
55 |
|
|
|
56 |
// Check section exists
|
|
|
57 |
if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
|
|
|
58 |
throw new backup_task_exception('section_task_section_not_found', $sectionid);
|
|
|
59 |
}
|
|
|
60 |
|
| 1441 |
ariadna |
61 |
$this->section = $section;
|
| 1 |
efrain |
62 |
$this->sectionid = $sectionid;
|
|
|
63 |
|
|
|
64 |
parent::__construct($name, $plan);
|
|
|
65 |
}
|
|
|
66 |
|
| 1441 |
ariadna |
67 |
/**
|
|
|
68 |
* Set the course module that is delegating this section.
|
|
|
69 |
*
|
|
|
70 |
* Delegated section can belong to any kind of plugin. However, when a delegated
|
|
|
71 |
* section belongs to a course module, the UI will present all settings according.
|
|
|
72 |
*
|
|
|
73 |
* @param int $cmid the course module id that is delegating this section
|
|
|
74 |
*/
|
|
|
75 |
public function set_delegated_cm(int $cmid) {
|
|
|
76 |
$this->delegatedcmid = $cmid;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Get the course module that is delegating this section.
|
|
|
81 |
*
|
|
|
82 |
* @return int|null the course module id that is delegating this section
|
|
|
83 |
*/
|
|
|
84 |
public function get_delegated_cm(): ?int {
|
|
|
85 |
return $this->delegatedcmid;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Get the delegate activity modname (if any).
|
|
|
90 |
*
|
|
|
91 |
* @return string|null the modname of the delegated activity
|
|
|
92 |
*/
|
|
|
93 |
public function get_modname(): ?string {
|
|
|
94 |
if (empty($this->section->component)) {
|
|
|
95 |
return null;
|
|
|
96 |
}
|
|
|
97 |
return core_component::normalize_component($this->section->component)[1];
|
|
|
98 |
}
|
|
|
99 |
|
| 1 |
efrain |
100 |
public function get_sectionid() {
|
|
|
101 |
return $this->sectionid;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Section tasks have their own directory to write files
|
|
|
106 |
*/
|
|
|
107 |
public function get_taskbasepath() {
|
|
|
108 |
|
|
|
109 |
return $this->get_basepath() . '/sections/section_' . $this->sectionid;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Create all the steps that will be part of this task
|
|
|
114 |
*/
|
|
|
115 |
public function build() {
|
|
|
116 |
|
|
|
117 |
// Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
|
|
|
118 |
$coursectxid = context_course::instance($this->get_courseid())->id;
|
| 1441 |
ariadna |
119 |
$this->add_section_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid);
|
| 1 |
efrain |
120 |
|
|
|
121 |
// Add some extra settings that related processors are going to need
|
| 1441 |
ariadna |
122 |
$this->add_section_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid);
|
|
|
123 |
$this->add_section_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid());
|
| 1 |
efrain |
124 |
|
|
|
125 |
// Create the section directory
|
|
|
126 |
$this->add_step(new create_taskbasepath_directory('create_section_directory'));
|
|
|
127 |
|
|
|
128 |
// Create the section.xml common file (course_sections)
|
|
|
129 |
$this->add_step(new backup_section_structure_step('section_commons', 'section.xml'));
|
|
|
130 |
|
|
|
131 |
// Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
|
|
|
132 |
$this->add_step(new backup_inforef_structure_step('section_inforef', 'inforef.xml'));
|
|
|
133 |
|
|
|
134 |
// Migrate the already exported inforef entries to final ones
|
|
|
135 |
$this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
|
|
|
136 |
|
|
|
137 |
// At the end, mark it as built
|
|
|
138 |
$this->built = true;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Exceptionally override the execute method, so, based in the section_included setting, we are able
|
|
|
143 |
* to skip the execution of one task completely
|
|
|
144 |
*/
|
|
|
145 |
public function execute() {
|
|
|
146 |
|
|
|
147 |
// Find section_included_setting
|
|
|
148 |
if (!$this->get_setting_value('included')) {
|
|
|
149 |
$this->log('section skipped by _included setting', backup::LOG_DEBUG, $this->name);
|
|
|
150 |
|
|
|
151 |
} else { // Setting tells us it's ok to execute
|
|
|
152 |
parent::execute();
|
|
|
153 |
}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
/**
|
|
|
157 |
* Specialisation that, first of all, looks for the setting within
|
|
|
158 |
* the task with the the prefix added and later, delegates to parent
|
|
|
159 |
* without adding anything
|
|
|
160 |
*/
|
|
|
161 |
public function get_setting($name) {
|
|
|
162 |
$namewithprefix = 'section_' . $this->sectionid . '_' . $name;
|
|
|
163 |
$result = null;
|
|
|
164 |
foreach ($this->settings as $key => $setting) {
|
|
|
165 |
if ($setting->get_name() == $namewithprefix) {
|
|
|
166 |
if ($result != null) {
|
|
|
167 |
throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
|
|
|
168 |
} else {
|
|
|
169 |
$result = $setting;
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
}
|
|
|
173 |
if ($result) {
|
|
|
174 |
return $result;
|
|
|
175 |
} else {
|
|
|
176 |
// Fallback to parent
|
|
|
177 |
return parent::get_setting($name);
|
|
|
178 |
}
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
// Protected API starts here
|
|
|
182 |
|
|
|
183 |
/**
|
| 1441 |
ariadna |
184 |
* Define the common setting that any backup section will have.
|
| 1 |
efrain |
185 |
*/
|
|
|
186 |
protected function define_settings() {
|
|
|
187 |
global $DB;
|
|
|
188 |
|
| 1441 |
ariadna |
189 |
// All the settings related to this activity will include this prefix.
|
| 1 |
efrain |
190 |
$settingprefix = 'section_' . $this->sectionid . '_';
|
|
|
191 |
|
| 1441 |
ariadna |
192 |
$incudefield = $this->add_section_included_setting($settingprefix);
|
|
|
193 |
$this->add_section_userinfo_setting($settingprefix, $incudefield);
|
|
|
194 |
}
|
| 1 |
efrain |
195 |
|
| 1441 |
ariadna |
196 |
/**
|
|
|
197 |
* Add a setting to the task. This method is used to add a setting to the task
|
|
|
198 |
*
|
|
|
199 |
* @param int|string $identifier the identifier of the setting
|
|
|
200 |
* @param string $type the type of the setting
|
|
|
201 |
* @param string|int $value the value of the setting
|
|
|
202 |
* @return section_backup_setting the setting added
|
|
|
203 |
*/
|
|
|
204 |
protected function add_section_setting(int|string $identifier, string $type, string|int $value): section_backup_setting {
|
|
|
205 |
if ($this->get_delegated_cm()) {
|
|
|
206 |
$setting = new backup_subsection_generic_setting($identifier, $type, $value);
|
|
|
207 |
} else {
|
|
|
208 |
$setting = new backup_section_generic_setting($identifier, $type, $value);
|
|
|
209 |
}
|
|
|
210 |
$this->add_setting($setting);
|
|
|
211 |
return $setting;
|
|
|
212 |
}
|
| 1 |
efrain |
213 |
|
| 1441 |
ariadna |
214 |
/**
|
|
|
215 |
* Add the section included setting to the task.
|
|
|
216 |
*
|
|
|
217 |
* @param string $settingprefix the identifier of the setting
|
|
|
218 |
* @return section_backup_setting the setting added
|
|
|
219 |
*/
|
|
|
220 |
protected function add_section_included_setting(string $settingprefix): section_backup_setting {
|
|
|
221 |
global $DB;
|
|
|
222 |
$course = $DB->get_record('course', ['id' => $this->section->course], '*', MUST_EXIST);
|
|
|
223 |
|
|
|
224 |
// Define sectionincluded (to decide if the whole task must be really executed).
|
| 1 |
efrain |
225 |
$settingname = $settingprefix . 'included';
|
|
|
226 |
|
| 1441 |
ariadna |
227 |
$delegatedcmid = $this->get_delegated_cm();
|
|
|
228 |
if ($delegatedcmid) {
|
|
|
229 |
$sectionincluded = new backup_subsection_included_setting($settingname, base_setting::IS_BOOLEAN, true);
|
|
|
230 |
// Subsections depends on the parent activity included setting.
|
|
|
231 |
$settingname = $this->get_modname() . '_' . $delegatedcmid . '_included';
|
|
|
232 |
if ($this->plan->setting_exists($settingname)) {
|
|
|
233 |
$cmincluded = $this->plan->get_setting($settingname);
|
|
|
234 |
$cmincluded->add_dependency(
|
|
|
235 |
$sectionincluded,
|
|
|
236 |
);
|
|
|
237 |
}
|
|
|
238 |
$sectionincluded->get_ui()->set_label(get_string('subsectioncontent', 'backup'));
|
|
|
239 |
} else {
|
|
|
240 |
$sectionincluded = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
|
|
|
241 |
$sectionincluded->get_ui()->set_label(get_section_name($course, $this->section));
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
$this->add_setting($sectionincluded);
|
|
|
245 |
|
|
|
246 |
return $sectionincluded;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* Add the section userinfo setting to the task.
|
|
|
251 |
*
|
|
|
252 |
* @param string $settingprefix the identifier of the setting
|
|
|
253 |
* @param section_backup_setting $includefield the setting to depend on
|
|
|
254 |
* @return section_backup_setting the setting added
|
|
|
255 |
*/
|
|
|
256 |
protected function add_section_userinfo_setting(
|
|
|
257 |
string $settingprefix,
|
|
|
258 |
section_backup_setting $includefield
|
|
|
259 |
): section_backup_setting {
|
|
|
260 |
// Define sectionuserinfo. Dependent of:
|
|
|
261 |
// - users root setting.
|
|
|
262 |
// - section_included setting.
|
| 1 |
efrain |
263 |
$settingname = $settingprefix . 'userinfo';
|
| 1441 |
ariadna |
264 |
|
|
|
265 |
$delegatedcmid = $this->get_delegated_cm();
|
|
|
266 |
if ($delegatedcmid) {
|
|
|
267 |
$sectionuserinfo = new backup_subsection_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
|
|
|
268 |
// Subsections depends on the parent activity included setting.
|
|
|
269 |
$settingname = $this->get_modname() . '_' . $delegatedcmid . '_userinfo';
|
|
|
270 |
if ($this->plan->setting_exists($settingname)) {
|
|
|
271 |
$cmincluded = $this->plan->get_setting($settingname);
|
|
|
272 |
$cmincluded->add_dependency(
|
|
|
273 |
$sectionuserinfo,
|
|
|
274 |
);
|
|
|
275 |
}
|
|
|
276 |
} else {
|
|
|
277 |
$sectionuserinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
$sectionuserinfo->get_ui()->set_label(get_string('includeuserinfo', 'backup'));
|
|
|
281 |
$sectionuserinfo->get_ui()->set_visually_hidden_label(
|
|
|
282 |
get_string('section_prefix', 'core_backup', $this->section->name ?: $this->section->section)
|
|
|
283 |
);
|
|
|
284 |
$this->add_setting($sectionuserinfo);
|
|
|
285 |
// Look for "users" root setting.
|
| 1 |
efrain |
286 |
$users = $this->plan->get_setting('users');
|
| 1441 |
ariadna |
287 |
$users->add_dependency($sectionuserinfo);
|
|
|
288 |
// Look for "section_included" section setting.
|
|
|
289 |
$includefield->add_dependency($sectionuserinfo);
|
|
|
290 |
|
|
|
291 |
return $sectionuserinfo;
|
| 1 |
efrain |
292 |
}
|
|
|
293 |
}
|