backup MDL-22142 This is the user interface for backups, several minor/cosmetic issue...
[moodle.git] / backup / util / settings / setting_dependency.class.php
CommitLineData
1904e9b3
SH
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * @package moodlecore
20 * @copyright 2010 Sam Hemelryk
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24/**
25 * Generic abstract dependency class
26 *
27 * @copyright 2010 Sam Hemelryk
28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 */
30abstract class setting_dependency {
31
32 /**
33 * Used to define the type of a dependency.
34 *
35 * Note with these that checked and true, and not checked and false are equal.
36 * This is because the terminology differs but the resulting action is the same.
37 * Reduces code!
38 */
39 const DISABLED_VALUE = 0;
40 const DISABLED_NOT_VALUE = 1;
41 const DISABLED_CHECKED = 2;
42 const DISABLED_TRUE = 2;
43 const DISABLED_NOT_CHECKED = 3;
44 const DISABLED_FALSE = 3;
45
46 /**
47 * The parent setting (primary)
48 * @var base_setting
49 */
50 protected $setting;
51 /**
52 * The dependent setting (secondary)
53 * @var base_setting
54 */
55 protected $dependentsetting;
56 /**
57 * The default setting
58 * @var mixed
59 */
60 protected $defaultvalue;
61 /**
62 * The last value the dependent setting had
63 * @var mixed
64 */
65 protected $lastvalue;
66 /**
67 * Creates the dependency object
68 * @param base_setting $setting The parent setting or the primary setting if you prefer
69 * @param base_setting $dependentsetting The dependent setting
70 * @param mixed $defaultvalue The default value to assign if the dependency is unmet
71 */
72 public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
73 $this->setting = $setting;
74 $this->dependentsetting = $dependentsetting;
75 $this->defaultvalue = $defaultvalue;
76 $this->lastvalue = $dependentsetting->get_value();
77 }
78 /**
79 * Processes a change is setting called by the primary setting
80 * @param int $changetype
81 * @param mixed $oldvalue
82 * @return bool
83 */
84 final public function process_change($changetype, $oldvalue) {
85 // Check the type of change requested
86 switch ($changetype) {
87 // Process a status change
88 case base_setting::CHANGED_STATUS: return $this->process_status_change($oldvalue);
89 // Process a visibility change
90 case base_setting::CHANGED_VISIBILITY: return $this->process_visibility_change($oldvalue);
91 // Process a value change
92 case base_setting::CHANGED_VALUE: return $this->process_value_change($oldvalue);
93 }
94 // Throw an exception if we get this far
95 throw new backup_ui_exception('unknownchangetype');
96 }
97 /**
98 * Processes a visibility change
99 * @param bool $oldvisibility
100 * @return bool
101 */
102 protected function process_visibility_change($oldvisibility) {
103 // Store the current dependent settings visibility for comparison
104 $prevalue = $this->dependentsetting->get_visibility();
105 // Set it regardless of whether we need to
106 $this->dependentsetting->set_visibility($this->setting->get_visibility());
107 // Return true if it changed
108 return ($prevalue != $this->dependentsetting->get_visibility());
109 }
110 /**
111 * All dependencies must define how they would like to deal with a status change
112 * @param int $oldstatus
113 */
114 abstract protected function process_status_change($oldstatus);
115 /**
116 * All dependencies must define how they would like to process a value change
117 */
118 abstract protected function process_value_change($oldvalue);
119 /**
120 * Gets the primary setting
121 * @return backup_setting
122 */
123 public function get_setting() {
124 return $this->setting;
125 }
126 /**
127 * Gets the dependent setting
128 * @return backup_setting
129 */
130 public function get_dependant_setting() {
131 return $this->dependentsetting;
132 }
133}
134
135/**
136 * A dependency that disables the secondary setting if the primary setting is
137 * equal to the provided value
138 *
139 * @copyright 2010 Sam Hemelryk
140 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
141 */
142class setting_dependency_disabledif_equals extends setting_dependency {
143 /**
144 * The value to compare to
145 * @var mixed
146 */
147 protected $value;
148 /**
149 * Creates the dependency
150 *
151 * @param base_setting $setting
152 * @param base_setting $dependentsetting
153 * @param mixed $value
154 * @param mixed $defaultvalue
155 */
156 public function __construct(base_setting $setting, base_setting $dependentsetting, $value, $defaultvalue = false) {
157 parent::__construct($setting, $dependentsetting, $defaultvalue);
158 $this->value = $value;
159 }
160 /**
161 * Processes a value change in the primary setting
162 * @param mixed $oldvalue
163 * @return bool
164 */
165 protected function process_value_change($oldvalue) {
166 $prevalue = $this->dependentsetting->get_value();
167 // If the setting is the desired value enact the dependency
168 if ($this->setting->get_value() == $this->value) {
169 // The dependent setting needs to be locked by hierachy and set to the
170 // default value.
171 $this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
172 $this->dependentsetting->set_value($this->defaultvalue);
173 } else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
174 // We can unlock the dependent setting and reset its value to the
175 // last value the user had for it.
176 $this->dependentsetting->set_status(base_setting::NOT_LOCKED);
177 $this->dependentsetting->set_value($this->lastvalue);
178 }
179 // Return true if the value has changed for the dependent setting
180 return ($prevalue != $this->dependentsetting->get_value());
181 }
182 /**
183 * Processes a status change in the primary setting
184 * @param mixed $oldstatus
185 * @return bool
186 */
187 protected function process_status_change($oldstatus) {
188 // Store the dependent status
189 $prevalue = $this->dependentsetting->get_status();
190 // Store the current status
191 $currentstatus = $this->setting->get_status();
192 if ($currentstatus == base_setting::NOT_LOCKED) {
193 if ($prevalue == base_setting::LOCKED_BY_HIERARCHY && $this->setting->get_value() != $this->value) {
194 // Dependency has changes, is not fine, unlock the dependent setting
195 $this->dependentsetting->set_status(base_setting::NOT_LOCKED);
196 }
197 } else {
198 // Make sure the dependent setting is also locked, in this case by hierarchy
199 $this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
200 }
201 // Return true if the dependent setting has changed.
202 return ($prevalue != $this->dependentsetting->get_status());
203 }
204 /**
205 * Enforces the dependency if required.
206 * @return bool True if there were changes
207 */
208 public function enforce() {
209 // This will be set to true if ANYTHING changes
210 $changes = false;
211 // First process any value changes
212 if ($this->process_value_change($this->setting->get_value())) {
213 $changes = true;
214 }
215 // Second process any status changes
216 if ($this->process_status_change($this->setting->get_status())) {
217 $changes = true;
218 }
219 // Finally process visibility changes
220 if ($this->process_visibility_change($this->setting->get_visibility())) {
221 $changes = true;
222 }
223 return $changes;
224 }
225}
226/**
227 * A dependency that disables the secondary element if the primary element is
228 * true or checked
229 *
230 * @copyright 2010 Sam Hemelryk
231 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
232 */
233class setting_dependency_disabledif_checked extends setting_dependency_disabledif_equals {
234 public function __construct(base_setting $setting, base_setting $dependentsetting) {
235 parent::__construct($setting, $dependentsetting, $defaultvalue = false);
236 $this->value = true;
237 }
238}
239
240/**
241 * A dependency that disables the secondary element if the primary element is
242 * false or not checked
243 *
244 * @copyright 2010 Sam Hemelryk
245 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
246 */
247class setting_dependency_disabledif_notchecked extends setting_dependency_disabledif_equals {
248 public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
249 parent::__construct($setting, $dependentsetting);
250 $this->value = false;
251 }
252}