MDL-49294 logging: Improve cleanup tests
[moodle.git] / backup / moodle2 / backup_plan_builder.class.php
blobc359096e44fabd4e6de15da87d496554c4b7dbff
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.
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 backup_plan_builder class
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
28 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/backup/moodle2/backup_root_task.class.php');
31 require_once($CFG->dirroot . '/backup/moodle2/backup_activity_task.class.php');
32 require_once($CFG->dirroot . '/backup/moodle2/backup_section_task.class.php');
33 require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php');
34 require_once($CFG->dirroot . '/backup/moodle2/backup_final_task.class.php');
35 require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
36 require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
37 require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
38 require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
39 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
40 require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
41 require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
42 require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
43 require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
44 require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
45 require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
46 require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
47 require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
48 require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
49 require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
50 require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
51 require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php');
53 // Load all the activity tasks for moodle2 format
54 $mods = core_component::get_plugin_list('mod');
55 foreach ($mods as $mod => $moddir) {
56 $taskpath = $moddir . '/backup/moodle2/backup_' . $mod . '_activity_task.class.php';
57 if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
58 if (file_exists($taskpath)) {
59 require_once($taskpath);
64 // Load all the block tasks for moodle2 format
65 $blocks = core_component::get_plugin_list('block');
66 foreach ($blocks as $block => $blockdir) {
67 $taskpath = $blockdir . '/backup/moodle2/backup_' . $block . '_block_task.class.php';
68 if (file_exists($taskpath)) {
69 require_once($taskpath);
73 /**
74 * Abstract class defining the static method in charge of building the whole
75 * backup plan, based in @backup_controller preferences.
77 * TODO: Finish phpdocs
79 abstract class backup_plan_builder {
81 /**
82 * Dispatches, based on type to specialised builders
84 static public function build_plan($controller) {
86 $plan = $controller->get_plan();
88 // Add the root task, responsible for storing global settings
89 // and some init tasks
90 $plan->add_task(new backup_root_task('root_task'));
92 switch ($controller->get_type()) {
93 case backup::TYPE_1ACTIVITY:
94 self::build_activity_plan($controller, $controller->get_id());
95 break;
96 case backup::TYPE_1SECTION:
97 self::build_section_plan($controller, $controller->get_id());
98 break;
99 case backup::TYPE_1COURSE:
100 self::build_course_plan($controller, $controller->get_id());
101 break;
104 // Add the final task, responsible for outputting
105 // all the global xml files (groups, users,
106 // gradebook, questions, roles, files...) and
107 // the main moodle_backup.xml file
108 // and perform other various final actions.
109 $plan->add_task(new backup_final_task('final_task'));
114 * Return one array of supported backup types
116 static public function supported_backup_types() {
117 return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
120 // Protected API starts here
123 * Build one 1-activity backup
125 static protected function build_activity_plan($controller, $id) {
127 $plan = $controller->get_plan();
129 // Add the activity task, responsible for outputting
130 // all the module related information
131 try {
132 $plan->add_task(backup_factory::get_backup_activity_task($controller->get_format(), $id));
134 // For the given activity, add as many block tasks as necessary
135 $blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
136 foreach ($blockids as $blockid) {
137 try {
138 $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
139 } catch (backup_task_exception $e) {
140 $a = stdClass();
141 $a->mid = $id;
142 $a->bid = $blockid;
143 $controller->log(get_string('error_block_for_module_not_found', 'backup', $a), backup::LOG_WARNING);
146 } catch (backup_task_exception $e) {
147 $controller->log(get_string('error_course_module_not_found', 'backup', $id), backup::LOG_WARNING);
152 * Build one 1-section backup
154 static protected function build_section_plan($controller, $id) {
156 $plan = $controller->get_plan();
158 // Add the section task, responsible for outputting
159 // all the section related information
160 $plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
162 // For the given section, add as many activity tasks as necessary
163 $coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
164 foreach ($coursemodules as $coursemodule) {
165 if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
166 self::build_activity_plan($controller, $coursemodule->id);
167 } else {
168 // TODO: Debug information about module not supported
174 * Build one 1-course backup
176 static protected function build_course_plan($controller, $id) {
178 $plan = $controller->get_plan();
180 // Add the course task, responsible for outputting
181 // all the course related information
182 $plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
184 // For the given course, add as many section tasks as necessary
185 $sections = backup_plan_dbops::get_sections_from_courseid($id);
186 foreach ($sections as $section) {
187 self::build_section_plan($controller, $section);
190 // For the given course, add as many block tasks as necessary
191 $blockids = backup_plan_dbops::get_blockids_from_courseid($id);
192 foreach ($blockids as $blockid) {
193 $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));