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.
116 if (!$rc->execute_precheck()) {
117 $precheckresults = $rc->get_precheck_results();
118 if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
119 fulldelete($tempdestination);
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();
128 if ($restoretarget == backup
::TARGET_CURRENT_DELETING ||
$restoretarget == backup
::TARGET_EXISTING_DELETING
) {
129 restore_dbops
::delete_course_content($course->id
);
131 // Execute the restore
135 // Delete the temp directory now
136 fulldelete($tempdestination);
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();
147 // Otherwise save the controller and progress
148 $backup->save_controller();
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());
156 // Display the current stage
157 echo $OUTPUT->header();
158 if ($backup->enforce_changed_dependencies()) {
159 debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER
);
161 echo $renderer->progress_bar($backup->get_progress_bar());
162 echo $backup->display($renderer);
165 echo $OUTPUT->footer();