diff --git a/backup/restore.php b/backup/restore.php index 6d49b640868ac..ed72ab84b762f 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -5,7 +5,7 @@ require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php'); $contextid = required_param('contextid', PARAM_INT); -$stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT); +$stage = optional_param('stage', restore_ui::STAGE_CONFIRM, PARAM_INT); list($context, $course, $cm) = get_context_info_array($contextid); @@ -30,9 +30,15 @@ } } if ($rc) { + // check if the format conversion must happen first + if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { + $rc->convert(); + } + $restore = new restore_ui($rc, array('contextid'=>$context->id)); } } + $outcome = $restore->process(); if (!$restore->is_independent()) { if ($restore->get_stage() == restore_ui::STAGE_PROCESS && !$restore->requires_substage()) { diff --git a/backup/util/ui/renderer.php b/backup/util/ui/renderer.php index ad1bfe49811f2..bf9b073daa97a 100644 --- a/backup/util/ui/renderer.php +++ b/backup/util/ui/renderer.php @@ -157,6 +157,49 @@ public function backup_details($details, $nextstageurl) { return $html; } + /** + * Displays the general information about a backup file with non-standard format + * + * @param string $format the name of the format + * @param moodle_url $nextstageurl URL to send user to + * @param array $details not supported yet + * @return string HTML code to display + */ + public function backup_details_nonstandard($format, $nextstageurl, array $details = array()) { + + $formatname = get_string('backupformat'.$format, 'backup'); + + $html = html_writer::start_tag('div', array('class' => 'backup-restore nonstandardformat')); + $html .= html_writer::start_tag('div', array('class' => 'backup-section')); + $html .= $this->output->heading(get_string('backupdetails', 'backup'), 2, 'header'); + $html .= $this->backup_detail_pair( + get_string('backupformat', 'backup'), + get_string('backupdetailsnonstandardinfo', 'backup', get_string('backupformat'.$format, 'backup')) + ); + $html .= html_writer::end_tag('div'); + $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post'); + $html .= html_writer::end_tag('div'); + + return $html; + } + + /** + * Displays the general information about a backup file with unknown format + * + * @param moodle_url $nextstageurl URL to send user to + * @return string HTML code to display + */ + public function backup_details_unknown(moodle_url $nextstageurl) { + + $html = html_writer::start_tag('div', array('class' => 'unknownformat')); + $html .= $this->output->heading(get_string('errorinvalidformat', 'backup'), 2, 'notifyproblem'); + $html .= html_writer::tag('div', get_string('errorinvalidformatinfo', 'backup'), array('class' => 'notifyproblem')); + $html .= $this->output->single_button($nextstageurl, get_string('continue'), 'post'); + $html .= html_writer::end_tag('div'); + + return $html; + } + /** * Displays a course selector for restore * @@ -618,23 +661,6 @@ public function render_restore_category_search(restore_category_search $componen $output .= html_writer::end_tag('div'); return $output; } - - public function invalid_format($format) { - $html = html_writer::start_tag('div', array('class'=>'invalidformat')); - $html .= html_writer::tag('h2', get_string('errorinvalidformat', 'backup'), array('class'=>'notifyproblem')); - if ($format == 'moodle1') { - // Moodle 1.x backups - $icon = $this->output->help_icon('errormoodle1format', 'backup'); - $message = get_string('errormoodle1formatdesc', 'backup').' '.$icon; - - } else { - // Totally unknown format - $message = get_string('errorinvalidformatdesc', 'backup'); - } - $html .= html_writer::tag('div', $message, array('class'=>'notifyproblem')); - $html .= html_writer::end_tag('div'); - return $html; - } } /** diff --git a/backup/util/ui/restore_ui_stage.class.php b/backup/util/ui/restore_ui_stage.class.php index 8374fd5b1dd77..a58fbecb47497 100644 --- a/backup/util/ui/restore_ui_stage.class.php +++ b/backup/util/ui/restore_ui_stage.class.php @@ -1,6 +1,5 @@ extract_to_pathname("$CFG->dataroot/temp/backup/".$this->filename, "$CFG->dataroot/temp/backup/$this->filepath/")); } + + /** + * Renders the confirmation stage screen + * + * @param core_backup_renderer $renderer renderer instance to use + * @return string HTML code + */ public function display($renderer) { - // TODO: Remove this when backup formats are better supported + $prevstageurl = new moodle_url('/backup/restorefile.php', array('contextid' => $this->contextid)); + $nextstageurl = new moodle_url('/backup/restore.php', array( + 'contextid' => $this->contextid, + 'filepath' => $this->filepath, + 'stage' => restore_ui::STAGE_DESTINATION)); + $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); - return $renderer->backup_details($this->details, new moodle_url('/backup/restore.php', array('contextid'=>$this->contextid, 'filepath'=>$this->filepath, 'stage'=>restore_ui::STAGE_DESTINATION))); + if ($format === backup::FORMAT_UNKNOWN) { + // unknown format - we can't do anything here + return $renderer->backup_details_unknown($prevstageurl); + + } else if ($format !== backup::FORMAT_MOODLE) { + // non-standard format to be converted + return $renderer->backup_details_nonstandard($format, $nextstageurl); + + } else { + // standard MBZ backup, let us get information from it and display + $this->details = backup_general_helper::get_backup_information($this->filepath); + return $renderer->backup_details($this->details, $nextstageurl); + } } + public function get_stage_name() { return get_string('restorestage'.restore_ui::STAGE_CONFIRM, 'backup'); } @@ -198,7 +218,6 @@ public function get_stage() { class restore_ui_stage_destination extends restore_ui_independent_stage { protected $contextid; protected $filepath = null; - protected $details; protected $courseid = null; protected $target = backup::TARGET_NEW_COURSE; protected $coursesearch = null; @@ -235,29 +254,48 @@ public function process() { } return false; } + /** + * Renders the destination stage screen * - * @global moodle_database $DB - * @param core_backup_renderer $renderer - * @return string + * @param core_backup_renderer $renderer renderer instance to use + * @return string HTML code */ 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); + + 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; } - $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)); - + $nextstageurl = 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; + 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); } + public function get_stage_name() { return get_string('restorestage'.restore_ui::STAGE_DESTINATION, 'backup'); } @@ -564,7 +602,6 @@ protected function initialise_stage_form() { class restore_ui_stage_process extends restore_ui_stage { const SUBSTAGE_NONE = 0; - const SUBSTAGE_CONVERT = 1; const SUBSTAGE_PRECHECKS = 2; protected $substage = 0; @@ -592,20 +629,16 @@ public function process(base_moodleform $form=null) { // First decide whether a substage is needed $rc = $this->ui->get_controller(); - if ($rc->get_status() == backup::STATUS_REQUIRE_CONV) { - $this->substage = self::SUBSTAGE_CONVERT; - } else { - if ($rc->get_status() == backup::STATUS_SETTING_UI) { - $rc->finish_ui(); + if ($rc->get_status() == backup::STATUS_SETTING_UI) { + $rc->finish_ui(); + } + if ($rc->get_status() == backup::STATUS_NEED_PRECHECK) { + if (!$rc->precheck_executed()) { + $rc->execute_precheck(true); } - if ($rc->get_status() == backup::STATUS_NEED_PRECHECK) { - if (!$rc->precheck_executed()) { - $rc->execute_precheck(true); - } - $results = $rc->get_precheck_results(); - if (!empty($results)) { - $this->substage = self::SUBSTAGE_PRECHECKS; - } + $results = $rc->get_precheck_results(); + if (!empty($results)) { + $this->substage = self::SUBSTAGE_PRECHECKS; } } @@ -635,37 +668,52 @@ public function process(base_moodleform $form=null) { protected function initialise_stage_form() { throw new backup_ui_exception('backup_ui_must_execute_first'); } + /** - * should NEVER be called... throws an exception + * Renders the process stage screen + * + * @param core_backup_renderer $renderer renderer instance to use + * @return string HTML code */ public function display($renderer) { global $PAGE; + + $html = ''; $haserrors = false; - $url = new moodle_url($PAGE->url, array('restore'=>$this->get_uniqueid(), 'stage'=>restore_ui::STAGE_PROCESS, 'substage'=>$this->substage, 'sesskey'=>sesskey())); - echo html_writer::start_tag('form', array('action'=>$url->out_omit_querystring(), 'class'=>'backup-restore', 'method'=>'post')); - foreach ($url->params() as $name=>$value) { - echo html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>$name, 'value'=>$value)); + $url = new moodle_url($PAGE->url, array( + 'restore' => $this->get_uniqueid(), + 'stage' => restore_ui::STAGE_PROCESS, + 'substage' => $this->substage, + 'sesskey' => sesskey())); + $html .= html_writer::start_tag('form', array( + 'action' => $url->out_omit_querystring(), + 'class' => 'backup-restore', + 'method' => 'post')); + foreach ($url->params() as $name => $value) { + $html .= html_writer::empty_tag('input', array( + 'type' => 'hidden', + 'name' => $name, + 'value' => $value)); } switch ($this->substage) { - case self::SUBSTAGE_CONVERT : - echo '