3f661835b1e9cc9fb3f70595ed973c6bca5711ed
[moodle.git] / backup / moodle2 / backup_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  * @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  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->dirroot . '/backup/moodle2/backup_root_task.class.php');
28 require_once($CFG->dirroot . '/backup/moodle2/backup_activity_task.class.php');
29 require_once($CFG->dirroot . '/backup/moodle2/backup_section_task.class.php');
30 require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php');
31 require_once($CFG->dirroot . '/backup/moodle2/backup_final_task.class.php');
32 require_once($CFG->dirroot . '/backup/moodle2/backup_block_task.class.php');
33 require_once($CFG->dirroot . '/backup/moodle2/backup_default_block_task.class.php');
34 require_once($CFG->dirroot . '/backup/moodle2/backup_xml_transformer.class.php');
35 require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
36 require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
37 require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
38 require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
39 require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
40 require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
41 require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php');
42 require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php');
43 require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php');
44 require_once($CFG->dirroot . '/backup/moodle2/backup_custom_fields.php');
46 // Load all the activity tasks for moodle2 format
47 $mods = get_plugin_list('mod');
48 foreach ($mods as $mod => $moddir) {
49     $taskpath = $moddir . '/backup/moodle2/backup_' . $mod . '_activity_task.class.php';
50     if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
51         if (file_exists($taskpath)) {
52             require_once($taskpath);
53         }
54     }
55 }
57 // Load all the block tasks for moodle2 format
58 $blocks = get_plugin_list('block');
59 foreach ($blocks as $block => $blockdir) {
60     $taskpath = $blockdir . '/backup/moodle2/backup_' . $block . '_block_task.class.php';
61     if (file_exists($taskpath)) {
62         require_once($taskpath);
63     }
64 }
66 /**
67  * Abstract class defining the static method in charge of building the whole
68  * backup plan, based in @backup_controller preferences.
69  *
70  * TODO: Finish phpdocs
71  */
72 abstract class backup_plan_builder {
74     /**
75      * Dispatches, based on type to specialised builders
76      */
77     static public function build_plan($controller) {
79         $plan = $controller->get_plan();
81         // Add the root task, responsible for storing global settings
82         // and some init tasks
83         $plan->add_task(new backup_root_task('root_task'));
85         switch ($controller->get_type()) {
86             case backup::TYPE_1ACTIVITY:
87                 self::build_activity_plan($controller, $controller->get_id());
88                 break;
89             case backup::TYPE_1SECTION:
90                 self::build_section_plan($controller, $controller->get_id());
91                 break;
92             case backup::TYPE_1COURSE:
93                 self::build_course_plan($controller, $controller->get_id());
94                 break;
95         }
97         // Add the final task, responsible for outputting
98         // all the global xml files (groups, users,
99         // gradebook, questions, roles, files...) and
100         // the main moodle_backup.xml file
101         // and perform other various final actions.
102         $plan->add_task(new backup_final_task('final_task'));
103     }
106     /**
107      * Return one array of supported backup types
108      */
109     static public function supported_backup_types() {
110         return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
111     }
113 // Protected API starts here
115     /**
116      * Build one 1-activity backup
117      */
118     static protected function build_activity_plan($controller, $id) {
120         $plan = $controller->get_plan();
122         // Add the activity task, responsible for outputting
123         // all the module related information
124         $plan->add_task(backup_factory::get_backup_activity_task($controller->get_format(), $id));
126         // For the given activity, add as many block tasks as necessary
127         $blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
128         foreach ($blockids as $blockid) {
129             $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
130         }
131     }
133     /**
134      * Build one 1-section backup
135      */
136     static protected function build_section_plan($controller, $id) {
138         $plan = $controller->get_plan();
140         // Add the section task, responsible for outputting
141         // all the section related information
142         $plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
144         // For the given section, add as many activity tasks as necessary
145         $coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
146         foreach ($coursemodules as $coursemodule) {
147             if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
148                 self::build_activity_plan($controller, $coursemodule->id);
149             } else {
150                 // TODO: Debug information about module not supported
151             }
152         }
153     }
155     /**
156      * Build one 1-course backup
157      */
158     static protected function build_course_plan($controller, $id) {
160         $plan = $controller->get_plan();
162         // Add the course task, responsible for outputting
163         // all the course related information
164         $plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
166         // For the given course, add as many section tasks as necessary
167         $sections = backup_plan_dbops::get_sections_from_courseid($id);
168         foreach ($sections as $section) {
169             self::build_section_plan($controller, $section);
170         }
172         // For the given course, add as many block tasks as necessary
173         $blockids = backup_plan_dbops::get_blockids_from_courseid($id);
174         foreach ($blockids as $blockid) {
175             $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));
176         }
177     }