Skip to content

Commit

Permalink
MDL-34859 backup: add restore defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Mar 20, 2017
1 parent f840bf0 commit 44e86fb
Show file tree
Hide file tree
Showing 15 changed files with 535 additions and 45 deletions.
101 changes: 101 additions & 0 deletions admin/settings/courses.php
Expand Up @@ -343,4 +343,105 @@
//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_blogs', new lang_string('blogs', 'blog'), new lang_string('backupblogshelp','blog'), 0));

$ADMIN->add('backups', $temp);

// Create a page for general restore configuration and defaults.
$temp = new admin_settingpage('restoregeneralsettings', new lang_string('generalrestoredefaults', 'backup'));

// General restore defaults.
$temp->add(new admin_setting_heading('generalsettings', new lang_string('generalrestoresettings', 'backup'), ''));

$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_users',
new lang_string('generalusers', 'backup'), new lang_string('configrestoreusers', 'backup'),
array('value' => 1, 'locked' => 0)));
// Can not use actual constants here because we'd need to include 100 of backup/restore files.
$options = [
0/*backup::ENROL_NEVER*/ => get_string('rootsettingenrolments_never', 'backup'),
1/*backup::ENROL_WITHUSERS*/ => get_string('rootsettingenrolments_withusers', 'backup'),
2/*backup::ENROL_ALWAYS*/ => get_string('rootsettingenrolments_always', 'backup'),
];
$temp->add(new admin_setting_configselect_with_lock('restore/restore_general_enrolments',
new lang_string('generalenrolments', 'backup'), new lang_string('configrestoreenrolments', 'backup'),
array('value' => 1/*backup::ENROL_WITHUSERS*/, 'locked' => 0), $options));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_role_assignments',
new lang_string('generalroleassignments', 'backup'),
new lang_string('configrestoreroleassignments', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_activities',
new lang_string('generalactivities', 'backup'),
new lang_string('configrestoreactivities', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_blocks',
new lang_string('generalblocks', 'backup'),
new lang_string('configrestoreblocks', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_filters',
new lang_string('generalfilters', 'backup'),
new lang_string('configrestorefilters', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_comments',
new lang_string('generalcomments', 'backup'),
new lang_string('configrestorecomments', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_badges',
new lang_string('generalbadges', 'backup'),
new lang_string('configrestorebadges', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_calendarevents',
new lang_string('generalcalendarevents', 'backup'),
new lang_string('configrestorecalendarevents', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_userscompletion',
new lang_string('generaluserscompletion', 'backup'),
new lang_string('configrestoreuserscompletion', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_logs',
new lang_string('generallogs', 'backup'),
new lang_string('configrestorelogs', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_histories',
new lang_string('generalhistories', 'backup'),
new lang_string('configrestorehistories', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_groups',
new lang_string('generalgroups', 'backup'), new lang_string('configrestoregroups', 'backup'),
array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_general_competencies',
new lang_string('generalcompetencies', 'backup'),
new lang_string('configrestorecompetencies', 'backup'), array('value' => 1, 'locked' => 0)));

// Restore defaults when merging into another course.
$temp->add(new admin_setting_heading('mergerestoredefaults', new lang_string('mergerestoredefaults', 'backup'), ''));

$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_merge_overwrite_conf',
new lang_string('setting_overwrite_conf', 'backup'),
new lang_string('config_overwrite_conf', 'backup'), array('value' => 0, 'locked' => 0)));

$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_merge_course_fullname',
new lang_string('setting_overwrite_course_fullname', 'backup'),
new lang_string('config_overwrite_course_fullname', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_merge_course_shortname',
new lang_string('setting_overwrite_course_shortname', 'backup'),
new lang_string('config_overwrite_course_shortname', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_merge_course_startdate',
new lang_string('setting_overwrite_course_startdate', 'backup'),
new lang_string('config_overwrite_course_startdate', 'backup'), array('value' => 1, 'locked' => 0)));

// Restore defaults when replacing course contents.
$temp->add(new admin_setting_heading('replacerestoredefaults', new lang_string('replacerestoredefaults', 'backup'), ''));

$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_replace_overwrite_conf',
new lang_string('setting_overwrite_conf', 'backup'),
new lang_string('config_overwrite_conf', 'backup'), array('value' => 0, 'locked' => 0)));

$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_replace_course_fullname',
new lang_string('setting_overwrite_course_fullname', 'backup'),
new lang_string('config_overwrite_course_fullname', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_replace_course_shortname',
new lang_string('setting_overwrite_course_shortname', 'backup'),
new lang_string('config_overwrite_course_shortname', 'backup'), array('value' => 1, 'locked' => 0)));
$temp->add(new admin_setting_configcheckbox_with_lock('restore/restore_replace_course_startdate',
new lang_string('setting_overwrite_course_startdate', 'backup'),
new lang_string('config_overwrite_course_startdate', 'backup'), array('value' => 1, 'locked' => 0)));

$temp->add(new admin_setting_configselect_with_lock('restore/restore_replace_keep_roles_and_enrolments',
new lang_string('setting_keep_roles_and_enrolments', 'backup'),
new lang_string('config_keep_roles_and_enrolments', 'backup'), array('value' => 0, 'locked' => 0),
array(1 => get_string('yes'), 0 => get_string('no'))));
$temp->add(new admin_setting_configselect_with_lock('restore/restore_replace_keep_groups_and_groupings',
new lang_string('setting_keep_groups_and_groupings', 'backup'),
new lang_string('config_keep_groups_and_groupings', 'backup'), array('value' => 0, 'locked' => 0),
array(1 => get_string('yes'), 0 => get_string('no'))));

$ADMIN->add('backups', $temp);

}
27 changes: 27 additions & 0 deletions admin/tool/uploadcourse/tests/course_test.php
Expand Up @@ -741,6 +741,33 @@ public function test_restore_file() {
$this->assertTrue($found);
}

/**
* Test that specifying course template respects default restore settings
*/
public function test_restore_file_settings() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();

// Set admin config setting so that activities are not restored by default.
set_config('restore_general_activities', 0, 'restore');

$c1 = $this->getDataGenerator()->create_course();

$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
$data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertTrue($co->prepare());
$co->proceed();
$course = $DB->get_record('course', array('shortname' => 'A1'));

// Make sure the glossary is not restored.
$modinfo = get_fast_modinfo($course);
$this->assertEmpty($modinfo->get_instances_of('glossary'));
}

public function test_restore_invalid_file() {
$this->resetAfterTest();

Expand Down
12 changes: 12 additions & 0 deletions backup/controller/restore_controller.class.php
Expand Up @@ -142,6 +142,9 @@ public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $
// Load plan
$this->load_plan();

// Apply all default settings (based on type/format/mode).
$this->apply_defaults();

// Perform all initial security checks and apply (2nd param) them to settings automatically
restore_check::check_security($this, true);

Expand Down Expand Up @@ -510,6 +513,15 @@ protected function load_plan() {
$this->plan->build(); // Build plan for this controller
$this->set_status(backup::STATUS_PLANNED);
}

/**
* Apply defaults from the global admin settings
*/
protected function apply_defaults() {
$this->log('applying restore defaults', backup::LOG_DEBUG);
restore_controller_dbops::apply_config_defaults($this);
$this->set_status(backup::STATUS_CONFIGURED);
}
}

/*
Expand Down
45 changes: 27 additions & 18 deletions backup/moodle2/restore_course_task.class.php
Expand Up @@ -162,17 +162,37 @@ static public function define_decode_rules() {
*/
protected function define_settings() {

//$name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED
$fullname = new restore_course_generic_text_setting('course_fullname', base_setting::IS_TEXT, $this->get_info()->original_course_fullname);
$fullname->get_ui()->set_label(get_string('setting_course_fullname', 'backup'));
// Define overwrite_conf to decide if course configuration will be restored over existing one.
$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_overwrite_conf', 'backup'));
if ($this->get_target() == backup::TARGET_NEW_COURSE) {
$overwrite->set_value(true);
$overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
$overwrite->set_visibility(backup_setting::HIDDEN);
$course = (object)['fullname' => null, 'shortname' => null, 'startdate' => null];
} else {
$course = get_course($this->get_courseid());
}
$this->add_setting($overwrite);

$fullnamedefaultvalue = $this->get_info()->original_course_fullname;
$fullname = new restore_course_defaultcustom_setting('course_fullname', base_setting::IS_TEXT, $fullnamedefaultvalue);
$fullname->set_ui(new backup_setting_ui_defaultcustom($fullname, get_string('setting_course_fullname', 'backup'),
['customvalue' => $fullnamedefaultvalue, 'defaultvalue' => $course->fullname]));
$this->add_setting($fullname);

$shortname = new restore_course_generic_text_setting('course_shortname', base_setting::IS_TEXT, $this->get_info()->original_course_shortname);
$shortname->get_ui()->set_label(get_string('setting_course_shortname', 'backup'));
$shortnamedefaultvalue = $this->get_info()->original_course_shortname;
$shortname = new restore_course_defaultcustom_setting('course_shortname', base_setting::IS_TEXT, $shortnamedefaultvalue);
$shortname->set_ui(new backup_setting_ui_defaultcustom($shortname, get_string('setting_course_shortname', 'backup'),
['customvalue' => $shortnamedefaultvalue, 'defaultvalue' => $course->shortname]));
$this->add_setting($shortname);

$startdate = new restore_course_generic_text_setting('course_startdate', base_setting::IS_INTEGER, $this->get_info()->original_course_startdate);
$startdate->set_ui(new backup_setting_ui_dateselector($startdate, get_string('setting_course_startdate', 'backup')));
$startdatedefaultvalue = $this->get_info()->original_course_startdate;
$startdate = new restore_course_defaultcustom_setting('course_startdate', base_setting::IS_INTEGER, $startdatedefaultvalue);
$startdate->set_ui(new backup_setting_ui_defaultcustom($startdate, get_string('setting_course_startdate', 'backup'),
['customvalue' => $startdatedefaultvalue, 'defaultvalue' => $course->startdate, 'type' => 'date_selector']));
$this->add_setting($startdate);

$keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
Expand All @@ -195,16 +215,5 @@ protected function define_settings() {
}
$this->add_setting($keep_groups);

// Define overwrite_conf to decide if course configuration will be restored over existing one
$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'));
if ($this->get_target() == backup::TARGET_NEW_COURSE) {
$overwrite->set_value(true);
$overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
$overwrite->set_visibility(backup_setting::HIDDEN);
}
$this->add_setting($overwrite);

}
}
35 changes: 35 additions & 0 deletions backup/moodle2/restore_settingslib.php
Expand Up @@ -143,6 +143,41 @@ class restore_course_generic_setting extends course_backup_setting {}
*/
class restore_course_overwrite_conf_setting extends restore_course_generic_setting {}

