MDL-63854 competencies, themes: misplaced dropdown arrows
[moodle.git] / backup / import.php
blob4cc57345f1e0541b062fa7698c348cf640cf9dfc
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 import 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 both the backup and restore libs
30 require_once('../config.php');
31 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
32 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
33 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
34 require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php');
36 // The courseid we are importing to
37 $courseid = required_param('id', PARAM_INT);
38 // The id of the course we are importing FROM (will only be set if past first stage
39 $importcourseid = optional_param('importid', false, PARAM_INT);
40 // We just want to check if a search has been run. True if anything is there.
41 $searchcourses = optional_param('searchcourses', false, PARAM_BOOL);
42 // The target method for the restore (adding or deleting)
43 $restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
45 // Load the course and context
46 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
47 $context = context_course::instance($courseid);
49 // Must pass login
50 require_login($course);
51 // Must hold restoretargetimport in the current course
52 require_capability('moodle/restore:restoretargetimport', $context);
54 // Set up the page
55 $PAGE->set_title($course->shortname . ': ' . get_string('import'));
56 $PAGE->set_heading($course->fullname);
57 $PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
58 $PAGE->set_context($context);
59 $PAGE->set_pagelayout('incourse');
61 // Prepare the backup renderer
62 $renderer = $PAGE->get_renderer('core','backup');
64 // Check if we already have a import course id
65 if ($importcourseid === false || $searchcourses) {
66 // Obviously not... show the selector so one can be chosen
67 $url = new moodle_url('/backup/import.php', array('id'=>$courseid));
68 $search = new import_course_search(array('url'=>$url));
70 // show the course selector
71 echo $OUTPUT->header();
72 echo $renderer->import_course_selector($url, $search);
73 echo $OUTPUT->footer();
74 die();
77 // Load the course +context to import from
78 $importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
79 $importcontext = context_course::instance($importcourseid);
81 // Make sure the user can backup from that course
82 require_capability('moodle/backup:backuptargetimport', $importcontext);
84 // Attempt to load the existing backup controller (backupid will be false if there isn't one)
85 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
86 if (!($bc = backup_ui::load_controller($backupid))) {
87 $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
88 backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
89 $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
90 $settings = $bc->get_plan()->get_settings();
92 // For the initial stage we want to hide all locked settings and if there are
93 // no visible settings move to the next stage
94 $visiblesettings = false;
95 foreach ($settings as $setting) {
96 if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
97 $setting->set_visibility(backup_setting::HIDDEN);
98 } else {
99 $visiblesettings = true;
102 import_ui::skip_current_stage(!$visiblesettings);
105 // Prepare the import UI
106 $backup = new import_ui($bc, array('importid'=>$importcourse->id, 'target'=>$restoretarget));
107 // Process the current stage
108 $backup->process();
110 // If this is the confirmation stage remove the filename setting
111 if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
112 $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
115 // If it's the final stage process the import
116 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
117 echo $OUTPUT->header();
119 // Display an extra progress bar so that we can show the current stage.
120 echo html_writer::start_div('', array('id' => 'executionprogress'));
121 echo $renderer->progress_bar($backup->get_progress_bar());
123 // Start the progress display - we split into 2 chunks for backup and restore.
124 $progress = new \core\progress\display();
125 $progress->start_progress('', 2);
126 $backup->get_controller()->set_progress($progress);
128 // Prepare logger for backup.
129 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
130 $backup->get_controller()->add_logger($logger);
132 // First execute the backup
133 $backup->execute();
134 $backup->destroy();
135 unset($backup);
137 // Note that we've done that progress.
138 $progress->progress(1);
140 // Check whether the backup directory still exists. If missing, something
141 // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
142 // backups don't store resulting files ever
143 $tempdestination = make_backup_temp_directory($backupid, false);
144 if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
145 print_error('unknownbackupexporterror'); // shouldn't happen ever
148 // Prepare the restore controller. We don't need a UI here as we will just use what
149 // ever the restore has (the user has just chosen).
150 $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
152 // Start a progress section for the restore, which will consist of 2 steps
153 // (the precheck and then the actual restore).
154 $progress->start_progress('Restore process', 2);
155 $rc->set_progress($progress);
157 // Set logger for restore.
158 $rc->add_logger($logger);
160 // Convert the backup if required.... it should NEVER happed
161 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
162 $rc->convert();
164 // Mark the UI finished.
165 $rc->finish_ui();
166 // Execute prechecks
167 $warnings = false;
168 if (!$rc->execute_precheck()) {
169 $precheckresults = $rc->get_precheck_results();
170 if (is_array($precheckresults)) {
171 if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
172 fulldelete($tempdestination);
174 echo $renderer->precheck_notices($precheckresults);
175 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
176 echo $OUTPUT->footer();
177 die();
179 if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
180 $warnings = $precheckresults['warnings'];
184 if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
185 restore_dbops::delete_course_content($course->id);
187 // Execute the restore.
188 $rc->execute_plan();
190 // Delete the temp directory now
191 fulldelete($tempdestination);
193 // End restore section of progress tracking (restore/precheck).
194 $progress->end_progress();
196 // All progress complete. Hide progress area.
197 $progress->end_progress();
198 echo html_writer::end_div();
199 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
201 // Display a notification and a continue button
202 if ($warnings) {
203 echo $OUTPUT->box_start();
204 echo $OUTPUT->notification(get_string('warning'), 'notifyproblem');
205 echo html_writer::start_tag('ul', array('class'=>'list'));
206 foreach ($warnings as $warning) {
207 echo html_writer::tag('li', $warning);
209 echo html_writer::end_tag('ul');
210 echo $OUTPUT->box_end();
212 echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
213 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
215 // Get and display log data if there was any.
216 $loghtml = $logger->get_html();
217 if ($loghtml != '') {
218 echo $renderer->log_display($loghtml);
221 echo $OUTPUT->footer();
223 die();
225 } else {
226 // Otherwise save the controller and progress
227 $backup->save_controller();
230 // Display the current stage
231 echo $OUTPUT->header();
232 if ($backup->enforce_changed_dependencies()) {
233 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
235 echo $renderer->progress_bar($backup->get_progress_bar());
236 echo $backup->display($renderer);
237 $backup->destroy();
238 unset($backup);
239 echo $OUTPUT->footer();