6040d4b76395196a781aa2f20bd4656878c85750
[moodle.git] / backup / restore.php
1 <?php
2     //This script is used to configure and execute the restore proccess.
4 require_once('../config.php');
5 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
7 $contextid   = required_param('contextid', PARAM_INT);
8 $stage       = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT);
10 list($context, $course, $cm) = get_context_info_array($contextid);
12 navigation_node::override_active_url(new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid)));
13 $PAGE->set_url(new moodle_url('/backup/restore.php', array('contextid'=>$contextid)));
14 $PAGE->set_context($context);
15 $PAGE->set_pagelayout('standard');
17 require_login($course, null, $cm);
18 require_capability('moodle/restore:restorecourse', $context);
20 // Restore of large courses requires extra memory. Use the amount configured
21 // in admin settings.
22 raise_memory_limit(MEMORY_EXTRA);
24 if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
25     $restore = restore_ui::engage_independent_stage($stage, $contextid);
26 } else {
27     $restoreid = optional_param('restore', false, PARAM_ALPHANUM);
28     $rc = restore_ui::load_controller($restoreid);
29     if (!$rc) {
30         $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
31         if ($restore->process()) {
32             $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
33                                 backup::MODE_GENERAL, $USER->id, $restore->get_target());
34         }
35     }
36     if ($rc) {
37         // check if the format conversion must happen first
38         if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
39             $rc->convert();
40         }
42         $restore = new restore_ui($rc, array('contextid'=>$context->id));
43     }
44 }
46 $heading = $course->fullname;
48 $PAGE->set_title($heading.': '.$restore->get_stage_name());
49 $PAGE->set_heading($heading);
50 $PAGE->navbar->add($restore->get_stage_name());
52 $renderer = $PAGE->get_renderer('core','backup');
53 echo $OUTPUT->header();
55 // Prepare a progress bar which can display optionally during long-running
56 // operations while setting up the UI.
57 $slowprogress = new core_backup_display_progress_if_slow();
58 // Depending on the code branch above, $restore may be a restore_ui or it may
59 // be a restore_ui_independent_stage. Either way, this function exists.
60 $restore->set_progress_reporter($slowprogress);
61 $outcome = $restore->process();
63 if (!$restore->is_independent() && $restore->enforce_changed_dependencies()) {
64     debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
65 }
67 if (!$restore->is_independent()) {
68     if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) {
69         try {
70             // Display an extra progress bar so that we can show the progress first.
71             echo html_writer::start_div('', array('id' => 'executionprogress'));
72             echo $renderer->progress_bar($restore->get_progress_bar());
73             $restore->get_controller()->set_progress(new core_backup_display_progress());
74             $restore->execute();
75             echo html_writer::end_div();
76             echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
77         } catch(Exception $e) {
78             $restore->cleanup();
79             throw $e;
80         }
81     } else {
82         $restore->save_controller();
83     }
84 }
86 echo $renderer->progress_bar($restore->get_progress_bar());
87 echo $restore->display($renderer);
88 $restore->destroy();
89 unset($restore);
90 echo $OUTPUT->footer();