/** * Imports a course * * @param int $importfrom The id of the course we are importing from * @param int $importto The id of the course we are importing to * @param bool $deletecontent Whether to delete the course we are importing to content * @param array $options List of backup options * @return null * @since Moodle 2.4 */ public static function import_course($importfrom, $importto, $deletecontent = 0, $options = array()) { global $CFG, $USER, $DB; require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php'); require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); // Parameter validation. $params = self::validate_parameters( self::import_course_parameters(), array( 'importfrom' => $importfrom, 'importto' => $importto, 'deletecontent' => $deletecontent, 'options' => $options ) ); if ($params['deletecontent'] !== 0 and $params['deletecontent'] !== 1) { throw new moodle_exception('invalidextparam', 'webservice', '', $params['deletecontent']); } // Context validation. if (! ($importfrom = $DB->get_record('course', array('id'=>$params['importfrom'])))) { throw new moodle_exception('invalidcourseid', 'error'); } if (! ($importto = $DB->get_record('course', array('id'=>$params['importto'])))) { throw new moodle_exception('invalidcourseid', 'error'); } $importfromcontext = context_course::instance($importfrom->id); self::validate_context($importfromcontext); $importtocontext = context_course::instance($importto->id); self::validate_context($importtocontext); $backupdefaults = array( 'activities' => 1, 'blocks' => 1, 'filters' => 1 ); $backupsettings = array(); // Check for backup and restore options. if (!empty($params['options'])) { foreach ($params['options'] as $option) { // Strict check for a correct value (allways 1 or 0, true or false). $value = clean_param($option['value'], PARAM_INT); if ($value !== 0 and $value !== 1) { throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']); } if (!isset($backupdefaults[$option['name']])) { throw new moodle_exception('invalidextparam', 'webservice', '', $option['name']); } $backupsettings[$option['name']] = $value; } } // Capability checking. require_capability('moodle/backup:backuptargetimport', $importfromcontext); require_capability('moodle/restore:restoretargetimport', $importtocontext); $bc = new backup_controller(backup::TYPE_1COURSE, $importfrom->id, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id); foreach ($backupsettings as $name => $value) { $bc->get_plan()->get_setting($name)->set_value($value); } $backupid = $bc->get_backupid(); $backupbasepath = $bc->get_plan()->get_basepath(); $bc->execute_plan(); $bc->destroy(); // Restore the backup immediately. // Check if we must delete the contents of the destination course. if ($params['deletecontent']) { $restoretarget = backup::TARGET_EXISTING_DELETING; } else { $restoretarget = backup::TARGET_EXISTING_ADDING; } $rc = new restore_controller($backupid, $importto->id, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, $restoretarget); foreach ($backupsettings as $name => $value) { $rc->get_plan()->get_setting($name)->set_value($value); } if (!$rc->execute_precheck()) { $precheckresults = $rc->get_precheck_results(); if (is_array($precheckresults) && !empty($precheckresults['errors'])) { if (empty($CFG->keeptempdirectoriesonbackup)) { fulldelete($backupbasepath); } $errorinfo = ''; foreach ($precheckresults['errors'] as $error) { $errorinfo .= $error; } if (array_key_exists('warnings', $precheckresults)) { foreach ($precheckresults['warnings'] as $warning) { $errorinfo .= $warning; } } throw new moodle_exception('backupprecheckerrors', 'webservice', '', $errorinfo); } } else { if ($restoretarget == backup::TARGET_EXISTING_DELETING) { restore_dbops::delete_course_content($importto->id); } } $rc->execute_plan(); $rc->destroy(); if (empty($CFG->keeptempdirectoriesonbackup)) { fulldelete($backupbasepath); } return null; }
// If errors are found, terminate the import. fulldelete($tempdestination); echo $OUTPUT->header(); echo $renderer->precheck_notices($precheckresults); echo $OUTPUT->continue_button(new moodle_url('/course/view.php', array('id' => $course->id))); echo $OUTPUT->footer(); die; } if (!empty($precheckresults['warnings'])) { // If warnings are found, go ahead but display warnings later. $warnings = $precheckresults['warnings']; } } } if ($restoretarget == backup::TARGET_CURRENT_DELETING || $restoretarget == backup::TARGET_EXISTING_DELETING) { restore_dbops::delete_course_content($course->id); } // Execute the restore. $rc->execute_plan(); // Delete the temp directory now fulldelete($tempdestination); // Display a notification and a continue button echo $OUTPUT->header(); if ($warnings) { echo $OUTPUT->box_start(); echo $OUTPUT->notification(get_string('warning'), 'notifyproblem'); echo html_writer::start_tag('ul', array('class' => 'list')); foreach ($warnings as $warning) { echo html_writer::tag('li', $warning); } echo html_writer::end_tag('ul');
/** * Executes the restore plan * @throws restore_ui_exception if the progress or stage is wrong. * @return bool */ public function execute() { if ($this->progress >= self::PROGRESS_EXECUTED) { throw new restore_ui_exception('restoreuialreadyexecuted'); } if ($this->stage->get_stage() < self::STAGE_PROCESS) { throw new restore_ui_exception('restoreuifinalisedbeforeexecute'); } if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) { $options = array(); $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments'); $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings'); restore_dbops::delete_course_content($this->controller->get_courseid(), $options); } $this->controller->execute_plan(); $this->progress = self::PROGRESS_EXECUTED; $this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results()); return true; }
function backup_and_restore($courseid_from,$courseid_to,$admin) { global $USER, $CFG; // Turn off file logging, otherwise it can't delete the file (Windows). $CFG->backup_file_logger_level = backup::LOG_NONE; // Do backup with default settings. MODE_IMPORT means it will just // create the directory and not zip it. $bc = new backup_controller(backup::TYPE_1COURSE, $courseid_from, backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT, $admin->id); $backupid = $bc->get_backupid(); $bc->execute_plan(); $bc->destroy(); // Do restore to new course with default settings. //$newcourseid = restore_dbops::create_new_course( // $course_fullname, $course_shortname, $course_category); //directly to particular course id restore_dbops::delete_course_content($courseid_to); $rc = new restore_controller($backupid, $courseid_to, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $admin->id,1); $rc->execute_precheck(); $rc->execute_plan(); $rc->destroy(); return 0; }
$options['keep_groups_and_groupings'] = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, true); restore_dbops::delete_course_content($courseid, $options); } $controller = new restore_controller($filepath, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, $target); $controller->execute_precheck(); $controller->execute_plan(); $transaction->allow_commit(); } else { $courseids = required_param('targetid', PARAM_INT); $transaction = $DB->start_delegated_transaction(); foreach ($courseids as $key => $courseid) { if ($target == backup::TARGET_EXISTING_DELETING) { $options = array(); $options['keep_roles_and_enrolments'] = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, true); $options['keep_groups_and_groupings'] = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, true); restore_dbops::delete_course_content($courseid, $options); } $controller = new restore_controller($filepath, $courseid, backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id, $target); $controller->execute_precheck(); $controller->execute_plan(); } $transaction->allow_commit(); } backup_helper::delete_backup_dir($filepath); } } echo $OUTPUT->header(); if ($restore) { $heading = $course->fullname; $PAGE->set_title($heading . ': ' . $restore->get_stage_name()); $PAGE->set_heading($heading);
public static function restore_course($emptycourseid, $backupfilename) { global $DB, $CFG; try { $courseid = $emptycourseid; $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); $restoretarget = 0; $fb = get_file_packer(); $fb->extract_to_pathname("{$CFG->dataroot}/temp/backup/" . $backupfilename, "{$CFG->dataroot}/temp/backup/" . md5($backupfilename) . "/"); restore_dbops::delete_course_content($course->id); $rc = new restore_controller(md5($backupfilename), $course->id, backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2, $restoretarget); $rc->execute_precheck(); $rc->execute_plan(); return 'success'; } catch (Exception $e) { if (extension_loaded('newrelic')) { newrelic_notice_error($e->getMessage() . "{$CFG->dataroot}/temp/backup/" . $backupfilename, $e); } return 'Exception ' . $e->getMessage() . "{$CFG->dataroot}/temp/backup/" . $backupfilename; } }