Commit | Line | Data |
---|---|---|
db26acd4 | 1 | <?php |
0c079f19 | 2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
db26acd4 | 4 | // Moodle is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
0c079f19 | 13 | // |
db26acd4 | 14 | // You should have received a copy of the GNU General Public License |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
88a7228a | 16 | |
17 | /** | |
0c079f19 | 18 | * Functions and classes used during installation, upgrades and for admin settings. |
db26acd4 | 19 | * |
0c079f19 | 20 | * ADMIN SETTINGS TREE INTRODUCTION |
db26acd4 | 21 | * |
22 | * This file performs the following tasks: | |
23 | * -it defines the necessary objects and interfaces to build the Moodle | |
24 | * admin hierarchy | |
61ef8f9f | 25 | * -it defines the admin_externalpage_setup() |
db26acd4 | 26 | * |
27 | * ADMIN_SETTING OBJECTS | |
28 | * | |
29 | * Moodle settings are represented by objects that inherit from the admin_setting | |
30 | * class. These objects encapsulate how to read a setting, how to write a new value | |
31 | * to a setting, and how to appropriately display the HTML to modify the setting. | |
32 | * | |
33 | * ADMIN_SETTINGPAGE OBJECTS | |
34 | * | |
35 | * The admin_setting objects are then grouped into admin_settingpages. The latter | |
36 | * appear in the Moodle admin tree block. All interaction with admin_settingpage | |
37 | * objects is handled by the admin/settings.php file. | |
38 | * | |
39 | * ADMIN_EXTERNALPAGE OBJECTS | |
40 | * | |
41 | * There are some settings in Moodle that are too complex to (efficiently) handle | |
42 | * with admin_settingpages. (Consider, for example, user management and displaying | |
43 | * lists of users.) In this case, we use the admin_externalpage object. This object | |
44 | * places a link to an external PHP file in the admin tree block. | |
45 | * | |
46 | * If you're using an admin_externalpage object for some settings, you can take | |
47 | * advantage of the admin_externalpage_* functions. For example, suppose you wanted | |
48 | * to add a foo.php file into admin. First off, you add the following line to | |
49 | * admin/settings/first.php (at the end of the file) or to some other file in | |
50 | * admin/settings: | |
51 | * <code> | |
52 | * $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'), | |
53 | * $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission')); | |
54 | * </code> | |
55 | * | |
56 | * Next, in foo.php, your file structure would resemble the following: | |
57 | * <code> | |
1fcf0ca8 | 58 | * require(__DIR__.'/../../config.php'); |
db26acd4 | 59 | * require_once($CFG->libdir.'/adminlib.php'); |
60 | * admin_externalpage_setup('foo'); | |
61 | * // functionality like processing form submissions goes here | |
4d553cb2 | 62 | * echo $OUTPUT->header(); |
db26acd4 | 63 | * // your HTML goes here |
4d553cb2 | 64 | * echo $OUTPUT->footer(); |
db26acd4 | 65 | * </code> |
66 | * | |
67 | * The admin_externalpage_setup() function call ensures the user is logged in, | |
68 | * and makes sure that they have the proper role permission to access the page. | |
01a2ce80 | 69 | * It also configures all $PAGE properties needed for navigation. |
db26acd4 | 70 | * |
71 | * ADMIN_CATEGORY OBJECTS | |
72 | * | |
73 | * Above and beyond all this, we have admin_category objects. These objects | |
74 | * appear as folders in the admin tree block. They contain admin_settingpage's, | |
75 | * admin_externalpage's, and other admin_category's. | |
76 | * | |
77 | * OTHER NOTES | |
78 | * | |
79 | * admin_settingpage's, admin_externalpage's, and admin_category's all inherit | |
80 | * from part_of_admin_tree (a pseudointerface). This interface insists that | |
81 | * a class has a check_access method for access permissions, a locate method | |
82 | * used to find a specific node in the admin tree and find parent path. | |
83 | * | |
84 | * admin_category's inherit from parentable_part_of_admin_tree. This pseudo- | |
85 | * interface ensures that the class implements a recursive add function which | |
86 | * accepts a part_of_admin_tree object and searches for the proper place to | |
87 | * put it. parentable_part_of_admin_tree implies part_of_admin_tree. | |
88 | * | |
89 | * Please note that the $this->name field of any part_of_admin_tree must be | |
90 | * UNIQUE throughout the ENTIRE admin tree. | |
91 | * | |
92 | * The $this->name field of an admin_setting object (which is *not* part_of_ | |
93 | * admin_tree) must be unique on the respective admin_settingpage where it is | |
94 | * used. | |
95 | * | |
0c079f19 | 96 | * Original author: Vincenzo K. Marcovecchio |
97 | * Maintainer: Petr Skoda | |
db26acd4 | 98 | * |
78bfb562 PS |
99 | * @package core |
100 | * @subpackage admin | |
101 | * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com | |
102 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
88a7228a | 103 | */ |
104 | ||
78bfb562 PS |
105 | defined('MOODLE_INTERNAL') || die(); |
106 | ||
598b38f7 | 107 | /// Add libraries |
108 | require_once($CFG->libdir.'/ddllib.php'); | |
b1f93b15 | 109 | require_once($CFG->libdir.'/xmlize.php'); |
3bcbd80e | 110 | require_once($CFG->libdir.'/messagelib.php'); |
b1f93b15 | 111 | |
bba0beae | 112 | define('INSECURE_DATAROOT_WARNING', 1); |
113 | define('INSECURE_DATAROOT_ERROR', 2); | |
0c079f19 | 114 | |
ed996ede | 115 | /** |
116 | * Automatically clean-up all plugin data and remove the plugin DB tables | |
117 | * | |
2f87bb03 PS |
118 | * NOTE: do not call directly, use new /admin/plugins.php?uninstall=component instead! |
119 | * | |
ed996ede | 120 | * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc. |
121 | * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc. | |
122 | * @uses global $OUTPUT to produce notices and other messages | |
123 | * @return void | |
124 | */ | |
125 | function uninstall_plugin($type, $name) { | |
126 | global $CFG, $DB, $OUTPUT; | |
127 | ||
3db309dc | 128 | // This may take a long time. |
3ef7279f | 129 | core_php_time_limit::raise(); |
3db309dc | 130 | |
3601c5f0 PS |
131 | // Recursively uninstall all subplugins first. |
132 | $subplugintypes = core_component::get_plugin_types_with_subplugins(); | |
133 | if (isset($subplugintypes[$type])) { | |
134 | $base = core_component::get_plugin_directory($type, $name); | |
29c442c5 AN |
135 | |
136 | $subpluginsfile = "{$base}/db/subplugins.json"; | |
137 | if (file_exists($subpluginsfile)) { | |
138 | $subplugins = (array) json_decode(file_get_contents($subpluginsfile))->plugintypes; | |
139 | } else if (file_exists("{$base}/db/subplugins.php")) { | |
140 | debugging('Use of subplugins.php has been deprecated. ' . | |
141 | 'Please update your plugin to provide a subplugins.json file instead.', | |
142 | DEBUG_DEVELOPER); | |
143 | $subplugins = []; | |
144 | include("{$base}/db/subplugins.php"); | |
145 | } | |
146 | ||
147 | if (!empty($subplugins)) { | |
148 | foreach (array_keys($subplugins) as $subplugintype) { | |
3601c5f0 | 149 | $instances = core_component::get_plugin_list($subplugintype); |
846e4e17 PS |
150 | foreach ($instances as $subpluginname => $notusedpluginpath) { |
151 | uninstall_plugin($subplugintype, $subpluginname); | |
152 | } | |
ed996ede | 153 | } |
154 | } | |
155 | } | |
156 | ||
2138244c | 157 | $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum' |
158 | ||
159 | if ($type === 'mod') { | |
160 | $pluginname = $name; // eg. 'forum' | |
70d35fe6 PS |
161 | if (get_string_manager()->string_exists('modulename', $component)) { |
162 | $strpluginname = get_string('modulename', $component); | |
163 | } else { | |
164 | $strpluginname = $component; | |
165 | } | |
df997f84 | 166 | |
2138244c | 167 | } else { |
9baf6825 | 168 | $pluginname = $component; |
70d35fe6 PS |
169 | if (get_string_manager()->string_exists('pluginname', $component)) { |
170 | $strpluginname = get_string('pluginname', $component); | |
171 | } else { | |
172 | $strpluginname = $component; | |
173 | } | |
2138244c | 174 | } |
df997f84 | 175 | |
ed996ede | 176 | echo $OUTPUT->heading($pluginname); |
177 | ||
c026a28d MG |
178 | // Delete all tag areas, collections and instances associated with this plugin. |
179 | core_tag_area::uninstall($component); | |
d9994071 | 180 | |
effff2a4 | 181 | // Custom plugin uninstall. |
1c74b260 | 182 | $plugindirectory = core_component::get_plugin_directory($type, $name); |
ed996ede | 183 | $uninstalllib = $plugindirectory . '/db/uninstall.php'; |
184 | if (file_exists($uninstalllib)) { | |
185 | require_once($uninstalllib); | |
2138244c | 186 | $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()' |
ed996ede | 187 | if (function_exists($uninstallfunction)) { |
effff2a4 PS |
188 | // Do not verify result, let plugin complain if necessary. |
189 | $uninstallfunction(); | |
ed996ede | 190 | } |
191 | } | |
192 | ||
a31e0b40 | 193 | // Specific plugin type cleanup. |
e87214bd | 194 | $plugininfo = core_plugin_manager::instance()->get_plugin_info($component); |
a31e0b40 PS |
195 | if ($plugininfo) { |
196 | $plugininfo->uninstall_cleanup(); | |
e87214bd | 197 | core_plugin_manager::reset_caches(); |
a31e0b40 PS |
198 | } |
199 | $plugininfo = null; | |
200 | ||
70d35fe6 | 201 | // perform clean-up task common for all the plugin/subplugin types |
ed996ede | 202 | |
bc81eadb JM |
203 | //delete the web service functions and pre-built services |
204 | require_once($CFG->dirroot.'/lib/externallib.php'); | |
205 | external_delete_descriptions($component); | |
206 | ||
ed996ede | 207 | // delete calendar events |
2138244c | 208 | $DB->delete_records('event', array('modulename' => $pluginname)); |
1a972b06 | 209 | $DB->delete_records('event', ['component' => $component]); |
ed996ede | 210 | |
309ae892 | 211 | // Delete scheduled tasks. |
c05f3109 | 212 | $DB->delete_records('task_scheduled', array('component' => $component)); |
309ae892 | 213 | |
6c0bfb14 | 214 | // Delete Inbound Message datakeys. |
39048505 | 215 | $DB->delete_records_select('messageinbound_datakeys', |
c05f3109 | 216 | 'handler IN (SELECT id FROM {messageinbound_handlers} WHERE component = ?)', array($component)); |
6c0bfb14 AN |
217 | |
218 | // Delete Inbound Message handlers. | |
c05f3109 | 219 | $DB->delete_records('messageinbound_handlers', array('component' => $component)); |
6c0bfb14 | 220 | |
ed996ede | 221 | // delete all the logs |
2138244c | 222 | $DB->delete_records('log', array('module' => $pluginname)); |
ed996ede | 223 | |
224 | // delete log_display information | |
c6d75bff | 225 | $DB->delete_records('log_display', array('component' => $component)); |
ed996ede | 226 | |
227 | // delete the module configuration records | |
bde002b8 PS |
228 | unset_all_config_for_plugin($component); |
229 | if ($type === 'mod') { | |
230 | unset_all_config_for_plugin($pluginname); | |
231 | } | |
ed996ede | 232 | |
3bcbd80e | 233 | // delete message provider |
8e265315 RK |
234 | message_provider_uninstall($component); |
235 | ||
ed996ede | 236 | // delete the plugin tables |
237 | $xmldbfilepath = $plugindirectory . '/db/install.xml'; | |
54483279 PS |
238 | drop_plugin_tables($component, $xmldbfilepath, false); |
239 | if ($type === 'mod' or $type === 'block') { | |
240 | // non-frankenstyle table prefixes | |
241 | drop_plugin_tables($name, $xmldbfilepath, false); | |
242 | } | |
ed996ede | 243 | |
ed996ede | 244 | // delete the capabilities that were defined by this module |
245 | capabilities_cleanup($component); | |
246 | ||
546b8864 DM |
247 | // Delete all remaining files in the filepool owned by the component. |
248 | $fs = get_file_storage(); | |
249 | $fs->delete_component_files($component); | |
250 | ||
5718a123 DM |
251 | // Finally purge all caches. |
252 | purge_all_caches(); | |
253 | ||
82b1cf00 PS |
254 | // Invalidate the hash used for upgrade detections. |
255 | set_config('allversionshash', ''); | |
256 | ||
ed996ede | 257 | echo $OUTPUT->notification(get_string('success'), 'notifysuccess'); |
258 | } | |
259 | ||
e243c8c4 DM |
260 | /** |
261 | * Returns the version of installed component | |
262 | * | |
263 | * @param string $component component name | |
264 | * @param string $source either 'disk' or 'installed' - where to get the version information from | |
265 | * @return string|bool version number or false if the component is not found | |
266 | */ | |
267 | function get_component_version($component, $source='installed') { | |
268 | global $CFG, $DB; | |
269 | ||
56da374e | 270 | list($type, $name) = core_component::normalize_component($component); |
e243c8c4 DM |
271 | |
272 | // moodle core or a core subsystem | |
273 | if ($type === 'core') { | |
274 | if ($source === 'installed') { | |
275 | if (empty($CFG->version)) { | |
276 | return false; | |
277 | } else { | |
278 | return $CFG->version; | |
279 | } | |
280 | } else { | |
281 | if (!is_readable($CFG->dirroot.'/version.php')) { | |
282 | return false; | |
283 | } else { | |
9a8abf13 | 284 | $version = null; //initialize variable for IDEs |
e243c8c4 DM |
285 | include($CFG->dirroot.'/version.php'); |
286 | return $version; | |
287 | } | |
288 | } | |
289 | } | |
290 | ||
291 | // activity module | |
292 | if ($type === 'mod') { | |
293 | if ($source === 'installed') { | |
5f8012d4 PS |
294 | if ($CFG->version < 2013092001.02) { |
295 | return $DB->get_field('modules', 'version', array('name'=>$name)); | |
296 | } else { | |
297 | return get_config('mod_'.$name, 'version'); | |
298 | } | |
299 | ||
e243c8c4 | 300 | } else { |
bd3b3bba | 301 | $mods = core_component::get_plugin_list('mod'); |
d361c804 | 302 | if (empty($mods[$name]) or !is_readable($mods[$name].'/version.php')) { |
e243c8c4 DM |
303 | return false; |
304 | } else { | |
bde002b8 PS |
305 | $plugin = new stdClass(); |
306 | $plugin->version = null; | |
307 | $module = $plugin; | |
d361c804 | 308 | include($mods[$name].'/version.php'); |
bde002b8 | 309 | return $plugin->version; |
e243c8c4 DM |
310 | } |
311 | } | |
312 | } | |
313 | ||
314 | // block | |
315 | if ($type === 'block') { | |
316 | if ($source === 'installed') { | |
c66b839a PS |
317 | if ($CFG->version < 2013092001.02) { |
318 | return $DB->get_field('block', 'version', array('name'=>$name)); | |
319 | } else { | |
320 | return get_config('block_'.$name, 'version'); | |
321 | } | |
e243c8c4 | 322 | } else { |
bd3b3bba | 323 | $blocks = core_component::get_plugin_list('block'); |
e243c8c4 DM |
324 | if (empty($blocks[$name]) or !is_readable($blocks[$name].'/version.php')) { |
325 | return false; | |
326 | } else { | |
327 | $plugin = new stdclass(); | |
328 | include($blocks[$name].'/version.php'); | |
329 | return $plugin->version; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | // all other plugin types | |
335 | if ($source === 'installed') { | |
336 | return get_config($type.'_'.$name, 'version'); | |
337 | } else { | |
bd3b3bba | 338 | $plugins = core_component::get_plugin_list($type); |
e243c8c4 DM |
339 | if (empty($plugins[$name])) { |
340 | return false; | |
341 | } else { | |
342 | $plugin = new stdclass(); | |
343 | include($plugins[$name].'/version.php'); | |
344 | return $plugin->version; | |
345 | } | |
346 | } | |
347 | } | |
348 | ||
b1f93b15 | 349 | /** |
350 | * Delete all plugin tables | |
db26acd4 | 351 | * |
db26acd4 | 352 | * @param string $name Name of plugin, used as table prefix |
353 | * @param string $file Path to install.xml file | |
354 | * @param bool $feedback defaults to true | |
0c079f19 | 355 | * @return bool Always returns true |
b1f93b15 | 356 | */ |
357 | function drop_plugin_tables($name, $file, $feedback=true) { | |
358 | global $CFG, $DB; | |
359 | ||
360 | // first try normal delete | |
8b4ca8f7 | 361 | if (file_exists($file) and $DB->get_manager()->delete_tables_from_xmldb_file($file)) { |
b1f93b15 | 362 | return true; |
363 | } | |
364 | ||
365 | // then try to find all tables that start with name and are not in any xml file | |
366 | $used_tables = get_used_table_names(); | |
367 | ||
368 | $tables = $DB->get_tables(); | |
369 | ||
370 | /// Iterate over, fixing id fields as necessary | |
371 | foreach ($tables as $table) { | |
372 | if (in_array($table, $used_tables)) { | |
373 | continue; | |
374 | } | |
375 | ||
8b4ca8f7 | 376 | if (strpos($table, $name) !== 0) { |
377 | continue; | |
378 | } | |
379 | ||
b1f93b15 | 380 | // found orphan table --> delete it |
381 | if ($DB->get_manager()->table_exists($table)) { | |
382 | $xmldb_table = new xmldb_table($table); | |
eee5d9bb | 383 | $DB->get_manager()->drop_table($xmldb_table); |
b1f93b15 | 384 | } |
385 | } | |
386 | ||
387 | return true; | |
388 | } | |
389 | ||
390 | /** | |
875e5f07 | 391 | * Returns names of all known tables == tables that moodle knows about. |
db26acd4 | 392 | * |
393 | * @return array Array of lowercase table names | |
b1f93b15 | 394 | */ |
395 | function get_used_table_names() { | |
396 | $table_names = array(); | |
397 | $dbdirs = get_db_directories(); | |
398 | ||
399 | foreach ($dbdirs as $dbdir) { | |
400 | $file = $dbdir.'/install.xml'; | |
401 | ||
402 | $xmldb_file = new xmldb_file($file); | |
403 | ||
404 | if (!$xmldb_file->fileExists()) { | |
405 | continue; | |
406 | } | |
407 | ||
408 | $loaded = $xmldb_file->loadXMLStructure(); | |
73fa96d5 | 409 | $structure = $xmldb_file->getStructure(); |
b1f93b15 | 410 | |
411 | if ($loaded and $tables = $structure->getTables()) { | |
412 | foreach($tables as $table) { | |
afe1cf72 | 413 | $table_names[] = strtolower($table->getName()); |
b1f93b15 | 414 | } |
415 | } | |
416 | } | |
417 | ||
418 | return $table_names; | |
419 | } | |
420 | ||
421 | /** | |
422 | * Returns list of all directories where we expect install.xml files | |
db26acd4 | 423 | * @return array Array of paths |
b1f93b15 | 424 | */ |
425 | function get_db_directories() { | |
426 | global $CFG; | |
427 | ||
428 | $dbdirs = array(); | |
429 | ||
9baf6825 | 430 | /// First, the main one (lib/db) |
b1f93b15 | 431 | $dbdirs[] = $CFG->libdir.'/db'; |
432 | ||
46f6f7f2 PS |
433 | /// Then, all the ones defined by core_component::get_plugin_types() |
434 | $plugintypes = core_component::get_plugin_types(); | |
17da2e6f | 435 | foreach ($plugintypes as $plugintype => $pluginbasedir) { |
bd3b3bba | 436 | if ($plugins = core_component::get_plugin_list($plugintype)) { |
17da2e6f | 437 | foreach ($plugins as $plugin => $plugindir) { |
438 | $dbdirs[] = $plugindir.'/db'; | |
76b6c644 | 439 | } |
b91b274b | 440 | } |
7cdd8b22 | 441 | } |
b1f93b15 | 442 | |
b1f93b15 | 443 | return $dbdirs; |
444 | } | |
445 | ||
eef868d1 | 446 | /** |
61460dd6 | 447 | * Try to obtain or release the cron lock. |
61460dd6 | 448 | * @param string $name name of lock |
875e5f07 PS |
449 | * @param int $until timestamp when this lock considered stale, null means remove lock unconditionally |
450 | * @param bool $ignorecurrent ignore current lock state, usually extend previous lock, defaults to false | |
61460dd6 | 451 | * @return bool true if lock obtained |
f3221af9 | 452 | */ |
61460dd6 | 453 | function set_cron_lock($name, $until, $ignorecurrent=false) { |
f33e1ed4 | 454 | global $DB; |
f3221af9 | 455 | if (empty($name)) { |
61460dd6 | 456 | debugging("Tried to get a cron lock for a null fieldname"); |
f3221af9 | 457 | return false; |
458 | } | |
459 | ||
61460dd6 | 460 | // remove lock by force == remove from config table |
461 | if (is_null($until)) { | |
462 | set_config($name, null); | |
f3221af9 | 463 | return true; |
464 | } | |
465 | ||
61460dd6 | 466 | if (!$ignorecurrent) { |
3e7069e7 | 467 | // read value from db - other processes might have changed it |
f33e1ed4 | 468 | $value = $DB->get_field('config', 'value', array('name'=>$name)); |
61460dd6 | 469 | |
470 | if ($value and $value > time()) { | |
3e7069e7 | 471 | //lock active |
61460dd6 | 472 | return false; |
f3221af9 | 473 | } |
474 | } | |
61460dd6 | 475 | |
476 | set_config($name, $until); | |
f3221af9 | 477 | return true; |
478 | } | |
a597f8a8 | 479 | |
bba0beae | 480 | /** |
481 | * Test if and critical warnings are present | |
482 | * @return bool | |
483 | */ | |
484 | function admin_critical_warnings_present() { | |
485 | global $SESSION; | |
486 | ||
b0c6dc1c | 487 | if (!has_capability('moodle/site:config', context_system::instance())) { |
bba0beae | 488 | return 0; |
489 | } | |
490 | ||
491 | if (!isset($SESSION->admin_critical_warning)) { | |
492 | $SESSION->admin_critical_warning = 0; | |
fbf2c91e | 493 | if (is_dataroot_insecure(true) === INSECURE_DATAROOT_ERROR) { |
bba0beae | 494 | $SESSION->admin_critical_warning = 1; |
495 | } | |
496 | } | |
497 | ||
498 | return $SESSION->admin_critical_warning; | |
499 | } | |
500 | ||
61f9c4b4 | 501 | /** |
db26acd4 | 502 | * Detects if float supports at least 10 decimal digits |
503 | * | |
875e5f07 | 504 | * Detects if float supports at least 10 decimal digits |
61f9c4b4 | 505 | * and also if float-->string conversion works as expected. |
db26acd4 | 506 | * |
61f9c4b4 | 507 | * @return bool true if problem found |
508 | */ | |
509 | function is_float_problem() { | |
510 | $num1 = 2009010200.01; | |
511 | $num2 = 2009010200.02; | |
512 | ||
513 | return ((string)$num1 === (string)$num2 or $num1 === $num2 or $num2 <= (string)$num1); | |
514 | } | |
515 | ||
57e35f32 | 516 | /** |
517 | * Try to verify that dataroot is not accessible from web. | |
57e35f32 | 518 | * |
db26acd4 | 519 | * Try to verify that dataroot is not accessible from web. |
520 | * It is not 100% correct but might help to reduce number of vulnerable sites. | |
57e35f32 | 521 | * Protection from httpd.conf and .htaccess is not detected properly. |
0c079f19 | 522 | * |
db26acd4 | 523 | * @uses INSECURE_DATAROOT_WARNING |
524 | * @uses INSECURE_DATAROOT_ERROR | |
525 | * @param bool $fetchtest try to test public access by fetching file, default false | |
875e5f07 | 526 | * @return mixed empty means secure, INSECURE_DATAROOT_ERROR found a critical problem, INSECURE_DATAROOT_WARNING might be problematic |
57e35f32 | 527 | */ |
bba0beae | 528 | function is_dataroot_insecure($fetchtest=false) { |
57e35f32 | 529 | global $CFG; |
530 | ||
531 | $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround | |
532 | ||
533 | $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1); | |
534 | $rp = strrev(trim($rp, '/')); | |
535 | $rp = explode('/', $rp); | |
536 | foreach($rp as $r) { | |
537 | if (strpos($siteroot, '/'.$r.'/') === 0) { | |
538 | $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory | |
539 | } else { | |
540 | break; // probably alias root | |
541 | } | |
542 | } | |
543 | ||
544 | $siteroot = strrev($siteroot); | |
545 | $dataroot = str_replace('\\', '/', $CFG->dataroot.'/'); | |
546 | ||
bba0beae | 547 | if (strpos($dataroot, $siteroot) !== 0) { |
548 | return false; | |
549 | } | |
550 | ||
551 | if (!$fetchtest) { | |
552 | return INSECURE_DATAROOT_WARNING; | |
553 | } | |
554 | ||
555 | // now try all methods to fetch a test file using http protocol | |
556 | ||
557 | $httpdocroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); | |
558 | preg_match('|(https?://[^/]+)|i', $CFG->wwwroot, $matches); | |
559 | $httpdocroot = $matches[1]; | |
560 | $datarooturl = $httpdocroot.'/'. substr($dataroot, strlen($siteroot)); | |
71904f4d | 561 | make_upload_directory('diag'); |
bba0beae | 562 | $testfile = $CFG->dataroot.'/diag/public.txt'; |
563 | if (!file_exists($testfile)) { | |
564 | file_put_contents($testfile, 'test file, do not delete'); | |
eb459f71 | 565 | @chmod($testfile, $CFG->filepermissions); |
bba0beae | 566 | } |
567 | $teststr = trim(file_get_contents($testfile)); | |
568 | if (empty($teststr)) { | |
9baf6825 | 569 | // hmm, strange |
bba0beae | 570 | return INSECURE_DATAROOT_WARNING; |
571 | } | |
572 | ||
573 | $testurl = $datarooturl.'/diag/public.txt'; | |
041a4b0f | 574 | if (extension_loaded('curl') and |
575 | !(stripos(ini_get('disable_functions'), 'curl_init') !== FALSE) and | |
576 | !(stripos(ini_get('disable_functions'), 'curl_setop') !== FALSE) and | |
577 | ($ch = @curl_init($testurl)) !== false) { | |
bba0beae | 578 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
579 | curl_setopt($ch, CURLOPT_HEADER, false); | |
580 | $data = curl_exec($ch); | |
581 | if (!curl_errno($ch)) { | |
582 | $data = trim($data); | |
583 | if ($data === $teststr) { | |
584 | curl_close($ch); | |
585 | return INSECURE_DATAROOT_ERROR; | |
586 | } | |
587 | } | |
588 | curl_close($ch); | |
589 | } | |
590 | ||
591 | if ($data = @file_get_contents($testurl)) { | |
592 | $data = trim($data); | |
593 | if ($data === $teststr) { | |
594 | return INSECURE_DATAROOT_ERROR; | |
595 | } | |
596 | } | |
597 | ||
598 | preg_match('|https?://([^/]+)|i', $testurl, $matches); | |
599 | $sitename = $matches[1]; | |
600 | $error = 0; | |
601 | if ($fp = @fsockopen($sitename, 80, $error)) { | |
602 | preg_match('|https?://[^/]+(.*)|i', $testurl, $matches); | |
603 | $localurl = $matches[1]; | |
604 | $out = "GET $localurl HTTP/1.1\r\n"; | |
605 | $out .= "Host: $sitename\r\n"; | |
606 | $out .= "Connection: Close\r\n\r\n"; | |
607 | fwrite($fp, $out); | |
608 | $data = ''; | |
609 | $incoming = false; | |
610 | while (!feof($fp)) { | |
611 | if ($incoming) { | |
612 | $data .= fgets($fp, 1024); | |
613 | } else if (@fgets($fp, 1024) === "\r\n") { | |
9baf6825 | 614 | $incoming = true; |
615 | } | |
bba0beae | 616 | } |
617 | fclose($fp); | |
618 | $data = trim($data); | |
619 | if ($data === $teststr) { | |
620 | return INSECURE_DATAROOT_ERROR; | |
621 | } | |
57e35f32 | 622 | } |
bba0beae | 623 | |
624 | return INSECURE_DATAROOT_WARNING; | |
57e35f32 | 625 | } |
6e4dc10f | 626 | |
48e114a5 PS |
627 | /** |
628 | * Enables CLI maintenance mode by creating new dataroot/climaintenance.html file. | |
629 | */ | |
630 | function enable_cli_maintenance_mode() { | |
43999a3f | 631 | global $CFG, $SITE; |
48e114a5 PS |
632 | |
633 | if (file_exists("$CFG->dataroot/climaintenance.html")) { | |
634 | unlink("$CFG->dataroot/climaintenance.html"); | |
635 | } | |
636 | ||
637 | if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { | |
638 | $data = $CFG->maintenance_message; | |
639 | $data = bootstrap_renderer::early_error_content($data, null, null, null); | |
640 | $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data); | |
641 | ||
642 | } else if (file_exists("$CFG->dataroot/climaintenance.template.html")) { | |
643 | $data = file_get_contents("$CFG->dataroot/climaintenance.template.html"); | |
644 | ||
645 | } else { | |
646 | $data = get_string('sitemaintenance', 'admin'); | |
647 | $data = bootstrap_renderer::early_error_content($data, null, null, null); | |
43999a3f | 648 | $data = bootstrap_renderer::plain_page(get_string('sitemaintenancetitle', 'admin', $SITE->fullname), $data); |
48e114a5 PS |
649 | } |
650 | ||
651 | file_put_contents("$CFG->dataroot/climaintenance.html", $data); | |
652 | chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions); | |
653 | } | |
654 | ||
6e4dc10f | 655 | /// CLASS DEFINITIONS ///////////////////////////////////////////////////////// |
656 | ||
3e7069e7 | 657 | |
6e4dc10f | 658 | /** |
875e5f07 | 659 | * Interface for anything appearing in the admin tree |
6e4dc10f | 660 | * |
875e5f07 | 661 | * The interface that is implemented by anything that appears in the admin tree |
6e4dc10f | 662 | * block. It forces inheriting classes to define a method for checking user permissions |
663 | * and methods for finding something in the admin tree. | |
664 | * | |
db26acd4 | 665 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 666 | */ |
73fa96d5 | 667 | interface part_of_admin_tree { |
6e4dc10f | 668 | |
9baf6825 | 669 | /** |
670 | * Finds a named part_of_admin_tree. | |
671 | * | |
672 | * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree | |
673 | * and not parentable_part_of_admin_tree, then this function should only check if | |
674 | * $this->name matches $name. If it does, it should return a reference to $this, | |
675 | * otherwise, it should return a reference to NULL. | |
676 | * | |
677 | * If a class inherits parentable_part_of_admin_tree, this method should be called | |
678 | * recursively on all child objects (assuming, of course, the parent object's name | |
679 | * doesn't match the search criterion). | |
680 | * | |
681 | * @param string $name The internal name of the part_of_admin_tree we're searching for. | |
682 | * @return mixed An object reference or a NULL reference. | |
683 | */ | |
73fa96d5 | 684 | public function locate($name); |
4672d955 | 685 | |
686 | /** | |
687 | * Removes named part_of_admin_tree. | |
688 | * | |
689 | * @param string $name The internal name of the part_of_admin_tree we want to remove. | |
a8a66c96 | 690 | * @return bool success. |
4672d955 | 691 | */ |
73fa96d5 | 692 | public function prune($name); |
4672d955 | 693 | |
220a90c5 | 694 | /** |
695 | * Search using query | |
db26acd4 | 696 | * @param string $query |
220a90c5 | 697 | * @return mixed array-object structure of found settings and pages |
698 | */ | |
73fa96d5 | 699 | public function search($query); |
220a90c5 | 700 | |
6e4dc10f | 701 | /** |
702 | * Verifies current user's access to this part_of_admin_tree. | |
703 | * | |
704 | * Used to check if the current user has access to this part of the admin tree or | |
705 | * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree, | |
706 | * then this method is usually just a call to has_capability() in the site context. | |
707 | * | |
708 | * If a class inherits parentable_part_of_admin_tree, this method should return the | |
709 | * logical OR of the return of check_access() on all child objects. | |
710 | * | |
711 | * @return bool True if the user has access, false if she doesn't. | |
712 | */ | |
73fa96d5 | 713 | public function check_access(); |
eef868d1 | 714 | |
a8a66c96 | 715 | /** |
875e5f07 | 716 | * Mostly useful for removing of some parts of the tree in admin tree block. |
a8a66c96 | 717 | * |
718 | * @return True is hidden from normal list view | |
719 | */ | |
73fa96d5 | 720 | public function is_hidden(); |
427649bf PS |
721 | |
722 | /** | |
723 | * Show we display Save button at the page bottom? | |
724 | * @return bool | |
725 | */ | |
726 | public function show_save(); | |
6e4dc10f | 727 | } |
728 | ||
3e7069e7 | 729 | |
6e4dc10f | 730 | /** |
875e5f07 | 731 | * Interface implemented by any part_of_admin_tree that has children. |
6e4dc10f | 732 | * |
875e5f07 | 733 | * The interface implemented by any part_of_admin_tree that can be a parent |
6e4dc10f | 734 | * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart |
eef868d1 | 735 | * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods |
6e4dc10f | 736 | * include an add method for adding other part_of_admin_tree objects as children. |
737 | * | |
db26acd4 | 738 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 739 | */ |
73fa96d5 | 740 | interface parentable_part_of_admin_tree extends part_of_admin_tree { |
eef868d1 | 741 | |
9baf6825 | 742 | /** |
743 | * Adds a part_of_admin_tree object to the admin tree. | |
744 | * | |
745 | * Used to add a part_of_admin_tree object to this object or a child of this | |
746 | * object. $something should only be added if $destinationname matches | |
747 | * $this->name. If it doesn't, add should be called on child objects that are | |
748 | * also parentable_part_of_admin_tree's. | |
749 | * | |
f6e85ab0 DM |
750 | * $something should be appended as the last child in the $destinationname. If the |
751 | * $beforesibling is specified, $something should be prepended to it. If the given | |
752 | * sibling is not found, $something should be appended to the end of $destinationname | |
753 | * and a developer debugging message should be displayed. | |
754 | * | |
9baf6825 | 755 | * @param string $destinationname The internal name of the new parent for $something. |
756 | * @param part_of_admin_tree $something The object to be added. | |
757 | * @return bool True on success, false on failure. | |
758 | */ | |
f6e85ab0 | 759 | public function add($destinationname, $something, $beforesibling = null); |
eef868d1 | 760 | |
6e4dc10f | 761 | } |
762 | ||
3e7069e7 | 763 | |
6e4dc10f | 764 | /** |
765 | * The object used to represent folders (a.k.a. categories) in the admin tree block. | |
eef868d1 | 766 | * |
6e4dc10f | 767 | * Each admin_category object contains a number of part_of_admin_tree objects. |
768 | * | |
db26acd4 | 769 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 770 | */ |
73fa96d5 | 771 | class admin_category implements parentable_part_of_admin_tree { |
6e4dc10f | 772 | |
cea3b7eb SH |
773 | /** @var part_of_admin_tree[] An array of part_of_admin_tree objects that are this object's children */ |
774 | protected $children; | |
0c079f19 | 775 | /** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 776 | public $name; |
0c079f19 | 777 | /** @var string The displayed name for this category. Usually obtained through get_string() */ |
73fa96d5 | 778 | public $visiblename; |
0c079f19 | 779 | /** @var bool Should this category be hidden in admin tree block? */ |
73fa96d5 | 780 | public $hidden; |
0c079f19 | 781 | /** @var mixed Either a string or an array or strings */ |
73fa96d5 | 782 | public $path; |
0c079f19 | 783 | /** @var mixed Either a string or an array or strings */ |
73fa96d5 | 784 | public $visiblepath; |
6e4dc10f | 785 | |
cde44edc PS |
786 | /** @var array fast lookup category cache, all categories of one tree point to one cache */ |
787 | protected $category_cache; | |
788 | ||
cea3b7eb SH |
789 | /** @var bool If set to true children will be sorted when calling {@link admin_category::get_children()} */ |
790 | protected $sort = false; | |
791 | /** @var bool If set to true children will be sorted in ascending order. */ | |
792 | protected $sortasc = true; | |
793 | /** @var bool If set to true sub categories and pages will be split and then sorted.. */ | |
794 | protected $sortsplit = true; | |
795 | /** @var bool $sorted True if the children have been sorted and don't need resorting */ | |
796 | protected $sorted = false; | |
797 | ||
6e4dc10f | 798 | /** |
799 | * Constructor for an empty admin category | |
800 | * | |
801 | * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects | |
802 | * @param string $visiblename The displayed named for this category. Usually obtained through get_string() | |
db26acd4 | 803 | * @param bool $hidden hide category in admin tree block, defaults to false |
6e4dc10f | 804 | */ |
73fa96d5 | 805 | public function __construct($name, $visiblename, $hidden=false) { |
220a90c5 | 806 | $this->children = array(); |
807 | $this->name = $name; | |
6e4dc10f | 808 | $this->visiblename = $visiblename; |
220a90c5 | 809 | $this->hidden = $hidden; |
6e4dc10f | 810 | } |
eef868d1 | 811 | |
6e4dc10f | 812 | /** |
220a90c5 | 813 | * Returns a reference to the part_of_admin_tree object with internal name $name. |
6e4dc10f | 814 | * |
220a90c5 | 815 | * @param string $name The internal name of the object we want. |
816 | * @param bool $findpath initialize path and visiblepath arrays | |
0c079f19 | 817 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
db26acd4 | 818 | * defaults to false |
6e4dc10f | 819 | */ |
73fa96d5 | 820 | public function locate($name, $findpath=false) { |
42b53d46 | 821 | if (!isset($this->category_cache[$this->name])) { |
cde44edc PS |
822 | // somebody much have purged the cache |
823 | $this->category_cache[$this->name] = $this; | |
824 | } | |
825 | ||
6e4dc10f | 826 | if ($this->name == $name) { |
220a90c5 | 827 | if ($findpath) { |
828 | $this->visiblepath[] = $this->visiblename; | |
829 | $this->path[] = $this->name; | |
830 | } | |
831 | return $this; | |
6e4dc10f | 832 | } |
eef868d1 | 833 | |
cde44edc | 834 | // quick category lookup |
42b53d46 | 835 | if (!$findpath and isset($this->category_cache[$name])) { |
cde44edc PS |
836 | return $this->category_cache[$name]; |
837 | } | |
838 | ||
220a90c5 | 839 | $return = NULL; |
840 | foreach($this->children as $childid=>$unused) { | |
73fa96d5 | 841 | if ($return = $this->children[$childid]->locate($name, $findpath)) { |
220a90c5 | 842 | break; |
6e4dc10f | 843 | } |
844 | } | |
eef868d1 | 845 | |
220a90c5 | 846 | if (!is_null($return) and $findpath) { |
847 | $return->visiblepath[] = $this->visiblename; | |
848 | $return->path[] = $this->name; | |
849 | } | |
eef868d1 | 850 | |
220a90c5 | 851 | return $return; |
6e4dc10f | 852 | } |
853 | ||
854 | /** | |
220a90c5 | 855 | * Search using query |
db26acd4 | 856 | * |
857 | * @param string query | |
220a90c5 | 858 | * @return mixed array-object structure of found settings and pages |
6e4dc10f | 859 | */ |
73fa96d5 | 860 | public function search($query) { |
220a90c5 | 861 | $result = array(); |
cea3b7eb | 862 | foreach ($this->get_children() as $child) { |
3cea9c55 | 863 | $subsearch = $child->search($query); |
864 | if (!is_array($subsearch)) { | |
865 | debugging('Incorrect search result from '.$child->name); | |
866 | continue; | |
867 | } | |
868 | $result = array_merge($result, $subsearch); | |
6e4dc10f | 869 | } |
220a90c5 | 870 | return $result; |
6e4dc10f | 871 | } |
872 | ||
4672d955 | 873 | /** |
874 | * Removes part_of_admin_tree object with internal name $name. | |
875 | * | |
876 | * @param string $name The internal name of the object we want to remove. | |
a8a66c96 | 877 | * @return bool success |
4672d955 | 878 | */ |
73fa96d5 | 879 | public function prune($name) { |
4672d955 | 880 | |
881 | if ($this->name == $name) { | |
882 | return false; //can not remove itself | |
883 | } | |
884 | ||
885 | foreach($this->children as $precedence => $child) { | |
886 | if ($child->name == $name) { | |
cde44edc | 887 | // clear cache and delete self |
42b53d46 AN |
888 | while($this->category_cache) { |
889 | // delete the cache, but keep the original array address | |
890 | array_pop($this->category_cache); | |
cde44edc | 891 | } |
eef868d1 | 892 | unset($this->children[$precedence]); |
4672d955 | 893 | return true; |
cde44edc | 894 | } else if ($this->children[$precedence]->prune($name)) { |
4672d955 | 895 | return true; |
896 | } | |
897 | } | |
898 | return false; | |
899 | } | |
900 | ||
6e4dc10f | 901 | /** |
902 | * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object. | |
903 | * | |
f6e85ab0 DM |
904 | * By default the new part of the tree is appended as the last child of the parent. You |
905 | * can specify a sibling node that the new part should be prepended to. If the given | |
906 | * sibling is not found, the part is appended to the end (as it would be by default) and | |
907 | * a developer debugging message is displayed. | |
908 | * | |
909 | * @throws coding_exception if the $beforesibling is empty string or is not string at all. | |
220a90c5 | 910 | * @param string $destinationame The internal name of the immediate parent that we want for $something. |
875e5f07 | 911 | * @param mixed $something A part_of_admin_tree or setting instance to be added. |
f6e85ab0 | 912 | * @param string $beforesibling The name of the parent's child the $something should be prepended to. |
220a90c5 | 913 | * @return bool True if successfully added, false if $something can not be added. |
6e4dc10f | 914 | */ |
f6e85ab0 | 915 | public function add($parentname, $something, $beforesibling = null) { |
96f81ea3 PS |
916 | global $CFG; |
917 | ||
73fa96d5 | 918 | $parent = $this->locate($parentname); |
220a90c5 | 919 | if (is_null($parent)) { |
920 | debugging('parent does not exist!'); | |
6e4dc10f | 921 | return false; |
922 | } | |
923 | ||
73fa96d5 | 924 | if ($something instanceof part_of_admin_tree) { |
925 | if (!($parent instanceof parentable_part_of_admin_tree)) { | |
220a90c5 | 926 | debugging('error - parts of tree can be inserted only into parentable parts'); |
927 | return false; | |
6e4dc10f | 928 | } |
96f81ea3 | 929 | if ($CFG->debugdeveloper && !is_null($this->locate($something->name))) { |
97a8eddd FM |
930 | // The name of the node is already used, simply warn the developer that this should not happen. |
931 | // It is intentional to check for the debug level before performing the check. | |
932 | debugging('Duplicate admin page name: ' . $something->name, DEBUG_DEVELOPER); | |
933 | } | |
f6e85ab0 DM |
934 | if (is_null($beforesibling)) { |
935 | // Append $something as the parent's last child. | |
936 | $parent->children[] = $something; | |
937 | } else { | |
938 | if (!is_string($beforesibling) or trim($beforesibling) === '') { | |
939 | throw new coding_exception('Unexpected value of the beforesibling parameter'); | |
940 | } | |
941 | // Try to find the position of the sibling. | |
942 | $siblingposition = null; | |
943 | foreach ($parent->children as $childposition => $child) { | |
944 | if ($child->name === $beforesibling) { | |
945 | $siblingposition = $childposition; | |
946 | break; | |
947 | } | |
948 | } | |
949 | if (is_null($siblingposition)) { | |
950 | debugging('Sibling '.$beforesibling.' not found', DEBUG_DEVELOPER); | |
951 | $parent->children[] = $something; | |
952 | } else { | |
953 | $parent->children = array_merge( | |
954 | array_slice($parent->children, 0, $siblingposition), | |
955 | array($something), | |
956 | array_slice($parent->children, $siblingposition) | |
957 | ); | |
958 | } | |
959 | } | |
42b53d46 | 960 | if ($something instanceof admin_category) { |
cde44edc | 961 | if (isset($this->category_cache[$something->name])) { |
3e7069e7 | 962 | debugging('Duplicate admin category name: '.$something->name); |
cde44edc PS |
963 | } else { |
964 | $this->category_cache[$something->name] = $something; | |
965 | $something->category_cache =& $this->category_cache; | |
966 | foreach ($something->children as $child) { | |
967 | // just in case somebody already added subcategories | |
968 | if ($child instanceof admin_category) { | |
969 | if (isset($this->category_cache[$child->name])) { | |
3e7069e7 | 970 | debugging('Duplicate admin category name: '.$child->name); |
cde44edc PS |
971 | } else { |
972 | $this->category_cache[$child->name] = $child; | |
973 | $child->category_cache =& $this->category_cache; | |
974 | } | |
975 | } | |
976 | } | |
977 | } | |
978 | } | |
6e4dc10f | 979 | return true; |
eef868d1 | 980 | |
220a90c5 | 981 | } else { |
982 | debugging('error - can not add this element'); | |
983 | return false; | |
6e4dc10f | 984 | } |
eef868d1 | 985 | |
6e4dc10f | 986 | } |
eef868d1 | 987 | |
6e4dc10f | 988 | /** |
989 | * Checks if the user has access to anything in this category. | |
990 | * | |
875e5f07 | 991 | * @return bool True if the user has access to at least one child in this category, false otherwise. |
6e4dc10f | 992 | */ |
73fa96d5 | 993 | public function check_access() { |
6e4dc10f | 994 | foreach ($this->children as $child) { |
220a90c5 | 995 | if ($child->check_access()) { |
996 | return true; | |
997 | } | |
6e4dc10f | 998 | } |
220a90c5 | 999 | return false; |
6e4dc10f | 1000 | } |
eef868d1 | 1001 | |
a8a66c96 | 1002 | /** |
1003 | * Is this category hidden in admin tree block? | |
1004 | * | |
1005 | * @return bool True if hidden | |
1006 | */ | |
73fa96d5 | 1007 | public function is_hidden() { |
a8a66c96 | 1008 | return $this->hidden; |
1009 | } | |
427649bf PS |
1010 | |
1011 | /** | |
1012 | * Show we display Save button at the page bottom? | |
1013 | * @return bool | |
1014 | */ | |
1015 | public function show_save() { | |
1016 | foreach ($this->children as $child) { | |
1017 | if ($child->show_save()) { | |
1018 | return true; | |
1019 | } | |
1020 | } | |
1021 | return false; | |
1022 | } | |
cea3b7eb SH |
1023 | |
1024 | /** | |
1025 | * Sets sorting on this category. | |
1026 | * | |
1027 | * Please note this function doesn't actually do the sorting. | |
1028 | * It can be called anytime. | |
1029 | * Sorting occurs when the user calls get_children. | |
1030 | * Code using the children array directly won't see the sorted results. | |
1031 | * | |
1032 | * @param bool $sort If set to true children will be sorted, if false they won't be. | |
1033 | * @param bool $asc If true sorting will be ascending, otherwise descending. | |
1034 | * @param bool $split If true we sort pages and sub categories separately. | |
1035 | */ | |
1036 | public function set_sorting($sort, $asc = true, $split = true) { | |
1037 | $this->sort = (bool)$sort; | |
1038 | $this->sortasc = (bool)$asc; | |
1039 | $this->sortsplit = (bool)$split; | |
1040 | } | |
1041 | ||
1042 | /** | |
1043 | * Returns the children associated with this category. | |
1044 | * | |
1045 | * @return part_of_admin_tree[] | |
1046 | */ | |
1047 | public function get_children() { | |
1048 | // If we should sort and it hasn't already been sorted. | |
1049 | if ($this->sort && !$this->sorted) { | |
1050 | if ($this->sortsplit) { | |
1051 | $categories = array(); | |
1052 | $pages = array(); | |
1053 | foreach ($this->children as $child) { | |
1054 | if ($child instanceof admin_category) { | |
1055 | $categories[] = $child; | |
1056 | } else { | |
1057 | $pages[] = $child; | |
1058 | } | |
1059 | } | |
1060 | core_collator::asort_objects_by_property($categories, 'visiblename'); | |
1061 | core_collator::asort_objects_by_property($pages, 'visiblename'); | |
1062 | if (!$this->sortasc) { | |
1063 | $categories = array_reverse($categories); | |
1064 | $pages = array_reverse($pages); | |
1065 | } | |
1066 | $this->children = array_merge($pages, $categories); | |
1067 | } else { | |
1068 | core_collator::asort_objects_by_property($this->children, 'visiblename'); | |
1069 | if (!$this->sortasc) { | |
1070 | $this->children = array_reverse($this->children); | |
1071 | } | |
1072 | } | |
1073 | $this->sorted = true; | |
1074 | } | |
1075 | return $this->children; | |
1076 | } | |
1077 | ||
1078 | /** | |
1079 | * Magically gets a property from this object. | |
1080 | * | |
1081 | * @param $property | |
1082 | * @return part_of_admin_tree[] | |
1083 | * @throws coding_exception | |
1084 | */ | |
1085 | public function __get($property) { | |
1086 | if ($property === 'children') { | |
1087 | return $this->get_children(); | |
1088 | } | |
1089 | throw new coding_exception('Invalid property requested.'); | |
1090 | } | |
1091 | ||
1092 | /** | |
1093 | * Magically sets a property against this object. | |
1094 | * | |
1095 | * @param string $property | |
1096 | * @param mixed $value | |
1097 | * @throws coding_exception | |
1098 | */ | |
1099 | public function __set($property, $value) { | |
1100 | if ($property === 'children') { | |
1101 | $this->sorted = false; | |
1102 | $this->children = $value; | |
1103 | } else { | |
1104 | throw new coding_exception('Invalid property requested.'); | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * Checks if an inaccessible property is set. | |
1110 | * | |
1111 | * @param string $property | |
1112 | * @return bool | |
1113 | * @throws coding_exception | |
1114 | */ | |
1115 | public function __isset($property) { | |
1116 | if ($property === 'children') { | |
1117 | return isset($this->children); | |
1118 | } | |
1119 | throw new coding_exception('Invalid property requested.'); | |
1120 | } | |
6e4dc10f | 1121 | } |
1122 | ||
3e7069e7 | 1123 | |
db26acd4 | 1124 | /** |
0c079f19 | 1125 | * Root of admin settings tree, does not have any parent. |
db26acd4 | 1126 | * |
1127 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
db26acd4 | 1128 | */ |
220a90c5 | 1129 | class admin_root extends admin_category { |
9baf6825 | 1130 | /** @var array List of errors */ |
73fa96d5 | 1131 | public $errors; |
0c079f19 | 1132 | /** @var string search query */ |
73fa96d5 | 1133 | public $search; |
875e5f07 | 1134 | /** @var bool full tree flag - true means all settings required, false only pages required */ |
73fa96d5 | 1135 | public $fulltree; |
0c079f19 | 1136 | /** @var bool flag indicating loaded tree */ |
73fa96d5 | 1137 | public $loaded; |
875e5f07 | 1138 | /** @var mixed site custom defaults overriding defaults in settings files*/ |
cd3acbf2 | 1139 | public $custom_defaults; |
1140 | ||
db26acd4 | 1141 | /** |
0c079f19 | 1142 | * @param bool $fulltree true means all settings required, |
db26acd4 | 1143 | * false only pages required |
1144 | */ | |
73fa96d5 | 1145 | public function __construct($fulltree) { |
cd3acbf2 | 1146 | global $CFG; |
1147 | ||
73fa96d5 | 1148 | parent::__construct('root', get_string('administration'), false); |
220a90c5 | 1149 | $this->errors = array(); |
1150 | $this->search = ''; | |
73fa96d5 | 1151 | $this->fulltree = $fulltree; |
1152 | $this->loaded = false; | |
cd3acbf2 | 1153 | |
cde44edc PS |
1154 | $this->category_cache = array(); |
1155 | ||
cd3acbf2 | 1156 | // load custom defaults if found |
1157 | $this->custom_defaults = null; | |
1158 | $defaultsfile = "$CFG->dirroot/local/defaults.php"; | |
1159 | if (is_readable($defaultsfile)) { | |
1160 | $defaults = array(); | |
1161 | include($defaultsfile); | |
1162 | if (is_array($defaults) and count($defaults)) { | |
1163 | $this->custom_defaults = $defaults; | |
1164 | } | |
1165 | } | |
73fa96d5 | 1166 | } |
1167 | ||
db26acd4 | 1168 | /** |
1169 | * Empties children array, and sets loaded to false | |
1170 | * | |
1171 | * @param bool $requirefulltree | |
1172 | */ | |
73fa96d5 | 1173 | public function purge_children($requirefulltree) { |
1174 | $this->children = array(); | |
1175 | $this->fulltree = ($requirefulltree || $this->fulltree); | |
1176 | $this->loaded = false; | |
cde44edc PS |
1177 | //break circular dependencies - this helps PHP 5.2 |
1178 | while($this->category_cache) { | |
1179 | array_pop($this->category_cache); | |
1180 | } | |
1181 | $this->category_cache = array(); | |
220a90c5 | 1182 | } |
1183 | } | |
1184 | ||
3e7069e7 | 1185 | |
6e4dc10f | 1186 | /** |
1187 | * Links external PHP pages into the admin tree. | |
1188 | * | |
1189 | * See detailed usage example at the top of this document (adminlib.php) | |
1190 | * | |
db26acd4 | 1191 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 1192 | */ |
73fa96d5 | 1193 | class admin_externalpage implements part_of_admin_tree { |
6e4dc10f | 1194 | |
3e7069e7 | 1195 | /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 1196 | public $name; |
eef868d1 | 1197 | |
0c079f19 | 1198 | /** @var string The displayed name for this external page. Usually obtained through get_string(). */ |
73fa96d5 | 1199 | public $visiblename; |
eef868d1 | 1200 | |
0c079f19 | 1201 | /** @var string The external URL that we should link to when someone requests this external page. */ |
73fa96d5 | 1202 | public $url; |
eef868d1 | 1203 | |
e3c28dff | 1204 | /** @var array The role capability/permission a user must have to access this external page. */ |
73fa96d5 | 1205 | public $req_capability; |
eef868d1 | 1206 | |
0c079f19 | 1207 | /** @var object The context in which capability/permission should be checked, default is site context. */ |
73fa96d5 | 1208 | public $context; |
84c8ede0 | 1209 | |
0c079f19 | 1210 | /** @var bool hidden in admin tree block. */ |
73fa96d5 | 1211 | public $hidden; |
a8a66c96 | 1212 | |
0c079f19 | 1213 | /** @var mixed either string or array of string */ |
73fa96d5 | 1214 | public $path; |
3e7069e7 PS |
1215 | |
1216 | /** @var array list of visible names of page parents */ | |
73fa96d5 | 1217 | public $visiblepath; |
220a90c5 | 1218 | |
6e4dc10f | 1219 | /** |
1220 | * Constructor for adding an external page into the admin tree. | |
1221 | * | |
1222 | * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects. | |
1223 | * @param string $visiblename The displayed name for this external page. Usually obtained through get_string(). | |
1224 | * @param string $url The external URL that we should link to when someone requests this external page. | |
38d2d43b | 1225 | * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'. |
92f00846 | 1226 | * @param boolean $hidden Is this external page hidden in admin tree block? Default false. |
44d8a940 | 1227 | * @param stdClass $context The context the page relates to. Not sure what happens |
92f00846 | 1228 | * if you specify something other than system or front page. Defaults to system. |
6e4dc10f | 1229 | */ |
73fa96d5 | 1230 | public function __construct($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
220a90c5 | 1231 | $this->name = $name; |
6e4dc10f | 1232 | $this->visiblename = $visiblename; |
220a90c5 | 1233 | $this->url = $url; |
38d2d43b | 1234 | if (is_array($req_capability)) { |
1235 | $this->req_capability = $req_capability; | |
1236 | } else { | |
1237 | $this->req_capability = array($req_capability); | |
1238 | } | |
92f00846 | 1239 | $this->hidden = $hidden; |
84c8ede0 | 1240 | $this->context = $context; |
6e4dc10f | 1241 | } |
eef868d1 | 1242 | |
6e4dc10f | 1243 | /** |
1244 | * Returns a reference to the part_of_admin_tree object with internal name $name. | |
1245 | * | |
1246 | * @param string $name The internal name of the object we want. | |
db26acd4 | 1247 | * @param bool $findpath defaults to false |
6e4dc10f | 1248 | * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL. |
1249 | */ | |
73fa96d5 | 1250 | public function locate($name, $findpath=false) { |
220a90c5 | 1251 | if ($this->name == $name) { |
1252 | if ($findpath) { | |
1253 | $this->visiblepath = array($this->visiblename); | |
1254 | $this->path = array($this->name); | |
1255 | } | |
1256 | return $this; | |
1257 | } else { | |
1258 | $return = NULL; | |
1259 | return $return; | |
1260 | } | |
6e4dc10f | 1261 | } |
4672d955 | 1262 | |
db26acd4 | 1263 | /** |
1264 | * This function always returns false, required function by interface | |
1265 | * | |
1266 | * @param string $name | |
1267 | * @return false | |
1268 | */ | |
73fa96d5 | 1269 | public function prune($name) { |
4672d955 | 1270 | return false; |
1271 | } | |
1272 | ||
220a90c5 | 1273 | /** |
1274 | * Search using query | |
db26acd4 | 1275 | * |
1276 | * @param string $query | |
220a90c5 | 1277 | * @return mixed array-object structure of found settings and pages |
1278 | */ | |
73fa96d5 | 1279 | public function search($query) { |
220a90c5 | 1280 | $found = false; |
1281 | if (strpos(strtolower($this->name), $query) !== false) { | |
1282 | $found = true; | |
2f1e464a | 1283 | } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) { |
9baf6825 | 1284 | $found = true; |
1285 | } | |
220a90c5 | 1286 | if ($found) { |
365a5941 | 1287 | $result = new stdClass(); |
220a90c5 | 1288 | $result->page = $this; |
1289 | $result->settings = array(); | |
1290 | return array($this->name => $result); | |
1291 | } else { | |
1292 | return array(); | |
1293 | } | |
1294 | } | |
1295 | ||
6e4dc10f | 1296 | /** |
2ce38b70 | 1297 | * Determines if the current user has access to this external page based on $this->req_capability. |
db26acd4 | 1298 | * |
6e4dc10f | 1299 | * @return bool True if user has access, false otherwise. |
1300 | */ | |
73fa96d5 | 1301 | public function check_access() { |
1caea91e | 1302 | global $CFG; |
b0c6dc1c | 1303 | $context = empty($this->context) ? context_system::instance() : $this->context; |
38d2d43b | 1304 | foreach($this->req_capability as $cap) { |
4f0c2d00 | 1305 | if (has_capability($cap, $context)) { |
38d2d43b | 1306 | return true; |
1307 | } | |
1308 | } | |
1309 | return false; | |
6e4dc10f | 1310 | } |
1311 | ||
a8a66c96 | 1312 | /** |
1313 | * Is this external page hidden in admin tree block? | |
1314 | * | |
1315 | * @return bool True if hidden | |
1316 | */ | |
73fa96d5 | 1317 | public function is_hidden() { |
a8a66c96 | 1318 | return $this->hidden; |
1319 | } | |
1320 | ||
427649bf PS |
1321 | /** |
1322 | * Show we display Save button at the page bottom? | |
1323 | * @return bool | |
1324 | */ | |
1325 | public function show_save() { | |
1326 | return false; | |
1327 | } | |
6e4dc10f | 1328 | } |
1329 | ||
8ed3671d DS |
1330 | /** |
1331 | * Used to store details of the dependency between two settings elements. | |
1332 | * | |
1333 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
1334 | * @copyright 2017 Davo Smith, Synergy Learning | |
1335 | */ | |
1336 | class admin_settingdependency { | |
1337 | /** @var string the name of the setting to be shown/hidden */ | |
1338 | public $settingname; | |
1339 | /** @var string the setting this is dependent on */ | |
1340 | public $dependenton; | |
1341 | /** @var string the condition to show/hide the element */ | |
1342 | public $condition; | |
1343 | /** @var string the value to compare against */ | |
1344 | public $value; | |
1345 | ||
1346 | /** @var string[] list of valid conditions */ | |
1347 | private static $validconditions = ['checked', 'notchecked', 'noitemselected', 'eq', 'neq', 'in']; | |
1348 | ||
1349 | /** | |
1350 | * admin_settingdependency constructor. | |
1351 | * @param string $settingname | |
1352 | * @param string $dependenton | |
1353 | * @param string $condition | |
1354 | * @param string $value | |
1355 | * @throws \coding_exception | |
1356 | */ | |
1357 | public function __construct($settingname, $dependenton, $condition, $value) { | |
1358 | $this->settingname = $this->parse_name($settingname); | |
1359 | $this->dependenton = $this->parse_name($dependenton); | |
1360 | $this->condition = $condition; | |
1361 | $this->value = $value; | |
1362 | ||
1363 | if (!in_array($this->condition, self::$validconditions)) { | |
1364 | throw new coding_exception("Invalid condition '$condition'"); | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | /** | |
1369 | * Convert the setting name into the form field name. | |
1370 | * @param string $name | |
1371 | * @return string | |
1372 | */ | |
1373 | private function parse_name($name) { | |
1374 | $bits = explode('/', $name); | |
1375 | $name = array_pop($bits); | |
1376 | $plugin = ''; | |
1377 | if ($bits) { | |
1378 | $plugin = array_pop($bits); | |
1379 | if ($plugin === 'moodle') { | |
1380 | $plugin = ''; | |
1381 | } | |
1382 | } | |
1383 | return 's_'.$plugin.'_'.$name; | |
1384 | } | |
1385 | ||
1386 | /** | |
1387 | * Gather together all the dependencies in a format suitable for initialising javascript | |
1388 | * @param admin_settingdependency[] $dependencies | |
1389 | * @return array | |
1390 | */ | |
1391 | public static function prepare_for_javascript($dependencies) { | |
1392 | $result = []; | |
1393 | foreach ($dependencies as $d) { | |
1394 | if (!isset($result[$d->dependenton])) { | |
1395 | $result[$d->dependenton] = []; | |
1396 | } | |
1397 | if (!isset($result[$d->dependenton][$d->condition])) { | |
1398 | $result[$d->dependenton][$d->condition] = []; | |
1399 | } | |
1400 | if (!isset($result[$d->dependenton][$d->condition][$d->value])) { | |
1401 | $result[$d->dependenton][$d->condition][$d->value] = []; | |
1402 | } | |
1403 | $result[$d->dependenton][$d->condition][$d->value][] = $d->settingname; | |
1404 | } | |
1405 | return $result; | |
1406 | } | |
1407 | } | |
3e7069e7 | 1408 | |
6e4dc10f | 1409 | /** |
1410 | * Used to group a number of admin_setting objects into a page and add them to the admin tree. | |
1411 | * | |
db26acd4 | 1412 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
6e4dc10f | 1413 | */ |
73fa96d5 | 1414 | class admin_settingpage implements part_of_admin_tree { |
6e4dc10f | 1415 | |
3e7069e7 | 1416 | /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */ |
73fa96d5 | 1417 | public $name; |
eef868d1 | 1418 | |
0c079f19 | 1419 | /** @var string The displayed name for this external page. Usually obtained through get_string(). */ |
73fa96d5 | 1420 | public $visiblename; |
0c079f19 | 1421 | |
1422 | /** @var mixed An array of admin_setting objects that are part of this setting page. */ | |
73fa96d5 | 1423 | public $settings; |
eef868d1 | 1424 | |
8ed3671d DS |
1425 | /** @var admin_settingdependency[] list of settings to hide when certain conditions are met */ |
1426 | protected $dependencies = []; | |
1427 | ||
e3c28dff | 1428 | /** @var array The role capability/permission a user must have to access this external page. */ |
73fa96d5 | 1429 | public $req_capability; |
eef868d1 | 1430 | |
0c079f19 | 1431 | /** @var object The context in which capability/permission should be checked, default is site context. */ |
73fa96d5 | 1432 | public $context; |
84c8ede0 | 1433 | |
0c079f19 | 1434 | /** @var bool hidden in admin tree block. */ |
73fa96d5 | 1435 | public $hidden; |
a8a66c96 | 1436 | |
0c079f19 | 1437 | /** @var mixed string of paths or array of strings of paths */ |
73fa96d5 | 1438 | public $path; |
3e7069e7 PS |
1439 | |
1440 | /** @var array list of visible names of page parents */ | |
73fa96d5 | 1441 | public $visiblepath; |
220a90c5 | 1442 | |
db26acd4 | 1443 | /** |
1444 | * see admin_settingpage for details of this function | |
0c079f19 | 1445 | * |
db26acd4 | 1446 | * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects. |
1447 | * @param string $visiblename The displayed name for this external page. Usually obtained through get_string(). | |
1448 | * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'. | |
1449 | * @param boolean $hidden Is this external page hidden in admin tree block? Default false. | |
44d8a940 | 1450 | * @param stdClass $context The context the page relates to. Not sure what happens |
db26acd4 | 1451 | * if you specify something other than system or front page. Defaults to system. |
1452 | */ | |
73fa96d5 | 1453 | public function __construct($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) { |
365a5941 | 1454 | $this->settings = new stdClass(); |
220a90c5 | 1455 | $this->name = $name; |
1456 | $this->visiblename = $visiblename; | |
1457 | if (is_array($req_capability)) { | |
1458 | $this->req_capability = $req_capability; | |
6e4dc10f | 1459 | } else { |
220a90c5 | 1460 | $this->req_capability = array($req_capability); |
6e4dc10f | 1461 | } |
220a90c5 | 1462 | $this->hidden = $hidden; |
1463 | $this->context = $context; | |
6e4dc10f | 1464 | } |
eef868d1 | 1465 | |
0c079f19 | 1466 | /** |
db26acd4 | 1467 | * see admin_category |
1468 | * | |
1469 | * @param string $name | |
1470 | * @param bool $findpath | |
1471 | * @return mixed Object (this) if name == this->name, else returns null | |
1472 | */ | |
73fa96d5 | 1473 | public function locate($name, $findpath=false) { |
220a90c5 | 1474 | if ($this->name == $name) { |
1475 | if ($findpath) { | |
1476 | $this->visiblepath = array($this->visiblename); | |
1477 | $this->path = array($this->name); | |
1478 | } | |
1479 | return $this; | |
1480 | } else { | |
1481 | $return = NULL; | |
1482 | return $return; | |
1483 | } | |
6e4dc10f | 1484 | } |
4672d955 | 1485 | |
0c079f19 | 1486 | /** |
1487 | * Search string in settings page. | |
1488 | * | |
db26acd4 | 1489 | * @param string $query |
1490 | * @return array | |
1491 | */ | |
73fa96d5 | 1492 | public function search($query) { |
220a90c5 | 1493 | $found = array(); |
4672d955 | 1494 | |
220a90c5 | 1495 | foreach ($this->settings as $setting) { |
1496 | if ($setting->is_related($query)) { | |
1497 | $found[] = $setting; | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | if ($found) { | |
365a5941 | 1502 | $result = new stdClass(); |
220a90c5 | 1503 | $result->page = $this; |
1504 | $result->settings = $found; | |
1505 | return array($this->name => $result); | |
1506 | } | |
1507 | ||
220a90c5 | 1508 | $found = false; |
1509 | if (strpos(strtolower($this->name), $query) !== false) { | |
1510 | $found = true; | |
2f1e464a | 1511 | } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) { |
9baf6825 | 1512 | $found = true; |
1513 | } | |
220a90c5 | 1514 | if ($found) { |
365a5941 | 1515 | $result = new stdClass(); |
220a90c5 | 1516 | $result->page = $this; |
1517 | $result->settings = array(); | |
1518 | return array($this->name => $result); | |
38d2d43b | 1519 | } else { |
220a90c5 | 1520 | return array(); |
38d2d43b | 1521 | } |
6e4dc10f | 1522 | } |
eef868d1 | 1523 | |
db26acd4 | 1524 | /** |
1525 | * This function always returns false, required by interface | |
1526 | * | |
1527 | * @param string $name | |
1528 | * @return bool Always false | |
1529 | */ | |
73fa96d5 | 1530 | public function prune($name) { |
6e4dc10f | 1531 | return false; |
1532 | } | |
eef868d1 | 1533 | |
220a90c5 | 1534 | /** |
db26acd4 | 1535 | * adds an admin_setting to this admin_settingpage |
1536 | * | |
220a90c5 | 1537 | * not the same as add for admin_category. adds an admin_setting to this admin_settingpage. settings appear (on the settingpage) in the order in which they're added |
1538 | * n.b. each admin_setting in an admin_settingpage must have a unique internal name | |
db26acd4 | 1539 | * |
220a90c5 | 1540 | * @param object $setting is the admin_setting object you want to add |
db26acd4 | 1541 | * @return bool true if successful, false if not |
220a90c5 | 1542 | */ |
73fa96d5 | 1543 | public function add($setting) { |
1544 | if (!($setting instanceof admin_setting)) { | |
220a90c5 | 1545 | debugging('error - not a setting instance'); |
1546 | return false; | |
1547 | } | |
1548 | ||
69d66020 DM |
1549 | $name = $setting->name; |
1550 | if ($setting->plugin) { | |
1551 | $name = $setting->plugin . $name; | |
1552 | } | |
1553 | $this->settings->{$name} = $setting; | |
220a90c5 | 1554 | return true; |
1555 | } | |
1556 | ||
8ed3671d DS |
1557 | /** |
1558 | * Hide the named setting if the specified condition is matched. | |
1559 | * | |
1560 | * @param string $settingname | |
1561 | * @param string $dependenton | |
1562 | * @param string $condition | |
1563 | * @param string $value | |
1564 | */ | |
1565 | public function hide_if($settingname, $dependenton, $condition = 'notchecked', $value = '1') { | |
1566 | $this->dependencies[] = new admin_settingdependency($settingname, $dependenton, $condition, $value); | |
08163b20 DS |
1567 | |
1568 | // Reformat the dependency name to the plugin | name format used in the display. | |
1569 | $dependenton = str_replace('/', ' | ', $dependenton); | |
1570 | ||
1571 | // Let the setting know, so it can be displayed underneath. | |
1572 | $findname = str_replace('/', '', $settingname); | |
1573 | foreach ($this->settings as $name => $setting) { | |
1574 | if ($name === $findname) { | |
1575 | $setting->add_dependent_on($dependenton); | |
1576 | } | |
1577 | } | |
8ed3671d DS |
1578 | } |
1579 | ||
db26acd4 | 1580 | /** |
1581 | * see admin_externalpage | |
1582 | * | |
1583 | * @return bool Returns true for yes false for no | |
1584 | */ | |
73fa96d5 | 1585 | public function check_access() { |
1caea91e | 1586 | global $CFG; |
b0c6dc1c | 1587 | $context = empty($this->context) ? context_system::instance() : $this->context; |
38d2d43b | 1588 | foreach($this->req_capability as $cap) { |
4f0c2d00 | 1589 | if (has_capability($cap, $context)) { |
38d2d43b | 1590 | return true; |
1591 | } | |
1592 | } | |
1593 | return false; | |
6e4dc10f | 1594 | } |
eef868d1 | 1595 | |
220a90c5 | 1596 | /** |
1597 | * outputs this page as html in a table (suitable for inclusion in an admin pagetype) | |
db26acd4 | 1598 | * @return string Returns an XHTML string |
220a90c5 | 1599 | */ |
73fa96d5 | 1600 | public function output_html() { |
1601 | $adminroot = admin_get_root(); | |
220a90c5 | 1602 | $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n"; |
6e4dc10f | 1603 | foreach($this->settings as $setting) { |
220a90c5 | 1604 | $fullname = $setting->get_full_name(); |
1605 | if (array_key_exists($fullname, $adminroot->errors)) { | |
1606 | $data = $adminroot->errors[$fullname]->data; | |
6e4dc10f | 1607 | } else { |
220a90c5 | 1608 | $data = $setting->get_setting(); |
79698344 | 1609 | // do not use defaults if settings not available - upgrade settings handles the defaults! |
6e4dc10f | 1610 | } |
220a90c5 | 1611 | $return .= $setting->output_html($data); |
6e4dc10f | 1612 | } |
220a90c5 | 1613 | $return .= '</fieldset>'; |
6e4dc10f | 1614 | return $return; |
1615 | } | |
1616 | ||
a8a66c96 | 1617 | /** |
875e5f07 | 1618 | * Is this settings page hidden in admin tree block? |
a8a66c96 | 1619 | * |
1620 | * @return bool True if hidden | |
1621 | */ | |
73fa96d5 | 1622 | public function is_hidden() { |
a8a66c96 | 1623 | return $this->hidden; |
1624 | } | |
1625 | ||
427649bf PS |
1626 | /** |
1627 | * Show we display Save button at the page bottom? | |
1628 | * @return bool | |
1629 | */ | |
1630 | public function show_save() { | |
1631 | foreach($this->settings as $setting) { | |
1632 | if (empty($setting->nosave)) { | |
1633 | return true; | |
1634 | } | |
1635 | } | |
1636 | return false; | |
1637 | } | |
8ed3671d DS |
1638 | |
1639 | /** | |
1640 | * Should any of the settings on this page be shown / hidden based on conditions? | |
1641 | * @return bool | |
1642 | */ | |
1643 | public function has_dependencies() { | |
1644 | return (bool)$this->dependencies; | |
1645 | } | |
1646 | ||
1647 | /** | |
1648 | * Format the setting show/hide conditions ready to initialise the page javascript | |
1649 | * @return array | |
1650 | */ | |
1651 | public function get_dependencies_for_javascript() { | |
1652 | if (!$this->has_dependencies()) { | |
1653 | return []; | |
1654 | } | |
1655 | return admin_settingdependency::prepare_for_javascript($this->dependencies); | |
1656 | } | |
6e4dc10f | 1657 | } |
1658 | ||
1659 | ||
220a90c5 | 1660 | /** |
1661 | * Admin settings class. Only exists on setting pages. | |
1662 | * Read & write happens at this level; no authentication. | |
db26acd4 | 1663 | * |
1664 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 1665 | */ |
301bf0b2 | 1666 | abstract class admin_setting { |
3e7069e7 | 1667 | /** @var string unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. */ |
73fa96d5 | 1668 | public $name; |
0c079f19 | 1669 | /** @var string localised name */ |
73fa96d5 | 1670 | public $visiblename; |
3fa37159 | 1671 | /** @var string localised long description in Markdown format */ |
73fa96d5 | 1672 | public $description; |
0c079f19 | 1673 | /** @var mixed Can be string or array of string */ |
73fa96d5 | 1674 | public $defaultsetting; |
0c079f19 | 1675 | /** @var string */ |
73fa96d5 | 1676 | public $updatedcallback; |
0c079f19 | 1677 | /** @var mixed can be String or Null. Null means main config table */ |
73fa96d5 | 1678 | public $plugin; // null means main config table |
427649bf PS |
1679 | /** @var bool true indicates this setting does not actually save anything, just information */ |
1680 | public $nosave = false; | |
adaeccb6 | 1681 | /** @var bool if set, indicates that a change to this setting requires rebuild course cache */ |
1682 | public $affectsmodinfo = false; | |
3c88a678 DW |
1683 | /** @var array of admin_setting_flag - These are extra checkboxes attached to a setting. */ |
1684 | private $flags = array(); | |
113efed5 | 1685 | /** @var bool Whether this field must be forced LTR. */ |
8a40bc36 | 1686 | private $forceltr = null; |
08163b20 DS |
1687 | /** @var array list of other settings that may cause this setting to be hidden */ |
1688 | private $dependenton = []; | |
4ceff26f SR |
1689 | /** @var bool Whether this setting uses a custom form control */ |
1690 | protected $customcontrol = false; | |
6e4dc10f | 1691 | |
220a90c5 | 1692 | /** |
1693 | * Constructor | |
0c079f19 | 1694 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, |
db26acd4 | 1695 | * or 'myplugin/mysetting' for ones in config_plugins. |
1a41e806 | 1696 | * @param string $visiblename localised name |
1697 | * @param string $description localised long description | |
220a90c5 | 1698 | * @param mixed $defaultsetting string or array depending on implementation |
1699 | */ | |
73fa96d5 | 1700 | public function __construct($name, $visiblename, $description, $defaultsetting) { |
7fb0303d | 1701 | $this->parse_setting_name($name); |
220a90c5 | 1702 | $this->visiblename = $visiblename; |
8dbe233a | 1703 | $this->description = $description; |
6e4dc10f | 1704 | $this->defaultsetting = $defaultsetting; |
1705 | } | |
eef868d1 | 1706 | |
3c88a678 DW |
1707 | /** |
1708 | * Generic function to add a flag to this admin setting. | |
1709 | * | |
1710 | * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED | |
1711 | * @param bool $default - The default for the flag | |
1712 | * @param string $shortname - The shortname for this flag. Used as a suffix for the setting name. | |
1713 | * @param string $displayname - The display name for this flag. Used as a label next to the checkbox. | |
1714 | */ | |
1715 | protected function set_flag_options($enabled, $default, $shortname, $displayname) { | |
1716 | if (empty($this->flags[$shortname])) { | |
1717 | $this->flags[$shortname] = new admin_setting_flag($enabled, $default, $shortname, $displayname); | |
1718 | } else { | |
1719 | $this->flags[$shortname]->set_options($enabled, $default); | |
1720 | } | |
1721 | } | |
1722 | ||
1723 | /** | |
1724 | * Set the enabled options flag on this admin setting. | |
1725 | * | |
1726 | * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED | |
1727 | * @param bool $default - The default for the flag | |
1728 | */ | |
1729 | public function set_enabled_flag_options($enabled, $default) { | |
1730 | $this->set_flag_options($enabled, $default, 'enabled', new lang_string('enabled', 'core_admin')); | |
1731 | } | |
1732 | ||
1733 | /** | |
1734 | * Set the advanced options flag on this admin setting. | |
1735 | * | |
1736 | * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED | |
1737 | * @param bool $default - The default for the flag | |
1738 | */ | |
1739 | public function set_advanced_flag_options($enabled, $default) { | |
1740 | $this->set_flag_options($enabled, $default, 'adv', new lang_string('advanced')); | |
1741 | } | |
1742 | ||
1743 | ||
1744 | /** | |
1745 | * Set the locked options flag on this admin setting. | |
1746 | * | |
1747 | * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED | |
1748 | * @param bool $default - The default for the flag | |
1749 | */ | |
1750 | public function set_locked_flag_options($enabled, $default) { | |
1751 | $this->set_flag_options($enabled, $default, 'locked', new lang_string('locked', 'core_admin')); | |
1752 | } | |
1753 | ||
948a951a AM |
1754 | /** |
1755 | * Set the required options flag on this admin setting. | |
1756 | * | |
1757 | * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED. | |
1758 | * @param bool $default - The default for the flag. | |
1759 | */ | |
1760 | public function set_required_flag_options($enabled, $default) { | |
1761 | $this->set_flag_options($enabled, $default, 'required', new lang_string('required', 'core_admin')); | |
1762 | } | |
1763 | ||
3c88a678 DW |
1764 | /** |
1765 | * Get the currently saved value for a setting flag | |
1766 | * | |
1767 | * @param admin_setting_flag $flag - One of the admin_setting_flag for this admin_setting. | |
1768 | * @return bool | |
1769 | */ | |
1770 | public function get_setting_flag_value(admin_setting_flag $flag) { | |
1771 | $value = $this->config_read($this->name . '_' . $flag->get_shortname()); | |
1772 | if (!isset($value)) { | |
1773 | $value = $flag->get_default(); | |
1774 | } | |
1775 | ||
1776 | return !empty($value); | |
1777 | } | |
1778 | ||
1779 | /** | |
1780 | * Get the list of defaults for the flags on this setting. | |
1781 | * | |
1782 | * @param array of strings describing the defaults for this setting. This is appended to by this function. | |
1783 | */ | |
1784 | public function get_setting_flag_defaults(& $defaults) { | |
1785 | foreach ($this->flags as $flag) { | |
1786 | if ($flag->is_enabled() && $flag->get_default()) { | |
1787 | $defaults[] = $flag->get_displayname(); | |
1788 | } | |
1789 | } | |
1790 | } | |
1791 | ||
1792 | /** | |
1793 | * Output the input fields for the advanced and locked flags on this setting. | |
1794 | * | |
1795 | * @param bool $adv - The current value of the advanced flag. | |
1796 | * @param bool $locked - The current value of the locked flag. | |
1797 | * @return string $output - The html for the flags. | |
1798 | */ | |
1799 | public function output_setting_flags() { | |
1800 | $output = ''; | |
1801 | ||
1802 | foreach ($this->flags as $flag) { | |
1803 | if ($flag->is_enabled()) { | |
1804 | $output .= $flag->output_setting_flag($this); | |
1805 | } | |
1806 | } | |
1807 | ||
459e3c88 DW |
1808 | if (!empty($output)) { |
1809 | return html_writer::tag('span', $output, array('class' => 'adminsettingsflags')); | |
1810 | } | |
3c88a678 DW |
1811 | return $output; |
1812 | } | |
1813 | ||
1814 | /** | |
1815 | * Write the values of the flags for this admin setting. | |
1816 | * | |
7e815a6e | 1817 | * @param array $data - The data submitted from the form or null to set the default value for new installs. |
3c88a678 DW |
1818 | * @return bool - true if successful. |
1819 | */ | |
1820 | public function write_setting_flags($data) { | |
1821 | $result = true; | |
1822 | foreach ($this->flags as $flag) { | |
1823 | $result = $result && $flag->write_setting_flag($this, $data); | |
1824 | } | |
1825 | return $result; | |
1826 | } | |
1827 | ||
7fb0303d | 1828 | /** |
db26acd4 | 1829 | * Set up $this->name and potentially $this->plugin |
1830 | * | |
7fb0303d | 1831 | * Set up $this->name and possibly $this->plugin based on whether $name looks |
1832 | * like 'settingname' or 'plugin/settingname'. Also, do some sanity checking | |
1833 | * on the names, that is, output a developer debug warning if the name | |
1834 | * contains anything other than [a-zA-Z0-9_]+. | |
1835 | * | |
1836 | * @param string $name the setting name passed in to the constructor. | |
1837 | */ | |
1838 | private function parse_setting_name($name) { | |
1839 | $bits = explode('/', $name); | |
1840 | if (count($bits) > 2) { | |
1841 | throw new moodle_exception('invalidadminsettingname', '', '', $name); | |
1842 | } | |
1843 | $this->name = array_pop($bits); | |
1844 | if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) { | |
1845 | throw new moodle_exception('invalidadminsettingname', '', '', $name); | |
1846 | } | |
1847 | if (!empty($bits)) { | |
1848 | $this->plugin = array_pop($bits); | |
cd3acbf2 | 1849 | if ($this->plugin === 'moodle') { |
1850 | $this->plugin = null; | |
1851 | } else if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->plugin)) { | |
9baf6825 | 1852 | throw new moodle_exception('invalidadminsettingname', '', '', $name); |
1853 | } | |
7fb0303d | 1854 | } |
1855 | } | |
1856 | ||
db26acd4 | 1857 | /** |
1858 | * Returns the fullname prefixed by the plugin | |
1859 | * @return string | |
1860 | */ | |
73fa96d5 | 1861 | public function get_full_name() { |
220a90c5 | 1862 | return 's_'.$this->plugin.'_'.$this->name; |
1863 | } | |
1864 | ||
db26acd4 | 1865 | /** |
1866 | * Returns the ID string based on plugin and name | |
1867 | * @return string | |
1868 | */ | |
73fa96d5 | 1869 | public function get_id() { |
220a90c5 | 1870 | return 'id_s_'.$this->plugin.'_'.$this->name; |
1871 | } | |
1872 | ||
adaeccb6 | 1873 | /** |
1874 | * @param bool $affectsmodinfo If true, changes to this setting will | |
1875 | * cause the course cache to be rebuilt | |
1876 | */ | |
1877 | public function set_affects_modinfo($affectsmodinfo) { | |
1878 | $this->affectsmodinfo = $affectsmodinfo; | |
1879 | } | |
1880 | ||
db26acd4 | 1881 | /** |
1882 | * Returns the config if possible | |
1883 | * | |
3e7069e7 | 1884 | * @return mixed returns config if successful else null |
db26acd4 | 1885 | */ |
73fa96d5 | 1886 | public function config_read($name) { |
220a90c5 | 1887 | global $CFG; |
eb6a973c | 1888 | if (!empty($this->plugin)) { |
220a90c5 | 1889 | $value = get_config($this->plugin, $name); |
1890 | return $value === false ? NULL : $value; | |
1891 | ||
1892 | } else { | |
1893 | if (isset($CFG->$name)) { | |
1894 | return $CFG->$name; | |
1895 | } else { | |
1896 | return NULL; | |
1897 | } | |
1898 | } | |
1899 | } | |
1900 | ||
301bf0b2 | 1901 | /** |
db26acd4 | 1902 | * Used to set a config pair and log change |
301bf0b2 | 1903 | * |
db26acd4 | 1904 | * @param string $name |
1905 | * @param mixed $value Gets converted to string if not null | |
875e5f07 | 1906 | * @return bool Write setting to config table |
301bf0b2 | 1907 | */ |
73fa96d5 | 1908 | public function config_write($name, $value) { |
301bf0b2 | 1909 | global $DB, $USER, $CFG; |
1910 | ||
427649bf PS |
1911 | if ($this->nosave) { |
1912 | return true; | |
1913 | } | |
1914 | ||
301bf0b2 | 1915 | // make sure it is a real change |
1916 | $oldvalue = get_config($this->plugin, $name); | |
1917 | $oldvalue = ($oldvalue === false) ? null : $oldvalue; // normalise | |
1918 | $value = is_null($value) ? null : (string)$value; | |
1919 | ||
1920 | if ($oldvalue === $value) { | |
1921 | return true; | |
1922 | } | |
1923 | ||
1924 | // store change | |
1925 | set_config($name, $value, $this->plugin); | |
1926 | ||
adaeccb6 | 1927 | // Some admin settings affect course modinfo |
1928 | if ($this->affectsmodinfo) { | |
1929 | // Clear course cache for all courses | |
1930 | rebuild_course_cache(0, true); | |
1931 | } | |
1932 | ||
914499a3 | 1933 | $this->add_to_config_log($name, $oldvalue, $value); |
301bf0b2 | 1934 | |
1935 | return true; // BC only | |
220a90c5 | 1936 | } |
1937 | ||
914499a3 PS |
1938 | /** |
1939 | * Log config changes if necessary. | |
1940 | * @param string $name | |
1941 | * @param string $oldvalue | |
1942 | * @param string $value | |
1943 | */ | |
1944 | protected function add_to_config_log($name, $oldvalue, $value) { | |
1945 | add_to_config_log($name, $oldvalue, $value, $this->plugin); | |
1946 | } | |
1947 | ||
220a90c5 | 1948 | /** |
1949 | * Returns current value of this setting | |
1950 | * @return mixed array or string depending on instance, NULL means not set yet | |
1951 | */ | |
301bf0b2 | 1952 | public abstract function get_setting(); |
eef868d1 | 1953 | |
220a90c5 | 1954 | /** |
1955 | * Returns default setting if exists | |
1956 | * @return mixed array or string depending on instance; NULL means no default, user must supply | |
1957 | */ | |
73fa96d5 | 1958 | public function get_defaultsetting() { |
cd3acbf2 | 1959 | $adminroot = admin_get_root(false, false); |
1960 | if (!empty($adminroot->custom_defaults)) { | |
1961 | $plugin = is_null($this->plugin) ? 'moodle' : $this->plugin; | |
1962 | if (isset($adminroot->custom_defaults[$plugin])) { | |
875e5f07 | 1963 | if (array_key_exists($this->name, $adminroot->custom_defaults[$plugin])) { // null is valid value here ;-) |
cd3acbf2 | 1964 | return $adminroot->custom_defaults[$plugin][$this->name]; |
1965 | } | |
1966 | } | |
1967 | } | |
8e5da17a | 1968 | return $this->defaultsetting; |
1969 | } | |
1970 | ||
220a90c5 | 1971 | /** |
1972 | * Store new setting | |
db26acd4 | 1973 | * |
1974 | * @param mixed $data string or array, must not be NULL | |
1975 | * @return string empty string if ok, string error message otherwise | |
220a90c5 | 1976 | */ |
301bf0b2 | 1977 | public abstract function write_setting($data); |
eef868d1 | 1978 | |
220a90c5 | 1979 | /** |
1980 | * Return part of form with setting | |
db26acd4 | 1981 | * This function should always be overwritten |
1982 | * | |
1983 | * @param mixed $data array or string depending on setting | |
1984 | * @param string $query | |
220a90c5 | 1985 | * @return string |
1986 | */ | |
73fa96d5 | 1987 | public function output_html($data, $query='') { |
9baf6825 | 1988 | // should be overridden |
220a90c5 | 1989 | return; |
1990 | } | |
1991 | ||
1992 | /** | |
db26acd4 | 1993 | * Function called if setting updated - cleanup, cache reset, etc. |
1994 | * @param string $functionname Sets the function name | |
3e7069e7 | 1995 | * @return void |
220a90c5 | 1996 | */ |
73fa96d5 | 1997 | public function set_updatedcallback($functionname) { |
220a90c5 | 1998 | $this->updatedcallback = $functionname; |
1999 | } | |
2000 | ||
6ddc9278 PS |
2001 | /** |
2002 | * Execute postupdatecallback if necessary. | |
2003 | * @param mixed $original original value before write_setting() | |
2004 | * @return bool true if changed, false if not. | |
2005 | */ | |
2006 | public function post_write_settings($original) { | |
2007 | // Comparison must work for arrays too. | |
2008 | if (serialize($original) === serialize($this->get_setting())) { | |
2009 | return false; | |
2010 | } | |
2011 | ||
2012 | $callbackfunction = $this->updatedcallback; | |
484b43f4 | 2013 | if (!empty($callbackfunction) and is_callable($callbackfunction)) { |
6ddc9278 PS |
2014 | $callbackfunction($this->get_full_name()); |
2015 | } | |
2016 | return true; | |
2017 | } | |
2018 | ||
220a90c5 | 2019 | /** |
2020 | * Is setting related to query text - used when searching | |
2021 | * @param string $query | |
2022 | * @return bool | |
2023 | */ | |
73fa96d5 | 2024 | public function is_related($query) { |
220a90c5 | 2025 | if (strpos(strtolower($this->name), $query) !== false) { |
2026 | return true; | |
2027 | } | |
2f1e464a | 2028 | if (strpos(core_text::strtolower($this->visiblename), $query) !== false) { |
220a90c5 | 2029 | return true; |
2030 | } | |
2f1e464a | 2031 | if (strpos(core_text::strtolower($this->description), $query) !== false) { |
220a90c5 | 2032 | return true; |
2033 | } | |
587c7040 | 2034 | $current = $this->get_setting(); |
2035 | if (!is_null($current)) { | |
2036 | if (is_string($current)) { | |
2f1e464a | 2037 | if (strpos(core_text::strtolower($current), $query) !== false) { |
587c7040 | 2038 | return true; |
2039 | } | |
2040 | } | |
2041 | } | |
2042 | $default = $this->get_defaultsetting(); | |
2043 | if (!is_null($default)) { | |
2044 | if (is_string($default)) { | |
2f1e464a | 2045 | if (strpos(core_text::strtolower($default), $query) !== false) { |
587c7040 | 2046 | return true; |
2047 | } | |
2048 | } | |
2049 | } | |
220a90c5 | 2050 | return false; |
6e4dc10f | 2051 | } |
113efed5 FM |
2052 | |
2053 | /** | |
2054 | * Get whether this should be displayed in LTR mode. | |
2055 | * | |
8a40bc36 | 2056 | * @return bool|null |
113efed5 FM |
2057 | */ |
2058 | public function get_force_ltr() { | |
2059 | return $this->forceltr; | |
2060 | } | |
2061 | ||
2062 | /** | |
2063 | * Set whether to force LTR or not. | |
2064 | * | |
8a40bc36 | 2065 | * @param bool $value True when forced, false when not force, null when unknown. |
113efed5 FM |
2066 | */ |
2067 | public function set_force_ltr($value) { | |
8a40bc36 | 2068 | $this->forceltr = $value; |
113efed5 | 2069 | } |
08163b20 DS |
2070 | |
2071 | /** | |
2072 | * Add a setting to the list of those that could cause this one to be hidden | |
2073 | * @param string $dependenton | |
2074 | */ | |
2075 | public function add_dependent_on($dependenton) { | |
2076 | $this->dependenton[] = $dependenton; | |
2077 | } | |
2078 | ||
2079 | /** | |
2080 | * Get a list of the settings that could cause this one to be hidden. | |
2081 | * @return array | |
2082 | */ | |
2083 | public function get_dependent_on() { | |
2084 | return $this->dependenton; | |
2085 | } | |
4ceff26f SR |
2086 | |
2087 | /** | |
2088 | * Whether this setting uses a custom form control. | |
2089 | * This function is especially useful to decide if we should render a label element for this setting or not. | |
2090 | * | |
2091 | * @return bool | |
2092 | */ | |
2093 | public function has_custom_form_control(): bool { | |
2094 | return $this->customcontrol; | |
2095 | } | |
220a90c5 | 2096 | } |
eef868d1 | 2097 | |
3c88a678 DW |
2098 | /** |
2099 | * An additional option that can be applied to an admin setting. | |
948a951a | 2100 | * The currently supported options are 'ADVANCED', 'LOCKED' and 'REQUIRED'. |
3c88a678 DW |
2101 | * |
2102 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2103 | */ | |
2104 | class admin_setting_flag { | |
2105 | /** @var bool Flag to indicate if this option can be toggled for this setting */ | |
2106 | private $enabled = false; | |
2107 | /** @var bool Flag to indicate if this option defaults to true or false */ | |
2108 | private $default = false; | |
2109 | /** @var string Short string used to create setting name - e.g. 'adv' */ | |
2110 | private $shortname = ''; | |
2111 | /** @var string String used as the label for this flag */ | |
2112 | private $displayname = ''; | |
2113 | /** @const Checkbox for this flag is displayed in admin page */ | |
2114 | const ENABLED = true; | |
2115 | /** @const Checkbox for this flag is not displayed in admin page */ | |
2116 | const DISABLED = false; | |
2117 | ||
2118 | /** | |
2119 | * Constructor | |
2120 | * | |
2121 | * @param bool $enabled Can this option can be toggled. | |
2122 | * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED. | |
2123 | * @param bool $default The default checked state for this setting option. | |
2124 | * @param string $shortname The shortname of this flag. Currently supported flags are 'locked' and 'adv' | |
2125 | * @param string $displayname The displayname of this flag. Used as a label for the flag. | |
2126 | */ | |
2127 | public function __construct($enabled, $default, $shortname, $displayname) { | |
2128 | $this->shortname = $shortname; | |
2129 | $this->displayname = $displayname; | |
2130 | $this->set_options($enabled, $default); | |
2131 | } | |
2132 | ||
2133 | /** | |
2134 | * Update the values of this setting options class | |
2135 | * | |
2136 | * @param bool $enabled Can this option can be toggled. | |
2137 | * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED. | |
2138 | * @param bool $default The default checked state for this setting option. | |
2139 | */ | |
2140 | public function set_options($enabled, $default) { | |
2141 | $this->enabled = $enabled; | |
2142 | $this->default = $default; | |
2143 | } | |
2144 | ||
2145 | /** | |
2146 | * Should this option appear in the interface and be toggleable? | |
2147 | * | |
2148 | * @return bool Is it enabled? | |
2149 | */ | |
2150 | public function is_enabled() { | |
2151 | return $this->enabled; | |
2152 | } | |
2153 | ||
2154 | /** | |
2155 | * Should this option be checked by default? | |
2156 | * | |
2157 | * @return bool Is it on by default? | |
2158 | */ | |
2159 | public function get_default() { | |
2160 | return $this->default; | |
2161 | } | |
2162 | ||
2163 | /** | |
2164 | * Return the short name for this flag. e.g. 'adv' or 'locked' | |
2165 | * | |
2166 | * @return string | |
2167 | */ | |
2168 | public function get_shortname() { | |
2169 | return $this->shortname; | |
2170 | } | |
2171 | ||
2172 | /** | |
2173 | * Return the display name for this flag. e.g. 'Advanced' or 'Locked' | |
2174 | * | |
2175 | * @return string | |
2176 | */ | |
2177 | public function get_displayname() { | |
2178 | return $this->displayname; | |
2179 | } | |
2180 | ||
2181 | /** | |
2182 | * Save the submitted data for this flag - or set it to the default if $data is null. | |
2183 | * | |
2184 | * @param admin_setting $setting - The admin setting for this flag | |
7e815a6e | 2185 | * @param array $data - The data submitted from the form or null to set the default value for new installs. |
3c88a678 DW |
2186 | * @return bool |
2187 | */ | |
2188 | public function write_setting_flag(admin_setting $setting, $data) { | |
2189 | $result = true; | |
2190 | if ($this->is_enabled()) { | |
7e815a6e DW |
2191 | if (!isset($data)) { |
2192 | $value = $this->get_default(); | |
2193 | } else { | |
2194 | $value = !empty($data[$setting->get_full_name() . '_' . $this->get_shortname()]); | |
2195 | } | |
3c88a678 DW |
2196 | $result = $setting->config_write($setting->name . '_' . $this->get_shortname(), $value); |
2197 | } | |
2198 | ||
2199 | return $result; | |
2200 | ||
2201 | } | |
2202 | ||
2203 | /** | |
2204 | * Output the checkbox for this setting flag. Should only be called if the flag is enabled. | |
2205 | * | |
2206 | * @param admin_setting $setting - The admin setting for this flag | |
2207 | * @return string - The html for the checkbox. | |
2208 | */ | |
2209 | public function output_setting_flag(admin_setting $setting) { | |
df760229 FM |
2210 | global $OUTPUT; |
2211 | ||
3c88a678 | 2212 | $value = $setting->get_setting_flag_value($this); |
df760229 FM |
2213 | |
2214 | $context = new stdClass(); | |
2215 | $context->id = $setting->get_id() . '_' . $this->get_shortname(); | |
2216 | $context->name = $setting->get_full_name() . '_' . $this->get_shortname(); | |
2217 | $context->value = 1; | |
2218 | $context->checked = $value ? true : false; | |
2219 | $context->label = $this->get_displayname(); | |
2220 | ||
2221 | return $OUTPUT->render_from_template('core_admin/setting_flag', $context); | |
3c88a678 DW |
2222 | } |
2223 | } | |
2224 | ||
3e7069e7 | 2225 | |
220a90c5 | 2226 | /** |
2227 | * No setting - just heading and text. | |
db26acd4 | 2228 | * |
2229 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2230 | */ |
2231 | class admin_setting_heading extends admin_setting { | |
3e7069e7 PS |
2232 | |
2233 | /** | |
2234 | * not a setting, just text | |
2235 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2236 | * @param string $heading heading | |
2237 | * @param string $information text in box | |
2238 | */ | |
73fa96d5 | 2239 | public function __construct($name, $heading, $information) { |
427649bf | 2240 | $this->nosave = true; |
73fa96d5 | 2241 | parent::__construct($name, $heading, $information, ''); |
220a90c5 | 2242 | } |
2243 | ||
db26acd4 | 2244 | /** |
2245 | * Always returns true | |
2246 | * @return bool Always returns true | |
2247 | */ | |
73fa96d5 | 2248 | public function get_setting() { |
220a90c5 | 2249 | return true; |
2250 | } | |
2251 | ||
db26acd4 | 2252 | /** |
2253 | * Always returns true | |
2254 | * @return bool Always returns true | |
2255 | */ | |
73fa96d5 | 2256 | public function get_defaultsetting() { |
220a90c5 | 2257 | return true; |
2258 | } | |
2259 | ||
db26acd4 | 2260 | /** |
2261 | * Never write settings | |
2262 | * @return string Always returns an empty string | |
2263 | */ | |
73fa96d5 | 2264 | public function write_setting($data) { |
9baf6825 | 2265 | // do not write any setting |
220a90c5 | 2266 | return ''; |
2267 | } | |
0c079f19 | 2268 | |
db26acd4 | 2269 | /** |
2270 | * Returns an HTML string | |
2271 | * @return string Returns an HTML string | |
2272 | */ | |
73fa96d5 | 2273 | public function output_html($data, $query='') { |
3c159385 | 2274 | global $OUTPUT; |
df760229 FM |
2275 | $context = new stdClass(); |
2276 | $context->title = $this->visiblename; | |
2277 | $context->description = $this->description; | |
2278 | $context->descriptionformatted = highlight($query, markdown_to_html($this->description)); | |
2279 | return $OUTPUT->render_from_template('core_admin/setting_heading', $context); | |
220a90c5 | 2280 | } |
2281 | } | |
6e4dc10f | 2282 | |
f765fb38 AA |
2283 | /** |
2284 | * No setting - just name and description in same row. | |
2285 | * | |
2286 | * @copyright 2018 onwards Amaia Anabitarte | |
2287 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2288 | */ | |
2289 | class admin_setting_description extends admin_setting { | |
2290 | ||
2291 | /** | |
2292 | * Not a setting, just text | |
2293 | * | |
2294 | * @param string $name | |
2295 | * @param string $visiblename | |
2296 | * @param string $description | |
2297 | */ | |
2298 | public function __construct($name, $visiblename, $description) { | |
2299 | $this->nosave = true; | |
2300 | parent::__construct($name, $visiblename, $description, ''); | |
2301 | } | |
2302 | ||
2303 | /** | |
2304 | * Always returns true | |
2305 | * | |
2306 | * @return bool Always returns true | |
2307 | */ | |
2308 | public function get_setting() { | |
2309 | return true; | |
2310 | } | |
2311 | ||
2312 | /** | |
2313 | * Always returns true | |
2314 | * | |
2315 | * @return bool Always returns true | |
2316 | */ | |
2317 | public function get_defaultsetting() { | |
2318 | return true; | |
2319 | } | |
2320 | ||
2321 | /** | |
2322 | * Never write settings | |
2323 | * | |
2324 | * @param mixed $data Gets converted to str for comparison against yes value | |
2325 | * @return string Always returns an empty string | |
2326 | */ | |
2327 | public function write_setting($data) { | |
2328 | // Do not write any setting. | |
2329 | return ''; | |
2330 | } | |
2331 | ||
2332 | /** | |
2333 | * Returns an HTML string | |
2334 | * | |
2335 | * @param string $data | |
2336 | * @param string $query | |
2337 | * @return string Returns an HTML string | |
2338 | */ | |
2339 | public function output_html($data, $query='') { | |
2340 | global $OUTPUT; | |
2341 | ||
2342 | $context = new stdClass(); | |
2343 | $context->title = $this->visiblename; | |
2344 | $context->description = $this->description; | |
2345 | ||
2346 | return $OUTPUT->render_from_template('core_admin/setting_description', $context); | |
2347 | } | |
2348 | } | |
2349 | ||
2350 | ||
3e7069e7 | 2351 | |
220a90c5 | 2352 | /** |
113efed5 FM |
2353 | * The most flexible setting, the user enters text. |
2354 | * | |
2355 | * This type of field should be used for config settings which are using | |
2356 | * English words and are not localised (passwords, database name, list of values, ...). | |
2357 | * | |
db26acd4 | 2358 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
220a90c5 | 2359 | */ |
6e4dc10f | 2360 | class admin_setting_configtext extends admin_setting { |
2361 | ||
3e7069e7 | 2362 | /** @var mixed int means PARAM_XXX type, string is a allowed format in regex */ |
73fa96d5 | 2363 | public $paramtype; |
0c079f19 | 2364 | /** @var int default field size */ |
73fa96d5 | 2365 | public $size; |
6e4dc10f | 2366 | |
220a90c5 | 2367 | /** |
875e5f07 | 2368 | * Config text constructor |
db26acd4 | 2369 | * |
1a41e806 | 2370 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 2371 | * @param string $visiblename localised |
2372 | * @param string $description long localised info | |
2373 | * @param string $defaultsetting | |
2374 | * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex | |
f7633b0f | 2375 | * @param int $size default field size |
220a90c5 | 2376 | */ |
73fa96d5 | 2377 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) { |
6e4dc10f | 2378 | $this->paramtype = $paramtype; |
f7633b0f | 2379 | if (!is_null($size)) { |
2380 | $this->size = $size; | |
2381 | } else { | |
40ea93a4 | 2382 | $this->size = ($paramtype === PARAM_INT) ? 5 : 30; |
f7633b0f | 2383 | } |
73fa96d5 | 2384 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
6e4dc10f | 2385 | } |
2386 | ||
8a40bc36 FM |
2387 | /** |
2388 | * Get whether this should be displayed in LTR mode. | |
2389 | * | |
2390 | * Try to guess from the PARAM type unless specifically set. | |
2391 | */ | |
2392 | public function get_force_ltr() { | |
2393 | $forceltr = parent::get_force_ltr(); | |
2394 | if ($forceltr === null) { | |
2395 | return !is_rtl_compatible($this->paramtype); | |
2396 | } | |
2397 | return $forceltr; | |
2398 | } | |
2399 | ||
db26acd4 | 2400 | /** |
2401 | * Return the setting | |
2402 | * | |
875e5f07 | 2403 | * @return mixed returns config if successful else null |
db26acd4 | 2404 | */ |
73fa96d5 | 2405 | public function get_setting() { |
220a90c5 | 2406 | return $this->config_read($this->name); |
6e4dc10f | 2407 | } |
eef868d1 | 2408 | |
73fa96d5 | 2409 | public function write_setting($data) { |
8cad6cca | 2410 | if ($this->paramtype === PARAM_INT and $data === '') { |
9baf6825 | 2411 | // do not complain if '' used instead of 0 |
8cad6cca | 2412 | $data = 0; |
2413 | } | |
220a90c5 | 2414 | // $data is a string |
c5d2d0dd | 2415 | $validated = $this->validate($data); |
e33fbf87 | 2416 | if ($validated !== true) { |
2417 | return $validated; | |
c235598d | 2418 | } |
220a90c5 | 2419 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2420 | } |
2421 | ||
e33fbf87 | 2422 | /** |
2423 | * Validate data before storage | |
2424 | * @param string data | |
2425 | * @return mixed true if ok string if error found | |
2426 | */ | |
73fa96d5 | 2427 | public function validate($data) { |
58aaa8e9 | 2428 | // allow paramtype to be a custom regex if it is the form of /pattern/ |
2429 | if (preg_match('#^/.*/$#', $this->paramtype)) { | |
e33fbf87 | 2430 | if (preg_match($this->paramtype, $data)) { |
2431 | return true; | |
2432 | } else { | |
2433 | return get_string('validateerror', 'admin'); | |
2434 | } | |
2435 | ||
9e24fbd1 | 2436 | } else if ($this->paramtype === PARAM_RAW) { |
4ea56b3f | 2437 | return true; |
9baf6825 | 2438 | |
4ea56b3f | 2439 | } else { |
2440 | $cleaned = clean_param($data, $this->paramtype); | |
40ea93a4 | 2441 | if ("$data" === "$cleaned") { // implicit conversion to string is needed to do exact comparison |
4ea56b3f | 2442 | return true; |
e33fbf87 | 2443 | } else { |
4ea56b3f | 2444 | return get_string('validateerror', 'admin'); |
e33fbf87 | 2445 | } |
4ea56b3f | 2446 | } |
c235598d | 2447 | } |
2448 | ||
db26acd4 | 2449 | /** |
2450 | * Return an XHTML string for the setting | |
2451 | * @return string Returns an XHTML string | |
2452 | */ | |
73fa96d5 | 2453 | public function output_html($data, $query='') { |
df760229 FM |
2454 | global $OUTPUT; |
2455 | ||
220a90c5 | 2456 | $default = $this->get_defaultsetting(); |
df760229 FM |
2457 | $context = (object) [ |
2458 | 'size' => $this->size, | |
2459 | 'id' => $this->get_id(), | |
2460 | 'name' => $this->get_full_name(), | |
113efed5 | 2461 | 'value' => $data, |
8a40bc36 | 2462 | 'forceltr' => $this->get_force_ltr(), |
df760229 FM |
2463 | ]; |
2464 | $element = $OUTPUT->render_from_template('core_admin/setting_configtext', $context); | |
220a90c5 | 2465 | |
df760229 | 2466 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query); |
6e4dc10f | 2467 | } |
6e4dc10f | 2468 | } |
2469 | ||
2639cdf0 AA |
2470 | /** |
2471 | * Text input with a maximum length constraint. | |
2472 | * | |
2473 | * @copyright 2015 onwards Ankit Agarwal | |
2474 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2475 | */ | |
2476 | class admin_setting_configtext_with_maxlength extends admin_setting_configtext { | |
2477 | ||
2478 | /** @var int maximum number of chars allowed. */ | |
2479 | protected $maxlength; | |
2480 | ||
2481 | /** | |
2482 | * Config text constructor | |
2483 | * | |
2484 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, | |
2485 | * or 'myplugin/mysetting' for ones in config_plugins. | |
2486 | * @param string $visiblename localised | |
2487 | * @param string $description long localised info | |
2488 | * @param string $defaultsetting | |
2489 | * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex | |
2490 | * @param int $size default field size | |
2491 | * @param mixed $maxlength int maxlength allowed, 0 for infinite. | |
2492 | */ | |
2493 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, | |
2494 | $size=null, $maxlength = 0) { | |
2495 | $this->maxlength = $maxlength; | |
2496 | parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $size); | |
2497 | } | |
2498 | ||
2499 | /** | |
2500 | * Validate data before storage | |
2501 | * | |
2502 | * @param string $data data | |
2503 | * @return mixed true if ok string if error found | |
2504 | */ | |
2505 | public function validate($data) { | |
2506 | $parentvalidation = parent::validate($data); | |
2507 | if ($parentvalidation === true) { | |
2508 | if ($this->maxlength > 0) { | |
2509 | // Max length check. | |
2510 | $length = core_text::strlen($data); | |
2511 | if ($length > $this->maxlength) { | |
2512 | return get_string('maximumchars', 'moodle', $this->maxlength); | |
2513 | } | |
2514 | return true; | |
2515 | } else { | |
2516 | return true; // No max length check needed. | |
2517 | } | |
2518 | } else { | |
2519 | return $parentvalidation; | |
2520 | } | |
2521 | } | |
2522 | } | |
3e7069e7 | 2523 | |
220a90c5 | 2524 | /** |
2525 | * General text area without html editor. | |
db26acd4 | 2526 | * |
2527 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2528 | */ |
2529 | class admin_setting_configtextarea extends admin_setting_configtext { | |
4fe2250a | 2530 | private $rows; |
2531 | private $cols; | |
eba8cd63 | 2532 | |
db26acd4 | 2533 | /** |
2534 | * @param string $name | |
2535 | * @param string $visiblename | |
2536 | * @param string $description | |
2537 | * @param mixed $defaultsetting string or array | |
2538 | * @param mixed $paramtype | |
2539 | * @param string $cols The number of columns to make the editor | |
2540 | * @param string $rows The number of rows to make the editor | |
2541 | */ | |
73fa96d5 | 2542 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { |
220a90c5 | 2543 | $this->rows = $rows; |
2544 | $this->cols = $cols; | |
73fa96d5 | 2545 | parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype); |
eba8cd63 | 2546 | } |
3e7069e7 | 2547 | |
db26acd4 | 2548 | /** |
2549 | * Returns an XHTML string for the editor | |
2550 | * | |
2551 | * @param string $data | |
2552 | * @param string $query | |
2553 | * @return string XHTML string for the editor | |
2554 | */ | |
73fa96d5 | 2555 | public function output_html($data, $query='') { |
df760229 | 2556 | global $OUTPUT; |
220a90c5 | 2557 | |
df760229 | 2558 | $default = $this->get_defaultsetting(); |
587c7040 | 2559 | $defaultinfo = $default; |
2560 | if (!is_null($default) and $default !== '') { | |
2561 | $defaultinfo = "\n".$default; | |
c5d2d0dd | 2562 | } |
220a90c5 | 2563 | |
df760229 FM |
2564 | $context = (object) [ |
2565 | 'cols' => $this->cols, | |
2566 | 'rows' => $this->rows, | |
2567 | 'id' => $this->get_id(), | |
2568 | 'name' => $this->get_full_name(), | |
113efed5 | 2569 | 'value' => $data, |
8a40bc36 | 2570 | 'forceltr' => $this->get_force_ltr(), |
df760229 FM |
2571 | ]; |
2572 | $element = $OUTPUT->render_from_template('core_admin/setting_configtextarea', $context); | |
2573 | ||
2574 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query); | |
4fe2250a | 2575 | } |
2576 | } | |
2577 | ||
2578 | /** | |
2579 | * General text area with html editor. | |
2580 | */ | |
8a40bc36 | 2581 | class admin_setting_confightmleditor extends admin_setting_configtextarea { |
0c079f19 | 2582 | |
4fe2250a | 2583 | /** |
2584 | * @param string $name | |
2585 | * @param string $visiblename | |
2586 | * @param string $description | |
2587 | * @param mixed $defaultsetting string or array | |
2588 | * @param mixed $paramtype | |
2589 | */ | |
2590 | public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') { | |
df760229 | 2591 | parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $cols, $rows); |
8a40bc36 | 2592 | $this->set_force_ltr(false); |
ff5fe311 | 2593 | editors_head_setup(); |
4fe2250a | 2594 | } |
3e7069e7 | 2595 | |
4fe2250a | 2596 | /** |
2597 | * Returns an XHTML string for the editor | |
2598 | * | |
2599 | * @param string $data | |
2600 | * @param string $query | |
2601 | * @return string XHTML string for the editor | |
2602 | */ | |
2603 | public function output_html($data, $query='') { | |
20e5da7d | 2604 | $editor = editors_get_preferred_editor(FORMAT_HTML); |
988592c5 | 2605 | $editor->set_text($data); |
69429650 | 2606 | $editor->use_editor($this->get_id(), array('noclean'=>true)); |
df760229 | 2607 | return parent::output_html($data, $query); |
220a90c5 | 2608 | } |
0c187454 MM |
2609 | |
2610 | /** | |
2611 | * Checks if data has empty html. | |
2612 | * | |
2613 | * @param string $data | |
2614 | * @return string Empty when no errors. | |
2615 | */ | |
2616 | public function write_setting($data) { | |
2617 | if (trim(html_to_text($data)) === '') { | |
2618 | $data = ''; | |
2619 | } | |
2620 | return parent::write_setting($data); | |
2621 | } | |
220a90c5 | 2622 | } |
2623 | ||
3e7069e7 | 2624 | |
220a90c5 | 2625 | /** |
2626 | * Password field, allows unmasking of password | |
db26acd4 | 2627 | * |
2628 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2629 | */ |
2630 | class admin_setting_configpasswordunmask extends admin_setting_configtext { | |
113efed5 | 2631 | |
3e7069e7 PS |
2632 | /** |
2633 | * Constructor | |
2634 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2635 | * @param string $visiblename localised | |
2636 | * @param string $description long localised info | |
2637 | * @param string $defaultsetting default password | |
2638 | */ | |
73fa96d5 | 2639 | public function __construct($name, $visiblename, $description, $defaultsetting) { |
2640 | parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 30); | |
220a90c5 | 2641 | } |
0c079f19 | 2642 | |
914499a3 PS |
2643 | /** |
2644 | * Log config changes if necessary. | |
2645 | * @param string $name | |
2646 | * @param string $oldvalue | |
2647 | * @param string $value | |
2648 | */ | |
2649 | protected function add_to_config_log($name, $oldvalue, $value) { | |
2650 | if ($value !== '') { | |
2651 | $value = '********'; | |
2652 | } | |
2653 | if ($oldvalue !== '' and $oldvalue !== null) { | |
2654 | $oldvalue = '********'; | |
2655 | } | |
2656 | parent::add_to_config_log($name, $oldvalue, $value); | |
2657 | } | |
2658 | ||
db26acd4 | 2659 | /** |
ca25005c | 2660 | * Returns HTML for the field. |
db26acd4 | 2661 | * |
ca25005c AN |
2662 | * @param string $data Value for the field |
2663 | * @param string $query Passed as final argument for format_admin_setting | |
2664 | * @return string Rendered HTML | |
db26acd4 | 2665 | */ |
73fa96d5 | 2666 | public function output_html($data, $query='') { |
6ed86eca A |
2667 | global $OUTPUT, $CFG; |
2668 | $forced = false; | |
2669 | if (empty($this->plugin)) { | |
2670 | if (array_key_exists($this->name, $CFG->config_php_settings)) { | |
2671 | $forced = true; | |
2672 | } | |
2673 | } else { | |
2674 | if (array_key_exists($this->plugin, $CFG->forced_plugin_settings) | |
2675 | and array_key_exists($this->name, $CFG->forced_plugin_settings[$this->plugin])) { | |
2676 | $forced = true; | |
2677 | } | |
2678 | } | |
df760229 FM |
2679 | $context = (object) [ |
2680 | 'id' => $this->get_id(), | |
2681 | 'name' => $this->get_full_name(), | |
2682 | 'size' => $this->size, | |
6ed86eca | 2683 | 'value' => $forced ? null : $data, |
8a40bc36 | 2684 | 'forceltr' => $this->get_force_ltr(), |
6ed86eca | 2685 | 'forced' => $forced |
df760229 FM |
2686 | ]; |
2687 | $element = $OUTPUT->render_from_template('core_admin/setting_configpasswordunmask', $context); | |
2688 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', null, $query); | |
220a90c5 | 2689 | } |
1cd62f2b | 2690 | } |
113efed5 | 2691 | |
1cd62f2b PH |
2692 | /** |
2693 | * Password field, allows unmasking of password, with an advanced checkbox that controls an additional $name.'_adv' setting. | |
2694 | * | |
2695 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2696 | * @copyright 2018 Paul Holden (pholden@greenhead.ac.uk) | |
2697 | */ | |
2698 | class admin_setting_configpasswordunmask_with_advanced extends admin_setting_configpasswordunmask { | |
2699 | ||
2700 | /** | |
2701 | * Constructor | |
2702 | * | |
2703 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2704 | * @param string $visiblename localised | |
2705 | * @param string $description long localised info | |
2706 | * @param array $defaultsetting ('value'=>string, 'adv'=>bool) | |
2707 | */ | |
2708 | public function __construct($name, $visiblename, $description, $defaultsetting) { | |
2709 | parent::__construct($name, $visiblename, $description, $defaultsetting['value']); | |
2710 | $this->set_advanced_flag_options(admin_setting_flag::ENABLED, !empty($defaultsetting['adv'])); | |
2711 | } | |
220a90c5 | 2712 | } |
2713 | ||
c7cd8d9c DW |
2714 | /** |
2715 | * Empty setting used to allow flags (advanced) on settings that can have no sensible default. | |
2716 | * Note: Only advanced makes sense right now - locked does not. | |
2717 | * | |
2718 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
2719 | */ | |
2720 | class admin_setting_configempty extends admin_setting_configtext { | |
2721 | ||
2722 | /** | |
2723 | * @param string $name | |
2724 | * @param string $visiblename | |
2725 | * @param string $description | |
2726 | */ | |
2727 | public function __construct($name, $visiblename, $description) { | |
2728 | parent::__construct($name, $visiblename, $description, '', PARAM_RAW); | |
2729 | } | |
2730 | ||
2731 | /** | |
2732 | * Returns an XHTML string for the hidden field | |
2733 | * | |
2734 | * @param string $data | |
2735 | * @param string $query | |
2736 | * @return string XHTML string for the editor | |
2737 | */ | |
2738 | public function output_html($data, $query='') { | |
df760229 FM |
2739 | global $OUTPUT; |
2740 | ||
2741 | $context = (object) [ | |
2742 | 'id' => $this->get_id(), | |
2743 | 'name' => $this->get_full_name() | |
2744 | ]; | |
2745 | $element = $OUTPUT->render_from_template('core_admin/setting_configempty', $context); | |
2746 | ||
2747 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', get_string('none'), $query); | |
c7cd8d9c DW |
2748 | } |
2749 | } | |
2750 | ||
3e7069e7 | 2751 | |
220a90c5 | 2752 | /** |
e9c0fa35 | 2753 | * Path to directory |
db26acd4 | 2754 | * |
2755 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2756 | */ |
e9c0fa35 | 2757 | class admin_setting_configfile extends admin_setting_configtext { |
3e7069e7 PS |
2758 | /** |
2759 | * Constructor | |
2760 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. | |
2761 | * @param string $visiblename localised | |
2762 | * @param string $description long localised info | |
2763 | * @param string $defaultdirectory default directory location | |
2764 | */ | |
73fa96d5 | 2765 | public function __construct($name, $visiblename, $description, $defaultdirectory) { |
2766 | parent::__construct($name, $visiblename, $description, $defaultdirectory, PARAM_RAW, 50); | |
220a90c5 | 2767 | } |
2768 | ||
db26acd4 | 2769 | /** |
2770 | * Returns XHTML for the field | |
0c079f19 | 2771 | * |
db26acd4 | 2772 | * Returns XHTML for the field and also checks whether the file |
2773 | * specified in $data exists using file_exists() | |
2774 | * | |
2775 | * @param string $data File name and path to use in value attr | |
2776 | * @param string $query | |
2777 | * @return string XHTML field | |
2778 | */ | |
73fa96d5 | 2779 | public function output_html($data, $query='') { |
df760229 FM |
2780 | global $CFG, $OUTPUT; |
2781 | ||
220a90c5 | 2782 | $default = $this->get_defaultsetting(); |
df760229 FM |
2783 | $context = (object) [ |
2784 | 'id' => $this->get_id(), | |
2785 | 'name' => $this->get_full_name(), | |
2786 | 'size' => $this->size, | |
2787 | 'value' => $data, | |
2788 | 'showvalidity' => !empty($data), | |
2789 | 'valid' => $data && file_exists($data), | |
113efed5 | 2790 | 'readonly' => !empty($CFG->preventexecpath), |
8a40bc36 | 2791 | 'forceltr' => $this->get_force_ltr(), |
df760229 | 2792 | ]; |
220a90c5 | 2793 | |
df760229 | 2794 | if ($context->readonly) { |
579ca95f | 2795 | $this->visiblename .= '<div class="alert alert-info">'.get_string('execpathnotallowed', 'admin').'</div>'; |
5aaac8b2 | 2796 | } |
220a90c5 | 2797 | |
df760229 FM |
2798 | $element = $OUTPUT->render_from_template('core_admin/setting_configfile', $context); |
2799 | ||
2800 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query); | |
eba8cd63 | 2801 | } |
41186f4f | 2802 | |
9a2b5e0b | 2803 | /** |
41186f4f | 2804 | * Checks if execpatch has been disabled in config.php |
9a2b5e0b HD |
2805 | */ |
2806 | public function write_setting($data) { | |
2807 | global $CFG; | |
2808 | if (!empty($CFG->preventexecpath)) { | |
41186f4f PS |
2809 | if ($this->get_setting() === null) { |
2810 | // Use default during installation. | |
2811 | $data = $this->get_defaultsetting(); | |
2812 | if ($data === null) { | |
2813 | $data = ''; | |
2814 | } | |
2815 | } else { | |
2816 | return ''; | |
2817 | } | |
9a2b5e0b HD |
2818 | } |
2819 | return parent::write_setting($data); | |
2820 | } | |
113efed5 | 2821 | |
220a90c5 | 2822 | } |
2823 | ||
3e7069e7 | 2824 | |
220a90c5 | 2825 | /** |
e9c0fa35 | 2826 | * Path to executable file |
db26acd4 | 2827 | * |
2828 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2829 | */ |
e9c0fa35 | 2830 | class admin_setting_configexecutable extends admin_setting_configfile { |
2831 | ||
3e7069e7 PS |
2832 | /** |
2833 | * Returns an XHTML field | |
2834 | * | |
2835 | * @param string $data This is the value for the field | |
2836 | * @param string $query | |
2837 | * @return string XHTML field | |
2838 | */ | |
73fa96d5 | 2839 | public function output_html($data, $query='') { |
df760229 | 2840 | global $CFG, $OUTPUT; |
e9c0fa35 | 2841 | $default = $this->get_defaultsetting(); |
bc219382 | 2842 | require_once("$CFG->libdir/filelib.php"); |
e9c0fa35 | 2843 | |
df760229 FM |
2844 | $context = (object) [ |
2845 | 'id' => $this->get_id(), | |
2846 | 'name' => $this->get_full_name(), | |
2847 | 'size' => $this->size, | |
2848 | 'value' => $data, | |
2849 | 'showvalidity' => !empty($data), | |
2850 | 'valid' => $data && file_exists($data) && !is_dir($data) && file_is_executable($data), | |
113efed5 | 2851 | 'readonly' => !empty($CFG->preventexecpath), |
8a40bc36 | 2852 | 'forceltr' => $this->get_force_ltr() |
df760229 FM |
2853 | ]; |
2854 | ||
9a2b5e0b | 2855 | if (!empty($CFG->preventexecpath)) { |
579ca95f | 2856 | $this->visiblename .= '<div class="alert alert-info">'.get_string('execpathnotallowed', 'admin').'</div>'; |
9a2b5e0b | 2857 | } |
e9c0fa35 | 2858 | |
df760229 FM |
2859 | $element = $OUTPUT->render_from_template('core_admin/setting_configexecutable', $context); |
2860 | ||
2861 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query); | |
220a90c5 | 2862 | } |
e9c0fa35 | 2863 | } |
220a90c5 | 2864 | |
3e7069e7 | 2865 | |
e9c0fa35 | 2866 | /** |
2867 | * Path to directory | |
db26acd4 | 2868 | * |
2869 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
e9c0fa35 | 2870 | */ |
2871 | class admin_setting_configdirectory extends admin_setting_configfile { | |
db26acd4 | 2872 | |
3e7069e7 PS |
2873 | /** |
2874 | * Returns an XHTML field | |
2875 | * | |
2876 | * @param string $data This is the value for the field | |
2877 | * @param string $query | |
2878 | * @return string XHTML | |
2879 | */ | |
73fa96d5 | 2880 | public function output_html($data, $query='') { |
df760229 | 2881 | global $CFG, $OUTPUT; |
220a90c5 | 2882 | $default = $this->get_defaultsetting(); |
2883 | ||
df760229 FM |
2884 | $context = (object) [ |
2885 | 'id' => $this->get_id(), | |
2886 | 'name' => $this->get_full_name(), | |
2887 | 'size' => $this->size, | |
2888 | 'value' => $data, | |
2889 | 'showvalidity' => !empty($data), | |
2890 | 'valid' => $data && file_exists($data) && is_dir($data), | |
113efed5 | 2891 | 'readonly' => !empty($CFG->preventexecpath), |
8a40bc36 | 2892 | 'forceltr' => $this->get_force_ltr() |
df760229 FM |
2893 | ]; |
2894 | ||
5aaac8b2 | 2895 | if (!empty($CFG->preventexecpath)) { |
579ca95f | 2896 | $this->visiblename .= '<div class="alert alert-info">'.get_string('execpathnotallowed', 'admin').'</div>'; |
5aaac8b2 | 2897 | } |
9ba38673 | 2898 | |
113efed5 | 2899 | $element = $OUTPUT->render_from_template('core_admin/setting_configdirectory', $context); |
df760229 FM |
2900 | |
2901 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query); | |
220a90c5 | 2902 | } |
eba8cd63 | 2903 | } |
2904 | ||
3e7069e7 | 2905 | |
220a90c5 | 2906 | /** |
2907 | * Checkbox | |
db26acd4 | 2908 | * |
2909 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2910 | */ |
6e4dc10f | 2911 | class admin_setting_configcheckbox extends admin_setting { |
3e7069e7 | 2912 | /** @var string Value used when checked */ |
73fa96d5 | 2913 | public $yes; |
0c079f19 | 2914 | /** @var string Value used when not checked */ |
73fa96d5 | 2915 | public $no; |
6e4dc10f | 2916 | |
220a90c5 | 2917 | /** |
2918 | * Constructor | |
1a41e806 | 2919 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 2920 | * @param string $visiblename localised |
2921 | * @param string $description long localised info | |
2922 | * @param string $defaultsetting | |
2923 | * @param string $yes value used when checked | |
2924 | * @param string $no value used when not checked | |
2925 | */ | |
73fa96d5 | 2926 | public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') { |
2927 | parent::__construct($name, $visiblename, $description, $defaultsetting); | |
220a90c5 | 2928 | $this->yes = (string)$yes; |
2929 | $this->no = (string)$no; | |
6e4dc10f | 2930 | } |
2931 | ||
db26acd4 | 2932 | /** |
2933 | * Retrieves the current setting using the objects name | |
2934 | * | |
2935 | * @return string | |
2936 | */ | |
73fa96d5 | 2937 | public function get_setting() { |
220a90c5 | 2938 | return $this->config_read($this->name); |
6e4dc10f | 2939 | } |
eef868d1 | 2940 | |
db26acd4 | 2941 | /** |
2942 | * Sets the value for the setting | |
2943 | * | |
2944 | * Sets the value for the setting to either the yes or no values | |
2945 | * of the object by comparing $data to yes | |
2946 | * | |
2947 | * @param mixed $data Gets converted to str for comparison against yes value | |
2948 | * @return string empty string or error | |
2949 | */ | |
73fa96d5 | 2950 | public function write_setting($data) { |
220a90c5 | 2951 | if ((string)$data === $this->yes) { // convert to strings before comparison |
2952 | $data = $this->yes; | |
6e4dc10f | 2953 | } else { |
220a90c5 | 2954 | $data = $this->no; |
6e4dc10f | 2955 | } |
220a90c5 | 2956 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 2957 | } |
2958 | ||
db26acd4 | 2959 | /** |
2960 | * Returns an XHTML checkbox field | |
2961 | * | |
2962 | * @param string $data If $data matches yes then checkbox is checked | |
2963 | * @param string $query | |
2964 | * @return string XHTML field | |
2965 | */ | |
73fa96d5 | 2966 | public function output_html($data, $query='') { |
df760229 FM |
2967 | global $OUTPUT; |
2968 | ||
2969 | $context = (object) [ | |
2970 | 'id' => $this->get_id(), | |
2971 | 'name' => $this->get_full_name(), | |
2972 | 'no' => $this->no, | |
2973 | 'value' => $this->yes, | |
2974 | 'checked' => (string) $data === $this->yes, | |
2975 | ]; | |
220a90c5 | 2976 | |
df760229 | 2977 | $default = $this->get_defaultsetting(); |
220a90c5 | 2978 | if (!is_null($default)) { |
2979 | if ((string)$default === $this->yes) { | |
587c7040 | 2980 | $defaultinfo = get_string('checkboxyes', 'admin'); |
220a90c5 | 2981 | } else { |
587c7040 | 2982 | $defaultinfo = get_string('checkboxno', 'admin'); |
220a90c5 | 2983 | } |
c8218a42 | 2984 | } else { |
587c7040 | 2985 | $defaultinfo = NULL; |
c8218a42 | 2986 | } |
220a90c5 | 2987 | |
df760229 | 2988 | $element = $OUTPUT->render_from_template('core_admin/setting_configcheckbox', $context); |
220a90c5 | 2989 | |
df760229 | 2990 | return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query); |
6e4dc10f | 2991 | } |
6e4dc10f | 2992 | } |
2993 | ||
3e7069e7 | 2994 | |
220a90c5 | 2995 | /** |
2996 | * Multiple checkboxes, each represents different value, stored in csv format | |
db26acd4 | 2997 | * |
2998 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 2999 | */ |
3000 | class admin_setting_configmulticheckbox extends admin_setting { | |
3e7069e7 | 3001 | /** @var array Array of choices value=>label */ |
73fa96d5 | 3002 | public $choices; |
eef868d1 | 3003 | |
220a90c5 | 3004 | /** |
db26acd4 | 3005 | * Constructor: uses parent::__construct |
3006 | * | |
1a41e806 | 3007 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 3008 | * @param string $visiblename localised |
3009 | * @param string $description long localised info | |
3010 | * @param array $defaultsetting array of selected | |
3011 | * @param array $choices array of $value=>$label for each checkbox | |
3012 | */ | |
73fa96d5 | 3013 | public function __construct($name, $visiblename, $description, $defaultsetting, $choices) { |
6e4dc10f | 3014 | $this->choices = $choices; |
73fa96d5 | 3015 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
6e4dc10f | 3016 | } |
3017 | ||
0a784551 | 3018 | /** |
73fa96d5 | 3019 | * This public function may be used in ancestors for lazy loading of choices |
db26acd4 | 3020 | * |
3021 | * @todo Check if this function is still required content commented out only returns true | |
3022 | * @return bool true if loaded, false if error | |
0a784551 | 3023 | */ |
73fa96d5 | 3024 | public function load_choices() { |
0a784551 | 3025 | /* |
220a90c5 | 3026 | if (is_array($this->choices)) { |
3027 | return true; | |
0a784551 | 3028 | } |
3029 | .... load choices here | |
3030 | */ | |
220a90c5 | 3031 | return true; |
3032 | } | |
3033 | ||
3034 | /** | |
3035 | * Is setting related to query text - used when searching | |
db26acd4 | 3036 | * |
220a90c5 | 3037 | * @param string $query |
db26acd4 | 3038 | * @return bool true on related, false on not or failure |
220a90c5 | 3039 | */ |
73fa96d5 | 3040 | public function is_related($query) { |
220a90c5 | 3041 | if (!$this->load_choices() or empty($this->choices)) { |
3042 | return false; | |
3043 | } | |
3044 | if (parent::is_related($query)) { | |
3045 | return true; | |
3046 | } | |
3047 | ||
220a90c5 | 3048 | foreach ($this->choices as $desc) { |
2f1e464a | 3049 | if (strpos(core_text::strtolower($desc), $query) !== false) { |
220a90c5 | 3050 | return true; |
3051 | } | |
3052 | } | |
3053 | return false; | |
0a784551 | 3054 | } |
3055 | ||
db26acd4 | 3056 | /** |
3057 | * Returns the current setting if it is set | |
3058 | * | |
3059 | * @return mixed null if null, else an array | |
3060 | */ | |
73fa96d5 | 3061 | public function get_setting() { |
220a90c5 | 3062 | $result = $this->config_read($this->name); |
10f19c49 | 3063 | |
220a90c5 | 3064 | if (is_null($result)) { |
3065 | return NULL; | |
3066 | } | |
3067 | if ($result === '') { | |
3068 | return array(); | |
3069 | } | |
10f19c49 | 3070 | $enabled = explode(',', $result); |
3071 | $setting = array(); | |
3072 | foreach ($enabled as $option) { | |
3073 | $setting[$option] = 1; | |
3074 | } | |
3075 | return $setting; | |
6e4dc10f | 3076 | } |
eef868d1 | 3077 | |
db26acd4 | 3078 | /** |
3079 | * Saves the setting(s) provided in $data | |
3080 | * | |
3081 | * @param array $data An array of data, if not array returns empty str | |
3082 | * @return mixed empty string on useless data or bool true=success, false=failed | |
3083 | */ | |
73fa96d5 | 3084 | public function write_setting($data) { |
220a90c5 | 3085 | if (!is_array($data)) { |
3086 | return ''; // ignore it | |
3087 | } | |
3088 | if (!$this->load_choices() or empty($this->choices)) { | |
3089 | return ''; | |
3090 | } | |
3091 | unset($data['xxxxx']); | |
3092 | $result = array(); | |
3093 | foreach ($data as $key => $value) { | |
3094 | if ($value and array_key_exists($key, $this->choices)) { | |
3095 | $result[] = $key; | |
3096 | } | |
3097 | } | |
3098 | return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin'); | |
6e4dc10f | 3099 | } |
0c079f19 | 3100 | |
db26acd4 | 3101 | /** |
3102 | * Returns XHTML field(s) as required by choices | |
3103 | * | |
3104 | * Relies on data being an array should data ever be another valid vartype with | |
3105 | * acceptable value this may cause a warning/error | |
3106 | * if (!is_array($data)) would fix the problem | |
3107 | * | |
3108 | * @todo Add vartype handling to ensure $data is an array | |
3109 | * | |
3110 | * @param array $data An array of checked values | |
3111 | * @param string $query | |
3112 | * @return string XHTML field | |
3113 | */ | |
73fa96d5 | 3114 | public function output_html($data, $query='') { |
df760229 FM |
3115 | global $OUTPUT; |
3116 | ||
220a90c5 | 3117 | if (!$this->load_choices() or empty($this->choices)) { |
3118 | return ''; | |
3119 | } | |
df760229 | 3120 | |
220a90c5 | 3121 | $default = $this->get_defaultsetting(); |
3122 | if (is_null($default)) { | |
3123 | $default = array(); | |
3124 | } | |
3125 | if (is_null($data)) { | |
775f811a | 3126 | $data = array(); |
220a90c5 | 3127 | } |
df760229 FM |
3128 | |
3129 | $context = (object) [ | |
3130 | 'id' => $this->get_id(), | |
3131 | 'name' => $this->get_full_name(), | |
3132 | ]; | |
3133 | ||
220a90c5 | 3134 | $options = array(); |
3135 | $defaults = array(); | |
df760229 | 3136 | foreach ($this->choices as $key => $description) { |
10f19c49 | 3137 | if (!empty($default[$key])) { |
220a90c5 | 3138 | $defaults[] = $description; |
3139 | } | |
3140 | ||
df760229 FM |
3141 | $options[] = [ |
3142 | 'key' => $key, | |
3143 | 'checked' => !empty($data[$key]), | |
3144 | 'label' => highlightfast($query, $description) | |
3145 | ]; | |
220a90c5 | 3146 | } |
3147 | ||
587c7040 | 3148 | if (is_null($default)) { |
df760229 | 3149 | $defaultinfo = null; |
587c7040 | 3150 | } else if (!empty($defaults)) { |
df760229 FM |
3151 | $defaultinfo = implode(', ', $defaults); |
3152 | } else { | |
3153 | $defaultinfo = get_string('none'); | |
6e4dc10f | 3154 | } |
6153cf58 | 3155 | |
df760229 FM |
3156 | $context->options = $options; |
3157 | $context->hasoptions = !empty($options); | |
3158 | ||
3159 | $element = $OUTPUT->render_from_template('core_admin/setting_configmulticheckbox', $context); | |
3160 | ||
3161 | return format_admin_setting($this, $this->visiblename, $element, $this->description, false, '', $defaultinfo, $query); | |
c5d2d0dd | 3162 | |
6e4dc10f | 3163 | } |
6e4dc10f | 3164 | } |
3165 | ||
3e7069e7 | 3166 | |
220a90c5 | 3167 | /** |
3168 | * Multiple checkboxes 2, value stored as string 00101011 | |
db26acd4 | 3169 | * |
3170 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 3171 | */ |
3172 | class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox { | |
db26acd4 | 3173 | |
3e7069e7 PS |
3174 | /** |
3175 | * Returns the setting if set | |
3176 | * | |
3177 | * @return mixed null if not set, else an array of set settings | |
3178 | */ | |
73fa96d5 | 3179 | public function get_setting() { |
220a90c5 | 3180 | $result = $this->config_read($this->name); |
3181 | if (is_null($result)) { | |
3182 | return NULL; | |
3183 | } | |
3184 | if (!$this->load_choices()) { | |
3185 | return NULL; | |
3186 | } | |
3187 | $result = str_pad($result, count($this->choices), '0'); | |
3188 | $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY); | |
3189 | $setting = array(); | |
3190 | foreach ($this->choices as $key=>$unused) { | |
3191 | $value = array_shift($result); | |
3192 | if ($value) { | |
10f19c49 | 3193 | $setting[$key] = 1; |
220a90c5 | 3194 | } |
3195 | } | |
3196 | return $setting; | |
3197 | } | |
6e4dc10f | 3198 | |
db26acd4 | 3199 | /** |
3200 | * Save setting(s) provided in $data param | |
3201 | * | |
3202 | * @param array $data An array of settings to save | |
3203 | * @return mixed empty string for bad data or bool true=>success, false=>error | |
3204 | */ | |
73fa96d5 | 3205 | public function write_setting($data) { |
220a90c5 | 3206 | if (!is_array($data)) { |
3207 | return ''; // ignore it | |
3208 | } | |
3209 | if (!$this->load_choices() or empty($this->choices)) { | |
3210 | return ''; | |
3211 | } | |
3212 | $result = ''; | |
3213 | foreach ($this->choices as $key=>$unused) { | |
3214 | if (!empty($data[$key])) { | |
3215 | $result .= '1'; | |
3216 | } else { | |
3217 | $result .= '0'; | |
3218 | } | |
3219 | } | |
3220 | return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin'); | |
3221 | } | |
3222 | } | |
3223 | ||
3e7069e7 | 3224 | |
220a90c5 | 3225 | /** |
3226 | * Select one value from list | |
db26acd4 | 3227 | * |
3228 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
220a90c5 | 3229 | */ |
3230 | class admin_setting_configselect extends admin_setting { | |
3e7069e7 | 3231 | /** @var array Array of choices value=>label */ |
73fa96d5 | 3232 | public $choices; |
25905de5 JL |
3233 | /** @var array Array of choices grouped using optgroups */ |
3234 | public $optgroups; | |
f87597aa | 3235 | /** @var callable|null Loader function for choices */ |
3236 | protected $choiceloader = null; | |
b5800959 | 3237 | /** @var callable|null Validation function */ |
3238 | protected $validatefunction = null; | |
6e4dc10f | 3239 | |
220a90c5 | 3240 | /** |
f87597aa | 3241 | * Constructor. |
3242 | * | |
3243 | * If you want to lazy-load the choices, pass a callback function that returns a choice | |
3244 | * array for the $choices parameter. | |
3245 | * | |
1a41e806 | 3246 | * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. |
220a90c5 | 3247 | * @param string $visiblename localised |
3248 | * @param string $description long localised info | |
eab8ed9f | 3249 | * @param string|int $defaultsetting |
f87597aa | 3250 | * @param array|callable|null $choices array of $value=>$label for each selection, or callback |
220a90c5 | 3251 | */ |
73fa96d5 | 3252 | public function __construct($name, $visiblename, $description, $defaultsetting, $choices) { |
25905de5 JL |
3253 | // Look for optgroup and single options. |
3254 | if (is_array($choices)) { | |
a15e5073 | 3255 | $this->choices = []; |
25905de5 JL |
3256 | foreach ($choices as $key => $val) { |
3257 | if (is_array($val)) { | |
3258 | $this->optgroups[$key] = $val; | |
3259 | $this->choices = array_merge($this->choices, $val); | |
3260 | } else { | |
3261 | $this->choices[$key] = $val; | |
3262 | } | |
3263 | } | |
3264 | } | |
f87597aa | 3265 | if (is_callable($choices)) { |
3266 | $this->choiceloader = $choices; | |
3267 | } | |
25905de5 | 3268 | |
73fa96d5 | 3269 | parent::__construct($name, $visiblename, $description, $defaultsetting); |
220a90c5 | 3270 | } |
3271 | ||
b5800959 | 3272 | /** |
3273 | * Sets a validate function. | |
3274 | * | |
3275 | * The callback will be passed one parameter, the new setting value, and should return either | |
3276 | * an empty string '' if the value is OK, or an error message if not. | |
3277 | * | |
3278 | * @param callable|null $validatefunction Validate function or null to clear | |
709b46db | 3279 | * @since Moodle 3.10 |
b5800959 | 3280 | */ |
3281 | public function set_validate_function(?callable $validatefunction = null) { | |
3282 | $this->validatefunction = $validatefunction; | |
3283 | } | |
3284 | ||
220a90c5 | 3285 | /** |
3286 | * This function may be used in ancestors for lazy loading of choices | |
db26acd4 | 3287 | * |
0c079f19 | 3288 | * Override this method if loading of choices is expensive, such |
3289 | * as when it requires multiple db requests. | |
3290 | * | |
db26acd4 | 3291 | * @return bool true if loaded, false if error |
220a90c5 | 3292 | */ |
73fa96d5 | 3293 | public function load_choices() { |
f87597aa | 3294 | if ($this->choiceloader) { |
3295 | if (!is_array($this->choices)) { | |
3296 | $this->choices = call_user_func($this->choiceloader); | |
3297 | } | |
220a90c5 | 3298 | return true; |
6e4dc10f | 3299 | } |
220a90c5 | 3300 | return true; |
6e4dc10f | 3301 | } |
3302 | ||
db26acd4 | 3303 | /** |
3304 | * Check if this is $query is related to a choice | |
3305 | * | |
3306 | * @param string $query | |
3307 | * @return bool true if related, false if not | |
3308 | */ | |
73fa96d5 | 3309 | public function is_related($query) { |
407d8134 | 3310 | if (parent::is_related($query)) { |
3311 | return true; | |
3312 | } | |
3313 | if (!$this->load_choices()) { | |
3314 | return false; | |
3315 | } | |
407d8134 | 3316 | foreach ($this->choices as $key=>$value) { |
2f1e464a | 3317 | if (strpos(core_text::strtolower($key), $query) !== false) { |
407d8134 | 3318 | return true; |
3319 | } | |
2f1e464a | 3320 | if (strpos(core_text::strtolower($value), $query) !== false) { |
407d8134 | 3321 | return true; |
3322 | } | |
c5d2d0dd | 3323 | } |
407d8134 | 3324 | return false; |
3325 | } | |
3326 | ||
db26acd4 | 3327 | /** |
3328 | * Return the setting | |
0c079f19 | 3329 | * |
875e5f07 | 3330 | * @return mixed returns config if successful else null |
db26acd4 | 3331 | */ |
73fa96d5 | 3332 | public function get_setting() { |
220a90c5 | 3333 | return $this->config_read($this->name); |
6e4dc10f | 3334 | } |
eef868d1 | 3335 | |
db26acd4 | 3336 | /** |
3337 | * Save a setting | |
3338 | * | |
3339 | * @param string $data | |
3340 | * @return string empty of error string | |
3341 | */ | |
73fa96d5 | 3342 | public function write_setting($data) { |
220a90c5 | 3343 | if (!$this->load_choices() or empty($this->choices)) { |
3344 | return ''; | |
3345 | } | |
3346 | if (!array_key_exists($data, $this->choices)) { | |
3347 | return ''; // ignore it | |
3348 | } | |
eef868d1 | 3349 | |
b5800959 | 3350 | // Validate the new setting. |
3351 | $error = $this->validate_setting($data); | |
3352 | if ($error) { | |
3353 | return $error; | |
3354 | } | |
3355 | ||
220a90c5 | 3356 | return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin')); |
6e4dc10f | 3357 | } |
eef868d1 | 3358 | |
b5800959 | 3359 | /** |
3360 | * Validate the setting. This uses the callback function if provided; subclasses could override | |
3361 | * to carry out validation directly in the class. | |
3362 | * | |
3363 | * @param string $data New value being set | |
3364 | * @return string Empty string if valid, or error message text | |
709b46db | 3365 | * @since Moodle 3.10 |
b5800959 | 3366 | */ |
3367 | protected function validate_setting(string $data): string { | |
3368 | // If validation function is specified, call it now. | |
3369 | if ($this->validatefunction) { | |
3370 | return call_user_func($this->validatefunction, $data); | |
3371 | } else { | |
3372 | return ''; | |
3373 | } | |
3374 | } | |
3375 | ||
e2249afe | 3376 | /** |
db26acd4 | 3377 | * Returns XHTML select field |
3378 | * | |
3379 | * Ensure the options are loaded, and generate the XHTML for the select | |
e2249afe | 3380 | * element and any warning message. Separating this out from output_html |
3381 | * makes it easier to subclass this class. | |
3382 | * | |
3383 | * @param string $data the option to show as selected. | |
3384 | * @param string $current the currently selected option in the database, null if none. | |
3385 | * @param string $default the default selected option. | |