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 restore_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/restore_root_task.class.php');
31
require_once($CFG->dirroot . '/backup/moodle2/restore_course_task.class.php');
32
require_once($CFG->dirroot . '/backup/moodle2/restore_section_task.class.php');
33
require_once($CFG->dirroot . '/backup/moodle2/restore_activity_task.class.php');
34
require_once($CFG->dirroot . '/backup/moodle2/restore_final_task.class.php');
35
require_once($CFG->dirroot . '/backup/moodle2/restore_block_task.class.php');
36
require_once($CFG->dirroot . '/backup/moodle2/restore_default_block_task.class.php');
37
require_once($CFG->dirroot . '/backup/moodle2/restore_plugin.class.php');
38
require_once($CFG->dirroot . '/backup/moodle2/restore_qbank_plugin.class.php');
39
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_plugin.class.php');
40
require_once($CFG->dirroot . '/backup/moodle2/restore_qtype_extrafields_plugin.class.php');
41
require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php');
42
require_once($CFG->dirroot . '/backup/moodle2/restore_local_plugin.class.php');
43
require_once($CFG->dirroot . '/backup/moodle2/restore_theme_plugin.class.php');
44
require_once($CFG->dirroot . '/backup/moodle2/restore_report_plugin.class.php');
45
require_once($CFG->dirroot . '/backup/moodle2/restore_coursereport_plugin.class.php');
46
require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.php');
47
require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php');
48
require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php');
49
require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php');
50
require_once($CFG->dirroot . '/backup/moodle2/backup_qbank_plugin.class.php');
51
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php');
52
require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_extrafields_plugin.class.php');
53
require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php');
54
require_once($CFG->dirroot . '/backup/moodle2/backup_local_plugin.class.php');
55
require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php');
56
require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php');
57
require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php');
58
require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php');
59
require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php');
60
require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php');
61
require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php');
62
require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php');
63
require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php');
64
 
65
// Load all the activity tasks for moodle2 format
66
$mods = core_component::get_plugin_list('mod');
67
foreach ($mods as $mod => $moddir) {
68
    $taskpath = $moddir . '/backup/moodle2/restore_' . $mod . '_activity_task.class.php';
69
    if (plugin_supports('mod', $mod, FEATURE_BACKUP_MOODLE2)) {
70
        if (file_exists($taskpath)) {
71
            require_once($taskpath);
72
        }
73
    }
74
}
75
 
76
// Load all the block tasks for moodle2 format
77
$blocks = core_component::get_plugin_list('block');
78
foreach ($blocks as $block => $blockdir) {
79
    $taskpath = $blockdir . '/backup/moodle2/restore_' . $block . '_block_task.class.php';
80
    if (file_exists($taskpath)) {
81
        require_once($taskpath);
82
    }
83
}
84
 
85
/**
86
 * Abstract class defining the static method in charge of building the whole
87
 * restore plan, based in @restore_controller preferences.
88
 *
89
 * TODO: Finish phpdocs
90
 */
91
abstract class restore_plan_builder {
92
 
93
    /**
94
     * Dispatches, based on type to specialised builders
95
     */
96
    public static function build_plan($controller) {
97
 
98
        $plan = $controller->get_plan();
99
 
100
        // Add the root task, responsible for
101
        // preparing everything, creating the
102
        // needed structures (users, roles),
103
        // preloading information to temp table
104
        // and other init tasks
105
        $plan->add_task(new restore_root_task('root_task'));
106
        $controller->get_progress()->progress();
107
 
108
        switch ($controller->get_type()) {
109
            case backup::TYPE_1ACTIVITY:
110
                self::build_activity_plan($controller, key($controller->get_info()->activities));
111
                break;
112
            case backup::TYPE_1SECTION:
113
                self::build_section_plan($controller, key($controller->get_info()->sections));
114
                break;
115
            case backup::TYPE_1COURSE:
116
                self::build_course_plan($controller, $controller->get_courseid());
117
                break;
118
        }
119
 
120
        // Add the final task, responsible for closing
121
        // all the pending bits (remapings, inter-links
122
        // conversion...)
123
        // and perform other various final actions.
124
        $plan->add_task(new restore_final_task('final_task'));
125
        $controller->get_progress()->progress();
126
    }
127
 
128
 
129
// Protected API starts here
130
 
131
    /**
132
     * Restore one 1-activity backup
133
     */
134
    protected static function build_activity_plan($controller, $activityid) {
135
 
136
        $plan = $controller->get_plan();
137
        $info = $controller->get_info();
138
        $infoactivity = $info->activities[$activityid];
139
 
140
        // Add the activity task, responsible for restoring
141
        // all the module related information. So it conditionally
142
        // as far as the module can be missing on restore
143
        if ($task = restore_factory::get_restore_activity_task($infoactivity)) { // can be missing
144
            $plan->add_task($task);
145
            $controller->get_progress()->progress();
146
 
1441 ariadna 147
            // Some activities may have delegated section integrations.
148
            self::build_delegated_section_plan($controller, $infoactivity->moduleid);
149
 
1 efrain 150
            // For the given activity path, add as many block tasks as necessary
151
            // TODO: Add blocks, we need to introspect xml here
152
            $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
153
            foreach ($blocks as $basepath => $name) {
154
                if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
155
                    $plan->add_task($task);
156
                    $controller->get_progress()->progress();
157
                } else {
158
                    // TODO: Debug information about block not supported
159
                }
160
            }
161
        } else { // Activity is missing in target site, inform plan about that
162
            $plan->set_missing_modules();
163
        }
164
 
165
    }
