mirrored from git://git.moodle.org/moodle.git
/
restore_plan_builder.class.php
195 lines (168 loc) · 7.74 KB
/
restore_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
<?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/>.
/**
* @package moodlecore
* @subpackage backup-moodle2
* @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/restore_root_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_theme_plugin.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_format_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
// Load all the activity tasks for moodle2 format
$mods = get_plugin_list('mod');
foreach ($mods as $mod => $moddir) {
$taskpath = $moddir . '/backup/moodle2/restore_' . $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 = get_plugin_list('block');
foreach ($blocks as $block => $blockdir) {
$taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
if (file_exists($taskpath)) {
require_once($taskpath);
}
}
/**
* Abstract class defining the static method in charge of building the whole
* restore plan, based in @restore_controller preferences.
*
* TODO: Finish phpdocs
*/
abstract class restore_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
// preparing everything, creating the
// needed structures (users, roles),
// preloading information to temp table
// and other init tasks
$plan->add_task(new restore_root_task('root_task'));
switch ($controller->get_type()) {
case backup::TYPE_1ACTIVITY:
self::build_activity_plan($controller, key($controller->get_info()->activities));
break;
case backup::TYPE_1SECTION:
self::build_section_plan($controller, key($controller->get_info()->sections));
break;
case backup::TYPE_1COURSE:
self::build_course_plan($controller, $controller->get_courseid());
break;
}
// Add the final task, responsible for closing
// all the pending bits (remapings, inter-links
// conversion...)
// and perform other various final actions.
$plan->add_task(new restore_final_task('final_task'));
}
// Protected API starts here
/**
* Restore one 1-activity backup
*/
static protected function build_activity_plan($controller, $activityid) {
$plan = $controller->get_plan();
$info = $controller->get_info();
$infoactivity = $info->activities[$activityid];
// Add the activity task, responsible for restoring
// all the module related information. So it conditionally
// as far as the module can be missing on restore
if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
$plan->add_task($task);
// For the given activity path, add as many block tasks as necessary
// TODO: Add blocks, we need to introspect xml here
$blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
foreach ($blocks as $basepath => $name) {
if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
$plan->add_task($task);
} else {
// TODO: Debug information about block not supported
}
}
} else { // Activity is missing in target site, inform plan about that
$plan->set_missing_modules();
}
}
/**
* Restore one 1-section backup
*/
static protected function build_section_plan($controller, $sectionid) {
$plan = $controller->get_plan();
$info = $controller->get_info();
$infosection = $info->sections[$sectionid];
// Add the section task, responsible for restoring
// all the section related information
$plan->add_task(restore_factory::get_restore_section_task($infosection));
// For the given section, add as many activity tasks as necessary
foreach ($info->activities as $activityid => $activity) {
if ($activity->sectionid != $infosection->sectionid) {
continue;
}
if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
self::build_activity_plan($controller, $activityid);
} else {
// TODO: Debug information about module not supported
}
}
}
/**
* Restore one 1-course backup
*/
static protected function build_course_plan($controller, $courseid) {
$plan = $controller->get_plan();
$info = $controller->get_info();
// Add the course task, responsible for restoring
// all the course related information
$task = restore_factory::get_restore_course_task($info->course, $courseid);
$plan->add_task($task);
// For the given course path, add as many block tasks as necessary
// TODO: Add blocks, we need to introspect xml here
$blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
foreach ($blocks as $basepath => $name) {
if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
$plan->add_task($task);
} else {
// TODO: Debug information about block not supported
}
}
// For the given course, add as many section tasks as necessary
foreach ($info->sections as $sectionid => $section) {
self::build_section_plan($controller, $sectionid);
}
}
}