MDL-22245 backup - added support for subplugins in activities
[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/**
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 */
24
ca2ba065
EL
25defined('MOODLE_INTERNAL') || die();
26
c9d8234a
EL
27require_once($CFG->dirroot . '/backup/moodle2/restore_root_task.class.php');
28require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
29require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
30require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
31require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
2801883f
EL
32require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
33require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
91d11057 34require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
c9d8234a 35require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
482aac65 36require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
c9d8234a
EL
37
38// Load all the activity tasks for moodle2 format
39$mods = get_plugin_list('mod');
40foreach ($mods as $mod => $moddir) {
41 $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
42 if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
43 if (file_exists($taskpath)) {
44 require_once($taskpath);
45 }
46 }
47}
48
49// Load all the block tasks for moodle2 format
50$blocks = get_plugin_list('block');
51foreach ($blocks as $block => $blockdir) {
52 $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
53 if (file_exists($taskpath)) {
54 require_once($taskpath);
55 }
56}
57
58/**
59 * Abstract class defining the static method in charge of building the whole
60 * restore plan, based in @restore_controller preferences.
61 *
62 * TODO: Finish phpdocs
63 */
64abstract class restore_plan_builder {
65
66 /**
67 * Dispatches, based on type to specialised builders
68 */
69 static public function build_plan($controller) {
70
71 $plan = $controller->get_plan();
72
73 // Add the root task, responsible for
74 // preparing everything, creating the
75 // needed structures (users, roles),
76 // preloading information to temp table
77 // and other init tasks
78 $plan->add_task(new restore_root_task('root_task'));
79
80 switch ($controller->get_type()) {
81 case backup::TYPE_1ACTIVITY:
0aa78279 82 self::build_activity_plan($controller, key($controller->get_info()->activities));
c9d8234a
EL
83 break;
84 case backup::TYPE_1SECTION:
0aa78279 85 self::build_section_plan($controller, key($controller->get_info()->sections));
c9d8234a
EL
86 break;
87 case backup::TYPE_1COURSE:
88 self::build_course_plan($controller, $controller->get_courseid());
89 break;
90 }
91
92 // Add the final task, responsible for closing
93 // all the pending bits (remapings, inter-links
94 // conversion...)
95 // and perform other various final actions.
96 $plan->add_task(new restore_final_task('final_task'));
97 }
98
99
100// Protected API starts here
101
102 /**
103 * Restore one 1-activity backup
104 */
105 static protected function build_activity_plan($controller, $activityid) {
106
107 $plan = $controller->get_plan();
108 $info = $controller->get_info();
109 $infoactivity = $info->activities[$activityid];
110
111 // Add the activity task, responsible for restoring
112 // all the module related information. So it conditionally
113 // as far as the module can be missing on restore
114 if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
115 $plan->add_task($task);
c9d8234a 116
2801883f
EL
117 // For the given activity path, add as many block tasks as necessary
118 // TODO: Add blocks, we need to introspect xml here
119 $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
120 foreach ($blocks as $basepath => $name) {
121 if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
ca2ba065 122 $plan->add_task($task);
2801883f
EL
123 } else {
124 // TODO: Debug information about block not supported
125 }
126 }
127 }
c9d8234a
EL
128 }
129
130 /**
131 * Restore one 1-section backup
132 */
133 static protected function build_section_plan($controller, $sectionid) {
134
135 $plan = $controller->get_plan();
136 $info = $controller->get_info();
137 $infosection = $info->sections[$sectionid];
138
139 // Add the section task, responsible for restoring
140 // all the section related information
141 $plan->add_task(restore_factory::get_restore_section_task($infosection));
142 // For the given section, add as many activity tasks as necessary
143 foreach ($info->activities as $activityid => $activity) {
144 if ($activity->sectionid != $infosection->sectionid) {
145 continue;
146 }
147 if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
148 self::build_activity_plan($controller, $activityid);
149 } else {
150 // TODO: Debug information about module not supported
151 }
152 }
153 }
154
155 /**
156 * Restore one 1-course backup
157 */
158 static protected function build_course_plan($controller, $courseid) {
159
160 $plan = $controller->get_plan();
161 $info = $controller->get_info();
162
163 // Add the course task, responsible for restoring
164 // all the course related information
2801883f
EL
165 $task = restore_factory::get_restore_course_task($info->course, $courseid);
166 $plan->add_task($task);
c9d8234a 167
2801883f 168 // For the given course path, add as many block tasks as necessary
c9d8234a 169 // TODO: Add blocks, we need to introspect xml here
2801883f
EL
170 $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
171 foreach ($blocks as $basepath => $name) {
172 if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
173 $plan->add_task($task);
174 } else {
175 // TODO: Debug information about block not supported
176 }
177 }
fbd74137
EL
178
179 // For the given course, add as many section tasks as necessary
180 foreach ($info->sections as $sectionid => $section) {
181 self::build_section_plan($controller, $sectionid);
182 }
c9d8234a
EL
183 }
184}