Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Defines backup_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
26
 */
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
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');
54
 
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);
62
        }
63
    }
64
}
65
 
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);
72
    }
73
}
74
 
75
/**
76
 * Abstract class defining the static method in charge of building the whole
77
 * backup plan, based in @backup_controller preferences.
78
 *
79
 * TODO: Finish phpdocs
80
 */
81
abstract class backup_plan_builder {
82
 
83
    /**
84
     * Dispatches, based on type to specialised builders
85
     */
86
    public static function build_plan($controller) {
87
 
88
        $plan = $controller->get_plan();
89
 
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'));
93
 
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;
104
        }
105
 
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'));
112
    }
113
 
114
 
115
    /**
116
     * Return one array of supported backup types
117
     */
118
    public static function supported_backup_types() {
119
        return array(backup::TYPE_1COURSE, backup::TYPE_1SECTION, backup::TYPE_1ACTIVITY);
120
    }
121
 
122
// Protected API starts here
123
 
124
    /**
125
     * Build one 1-activity backup
126
     */
127
    protected static function build_activity_plan($controller, $id) {
128
 
129
        $plan = $controller->get_plan();
130
 
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));
135
 
1441 ariadna 136
            // Some activities may have delegated section integrations.
137
            self::build_delegated_section_plan($controller, $id);
138
 
1 efrain 139
            // For the given activity, add as many block tasks as necessary
140
            $blockids = backup_plan_dbops::get_blockids_from_moduleid($id);
141
            foreach ($blockids as $blockid) {
142
                try {
143
                    $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid, $id));
144
                } catch (backup_task_exception $e) {
145
                    $a = stdClass();
146
                    $a->mid = $id;
147
                    $a->bid = $blockid;
148
                    $controller->log(get_string('error_block_for_module_not_found', 'backup', $a), backup::LOG_WARNING);
149
                }
150
            }
151
        } catch (backup_task_exception $e) {
152
            $controller->log(get_string('error_course_module_not_found', 'backup', $id), backup::LOG_WARNING);
153
        }
154
    }
155
 
156
    /**
1441 ariadna 157
     * Build a course module delegated section backup plan.
158
     * @param backup_controller $controller
159
     * @param int $cmid the parent course module id.
160
     */
161
    protected static function build_delegated_section_plan($controller, $cmid) {
162
        global $CFG, $DB;
163
 
164
        // Check moduleid exists.
165
        if (!$coursemodule = get_coursemodule_from_id(false, $cmid)) {
166
            $controller->log(get_string('error_course_module_not_found', 'backup', $cmid), backup::LOG_WARNING);
167
        }
168
        $classname = 'mod_' . $coursemodule->modname . '\courseformat\sectiondelegate';
169
        if (!class_exists($classname)) {
170
            return;
171
        }
172
        $sectionid = null;
173
        try {
174
            $sectionid = $classname::delegated_section_id($coursemodule);
175
        } catch (dml_exception $error) {
176
            $controller->log(get_string('error_delegate_section_not_found', 'backup', $cmid), backup::LOG_WARNING);
177
            return;
178
        }
179
 
180
        $plan = $controller->get_plan();
181
        $sectiontask = backup_factory::get_backup_section_task($controller->get_format(), $sectionid);
182
        $sectiontask->set_delegated_cm($cmid);
183
        $plan->add_task($sectiontask);
184
 
185
        // For the given section, add as many activity tasks as necessary.
186
        $coursemodules = backup_plan_dbops::get_modules_from_sectionid($sectionid);
187
        foreach ($coursemodules as $coursemodule) {
188
            if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) {
189
                self::build_activity_plan($controller, $coursemodule->id);
190
            }
191
        }
192
    }
193
 
194
    /**
1 efrain 195
     * Build one 1-section backup
196
     */
197
    protected static function build_section_plan($controller, $id) {
198
 
199
        $plan = $controller->get_plan();
200
 
201
        // Add the section task, responsible for outputting
202
        // all the section related information
203
        $plan->add_task(backup_factory::get_backup_section_task($controller->get_format(), $id));
204
 
205
        // For the given section, add as many activity tasks as necessary
206
        $coursemodules = backup_plan_dbops::get_modules_from_sectionid($id);
207
        foreach ($coursemodules as $coursemodule) {
208
            if (plugin_supports('mod', $coursemodule->modname, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
209
                self::build_activity_plan($controller, $coursemodule->id);
210
            } else {
211
                // TODO: Debug information about module not supported
212
            }
213
        }
214
    }
215
 
216
    /**
217
     * Build one 1-course backup
218
     */
219
    protected static function build_course_plan($controller, $id) {
220
 
221
        $plan = $controller->get_plan();
222
 
223
        // Add the course task, responsible for outputting
224
        // all the course related information
225
        $plan->add_task(backup_factory::get_backup_course_task($controller->get_format(), $id));
226
 
227
        // For the given course, add as many section tasks as necessary
228
        $sections = backup_plan_dbops::get_sections_from_courseid($id);
1441 ariadna 229
        foreach ($sections as $sectionid) {
230
            // Delegated sections are not course responsability.
231
            $sectiondata = backup_plan_dbops::get_section_from_id($sectionid);
232
            if (!empty($sectiondata->component)) {
233
                continue;
234
            }
235
            self::build_section_plan($controller, $sectionid);
1 efrain 236
        }
237
 
238
        // For the given course, add as many block tasks as necessary
239
        $blockids = backup_plan_dbops::get_blockids_from_courseid($id);
240
        foreach ($blockids as $blockid) {
241
            $plan->add_task(backup_factory::get_backup_block_task($controller->get_format(), $blockid));
242
        }
243
    }
244
}