fe1905ca760825c101d33c245542e550cca150b3
[moodle.git] / backup / moodle2 / restore_plan_builder.class.php
1 <?php
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/>.
18 /**
19  * @package moodlecore
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
23  */
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);
43         }
44     }
45 }
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);
53     }
54 }
56 /**
57  * Abstract class defining the static method in charge of building the whole
58  * restore plan, based in @restore_controller preferences.
59  *
60  * TODO: Finish phpdocs
61  */
62 abstract class restore_plan_builder {
64     /**
65      * Dispatches, based on type to specialised builders
66      */
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());
81                 break;
82             case backup::TYPE_1SECTION:
83                 self::build_section_plan($controller, $controller->get_courseid());
84                 break;
85             case backup::TYPE_1COURSE:
86                 self::build_course_plan($controller, $controller->get_courseid());
87                 break;
88         }
90         // Add the final task, responsible for closing
91         // all the pending bits (remapings, inter-links
92         // conversion...)
93         // and perform other various final actions.
94         $plan->add_task(new restore_final_task('final_task'));
95     }
98 // Protected API starts here
100     /**
101      * Restore one 1-activity backup
102      */
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);
121                 } else {
122                     // TODO: Debug information about block not supported
123                 }
124             }
125         }
126     }
128     /**
129      * Restore one 1-section backup
130      */
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) {
143                 continue;
144             }
145             if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
146                 self::build_activity_plan($controller, $activityid);
147             } else {
148                 // TODO: Debug information about module not supported
149             }
150         }
151     }
153     /**
154      * Restore one 1-course backup
155      */
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);
169         }
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);
177             } else {
178                 // TODO: Debug information about block not supported
179             }
180         }
181     }