MDL-25617 backup: backup/restore using extra_question_fields
[moodle.git] / backup / moodle2 / restore_plan_builder.class.php
CommitLineData
c9d8234a
EL
1<?php
2
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/>.
17
18/**
f9dab4be
DM
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
c9d8234a
EL
26 */
27
ca2ba065
EL
28defined('MOODLE_INTERNAL') || die();
29
c9d8234a
EL
30require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
31require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
32require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
33require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
34require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
2801883f
EL
35require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
36require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
41941110
EL
37require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
38require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
5a8d683f 39require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_extrafields_plugin.class.php');
4fda584f 40require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
a66e8bb3 41require_once($CFG->dirroot . '/backup/moodle2/restore_local_plugin.class.php');
89213d00 42require_once($CFG->dirroot . '/backup/moodle2/restore_theme_plugin.class.php');
158e9e2a 43require_once($CFG->dirroot . '/backup/moodle2/restore_report_plugin.class.php');
5e0dae12 44require_once($CFG->dirroot . '/backup/moodle2/restore_coursereport_plugin.class.php');
571ae252 45require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.php');
37065c2e 46require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php');
f6199295 47require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php');
41941110
EL
48require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
49require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
5a8d683f 50require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
4fda584f 51require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
a66e8bb3 52require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
89213d00 53require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
158e9e2a 54require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
5e0dae12 55require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
571ae252 56require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
37065c2e 57require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
f6199295 58require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
91d11057 59require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
c9d8234a 60require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
482aac65 61require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
c9d8234a
EL
62
63// Load all the activity tasks for moodle2 format
bd3b3bba 64$mods = core_component::get_plugin_list('mod');
c9d8234a
EL
65foreach ($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}
73
74// Load all the block tasks for moodle2 format
bd3b3bba 75$blocks = core_component::get_plugin_list('block');
c9d8234a
EL
76foreach ($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}
82
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 */
89abstract class restore_plan_builder {
90
91 /**
92 * Dispatches, based on type to specialised builders
93 */
94 static public function build_plan($controller) {
95
96 $plan = $controller->get_plan();
97
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'));
bf2a039e 104 $controller->get_progress()->progress();
c9d8234a
EL
105
106 switch ($controller->get_type()) {
107 case backup::TYPE_1ACTIVITY:
0aa78279 108 self::build_activity_plan($controller, key($controller->get_info()->activities));
c9d8234a
EL
109 break;
110 case backup::TYPE_1SECTION:
0aa78279 111 self::build_section_plan($controller, key($controller->get_info()->sections));
c9d8234a
EL
112 break;
113 case backup::TYPE_1COURSE:
114 self::build_course_plan($controller, $controller->get_courseid());
115 break;
116 }
117
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'));
bf2a039e 123 $controller->get_progress()->progress();
c9d8234a
EL
124 }
125
126
127// Protected API starts here
128
129 /**
130 * Restore one 1-activity backup
131 */
132 static protected function build_activity_plan($controller, $activityid) {
133
134 $plan = $controller->get_plan();
135 $info = $controller->get_info();
136 $infoactivity = $info->activities[$activityid];
137
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);
bf2a039e 143 $controller->get_progress()->progress();
c9d8234a 144
2801883f
EL
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)) {
ca2ba065 150 $plan->add_task($task);
bf2a039e 151 $controller->get_progress()->progress();
2801883f
EL
152 } else {
153 // TODO: Debug information about block not supported
154 }
155 }
58328ce8
EL
156 } else { // Activity is missing in target site, inform plan about that
157 $plan->set_missing_modules();
2801883f 158 }
58328ce8 159
c9d8234a
EL
160 }
161
162 /**
163 * Restore one 1-section backup
164 */
165 static protected function build_section_plan($controller, $sectionid) {
166
167 $plan = $controller->get_plan();
168 $info = $controller->get_info();
169 $infosection = $info->sections[$sectionid];
170
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));
bf2a039e 174 $controller->get_progress()->progress();
c9d8234a
EL
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 }
187
188 /**
189 * Restore one 1-course backup
190 */
191 static protected function build_course_plan($controller, $courseid) {
192
193 $plan = $controller->get_plan();
194 $info = $controller->get_info();
195
196 // Add the course task, responsible for restoring
197 // all the course related information
2801883f
EL
198 $task = restore_factory::get_restore_course_task($info->course, $courseid);
199 $plan->add_task($task);
bf2a039e 200 $controller->get_progress()->progress();
c9d8234a 201
2801883f 202 // For the given course path, add as many block tasks as necessary
c9d8234a 203 // TODO: Add blocks, we need to introspect xml here
2801883f
EL
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);
bf2a039e 208 $controller->get_progress()->progress();
2801883f
EL
209 } else {
210 // TODO: Debug information about block not supported
211 }
212 }
fbd74137
EL
213
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 }
c9d8234a
EL
218 }
219}