MDL-25617 backup: backup/restore using extra_question_fields
[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  * Defines restore_plan_builder 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  */
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
31 require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
32 require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
33 require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
34 require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
35 require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
36 require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
37 require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
38 require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
39 require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_extrafields_plugin.class.php');
40 require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
41 require_once($CFG->dirroot . '/backup/moodle2/restore_local_plugin.class.php');
42 require_once($CFG->dirroot . '/backup/moodle2/restore_theme_plugin.class.php');
43 require_once($CFG->dirroot . '/backup/moodle2/restore_report_plugin.class.php');
44 require_once($CFG->dirroot . '/backup/moodle2/restore_coursereport_plugin.class.php');
45 require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.php');
46 require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php');
47 require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php');
48 require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
49 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
50 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
51 require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
52 require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
53 require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
54 require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
55 require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
56 require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
57 require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
58 require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
59 require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
60 require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
61 require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
63 // Load all the activity tasks for moodle2 format
64 $mods = core_component::get_plugin_list('mod');
65 foreach ($mods as $mod => $moddir) {
66     $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
67     if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
68         if (file_exists($taskpath)) {
69             require_once($taskpath);
70         }
71     }
72 }
74 // Load all the block tasks for moodle2 format
75 $blocks = core_component::get_plugin_list('block');
76 foreach ($blocks as $block => $blockdir) {
77     $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
78     if (file_exists($taskpath)) {
79         require_once($taskpath);
80     }
81 }
83 /**
84  * Abstract class defining the static method in charge of building the whole
85  * restore plan, based in @restore_controller preferences.
86  *
87  * TODO: Finish phpdocs
88  */
89 abstract class restore_plan_builder {
91     /**
92      * Dispatches, based on type to specialised builders
93      */
94     static public function build_plan($controller) {
96         $plan = $controller->get_plan();
98         // Add the root task, responsible for
99         // preparing everything, creating the
100         // needed structures (users, roles),
101         // preloading information to temp table
102         // and other init tasks
103         $plan->add_task(new restore_root_task('root_task'));
104         $controller->get_progress()->progress();
106         switch ($controller->get_type()) {
107             case backup::TYPE_1ACTIVITY:
108                 self::build_activity_plan($controller, key($controller->get_info()->activities));
109                 break;
110             case backup::TYPE_1SECTION:
111                 self::build_section_plan($controller, key($controller->get_info()->sections));
112                 break;
113             case backup::TYPE_1COURSE:
114                 self::build_course_plan($controller, $controller->get_courseid());
115                 break;
116         }
118         // Add the final task, responsible for closing
119         // all the pending bits (remapings, inter-links
120         // conversion...)
121         // and perform other various final actions.
122         $plan->add_task(new restore_final_task('final_task'));
123         $controller->get_progress()->progress();
124     }
127 // Protected API starts here
129     /**
130      * Restore one 1-activity backup
131      */
132     static protected function build_activity_plan($controller, $activityid) {
134         $plan = $controller->get_plan();
135         $info = $controller->get_info();
136         $infoactivity = $info->activities[$activityid];
138         // Add the activity task, responsible for restoring
139         // all the module related information. So it conditionally
140         // as far as the module can be missing on restore
141         if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
142             $plan->add_task($task);
143             $controller->get_progress()->progress();
145             // For the given activity path, add as many block tasks as necessary
146             // TODO: Add blocks, we need to introspect xml here
147             $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
148             foreach ($blocks as $basepath => $name) {
149                 if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
150                     $plan->add_task($task);
151                     $controller->get_progress()->progress();
152                 } else {
153                     // TODO: Debug information about block not supported
154                 }
155             }
156         } else { // Activity is missing in target site, inform plan about that
157             $plan->set_missing_modules();
158         }
160     }
162     /**
163      * Restore one 1-section backup
164      */
165     static protected function build_section_plan($controller, $sectionid) {
167         $plan = $controller->get_plan();
168         $info = $controller->get_info();
169         $infosection = $info->sections[$sectionid];
171         // Add the section task, responsible for restoring
172         // all the section related information
173         $plan->add_task(restore_factory::get_restore_section_task($infosection));
174         $controller->get_progress()->progress();
175         // For the given section, add as many activity tasks as necessary
176         foreach ($info->activities as $activityid => $activity) {
177             if ($activity->sectionid != $infosection->sectionid) {
178                 continue;
179             }
180             if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
181                 self::build_activity_plan($controller, $activityid);
182             } else {
183                 // TODO: Debug information about module not supported
184             }
185         }
186     }
188     /**
189      * Restore one 1-course backup
190      */
191     static protected function build_course_plan($controller, $courseid) {
193         $plan = $controller->get_plan();
194         $info = $controller->get_info();
196         // Add the course task, responsible for restoring
197         // all the course related information
198         $task = restore_factory::get_restore_course_task($info->course, $courseid);
199         $plan->add_task($task);
200         $controller->get_progress()->progress();
202         // For the given course path, add as many block tasks as necessary
203         // TODO: Add blocks, we need to introspect xml here
204         $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
205         foreach ($blocks as $basepath => $name) {
206             if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
207                 $plan->add_task($task);
208                 $controller->get_progress()->progress();
209             } else {
210                 // TODO: Debug information about block not supported
211             }
212         }
214         // For the given course, add as many section tasks as necessary
215         foreach ($info->sections as $sectionid => $section) {
216             self::build_section_plan($controller, $sectionid);
217         }
218     }