Merge branch 'w39_MDL-41208_m26_yui312' of https://github.com/skodak/moodle
[moodle.git] / backup / import.php
blob0bb9e6a2a7c274546b2817828a9f2714346677ee
1 <?php
3 // Require both the backup and restore libs
4 require_once('../config.php');
5 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
6 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
7 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
8 require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php');
10 // The courseid we are importing to
11 $courseid = required_param('id', PARAM_INT);
12 // The id of the course we are importing FROM (will only be set if past first stage
13 $importcourseid = optional_param('importid', false, PARAM_INT);
14 // We just want to check if a search has been run. True if anything is there.
15 $searchcourses = optional_param('searchcourses', false, PARAM_BOOL);
16 // The target method for the restore (adding or deleting)
17 $restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
19 // Load the course and context
20 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
21 $context = context_course::instance($courseid);
23 // Must pass login
24 require_login($course);
25 // Must hold restoretargetimport in the current course
26 require_capability('moodle/restore:restoretargetimport', $context);
28 // Set up the page
29 $PAGE->set_title($course->shortname . ': ' . get_string('import'));
30 $PAGE->set_heading($course->fullname);
31 $PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
32 $PAGE->set_context($context);
33 $PAGE->set_pagelayout('incourse');
35 // Prepare the backup renderer
36 $renderer = $PAGE->get_renderer('core','backup');
38 // Check if we already have a import course id
39 if ($importcourseid === false || $searchcourses) {
40 // Obviously not... show the selector so one can be chosen
41 $url = new moodle_url('/backup/import.php', array('id'=>$courseid));
42 $search = new import_course_search(array('url'=>$url));
44 // show the course selector
45 echo $OUTPUT->header();
46 echo $renderer->import_course_selector($url, $search);
47 echo $OUTPUT->footer();
48 die();
51 // Load the course +context to import from
52 $importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
53 $importcontext = context_course::instance($importcourseid);
55 // Make sure the user can backup from that course
56 require_capability('moodle/backup:backuptargetimport', $importcontext);
58 // Attempt to load the existing backup controller (backupid will be false if there isn't one)
59 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
60 if (!($bc = backup_ui::load_controller($backupid))) {
61 $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
62 backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
63 $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
64 $settings = $bc->get_plan()->get_settings();
66 // For the initial stage we want to hide all locked settings and if there are
67 // no visible settings move to the next stage
68 $visiblesettings = false;
69 foreach ($settings as $setting) {
70 if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
71 $setting->set_visibility(backup_setting::HIDDEN);
72 } else {
73 $visiblesettings = true;
76 import_ui::skip_current_stage(!$visiblesettings);
79 // Prepare the import UI
80 $backup = new import_ui($bc, array('importid'=>$importcourse->id, 'target'=>$restoretarget));
81 // Process the current stage
82 $backup->process();
84 // If this is the confirmation stage remove the filename setting
85 if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
86 $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
89 // If it's the final stage process the import
90 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
91 echo $OUTPUT->header();
93 // Display an extra progress bar so that we can show the current stage.
94 echo html_writer::start_div('', array('id' => 'executionprogress'));
95 echo $renderer->progress_bar($backup->get_progress_bar());
97 // Start the progress display - we split into 2 chunks for backup and restore.
98 $progress = new core_backup_display_progress();
99 $progress->start_progress('', 2);
100 $backup->get_controller()->set_progress($progress);
102 // First execute the backup
103 $backup->execute();
104 $backup->destroy();
105 unset($backup);
107 // Note that we've done that progress.
108 $progress->progress(1);
110 // Check whether the backup directory still exists. If missing, something
111 // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
112 // backups don't store resulting files ever
113 $tempdestination = $CFG->tempdir . '/backup/' . $backupid;
114 if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
115 print_error('unknownbackupexporterror'); // shouldn't happen ever
118 // Prepare the restore controller. We don't need a UI here as we will just use what
119 // ever the restore has (the user has just chosen).
120 $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
122 // Start a progress section for the restore, which will consist of 2 steps
123 // (the precheck and then the actual restore).
124 $progress->start_progress('Restore process', 2);
125 $rc->set_progress($progress);
126 // Convert the backup if required.... it should NEVER happed
127 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
128 $rc->convert();
130 // Mark the UI finished.
131 $rc->finish_ui();
132 // Execute prechecks
133 $warnings = false;
134 if (!$rc->execute_precheck()) {
135 $precheckresults = $rc->get_precheck_results();
136 if (is_array($precheckresults)) {
137 if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
138 fulldelete($tempdestination);
140 echo $OUTPUT->header();
141 echo $renderer->precheck_notices($precheckresults);
142 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
143 echo $OUTPUT->footer();
144 die();
146 if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
147 $warnings = $precheckresults['warnings'];
151 if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
152 restore_dbops::delete_course_content($course->id);
154 // Execute the restore.
155 $rc->execute_plan();
157 // Delete the temp directory now
158 fulldelete($tempdestination);
160 // End restore section of progress tracking (restore/precheck).
161 $progress->end_progress();
163 // All progress complete. Hide progress area.
164 $progress->end_progress();
165 echo html_writer::end_div();
166 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
168 // Display a notification and a continue button
169 if ($warnings) {
170 echo $OUTPUT->box_start();
171 echo $OUTPUT->notification(get_string('warning'), 'notifywarning');
172 echo html_writer::start_tag('ul', array('class'=>'list'));
173 foreach ($warnings as $warning) {
174 echo html_writer::tag('li', $warning);
176 echo html_writer::end_tag('ul');
177 echo $OUTPUT->box_end();
179 echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
180 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
181 echo $OUTPUT->footer();
183 die();
185 } else {
186 // Otherwise save the controller and progress
187 $backup->save_controller();
190 // Display the current stage
191 echo $OUTPUT->header();
192 if ($backup->enforce_changed_dependencies()) {
193 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
195 echo $renderer->progress_bar($backup->get_progress_bar());
196 echo $backup->display($renderer);
197 $backup->destroy();
198 unset($backup);
199 echo $OUTPUT->footer();