restore MDL-23109 Fixed several minor issues with restore UI
authorSam Hemelryk <sam@moodle.com>
Mon, 26 Jul 2010 07:10:54 +0000 (07:10 +0000)
committerSam Hemelryk <sam@moodle.com>
Mon, 26 Jul 2010 07:10:54 +0000 (07:10 +0000)
* Category + course selectors now perform required capability checks
* Show/Hide settings from UI tested and implemented for overwrite_conf setting
* Final continue button is GET rather than post
* Temp zip file is deleted after successfull extraction
* Removed previous button from settings stage MDL-23480

backup/moodle2/restore_course_task.class.php
backup/restore.php
backup/util/ui/backup_ui_setting.class.php
backup/util/ui/backup_ui_stage.class.php
backup/util/ui/base_moodleform.class.php
backup/util/ui/base_ui_stage.class.php
backup/util/ui/renderer.php
backup/util/ui/restore_ui_stage.class.php

index e5f3706..74d81ce 100644 (file)
@@ -126,6 +126,12 @@ class restore_course_task extends restore_task {
         $overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
         $overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
         $overwrite->get_ui()->set_label(get_string('setting_overwriteconf', 'backup'));
+        $fixedtargets = array(backup::TARGET_NEW_COURSE, backup::TARGET_EXISTING_DELETING, backup::TARGET_CURRENT_DELETING);
+        if (in_array($this->get_target(), $fixedtargets)) {
+            $overwrite->set_value(true);
+            $overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
+            $overwrite->set_visibility(backup_setting::HIDDEN);
+        }
         $this->add_setting($overwrite);
 
     }
index bb906d2..e473a56 100644 (file)
@@ -15,6 +15,7 @@ $PAGE->set_context($context);
 $PAGE->set_pagelayout('standard');
 
 require_login($course, null, $cm);
+require_capability('moodle/restore:restorecourse', $context);
 
 $isfrontpage = ($course->id == SITEID);
 
