Merge branch 'MDL-69690-401' of https://github.com/ilyatregubov/moodle into MOODLE_40...
[moodle.git] / backup / moodle2 / backup_plan_builder.class.php
blobeae28a80c56fe91d8ac6d74ffac7654a88fbea5b
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_qbank_plugin.class.php');
40 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
41 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
42 require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
43 require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
44 require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
45 require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
46 require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
47 require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
48 require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
49 require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
50 require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
51 require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
52 require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
53 require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php');
55 // Load all the activity tasks for moodle2 format
56 $mods = core_component::get_plugin_list('mod');
57 foreach ($mods as $mod => $moddir) {
58 $taskpath = $moddir . '/backup/moodle2/backup_' . $mod . '_activity_task.class.php';
59 if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
60 if (file_exists($taskpath)) {
61 require_once($taskpath);
66 // Load all the block tasks for moodle2 format
67 $blocks = core_component::get_plugin_list('block');
68 foreach ($blocks as $block => $blockdir) {
69 $taskpath = $blockdir . '/backup/moodle2/backup_' . $block . '_block_task.class.php';
70 if (file_exists($taskpath)) {
71 require_once($taskpath);
75 /**
76 * Abstract class defining the static method in charge of building the whole
77 * backup plan, based in @backup_controller preferences.
79 * TODO: Finish phpdocs
81 abstract class backup_plan_builder {
83 /**
84 * Dispatches, based on type to specialised builders
86 static public function build_plan($controller) {
88 $plan = $controller->get_plan();
90 // Add the root task, responsible for storing global settings
91 // and some init tasks
92 $plan->add_task(new backup_root_task('root_task'));
94 switch ($controller->get_type()) {
95 case backup::TYPE_1ACTIVITY:
96 self::build_activity_plan($controller, $controller->get_id());
97 break;
98 case backup::TYPE_1SECTION:
99 self::build_section_plan($controller, $controller->get_id());
100 break;
101 case backup::TYPE_1COURSE:
102 self::build_course_plan($controller, $controller->get_id());
103 break;
106 // Add the final task, responsible for outputting
107 // all the global xml files (groups, users,
108 // gradebook, questions, roles, files...) and
109 // the main moodle_backup.xml file
110 // and perform other various final actions.
111 $plan->add_task(new backup_final_task('final_task'));
116 * Return one array of supported backup types
118 static public function supported_backup_types() {
119 return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
122 // Protected API starts here
125 * Build one 1-activity backup
127 static protected function build_activity_plan($controller, $id) {
129 $plan = $controller->get_plan();
131 // Add the activity task, responsible for outputting
132 // all the module related information
133 try {
134 $plan->add_task(backup_factory::get_backup_activity_task($controller->get_format(), $id));
136 // For the given activity, add as many block tasks as necessary
137 $blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
138 foreach ($blockids as $blockid) {
139 try {
140 $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
141 } catch (backup_task_exception $e) {
142 $a = stdClass();
143 $a->mid = $id;
144 $a->bid = $blockid;
145 $controller->log(get_string('error_block_for_module_not_found', 'backup', $a), backup::LOG_WARNING);
148 } catch (backup_task_exception $e) {
149 $controller->log(get_string('error_course_module_not_found', 'backup', $id), backup::LOG_WARNING);
154 * Build one 1-section backup
156 static protected function build_section_plan($controller, $id) {
158 $plan = $controller->get_plan();
160 // Add the section task, responsible for outputting
161 // all the section related information
162 $plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
164 // For the given section, add as many activity tasks as necessary
165 $coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
166 foreach ($coursemodules as $coursemodule) {
167 if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
168 self::build_activity_plan($controller, $coursemodule->id);
169 } else {
170 // TODO: Debug information about module not supported
176 * Build one 1-course backup
178 static protected function build_course_plan($controller, $id) {
180 $plan = $controller->get_plan();
182 // Add the course task, responsible for outputting
183 // all the course related information
184 $plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
186 // For the given course, add as many section tasks as necessary
187 $sections = backup_plan_dbops::get_sections_from_courseid($id);
188 foreach ($sections as $section) {
189 self::build_section_plan($controller, $section);
192 // For the given course, add as many block tasks as necessary
193 $blockids = backup_plan_dbops::get_blockids_from_courseid($id);
194 foreach ($blockids as $blockid) {
195 $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));