/** * Renders the destination stage screen * * @param core_backup_renderer $renderer renderer instance to use * @return string HTML code */ public function display(core_backup_renderer $renderer) { $format = backup_general_helper::detect_backup_format($this->filepath); if ($format === backup::FORMAT_MOODLE) { // Standard Moodle 2 format, let use get the type of the backup. $details = backup_general_helper::get_backup_information($this->filepath); if ($details->type === backup::TYPE_1COURSE) { $wholecourse = true; } else { $wholecourse = false; } } else { // Non-standard format to be converted. We assume it contains the // whole course for now. However, in the future there might be a callback // to the installed converters. $wholecourse = true; } $nextstageurl = new moodle_url('/backup/restore.php', array('contextid' => $this->contextid, 'filepath' => $this->filepath, 'stage' => restore_ui::STAGE_SETTINGS)); $context = context::instance_by_id($this->contextid); if ($context->contextlevel == CONTEXT_COURSE and has_capability('moodle/restore:restorecourse', $context)) { $currentcourse = $context->instanceid; } else { $currentcourse = false; } return $renderer->course_selector($nextstageurl, $wholecourse, $this->categorysearch, $this->coursesearch, $currentcourse); }
/** * Converts the given directory with the backup into moodle2 format * * @param string $tempdir The directory to convert * @param string $format The current format, if already detected * @param base_logger|null if the conversion should be logged, use this logger * @throws convert_helper_exception * @return bool false if unable to find the conversion path, true otherwise */ public static function to_moodle2_format($tempdir, $format = null, $logger = null) { if (is_null($format)) { $format = backup_general_helper::detect_backup_format($tempdir); } // get the supported conversion paths from all available converters $converters = self::available_converters(); $descriptions = array(); foreach ($converters as $name) { $classname = "{$name}_converter"; if (!class_exists($classname)) { throw new convert_helper_exception('class_not_loaded', $classname); } if ($logger instanceof base_logger) { backup_helper::log('available converter', backup::LOG_DEBUG, $classname, 1, false, $logger); } $descriptions[$name] = call_user_func($classname . '::description'); } // choose the best conversion path for the given format $path = self::choose_conversion_path($format, $descriptions); if (empty($path)) { if ($logger instanceof base_logger) { backup_helper::log('unable to find the conversion path', backup::LOG_ERROR, null, 0, false, $logger); } return false; } if ($logger instanceof base_logger) { backup_helper::log('conversion path established', backup::LOG_INFO, implode(' => ', array_merge($path, array('moodle2'))), 0, false, $logger); } foreach ($path as $name) { if ($logger instanceof base_logger) { backup_helper::log('running converter', backup::LOG_INFO, $name, 0, false, $logger); } $converter = convert_factory::get_converter($name, $tempdir, $logger); $converter->convert(); } // make sure we ended with moodle2 format if (!self::detect_moodle2_format($tempdir)) { throw new convert_helper_exception('conversion_failed'); } return true; }
/** * * @param string $tempdir Directory under tempdir/backup awaiting restore * @param int $courseid Course id where restore is going to happen * @param bool $interactive backup::INTERACTIVE_YES[true] or backup::INTERACTIVE_NO[false] * @param int $mode backup::MODE_[ GENERAL | HUB | IMPORT | SAMESITE ] * @param int $userid * @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ] */ public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target) { $this->tempdir = $tempdir; $this->courseid = $courseid; $this->interactive = $interactive; $this->mode = $mode; $this->userid = $userid; $this->target = $target; // Apply some defaults $this->type = ''; $this->format = backup::FORMAT_UNKNOWN; $this->execution = backup::EXECUTION_INMEDIATE; $this->operation = backup::OPERATION_RESTORE; $this->executiontime = 0; $this->samesite = false; $this->checksum = ''; $this->precheck = null; // Apply current backup version and release if necessary backup_controller_dbops::apply_version_and_release(); // Check courseid is correct restore_check::check_courseid($this->courseid); // Check user is correct restore_check::check_user($this->userid); // Calculate unique $restoreid $this->calculate_restoreid(); // Default logger chain (based on interactive/execution) $this->logger = backup_factory::get_logger_chain($this->interactive, $this->execution, $this->restoreid); // Instantiate the output_controller singleton and active it if interactive and inmediate $oc = output_controller::get_instance(); if ($this->interactive == backup::INTERACTIVE_YES && $this->execution == backup::EXECUTION_INMEDIATE) { $oc->set_active(true); } $this->log('instantiating restore controller', backup::LOG_INFO, $this->restoreid); // Set initial status $this->set_status(backup::STATUS_CREATED); // Calculate original restore format $this->format = backup_general_helper::detect_backup_format($tempdir); // If format is not moodle2, set to conversion needed if ($this->format !== backup::FORMAT_MOODLE) { $this->set_status(backup::STATUS_REQUIRE_CONV); // Else, format is moodle2, load plan, apply security and set status based on interactivity } else { // Load plan $this->load_plan(); // Perform all initial security checks and apply (2nd param) them to settings automatically restore_check::check_security($this, true); if ($this->interactive == backup::INTERACTIVE_YES) { $this->set_status(backup::STATUS_SETTING_UI); } else { $this->set_status(backup::STATUS_NEED_PRECHECK); } } }
/** * * @global moodle_database $DB * @param core_backup_renderer $renderer * @return string */ public function display($renderer) { global $DB, $USER, $PAGE; $format = backup_general_helper::detect_backup_format($this->filepath); if ($format !== 'moodle2') { return $renderer->invalid_format($format); } $this->details = backup_general_helper::get_backup_information($this->filepath); $url = new moodle_url('/backup/restore.php', array('contextid' => $this->contextid, 'filepath' => $this->filepath, 'stage' => restore_ui::STAGE_SETTINGS)); $context = get_context_instance_by_id($this->contextid); $currentcourse = $context->contextlevel == CONTEXT_COURSE && has_capability('moodle/restore:restorecourse', $context) ? $context->instanceid : false; $html = $renderer->course_selector($url, $this->details, $this->categorysearch, $this->coursesearch, $currentcourse); return $html; }
/** * convert from current format to backup::MOODLE format */ public function convert() { if ($this->status != backup::STATUS_REQUIRE_CONV) { throw new restore_controller_exception('cannot_convert_not_required_status'); } if ($this->format == backup::FORMAT_UNKNOWN) { throw new restore_controller_exception('cannot_convert_from_unknown_format'); } if ($this->format == backup::FORMAT_MOODLE1) { // TODO: Implement moodle1 => moodle2 conversion throw new restore_controller_exception('cannot_convert_yet_from_moodle1_format'); } // Once conversions have finished, we check again the format $newformat = backup_general_helper::detect_backup_format($tempdir); // If format is moodle2, load plan, apply security and set status based on interactivity if ($newformat === backup::FORMAT_MOODLE) { // Load plan $this->load_plan(); // Perform all initial security checks and apply (2nd param) them to settings automatically restore_check::check_security($this, true); if ($this->interactive == backup::INTERACTIVE_YES) { $this->set_status(backup::STATUS_SETTING_UI); } else { $this->set_status(backup::STATUS_NEED_PRECHECK); } } else { throw new restore_controller_exception('conversion_ended_with_wrong_format', $newformat); } }