Merge branch 'MDL-62572-35' of git://github.com/damyon/moodle into MOODLE_35_STABLE
[moodle.git] / backup / backup.php
blobc1b1f648f509e6818c28a6deb3347b2f4f6faf9e
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 * This script is used to configure and execute the backup proccess.
21 * @package core
22 * @subpackage backup
23 * @copyright Moodle
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 define('NO_OUTPUT_BUFFERING', true);
29 require_once('../config.php');
30 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
31 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
34 $courseid = required_param('id', PARAM_INT);
35 $sectionid = optional_param('section', null, PARAM_INT);
36 $cmid = optional_param('cm', null, PARAM_INT);
37 $cancel = optional_param('cancel', '', PARAM_ALPHA);
38 /**
39 * Part of the forms in stages after initial, is POST never GET
41 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
43 $url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
44 if ($sectionid !== null) {
45 $url->param('section', $sectionid);
47 if ($cmid !== null) {
48 $url->param('cm', $cmid);
50 $PAGE->set_url($url);
51 $PAGE->set_pagelayout('admin');
53 $id = $courseid;
54 $cm = null;
55 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
56 $type = backup::TYPE_1COURSE;
57 if (!is_null($sectionid)) {
58 $section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$sectionid), '*', MUST_EXIST);
59 $type = backup::TYPE_1SECTION;
60 $id = $sectionid;
62 if (!is_null($cmid)) {
63 $cm = get_coursemodule_from_id(null, $cmid, $course->id, false, MUST_EXIST);
64 $type = backup::TYPE_1ACTIVITY;
65 $id = $cmid;
67 require_login($course, false, $cm);
69 switch ($type) {
70 case backup::TYPE_1COURSE :
71 require_capability('moodle/backup:backupcourse', context_course::instance($course->id));
72 $heading = get_string('backupcourse', 'backup', $course->shortname);
73 break;
74 case backup::TYPE_1SECTION :
75 $coursecontext = context_course::instance($course->id);
76 require_capability('moodle/backup:backupsection', $coursecontext);
77 if ((string)$section->name !== '') {
78 $sectionname = format_string($section->name, true, array('context' => $coursecontext));
79 $heading = get_string('backupsection', 'backup', $sectionname);
80 $PAGE->navbar->add($sectionname);
81 } else {
82 $heading = get_string('backupsection', 'backup', $section->section);
83 $PAGE->navbar->add(get_string('section').' '.$section->section);
85 break;
86 case backup::TYPE_1ACTIVITY :
87 require_capability('moodle/backup:backupactivity', context_module::instance($cm->id));
88 $heading = get_string('backupactivity', 'backup', $cm->name);
89 break;
90 default :
91 print_error('unknownbackuptype');
94 // Backup of large courses requires extra memory. Use the amount configured
95 // in admin settings.
96 raise_memory_limit(MEMORY_EXTRA);
98 if (!($bc = backup_ui::load_controller($backupid))) {
99 $bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
100 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
102 $backup = new backup_ui($bc);
104 $PAGE->set_title($heading);
105 $PAGE->set_heading($heading);
107 $renderer = $PAGE->get_renderer('core','backup');
108 if (empty($cancel)) {
109 // Do not print the header if user cancelled the process, as we are going to redirect the user.
110 echo $OUTPUT->header();
113 // Prepare a progress bar which can display optionally during long-running
114 // operations while setting up the UI.
115 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
117 $previous = optional_param('previous', false, PARAM_BOOL);
118 if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) {
119 // After schema stage, we are probably going to get to the confirmation stage,
120 // The confirmation stage has 2 sets of progress, so this is needed to prevent
121 // it showing 2 progress bars.
122 $twobars = true;
123 $slowprogress->start_progress('', 2);
124 } else {
125 $twobars = false;
127 $backup->get_controller()->set_progress($slowprogress);
128 $backup->process();
130 if ($backup->enforce_changed_dependencies()) {
131 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
134 $loghtml = '';
135 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
136 // Display an extra backup step bar so that we can show the 'processing' step first.
137 echo html_writer::start_div('', array('id' => 'executionprogress'));
138 echo $renderer->progress_bar($backup->get_progress_bar());
139 $backup->get_controller()->set_progress(new \core\progress\display());
141 // Prepare logger and add to end of chain.
142 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
143 $backup->get_controller()->add_logger($logger);
145 // Carry out actual backup.
146 $backup->execute();
148 // Backup controller gets saved/loaded so the logger object changes and we
149 // have to retrieve it.
150 $logger = $backup->get_controller()->get_logger();
151 while (!is_a($logger, 'core_backup_html_logger')) {
152 $logger = $logger->get_next();
155 // Get HTML from logger.
156 if ($CFG->debugdisplay) {
157 $loghtml = $logger->get_html();
160 // Hide the progress display and first backup step bar (the 'finished' step will show next).
161 echo html_writer::end_div();
162 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
163 } else {
164 $backup->save_controller();
167 // Displaying UI can require progress reporting, so do it here before outputting
168 // the backup stage bar (as part of the existing progress bar, if required).
169 $ui = $backup->display($renderer);
170 if ($twobars) {
171 $slowprogress->end_progress();
174 echo $renderer->progress_bar($backup->get_progress_bar());
176 echo $ui;
177 $backup->destroy();
178 unset($backup);
180 // Display log data if there was any.
181 if ($loghtml != '') {
182 echo $renderer->log_display($loghtml);
185 echo $OUTPUT->footer();