166
 
167
    /**
1441 ariadna 168
     * Build a course module delegated section backup plan.
169
     * @param restore_controller $controller
170
     * @param int $cmid the parent course module id.
171
     */
172
    protected static function build_delegated_section_plan($controller, $cmid) {
173
        $info = $controller->get_info();
174
 
175
        // Find if some section depends on that course module.
176
        $delegatedsectionid = null;
177
        foreach ($info->sections as $sectionid => $section) {
178
            // Delegated sections are not course responsability.
179
            if (isset($section->parentcmid) && $section->parentcmid == $cmid) {
180
                $delegatedsectionid = $sectionid;
181
                break;
182
            }
183
        }
184
 
185
        if (!$delegatedsectionid) {
186
            return;
187
        }
188
        self::build_section_plan($controller, $delegatedsectionid);
189
    }
190
 
191
    /**
1 efrain 192
     * Restore one 1-section backup
193
     */
194
    protected static function build_section_plan($controller, $sectionid) {
195
 
196
        $plan = $controller->get_plan();
197
        $info = $controller->get_info();
198
        $infosection = $info->sections[$sectionid];
199
 
200
        // Add the section task, responsible for restoring
201
        // all the section related information
202
        $plan->add_task(restore_factory::get_restore_section_task($infosection));
203
        $controller->get_progress()->progress();
204
        // For the given section, add as many activity tasks as necessary
205
        foreach ($info->activities as $activityid => $activity) {
206
            if ($activity->sectionid != $infosection->sectionid) {
207
                continue;
208
            }
209
            if (plugin_supports('mod', $activity->modulename, FEATURE_BACKUP_MOODLE2)) { // Check we support the format
210
                self::build_activity_plan($controller, $activityid);
211
            } else {
212
                // TODO: Debug information about module not supported
213
            }
214
        }
215
    }
216
 
217
    /**
218
     * Restore one 1-course backup
219
     */
220
    protected static function build_course_plan($controller, $courseid) {
221
 
222
        $plan = $controller->get_plan();
223
        $info = $controller->get_info();
224
 
225
        // Add the course task, responsible for restoring
226
        // all the course related information
227
        $task = restore_factory::get_restore_course_task($info->course, $courseid);
228
        $plan->add_task($task);
229
        $controller->get_progress()->progress();
230
 
231
        // For the given course path, add as many block tasks as necessary
232
        // TODO: Add blocks, we need to introspect xml here
233
        $blocks = backup_general_helper::get_blocks_from_path($task->get_taskbasepath());
234
        foreach ($blocks as $basepath => $name) {
235
            if ($task = restore_factory::get_restore_block_task($name, $basepath)) {
236
                $plan->add_task($task);
237
                $controller->get_progress()->progress();
238
            } else {
239
                // TODO: Debug information about block not supported
240
            }
241
        }
242
 
243
        // For the given course, add as many section tasks as necessary
244
        foreach ($info->sections as $sectionid => $section) {
1441 ariadna 245
            // Delegated sections are not course responsability.
246
            if (isset($section->parentcmid) && !empty($section->parentcmid)) {
247
                continue;
248
            }
1 efrain 249
            self::build_section_plan($controller, $sectionid);
250
        }
251
    }
252
}