MDL-41644 Backup: Update CSS class .notifywarning with .notifyproblem
[moodle.git] / backup / import.php
CommitLineData
ad93ddd5
SH
1<?php
2
3// Require both the backup and restore libs
4require_once('../config.php');
5require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
6require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
7require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
8require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php');
9
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);
db6539c6 14// We just want to check if a search has been run. True if anything is there.
17270572 15$searchcourses = optional_param('searchcourses', false, PARAM_BOOL);
ad93ddd5
SH
16// The target method for the restore (adding or deleting)
17$restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
18
19// Load the course and context
74df2951 20$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
a689cd1d 21$context = context_course::instance($courseid);
ad93ddd5
SH
22
23// Must pass login
24require_login($course);
25// Must hold restoretargetimport in the current course
26require_capability('moodle/restore:restoretargetimport', $context);
27
ad93ddd5 28// Set up the page
8d137307 29$PAGE->set_title($course->shortname . ': ' . get_string('import'));
30$PAGE->set_heading($course->fullname);
ad93ddd5
SH
31$PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
32$PAGE->set_context($context);
33$PAGE->set_pagelayout('incourse');
34
35// Prepare the backup renderer
36$renderer = $PAGE->get_renderer('core','backup');
37
38// Check if we already have a import course id
17270572 39if ($importcourseid === false || $searchcourses) {
ad93ddd5
SH
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));
43
44 // show the course selector
45 echo $OUTPUT->header();
46 echo $renderer->import_course_selector($url, $search);
47 echo $OUTPUT->footer();
48 die();
49}
50
51// Load the course +context to import from
74df2951 52$importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
a689cd1d 53$importcontext = context_course::instance($importcourseid);
ad93ddd5
SH
54
55// Make sure the user can backup from that course
56require_capability('moodle/backup:backuptargetimport', $importcontext);
57
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);
60if (!($bc = backup_ui::load_controller($backupid))) {
61 $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
dc1e4cce 62 backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
ad93ddd5
SH
63 $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
64 $settings = $bc->get_plan()->get_settings();
65
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;
74 }
75 }
76 import_ui::skip_current_stage(!$visiblesettings);
77}
78
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();
83
84// If this is the confirmation stage remove the filename setting
85if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
86 $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
87}
88
89// If it's the final stage process the import
90if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
f06abc2a 91 echo $OUTPUT->header();
92
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());
96
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);
101
77b3d9df 102 // Prepare logger for backup.
103 $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
104 $backup->get_controller()->add_logger($logger);
105
ad93ddd5
SH
106 // First execute the backup
107 $backup->execute();
44bb101d
SH
108 $backup->destroy();
109 unset($backup);
dc1e4cce 110
f06abc2a 111 // Note that we've done that progress.
112 $progress->progress(1);
113
dc1e4cce
EL
114 // Check whether the backup directory still exists. If missing, something
115 // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
116 // backups don't store resulting files ever
7aa06e6d 117 $tempdestination = $CFG->tempdir . '/backup/' . $backupid;
ad93ddd5 118 if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
dc1e4cce 119 print_error('unknownbackupexporterror'); // shouldn't happen ever
24736932 120 }
ad93ddd5
SH
121
122 // Prepare the restore controller. We don't need a UI here as we will just use what
123 // ever the restore has (the user has just chosen).
dc1e4cce 124 $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
4fb31ae6 125
126 // Start a progress section for the restore, which will consist of 2 steps
127 // (the precheck and then the actual restore).
128 $progress->start_progress('Restore process', 2);
f06abc2a 129 $rc->set_progress($progress);
77b3d9df 130
131 // Set logger for restore.
132 $rc->add_logger($logger);
133
ad93ddd5
SH
134 // Convert the backup if required.... it should NEVER happed
135 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
136 $rc->convert();
137 }
138 // Mark the UI finished.
139 $rc->finish_ui();
140 // Execute prechecks
55e41006 141 $warnings = false;
ad93ddd5
SH
142 if (!$rc->execute_precheck()) {
143 $precheckresults = $rc->get_precheck_results();
55e41006
HB
144 if (is_array($precheckresults)) {
145 if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
146 fulldelete($tempdestination);
147
148 echo $OUTPUT->header();
149 echo $renderer->precheck_notices($precheckresults);
150 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
151 echo $OUTPUT->footer();
152 die();
153 }
154 if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
155 $warnings = $precheckresults['warnings'];
156 }
ad93ddd5 157 }
ad93ddd5 158 }
55e41006
HB
159 if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
160 restore_dbops::delete_course_content($course->id);
161 }
162 // Execute the restore.
163 $rc->execute_plan();
ad93ddd5
SH
164
165 // Delete the temp directory now
166 fulldelete($tempdestination);
167
4fb31ae6 168 // End restore section of progress tracking (restore/precheck).
169 $progress->end_progress();
170
f06abc2a 171 // All progress complete. Hide progress area.
172 $progress->end_progress();
173 echo html_writer::end_div();
174 echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
175
ad93ddd5 176 // Display a notification and a continue button
55e41006
HB
177 if ($warnings) {
178 echo $OUTPUT->box_start();
54ae34ce 179 echo $OUTPUT->notification(get_string('warning'), 'notifyproblem');
55e41006
HB
180 echo html_writer::start_tag('ul', array('class'=>'list'));
181 foreach ($warnings as $warning) {
182 echo html_writer::tag('li', $warning);
183 }
184 echo html_writer::end_tag('ul');
185 echo $OUTPUT->box_end();
186 }
187 echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
ad93ddd5 188 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
77b3d9df 189
190 // Get and display log data if there was any.
191 $loghtml = $logger->get_html();
192 if ($loghtml != '') {
193 echo $renderer->log_display($loghtml);
194 }
195
ad93ddd5
SH
196 echo $OUTPUT->footer();
197
198 die();
199
200} else {
201 // Otherwise save the controller and progress
202 $backup->save_controller();
203}
204
ad93ddd5
SH
205// Display the current stage
206echo $OUTPUT->header();
207if ($backup->enforce_changed_dependencies()) {
c8d1e4aa 208 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
ad93ddd5
SH
209}
210echo $renderer->progress_bar($backup->get_progress_bar());
748e9651 211echo $backup->display($renderer);
44bb101d
SH
212$backup->destroy();
213unset($backup);
17270572 214echo $OUTPUT->footer();