@@ -27,7 +28,7 @@ if ($stage & restore_ui::STAGE_CONFIRM + restore_ui::STAGE_DESTINATION) {
         $restore = restore_ui::engage_independent_stage($stage/2, $contextid);
         if ($restore->process()) {
             $rc = new restore_controller($restore->get_filepath(), $restore->get_course_id(), backup::INTERACTIVE_YES,
-                                backup::MODE_GENERAL, $USER->id, backup::TARGET_NEW_COURSE);
+                                backup::MODE_GENERAL, $USER->id, $restore->get_target());
         }
     }
     if ($rc) {
index fac5f77..feb8a3c 100644 (file)
@@ -57,7 +57,8 @@ class base_setting_ui {
      */
     protected $type;
     /**
-     * @var base_setting
+     * The setting this UI belongs to (parent reference)
+     * @var base_setting|backup_setting
      */
     protected $setting;
     /**
index c93e054..6dbcf75 100644 (file)
@@ -461,7 +461,7 @@ class backup_ui_stage_complete extends backup_ui_stage_final {
         ));
 
         echo $OUTPUT->box_start();
-        echo get_string('executionsuccess', 'backup');
+        echo $OUTPUT->notification(get_string('executionsuccess', 'backup'), 'notifysuccess');
         echo $OUTPUT->continue_button($fileurl);
         echo $OUTPUT->box_end();
     }
index 47a141a..d483f48 100644 (file)
@@ -128,8 +128,8 @@ abstract class base_moodleform extends moodleform {
      */
     function add_setting(backup_setting $setting, base_task $task=null) {
 
-        // If the setting cant be changed then add it as a fixed setting.
-        if (!$setting->get_ui()->is_changeable()) {
+        // If the setting cant be changed or isn't visible then add it as a fixed setting.
+        if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
             return $this->add_fixed_setting($setting);
         }
 
index 011ff8d..d56880b 100644 (file)
@@ -141,7 +141,7 @@ abstract class base_ui_stage {
         return $this->ui;
     }
 
-    final public function is_first_stage() {
+    public function is_first_stage() {
         return $this->stage == 1;
     }
 }
\ No newline at end of file
index c36f4ee..88c928c 100644 (file)
@@ -172,26 +172,30 @@ class core_backup_renderer extends plugin_renderer_base {
             $html .= html_writer::end_tag('form');
         }
 
-        // New course
-        $html .= $form;
-        $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
-        $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
-        $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
-        $html .= $this->backup_detail_select(get_string('coursecategory', 'backup'), 'targetid', $categories);
-        $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
-        $html .= html_writer::end_tag('div');
-        $html .= html_writer::end_tag('form');
+        if (count($categories) > 0) {
+            // New course
+            $html .= $form;
+            $html .= html_writer::start_tag('div', array('class'=>'bcs-new-course backup-section'));
+            $html .= $this->output->heading(get_string('restoretonewcourse', 'backup'), 2, array('class'=>'header'));
+            $html .= $this->backup_detail_input(get_string('restoretonewcourse', 'backup'), 'radio', 'target', backup::TARGET_NEW_COURSE, array('checked'=>'checked'));
+            $html .= $this->backup_detail_select(get_string('coursecategory', 'backup'), 'targetid', $categories);
+            $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
+            $html .= html_writer::end_tag('div');
+            $html .= html_writer::end_tag('form');
+        }
 
-        // Existing course
-        $html .= $form;
-        $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
-        $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
-        $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING);
-        $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
-        $html .= $this->backup_detail_select(get_string('restoretocourse', 'backup'), 'targetid', $courses);
-        $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
-        $html .= html_writer::end_tag('div');
-        $html .= html_writer::end_tag('form');
+        if (count($courses) > 0) {
+            // Existing course
+            $html .= $form;
+            $html .= html_writer::start_tag('div', array('class'=>'bcs-existing-course backup-section'));
+            $html .= $this->output->heading(get_string('restoretoexistingcourse', 'backup'), 2, array('class'=>'header'));
+            $html .= $this->backup_detail_input(get_string('restoretoexistingcourseadding', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_ADDING);
+            $html .= $this->backup_detail_input(get_string('restoretoexistingcoursedeleting', 'backup'), 'radio', 'target', backup::TARGET_EXISTING_DELETING);
+            $html .= $this->backup_detail_select(get_string('restoretocourse', 'backup'), 'targetid', $courses);
+            $html .= $this->backup_detail_pair('', html_writer::empty_tag('input', array('type'=>'submit', 'value'=>get_string('continue'))));
+            $html .= html_writer::end_tag('div');
+            $html .= html_writer::end_tag('form');
+        }
 
         $html .= html_writer::end_tag('div');
         return $html;
@@ -230,12 +234,15 @@ class core_backup_renderer extends plugin_renderer_base {
         return $output.html_writer::end_tag('div');
     }
 
-    public function continue_button($url) {
+    public function continue_button($url, $method='post') {
         if (!($url instanceof moodle_url)) {
             $url = new moodle_url($url);
         }
+        if ($method != 'post') {
+            $method = 'get';
+        }
         $url->param('sesskey', sesskey());
-        $button = new single_button($url, get_string('continue'), 'post');
+        $button = new single_button($url, get_string('continue'), $method);
         $button->class = 'continuebutton';
         return $this->render($button);
     }
index fc4e06c..3d7fab5 100644 (file)
@@ -71,6 +71,9 @@ abstract class restore_ui_stage extends base_ui_stage {
     final public function get_name() {
         return get_string('restorestage'.$this->stage,'backup');
     }
+    final public function is_first_stage() {
+        return $this->stage == restore_ui::STAGE_SETTINGS;
+    }
 }
 
 abstract class restore_ui_independent_stage {
@@ -125,7 +128,11 @@ class restore_ui_stage_confirm extends restore_ui_independent_stage {
         if (!file_exists("$CFG->dataroot/temp/backup/".$this->filename)) {
             throw new restore_ui_exception('invalidrestorefile');
         }
-        return $this->extract_file_to_dir();
+        $outcome = $this->extract_file_to_dir();
+        if ($outcome) {
+            fulldelete($this->filename);
+        }
+        return $outcome;
     }
     protected function extract_file_to_dir() {
         global $CFG, $USER;
@@ -152,6 +159,7 @@ class restore_ui_stage_destination extends restore_ui_independent_stage {
     protected $filepath = null;
     protected $details;
     protected $courseid = null;
+    protected $target = backup::TARGET_NEW_COURSE;
     public function __construct($contextid) {
         $this->contextid = $contextid;
         $this->filepath = required_param('filepath', PARAM_ALPHANUM);
@@ -161,10 +169,10 @@ class restore_ui_stage_destination extends restore_ui_independent_stage {
         if (!file_exists("$CFG->dataroot/temp/backup/".$this->filepath) || !is_dir("$CFG->dataroot/temp/backup/".$this->filepath)) {
             throw new restore_ui_exception('invalidrestorepath');
         }
-        $target = optional_param('target', null, PARAM_INT);
-        if (!is_null($target) && confirm_sesskey()) {
-            $targetid = required_param('targetid', PARAM_INT);
-            if ($target == backup::TARGET_NEW_COURSE) {
+        $this->target = optional_param('target', backup::TARGET_NEW_COURSE, PARAM_INT);
+        $targetid = optional_param('targetid', null, PARAM_INT);
+        if (!is_null($this->target) && !is_null($targetid) && confirm_sesskey()) {
+            if ($this->target == backup::TARGET_NEW_COURSE) {
                 list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'), get_string('restoringcourseshortname', 'backup'));
                 $this->courseid = restore_dbops::create_new_course($fullname, $shortname, $targetid);
             } else {
@@ -181,18 +189,27 @@ class restore_ui_stage_destination extends restore_ui_independent_stage {
      * @return string
      */
     public function display($renderer) {
-        global $DB;
+        global $DB, $USER;
         $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)?$context->instanceid:false;
+        $currentcourse = ($context->contextlevel == CONTEXT_COURSE && has_capability('moodle/restore:restorecourse', $context))?$context->instanceid:false;
+        
         $courselist = array();
-        $courses = $DB->get_recordset('course', array(), 'id,fullname,shortname');
+        $courses = get_user_capability_course('moodle/restore:restorecourse', $USER->id, true, 'fullname,shortname,visible,sortorder');
         foreach ($courses as $course) {
             $courselist[$course->id] = $course->fullname.' ['.$course->shortname.']';
         }
-        $courses->close();
-        $html = $renderer->course_selector($url, $this->details, make_categories_options(), $courselist, $currentcourse);
+
+        $categories = make_categories_options();
+        foreach ($categories as $categoryid=>$category) {
+            if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $categoryid))) {
+                unset($categories[$categoryid]);
+            }
+        }
+
+        $html = $renderer->course_selector($url, $this->details, $categories, $courselist, $currentcourse);
         return $html;
     }
     public function get_stage_name() {
@@ -207,6 +224,9 @@ class restore_ui_stage_destination extends restore_ui_independent_stage {
     public function get_stage() {
         return restore_ui::STAGE_DESTINATION;
     }
+    public function get_target() {
+        return $this->target;
+    }
 }
 
 class restore_ui_stage_settings extends restore_ui_stage {
@@ -582,12 +602,13 @@ class restore_ui_stage_complete extends restore_ui_stage_process {
      * appropriate message.
      *
      * @global core_renderer $OUTPUT
+     * @param core_backup_renderer $renderer
      */
-    public function display($renderer) {
+    public function display(core_backup_renderer $renderer) {
         global $OUTPUT;
         echo $OUTPUT->box_start();
         echo $OUTPUT->notification(get_string('restoreexecutionsuccess', 'backup'), 'notifysuccess');
-        echo $renderer->continue_button(new moodle_url('/course/view.php', array('id'=>$this->get_ui()->get_controller()->get_courseid())));
+        echo $renderer->continue_button(new moodle_url('/course/view.php', array('id'=>$this->get_ui()->get_controller()->get_courseid())), 'get');
         echo $OUTPUT->box_end();
     }
 }
\ No newline at end of file