Updated the HEAD build version to 20100615
[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;
1904e9b3 41 const DISABLED_TRUE = 2;
1904e9b3 42 const DISABLED_FALSE = 3;
84e546e0
SH
43 const DISABLED_CHECKED = 4;
44 const DISABLED_NOT_CHECKED = 5;
1904e9b3
SH
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 */
e35fb479 130 public function get_dependent_setting() {
1904e9b3
SH
131 return $this->dependentsetting;
132 }
84e546e0
SH
133 /**
134 * This function enforces the dependency
135 */
136 abstract public function enforce();
137 /**
138 * Returns an array of properties suitable to be used to define a moodleforms
139 * disabled command
140 * @return array
141 */
142 abstract public function get_moodleform_properties();
1904e9b3
SH
143}
144
145/**
146 * A dependency that disables the secondary setting if the primary setting is
147 * equal to the provided value
148 *
149 * @copyright 2010 Sam Hemelryk
150 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
151 */
152class setting_dependency_disabledif_equals extends setting_dependency {
153 /**
154 * The value to compare to
155 * @var mixed
156 */
157 protected $value;
158 /**
159 * Creates the dependency
160 *
161 * @param base_setting $setting
162 * @param base_setting $dependentsetting
163 * @param mixed $value
164 * @param mixed $defaultvalue
165 */
166 public function __construct(base_setting $setting, base_setting $dependentsetting, $value, $defaultvalue = false) {
167 parent::__construct($setting, $dependentsetting, $defaultvalue);
84e546e0 168 $this->value = ($value)?(string)$value:0;
1904e9b3
SH
169 }
170 /**
171 * Processes a value change in the primary setting
172 * @param mixed $oldvalue
173 * @return bool
174 */
175 protected function process_value_change($oldvalue) {
176 $prevalue = $this->dependentsetting->get_value();
177 // If the setting is the desired value enact the dependency
178 if ($this->setting->get_value() == $this->value) {
179 // The dependent setting needs to be locked by hierachy and set to the
180 // default value.
181 $this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
182 $this->dependentsetting->set_value($this->defaultvalue);
183 } else if ($this->dependentsetting->get_status() == base_setting::LOCKED_BY_HIERARCHY) {
84e546e0 184 // We can unlock the dependent setting
1904e9b3 185 $this->dependentsetting->set_status(base_setting::NOT_LOCKED);
1904e9b3
SH
186 }
187 // Return true if the value has changed for the dependent setting
188 return ($prevalue != $this->dependentsetting->get_value());
189 }
190 /**
191 * Processes a status change in the primary setting
192 * @param mixed $oldstatus
193 * @return bool
194 */
195 protected function process_status_change($oldstatus) {
196 // Store the dependent status
197 $prevalue = $this->dependentsetting->get_status();
198 // Store the current status
199 $currentstatus = $this->setting->get_status();
200 if ($currentstatus == base_setting::NOT_LOCKED) {
201 if ($prevalue == base_setting::LOCKED_BY_HIERARCHY && $this->setting->get_value() != $this->value) {
202 // Dependency has changes, is not fine, unlock the dependent setting
203 $this->dependentsetting->set_status(base_setting::NOT_LOCKED);
204 }
205 } else {
206 // Make sure the dependent setting is also locked, in this case by hierarchy
207 $this->dependentsetting->set_status(base_setting::LOCKED_BY_HIERARCHY);
208 }
209 // Return true if the dependent setting has changed.
210 return ($prevalue != $this->dependentsetting->get_status());
211 }
212 /**
213 * Enforces the dependency if required.
214 * @return bool True if there were changes
215 */
216 public function enforce() {
217 // This will be set to true if ANYTHING changes
218 $changes = false;
219 // First process any value changes
220 if ($this->process_value_change($this->setting->get_value())) {
221 $changes = true;
222 }
223 // Second process any status changes
224 if ($this->process_status_change($this->setting->get_status())) {
225 $changes = true;
226 }
227 // Finally process visibility changes
228 if ($this->process_visibility_change($this->setting->get_visibility())) {
229 $changes = true;
230 }
231 return $changes;
232 }
84e546e0
SH
233 /**
234 * Returns an array of properties suitable to be used to define a moodleforms
235 * disabled command
236 * @return array
237 */
238 public function get_moodleform_properties() {
239 return array(
240 'setting'=>$this->dependentsetting->get_ui_name(),
241 'dependenton'=>$this->setting->get_ui_name(),
242 'condition'=>'eq',
243 'value'=>$this->value
244 );
245 }
1904e9b3
SH
246}
247/**
248 * A dependency that disables the secondary element if the primary element is
249 * true or checked
250 *
251 * @copyright 2010 Sam Hemelryk
252 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
253 */
254class setting_dependency_disabledif_checked extends setting_dependency_disabledif_equals {
84e546e0
SH
255 public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
256 parent::__construct($setting, $dependentsetting, true, $defaultvalue);
1904e9b3
SH
257 $this->value = true;
258 }
84e546e0
SH
259 /**
260 * Returns an array of properties suitable to be used to define a moodleforms
261 * disabled command
262 * @return array
263 */
264 public function get_moodleform_properties() {
265 return array(
266 'setting'=>$this->dependentsetting->get_ui_name(),
267 'dependenton'=>$this->setting->get_ui_name(),
268 'condition'=>'checked'
269 );
270 }
1904e9b3
SH
271}
272
273/**
274 * A dependency that disables the secondary element if the primary element is
275 * false or not checked
276 *
277 * @copyright 2010 Sam Hemelryk
278 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
279 */
84e546e0 280class setting_dependency_disabledif_not_checked extends setting_dependency_disabledif_equals {
1904e9b3 281 public function __construct(base_setting $setting, base_setting $dependentsetting, $defaultvalue = false) {
84e546e0 282 parent::__construct($setting, $dependentsetting, false, $defaultvalue);
1904e9b3
SH
283 $this->value = false;
284 }
84e546e0
SH
285 /**
286 * Returns an array of properties suitable to be used to define a moodleforms
287 * disabled command
288 * @return array
289 */
290 public function get_moodleform_properties() {
291 return array(
292 'setting'=>$this->dependentsetting->get_ui_name(),
293 'dependenton'=>$this->setting->get_ui_name(),
294 'condition'=>'notchecked'
295 );
296 }
1904e9b3 297}