MDL-38190 (2) Backup/restore: Progress tracking for tasks and steps
[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
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
28$heading = get_string('import');
29
30// Set up the page
31$PAGE->set_title($heading);
32$PAGE->set_heading($heading);
33$PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
34$PAGE->set_context($context);
35$PAGE->set_pagelayout('incourse');
36
37// Prepare the backup renderer
38$renderer = $PAGE->get_renderer('core','backup');
39
40// Check if we already have a import course id
17270572 41if ($importcourseid === false || $searchcourses) {
ad93ddd5
SH
42 // Obviously not... show the selector so one can be chosen
43 $url = new moodle_url('/backup/import.php', array('id'=>$courseid));
44 $search = new import_course_search(array('url'=>$url));
45
46 // show the course selector
47 echo $OUTPUT->header();
48 echo $renderer->import_course_selector($url, $search);
49 echo $OUTPUT->footer();
50 die();
51}
52
53// Load the course +context to import from
54$importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
a689cd1d 55$importcontext = context_course::instance($importcourseid);
ad93ddd5
SH
56
57// Make sure the user can backup from that course
58require_capability('moodle/backup:backuptargetimport', $importcontext);
59
60// Attempt to load the existing backup controller (backupid will be false if there isn't one)
61$backupid = optional_param('backup', false, PARAM_ALPHANUM);
62if (!($bc = backup_ui::load_controller($backupid))) {
63 $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
dc1e4cce 64 backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
ad93ddd5
SH
65 $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
66 $settings = $bc->get_plan()->get_settings();
67
68 // For the initial stage we want to hide all locked settings and if there are
69 // no visible settings move to the next stage
70 $visiblesettings = false;
71 foreach ($settings as $setting) {
72 if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
73 $setting->set_visibility(backup_setting::HIDDEN);
74 } else {
75 $visiblesettings = true;
76 }
77 }
78 import_ui::skip_current_stage(!$visiblesettings);
79}
80
81// Prepare the import UI
82$backup = new import_ui($bc, array('importid'=>$importcourse->id, 'target'=>$restoretarget));
83// Process the current stage
84$backup->process();
85
86// If this is the confirmation stage remove the filename setting
87if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
88 $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
89}
90
91// If it's the final stage process the import
92if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
93 // First execute the backup
94 $backup->execute();
44bb101d
SH
95 $backup->destroy();
96 unset($backup);
dc1e4cce
EL
97
98 // Check whether the backup directory still exists. If missing, something
99 // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
100 // backups don't store resulting files ever
7aa06e6d 101 $tempdestination = $CFG->tempdir . '/backup/' . $backupid;
ad93ddd5 102 if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
dc1e4cce 103 print_error('unknownbackupexporterror'); // shouldn't happen ever
24736932 104 }
ad93ddd5
SH
105
106 // Prepare the restore controller. We don't need a UI here as we will just use what
107 // ever the restore has (the user has just chosen).
dc1e4cce 108 $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
ad93ddd5
SH
109 // Convert the backup if required.... it should NEVER happed
110 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
111 $rc->convert();
112 }
113 // Mark the UI finished.
114 $rc->finish_ui();
115 // Execute prechecks
116 if (!$rc->execute_precheck()) {
117 $precheckresults = $rc->get_precheck_results();
118 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
119 fulldelete($tempdestination);
120
121 echo $OUTPUT->header();
122 echo $renderer->precheck_notices($precheckresults);
123 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
124 echo $OUTPUT->footer();
125 die();
126 }
127 } else {
24736932
SH
128 if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
129 restore_dbops::delete_course_content($course->id);
130 }
ad93ddd5
SH
131 // Execute the restore
132 $rc->execute_plan();
133 }
134
135 // Delete the temp directory now
136 fulldelete($tempdestination);
137
138 // Display a notification and a continue button
139 echo $OUTPUT->header();
140 echo $OUTPUT->notification(get_string('importsuccess', 'backup'),'notifysuccess');
141 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
142 echo $OUTPUT->footer();
143
144 die();
145
146} else {
147 // Otherwise save the controller and progress
148 $backup->save_controller();
149}
150
151// Adjust the page for the stage
152$PAGE->set_title($heading.': '.$backup->get_stage_name());
153$PAGE->set_heading($heading.': '.$backup->get_stage_name());
154$PAGE->navbar->add($backup->get_stage_name());
155
156// Display the current stage
157echo $OUTPUT->header();
158if ($backup->enforce_changed_dependencies()) {
c8d1e4aa 159 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
ad93ddd5
SH
160}
161echo $renderer->progress_bar($backup->get_progress_bar());
748e9651 162echo $backup->display($renderer);
44bb101d
SH
163$backup->destroy();
164unset($backup);
17270572 165echo $OUTPUT->footer();