/**
* Setting to switch between current and new course name/startdate
*
* @copyright 2017 Marina Glancy
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_course_defaultcustom_setting extends restore_course_generic_setting {
/**
* Validates that the value $value has type $vtype
* @param int $vtype
* @param mixed $value
* @return mixed
*/
public function validate_value($vtype, $value) {
if ($value === false) {
// Value "false" means default and is allowed for this setting type even if it does not match $vtype.
return $value;
}
return parent::validate_value($vtype, $value);
}

/**
* Special method for this element only. When value is "false" returns the default value.
* @return mixed
*/
public function get_normalized_value() {
$value = $this->get_value();
if ($value === false && $this->get_ui() instanceof backup_setting_ui_defaultcustom) {
$attributes = $this->get_ui()->get_attributes();
return $attributes['defaultvalue'];
}
return $value;
}
}


class restore_course_generic_text_setting extends restore_course_generic_setting {

Expand Down
24 changes: 9 additions & 15 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -1810,36 +1810,30 @@ public function process_course($data) {
$context = context::instance_by_id($this->task->get_contextid());
$userid = $this->task->get_userid();
$target = $this->get_task()->get_target();
$isnewcourse = $target != backup::TARGET_CURRENT_ADDING && $target != backup::TARGET_EXISTING_ADDING;
$isnewcourse = $target == backup::TARGET_NEW_COURSE;

// When restoring to a new course we can set all the things except for the ID number.
$canchangeidnumber = $isnewcourse || has_capability('moodle/course:changeidnumber', $context, $userid);
$canchangeshortname = $isnewcourse || has_capability('moodle/course:changeshortname', $context, $userid);
$canchangefullname = $isnewcourse || has_capability('moodle/course:changefullname', $context, $userid);
$canchangesummary = $isnewcourse || has_capability('moodle/course:changesummary', $context, $userid);

$data = (object)$data;
$data->id = $this->get_courseid();

// Calculate final course names, to avoid dupes.
$fullname = $this->get_setting_value('course_fullname');
$shortname = $this->get_setting_value('course_shortname');
$startdate = $this->get_setting_value('course_startdate');

// Calculate final course names, to avoid dupes.
list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname);

if ($canchangefullname) {
$data->fullname = $fullname;
} else {
list($data->fullname, $data->shortname) = restore_dbops::calculate_course_names($this->get_courseid(),
$fullname === false ? $data->fullname : $fullname,
$shortname === false ? $data->shortname : $shortname);
// Do not modify the course names at all when merging and user selected to keep the names (or prohibited by cap).
if (!$isnewcourse && $fullname === false) {
unset($data->fullname);
}

if ($canchangeshortname) {
$data->shortname = $shortname;
} else {
if (!$isnewcourse && $shortname === false) {
unset($data->shortname);
}

// Unset summary if user can't change it.
if (!$canchangesummary) {
unset($data->summary);
unset($data->summaryformat);
Expand Down

0 comments on commit 44e86fb

Please sign in to comment.