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