Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/import.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // Require both the backup and restore libs
00004 require_once('../config.php');
00005 require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
00006 require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
00007 require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
00008 require_once($CFG->dirroot . '/backup/util/ui/import_extensions.php');
00009 
00010 // The courseid we are importing to
00011 $courseid = required_param('id', PARAM_INT);
00012 // The id of the course we are importing FROM (will only be set if past first stage
00013 $importcourseid = optional_param('importid', false, PARAM_INT);
00014 // The target method for the restore (adding or deleting)
00015 $restoretarget = optional_param('target', backup::TARGET_CURRENT_ADDING, PARAM_INT);
00016 
00017 // Load the course and context
00018 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
00019 $context = get_context_instance(CONTEXT_COURSE, $courseid);
00020 
00021 // Must pass login
00022 require_login($course);
00023 // Must hold restoretargetimport in the current course
00024 require_capability('moodle/restore:restoretargetimport', $context);
00025 
00026 $heading = get_string('import');
00027 
00028 // Set up the page
00029 $PAGE->set_title($heading);
00030 $PAGE->set_heading($heading);
00031 $PAGE->set_url(new moodle_url('/backup/import.php', array('id'=>$courseid)));
00032 $PAGE->set_context($context);
00033 $PAGE->set_pagelayout('incourse');
00034 
00035 // Prepare the backup renderer
00036 $renderer = $PAGE->get_renderer('core','backup');
00037 
00038 // Check if we already have a import course id
00039 if ($importcourseid === false) {
00040     // Obviously not... show the selector so one can be chosen
00041     $url = new moodle_url('/backup/import.php', array('id'=>$courseid));
00042     $search = new import_course_search(array('url'=>$url));
00043 
00044     // show the course selector
00045     echo $OUTPUT->header();
00046     echo $renderer->import_course_selector($url, $search);
00047     echo $OUTPUT->footer();
00048     die();
00049 }
00050 
00051 // Load the course +context to import from
00052 $importcourse = $DB->get_record('course', array('id'=>$importcourseid), '*', MUST_EXIST);
00053 $importcontext = get_context_instance(CONTEXT_COURSE, $importcourseid);
00054 
00055 // Make sure the user can backup from that course
00056 require_capability('moodle/backup:backuptargetimport', $importcontext);
00057 
00058 // Attempt to load the existing backup controller (backupid will be false if there isn't one)
00059 $backupid = optional_param('backup', false, PARAM_ALPHANUM);
00060 if (!($bc = backup_ui::load_controller($backupid))) {
00061     $bc = new backup_controller(backup::TYPE_1COURSE, $importcourse->id, backup::FORMAT_MOODLE,
00062                             backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id);
00063     $bc->get_plan()->get_setting('users')->set_status(backup_setting::LOCKED_BY_CONFIG);
00064     $settings = $bc->get_plan()->get_settings();
00065 
00066     // For the initial stage we want to hide all locked settings and if there are
00067     // no visible settings move to the next stage
00068     $visiblesettings = false;
00069     foreach ($settings as $setting) {
00070         if ($setting->get_status() !== backup_setting::NOT_LOCKED) {
00071             $setting->set_visibility(backup_setting::HIDDEN);
00072         } else {
00073             $visiblesettings = true;
00074         }
00075     }
00076     import_ui::skip_current_stage(!$visiblesettings);
00077 }
00078 
00079 // Prepare the import UI
00080 $backup = new import_ui($bc, array('importid'=>$importcourse->id, 'target'=>$restoretarget));
00081 // Process the current stage
00082 $backup->process();
00083 
00084 // If this is the confirmation stage remove the filename setting
00085 if ($backup->get_stage() == backup_ui::STAGE_CONFIRMATION) {
00086     $backup->get_setting('filename')->set_visibility(backup_setting::HIDDEN);
00087 }
00088 
00089 // If it's the final stage process the import
00090 if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
00091     // First execute the backup
00092     $backup->execute();
00093     $backup->destroy();
00094     unset($backup);
00095 
00096     // Check whether the backup directory still exists. If missing, something
00097     // went really wrong in backup, throw error. Note that backup::MODE_IMPORT
00098     // backups don't store resulting files ever
00099     $tempdestination = $CFG->tempdir . '/backup/' . $backupid;
00100     if (!file_exists($tempdestination) || !is_dir($tempdestination)) {
00101         print_error('unknownbackupexporterror'); // shouldn't happen ever
00102     }
00103 
00104     // Prepare the restore controller. We don't need a UI here as we will just use what
00105     // ever the restore has (the user has just chosen).
00106     $rc = new restore_controller($backupid, $course->id, backup::INTERACTIVE_YES, backup::MODE_IMPORT, $USER->id, $restoretarget);
00107     // Convert the backup if required.... it should NEVER happed
00108     if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) {
00109         $rc->convert();
00110     }
00111     // Mark the UI finished.
00112     $rc->finish_ui();
00113     // Execute prechecks
00114     if (!$rc->execute_precheck()) {
00115         $precheckresults = $rc->get_precheck_results();
00116         if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
00117             fulldelete($tempdestination);
00118 
00119             echo $OUTPUT->header();
00120             echo $renderer->precheck_notices($precheckresults);
00121             echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
00122             echo $OUTPUT->footer();
00123             die();
00124         }
00125     } else {
00126         if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) {
00127             restore_dbops::delete_course_content($course->id);
00128         }
00129         // Execute the restore
00130         $rc->execute_plan();
00131     }
00132 
00133     // Delete the temp directory now
00134     fulldelete($tempdestination);
00135 
00136     // Display a notification and a continue button
00137     echo $OUTPUT->header();
00138     echo $OUTPUT->notification(get_string('importsuccess', 'backup'),'notifysuccess');
00139     echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id'=>$course->id)));
00140     echo $OUTPUT->footer();
00141 
00142     die();
00143 
00144 } else {
00145     // Otherwise save the controller and progress
00146     $backup->save_controller();
00147 }
00148 
00149 // Adjust the page for the stage
00150 $PAGE->set_title($heading.': '.$backup->get_stage_name());
00151 $PAGE->set_heading($heading.': '.$backup->get_stage_name());
00152 $PAGE->navbar->add($backup->get_stage_name());
00153 
00154 // Display the current stage
00155 echo $OUTPUT->header();
00156 if ($backup->enforce_changed_dependencies()) {
00157     echo $renderer->dependency_notification(get_string('dependenciesenforced','backup'));
00158 }
00159 echo $renderer->progress_bar($backup->get_progress_bar());
00160 echo $backup->display();
00161 $backup->destroy();
00162 unset($backup);
00163 echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations