3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @subpackage backup-moodle2
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
26 require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
27 require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
28 require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
29 require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
30 require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
31 require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
32 //require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
33 require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
34 require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
36 // Load all the activity tasks for moodle2 format
37 $mods = get_plugin_list('mod');
38 foreach ($mods as $mod => $moddir) {
39 $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
40 if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
41 if (file_exists($taskpath)) {
42 require_once($taskpath);
47 // Load all the block tasks for moodle2 format
48 $blocks = get_plugin_list('block');
49 foreach ($blocks as $block => $blockdir) {
50 $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
51 if (file_exists($taskpath)) {
52 require_once($taskpath);
57 * Abstract class defining the static method in charge of building the whole
58 * restore plan, based in @restore_controller preferences.
60 * TODO: Finish phpdocs
62 abstract class restore_plan_builder {
65 * Dispatches, based on type to specialised builders
67 static public function build_plan($controller) {
69 $plan = $controller->get_plan();
71 // Add the root task, responsible for
72 // preparing everything, creating the
73 // needed structures (users, roles),
74 // preloading information to temp table
75 // and other init tasks
76 $plan->add_task(new restore_root_task('root_task'));
78 switch ($controller->get_type()) {
79 case backup::TYPE_1ACTIVITY:
80 self::build_activity_plan($controller, $controller->get_courseid());
82 case backup::TYPE_1SECTION:
83 self::build_section_plan($controller, $controller->get_courseid());
85 case backup::TYPE_1COURSE:
86 self::build_course_plan($controller, $controller->get_courseid());
90 // Add the final task, responsible for closing
91 // all the pending bits (remapings, inter-links
93 // and perform other various final actions.
94 $plan->add_task(new restore_final_task('final_task'));
98 // Protected API starts here
101 * Restore one 1-activity backup
103 static protected function build_activity_plan($controller, $activityid) {
105 $plan = $controller->get_plan();
106 $info = $controller->get_info();
107 $infoactivity = $info->activities[$activityid];
109 // Add the activity task, responsible for restoring
110 // all the module related information. So it conditionally
111 // as far as the module can be missing on restore
112 if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
113 $plan->add_task($task);
115 // For the given activity path, add as many block tasks as necessary
116 // TODO: Add blocks, we need to introspect xml here
117 $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
118 foreach ($blocks as $basepath => $name) {
119 if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
120 $plan->add_task($task);
122 // TODO: Debug information about block not supported
129 * Restore one 1-section backup
131 static protected function build_section_plan($controller, $sectionid) {
133 $plan = $controller->get_plan();
134 $info = $controller->get_info();
135 $infosection = $info->sections[$sectionid];
137 // Add the section task, responsible for restoring
138 // all the section related information
139 $plan->add_task(restore_factory::get_restore_section_task($infosection));
140 // For the given section, add as many activity tasks as necessary
141 foreach ($info->activities as $activityid => $activity) {
142 if ($activity->sectionid != $infosection->sectionid) {
145 if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
146 self::build_activity_plan($controller, $activityid);
148 // TODO: Debug information about module not supported
154 * Restore one 1-course backup
156 static protected function build_course_plan($controller, $courseid) {
158 $plan = $controller->get_plan();
159 $info = $controller->get_info();
161 // Add the course task, responsible for restoring
162 // all the course related information
163 $task = restore_factory::get_restore_course_task($info->course, $courseid);
164 $plan->add_task($task);
166 // For the given course, add as many section tasks as necessary
167 foreach ($info->sections as $sectionid => $section) {
168 self::build_section_plan($controller, $sectionid);
171 // For the given course path, add as many block tasks as necessary
172 // TODO: Add blocks, we need to introspect xml here
173 $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
174 foreach ($blocks as $basepath => $name) {
175 if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
176 $plan->add_task($task);
178 // TODO: Debug information about block not supported