wiki MDL-24130 fixed editing errors when editing is turned on when creating a first...
[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);
14// The target method for the restore (adding or deleting)
15$restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
16
17// Load the course and context
18$course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
19$context = get_context_instance(CONTEXT_COURSE, $courseid);
20
21// Must pass login
22require_login($course);
23// Must hold restoretargetimport in the current course
24require_capability('moodle/restore:restoretargetimport', $context);
25
26$heading = get_string('import');
27
28// Set up the page
29$PAGE->set_title($heading);
30$PAGE->set_heading($heading);
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
39if ($importcourseid === false) {
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
52$importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
53$importcontext = get_context_instance(CONTEXT_COURSE, $importcourseid);
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,
62 backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
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) {
91 // First execute the backup
92 $backup->execute();
24736932
SH
93 $results = $backup->get_controller()->get_results();
94
ad93ddd5
SH
95 // Check whether the backup directory still exists and if it doesn't extract the
96 // backup file so that we have it
97 $tempdestination = $CFG->dataroot . '/temp/backup/' . $backupid;
98 if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
ad93ddd5 99 $file = $results['backup_destination'];
74535bb5 100 $file->extract_to_pathname(get_file_packer('application/zip'), $tempdestination);
ad93ddd5 101 }
24736932
SH
102 if (!empty($results['backup_destination'])) {
103 // Delete the backup file, we only want the directory
104 $results['backup_destination']->delete();
105 }
ad93ddd5
SH
106
107 // Prepare the restore controller. We don't need a UI here as we will just use what
108 // ever the restore has (the user has just chosen).
109 $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id, $restoretarget);
110 // Convert the backup if required.... it should NEVER happed
111 if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
112 $rc->convert();
113 }
114 // Mark the UI finished.
115 $rc->finish_ui();
116 // Execute prechecks
117 if (!$rc->execute_precheck()) {
118 $precheckresults = $rc->get_precheck_results();
119 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
120 fulldelete($tempdestination);
121
122 echo $OUTPUT->header();
123 echo $renderer->precheck_notices($precheckresults);
124 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
125 echo $OUTPUT->footer();
126 die();
127 }
128 } else {
24736932
SH
129 if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
130 restore_dbops::delete_course_content($course->id);
131 }
ad93ddd5
SH
132 // Execute the restore
133 $rc->execute_plan();
134 }
135
136 // Delete the temp directory now
137 fulldelete($tempdestination);
138
139 // Display a notification and a continue button
140 echo $OUTPUT->header();
141 echo $OUTPUT->notification(get_string('importsuccess', 'backup'),'notifysuccess');
142 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
143 echo $OUTPUT->footer();
144
145 die();
146
147} else {
148 // Otherwise save the controller and progress
149 $backup->save_controller();
150}
151
152// Adjust the page for the stage
153$PAGE->set_title($heading.': '.$backup->get_stage_name());
154$PAGE->set_heading($heading.': '.$backup->get_stage_name());
155$PAGE->navbar->add($backup->get_stage_name());
156
157// Display the current stage
158echo $OUTPUT->header();
159if ($backup->enforce_changed_dependencies()) {
160 echo $renderer->dependency_notification(get_string('dependenciesenforced','backup'));
161}
162echo $renderer->progress_bar($backup->get_progress_bar());
163echo $backup->display();
164echo $OUTPUT->footer();