first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @subpackage backup-settings
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Abstract class containing all the common stuff for activity backup settings
*
* TODO: Finish phpdocs
*/
abstract class activity_backup_setting extends backup_setting {
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
$this->level = self::ACTIVITY_LEVEL;
parent::__construct($name, $vtype, $value, $visibility, $status);
}
}
@@ -0,0 +1,122 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines backup_setting class
*
* @package core_backup
* @category backup
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Parent class for all backup settings
*/
abstract class backup_setting extends base_setting implements checksumable {
// Some constants defining levels of setting
const ROOT_LEVEL = 1;
const COURSE_LEVEL = 5;
const SECTION_LEVEL = 9;
const ACTIVITY_LEVEL = 13;
/** @var int Level of the setting, eg {@link self::ROOT_LEVEL} */
protected $level;
/**
* {@inheritdoc}
*/
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
parent::__construct($name, $vtype, $value, $visibility, $status);
// Generate a default ui
$this->uisetting = new backup_setting_ui_checkbox($this, $name);
}
/**
* @return int Level of the setting, eg {@link self::ROOT_LEVEL}
*/
public function get_level() {
return $this->level;
}
/**
* Creates and sets a user interface for this setting given appropriate arguments
*
* @param int $type
* @param string $label
* @param array $attributes
* @param array $options
*/
public function make_ui($type, $label, array $attributes = null, array $options = null) {
$this->uisetting = backup_setting_ui::make($this, $type, $label, $attributes, $options);
if (is_array($options) || is_object($options)) {
$options = (array)$options;
switch (get_class($this->uisetting)) {
case 'backup_setting_ui_radio' :
// text
if (array_key_exists('text', $options)) {
$this->uisetting->set_text($options['text']);
}
case 'backup_setting_ui_checkbox' :
// value
if (array_key_exists('value', $options)) {
$this->uisetting->set_value($options['value']);
}
break;
case 'backup_setting_ui_select' :
// options
if (array_key_exists('options', $options)) {
$this->uisetting->set_values($options['options']);
}
break;
}
}
}
public function add_dependency(base_setting $dependentsetting, $type=setting_dependency::DISABLED_VALUE, $options=array()) {
if (!($dependentsetting instanceof backup_setting)) {
throw new backup_setting_exception('invalid_backup_setting_parameter');
}
// Check the dependency level is >= current level
if ($dependentsetting->get_level() < $this->level) {
throw new backup_setting_exception('cannot_add_upper_level_dependency');
}
parent::add_dependency($dependentsetting, $type, $options);
}
// checksumable interface methods
public function calculate_checksum() {
// Checksum is a simple md5 hash of name, value, level
// Not following dependencies at all. Each setting will
// calculate its own checksum
return md5($this->name . '-' . $this->value . '-' . $this->level);
}
public function is_checksum_correct($checksum) {
return $this->calculate_checksum() === $checksum;
}
}
/**
* Exception class used by all the @backup_setting stuff
*/
class backup_setting_exception extends base_setting_exception {
}
+573
View File
@@ -0,0 +1,573 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @subpackage backup-settings
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* This abstract class defines one basic setting
*
* Each setting will be able to control its name, value (from a list), ui
* representation (check box, drop down, text field...), visibility, status
* (editable/locked...) and its hierarchy with other settings (using one
* like-observer pattern.
*
* TODO: Finish phpdocs
*/
abstract class base_setting {
// Some constants defining different ui representations for the setting
const UI_NONE = 0;
const UI_HTML_CHECKBOX = 10;
const UI_HTML_RADIOBUTTON = 20;
const UI_HTML_DROPDOWN = 30;
const UI_HTML_TEXTFIELD = 40;
// Type of validation to perform against the value (relaying in PARAM_XXX validations)
const IS_BOOLEAN = 'bool';
const IS_INTEGER = 'int';
const IS_FILENAME= 'file';
const IS_PATH = 'path';
const IS_TEXT = 'text';
// Visible/hidden
const VISIBLE = 1;
const HIDDEN = 0;
// Editable/locked (by different causes)
const NOT_LOCKED = 3;
const LOCKED_BY_CONFIG = 5;
const LOCKED_BY_HIERARCHY = 7;
const LOCKED_BY_PERMISSION = 9;
// Type of change to inform dependencies
const CHANGED_VALUE = 1;
const CHANGED_VISIBILITY = 2;
const CHANGED_STATUS = 3;
protected $name; // name of the setting
protected $value; // value of the setting
protected $unlockedvalue; // Value to set after the setting is unlocked.
protected $vtype; // type of value (setting_base::IS_BOOLEAN/setting_base::IS_INTEGER...)
protected $visibility; // visibility of the setting (setting_base::VISIBLE/setting_base::HIDDEN)
protected $status; // setting_base::NOT_LOCKED/setting_base::LOCKED_BY_PERMISSION...
/** @var setting_dependency[] */
protected $dependencies = array(); // array of dependent (observer) objects (usually setting_base ones)
protected $dependenton = array();
/**
* The user interface for this setting
* @var backup_setting_ui|backup_setting_ui_checkbox|backup_setting_ui_radio|backup_setting_ui_select|backup_setting_ui_text
*/
protected $uisetting;
/**
* An array that contains the identifier and component of a help string if one
* has been set
* @var array
*/
protected $help = array();
/**
* Instantiates a setting object
*
* @param string $name Name of the setting
* @param string $vtype Type of the setting, eg {@link self::IS_TEXT}
* @param mixed $value Value of the setting
* @param bool $visibility Is the setting visible in the UI, eg {@link self::VISIBLE}
* @param int $status Status of the setting with regards to the locking, eg {@link self::NOT_LOCKED}
*/
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
// Check vtype
if ($vtype !== self::IS_BOOLEAN && $vtype !== self::IS_INTEGER &&
$vtype !== self::IS_FILENAME && $vtype !== self::IS_PATH &&
$vtype !== self::IS_TEXT) {
throw new base_setting_exception('setting_invalid_type');
}
// Validate value
$value = $this->validate_value($vtype, $value);
// Check visibility
$visibility = $this->validate_visibility($visibility);
// Check status
$status = $this->validate_status($status);
$this->name = $name;
$this->vtype = $vtype;
$this->value = $value;
$this->visibility = $visibility;
$this->status = $status;
$this->unlockedvalue = $this->value;
// Generate a default ui
$this->uisetting = new base_setting_ui($this);
}
/**
* Destroy all circular references. It helps PHP 5.2 a lot!
*/
public function destroy() {
// Before reseting anything, call destroy recursively
foreach ($this->dependencies as $dependency) {
$dependency->destroy();
}
foreach ($this->dependenton as $dependenton) {
$dependenton->destroy();
}
if ($this->uisetting) {
$this->uisetting->destroy();
}
// Everything has been destroyed recursively, now we can reset safely
$this->dependencies = array();
$this->dependenton = array();
$this->uisetting = null;
}
public function get_name() {
return $this->name;
}
public function get_value() {
return $this->value;
}
public function get_visibility() {
return $this->visibility;
}
public function get_status() {
return $this->status;
}
public function set_value($value) {
// Validate value
$value = $this->validate_value($this->vtype, $value);
// Only can change value if setting is not locked
if ($this->status != self::NOT_LOCKED) {
switch ($this->status) {
case self::LOCKED_BY_PERMISSION:
throw new base_setting_exception('setting_locked_by_permission');
case self::LOCKED_BY_CONFIG:
throw new base_setting_exception('setting_locked_by_config');
}
}
$oldvalue = $this->value;
$this->value = $value;
if ($value !== $oldvalue) { // Value has changed, let's inform dependencies
$this->inform_dependencies(self::CHANGED_VALUE, $oldvalue);
}
}
public function set_visibility($visibility) {
$visibility = $this->validate_visibility($visibility);
// If this setting is dependent on other settings first check that all
// of those settings are visible
if (count($this->dependenton) > 0 && $visibility == base_setting::VISIBLE) {
foreach ($this->dependenton as $dependency) {
if ($dependency->get_setting()->get_visibility() != base_setting::VISIBLE) {
$visibility = base_setting::HIDDEN;
break;
}
}
}
$oldvisibility = $this->visibility;
$this->visibility = $visibility;
if ($visibility !== $oldvisibility) { // Visibility has changed, let's inform dependencies
$this->inform_dependencies(self::CHANGED_VISIBILITY, $oldvisibility);
}
}
public function set_status($status) {
$status = $this->validate_status($status);
if (($this->status == base_setting::LOCKED_BY_PERMISSION || $this->status == base_setting::LOCKED_BY_CONFIG)
&& $status == base_setting::LOCKED_BY_HIERARCHY) {
// Lock by permission or config can not be overriden by lock by hierarchy.
return;
}
// 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->is_locked()) {
// It still needs to be locked
$status = base_setting::LOCKED_BY_HIERARCHY;
break;
}
}
}
$oldstatus = $this->status;
$this->status = $status;
if ($status !== $oldstatus) { // Status has changed, let's inform dependencies
$this->inform_dependencies(self::CHANGED_STATUS, $oldstatus);
if ($status == base_setting::NOT_LOCKED) {
// When setting gets unlocked set it to the original value.
$this->set_value($this->unlockedvalue);
}
}
}
/**
* Gets an array of properties for all of the dependencies that will affect
* this setting.
*
* 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.
*
* This method also ensures that all dependencies are transmuted to affect
* the setting in question and that we don't provide any duplicates.
*
* @param string|null $settingname
* @return array
*/
public function get_my_dependency_properties($settingname=null) {
if ($settingname == null) {
$settingname = $this->get_ui_name();
}
$dependencies = array();
foreach ($this->dependenton as $dependenton) {
$properties = $dependenton->get_moodleform_properties();
$properties['setting'] = $settingname;
$dependencies[$properties['setting'].'-'.$properties['dependenton']] = $properties;
$dependencies = array_merge($dependencies, $dependenton->get_setting()->get_my_dependency_properties($settingname));
}
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
*
* @return bool True if there are other settings that are dependent on this setting
*/
public function has_dependent_settings() {
return (count($this->dependencies)>0);
}
/**
* Checks if this setting is dependent on any other settings
*
* @return bool True if this setting is dependent on any other settings
*/
public function has_dependencies_on_settings() {
return (count($this->dependenton)>0);
}
/**
* Sets the user interface for this setting
*
* @param base_setting_ui $ui
*/
public function set_ui(backup_setting_ui $ui) {
$this->uisetting = $ui;
}
/**
* Gets the user interface for this setting
*
* @return base_setting_ui
*/
public function get_ui() {
return $this->uisetting;
}
/**
* Adds a dependency where another setting depends on this setting.
* @param setting_dependency $dependency
*/
public function register_dependency(setting_dependency $dependency) {
if ($this->is_circular_reference($dependency->get_dependent_setting())) {
$a = new stdclass();
$a->alreadydependent = $this->name;
$a->main = $dependency->get_dependent_setting()->get_name();
throw new base_setting_exception('setting_circular_reference', $a);
}
$this->dependencies[$dependency->get_dependent_setting()->get_name()] = $dependency;
$dependency->get_dependent_setting()->register_dependent_dependency($dependency);
}
/**
* Adds a dependency where this setting is dependent on another.
*
* This should only be called internally once we are sure it is not cicrular.
*
* @param setting_dependency $dependency
*/
protected function register_dependent_dependency(setting_dependency $dependency) {
$this->dependenton[$dependency->get_setting()->get_name()] = $dependency;
}
/**
* Quick method to add a dependency to this setting.
*
* The dependency created is done so by inspecting this setting and the
* setting that is passed in as the dependent setting.
*
* @param base_setting $dependentsetting
* @param int $type One of setting_dependency::*
* @param array $options
*/
public function add_dependency(base_setting $dependentsetting, $type=null, $options=array()) {
if ($this->is_circular_reference($dependentsetting)) {
$a = new stdclass();
$a->alreadydependent = $this->name;
$a->main = $dependentsetting->get_name();
throw new base_setting_exception('setting_circular_reference', $a);
}
// Check the settings hasn't been already added
if (array_key_exists($dependentsetting->get_name(), $this->dependencies)) {
throw new base_setting_exception('setting_already_added');
}
$options = (array)$options;
if (!array_key_exists('defaultvalue', $options)) {
$options['defaultvalue'] = false;
}
if ($type == null) {
switch ($this->vtype) {
case self::IS_BOOLEAN :
if ($this->get_ui_type() == self::UI_HTML_CHECKBOX) {
if ($this->value) {
$type = setting_dependency::DISABLED_NOT_CHECKED;
} else {
$type = setting_dependency::DISABLED_CHECKED;
}
} else {
if ($this->value) {
$type = setting_dependency::DISABLED_FALSE;
} else {
$type = setting_dependency::DISABLED_TRUE;
}
}
break;
case self::IS_FILENAME :
case self::IS_PATH :
case self::IS_INTEGER :
default :
$type = setting_dependency::DISABLED_VALUE;
break;
}
}
switch ($type) {
case setting_dependency::DISABLED_VALUE :
if (!array_key_exists('value', $options)) {
throw new base_setting_exception('dependency_needs_value');
}
$dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, $options['value'], $options['defaultvalue']);
break;
case setting_dependency::DISABLED_TRUE :
$dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, true, $options['defaultvalue']);
break;
case setting_dependency::DISABLED_FALSE :
$dependency = new setting_dependency_disabledif_equals($this, $dependentsetting, false, $options['defaultvalue']);
break;
case setting_dependency::DISABLED_CHECKED :
$dependency = new setting_dependency_disabledif_checked($this, $dependentsetting, $options['defaultvalue']);
break;
case setting_dependency::DISABLED_NOT_CHECKED :
$dependency = new setting_dependency_disabledif_not_checked($this, $dependentsetting, $options['defaultvalue']);
break;
case setting_dependency::DISABLED_EMPTY :
$dependency = new setting_dependency_disabledif_empty($this, $dependentsetting, $options['defaultvalue']);
break;
case setting_dependency::DISABLED_NOT_EMPTY :
$dependency = new setting_dependency_disabledif_not_empty($this, $dependentsetting, $options['defaultvalue']);
break;
}
$this->dependencies[$dependentsetting->get_name()] = $dependency;
$dependency->get_dependent_setting()->register_dependent_dependency($dependency);
}
/**
* Get the PARAM_XXXX validation to be applied to the setting
*
* @return string The PARAM_XXXX constant of null if the setting type is not defined
*/
public function get_param_validation() {
switch ($this->vtype) {
case self::IS_BOOLEAN:
return PARAM_BOOL;
case self::IS_INTEGER:
return PARAM_INT;
case self::IS_FILENAME:
return PARAM_FILE;
case self::IS_PATH:
return PARAM_PATH;
case self::IS_TEXT:
return PARAM_TEXT;
}
return null;
}
// Protected API starts here
protected function validate_value($vtype, $value) {
if (is_null($value)) { // Nulls aren't validated
return null;
}
$oldvalue = $value;
switch ($vtype) {
case self::IS_BOOLEAN:
$value = clean_param($oldvalue, PARAM_BOOL); // Just clean
break;
case self::IS_INTEGER:
$value = clean_param($oldvalue, PARAM_INT);
if ($value != $oldvalue) {
throw new base_setting_exception('setting_invalid_integer', $oldvalue);
}
break;
case self::IS_FILENAME:
$value = clean_param($oldvalue, PARAM_FILE);
if ($value != $oldvalue) {
throw new base_setting_exception('setting_invalid_filename', $oldvalue);
}
break;
case self::IS_PATH:
$value = clean_param($oldvalue, PARAM_PATH);
if ($value != $oldvalue) {
throw new base_setting_exception('setting_invalid_path', $oldvalue);
}
break;
case self::IS_TEXT:
$value = clean_param($oldvalue, PARAM_TEXT);
if ($value != $oldvalue) {
throw new base_setting_exception('setting_invalid_text', $oldvalue);
}
break;
}
return $value;
}
protected function validate_visibility($visibility) {
if (is_null($visibility)) {
$visibility = self::VISIBLE;
}
if ($visibility !== self::VISIBLE && $visibility !== self::HIDDEN) {
throw new base_setting_exception('setting_invalid_visibility');
}
return $visibility;
}
protected function validate_status($status) {
if (is_null($status)) {
$status = self::NOT_LOCKED;
}
if ($status !== self::NOT_LOCKED && $status !== self::LOCKED_BY_CONFIG &&
$status !== self::LOCKED_BY_PERMISSION && $status !== self::LOCKED_BY_HIERARCHY) {
throw new base_setting_exception('setting_invalid_status', $status);
}
return $status;
}
protected function inform_dependencies($ctype, $oldv) {
foreach ($this->dependencies as $dependency) {
$dependency->process_change($ctype, $oldv);
}
}
protected function is_circular_reference($obj) {
// Get object dependencies recursively and check (by name) if $this is already there
$dependencies = $obj->get_dependencies();
if (array_key_exists($this->name, $dependencies) || $obj == $this) {
return true;
}
// Recurse the dependent settings one by one
foreach ($dependencies as $dependency) {
if ($dependency->get_dependent_setting()->is_circular_reference($obj)) {
return true;
}
}
return false;
}
public function get_dependencies() {
return $this->dependencies;
}
public function get_ui_name() {
return $this->uisetting->get_name();
}
public function get_ui_type() {
return $this->uisetting->get_type();
}
/**
* Sets a help string for this setting
*
* @param string $identifier
* @param string $component
*/
public function set_help($identifier, $component='moodle') {
$this->help = array($identifier, $component);
}
/**
* Gets the help string params for this setting if it has been set
* @return array|false An array (identifier, component) or false if not set
*/
public function get_help() {
if ($this->has_help()) {
return $this->help;
}
return false;
}
/**
* Returns true if help has been set for this setting
* @return cool
*/
public function has_help() {
return (!empty($this->help));
}
}
/*
* Exception class used by all the @setting_base stuff
*/
class base_setting_exception extends backup_exception {
public function __construct($errorcode, $a=NULL, $debuginfo=null) {
parent::__construct($errorcode, $a, $debuginfo);
}
}
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @subpackage backup-settings
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Abstract class containing all the common stuff for course backup settings
*
* TODO: Finish phpdocs
*/
abstract class course_backup_setting extends backup_setting {
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
$this->level = self::COURSE_LEVEL;
parent::__construct($name, $vtype, $value, $visibility, $status);
}
}
@@ -0,0 +1,41 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines root_backup_setting class
*
* @package core_backup
* @category backup
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Abstract class containing all the common stuff for root backup settings
*/
abstract class root_backup_setting extends backup_setting {
/**
* {@inheritdoc}
*/
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
$this->level = self::ROOT_LEVEL;
parent::__construct($name, $vtype, $value, $visibility, $status);
}
}
@@ -0,0 +1,36 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @subpackage backup-settings
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Abstract class containing all the common stuff for section backup settings
*
* TODO: Finish phpdocs
*/
abstract class section_backup_setting extends backup_setting {
public function __construct($name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED) {
$this->level = self::SECTION_LEVEL;
parent::__construct($name, $vtype, $value, $visibility, $status);
}
}
@@ -0,0 +1,497 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package moodlecore
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Generic abstract dependency class
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class setting_dependency {
/**
* Used to define the type of a dependency.
*
* Note with these that checked and true, and not checked and false are equal.
* This is because the terminology differs but the resulting action is the same.
* Reduces code!
*/
const DISABLED_VALUE = 0;
const DISABLED_NOT_VALUE = 1;
const DISABLED_TRUE = 2;
const DISABLED_FALSE = 3;
const DISABLED_CHECKED = 4;
const DISABLED_NOT_CHECKED = 5;
const DISABLED_EMPTY = 6;
const DISABLED_NOT_EMPTY = 7;
/**
* The parent setting (primary)
* @var base_setting
*/
protected $setting;
/**
* The dependent setting (secondary)
* @var base_setting
*/
protected $dependentsetting;
/**
* The default setting
* @var mixed
*/
protected $defaultvalue;
/**
* The last value the dependent setting had
* @var mixed
*/
protected $lastvalue;
/**
* Creates the dependency object
* @param base_setting $setting The parent setting or the primary setting if you prefer
* @param base_setting $dependentsetting The dependent setting
* @param mixed $defaultvalue The default value to assign if the dependency is unmet
*/
public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
$this->setting = $setting;
$this->dependentsetting = $dependentsetting;
$this->defaultvalue = $defaultvalue;
$this->lastvalue = $dependentsetting->get_value();
}
/**
* Destroy all circular references. It helps PHP 5.2 a lot!
*/
public function destroy() {
// No need to destroy anything recursively here, direct reset.
$this->setting = null;
$this->dependentsetting = null;
}
/**
* Processes a change is setting called by the primary setting
* @param int $changetype
* @param mixed $oldvalue
* @return bool
*/
final public function process_change($changetype, $oldvalue) {
// 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);
}
// Throw an exception if we get this far.
throw new backup_ui_exception('unknownchangetype');
}
/**
* Processes a visibility change
* @param bool $oldvisibility
* @return bool
*/
protected function process_visibility_change($oldvisibility) {
// Store the current dependent settings visibility for comparison.
$prevalue = $this->dependentsetting->get_visibility();
// Set it regardless of whether we need to.
$this->dependentsetting->set_visibility($this->setting->get_visibility());
// Return true if it changed.
return ($prevalue != $this->dependentsetting->get_visibility());
}
/**
* All dependencies must define how they would like to deal with a status change
* @param int $oldstatus
*/
abstract protected function process_status_change($oldstatus);
/**
* All dependencies must define how they would like to process a value change
*/
abstract protected function process_value_change($oldvalue);
/**
* Gets the primary setting
* @return backup_setting
*/
public function get_setting() {
return $this->setting;
}
/**
* Gets the dependent setting
* @return backup_setting
*/
public function get_dependent_setting() {
return $this->dependentsetting;
}
/**
* This function enforces the dependency
*/
abstract public function enforce();
/**
* Returns an array of properties suitable to be used to define a moodleforms
* disabled command
* @return array
*/
abstract public function get_moodleform_properties();
/**
* Returns true if the dependent setting is locked by this setting_dependency.
* @return bool
*/
abstract public function is_locked();
}
/**
* A dependency that disables the secondary setting if the primary setting is
* equal to the provided value
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_equals extends setting_dependency {
/**
* 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, $value, $defaultvalue = false) {
parent::__construct($setting, $dependentsetting, $defaultvalue);
$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())) {
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) {
if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_PERMISSION ||
$this->dependentsetting->get_status() == base_setting::LOCKED_BY_CONFIG) {
// When setting is locked by permission or config do not apply dependencies.
return false;
}
$prevalue = $this->dependentsetting->get_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);
// 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);
}
// 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 &&
!$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.
$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;
}
/**
* 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' => 'eq',
'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;
}
}
/**
* A dependency that disables the secondary setting if the primary setting is
* not equal to the provided value
*
* @copyright 2011 Darko Miletic <dmiletic@moodlerooms.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_not_equals extends setting_dependency_disabledif_equals {
/**
* 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
* @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
);
}
}
/**
* Disable if a value is in a list.
*/
class setting_dependency_disabledif_in_array extends setting_dependency_disabledif_equals {
/**
* 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 in_array($value, $this->value);
}
/**
* 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' => 'eq',
'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
* true or checked
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_checked extends setting_dependency_disabledif_equals {
public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
parent::__construct($setting, $dependentsetting, true, $defaultvalue);
$this->value = true;
}
/**
* 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' => 'checked'
);
}
}
/**
* A dependency that disables the secondary element if the primary element is
* false or not checked
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_not_checked extends setting_dependency_disabledif_equals {
public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
parent::__construct($setting, $dependentsetting, false, $defaultvalue);
$this->value = false;
}
/**
* 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' => 'notchecked'
);
}
}
/**
* A dependency that disables the secondary setting if the value of the primary setting
* is not empty.
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_not_empty extends setting_dependency_disabledif_equals {
public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
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
* @return array
*/
public function get_moodleform_properties() {
return array(
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notequal',
'value' => ''
);
}
}
/**
* A dependency that disables the secondary setting if the value of the primary setting
* is empty.
*
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class setting_dependency_disabledif_empty extends setting_dependency_disabledif_equals {
public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
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
* @return array
*/
public function get_moodleform_properties() {
return array(
'setting' => $this->dependentsetting->get_ui_name(),
'dependenton' => $this->setting->get_ui_name(),
'condition' => 'notequal',
'value' => ''
);
}
}
@@ -0,0 +1,528 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Setting tests (all).
*
* @package core_backup
* @category test
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_backup;
use activity_backup_setting;
use backup_setting;
use backup_setting_exception;
use base_setting;
use base_setting_exception;
use course_backup_setting;
use section_backup_setting;
use setting_dependency;
use setting_dependency_disabledif_empty;
defined('MOODLE_INTERNAL') || die();
// Include all the needed stuff
global $CFG;
require_once($CFG->dirroot . '/backup/util/interfaces/checksumable.class.php');
require_once($CFG->dirroot . '/backup/backup.class.php');
require_once($CFG->dirroot . '/backup/util/settings/base_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php');
require_once($CFG->dirroot . '/backup/util/settings/root/root_backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/activity/activity_backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/section/section_backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/settings/course/course_backup_setting.class.php');
require_once($CFG->dirroot . '/backup/util/ui/backup_ui_setting.class.php');
/**
* Setting tests (all).
*
* @package core_backup
* @category test
* @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class settings_test extends \basic_testcase {
/**
* test base_setting class
*/
public function test_base_setting(): void {
// Instantiate base_setting and check everything
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN);
$this->assertTrue($bs instanceof base_setting);
$this->assertEquals($bs->get_name(), 'test');
$this->assertEquals($bs->get_vtype(), base_setting::IS_BOOLEAN);
$this->assertTrue(is_null($bs->get_value()));
$this->assertEquals($bs->get_visibility(), base_setting::VISIBLE);
$this->assertEquals($bs->get_status(), base_setting::NOT_LOCKED);
// Instantiate base_setting with explicit nulls
$bs = new mock_base_setting('test', base_setting::IS_FILENAME, 'filename.txt', null, null);
$this->assertEquals($bs->get_value() , 'filename.txt');
$this->assertEquals($bs->get_visibility(), base_setting::VISIBLE);
$this->assertEquals($bs->get_status(), base_setting::NOT_LOCKED);
// Instantiate base_setting and set value, visibility and status
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN);
$bs->set_value(true);
$this->assertNotEmpty($bs->get_value());
$bs->set_visibility(base_setting::HIDDEN);
$this->assertEquals($bs->get_visibility(), base_setting::HIDDEN);
$bs->set_status(base_setting::LOCKED_BY_HIERARCHY);
$this->assertEquals($bs->get_status(), base_setting::LOCKED_BY_HIERARCHY);
// Instantiate with wrong vtype
try {
$bs = new mock_base_setting('test', 'one_wrong_type');
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_invalid_type');
}
// Instantiate with wrong integer value
try {
$bs = new mock_base_setting('test', base_setting::IS_INTEGER, 99.99);
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_invalid_integer');
}
// Instantiate with wrong filename value
try {
$bs = new mock_base_setting('test', base_setting::IS_FILENAME, '../../filename.txt');
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_invalid_filename');
}
// Instantiate with wrong visibility
try {
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, 'one_wrong_visibility');
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_invalid_visibility');
}
// Instantiate with wrong status
try {
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, 'one_wrong_status');
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_invalid_status');
}
// Instantiate base_setting and try to set wrong ui_type
// We need a custom error handler to catch the type hinting error
// that should return incorrect_object_passed
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN);
set_error_handler('\core_backup\backup_setting_error_handler', E_RECOVERABLE_ERROR);
try {
$bs->set_ui('one_wrong_ui_type', 'label', array(), array());
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'incorrect_object_passed');
} catch (\TypeError $e) {
// On PHP7+ we get a TypeError raised, lets check we've the right error.
$this->assertMatchesRegularExpression('/must be (of type|an instance of) backup_setting_ui/', $e->getMessage());
}
restore_error_handler();
// Instantiate base_setting and try to set wrong ui_label
// We need a custom error handler to catch the type hinting error
// that should return incorrect_object_passed
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN);
set_error_handler('\core_backup\backup_setting_error_handler', E_RECOVERABLE_ERROR);
try {
$bs->set_ui(base_setting::UI_HTML_CHECKBOX, 'one/wrong/label', array(), array());
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'incorrect_object_passed');
} catch (\TypeError $e) {
// On PHP7+ we get a TypeError raised, lets check we've the right error.
$this->assertMatchesRegularExpression('/must be (of type|an instance of) backup_setting_ui/', $e->getMessage());
}
restore_error_handler();
// Try to change value of locked setting by permission
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_PERMISSION);
try {
$bs->set_value(true);
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_locked_by_permission');
}
// Try to change value of locked setting by config
$bs = new mock_base_setting('test', base_setting::IS_BOOLEAN, null, null, base_setting::LOCKED_BY_CONFIG);
try {
$bs->set_value(true);
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_locked_by_config');
}
// Try to add same setting twice
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
$bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null);
$bs1->add_dependency($bs2, null, array('value'=>0));
try {
$bs1->add_dependency($bs2);
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_already_added');
}
// Try to create one circular reference
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
try {
$bs1->add_dependency($bs1); // self
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_circular_reference');
$this->assertTrue($e->a instanceof \stdClass);
$this->assertEquals($e->a->main, 'test1');
$this->assertEquals($e->a->alreadydependent, 'test1');
}
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
$bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null);
$bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null);
$bs4 = new mock_base_setting('test4', base_setting::IS_INTEGER, null);
$bs1->add_dependency($bs2, null, array('value'=>0));
$bs2->add_dependency($bs3, null, array('value'=>0));
$bs3->add_dependency($bs4, null, array('value'=>0));
try {
$bs4->add_dependency($bs1, null, array('value'=>0));
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_circular_reference');
$this->assertTrue($e->a instanceof \stdClass);
$this->assertEquals($e->a->main, 'test1');
$this->assertEquals($e->a->alreadydependent, 'test4');
}
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
$bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null);
$bs1->register_dependency(new setting_dependency_disabledif_empty($bs1, $bs2));
try {
// $bs1 is already dependent on $bs2 so this should fail.
$bs2->register_dependency(new setting_dependency_disabledif_empty($bs2, $bs1));
$this->assertTrue(false, 'base_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof base_setting_exception);
$this->assertEquals($e->errorcode, 'setting_circular_reference');
$this->assertTrue($e->a instanceof \stdClass);
$this->assertEquals($e->a->main, 'test1');
$this->assertEquals($e->a->alreadydependent, 'test2');
}
// Create 3 settings and observe between them, last one must
// automatically inherit all the settings defined in the main one
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
$bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null);
$bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null);
$bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY);
$bs2->add_dependency($bs3, setting_dependency::DISABLED_NOT_EMPTY);
// Check values are spreaded ok
$bs1->set_value(123);
$this->assertEquals($bs1->get_value(), 123);
$this->assertEquals($bs2->get_value(), $bs1->get_value());
$this->assertEquals($bs3->get_value(), $bs1->get_value());
// Add one more setting and set value again
$bs4 = new mock_base_setting('test4', base_setting::IS_INTEGER, null);
$bs2->add_dependency($bs4, setting_dependency::DISABLED_NOT_EMPTY);
$bs2->set_value(321);
// The above change should change
$this->assertEquals($bs1->get_value(), 123);
$this->assertEquals($bs2->get_value(), 321);
$this->assertEquals($bs3->get_value(), 321);
$this->assertEquals($bs4->get_value(), 321);
// Check visibility is spreaded ok
$bs1->set_visibility(base_setting::HIDDEN);
$this->assertEquals($bs2->get_visibility(), $bs1->get_visibility());
$this->assertEquals($bs3->get_visibility(), $bs1->get_visibility());
// Check status is spreaded ok
$bs1->set_status(base_setting::LOCKED_BY_HIERARCHY);
$this->assertEquals($bs2->get_status(), $bs1->get_status());
$this->assertEquals($bs3->get_status(), $bs1->get_status());
// Create 3 settings and observe between them, put them in one array,
// force serialize/deserialize to check the observable pattern continues
// working after that
$bs1 = new mock_base_setting('test1', base_setting::IS_INTEGER, null);
$bs2 = new mock_base_setting('test2', base_setting::IS_INTEGER, null);
$bs3 = new mock_base_setting('test3', base_setting::IS_INTEGER, null);
$bs1->add_dependency($bs2, null, array('value'=>0));
$bs2->add_dependency($bs3, null, array('value'=>0));
// Serialize
$arr = array($bs1, $bs2, $bs3);
$ser = base64_encode(serialize($arr));
// Unserialize and copy to new objects
$newarr = unserialize(base64_decode($ser));
$ubs1 = $newarr[0];
$ubs2 = $newarr[1];
$ubs3 = $newarr[2];
// Must continue being base settings
$this->assertTrue($ubs1 instanceof base_setting);
$this->assertTrue($ubs2 instanceof base_setting);
$this->assertTrue($ubs3 instanceof base_setting);
// Set parent setting
$ubs1->set_value(1234);
$ubs1->set_visibility(base_setting::HIDDEN);
$ubs1->set_status(base_setting::LOCKED_BY_HIERARCHY);
// Check changes have been spreaded
$this->assertEquals($ubs2->get_visibility(), $ubs1->get_visibility());
$this->assertEquals($ubs3->get_visibility(), $ubs1->get_visibility());
$this->assertEquals($ubs2->get_status(), $ubs1->get_status());
$this->assertEquals($ubs3->get_status(), $ubs1->get_status());
}
/**
* 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.
*/
public function test_dependency_empty_locked_by_permission_child_is_not_unlocked(): void {
// 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.
$bs1->add_dependency($bs2, setting_dependency::DISABLED_EMPTY);
$bs1->set_status(base_setting::LOCKED_BY_PERMISSION);
$this->assertEquals(base_setting::LOCKED_BY_HIERARCHY, $bs2->get_status());
$this->assertEquals(base_setting::LOCKED_BY_PERMISSION, $bs1->get_status());
$bs2->set_status(base_setting::LOCKED_BY_PERMISSION);
$this->assertEquals(base_setting::LOCKED_BY_PERMISSION, $bs1->get_status());
// Unlocking the parent should NOT unlock the child.
$bs1->set_status(base_setting::NOT_LOCKED);
$this->assertEquals(base_setting::LOCKED_BY_PERMISSION, $bs2->get_status());
}
/**
* 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.
*/
public function test_dependency_not_empty_locked_by_config_parent_is_unlocked(): void {
$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.
$bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY);
$bs1->set_status(base_setting::LOCKED_BY_CONFIG);
$this->assertEquals(base_setting::LOCKED_BY_HIERARCHY, $bs2->get_status());
$this->assertEquals(base_setting::LOCKED_BY_CONFIG, $bs1->get_status());
// Unlocking the parent should unlock the child.
$bs1->set_status(base_setting::NOT_LOCKED);
$this->assertEquals(base_setting::NOT_LOCKED, $bs2->get_status());
}
/**
* test backup_setting class
*/
public function test_backup_setting(): void {
// Instantiate backup_setting class and set level
$bs = new mock_backup_setting('test', base_setting::IS_INTEGER, null);
$bs->set_level(1);
$this->assertEquals($bs->get_level(), 1);
// Instantiate backup setting class and try to add one non backup_setting dependency
set_error_handler('\core_backup\backup_setting_error_handler', E_RECOVERABLE_ERROR);
$bs = new mock_backup_setting('test', base_setting::IS_INTEGER, null);
try {
$bs->add_dependency(new \stdClass());
$this->assertTrue(false, 'backup_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof backup_setting_exception);
$this->assertEquals($e->errorcode, 'incorrect_object_passed');
} catch (\TypeError $e) {
// On PHP7+ we get a TypeError raised, lets check we've the right error.
$this->assertMatchesRegularExpression('/must be (an instance of|of type) base_setting/', $e->getMessage());
}
restore_error_handler();
// Try to assing upper level dependency
$bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, null);
$bs1->set_level(1);
$bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, null);
$bs2->set_level(2);
try {
$bs2->add_dependency($bs1);
$this->assertTrue(false, 'backup_setting_exception expected');
} catch (\Exception $e) {
$this->assertTrue($e instanceof backup_setting_exception);
$this->assertEquals($e->errorcode, 'cannot_add_upper_level_dependency');
}
// Check dependencies are working ok
$bs1 = new mock_backup_setting('test1', base_setting::IS_INTEGER, null);
$bs1->set_level(1);
$bs2 = new mock_backup_setting('test2', base_setting::IS_INTEGER, null);
$bs2->set_level(1); // Same level *must* work
$bs1->add_dependency($bs2, setting_dependency::DISABLED_NOT_EMPTY);
$bs1->set_value(123456);
$this->assertEquals($bs2->get_value(), $bs1->get_value());
}
/**
* test activity_backup_setting class
*/
public function test_activity_backup_setting(): void {
$bs = new mock_activity_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::ACTIVITY_LEVEL);
// Check checksum implementation is working
$bs1 = new mock_activity_backup_setting('test', base_setting::IS_INTEGER, null);
$bs1->set_value(123);
$checksum = $bs1->calculate_checksum();
$this->assertNotEmpty($checksum);
$this->assertTrue($bs1->is_checksum_correct($checksum));
}
/**
* test section_backup_setting class
*/
public function test_section_backup_setting(): void {
$bs = new mock_section_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::SECTION_LEVEL);
// Check checksum implementation is working
$bs1 = new mock_section_backup_setting('test', base_setting::IS_INTEGER, null);
$bs1->set_value(123);
$checksum = $bs1->calculate_checksum();
$this->assertNotEmpty($checksum);
$this->assertTrue($bs1->is_checksum_correct($checksum));
}
/**
* test course_backup_setting class
*/
public function test_course_backup_setting(): void {
$bs = new mock_course_backup_setting('test', base_setting::IS_INTEGER, null);
$this->assertEquals($bs->get_level(), backup_setting::COURSE_LEVEL);
// Check checksum implementation is working
$bs1 = new mock_course_backup_setting('test', base_setting::IS_INTEGER, null);
$bs1->set_value(123);
$checksum = $bs1->calculate_checksum();
$this->assertNotEmpty($checksum);
$this->assertTrue($bs1->is_checksum_correct($checksum));
}
}
/**
* helper extended base_setting class that makes some methods public for testing
*/
class mock_base_setting extends base_setting {
public function get_vtype() {
return $this->vtype;
}
public function process_change($setting, $ctype, $oldv) {
// Simply, inherit from the main object
$this->set_value($setting->get_value());
$this->set_visibility($setting->get_visibility());
$this->set_status($setting->get_status());
}
}
/**
* helper extended backup_setting class that makes some methods public for testing
*/
class mock_backup_setting extends backup_setting {
public function set_level($level) {
$this->level = $level;
}
public function process_change($setting, $ctype, $oldv) {
// Simply, inherit from the main object
$this->set_value($setting->get_value());
$this->set_visibility($setting->get_visibility());
$this->set_status($setting->get_status());
}
}
/**
* helper extended activity_backup_setting class that makes some methods public for testing
*/
class mock_activity_backup_setting extends activity_backup_setting {
public function process_change($setting, $ctype, $oldv) {
// Do nothing
}
}
/**
* helper extended section_backup_setting class that makes some methods public for testing
*/
class mock_section_backup_setting extends section_backup_setting {
public function process_change($setting, $ctype, $oldv) {
// Do nothing
}
}
/**
* helper extended course_backup_setting class that makes some methods public for testing
*/
class mock_course_backup_setting extends course_backup_setting {
public function process_change($setting, $ctype, $oldv) {
// Do nothing
}
}
/**
* This error handler is used to convert errors to excpetions so that simepltest can
* catch them.
*
* This is required in order to catch type hint mismatches that result in a error
* being thrown. It should only ever be used to catch E_RECOVERABLE_ERROR's.
*
* It throws a backup_setting_exception with 'incorrect_object_passed'
*
* @param int $errno E_RECOVERABLE_ERROR
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return null
*/
function backup_setting_error_handler($errno, $errstr, $errfile, $errline) {
if ($errno !== E_RECOVERABLE_ERROR) {
// Currently we only want to deal with type hinting errors
return false;
}
throw new backup_setting_exception('incorrect_object_passed');
}