Skip to content

Commit

Permalink
restore MDL-23109 Fixed several minor issues with restore UI
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Sam Hemelryk committed Jul 26, 2010
1 parent b48c447 commit 39bc4c6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 39 deletions.
6 changes: 6 additions & 0 deletions backup/moodle2/restore_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ protected function define_settings() {
$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);

}
Expand Down
3 changes: 2 additions & 1 deletion backup/restore.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
$PAGE->set_pagelayout('standard');

require_login($course, null, $cm);
require_capability('moodle/restore:restorecourse', $context);

$isfrontpage = ($course->id == SITEID);

Expand All @@ -27,7 +28,7 @@
$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) {
Expand Down
3 changes: 2 additions & 1 deletion backup/util/ui/backup_ui_setting.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/**
Expand Down
2 changes: 1 addition & 1 deletion backup/util/ui/backup_ui_stage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ public function display() {
));

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();
}
Expand Down
4 changes: 2 additions & 2 deletions backup/util/ui/base_moodleform.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ function close_task_divs() {
*/
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);
}

Expand Down
2 changes: 1 addition & 1 deletion backup/util/ui/base_ui_stage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ final public function get_ui() {
return $this->ui;
}

final public function is_first_stage() {
public function is_first_stage() {
return $this->stage == 1;
}
}
49 changes: 28 additions & 21 deletions backup/util/ui/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,30 @@ public function course_selector(moodle_url $nextstageurl, $details, $categories,
$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;
Expand Down Expand Up @@ -230,12 +234,15 @@ public function precheck_notices($results) {
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);
}
Expand Down
45 changes: 33 additions & 12 deletions backup/util/ui/restore_ui_stage.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public function has_sub_stages() {
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 {
Expand Down Expand Up @@ -125,7 +128,11 @@ public function process() {
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;
Expand All @@ -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);
Expand All @@ -161,10 +169,10 @@ public function process() {
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 {
Expand All @@ -181,18 +189,27 @@ public function process() {
* @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() {
Expand All @@ -207,6 +224,9 @@ public function get_course_id() {
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 {
Expand Down Expand Up @@ -582,12 +602,13 @@ public function __construct(restore_ui $ui, array $params=null, array $results=n
* 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();
}
}

0 comments on commit 39bc4c6

Please sign in to comment.