weekly release 2.0.5+
[moodle.git] / backup / backup.php
blob7d3170d9684911077b3768a356618f6d8624e439
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 require_once('../config.php');
19 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
20 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
23 $courseid = required_param('id', PARAM_INT);
24 $sectionid = optional_param('section', null, PARAM_INT);
25 $cmid = optional_param('cm', null, PARAM_INT);
26 /**
27 * Part of the forms in stages after initial, is POST never GET
29 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
31 $url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
32 if ($sectionid !== null) {
33 $url->param('section', $sectionid);
35 if ($cmid !== null) {
36 $url->param('cm', $cmid);
38 $PAGE->set_url($url);
39 $PAGE->set_pagelayout('admin');
41 $id = $courseid;
42 $cm = null;
43 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
44 $type = backup::TYPE_1COURSE;
45 if (!is_null($sectionid)) {
46 $section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$sectionid), '*', MUST_EXIST);
47 $type = backup::TYPE_1SECTION;
48 $id = $sectionid;
50 if (!is_null($cmid)) {
51 $cm = get_coursemodule_from_id(null, $cmid, $course->id, false, MUST_EXIST);
52 $type = backup::TYPE_1ACTIVITY;
53 $id = $cmid;
55 require_login($course, false, $cm);
57 switch ($type) {
58 case backup::TYPE_1COURSE :
59 require_capability('moodle/backup:backupcourse', get_context_instance(CONTEXT_COURSE, $course->id));
60 $heading = get_string('backupcourse', 'backup', $course->shortname);
61 break;
62 case backup::TYPE_1SECTION :
63 require_capability('moodle/backup:backupsection', get_context_instance(CONTEXT_COURSE, $course->id));
64 if (!empty($section->name)) {
65 $heading = get_string('backupsection', 'backup', $section->name);
66 $PAGE->navbar->add($section->name);
67 } else {
68 $heading = get_string('backupsection', 'backup', $section->section);
69 $PAGE->navbar->add(get_string('section').' '.$section->section);
71 break;
72 case backup::TYPE_1ACTIVITY :
73 require_capability('moodle/backup:backupactivity', get_context_instance(CONTEXT_MODULE, $cm->id));
74 $heading = get_string('backupactivity', 'backup', $cm->name);
75 break;
76 default :
77 print_error('unknownbackuptype');
80 if (!($bc = backup_ui::load_controller($backupid))) {
81 $bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
82 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
84 $backup = new backup_ui($bc);
85 $backup->process();
86 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
87 $backup->execute();
88 } else {
89 $backup->save_controller();
92 $PAGE->set_title($heading.': '.$backup->get_stage_name());
93 $PAGE->set_heading($heading);
94 $PAGE->navbar->add($backup->get_stage_name());
96 $renderer = $PAGE->get_renderer('core','backup');
97 echo $OUTPUT->header();
98 if ($backup->enforce_changed_dependencies()) {
99 echo $renderer->dependency_notification(get_string('dependenciesenforced','backup'));
101 echo $renderer->progress_bar($backup->get_progress_bar());
102 echo $backup->display();
103 $backup->destroy();
104 unset($backup);
105 echo $OUTPUT->footer();