MDL-60851 backup: Sanitise setting dependencies

The only different between each setting dependency type is the evaluation of the condition,
and the mform js validation arguments - so that should be the only thing that is extended
by each subclass.
This commit is contained in:
Damyon Wiese 2017-11-20 14:19:36 +08:00
parent 81cbb5b4ef
commit a1bda6b152

View File

@ -189,8 +189,8 @@ class setting_dependency_disabledif_equals extends 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->setting->get_value() == $this->value) {
// 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())) {
return true;
}
// Else the dependent setting is not locked by this setting_dependency.
@ -208,12 +208,20 @@ class setting_dependency_disabledif_equals extends setting_dependency {
return false;
}
$prevalue = $this->dependentsetting->get_value();
// If the setting is the desired value enact the dependency
if ($this->setting->get_value() == $this->value) {
// If the setting is the desired value enact the dependency.
$settingvalue = $this->setting->get_value();
if ($this->evaluate_disabled_condition($settingvalue)) {
// The dependent setting needs to be locked by hierachy and set to the
// default value.
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
$this->dependentsetting->set_value($this->defaultvalue);
// For checkboxes the default value is false, but when the setting is
// locked, the value should inherit from the parent setting.
if ($this->defaultvalue === false) {
$this->dependentsetting->set_value($settingvalue);
} else {
$this->dependentsetting->set_value($this->defaultvalue);
}
} else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
// We can unlock the dependent setting
$this->dependentsetting->set_status(base_setting::NOT_LOCKED);
@ -232,8 +240,8 @@ class setting_dependency_disabledif_equals extends setting_dependency {
// Store the current status
$currentstatus = $this->setting->get_status();
if ($currentstatus == base_setting::NOT_LOCKED) {
if ($prevalue == base_setting::LOCKED_BY_HIERARCHY && $this->setting->get_value() != $this->value) {
// Dependency has changes, is not fine, unlock the dependent setting
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 {
@ -277,6 +285,17 @@ class setting_dependency_disabledif_equals extends setting_dependency {
'value'=>$this->value
);
}
/**
* Evaluate the current value of the setting and return true if the dependent setting should be locked or false.
* This function should be abstract, but there will probably be existing sub-classes so we must provide a default
* implementation.
* @param mixed $value The value of the parent setting.
* @return bool
*/
protected function evaluate_disabled_condition($value) {
return $value == $this->value;
}
}
/**
@ -287,27 +306,16 @@ class setting_dependency_disabledif_equals extends setting_dependency {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_not_equals extends setting_dependency_disabledif_equals {
/**
* Enforces the dependency if required.
* @return bool True if there were changes
*/
public function enforce() {
// This will be set to true if ANYTHING changes
$changes = false;
// First process any value changes
if (!$this->process_value_change($this->setting->get_value())) {
$changes = true;
}
// Second process any status changes
if ($this->process_status_change($this->setting->get_status())) {
$changes = true;
}
// Finally process visibility changes
if ($this->process_visibility_change($this->setting->get_visibility())) {
$changes = true;
}
return $changes;
* Evaluate the current value of the setting and return true if the dependent setting should be locked or false.
* @param mixed $value The value of the parent setting.
* @return bool
*/
protected function evaluate_disabled_condition($value) {
return $value != $this->value;
}
/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
@ -323,100 +331,17 @@ class setting_dependency_disabledif_not_equals extends setting_dependency_disabl
}
}
//with array
class setting_dependency_disabledif_equals2 extends setting_dependency {
class setting_dependency_disabledif_in_array extends setting_dependency_disabledif_equals {
/**
* The value to compare to
* @var mixed
*/
protected $value;
/**
* Creates the dependency
*
* @param base_setting $setting
* @param base_setting $dependentsetting
* @param mixed $value
* @param mixed $defaultvalue
*/
public function __construct(base_setting $setting, base_setting $dependentsetting, array $value, $defaultvalue = false) {
parent::__construct($setting, $dependentsetting, $defaultvalue);
$this->value = $value;
}
/**
* Returns true if the dependent setting is locked by this setting_dependency.
* Evaluate the current value of the setting and return true if the dependent setting should be locked or false.
* @param mixed $value The value of the parent setting.
* @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 || in_array($this->setting->get_value(), $this->value)) {
return true;
}
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
/**
* Processes a value change in the primary setting
* @param mixed $oldvalue
* @return bool
*/
protected function process_value_change($oldvalue) {
$prevalue = $this->dependentsetting->get_value();
// If the setting is the desired value enact the dependency
if (in_array($this->setting->get_value(), $this->value)) {
// The dependent setting needs to be locked by hierachy and set to the
// default value.
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
$this->dependentsetting->set_value($this->defaultvalue);
} else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
// 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 ($prevalue != $this->dependentsetting->get_value());
}
/**
* Processes a status change in the primary setting
* @param mixed $oldstatus
* @return bool
*/
protected function process_status_change($oldstatus) {
// Store the dependent status
$prevalue = $this->dependentsetting->get_status();
// Store the current status
$currentstatus = $this->setting->get_status();
if ($currentstatus == base_setting::NOT_LOCKED) {
if ($prevalue == base_setting::LOCKED_BY_HIERARCHY && !in_array($this->setting->get_value(), $this->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
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
}
// Return true if the dependent setting has changed.
return ($prevalue != $this->dependentsetting->get_status());
}
/**
* Enforces the dependency if required.
* @return bool True if there were changes
*/
public function enforce() {
// This will be set to true if ANYTHING changes
$changes = false;
// First process any value changes
if ($this->process_value_change($this->setting->get_value())) {
$changes = true;
}
// Second process any status changes
if ($this->process_status_change($this->setting->get_status())) {
$changes = true;
}
// Finally process visibility changes
if ($this->process_visibility_change($this->setting->get_visibility())) {
$changes = true;
}
return $changes;
protected function evaluate_disabled_condition($value) {
return in_array($value, $this->value);
}
/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
@ -430,8 +355,12 @@ class setting_dependency_disabledif_equals2 extends setting_dependency {
'value'=>$this->value
);
}
}
// This class is here for backwards compatibility (terrible name).
class setting_dependency_disabledif_equals2 extends setting_dependency_disabledif_in_array {
}
/**
* A dependency that disables the secondary element if the primary element is
@ -497,6 +426,16 @@ class setting_dependency_disabledif_not_empty extends setting_dependency_disable
parent::__construct($setting, $dependentsetting, false, $defaultvalue);
$this->value = false;
}
/**
* Evaluate the current value of the setting and return true if the dependent setting should be locked or false.
* @param mixed $value The value of the parent setting.
* @return bool
*/
protected function evaluate_disabled_condition($value) {
return !empty($value);
}
/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
@ -510,45 +449,6 @@ class setting_dependency_disabledif_not_empty extends setting_dependency_disable
'value'=>''
);
}
/**
* Processes a value change in the primary setting
* @param mixed $oldvalue
* @return bool
*/
protected function process_value_change($oldvalue) {
$prevalue = $this->dependentsetting->get_value();
// If the setting is the desired value enact the dependency
$value = $this->setting->get_value();
if (!empty($value)) {
// The dependent setting needs to be locked by hierachy and set to the
// default value.
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
if ($this->defaultvalue === false) {
$this->dependentsetting->set_value($value);
} else {
$this->dependentsetting->set_value($this->defaultvalue);
}
} else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
// 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 ($prevalue != $this->dependentsetting->get_value());
}
/**
* 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
$value = $this->setting->get_value();
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || !empty($value)) {
return true;
}
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
}
/**
@ -563,6 +463,16 @@ class setting_dependency_disabledif_empty extends setting_dependency_disabledif_
parent::__construct($setting, $dependentsetting, false, $defaultvalue);
$this->value = false;
}
/**
* Evaluate the current value of the setting and return true if the dependent setting should be locked or false.
* @param mixed $value The value of the parent setting.
* @return bool
*/
protected function evaluate_disabled_condition($value) {
return empty($value);
}
/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
@ -576,42 +486,4 @@ class setting_dependency_disabledif_empty extends setting_dependency_disabledif_
'value'=>''
);
}
/**
* Processes a value change in the primary setting
* @param mixed $oldvalue
* @return bool
*/
protected function process_value_change($oldvalue) {
$prevalue = $this->dependentsetting->get_value();
// If the setting is the desired value enact the dependency
$value = $this->setting->get_value();
if (empty($value)) {
// The dependent setting needs to be locked by hierachy and set to the
// default value.
$this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
if ($this->defaultvalue === false) {
$this->dependentsetting->set_value($value);
} else {
$this->dependentsetting->set_value($this->defaultvalue);
}
} else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
// 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 ($prevalue != $this->dependentsetting->get_value());
}
/**
* 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
$value = $this->setting->get_value();
if ($this->setting->get_status() !== base_setting::NOT_LOCKED || empty($value)) {
return true;
}
// Else the dependent setting is not locked by this setting_dependency.
return false;
}
}