MDL-80268 badges: require capability to view awarded badges.
[moodle.git] / backup / restore.php
blobe6edbb4efbc8509a28dcc0641ccd9f1aec40faea
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * This script is used to configure and execute the restore proccess.
20 * @package core
21 * @subpackage backup
22 * @copyright Moodle
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('NO_OUTPUT_BUFFERING', true);
28 require_once('../config.php');
29 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
31 // Restore of large courses requires extra memory. Use the amount configured
32 // in admin settings.
33 raise_memory_limit(MEMORY_EXTRA);
35 $contextid = required_param('contextid', PARAM_INT);
36 $stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);
37 $cancel = optional_param('cancel', '', PARAM_ALPHA);
39 // Determine if we are performing realtime for asynchronous backups.
40 $backupmode = backup::MODE_GENERAL;
41 if (async_helper::is_async_enabled()) {
42 $backupmode = backup::MODE_ASYNC;
45 list($context, $course, $cm) = get_context_info_array($contextid);
47 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
48 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
49 $PAGE->set_context($context);
50 $PAGE->set_pagelayout('admin');
51 $PAGE->set_secondary_active_tab('coursereuse');
53 require_login($course, null, $cm);
54 require_capability('moodle/restore:restorecourse', $context);
56 $PAGE->secondarynav->set_overflow_selected_node('restore');
58 if (is_null($course)) {
59 $coursefullname = $SITE->fullname;
60 $courseshortname = $SITE->shortname;
61 $courseurl = new moodle_url('/');
62 } else {
63 $coursefullname = $course->fullname;
64 $courseshortname = $course->shortname;
65 $courseurl = course_get_url($course->id);
68 // Show page header.
69 $PAGE->set_title($courseshortname . ': ' . get_string('restore'));
70 $PAGE->set_heading($coursefullname);
72 $renderer = $PAGE->get_renderer('core','backup');
73 if (empty($cancel)) {
74 // Do not print the header if user cancelled the process, as we are going to redirect the user.
75 echo $OUTPUT->header();
78 // Prepare a progress bar which can display optionally during long-running
79 // operations while setting up the UI.
80 $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
82 // Overall, allow 10 units of progress.
83 $slowprogress->start_progress('', 10);
85 // This progress section counts for loading the restore controller.
86 $slowprogress->start_progress('', 1, 1);
88 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
89 $restore = restore_ui::engage_independent_stage($stage, $contextid);
90 } else {
91 $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
92 $rc = restore_ui::load_controller($restoreid);
93 if (!$rc) {
94 $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
95 if ($restore->process()) {
96 $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
97 $backupmode, $USER->id, $restore->get_target(), null, backup::RELEASESESSION_YES);
100 if ($rc) {
101 // check if the format conversion must happen first
102 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
103 $rc->convert();
106 $restore = new restore_ui($rc, array('contextid'=>$context->id));
110 // End progress section for loading restore controller.
111 $slowprogress->end_progress();
113 // This progress section is for the 'process' function below.
114 $slowprogress->start_progress('', 1, 9);
116 // Depending on the code branch above, $restore may be a restore_ui or it may
117 // be a restore_ui_independent_stage. Either way, this function exists.
118 $restore->set_progress_reporter($slowprogress);
119 $outcome = $restore->process();
121 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
122 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
125 $loghtml = '';
126 // Finish the 'process' progress reporting section, and the overall count.
127 $slowprogress->end_progress();
128 $slowprogress->end_progress();
130 if (!$restore->is_independent()) {
131 // Use a temporary (disappearing) progress bar to show the precheck progress if any.
132 $precheckprogress = new \core\progress\display_if_slow(get_string('preparingdata', 'backup'));
133 $restore->get_controller()->set_progress($precheckprogress);
134 if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage() && $backupmode != backup::MODE_ASYNC) {
135 try {
136 // Div used to hide the 'progress' step once the page gets onto 'finished'.
137 echo html_writer::start_div('', array('id' => 'executionprogress'));
138 // Show the current restore state (header with bolded item).
139 echo $renderer->progress_bar($restore->get_progress_bar());
140 // Start displaying the actual progress bar percentage.
141 $restore->get_controller()->set_progress(new \core\progress\display());
142 // Prepare logger.
143 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
144 $restore->get_controller()->add_logger($logger);
145 // Do actual restore.
146 $restore->execute();
147 // Get HTML from logger.
148 if ($CFG->debugdisplay) {
149 $loghtml = $logger->get_html();
151 // Hide this section because we are now going to make the page show 'finished'.
152 echo html_writer::end_div();
153 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
154 } catch(Exception $e) {
155 $restore->cleanup();
156 throw $e;
158 } else {
159 $restore->save_controller();
163 echo $renderer->progress_bar($restore->get_progress_bar());
165 if ($restore->get_stage() != restore_ui::STAGE_PROCESS) {
166 echo $restore->display($renderer);
167 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS && $restore->requires_substage()) {
168 echo $restore->display($renderer);
169 } else if ($restore->get_stage() == restore_ui::STAGE_PROCESS
170 && !$restore->requires_substage()
171 && $backupmode == backup::MODE_ASYNC) {
172 // Asynchronous restore.
173 // Create adhoc task for restore.
174 $restoreid = $restore->get_restoreid();
175 $asynctask = new \core\task\asynchronous_restore_task();
176 $asynctask->set_blocking(false);
177 $asynctask->set_userid($USER->id);
178 $asynctask->set_custom_data(array('backupid' => $restoreid));
179 \core\task\manager::queue_adhoc_task($asynctask);
181 // Add ajax progress bar and initiate ajax via a template.
182 $restoreurl = new moodle_url('/backup/restorefile.php', array('contextid' => $contextid));
183 $progresssetup = array(
184 'backupid' => $restoreid,
185 'contextid' => $contextid,
186 'courseurl' => $courseurl->out(),
187 'restoreurl' => $restoreurl->out()
189 echo $renderer->render_from_template('core/async_backup_status', $progresssetup);
192 $restore->destroy();
193 unset($restore);
195 // Display log data if there was any.
196 if ($loghtml != '') {
197 echo $renderer->log_display($loghtml);
200 echo $OUTPUT->footer();