Skip to content

Commit

Permalink
MDL-60851 backup: coding style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed May 1, 2019
1 parent a1bda6b commit 1721176
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 73 deletions.
134 changes: 71 additions & 63 deletions backup/util/settings/setting_dependency.class.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -82,7 +81,7 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
* Destroy all circular references. It helps PHP 5.2 a lot!
*/
public function destroy() {
// No need to destroy anything recursively here, direct reset
// No need to destroy anything recursively here, direct reset.
$this->setting = null;
$this->dependentsetting = null;
}
Expand All @@ -94,16 +93,19 @@ public function destroy() {
* @return bool
*/
final public function process_change($changetype, $oldvalue) {
// Check the type of change requested
// Check the type of change requested.
switch ($changetype) {
// Process a status change
case base_setting::CHANGED_STATUS: return $this->process_status_change($oldvalue);
// Process a visibility change
case base_setting::CHANGED_VISIBILITY: return $this->process_visibility_change($oldvalue);
// Process a value change
case base_setting::CHANGED_VALUE: return $this->process_value_change($oldvalue);
// Process a status change.
case base_setting::CHANGED_STATUS:
return $this->process_status_change($oldvalue);
// Process a visibility change.
case base_setting::CHANGED_VISIBILITY:
return $this->process_visibility_change($oldvalue);
// Process a value change.
case base_setting::CHANGED_VALUE:
return $this->process_value_change($oldvalue);
}
// Throw an exception if we get this far
// Throw an exception if we get this far.
throw new backup_ui_exception('unknownchangetype');
}
/**
Expand All @@ -112,11 +114,11 @@ final public function process_change($changetype, $oldvalue) {
* @return bool
*/
protected function process_visibility_change($oldvisibility) {
// Store the current dependent settings visibility for comparison
// Store the current dependent settings visibility for comparison.
$prevalue = $this->dependentsetting->get_visibility();
// Set it regardless of whether we need to
// Set it regardless of whether we need to.
$this->dependentsetting->set_visibility($this->setting->get_visibility());
// Return true if it changed
// Return true if it changed.
return ($prevalue != $this->dependentsetting->get_visibility());
}
/**
Expand Down Expand Up @@ -182,15 +184,16 @@ class setting_dependency_disabledif_equals extends setting_dependency {
*/
public function __construct(base_setting $setting, base_setting $dependentsetting, $value, $defaultvalue = false) {
parent::__construct($setting, $dependentsetting, $defaultvalue);
$this->value = ($value)?(string)$value:0;
$this->value = ($value) ? (string)$value : 0;
}
/**
* Returns true if the dependent setting is locked by this setting_dependency.
* @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->evaluate_disabled_condition($this->setting->get_value())) {
if ($this->setting->get_status() !== base_setting::NOT_LOCKED ||
$this->evaluate_disabled_condition($this->setting->get_value())) {
return true;
}
// Else the dependent setting is not locked by this setting_dependency.
Expand Down Expand Up @@ -223,10 +226,10 @@ protected function process_value_change($oldvalue) {
$this->dependentsetting->set_value($this->defaultvalue);
}
} else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
// We can unlock the dependent setting
// We can unlock the dependent setting.
$this->dependentsetting->set_status(base_setting::NOT_LOCKED);
}
// Return true if the value has changed for the dependent setting
// Return true if the value has changed for the dependent setting.
return ($prevalue != $this->dependentsetting->get_value());
}
/**
Expand All @@ -235,17 +238,18 @@ protected function process_value_change($oldvalue) {
* @return bool
*/
protected function process_status_change($oldstatus) {
// Store the dependent status
// Store the dependent status.
$prevalue = $this->dependentsetting->get_status();
// Store the current status
// Store the current status.
$currentstatus = $this->setting->get_status();
if ($currentstatus == base_setting::NOT_LOCKED) {
if ($prevalue == base_setting::LOCKED_BY_HIERARCHY && !$this->evaluate_disabled_condition($this->setting->get_value())) {
if ($prevalue == base_setting::LOCKED_BY_HIERARCHY &&
!$this->evaluate_disabled_condition($this->setting->get_value())) {
// Dependency has changes, is not fine, unlock the dependent setting.
$this->dependentsetting->set_status(base_setting::NOT_LOCKED);
}
} else {
// Make sure the dependent setting is also locked, in this case by hierarchy
// Make sure the dependent setting is also locked, in this case by hierarchy.
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
}
// Return true if the dependent setting has changed.
Expand All @@ -256,17 +260,17 @@ protected function process_status_change($oldstatus) {
* @return bool True if there were changes
*/
public function enforce() {
// This will be set to true if ANYTHING changes
// This will be set to true if ANYTHING changes.
$changes = false;
// First process any value changes
// First process any value changes.
if ($this->process_value_change($this->setting->get_value())) {
$changes = true;
}
// Second process any status changes
// Second process any status changes.
if ($this->process_status_change($this->setting->get_status())) {
$changes = true;
}
// Finally process visibility changes
// Finally process visibility changes.
if ($this->process_visibility_change($this->setting->get_visibility())) {
$changes = true;
}
Expand All @@ -279,10 +283,10 @@ public function enforce() {
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'eq',
'value'=>$this->value
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'eq',
'value' => $this->value
);
}

Expand All @@ -299,12 +303,12 @@ protected function evaluate_disabled_condition($value) {
}

/**
* A dependency that disables the secondary setting if the primary setting is
* not equal to the provided value
*
* @copyright 2011 Darko Miletic <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
* A dependency that disables the secondary setting if the primary setting is
* not equal to the provided value
*
* @copyright 2011 Darko Miletic <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_not_equals extends setting_dependency_disabledif_equals {

/**
Expand All @@ -317,20 +321,23 @@ protected function evaluate_disabled_condition($value) {
}

/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
* @return array
*/
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
* @return array
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'notequal',
'value'=>$this->value
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notequal',
'value' => $this->value
);
}
}

/**
* Disable if a value is in a list.
*/
class setting_dependency_disabledif_in_array extends setting_dependency_disabledif_equals {

/**
Expand All @@ -349,16 +356,17 @@ protected function evaluate_disabled_condition($value) {
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'eq',
'value'=>$this->value
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'eq',
'value' => $this->value
);
}

}

// This class is here for backwards compatibility (terrible name).
/**
* This class is here for backwards compatibility (terrible name).
*/
class setting_dependency_disabledif_equals2 extends setting_dependency_disabledif_in_array {
}

Expand All @@ -381,9 +389,9 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'checked'
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'checked'
);
}
}
Expand All @@ -407,9 +415,9 @@ public function __construct(base_setting $setting, base_setting $dependentsettin
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'notchecked'
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notchecked'
);
}
}
Expand Down Expand Up @@ -443,10 +451,10 @@ protected function evaluate_disabled_condition($value) {
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'notequal',
'value'=>''
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notequal',
'value' => ''
);
}
}
Expand Down Expand Up @@ -480,10 +488,10 @@ protected function evaluate_disabled_condition($value) {
*/
public function get_moodleform_properties() {
return array(
'setting'=>$this->dependentsetting->get_ui_name(),
'dependenton'=>$this->setting->get_ui_name(),
'condition'=>'notequal',
'value'=>''
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notequal',
'value' => ''
);
}
}
20 changes: 10 additions & 10 deletions backup/util/settings/tests/settings_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class backp_settings_testcase extends basic_testcase {
/**
* test base_setting class
*/
function test_base_setting() {
public function test_base_setting() {
// Instantiate base_setting and check everything
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN);
$this->assertTrue($bs instanceof base_setting);
Expand Down Expand Up @@ -294,12 +294,12 @@ function test_base_setting() {
* Test that locked and unlocked states on dependent backup settings at the same level
* correctly do not flow from the parent to the child setting when the setting is locked by permissions.
*/
function test_dependency_empty_locked_by_permission_child_is_not_unlocked() {
// Check dependencies are working ok
public function test_dependency_empty_locked_by_permission_child_is_not_unlocked() {
// Check dependencies are working ok.
$bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, 2);
$bs1->set_level(1);
$bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, 2);
$bs2->set_level(1); // Same level *must* work
$bs2->set_level(1); // Same level *must* work.
$bs1->add_dependency($bs2, setting_dependency::DISABLED_EMPTY);

$bs1->set_status(base_setting::LOCKED_BY_PERMISSION);
Expand All @@ -318,11 +318,11 @@ function test_dependency_empty_locked_by_permission_child_is_not_unlocked() {
* Test that locked and unlocked states on dependent backup settings at the same level
* correctly do flow from the parent to the child setting when the setting is locked by config.
*/
function test_dependency_not_empty_locked_by_config_parent_is_unlocked() {
public function test_dependency_not_empty_locked_by_config_parent_is_unlocked() {
$bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, 0);
$bs1->set_level(1);
$bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, 0);
$bs2->set_level(1); // Same level *must* work
$bs2->set_level(1); // Same level *must* work.
$bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY);

$bs1->set_status(base_setting::LOCKED_BY_CONFIG);
Expand All @@ -337,7 +337,7 @@ function test_dependency_not_empty_locked_by_config_parent_is_unlocked() {
/**
* test backup_setting class
*/
function test_backup_setting() {
public function test_backup_setting() {
// Instantiate backup_setting class and set level
$bs = new mock_backup_setting('test', base_setting::IS_INTEGER, null);
$bs->set_level(1);
Expand Down Expand Up @@ -384,7 +384,7 @@ function test_backup_setting() {
/**
* test activity_backup_setting class
*/
function test_activity_backup_setting() {
public function test_activity_backup_setting() {
$bs = new mock_activity_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::ACTIVITY_LEVEL);

Expand All @@ -399,7 +399,7 @@ function test_activity_backup_setting() {
/**
* test section_backup_setting class
*/
function test_section_backup_setting() {
public function test_section_backup_setting() {
$bs = new mock_section_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::SECTION_LEVEL);

Expand All @@ -414,7 +414,7 @@ function test_section_backup_setting() {
/**
* test course_backup_setting class
*/
function test_course_backup_setting() {
public function test_course_backup_setting() {
$bs = new mock_course_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::COURSE_LEVEL);

Expand Down

0 comments on commit 1721176

Please sign in to comment.