forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_plan_builder.class.php
197 lines (169 loc) · 7.95 KB
/
backup_plan_builder.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines backup_plan_builder class
*
* @package core_backup
* @subpackage moodle2
* @category backup
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/moodle2/backup_root_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_activity_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_section_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_final_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php');
// Load all the activity tasks for moodle2 format
$mods = core_component::get_plugin_list('mod');
foreach ($mods as $mod => $moddir) {
$taskpath = $moddir . '/backup/moodle2/backup_' . $mod . '_activity_task.class.php';
if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
if (file_exists($taskpath)) {
require_once($taskpath);
}
}
}
// Load all the block tasks for moodle2 format
$blocks = core_component::get_plugin_list('block');
foreach ($blocks as $block => $blockdir) {
$taskpath = $blockdir . '/backup/moodle2/backup_' . $block . '_block_task.class.php';
if (file_exists($taskpath)) {
require_once($taskpath);
}
}
/**
* Abstract class defining the static method in charge of building the whole
* backup plan, based in @backup_controller preferences.
*
* TODO: Finish phpdocs
*/
abstract class backup_plan_builder {
/**
* Dispatches, based on type to specialised builders
*/
static public function build_plan($controller) {
$plan = $controller->get_plan();
// Add the root task, responsible for storing global settings
// and some init tasks
$plan->add_task(new backup_root_task('root_task'));
switch ($controller->get_type()) {
case backup::TYPE_1ACTIVITY:
self::build_activity_plan($controller, $controller->get_id());
break;
case backup::TYPE_1SECTION:
self::build_section_plan($controller, $controller->get_id());
break;
case backup::TYPE_1COURSE:
self::build_course_plan($controller, $controller->get_id());
break;
}
// Add the final task, responsible for outputting
// all the global xml files (groups, users,
// gradebook, questions, roles, files...) and
// the main moodle_backup.xml file
// and perform other various final actions.
$plan->add_task(new backup_final_task('final_task'));
}
/**
* Return one array of supported backup types
*/
static public function supported_backup_types() {
return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
}
// Protected API starts here
/**
* Build one 1-activity backup
*/
static protected function build_activity_plan($controller, $id) {
$plan = $controller->get_plan();
// Add the activity task, responsible for outputting
// all the module related information
try {
$plan->add_task(backup_factory::get_backup_activity_task($controller->get_format(), $id));
// For the given activity, add as many block tasks as necessary
$blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
foreach ($blockids as $blockid) {
try {
$plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
} catch (backup_task_exception $e) {
$a = stdClass();
$a->mid = $id;
$a->bid = $blockid;
$controller->log(get_string('error_block_for_module_not_found', 'backup', $a), backup::LOG_WARNING);
}
}
} catch (backup_task_exception $e) {
$controller->log(get_string('error_course_module_not_found', 'backup', $id), backup::LOG_WARNING);
}
}
/**
* Build one 1-section backup
*/
static protected function build_section_plan($controller, $id) {
$plan = $controller->get_plan();
// Add the section task, responsible for outputting
// all the section related information
$plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
// For the given section, add as many activity tasks as necessary
$coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
foreach ($coursemodules as $coursemodule) {
if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
self::build_activity_plan($controller, $coursemodule->id);
} else {
// TODO: Debug information about module not supported
}
}
}
/**
* Build one 1-course backup
*/
static protected function build_course_plan($controller, $id) {
$plan = $controller->get_plan();
// Add the course task, responsible for outputting
// all the course related information
$plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
// For the given course, add as many section tasks as necessary
$sections = backup_plan_dbops::get_sections_from_courseid($id);
foreach ($sections as $section) {
self::build_section_plan($controller, $section);
}
// For the given course, add as many block tasks as necessary
$blockids = backup_plan_dbops::get_blockids_from_courseid($id);
foreach ($blockids as $blockid) {
$plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));
}
}
}