Skip to content

Commit

Permalink
backup MDL-22140 Added conf settings to control the default general b…
Browse files Browse the repository at this point in the history
…ackup settings
  • Loading branch information
Sam Hemelryk committed Jun 17, 2010
1 parent d32f6ea commit 61ebef8
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 21 deletions.
22 changes: 20 additions & 2 deletions admin/settings/courses.php
Expand Up @@ -129,8 +129,26 @@
$CFG->wwwroot . '/course/pending.php', array('moodle/site:approvecourse')));
}

// Add a category for backups
$ADMIN->add('courses', new admin_category('backups', get_string('backups','admin')));

// Create a page for general backup defaults
$temp = new admin_settingpage('backupgeneralsettings', get_string('generalbackdefaults', 'backup'), 'moodle/backup:backupcourse');
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_users', get_string('generalusers','backup'), get_string('configgeneralusers','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_anonymize', get_string('generalanonymize','backup'), get_string('configgeneralanonymize','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_role_assignments', get_string('generalroleassignments','backup'), get_string('configgeneralroleassignments','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_user_files', get_string('generaluserfiles','backup'), get_string('configgeneraluserfiles','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_activities', get_string('generalactivities','backup'), get_string('configgeneralactivities','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_blocks', get_string('generalblocks','backup'), get_string('configgeneralblocks','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_filters', get_string('generalfilters','backup'), get_string('configgeneralfilters','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_comments', get_string('generalcomments','backup'), get_string('configgeneralcomments','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_userscompletion', get_string('generaluserscompletion','backup'), get_string('configgeneraluserscompletion','backup'), array('value'=>1, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_logs', get_string('generallogs','backup'), get_string('configgenerallogs','backup'), array('value'=>0, 'locked'=>0)));
$temp->add(new admin_setting_configcheckbox_with_lock('backup/backup_general_histories', get_string('generalhistories','backup'), get_string('configgeneralhistories','backup'), array('value'=>0, 'locked'=>0)));
$ADMIN->add('backups', $temp);

/// "backups" settingpage
$temp = new admin_settingpage('backups', get_string('backups','admin'), 'moodle/backup:backupcourse');
$temp = new admin_settingpage('scheduled', get_string('scheduledsettings','backup'), 'moodle/backup:backupcourse');
$temp->add(new admin_setting_configcheckbox('backup/backup_sche_modules', get_string('includemodules'), get_string('backupincludemoduleshelp'), 0));
$temp->add(new admin_setting_configcheckbox('backup/backup_sche_withuserdata', get_string('includemoduleuserdata'), get_string('backupincludemoduleuserdatahelp'), 0));
$temp->add(new admin_setting_configcheckbox('backup/backup_sche_metacourse', get_string('metacourse'), get_string('backupmetacoursehelp'), 0));
Expand Down Expand Up @@ -166,6 +184,6 @@
get_string('backupexecuteathelp'), array('h' => 0, 'm' => 0)));
$temp->add(new admin_setting_configdirectory('backup/backup_sche_destination', get_string('saveto'), get_string('backupsavetohelp'), ''));

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

} // end of speedup
4 changes: 4 additions & 0 deletions backup/controller/backup_controller.class.php
Expand Up @@ -236,6 +236,9 @@ public function get_executiontime() {
return $this->executiontime;
}

/**
* @return backup_plan
*/
public function get_plan() {
return $this->plan;
}
Expand Down Expand Up @@ -292,6 +295,7 @@ protected function load_plan() {

protected function apply_defaults() {
$this->log('applying plan defaults', backup::LOG_DEBUG);
backup_controller_dbops::apply_general_config_defaults($this);
$this->set_status(backup::STATUS_CONFIGURED);
}
}
Expand Down
36 changes: 36 additions & 0 deletions backup/util/dbops/backup_controller_dbops.class.php
Expand Up @@ -344,4 +344,40 @@ public static function apply_version_and_release() {
set_config('backup_release', backup::RELEASE);
}
}

