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);
24 require_login($course);
25 // Must hold restoretargetimport in the current course
26 require_capability('moodle/restore:restoretargetimport', $context);
28 $heading = get_string('import');
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');
37 // Prepare the backup renderer
38 $renderer = $PAGE->get_renderer('core','backup');
40 // Check if we already have a import course id
41 if ($importcourseid === false ||
$searchcourses) {
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));
46 // show the course selector
47 echo $OUTPUT->header();
48 echo $renderer->import_course_selector($url, $search);
49 echo $OUTPUT->footer();
53 // Load the course +context to import from
54 $importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST
);
55 $importcontext = context_course
::instance($importcourseid);
57 // Make sure the user can backup from that course
58 require_capability('moodle/backup:backuptargetimport', $importcontext);
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
);
62 if (!($bc = backup_ui
::load_controller($backupid))) {
63 $bc = new backup_controller(backup
::TYPE_1COURSE
, $importcourse->id
, backup
::FORMAT_MOODLE
,
64 backup
::INTERACTIVE_YES
, backup
::MODE_IMPORT
, $USER->id
);
65 $bc->get_plan()->get_setting('users')->set_status(backup_setting
::LOCKED_BY_CONFIG
);
66 $settings = $bc->get_plan()->get_settings();
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
);
75 $visiblesettings = true;
78 import_ui
::skip_current_stage(!$visiblesettings);
81 // Prepare the import UI
82 $backup = new import_ui($bc, array('importid'=>$importcourse->id
, 'target'=>$restoretarget));
83 // Process the current stage
86 // If this is the confirmation stage remove the filename setting
87 if ($backup->get_stage() == backup_ui
::STAGE_CONFIRMATION
) {
88 $backup->get_setting('filename')->set_visibility(backup_setting
::HIDDEN
);
91 // If it's the final stage process the import
92 if ($backup->get_stage() == backup_ui
::STAGE_FINAL
) {
93 // First execute the backup
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
101 $tempdestination = $CFG->tempdir
. '/backup/' . $backupid;
102 if (!file_exists($tempdestination) ||
!is_dir($tempdestination)) {
103 print_error('unknownbackupexporterror'); // shouldn't happen ever
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).
108 $rc = new restore_controller($backupid, $course->id
, backup
::INTERACTIVE_YES
, backup
::MODE_IMPORT
, $USER->id
, $restoretarget);
109 // Convert the backup if required.... it should NEVER happed
110 if ($rc->get_status() == backup
::STATUS_REQUIRE_CONV
) {
113 // Mark the UI finished.
117 if (!$rc->execute_precheck()) {
118 $precheckresults = $rc->get_precheck_results();
119 if (is_array($precheckresults)) {
120 if (!empty($precheckresults['errors'])) { // If errors are found, terminate the import.
121 fulldelete($tempdestination);
123 echo $OUTPUT->header();
124 echo $renderer->precheck_notices($precheckresults);
125 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id
)));
126 echo $OUTPUT->footer();
129 if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later.
130 $warnings = $precheckresults['warnings'];
134 if ($restoretarget == backup
::TARGET_CURRENT_DELETING ||
$restoretarget == backup
::TARGET_EXISTING_DELETING
) {
135 restore_dbops
::delete_course_content($course->id
);
137 // Execute the restore.
140 // Delete the temp directory now
141 fulldelete($tempdestination);
143 // Display a notification and a continue button
144 echo $OUTPUT->header();
146 echo $OUTPUT->box_start();
147 echo $OUTPUT->notification(get_string('warning'), 'notifywarning');
148 echo html_writer
::start_tag('ul', array('class'=>'list'));
149 foreach ($warnings as $warning) {
150 echo html_writer
::tag('li', $warning);
152 echo html_writer
::end_tag('ul');
153 echo $OUTPUT->box_end();
155 echo $OUTPUT->notification(get_string('importsuccess', 'backup'), 'notifysuccess');
156 echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id
)));
157 echo $OUTPUT->footer();
162 // Otherwise save the controller and progress
163 $backup->save_controller();
166 // Adjust the page for the stage
167 $PAGE->set_title($heading.': '.$backup->get_stage_name());
168 $PAGE->set_heading($heading.': '.$backup->get_stage_name());
169 $PAGE->navbar
->add($backup->get_stage_name());
171 // Display the current stage
172 echo $OUTPUT->header();
173 if ($backup->enforce_changed_dependencies()) {
174 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER
);
176 echo $renderer->progress_bar($backup->get_progress_bar());
177 echo $backup->display($renderer);
180 echo $OUTPUT->footer();