/**
* Sets the controller settings default values from the backup config.
*
* @param backup_controller $controller
*/
public static function apply_general_config_defaults(backup_controller $controller) {
$settings = array(
// Config name => Setting name
'backup_general_users' => 'users',
'backup_general_anonymize' => 'anonymize',
'backup_general_role_assignments' => 'role_assignments',
'backup_general_user_files' => 'user_files',
'backup_general_activities' => 'activities',
'backup_general_blocks' => 'blocks',
'backup_general_filters' => 'filters',
'backup_general_comments' => 'comments',
'backup_general_userscompletion' => 'userscompletion',
'backup_general_logs' => 'logs',
'backup_general_histories' => 'grade_histories'
);
$plan = $controller->get_plan();
foreach ($settings as $config=>$settingname) {
$value = get_config('backup', $config);
$locked = (get_config('backup', $config.'_locked') == true);
if ($plan->setting_exists($settingname)) {
$setting = $plan->get_setting($settingname);
if ($setting->get_value() != $value || 1==1) {
$setting->set_value($value);
if ($locked) {
$setting->set_status(base_setting::LOCKED_BY_CONFIG);
}
}
}
}
}
}
20 changes: 16 additions & 4 deletions backup/util/settings/base_setting.class.php
Expand Up @@ -170,11 +170,13 @@ public function set_visibility($visibility) {
public function set_status($status) {
$status = $this->validate_status($status);

// If this setting is dependent on other settings first check that all
// of those settings are not locked
// If the setting is being unlocked first check whether an other settings
// this setting is dependent on are locked. If they are then we still don't
// want to lock this setting.
if (count($this->dependenton) > 0 && $status == base_setting::NOT_LOCKED) {
foreach ($this->dependenton as $dependency) {
if ($dependency->get_setting()->get_status() != base_setting::NOT_LOCKED) {
if ($dependency->is_locked()) {
// It still needs to be locked
$status = base_setting::LOCKED_BY_HIERARCHY;
break;
}
Expand All @@ -192,7 +194,7 @@ public function set_status($status) {
* Gets an array of properties for all of the dependencies that will affect
* this setting.
*
* This method returns and array rather than the dependencies in order to
* This method returns an array rather than the dependencies in order to
* minimise the memory footprint of for the potentially huge recursive
* dependency structure that we may be dealing with.
*
Expand All @@ -216,6 +218,16 @@ public function get_my_dependency_properties($settingname=null) {
return $dependencies;
}

/**
* Returns all of the dependencies that affect this setting.
* e.g. settings this setting depends on.
*
* @return array Array of setting_dependency's
*/
public function get_settings_depended_on() {
return $this->dependenton;
}

/**
* Checks if there are other settings that are dependent on this setting
*
Expand Down
42 changes: 42 additions & 0 deletions backup/util/settings/setting_dependency.class.php
Expand Up @@ -142,6 +142,11 @@ abstract public function enforce();
* @return array
*/
abstract public function get_moodleform_properties();
/**
* Returns true if the dependent setting is locked.
* @return bool
*/
abstract public function is_locked();
}

/**
Expand Down Expand Up @@ -169,6 +174,18 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
parent::__construct($setting, $dependentsetting, $defaultvalue);
$this->value = ($value)?(string)$value:0;
}
/**
* Returns true if the dependent setting is locked.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || $this->setting->get_value() == $this->value) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
}
/**
* Processes a value change in the primary setting
* @param mixed $oldvalue
Expand Down Expand Up @@ -348,6 +365,19 @@ protected function process_value_change($oldvalue) {
// Return true if the value has changed for the dependent setting
return ($prevalue != $this->dependentsetting->get_value());
}

/**
* Returns true if the dependent setting is locked.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || !empty($value)) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
}
}

/**
Expand Down Expand Up @@ -400,4 +430,16 @@ protected function process_value_change($oldvalue) {
// Return true if the value has changed for the dependent setting
return ($prevalue != $this->dependentsetting->get_value());
}
/**
* Returns true if the dependent setting is locked.
* @return bool
*/
public function is_locked() {
// If the setting is locked or the dependent setting should be locked then return true
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || empty($value)) {
return true;
}
// Else return based upon the dependent settings status
return ($this->dependentsetting->get_status() !== base_setting::NOT_LOCKED);
}
}
31 changes: 17 additions & 14 deletions backup/util/ui/backup_moodleform.class.php
Expand Up @@ -123,16 +123,9 @@ function close_task_divs() {
*/
function add_setting(backup_setting $setting, backup_task $task=null) {

// Check if the setting is locked first up
if ($setting->get_status() !== base_setting::NOT_LOCKED) {
// If it has no dependencies on other settings we can add it as a
// fixed setting instead
if (!$setting->has_dependencies_on_settings()) {
// Fixed setting it is!
return $this->add_fixed_setting($setting);
}
// Hmm possible to unlock it in the UI so disable instead.
$setting->get_ui()->disable();
// If the setting cant be changed then add it as a fixed setting.
if (!$setting->get_ui()->is_changeable()) {
return $this->add_fixed_setting($setting);
}

// First add the formatting for this setting
Expand Down Expand Up @@ -220,11 +213,21 @@ function add_fixed_setting(backup_setting $setting) {
$settingui = $setting->get_ui();
if ($setting->get_visibility() == backup_setting::VISIBLE) {
$this->add_html_formatting($setting);
if ($setting->get_status() != backup_setting::NOT_LOCKED) {
$this->_form->addElement('static', 'static_'.$settingui->get_name(), $settingui->get_label(),$settingui->get_static_value().' '.$OUTPUT->pix_icon('i/unlock', get_string('locked', 'backup'), 'moodle', array('class'=>'smallicon lockedicon')));
} else {
$this->_form->addElement('static','static_'. $settingui->get_name(), $settingui->get_label(), $settingui->get_static_value());
switch ($setting->get_status()) {
case backup_setting::LOCKED_BY_PERMISSION:
$icon = ' '.$OUTPUT->pix_icon('i/permissionlock', get_string('lockedbypermission', 'backup'), 'moodle', array('class'=>'smallicon lockedicon permissionlock'));
break;
case backup_setting::LOCKED_BY_CONFIG:
$icon = ' '.$OUTPUT->pix_icon('i/configlock', get_string('lockedbyconfig', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
break;
case backup_setting::LOCKED_BY_HIERARCHY:
$icon = ' '.$OUTPUT->pix_icon('i/hierarchylock', get_string('lockedbyhierarchy', 'backup'), 'moodle', array('class'=>'smallicon lockedicon configlock'));
break;
default:
$icon = '';
break;
}
$this->_form->addElement('static', 'static_'.$settingui->get_name(), $settingui->get_label(), $settingui->get_static_value().$icon);
$this->_form->addElement('html', html_writer::end_tag('div'));
}
$this->_form->addElement('hidden', $settingui->get_name(), $settingui->get_value());
Expand Down
36 changes: 35 additions & 1 deletion backup/util/ui/backup_ui_setting.class.php
Expand Up @@ -148,7 +148,6 @@ abstract class backup_setting_ui extends base_setting_ui {
public function __construct(backup_setting $setting, $label = null, array $attributes = null, array $options = null) {
parent::__construct($setting);
// Improve the inputs name by appending the level to the name
if (!($setting instanceof backup_setting)) debug($setting);
switch ($setting->get_level()) {
case backup_setting::ROOT_LEVEL :
$this->name = 'root_'.$setting->get_name();
Expand Down Expand Up @@ -230,6 +229,41 @@ public function get_label(backup_task $task=null) {
}
return $this->label;
}
/**
* Returns true if the setting is changeable.
*
* A setting is changeable if it meets either of the two following conditions.
*
* 1. The setting is not locked
* 2. The setting is locked but only by settings that are of the same level (same page)
*
* Condition 2 is really why we have this function
*
* @return bool
*/
public function is_changeable() {
if ($this->setting->get_status() === backup_setting::NOT_LOCKED) {
// Its not locked so its chanegable
return true;
} else if ($this->setting->get_status() !== backup_setting::LOCKED_BY_HIERARCHY) {
// Its not changeable because its locked by permission or config
return false;
} else if ($this->setting->has_dependencies_on_settings()) {
foreach ($this->setting->get_settings_depended_on() as $dependency) {
if ($dependency->is_locked() && $dependency->get_setting()->get_level() !== $this->setting->get_level()) {
// Its not changeable because one or more dependancies arn't
// changeable.
return false;
}
}
// Its changeable because all dependencies are changeable.
return true;
}
// We should never get here but if we do return false to be safe.
// The setting would need to be locked by hierarchy and not have any deps
return false;
}

}

/**
Expand Down
1 change: 1 addition & 0 deletions files/index.php
Expand Up @@ -41,6 +41,7 @@
}

$context = get_context_instance_by_id($contextid, MUST_EXIST);
$PAGE->set_context($context);

$course = null;
$cm = null;
Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -636,6 +636,7 @@
$string['localstringcustomization'] = 'Local string customization';
$string['location'] = 'Location';
$string['locationsettings'] = 'Location settings';
$string['locked'] = 'locked';
$string['log'] = 'Logs';
$string['loginhttps'] = 'Use HTTPS for logins';
$string['loglifetime'] = 'Keep logs for';
Expand Down
27 changes: 27 additions & 0 deletions lang/en/backup.php
Expand Up @@ -26,6 +26,17 @@
$string['backupcourse'] = 'Backup course: {$a}';
$string['backupsection'] = 'Backup course section: {$a}';
$string['backupactivity'] = 'Backup activity: {$a}';
$string['configgeneralactivities'] = 'Sets the default for including activities in a backup.';
$string['configgeneralanonymize'] = 'If enabled all information pertaining to users will be anonymised by default.';
$string['configgeneralblocks'] = 'Sets the default for including blocks in a backup.';
$string['configgeneralcomments'] = 'Sets the default for including comments in a backup.';
$string['configgeneralfilters'] = 'Sets the default for including filters in a backup.';
$string['configgeneralhistories'] = 'Sets the default for including user history within a backup.';
$string['configgenerallogs'] = 'If enabled logs will be included in backups by default.';
$string['configgeneralroleassignments'] = 'If enabled by default roles assignments will also be backed up.';
$string['configgeneraluserscompletion'] = 'If enabled user completion information will be included in backups by default.';
$string['configgeneraluserfiles'] = 'Sets the default for whether user files will be included in backups.';
$string['configgeneralusers'] = 'Sets the default for whether to include users in backups.';
$string['coursesettings'] = 'Course settings';
$string['currentstage1'] = 'Initial settings';
$string['currentstage2'] = 'Schema settings';
Expand All @@ -37,14 +48,30 @@
$string['errorfilenamemustbezip'] = 'The filename you enter must be a ZIP file and have the .zip extension';
$string['executionsuccess'] = 'Your backup completed successfully, clicking the continue button below will take you to view your backup file.';
$string['filename'] = 'Filename';
$string['generalactivities'] = 'Include activities';
$string['generalanonymize'] = 'Anonymise information';
$string['generalbackdefaults'] = 'General backup defaults';
$string['generalblocks'] = 'Include blocks';
$string['generalcomments'] = 'Include comments';
$string['generalfilters'] = 'Include filters';
$string['generalhistories'] = 'Include histories';
$string['generallogs'] = 'Include logs';
$string['generalroleassignments'] = 'Include role assignments';
$string['generaluserscompletion'] = 'Include user completion information';
$string['generaluserfiles'] = 'Include user files';
$string['generalusers'] = 'Include users';
$string['includesection'] = 'Include section {$a}';
$string['includeother'] = 'Include {$a}';
$string['includeuserinfo'] = 'Include user information';
$string['locked'] = 'Locked';
$string['lockedbypermission'] = 'You don\'t have sufficient permissions to change this setting';
$string['lockedbyconfig'] = 'This setting has been locked by the default backup settings';
$string['lockedbyhierarchy'] = 'Locked by dependencies';
$string['onstage1action'] = 'Next';
$string['onstage2action'] = 'Next';
$string['onstage4action'] = 'Perform backup';
$string['onstage8action'] = 'Continue';
$string['onstage16action'] = 'Continue';
$string['previousstage'] = 'Previous';
$string['rootsettings'] = 'Backup settings';
$string['scheduledsettings'] = 'Scheduled backup settings';

0 comments on commit 61ebef8

Please sign in to comment.