2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Functions and classes used during installation, upgrades and for admin settings.
20 * ADMIN SETTINGS TREE INTRODUCTION
22 * This file performs the following tasks:
23 * -it defines the necessary objects and interfaces to build the Moodle
25 * -it defines the admin_externalpage_setup()
27 * ADMIN_SETTING OBJECTS
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.
33 * ADMIN_SETTINGPAGE OBJECTS
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.
39 * ADMIN_EXTERNALPAGE OBJECTS
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.
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
52 * $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
53 * $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
56 * Next, in foo.php, your file structure would resemble the following:
58 * require(__DIR__.'/../../config.php');
59 * require_once($CFG->libdir.'/adminlib.php');
60 * admin_externalpage_setup('foo');
61 * // functionality like processing form submissions goes here
62 * echo $OUTPUT->header();
63 * // your HTML goes here
64 * echo $OUTPUT->footer();
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.
69 * It also configures all $PAGE properties needed for navigation.
71 * ADMIN_CATEGORY OBJECTS
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.
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.
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.
89 * Please note that the $this->name field of any part_of_admin_tree must be
90 * UNIQUE throughout the ENTIRE admin tree.
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
96 * Original author: Vincenzo K. Marcovecchio
97 * Maintainer: Petr Skoda
101 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
102 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
105 defined('MOODLE_INTERNAL') || die();
108 require_once($CFG->libdir.'/ddllib.php');
109 require_once($CFG->libdir.'/xmlize.php');
110 require_once($CFG->libdir.'/messagelib.php');
112 define('INSECURE_DATAROOT_WARNING', 1);
113 define('INSECURE_DATAROOT_ERROR', 2);
116 * Automatically clean-up all plugin data and remove the plugin DB tables
118 * NOTE: do not call directly, use new /admin/plugins.php?uninstall=component instead!
120 * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc.
121 * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc.
122 * @uses global $OUTPUT to produce notices and other messages
125 function uninstall_plugin($type, $name) {
126 global $CFG, $DB, $OUTPUT;
128 // This may take a long time.
129 core_php_time_limit::raise();
131 // Recursively uninstall all subplugins first.
132 $subplugintypes = core_component::get_plugin_types_with_subplugins();
133 if (isset($subplugintypes[$type])) {
134 $base = core_component::get_plugin_directory($type, $name);
135 if (file_exists("$base/db/subplugins.php")) {
136 $subplugins = array();
137 include("$base/db/subplugins.php");
138 foreach ($subplugins as $subplugintype=>$dir) {
139 $instances = core_component::get_plugin_list($subplugintype);
140 foreach ($instances as $subpluginname => $notusedpluginpath) {
141 uninstall_plugin($subplugintype, $subpluginname);
148 $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum'
150 if ($type === 'mod') {
151 $pluginname = $name; // eg. 'forum'
152 if (get_string_manager()->string_exists('modulename', $component)) {
153 $strpluginname = get_string('modulename', $component);
155 $strpluginname = $component;
159 $pluginname = $component;
160 if (get_string_manager()->string_exists('pluginname', $component)) {
161 $strpluginname = get_string('pluginname', $component);
163 $strpluginname = $component;
167 echo $OUTPUT->heading($pluginname);
169 // Delete all tag areas, collections and instances associated with this plugin.
170 core_tag_area::uninstall($component);
172 // Custom plugin uninstall.
173 $plugindirectory = core_component::get_plugin_directory($type, $name);
174 $uninstalllib = $plugindirectory . '/db/uninstall.php';
175 if (file_exists($uninstalllib)) {
176 require_once($uninstalllib);
177 $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()'
178 if (function_exists($uninstallfunction)) {
179 // Do not verify result, let plugin complain if necessary.
180 $uninstallfunction();
184 // Specific plugin type cleanup.
185 $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
187 $plugininfo->uninstall_cleanup();
188 core_plugin_manager::reset_caches();
192 // perform clean-up task common for all the plugin/subplugin types
194 //delete the web service functions and pre-built services
195 require_once($CFG->dirroot.'/lib/externallib.php');
196 external_delete_descriptions($component);
198 // delete calendar events
199 $DB->delete_records('event', array('modulename' => $pluginname));
201 // Delete scheduled tasks.
202 $DB->delete_records('task_scheduled', array('component' => $component));
204 // Delete Inbound Message datakeys.
205 $DB->delete_records_select('messageinbound_datakeys',
206 'handler IN (SELECT id FROM {messageinbound_handlers} WHERE component = ?)', array($component));
208 // Delete Inbound Message handlers.
209 $DB->delete_records('messageinbound_handlers', array('component' => $component));
211 // delete all the logs
212 $DB->delete_records('log', array('module' => $pluginname));
214 // delete log_display information
215 $DB->delete_records('log_display', array('component' => $component));
217 // delete the module configuration records
218 unset_all_config_for_plugin($component);
219 if ($type === 'mod') {
220 unset_all_config_for_plugin($pluginname);
223 // delete message provider
224 message_provider_uninstall($component);
226 // delete the plugin tables
227 $xmldbfilepath = $plugindirectory . '/db/install.xml';
228 drop_plugin_tables($component, $xmldbfilepath, false);
229 if ($type === 'mod' or $type === 'block') {
230 // non-frankenstyle table prefixes
231 drop_plugin_tables($name, $xmldbfilepath, false);
234 // delete the capabilities that were defined by this module
235 capabilities_cleanup($component);
237 // remove event handlers and dequeue pending events
238 events_uninstall($component);
240 // Delete all remaining files in the filepool owned by the component.
241 $fs = get_file_storage();
242 $fs->delete_component_files($component);
244 // Finally purge all caches.
247 // Invalidate the hash used for upgrade detections.
248 set_config('allversionshash', '');
250 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
254 * Returns the version of installed component
256 * @param string $component component name
257 * @param string $source either 'disk' or 'installed' - where to get the version information from
258 * @return string|bool version number or false if the component is not found
260 function get_component_version($component, $source='installed') {
263 list($type, $name) = core_component::normalize_component($component);
265 // moodle core or a core subsystem
266 if ($type === 'core') {
267 if ($source === 'installed') {
268 if (empty($CFG->version)) {
271 return $CFG->version;
274 if (!is_readable($CFG->dirroot.'/version.php')) {
277 $version = null; //initialize variable for IDEs
278 include($CFG->dirroot.'/version.php');
285 if ($type === 'mod') {
286 if ($source === 'installed') {
287 if ($CFG->version < 2013092001.02) {
288 return $DB->get_field('modules', 'version', array('name'=>$name));
290 return get_config('mod_'.$name, 'version');
294 $mods = core_component::get_plugin_list('mod');
295 if (empty($mods[$name]) or !is_readable($mods[$name].'/version.php')) {
298 $plugin = new stdClass();
299 $plugin->version = null;
301 include($mods[$name].'/version.php');
302 return $plugin->version;
308 if ($type === 'block') {
309 if ($source === 'installed') {
310 if ($CFG->version < 2013092001.02) {
311 return $DB->get_field('block', 'version', array('name'=>$name));
313 return get_config('block_'.$name, 'version');
316 $blocks = core_component::get_plugin_list('block');
317 if (empty($blocks[$name]) or !is_readable($blocks[$name].'/version.php')) {
320 $plugin = new stdclass();
321 include($blocks[$name].'/version.php');
322 return $plugin->version;
327 // all other plugin types
328 if ($source === 'installed') {
329 return get_config($type.'_'.$name, 'version');
331 $plugins = core_component::get_plugin_list($type);
332 if (empty($plugins[$name])) {
335 $plugin = new stdclass();
336 include($plugins[$name].'/version.php');
337 return $plugin->version;
343 * Delete all plugin tables
345 * @param string $name Name of plugin, used as table prefix
346 * @param string $file Path to install.xml file
347 * @param bool $feedback defaults to true
348 * @return bool Always returns true
350 function drop_plugin_tables($name, $file, $feedback=true) {
353 // first try normal delete
354 if (file_exists($file) and $DB->get_manager()->delete_tables_from_xmldb_file($file)) {
358 // then try to find all tables that start with name and are not in any xml file
359 $used_tables = get_used_table_names();
361 $tables = $DB->get_tables();
363 /// Iterate over, fixing id fields as necessary
364 foreach ($tables as $table) {
365 if (in_array($table, $used_tables)) {
369 if (strpos($table, $name) !== 0) {
373 // found orphan table --> delete it
374 if ($DB->get_manager()->table_exists($table)) {
375 $xmldb_table = new xmldb_table($table);
376 $DB->get_manager()->drop_table($xmldb_table);
384 * Returns names of all known tables == tables that moodle knows about.
386 * @return array Array of lowercase table names
388 function get_used_table_names() {
389 $table_names = array();
390 $dbdirs = get_db_directories();
392 foreach ($dbdirs as $dbdir) {
393 $file = $dbdir.'/install.xml';
395 $xmldb_file = new xmldb_file($file);
397 if (!$xmldb_file->fileExists()) {
401 $loaded = $xmldb_file->loadXMLStructure();
402 $structure = $xmldb_file->getStructure();
404 if ($loaded and $tables = $structure->getTables()) {
405 foreach($tables as $table) {
406 $table_names[] = strtolower($table->getName());
415 * Returns list of all directories where we expect install.xml files
416 * @return array Array of paths
418 function get_db_directories() {
423 /// First, the main one (lib/db)
424 $dbdirs[] = $CFG->libdir.'/db';
426 /// Then, all the ones defined by core_component::get_plugin_types()
427 $plugintypes = core_component::get_plugin_types();
428 foreach ($plugintypes as $plugintype => $pluginbasedir) {
429 if ($plugins = core_component::get_plugin_list($plugintype)) {
430 foreach ($plugins as $plugin => $plugindir) {
431 $dbdirs[] = $plugindir.'/db';
440 * Try to obtain or release the cron lock.
441 * @param string $name name of lock
442 * @param int $until timestamp when this lock considered stale, null means remove lock unconditionally
443 * @param bool $ignorecurrent ignore current lock state, usually extend previous lock, defaults to false
444 * @return bool true if lock obtained
446 function set_cron_lock($name, $until, $ignorecurrent=false) {
449 debugging("Tried to get a cron lock for a null fieldname");
453 // remove lock by force == remove from config table
454 if (is_null($until)) {
455 set_config($name, null);
459 if (!$ignorecurrent) {
460 // read value from db - other processes might have changed it
461 $value = $DB->get_field('config', 'value', array('name'=>$name));
463 if ($value and $value > time()) {
469 set_config($name, $until);
474 * Test if and critical warnings are present
477 function admin_critical_warnings_present() {
480 if (!has_capability('moodle/site:config', context_system::instance())) {
484 if (!isset($SESSION->admin_critical_warning)) {
485 $SESSION->admin_critical_warning = 0;
486 if (is_dataroot_insecure(true) === INSECURE_DATAROOT_ERROR) {
487 $SESSION->admin_critical_warning = 1;
491 return $SESSION->admin_critical_warning;
495 * Detects if float supports at least 10 decimal digits
497 * Detects if float supports at least 10 decimal digits
498 * and also if float-->string conversion works as expected.
500 * @return bool true if problem found
502 function is_float_problem() {
503 $num1 = 2009010200.01;
504 $num2 = 2009010200.02;
506 return ((string)$num1 === (string)$num2 or $num1 === $num2 or $num2 <= (string)$num1);
510 * Try to verify that dataroot is not accessible from web.
512 * Try to verify that dataroot is not accessible from web.
513 * It is not 100% correct but might help to reduce number of vulnerable sites.
514 * Protection from httpd.conf and .htaccess is not detected properly.
516 * @uses INSECURE_DATAROOT_WARNING
517 * @uses INSECURE_DATAROOT_ERROR
518 * @param bool $fetchtest try to test public access by fetching file, default false
519 * @return mixed empty means secure, INSECURE_DATAROOT_ERROR found a critical problem, INSECURE_DATAROOT_WARNING might be problematic
521 function is_dataroot_insecure($fetchtest=false) {
524 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
526 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
527 $rp = strrev(trim($rp, '/'));
528 $rp = explode('/', $rp);
530 if (strpos($siteroot, '/'.$r.'/') === 0) {
531 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
533 break; // probably alias root
537 $siteroot = strrev($siteroot);
538 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
540 if (strpos($dataroot, $siteroot) !== 0) {
545 return INSECURE_DATAROOT_WARNING;
548 // now try all methods to fetch a test file using http protocol
550 $httpdocroot = str_replace('\\', '/', strrev($CFG->dirroot.'/'));
551 preg_match('|(https?://[^/]+)|i', $CFG->wwwroot, $matches);
552 $httpdocroot = $matches[1];
553 $datarooturl = $httpdocroot.'/'. substr($dataroot, strlen($siteroot));
554 make_upload_directory('diag');
555 $testfile = $CFG->dataroot.'/diag/public.txt';
556 if (!file_exists($testfile)) {
557 file_put_contents($testfile, 'test file, do not delete');
558 @chmod($testfile, $CFG->filepermissions);
560 $teststr = trim(file_get_contents($testfile));
561 if (empty($teststr)) {
563 return INSECURE_DATAROOT_WARNING;
566 $testurl = $datarooturl.'/diag/public.txt';
567 if (extension_loaded('curl') and
568 !(stripos(ini_get('disable_functions'), 'curl_init') !== FALSE) and
569 !(stripos(ini_get('disable_functions'), 'curl_setop') !== FALSE) and
570 ($ch = @curl_init($testurl)) !== false) {
571 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
572 curl_setopt($ch, CURLOPT_HEADER, false);
573 $data = curl_exec($ch);
574 if (!curl_errno($ch)) {
576 if ($data === $teststr) {
578 return INSECURE_DATAROOT_ERROR;
584 if ($data = @file_get_contents($testurl)) {
586 if ($data === $teststr) {
587 return INSECURE_DATAROOT_ERROR;
591 preg_match('|https?://([^/]+)|i', $testurl, $matches);
592 $sitename = $matches[1];
594 if ($fp = @fsockopen($sitename, 80, $error)) {
595 preg_match('|https?://[^/]+(.*)|i', $testurl, $matches);
596 $localurl = $matches[1];
597 $out = "GET $localurl HTTP/1.1\r\n";
598 $out .= "Host: $sitename\r\n";
599 $out .= "Connection: Close\r\n\r\n";
605 $data .= fgets($fp, 1024);
606 } else if (@fgets($fp, 1024) === "\r\n") {
612 if ($data === $teststr) {
613 return INSECURE_DATAROOT_ERROR;
617 return INSECURE_DATAROOT_WARNING;
621 * Enables CLI maintenance mode by creating new dataroot/climaintenance.html file.
623 function enable_cli_maintenance_mode() {
626 if (file_exists("$CFG->dataroot/climaintenance.html")) {
627 unlink("$CFG->dataroot/climaintenance.html");
630 if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) {
631 $data = $CFG->maintenance_message;
632 $data = bootstrap_renderer::early_error_content($data, null, null, null);
633 $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);
635 } else if (file_exists("$CFG->dataroot/climaintenance.template.html")) {
636 $data = file_get_contents("$CFG->dataroot/climaintenance.template.html");
639 $data = get_string('sitemaintenance', 'admin');
640 $data = bootstrap_renderer::early_error_content($data, null, null, null);
641 $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);
644 file_put_contents("$CFG->dataroot/climaintenance.html", $data);
645 chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions);
648 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
652 * Interface for anything appearing in the admin tree
654 * The interface that is implemented by anything that appears in the admin tree
655 * block. It forces inheriting classes to define a method for checking user permissions
656 * and methods for finding something in the admin tree.
658 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
660 interface part_of_admin_tree {
663 * Finds a named part_of_admin_tree.
665 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
666 * and not parentable_part_of_admin_tree, then this function should only check if
667 * $this->name matches $name. If it does, it should return a reference to $this,
668 * otherwise, it should return a reference to NULL.
670 * If a class inherits parentable_part_of_admin_tree, this method should be called
671 * recursively on all child objects (assuming, of course, the parent object's name
672 * doesn't match the search criterion).
674 * @param string $name The internal name of the part_of_admin_tree we're searching for.
675 * @return mixed An object reference or a NULL reference.
677 public function locate($name);
680 * Removes named part_of_admin_tree.
682 * @param string $name The internal name of the part_of_admin_tree we want to remove.
683 * @return bool success.
685 public function prune($name);
689 * @param string $query
690 * @return mixed array-object structure of found settings and pages
692 public function search($query);
695 * Verifies current user's access to this part_of_admin_tree.
697 * Used to check if the current user has access to this part of the admin tree or
698 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
699 * then this method is usually just a call to has_capability() in the site context.
701 * If a class inherits parentable_part_of_admin_tree, this method should return the
702 * logical OR of the return of check_access() on all child objects.
704 * @return bool True if the user has access, false if she doesn't.
706 public function check_access();
709 * Mostly useful for removing of some parts of the tree in admin tree block.
711 * @return True is hidden from normal list view
713 public function is_hidden();
716 * Show we display Save button at the page bottom?
719 public function show_save();
724 * Interface implemented by any part_of_admin_tree that has children.
726 * The interface implemented by any part_of_admin_tree that can be a parent
727 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
728 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
729 * include an add method for adding other part_of_admin_tree objects as children.
731 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
733 interface parentable_part_of_admin_tree extends part_of_admin_tree {
736 * Adds a part_of_admin_tree object to the admin tree.
738 * Used to add a part_of_admin_tree object to this object or a child of this
739 * object. $something should only be added if $destinationname matches
740 * $this->name. If it doesn't, add should be called on child objects that are
741 * also parentable_part_of_admin_tree's.
743 * $something should be appended as the last child in the $destinationname. If the
744 * $beforesibling is specified, $something should be prepended to it. If the given
745 * sibling is not found, $something should be appended to the end of $destinationname
746 * and a developer debugging message should be displayed.
748 * @param string $destinationname The internal name of the new parent for $something.
749 * @param part_of_admin_tree $something The object to be added.
750 * @return bool True on success, false on failure.
752 public function add($destinationname, $something, $beforesibling = null);
758 * The object used to represent folders (a.k.a. categories) in the admin tree block.
760 * Each admin_category object contains a number of part_of_admin_tree objects.
762 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
764 class admin_category implements parentable_part_of_admin_tree {
766 /** @var part_of_admin_tree[] An array of part_of_admin_tree objects that are this object's children */
768 /** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */
770 /** @var string The displayed name for this category. Usually obtained through get_string() */
772 /** @var bool Should this category be hidden in admin tree block? */
774 /** @var mixed Either a string or an array or strings */
776 /** @var mixed Either a string or an array or strings */
779 /** @var array fast lookup category cache, all categories of one tree point to one cache */
780 protected $category_cache;
782 /** @var bool If set to true children will be sorted when calling {@link admin_category::get_children()} */
783 protected $sort = false;
784 /** @var bool If set to true children will be sorted in ascending order. */
785 protected $sortasc = true;
786 /** @var bool If set to true sub categories and pages will be split and then sorted.. */
787 protected $sortsplit = true;
788 /** @var bool $sorted True if the children have been sorted and don't need resorting */
789 protected $sorted = false;
792 * Constructor for an empty admin category
794 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
795 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
796 * @param bool $hidden hide category in admin tree block, defaults to false
798 public function __construct($name, $visiblename, $hidden=false) {
799 $this->children = array();
801 $this->visiblename = $visiblename;
802 $this->hidden = $hidden;
806 * Returns a reference to the part_of_admin_tree object with internal name $name.
808 * @param string $name The internal name of the object we want.
809 * @param bool $findpath initialize path and visiblepath arrays
810 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
813 public function locate($name, $findpath=false) {
814 if (!isset($this->category_cache[$this->name])) {
815 // somebody much have purged the cache
816 $this->category_cache[$this->name] = $this;
819 if ($this->name == $name) {
821 $this->visiblepath[] = $this->visiblename;
822 $this->path[] = $this->name;
827 // quick category lookup
828 if (!$findpath and isset($this->category_cache[$name])) {
829 return $this->category_cache[$name];
833 foreach($this->children as $childid=>$unused) {
834 if ($return = $this->children[$childid]->locate($name, $findpath)) {
839 if (!is_null($return) and $findpath) {
840 $return->visiblepath[] = $this->visiblename;
841 $return->path[] = $this->name;
850 * @param string query
851 * @return mixed array-object structure of found settings and pages
853 public function search($query) {
855 foreach ($this->get_children() as $child) {
856 $subsearch = $child->search($query);
857 if (!is_array($subsearch)) {
858 debugging('Incorrect search result from '.$child->name);
861 $result = array_merge($result, $subsearch);
867 * Removes part_of_admin_tree object with internal name $name.
869 * @param string $name The internal name of the object we want to remove.
870 * @return bool success
872 public function prune($name) {
874 if ($this->name == $name) {
875 return false; //can not remove itself
878 foreach($this->children as $precedence => $child) {
879 if ($child->name == $name) {
880 // clear cache and delete self
881 while($this->category_cache) {
882 // delete the cache, but keep the original array address
883 array_pop($this->category_cache);
885 unset($this->children[$precedence]);
887 } else if ($this->children[$precedence]->prune($name)) {
895 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
897 * By default the new part of the tree is appended as the last child of the parent. You
898 * can specify a sibling node that the new part should be prepended to. If the given
899 * sibling is not found, the part is appended to the end (as it would be by default) and
900 * a developer debugging message is displayed.
902 * @throws coding_exception if the $beforesibling is empty string or is not string at all.
903 * @param string $destinationame The internal name of the immediate parent that we want for $something.
904 * @param mixed $something A part_of_admin_tree or setting instance to be added.
905 * @param string $beforesibling The name of the parent's child the $something should be prepended to.
906 * @return bool True if successfully added, false if $something can not be added.
908 public function add($parentname, $something, $beforesibling = null) {
911 $parent = $this->locate($parentname);
912 if (is_null($parent)) {
913 debugging('parent does not exist!');
917 if ($something instanceof part_of_admin_tree) {
918 if (!($parent instanceof parentable_part_of_admin_tree)) {
919 debugging('error - parts of tree can be inserted only into parentable parts');
922 if ($CFG->debugdeveloper && !is_null($this->locate($something->name))) {
923 // The name of the node is already used, simply warn the developer that this should not happen.
924 // It is intentional to check for the debug level before performing the check.
925 debugging('Duplicate admin page name: ' . $something->name, DEBUG_DEVELOPER);
927 if (is_null($beforesibling)) {
928 // Append $something as the parent's last child.
929 $parent->children[] = $something;
931 if (!is_string($beforesibling) or trim($beforesibling) === '') {
932 throw new coding_exception('Unexpected value of the beforesibling parameter');
934 // Try to find the position of the sibling.
935 $siblingposition = null;
936 foreach ($parent->children as $childposition => $child) {
937 if ($child->name === $beforesibling) {
938 $siblingposition = $childposition;
942 if (is_null($siblingposition)) {
943 debugging('Sibling '.$beforesibling.' not found', DEBUG_DEVELOPER);
944 $parent->children[] = $something;
946 $parent->children = array_merge(
947 array_slice($parent->children, 0, $siblingposition),
949 array_slice($parent->children, $siblingposition)
953 if ($something instanceof admin_category) {
954 if (isset($this->category_cache[$something->name])) {
955 debugging('Duplicate admin category name: '.$something->name);
957 $this->category_cache[$something->name] = $something;
958 $something->category_cache =& $this->category_cache;
959 foreach ($something->children as $child) {
960 // just in case somebody already added subcategories
961 if ($child instanceof admin_category) {
962 if (isset($this->category_cache[$child->name])) {
963 debugging('Duplicate admin category name: '.$child->name);
965 $this->category_cache[$child->name] = $child;
966 $child->category_cache =& $this->category_cache;
975 debugging('error - can not add this element');
982 * Checks if the user has access to anything in this category.
984 * @return bool True if the user has access to at least one child in this category, false otherwise.
986 public function check_access() {
987 foreach ($this->children as $child) {
988 if ($child->check_access()) {
996 * Is this category hidden in admin tree block?
998 * @return bool True if hidden
1000 public function is_hidden() {
1001 return $this->hidden;
1005 * Show we display Save button at the page bottom?
1008 public function show_save() {
1009 foreach ($this->children as $child) {
1010 if ($child->show_save()) {
1018 * Sets sorting on this category.
1020 * Please note this function doesn't actually do the sorting.
1021 * It can be called anytime.
1022 * Sorting occurs when the user calls get_children.
1023 * Code using the children array directly won't see the sorted results.
1025 * @param bool $sort If set to true children will be sorted, if false they won't be.
1026 * @param bool $asc If true sorting will be ascending, otherwise descending.
1027 * @param bool $split If true we sort pages and sub categories separately.
1029 public function set_sorting($sort, $asc = true, $split = true) {
1030 $this->sort = (bool)$sort;
1031 $this->sortasc = (bool)$asc;
1032 $this->sortsplit = (bool)$split;
1036 * Returns the children associated with this category.
1038 * @return part_of_admin_tree[]
1040 public function get_children() {
1041 // If we should sort and it hasn't already been sorted.
1042 if ($this->sort && !$this->sorted) {
1043 if ($this->sortsplit) {
1044 $categories = array();
1046 foreach ($this->children as $child) {
1047 if ($child instanceof admin_category) {
1048 $categories[] = $child;
1053 core_collator::asort_objects_by_property($categories, 'visiblename');
1054 core_collator::asort_objects_by_property($pages, 'visiblename');
1055 if (!$this->sortasc) {
1056 $categories = array_reverse($categories);
1057 $pages = array_reverse($pages);
1059 $this->children = array_merge($pages, $categories);
1061 core_collator::asort_objects_by_property($this->children, 'visiblename');
1062 if (!$this->sortasc) {
1063 $this->children = array_reverse($this->children);
1066 $this->sorted = true;
1068 return $this->children;
1072 * Magically gets a property from this object.
1075 * @return part_of_admin_tree[]
1076 * @throws coding_exception
1078 public function __get($property) {
1079 if ($property === 'children') {
1080 return $this->get_children();
1082 throw new coding_exception('Invalid property requested.');
1086 * Magically sets a property against this object.
1088 * @param string $property
1089 * @param mixed $value
1090 * @throws coding_exception
1092 public function __set($property, $value) {
1093 if ($property === 'children') {
1094 $this->sorted = false;
1095 $this->children = $value;
1097 throw new coding_exception('Invalid property requested.');
1102 * Checks if an inaccessible property is set.
1104 * @param string $property
1106 * @throws coding_exception
1108 public function __isset($property) {
1109 if ($property === 'children') {
1110 return isset($this->children);
1112 throw new coding_exception('Invalid property requested.');
1118 * Root of admin settings tree, does not have any parent.
1120 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1122 class admin_root extends admin_category {
1123 /** @var array List of errors */
1125 /** @var string search query */
1127 /** @var bool full tree flag - true means all settings required, false only pages required */
1129 /** @var bool flag indicating loaded tree */
1131 /** @var mixed site custom defaults overriding defaults in settings files*/
1132 public $custom_defaults;
1135 * @param bool $fulltree true means all settings required,
1136 * false only pages required
1138 public function __construct($fulltree) {
1141 parent::__construct('root', get_string('administration'), false);
1142 $this->errors = array();
1144 $this->fulltree = $fulltree;
1145 $this->loaded = false;
1147 $this->category_cache = array();
1149 // load custom defaults if found
1150 $this->custom_defaults = null;
1151 $defaultsfile = "$CFG->dirroot/local/defaults.php";
1152 if (is_readable($defaultsfile)) {
1153 $defaults = array();
1154 include($defaultsfile);
1155 if (is_array($defaults) and count($defaults)) {
1156 $this->custom_defaults = $defaults;
1162 * Empties children array, and sets loaded to false
1164 * @param bool $requirefulltree
1166 public function purge_children($requirefulltree) {
1167 $this->children = array();
1168 $this->fulltree = ($requirefulltree || $this->fulltree);
1169 $this->loaded = false;
1170 //break circular dependencies - this helps PHP 5.2
1171 while($this->category_cache) {
1172 array_pop($this->category_cache);
1174 $this->category_cache = array();
1180 * Links external PHP pages into the admin tree.
1182 * See detailed usage example at the top of this document (adminlib.php)
1184 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1186 class admin_externalpage implements part_of_admin_tree {
1188 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1191 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1192 public $visiblename;
1194 /** @var string The external URL that we should link to when someone requests this external page. */
1197 /** @var string The role capability/permission a user must have to access this external page. */
1198 public $req_capability;
1200 /** @var object The context in which capability/permission should be checked, default is site context. */
1203 /** @var bool hidden in admin tree block. */
1206 /** @var mixed either string or array of string */
1209 /** @var array list of visible names of page parents */
1210 public $visiblepath;
1213 * Constructor for adding an external page into the admin tree.
1215 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1216 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1217 * @param string $url The external URL that we should link to when someone requests this external page.
1218 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1219 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1220 * @param stdClass $context The context the page relates to. Not sure what happens
1221 * if you specify something other than system or front page. Defaults to system.
1223 public function __construct($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1224 $this->name = $name;
1225 $this->visiblename = $visiblename;
1227 if (is_array($req_capability)) {
1228 $this->req_capability = $req_capability;
1230 $this->req_capability = array($req_capability);
1232 $this->hidden = $hidden;
1233 $this->context = $context;
1237 * Returns a reference to the part_of_admin_tree object with internal name $name.
1239 * @param string $name The internal name of the object we want.
1240 * @param bool $findpath defaults to false
1241 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1243 public function locate($name, $findpath=false) {
1244 if ($this->name == $name) {
1246 $this->visiblepath = array($this->visiblename);
1247 $this->path = array($this->name);
1257 * This function always returns false, required function by interface
1259 * @param string $name
1262 public function prune($name) {
1267 * Search using query
1269 * @param string $query
1270 * @return mixed array-object structure of found settings and pages
1272 public function search($query) {
1274 if (strpos(strtolower($this->name), $query) !== false) {
1276 } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) {
1280 $result = new stdClass();
1281 $result->page = $this;
1282 $result->settings = array();
1283 return array($this->name => $result);
1290 * Determines if the current user has access to this external page based on $this->req_capability.
1292 * @return bool True if user has access, false otherwise.
1294 public function check_access() {
1296 $context = empty($this->context) ? context_system::instance() : $this->context;
1297 foreach($this->req_capability as $cap) {
1298 if (has_capability($cap, $context)) {
1306 * Is this external page hidden in admin tree block?
1308 * @return bool True if hidden
1310 public function is_hidden() {
1311 return $this->hidden;
1315 * Show we display Save button at the page bottom?
1318 public function show_save() {
1325 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1327 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1329 class admin_settingpage implements part_of_admin_tree {
1331 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1334 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1335 public $visiblename;
1337 /** @var mixed An array of admin_setting objects that are part of this setting page. */
1340 /** @var string The role capability/permission a user must have to access this external page. */
1341 public $req_capability;
1343 /** @var object The context in which capability/permission should be checked, default is site context. */
1346 /** @var bool hidden in admin tree block. */
1349 /** @var mixed string of paths or array of strings of paths */
1352 /** @var array list of visible names of page parents */
1353 public $visiblepath;
1356 * see admin_settingpage for details of this function
1358 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1359 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1360 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1361 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1362 * @param stdClass $context The context the page relates to. Not sure what happens
1363 * if you specify something other than system or front page. Defaults to system.
1365 public function __construct($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1366 $this->settings = new stdClass();
1367 $this->name = $name;
1368 $this->visiblename = $visiblename;
1369 if (is_array($req_capability)) {
1370 $this->req_capability = $req_capability;
1372 $this->req_capability = array($req_capability);
1374 $this->hidden = $hidden;
1375 $this->context = $context;
1379 * see admin_category
1381 * @param string $name
1382 * @param bool $findpath
1383 * @return mixed Object (this) if name == this->name, else returns null
1385 public function locate($name, $findpath=false) {
1386 if ($this->name == $name) {
1388 $this->visiblepath = array($this->visiblename);
1389 $this->path = array($this->name);
1399 * Search string in settings page.
1401 * @param string $query
1404 public function search($query) {
1407 foreach ($this->settings as $setting) {
1408 if ($setting->is_related($query)) {
1409 $found[] = $setting;
1414 $result = new stdClass();
1415 $result->page = $this;
1416 $result->settings = $found;
1417 return array($this->name => $result);
1421 if (strpos(strtolower($this->name), $query) !== false) {
1423 } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) {
1427 $result = new stdClass();
1428 $result->page = $this;
1429 $result->settings = array();
1430 return array($this->name => $result);
1437 * This function always returns false, required by interface
1439 * @param string $name
1440 * @return bool Always false
1442 public function prune($name) {
1447 * adds an admin_setting to this admin_settingpage
1449 * 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
1450 * n.b. each admin_setting in an admin_settingpage must have a unique internal name
1452 * @param object $setting is the admin_setting object you want to add
1453 * @return bool true if successful, false if not
1455 public function add($setting) {
1456 if (!($setting instanceof admin_setting)) {
1457 debugging('error - not a setting instance');
1461 $name = $setting->name;
1462 if ($setting->plugin) {
1463 $name = $setting->plugin . $name;
1465 $this->settings->{$name} = $setting;
1470 * see admin_externalpage
1472 * @return bool Returns true for yes false for no
1474 public function check_access() {
1476 $context = empty($this->context) ? context_system::instance() : $this->context;
1477 foreach($this->req_capability as $cap) {
1478 if (has_capability($cap, $context)) {
1486 * outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1487 * @return string Returns an XHTML string
1489 public function output_html() {
1490 $adminroot = admin_get_root();
1491 $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n";
1492 foreach($this->settings as $setting) {
1493 $fullname = $setting->get_full_name();
1494 if (array_key_exists($fullname, $adminroot->errors)) {
1495 $data = $adminroot->errors[$fullname]->data;
1497 $data = $setting->get_setting();
1498 // do not use defaults if settings not available - upgrade settings handles the defaults!
1500 $return .= $setting->output_html($data);
1502 $return .= '</fieldset>';
1507 * Is this settings page hidden in admin tree block?
1509 * @return bool True if hidden
1511 public function is_hidden() {
1512 return $this->hidden;
1516 * Show we display Save button at the page bottom?
1519 public function show_save() {
1520 foreach($this->settings as $setting) {
1521 if (empty($setting->nosave)) {
1531 * Admin settings class. Only exists on setting pages.
1532 * Read & write happens at this level; no authentication.
1534 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1536 abstract class admin_setting {
1537 /** @var string unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. */
1539 /** @var string localised name */
1540 public $visiblename;
1541 /** @var string localised long description in Markdown format */
1542 public $description;
1543 /** @var mixed Can be string or array of string */
1544 public $defaultsetting;
1546 public $updatedcallback;
1547 /** @var mixed can be String or Null. Null means main config table */
1548 public $plugin; // null means main config table
1549 /** @var bool true indicates this setting does not actually save anything, just information */
1550 public $nosave = false;
1551 /** @var bool if set, indicates that a change to this setting requires rebuild course cache */
1552 public $affectsmodinfo = false;
1553 /** @var array of admin_setting_flag - These are extra checkboxes attached to a setting. */
1554 private $flags = array();
1558 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
1559 * or 'myplugin/mysetting' for ones in config_plugins.
1560 * @param string $visiblename localised name
1561 * @param string $description localised long description
1562 * @param mixed $defaultsetting string or array depending on implementation
1564 public function __construct($name, $visiblename, $description, $defaultsetting) {
1565 $this->parse_setting_name($name);
1566 $this->visiblename = $visiblename;
1567 $this->description = $description;
1568 $this->defaultsetting = $defaultsetting;
1572 * Generic function to add a flag to this admin setting.
1574 * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED
1575 * @param bool $default - The default for the flag
1576 * @param string $shortname - The shortname for this flag. Used as a suffix for the setting name.
1577 * @param string $displayname - The display name for this flag. Used as a label next to the checkbox.
1579 protected function set_flag_options($enabled, $default, $shortname, $displayname) {
1580 if (empty($this->flags[$shortname])) {
1581 $this->flags[$shortname] = new admin_setting_flag($enabled, $default, $shortname, $displayname);
1583 $this->flags[$shortname]->set_options($enabled, $default);
1588 * Set the enabled options flag on this admin setting.
1590 * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED
1591 * @param bool $default - The default for the flag
1593 public function set_enabled_flag_options($enabled, $default) {
1594 $this->set_flag_options($enabled, $default, 'enabled', new lang_string('enabled', 'core_admin'));
1598 * Set the advanced options flag on this admin setting.
1600 * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED
1601 * @param bool $default - The default for the flag
1603 public function set_advanced_flag_options($enabled, $default) {
1604 $this->set_flag_options($enabled, $default, 'adv', new lang_string('advanced'));
1609 * Set the locked options flag on this admin setting.
1611 * @param bool $enabled - One of self::OPTION_ENABLED or self::OPTION_DISABLED
1612 * @param bool $default - The default for the flag
1614 public function set_locked_flag_options($enabled, $default) {
1615 $this->set_flag_options($enabled, $default, 'locked', new lang_string('locked', 'core_admin'));
1619 * Get the currently saved value for a setting flag
1621 * @param admin_setting_flag $flag - One of the admin_setting_flag for this admin_setting.
1624 public function get_setting_flag_value(admin_setting_flag $flag) {
1625 $value = $this->config_read($this->name . '_' . $flag->get_shortname());
1626 if (!isset($value)) {
1627 $value = $flag->get_default();
1630 return !empty($value);
1634 * Get the list of defaults for the flags on this setting.
1636 * @param array of strings describing the defaults for this setting. This is appended to by this function.
1638 public function get_setting_flag_defaults(& $defaults) {
1639 foreach ($this->flags as $flag) {
1640 if ($flag->is_enabled() && $flag->get_default()) {
1641 $defaults[] = $flag->get_displayname();
1647 * Output the input fields for the advanced and locked flags on this setting.
1649 * @param bool $adv - The current value of the advanced flag.
1650 * @param bool $locked - The current value of the locked flag.
1651 * @return string $output - The html for the flags.
1653 public function output_setting_flags() {
1656 foreach ($this->flags as $flag) {
1657 if ($flag->is_enabled()) {
1658 $output .= $flag->output_setting_flag($this);
1662 if (!empty($output)) {
1663 return html_writer::tag('span', $output, array('class' => 'adminsettingsflags'));
1669 * Write the values of the flags for this admin setting.
1671 * @param array $data - The data submitted from the form or null to set the default value for new installs.
1672 * @return bool - true if successful.
1674 public function write_setting_flags($data) {
1676 foreach ($this->flags as $flag) {
1677 $result = $result && $flag->write_setting_flag($this, $data);
1683 * Set up $this->name and potentially $this->plugin
1685 * Set up $this->name and possibly $this->plugin based on whether $name looks
1686 * like 'settingname' or 'plugin/settingname'. Also, do some sanity checking
1687 * on the names, that is, output a developer debug warning if the name
1688 * contains anything other than [a-zA-Z0-9_]+.
1690 * @param string $name the setting name passed in to the constructor.
1692 private function parse_setting_name($name) {
1693 $bits = explode('/', $name);
1694 if (count($bits) > 2) {
1695 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1697 $this->name = array_pop($bits);
1698 if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->name)) {
1699 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1701 if (!empty($bits)) {
1702 $this->plugin = array_pop($bits);
1703 if ($this->plugin === 'moodle') {
1704 $this->plugin = null;
1705 } else if (!preg_match('/^[a-zA-Z0-9_]+$/', $this->plugin)) {
1706 throw new moodle_exception('invalidadminsettingname', '', '', $name);
1712 * Returns the fullname prefixed by the plugin
1715 public function get_full_name() {
1716 return 's_'.$this->plugin.'_'.$this->name;
1720 * Returns the ID string based on plugin and name
1723 public function get_id() {
1724 return 'id_s_'.$this->plugin.'_'.$this->name;
1728 * @param bool $affectsmodinfo If true, changes to this setting will
1729 * cause the course cache to be rebuilt
1731 public function set_affects_modinfo($affectsmodinfo) {
1732 $this->affectsmodinfo = $affectsmodinfo;
1736 * Returns the config if possible
1738 * @return mixed returns config if successful else null
1740 public function config_read($name) {
1742 if (!empty($this->plugin)) {
1743 $value = get_config($this->plugin, $name);
1744 return $value === false ? NULL : $value;
1747 if (isset($CFG->$name)) {
1756 * Used to set a config pair and log change
1758 * @param string $name
1759 * @param mixed $value Gets converted to string if not null
1760 * @return bool Write setting to config table
1762 public function config_write($name, $value) {
1763 global $DB, $USER, $CFG;
1765 if ($this->nosave) {
1769 // make sure it is a real change
1770 $oldvalue = get_config($this->plugin, $name);
1771 $oldvalue = ($oldvalue === false) ? null : $oldvalue; // normalise
1772 $value = is_null($value) ? null : (string)$value;
1774 if ($oldvalue === $value) {
1779 set_config($name, $value, $this->plugin);
1781 // Some admin settings affect course modinfo
1782 if ($this->affectsmodinfo) {
1783 // Clear course cache for all courses
1784 rebuild_course_cache(0, true);
1787 $this->add_to_config_log($name, $oldvalue, $value);
1789 return true; // BC only
1793 * Log config changes if necessary.
1794 * @param string $name
1795 * @param string $oldvalue
1796 * @param string $value
1798 protected function add_to_config_log($name, $oldvalue, $value) {
1799 add_to_config_log($name, $oldvalue, $value, $this->plugin);
1803 * Returns current value of this setting
1804 * @return mixed array or string depending on instance, NULL means not set yet
1806 public abstract function get_setting();
1809 * Returns default setting if exists
1810 * @return mixed array or string depending on instance; NULL means no default, user must supply
1812 public function get_defaultsetting() {
1813 $adminroot = admin_get_root(false, false);
1814 if (!empty($adminroot->custom_defaults)) {
1815 $plugin = is_null($this->plugin) ? 'moodle' : $this->plugin;
1816 if (isset($adminroot->custom_defaults[$plugin])) {
1817 if (array_key_exists($this->name, $adminroot->custom_defaults[$plugin])) { // null is valid value here ;-)
1818 return $adminroot->custom_defaults[$plugin][$this->name];
1822 return $this->defaultsetting;
1828 * @param mixed $data string or array, must not be NULL
1829 * @return string empty string if ok, string error message otherwise
1831 public abstract function write_setting($data);
1834 * Return part of form with setting
1835 * This function should always be overwritten
1837 * @param mixed $data array or string depending on setting
1838 * @param string $query
1841 public function output_html($data, $query='') {
1842 // should be overridden
1847 * Function called if setting updated - cleanup, cache reset, etc.
1848 * @param string $functionname Sets the function name
1851 public function set_updatedcallback($functionname) {
1852 $this->updatedcallback = $functionname;
1856 * Execute postupdatecallback if necessary.
1857 * @param mixed $original original value before write_setting()
1858 * @return bool true if changed, false if not.
1860 public function post_write_settings($original) {
1861 // Comparison must work for arrays too.
1862 if (serialize($original) === serialize($this->get_setting())) {
1866 $callbackfunction = $this->updatedcallback;
1867 if (!empty($callbackfunction) and function_exists($callbackfunction)) {
1868 $callbackfunction($this->get_full_name());
1874 * Is setting related to query text - used when searching
1875 * @param string $query
1878 public function is_related($query) {
1879 if (strpos(strtolower($this->name), $query) !== false) {
1882 if (strpos(core_text::strtolower($this->visiblename), $query) !== false) {
1885 if (strpos(core_text::strtolower($this->description), $query) !== false) {
1888 $current = $this->get_setting();
1889 if (!is_null($current)) {
1890 if (is_string($current)) {
1891 if (strpos(core_text::strtolower($current), $query) !== false) {
1896 $default = $this->get_defaultsetting();
1897 if (!is_null($default)) {
1898 if (is_string($default)) {
1899 if (strpos(core_text::strtolower($default), $query) !== false) {
1909 * An additional option that can be applied to an admin setting.
1910 * The currently supported options are 'ADVANCED' and 'LOCKED'.
1912 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1914 class admin_setting_flag {
1915 /** @var bool Flag to indicate if this option can be toggled for this setting */
1916 private $enabled = false;
1917 /** @var bool Flag to indicate if this option defaults to true or false */
1918 private $default = false;
1919 /** @var string Short string used to create setting name - e.g. 'adv' */
1920 private $shortname = '';
1921 /** @var string String used as the label for this flag */
1922 private $displayname = '';
1923 /** @const Checkbox for this flag is displayed in admin page */
1924 const ENABLED = true;
1925 /** @const Checkbox for this flag is not displayed in admin page */
1926 const DISABLED = false;
1931 * @param bool $enabled Can this option can be toggled.
1932 * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED.
1933 * @param bool $default The default checked state for this setting option.
1934 * @param string $shortname The shortname of this flag. Currently supported flags are 'locked' and 'adv'
1935 * @param string $displayname The displayname of this flag. Used as a label for the flag.
1937 public function __construct($enabled, $default, $shortname, $displayname) {
1938 $this->shortname = $shortname;
1939 $this->displayname = $displayname;
1940 $this->set_options($enabled, $default);
1944 * Update the values of this setting options class
1946 * @param bool $enabled Can this option can be toggled.
1947 * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED.
1948 * @param bool $default The default checked state for this setting option.
1950 public function set_options($enabled, $default) {
1951 $this->enabled = $enabled;
1952 $this->default = $default;
1956 * Should this option appear in the interface and be toggleable?
1958 * @return bool Is it enabled?
1960 public function is_enabled() {
1961 return $this->enabled;
1965 * Should this option be checked by default?
1967 * @return bool Is it on by default?
1969 public function get_default() {
1970 return $this->default;
1974 * Return the short name for this flag. e.g. 'adv' or 'locked'
1978 public function get_shortname() {
1979 return $this->shortname;
1983 * Return the display name for this flag. e.g. 'Advanced' or 'Locked'
1987 public function get_displayname() {
1988 return $this->displayname;
1992 * Save the submitted data for this flag - or set it to the default if $data is null.
1994 * @param admin_setting $setting - The admin setting for this flag
1995 * @param array $data - The data submitted from the form or null to set the default value for new installs.
1998 public function write_setting_flag(admin_setting $setting, $data) {
2000 if ($this->is_enabled()) {
2001 if (!isset($data)) {
2002 $value = $this->get_default();
2004 $value = !empty($data[$setting->get_full_name() . '_' . $this->get_shortname()]);
2006 $result = $setting->config_write($setting->name . '_' . $this->get_shortname(), $value);
2014 * Output the checkbox for this setting flag. Should only be called if the flag is enabled.
2016 * @param admin_setting $setting - The admin setting for this flag
2017 * @return string - The html for the checkbox.
2019 public function output_setting_flag(admin_setting $setting) {
2020 $value = $setting->get_setting_flag_value($this);
2021 $output = ' <input type="checkbox" class="form-checkbox" ' .
2022 ' id="' . $setting->get_id() . '_' . $this->get_shortname() . '" ' .
2023 ' name="' . $setting->get_full_name() . '_' . $this->get_shortname() . '" ' .
2024 ' value="1" ' . ($value ? 'checked="checked"' : '') . ' />' .
2025 ' <label for="' . $setting->get_id() . '_' . $this->get_shortname() . '">' .
2026 $this->get_displayname() .
2034 * No setting - just heading and text.
2036 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2038 class admin_setting_heading extends admin_setting {
2041 * not a setting, just text
2042 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2043 * @param string $heading heading
2044 * @param string $information text in box
2046 public function __construct($name, $heading, $information) {
2047 $this->nosave = true;
2048 parent::__construct($name, $heading, $information, '');
2052 * Always returns true
2053 * @return bool Always returns true
2055 public function get_setting() {
2060 * Always returns true
2061 * @return bool Always returns true
2063 public function get_defaultsetting() {
2068 * Never write settings
2069 * @return string Always returns an empty string
2071 public function write_setting($data) {
2072 // do not write any setting
2077 * Returns an HTML string
2078 * @return string Returns an HTML string
2080 public function output_html($data, $query='') {
2083 if ($this->visiblename != '') {
2084 $return .= $OUTPUT->heading($this->visiblename, 3, 'main');
2086 if ($this->description != '') {
2087 $return .= $OUTPUT->box(highlight($query, markdown_to_html($this->description)), 'generalbox formsettingheading');
2095 * The most flexibly setting, user is typing text
2097 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2099 class admin_setting_configtext extends admin_setting {
2101 /** @var mixed int means PARAM_XXX type, string is a allowed format in regex */
2103 /** @var int default field size */
2107 * Config text constructor
2109 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2110 * @param string $visiblename localised
2111 * @param string $description long localised info
2112 * @param string $defaultsetting
2113 * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex
2114 * @param int $size default field size
2116 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) {
2117 $this->paramtype = $paramtype;
2118 if (!is_null($size)) {
2119 $this->size = $size;
2121 $this->size = ($paramtype === PARAM_INT) ? 5 : 30;
2123 parent::__construct($name, $visiblename, $description, $defaultsetting);
2127 * Return the setting
2129 * @return mixed returns config if successful else null
2131 public function get_setting() {
2132 return $this->config_read($this->name);
2135 public function write_setting($data) {
2136 if ($this->paramtype === PARAM_INT and $data === '') {
2137 // do not complain if '' used instead of 0
2140 // $data is a string
2141 $validated = $this->validate($data);
2142 if ($validated !== true) {
2145 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2149 * Validate data before storage
2150 * @param string data
2151 * @return mixed true if ok string if error found
2153 public function validate($data) {
2154 // allow paramtype to be a custom regex if it is the form of /pattern/
2155 if (preg_match('#^/.*/$#', $this->paramtype)) {
2156 if (preg_match($this->paramtype, $data)) {
2159 return get_string('validateerror', 'admin');
2162 } else if ($this->paramtype === PARAM_RAW) {
2166 $cleaned = clean_param($data, $this->paramtype);
2167 if ("$data" === "$cleaned") { // implicit conversion to string is needed to do exact comparison
2170 return get_string('validateerror', 'admin');
2176 * Return an XHTML string for the setting
2177 * @return string Returns an XHTML string
2179 public function output_html($data, $query='') {
2180 $default = $this->get_defaultsetting();
2182 return format_admin_setting($this, $this->visiblename,
2183 '<div class="form-text defaultsnext"><input type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" /></div>',
2184 $this->description, true, '', $default, $query);
2189 * Text input with a maximum length constraint.
2191 * @copyright 2015 onwards Ankit Agarwal
2192 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2194 class admin_setting_configtext_with_maxlength extends admin_setting_configtext {
2196 /** @var int maximum number of chars allowed. */
2197 protected $maxlength;
2200 * Config text constructor
2202 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
2203 * or 'myplugin/mysetting' for ones in config_plugins.
2204 * @param string $visiblename localised
2205 * @param string $description long localised info
2206 * @param string $defaultsetting
2207 * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex
2208 * @param int $size default field size
2209 * @param mixed $maxlength int maxlength allowed, 0 for infinite.
2211 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW,
2212 $size=null, $maxlength = 0) {
2213 $this->maxlength = $maxlength;
2214 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $size);
2218 * Validate data before storage
2220 * @param string $data data
2221 * @return mixed true if ok string if error found
2223 public function validate($data) {
2224 $parentvalidation = parent::validate($data);
2225 if ($parentvalidation === true) {
2226 if ($this->maxlength > 0) {
2227 // Max length check.
2228 $length = core_text::strlen($data);
2229 if ($length > $this->maxlength) {
2230 return get_string('maximumchars', 'moodle', $this->maxlength);
2234 return true; // No max length check needed.
2237 return $parentvalidation;
2243 * General text area without html editor.
2245 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2247 class admin_setting_configtextarea extends admin_setting_configtext {
2252 * @param string $name
2253 * @param string $visiblename
2254 * @param string $description
2255 * @param mixed $defaultsetting string or array
2256 * @param mixed $paramtype
2257 * @param string $cols The number of columns to make the editor
2258 * @param string $rows The number of rows to make the editor
2260 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') {
2261 $this->rows = $rows;
2262 $this->cols = $cols;
2263 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype);
2267 * Returns an XHTML string for the editor
2269 * @param string $data
2270 * @param string $query
2271 * @return string XHTML string for the editor
2273 public function output_html($data, $query='') {
2274 $default = $this->get_defaultsetting();
2276 $defaultinfo = $default;
2277 if (!is_null($default) and $default !== '') {
2278 $defaultinfo = "\n".$default;
2281 return format_admin_setting($this, $this->visiblename,
2282 '<div class="form-textarea" ><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'" spellcheck="true">'. s($data) .'</textarea></div>',
2283 $this->description, true, '', $defaultinfo, $query);
2289 * General text area with html editor.
2291 class admin_setting_confightmleditor extends admin_setting_configtext {
2296 * @param string $name
2297 * @param string $visiblename
2298 * @param string $description
2299 * @param mixed $defaultsetting string or array
2300 * @param mixed $paramtype
2302 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') {
2303 $this->rows = $rows;
2304 $this->cols = $cols;
2305 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype);
2306 editors_head_setup();
2310 * Returns an XHTML string for the editor
2312 * @param string $data
2313 * @param string $query
2314 * @return string XHTML string for the editor
2316 public function output_html($data, $query='') {
2317 $default = $this->get_defaultsetting();
2319 $defaultinfo = $default;
2320 if (!is_null($default) and $default !== '') {
2321 $defaultinfo = "\n".$default;
2324 $editor = editors_get_preferred_editor(FORMAT_HTML);
2325 $editor->set_text($data);
2326 $editor->use_editor($this->get_id(), array('noclean'=>true));
2328 return format_admin_setting($this, $this->visiblename,
2329 '<div class="form-textarea"><textarea rows="'. $this->rows .'" cols="'. $this->cols .'" id="'. $this->get_id() .'" name="'. $this->get_full_name() .'" spellcheck="true">'. s($data) .'</textarea></div>',
2330 $this->description, true, '', $defaultinfo, $query);
2336 * Password field, allows unmasking of password
2338 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2340 class admin_setting_configpasswordunmask extends admin_setting_configtext {
2343 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2344 * @param string $visiblename localised
2345 * @param string $description long localised info
2346 * @param string $defaultsetting default password
2348 public function __construct($name, $visiblename, $description, $defaultsetting) {
2349 parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 30);
2353 * Log config changes if necessary.
2354 * @param string $name
2355 * @param string $oldvalue
2356 * @param string $value
2358 protected function add_to_config_log($name, $oldvalue, $value) {
2359 if ($value !== '') {
2360 $value = '********';
2362 if ($oldvalue !== '' and $oldvalue !== null) {
2363 $oldvalue = '********';
2365 parent::add_to_config_log($name, $oldvalue, $value);
2369 * Returns XHTML for the field
2370 * Writes Javascript into the HTML below right before the last div
2372 * @todo Make javascript available through newer methods if possible
2373 * @param string $data Value for the field
2374 * @param string $query Passed as final argument for format_admin_setting
2375 * @return string XHTML field
2377 public function output_html($data, $query='') {
2378 $id = $this->get_id();
2379 $unmask = get_string('unmaskpassword', 'form');
2380 $unmaskjs = '<script type="text/javascript">
2382 var is_ie = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
2384 document.getElementById("'.$id.'").setAttribute("autocomplete", "off");
2386 var unmaskdiv = document.getElementById("'.$id.'unmaskdiv");
2388 var unmaskchb = document.createElement("input");
2389 unmaskchb.setAttribute("type", "checkbox");
2390 unmaskchb.setAttribute("id", "'.$id.'unmask");
2391 unmaskchb.onchange = function() {unmaskPassword("'.$id.'");};
2392 unmaskdiv.appendChild(unmaskchb);
2394 var unmasklbl = document.createElement("label");
2395 unmasklbl.innerHTML = "'.addslashes_js($unmask).'";
2397 unmasklbl.setAttribute("htmlFor", "'.$id.'unmask");
2399 unmasklbl.setAttribute("for", "'.$id.'unmask");
2401 unmaskdiv.appendChild(unmasklbl);
2404 // ugly hack to work around the famous onchange IE bug
2405 unmaskchb.onclick = function() {this.blur();};
2406 unmaskdiv.onclick = function() {this.blur();};
2410 return format_admin_setting($this, $this->visiblename,
2411 '<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>',
2412 $this->description, true, '', NULL, $query);
2417 * Empty setting used to allow flags (advanced) on settings that can have no sensible default.
2418 * Note: Only advanced makes sense right now - locked does not.
2420 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2422 class admin_setting_configempty extends admin_setting_configtext {
2425 * @param string $name
2426 * @param string $visiblename
2427 * @param string $description
2429 public function __construct($name, $visiblename, $description) {
2430 parent::__construct($name, $visiblename, $description, '', PARAM_RAW);
2434 * Returns an XHTML string for the hidden field
2436 * @param string $data
2437 * @param string $query
2438 * @return string XHTML string for the editor
2440 public function output_html($data, $query='') {
2441 return format_admin_setting($this,
2443 '<div class="form-empty" >' .
2444 '<input type="hidden"' .
2445 ' id="'. $this->get_id() .'"' .
2446 ' name="'. $this->get_full_name() .'"' .
2447 ' value=""/></div>',
2460 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2462 class admin_setting_configfile extends admin_setting_configtext {
2465 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2466 * @param string $visiblename localised
2467 * @param string $description long localised info
2468 * @param string $defaultdirectory default directory location
2470 public function __construct($name, $visiblename, $description, $defaultdirectory) {
2471 parent::__construct($name, $visiblename, $description, $defaultdirectory, PARAM_RAW, 50);
2475 * Returns XHTML for the field
2477 * Returns XHTML for the field and also checks whether the file
2478 * specified in $data exists using file_exists()
2480 * @param string $data File name and path to use in value attr
2481 * @param string $query
2482 * @return string XHTML field
2484 public function output_html($data, $query='') {
2486 $default = $this->get_defaultsetting();
2489 if (file_exists($data)) {
2490 $executable = '<span class="pathok">✔</span>';
2492 $executable = '<span class="patherror">✘</span>';
2498 if (!empty($CFG->preventexecpath)) {
2499 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2500 $readonly = 'readonly="readonly"';
2503 return format_admin_setting($this, $this->visiblename,
2504 '<div class="form-file defaultsnext"><input '.$readonly.' type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>',
2505 $this->description, true, '', $default, $query);
2509 * Checks if execpatch has been disabled in config.php
2511 public function write_setting($data) {
2513 if (!empty($CFG->preventexecpath)) {
2514 if ($this->get_setting() === null) {
2515 // Use default during installation.
2516 $data = $this->get_defaultsetting();
2517 if ($data === null) {
2524 return parent::write_setting($data);
2530 * Path to executable file
2532 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2534 class admin_setting_configexecutable extends admin_setting_configfile {
2537 * Returns an XHTML field
2539 * @param string $data This is the value for the field
2540 * @param string $query
2541 * @return string XHTML field
2543 public function output_html($data, $query='') {
2545 $default = $this->get_defaultsetting();
2546 require_once("$CFG->libdir/filelib.php");
2549 if (file_exists($data) and !is_dir($data) and file_is_executable($data)) {
2550 $executable = '<span class="pathok">✔</span>';
2552 $executable = '<span class="patherror">✘</span>';
2558 if (!empty($CFG->preventexecpath)) {
2559 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2560 $readonly = 'readonly="readonly"';
2563 return format_admin_setting($this, $this->visiblename,
2564 '<div class="form-file defaultsnext"><input '.$readonly.' type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>',
2565 $this->description, true, '', $default, $query);
2573 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2575 class admin_setting_configdirectory extends admin_setting_configfile {
2578 * Returns an XHTML field
2580 * @param string $data This is the value for the field
2581 * @param string $query
2582 * @return string XHTML
2584 public function output_html($data, $query='') {
2586 $default = $this->get_defaultsetting();
2589 if (file_exists($data) and is_dir($data)) {
2590 $executable = '<span class="pathok">✔</span>';
2592 $executable = '<span class="patherror">✘</span>';
2598 if (!empty($CFG->preventexecpath)) {
2599 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2600 $readonly = 'readonly="readonly"';
2603 return format_admin_setting($this, $this->visiblename,
2604 '<div class="form-file defaultsnext"><input '.$readonly.' type="text" size="'.$this->size.'" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($data).'" />'.$executable.'</div>',
2605 $this->description, true, '', $default, $query);
2613 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2615 class admin_setting_configcheckbox extends admin_setting {
2616 /** @var string Value used when checked */
2618 /** @var string Value used when not checked */
2623 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2624 * @param string $visiblename localised
2625 * @param string $description long localised info
2626 * @param string $defaultsetting
2627 * @param string $yes value used when checked
2628 * @param string $no value used when not checked
2630 public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') {
2631 parent::__construct($name, $visiblename, $description, $defaultsetting);
2632 $this->yes = (string)$yes;
2633 $this->no = (string)$no;
2637 * Retrieves the current setting using the objects name
2641 public function get_setting() {
2642 return $this->config_read($this->name);
2646 * Sets the value for the setting
2648 * Sets the value for the setting to either the yes or no values
2649 * of the object by comparing $data to yes
2651 * @param mixed $data Gets converted to str for comparison against yes value
2652 * @return string empty string or error
2654 public function write_setting($data) {
2655 if ((string)$data === $this->yes) { // convert to strings before comparison
2660 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2664 * Returns an XHTML checkbox field
2666 * @param string $data If $data matches yes then checkbox is checked
2667 * @param string $query
2668 * @return string XHTML field
2670 public function output_html($data, $query='') {
2671 $default = $this->get_defaultsetting();
2673 if (!is_null($default)) {
2674 if ((string)$default === $this->yes) {
2675 $defaultinfo = get_string('checkboxyes', 'admin');
2677 $defaultinfo = get_string('checkboxno', 'admin');
2680 $defaultinfo = NULL;
2683 if ((string)$data === $this->yes) { // convert to strings before comparison
2684 $checked = 'checked="checked"';
2689 return format_admin_setting($this, $this->visiblename,
2690 '<div class="form-checkbox defaultsnext" ><input type="hidden" name="'.$this->get_full_name().'" value="'.s($this->no).'" /> '
2691 .'<input type="checkbox" id="'.$this->get_id().'" name="'.$this->get_full_name().'" value="'.s($this->yes).'" '.$checked.' /></div>',
2692 $this->description, true, '', $defaultinfo, $query);
2698 * Multiple checkboxes, each represents different value, stored in csv format
2700 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2702 class admin_setting_configmulticheckbox extends admin_setting {
2703 /** @var array Array of choices value=>label */
2707 * Constructor: uses parent::__construct
2709 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2710 * @param string $visiblename localised
2711 * @param string $description long localised info
2712 * @param array $defaultsetting array of selected
2713 * @param array $choices array of $value=>$label for each checkbox
2715 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2716 $this->choices = $choices;
2717 parent::__construct($name, $visiblename, $description, $defaultsetting);
2721 * This public function may be used in ancestors for lazy loading of choices
2723 * @todo Check if this function is still required content commented out only returns true
2724 * @return bool true if loaded, false if error
2726 public function load_choices() {
2728 if (is_array($this->choices)) {
2731 .... load choices here
2737 * Is setting related to query text - used when searching
2739 * @param string $query
2740 * @return bool true on related, false on not or failure
2742 public function is_related($query) {
2743 if (!$this->load_choices() or empty($this->choices)) {
2746 if (parent::is_related($query)) {
2750 foreach ($this->choices as $desc) {
2751 if (strpos(core_text::strtolower($desc), $query) !== false) {
2759 * Returns the current setting if it is set
2761 * @return mixed null if null, else an array
2763 public function get_setting() {
2764 $result = $this->config_read($this->name);
2766 if (is_null($result)) {
2769 if ($result === '') {
2772 $enabled = explode(',', $result);
2774 foreach ($enabled as $option) {
2775 $setting[$option] = 1;
2781 * Saves the setting(s) provided in $data
2783 * @param array $data An array of data, if not array returns empty str
2784 * @return mixed empty string on useless data or bool true=success, false=failed
2786 public function write_setting($data) {
2787 if (!is_array($data)) {
2788 return ''; // ignore it
2790 if (!$this->load_choices() or empty($this->choices)) {
2793 unset($data['xxxxx']);
2795 foreach ($data as $key => $value) {
2796 if ($value and array_key_exists($key, $this->choices)) {
2800 return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin');
2804 * Returns XHTML field(s) as required by choices
2806 * Relies on data being an array should data ever be another valid vartype with
2807 * acceptable value this may cause a warning/error
2808 * if (!is_array($data)) would fix the problem
2810 * @todo Add vartype handling to ensure $data is an array
2812 * @param array $data An array of checked values
2813 * @param string $query
2814 * @return string XHTML field
2816 public function output_html($data, $query='') {
2817 if (!$this->load_choices() or empty($this->choices)) {
2820 $default = $this->get_defaultsetting();
2821 if (is_null($default)) {
2824 if (is_null($data)) {
2828 $defaults = array();
2829 foreach ($this->choices as $key=>$description) {
2830 if (!empty($data[$key])) {
2831 $checked = 'checked="checked"';
2835 if (!empty($default[$key])) {
2836 $defaults[] = $description;
2839 $options[] = '<input type="checkbox" id="'.$this->get_id().'_'.$key.'" name="'.$this->get_full_name().'['.$key.']" value="1" '.$checked.' />'
2840 .'<label for="'.$this->get_id().'_'.$key.'">'.highlightfast($query, $description).'</label>';
2843 if (is_null($default)) {
2844 $defaultinfo = NULL;
2845 } else if (!empty($defaults)) {
2846 $defaultinfo = implode(', ', $defaults);
2848 $defaultinfo = get_string('none');
2851 $return = '<div class="form-multicheckbox">';
2852 $return .= '<input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected
2855 foreach ($options as $option) {
2856 $return .= '<li>'.$option.'</li>';
2860 $return .= '</div>';
2862 return format_admin_setting($this, $this->visiblename, $return, $this->description, false, '', $defaultinfo, $query);
2869 * Multiple checkboxes 2, value stored as string 00101011
2871 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2873 class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox {
2876 * Returns the setting if set
2878 * @return mixed null if not set, else an array of set settings
2880 public function get_setting() {
2881 $result = $this->config_read($this->name);
2882 if (is_null($result)) {
2885 if (!$this->load_choices()) {
2888 $result = str_pad($result, count($this->choices), '0');
2889 $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY);
2891 foreach ($this->choices as $key=>$unused) {
2892 $value = array_shift($result);
2901 * Save setting(s) provided in $data param
2903 * @param array $data An array of settings to save
2904 * @return mixed empty string for bad data or bool true=>success, false=>error
2906 public function write_setting($data) {
2907 if (!is_array($data)) {
2908 return ''; // ignore it
2910 if (!$this->load_choices() or empty($this->choices)) {
2914 foreach ($this->choices as $key=>$unused) {
2915 if (!empty($data[$key])) {
2921 return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin');
2927 * Select one value from list
2929 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2931 class admin_setting_configselect extends admin_setting {
2932 /** @var array Array of choices value=>label */
2937 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2938 * @param string $visiblename localised
2939 * @param string $description long localised info
2940 * @param string|int $defaultsetting
2941 * @param array $choices array of $value=>$label for each selection
2943 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2944 $this->choices = $choices;
2945 parent::__construct($name, $visiblename, $description, $defaultsetting);
2949 * This function may be used in ancestors for lazy loading of choices
2951 * Override this method if loading of choices is expensive, such
2952 * as when it requires multiple db requests.
2954 * @return bool true if loaded, false if error
2956 public function load_choices() {
2958 if (is_array($this->choices)) {
2961 .... load choices here
2967 * Check if this is $query is related to a choice
2969 * @param string $query
2970 * @return bool true if related, false if not
2972 public function is_related($query) {
2973 if (parent::is_related($query)) {
2976 if (!$this->load_choices()) {
2979 foreach ($this->choices as $key=>$value) {
2980 if (strpos(core_text::strtolower($key), $query) !== false) {
2983 if (strpos(core_text::strtolower($value), $query) !== false) {
2991 * Return the setting
2993 * @return mixed returns config if successful else null
2995 public function get_setting() {
2996 return $this->config_read($this->name);
3002 * @param string $data
3003 * @return string empty of error string
3005 public function write_setting($data) {
3006 if (!$this->load_choices() or empty($this->choices)) {
3009 if (!array_key_exists($data, $this->choices)) {
3010 return ''; // ignore it
3013 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
3017 * Returns XHTML select field
3019 * Ensure the options are loaded, and generate the XHTML for the select
3020 * element and any warning message. Separating this out from output_html
3021 * makes it easier to subclass this class.
3023 * @param string $data the option to show as selected.
3024 * @param string $current the currently selected option in the database, null if none.
3025 * @param string $default the default selected option.
3026 * @return array the HTML for the select element, and a warning message.
3028 public function output_select_html($data, $current, $default, $extraname = '') {
3029 if (!$this->load_choices() or empty($this->choices)) {
3030 return array('', '');
3034 if (is_null($current)) {
3036 } else if (empty($current) and (array_key_exists('', $this->choices) or array_key_exists(0, $this->choices))) {
3038 } else if (!array_key_exists($current, $this->choices)) {
3039 $warning = get_string('warningcurrentsetting', 'admin', s($current));
3040 if (!is_null($default) and $data == $current) {
3041 $data = $default; // use default instead of first value when showing the form
3045 $selecthtml = '<select id="'.$this->get_id().'" name="'.$this->get_full_name().$extraname.'">';
3046 foreach ($this->choices as $key => $value) {
3047 // the string cast is needed because key may be integer - 0 is equal to most strings!
3048 $selecthtml .= '<option value="'.$key.'"'.((string)$key==$data ? ' selected="selected"' : '').'>'.$value.'</option>';
3050 $selecthtml .= '</select>';
3051 return array($selecthtml, $warning);
3055 * Returns XHTML select field and wrapping div(s)
3057 * @see output_select_html()
3059 * @param string $data the option to show as selected
3060 * @param string $query
3061 * @return string XHTML field and wrapping div
3063 public function output_html($data, $query='') {
3064 $default = $this->get_defaultsetting();
3065 $current = $this->get_setting();
3067 list($selecthtml, $warning) = $this->output_select_html($data, $current, $default);
3072 if (!is_null($default) and array_key_exists($default, $this->choices)) {
3073 $defaultinfo = $this->choices[$default];
3075 $defaultinfo = NULL;
3078 $return = '<div class="form-select defaultsnext">' . $selecthtml . '</div>';
3080 return format_admin_setting($this, $this->visiblename, $return, $this->description, true, $warning, $defaultinfo, $query);
3086 * Select multiple items from list
3088 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3090 class admin_setting_configmultiselect extends admin_setting_configselect {
3093 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
3094 * @param string $visiblename localised
3095 * @param string $description long localised info
3096 * @param array $defaultsetting array of selected items
3097 * @param array $choices array of $value=>$label for each list item
3099 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
3100 parent::__construct($name, $visiblename, $description, $defaultsetting, $choices);
3104 * Returns the select setting(s)
3106 * @return mixed null or array. Null if no settings else array of setting(s)
3108 public function get_setting() {
3109 $result = $this->config_read($this->name);
3110 if (is_null($result)) {
3113 if ($result === '') {
3116 return explode(',', $result);
3120 * Saves setting(s) provided through $data
3122 * Potential bug in the works should anyone call with this function
3123 * using a vartype that is not an array
3125 * @param array $data
3127 public function write_setting($data) {
3128 if (!is_array($data)) {
3129 return ''; //ignore it
3131 if (!$this->load_choices() or empty($this->choices)) {
3135 unset($data['xxxxx']);
3138 foreach ($data as $value) {
3139 if (!array_key_exists($value, $this->choices)) {
3140 continue; // ignore it
3145 return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin'));
3149 * Is setting related to query text - used when searching
3151 * @param string $query
3152 * @return bool true if related, false if not
3154 public function is_related($query) {
3155 if (!$this->load_choices() or empty($this->choices)) {
3158 if (parent::is_related($query)) {
3162 foreach ($this->choices as $desc) {
3163 if (strpos(core_text::strtolower($desc), $query) !== false) {
3171 * Returns XHTML multi-select field
3173 * @todo Add vartype handling to ensure $data is an array
3174 * @param array $data Array of values to select by default
3175 * @param string $query
3176 * @return string XHTML multi-select field
3178 public function output_html($data, $query='') {
3179 if (!$this->load_choices() or empty($this->choices)) {
3182 $choices = $this->choices;
3183 $default = $this->get_defaultsetting();
3184 if (is_null($default)) {
3187 if (is_null($data)) {
3191 $defaults = array();
3192 $size = min(10, count($this->choices));
3193 $return = '<div class="form-select"><input type="hidden" name="'.$this->get_full_name().'[xxxxx]" value="1" />'; // something must be submitted even if nothing selected
3194 $return .= '<select id="'.$this->get_id().'" name="'.$this->get_full_name().'[]" size="'.$size.'" multiple="multiple">';
3195 foreach ($this->choices as $key => $description) {
3196 if (in_array($key, $data)) {
3197 $selected = 'selected="selected"';
3201 if (in_array($key, $default)) {
3202 $defaults[] = $description;
3205 $return .= '<option value="'.s($key).'" '.$selected.'>'.$description.'</option>';
3208 if (is_null($default)) {
3209 $defaultinfo = NULL;
3210 } if (!empty($defaults)) {
3211 $defaultinfo = implode(', ', $defaults);
3213 $defaultinfo = get_string('none');
3216 $return .= '</select></div>';
3217 return format_admin_setting($this, $this->visiblename, $return, $this->description, true, '', $defaultinfo, $query);
3224 * This is a liiitle bit messy. we're using two selects, but we're returning
3225 * them as an array named after $name (so we only use $name2 internally for the setting)
3227 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3229 class admin_setting_configtime extends admin_setting {
3230 /** @var string Used for setting second select (minutes) */
3235 * @param string $hoursname setting for hours
3236 * @param string $minutesname setting for hours
3237 * @param string $visiblename localised
3238 * @param string $description long localised info
3239 * @param array $defaultsetting array representing default time 'h'=>hours, 'm'=>minutes
3241 public function __construct($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
3242 $this->name2 = $minutesname;
3243 parent::__construct($hoursname, $visiblename, $description, $defaultsetting);
3247 * Get the selected time
3249 * @return mixed An array containing 'h'=>xx, 'm'=>xx, or null if not set
3251 public function get_setting() {
3252 $result1 = $this->config_read($this->name);
3253 $result2 = $this->config_read($this->name2);
3254 if (is_null($result1) or is_null($result2)) {
3258 return array('h' => $result1, 'm' => $result2);
3262 * Store the time (hours and minutes)
3264 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3265 * @return bool true if success, false if not
3267 public function write_setting($data) {
3268 if (!is_array($data)) {
3272 $result = $this->config_write($this->name, (int)$data['h']) && $this->config_write($this->name2, (int)$data['m']);
3273 return ($result ? '' : get_string('errorsetting', 'admin'));
3277 * Returns XHTML time select fields
3279 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3280 * @param string $query
3281 * @return string XHTML time select fields and wrapping div(s)
3283 public function output_html($data, $query='') {
3284 $default = $this->get_defaultsetting();
3286 if (is_array($default)) {
3287 $defaultinfo = $default['h'].':'.$default['m'];
3289 $defaultinfo = NULL;
3292 $return = '<div class="form-time defaultsnext">';
3293 $return .= '<label class="accesshide" for="' . $this->get_id() . 'h">' . get_string('hours') . '</label>';
3294 $return .= '<select id="' . $this->get_id() . 'h" name="' . $this->get_full_name() . '[h]">';
3295 for ($i = 0; $i < 24; $i++) {
3296 $return .= '<option value="' . $i . '"' . ($i == $data['h'] ? ' selected="selected"' : '') . '>' . $i . '</option>';
3298 $return .= '</select>:';
3299 $return .= '<label class="accesshide" for="' . $this->get_id() . 'm">' . get_string('minutes') . '</label>';
3300 $return .= '<select id="' . $this->get_id() . 'm" name="' . $this->get_full_name() . '[m]">';
3301 for ($i = 0; $i < 60; $i += 5) {
3302 $return .= '<option value="' . $i . '"' . ($i == $data['m'] ? ' selected="selected"' : '') . '>' . $i . '</option>';
3304 $return .= '</select>';
3305 $return .= '</div>';
3306 return format_admin_setting($this, $this->visiblename, $return, $this->description,
3307 $this->get_id() . 'h', '', $defaultinfo, $query);
3314 * Seconds duration setting.
3316 * @copyright 2012 Petr Skoda (http://skodak.org)
3317 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3319 class admin_setting_configduration extends admin_setting {
3321 /** @var int default duration unit */
3322 protected $defaultunit;
3326 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
3327 * or 'myplugin/mysetting' for ones in config_plugins.
3328 * @param string $visiblename localised name
3329 * @param string $description localised long description
3330 * @param mixed $defaultsetting string or array depending on implementation
3331 * @param int $defaultunit - day, week, etc. (in seconds)
3333 public function __construct($name, $visiblename, $description, $defaultsetting, $defaultunit = 86400) {
3334 if (is_number($defaultsetting)) {
3335 $defaultsetting = self::parse_seconds($defaultsetting);
3337 $units = self::get_units();
3338 if (isset($units[$defaultunit])) {
3339 $this->defaultunit = $defaultunit;
3341 $this->defaultunit = 86400;
3343 parent::__construct($name, $visiblename, $description, $defaultsetting);
3347 * Returns selectable units.
3351 protected static function get_units() {
3353 604800 => get_string('weeks'),
3354 86400 => get_string('days'),
3355 3600 => get_string('hours'),
3356 60 => get_string('minutes'),
3357 1 => get_string('seconds'),
3362 * Converts seconds to some more user friendly string.
3364 * @param int $seconds
3367 protected static function get_duration_text($seconds) {
3368 if (empty($seconds)) {
3369 return get_string('none');
3371 $data = self::parse_seconds($seconds);
3372 switch ($data['u']) {
3374 return get_string('numweeks', '', $data['v']);
3376 return get_string('numdays', '', $data['v']);
3378 return get_string('numhours', '', $data['v']);
3380 return get_string('numminutes', '', $data['v']);
3382 return get_string('numseconds', '', $data['v']*$data['u']);
3387 * Finds suitable units for given duration.
3389 * @param int $seconds
3392 protected static function parse_seconds($seconds) {
3393 foreach (self::get_units() as $unit => $unused) {
3394 if ($seconds % $unit === 0) {
3395 return array('v'=>(int)($seconds/$unit), 'u'=>$unit);
3398 return array('v'=>(int)$seconds, 'u'=>1);
3402 * Get the selected duration as array.
3404 * @return mixed An array containing 'v'=>xx, 'u'=>xx, or null if not set
3406 public function get_setting() {
3407 $seconds = $this->config_read($this->name);
3408 if (is_null($seconds)) {
3412 return self::parse_seconds($seconds);
3416 * Store the duration as seconds.
3418 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3419 * @return bool true if success, false if not
3421 public function write_setting($data) {
3422 if (!is_array($data)) {
3426 $seconds = (int)($data['v']*$data['u']);
3428 return get_string('errorsetting', 'admin');
3431 $result = $this->config_write($this->name, $seconds);
3432 return ($result ? '' : get_string('errorsetting', 'admin'));
3436 * Returns duration text+select fields.
3438 * @param array $data Must be form 'v'=>xx, 'u'=>xx
3439 * @param string $query
3440 * @return string duration text+select fields and wrapping div(s)
3442 public function output_html($data, $query='') {
3443 $default = $this->get_defaultsetting();
3445 if (is_number($default)) {
3446 $defaultinfo = self::get_duration_text($default);
3447 } else if (is_array($default)) {
3448 $defaultinfo = self::get_duration_text($default['v']*$default['u']);
3450 $defaultinfo = null;
3453 $units = self::get_units();
3455 $inputid = $this->get_id() . 'v';
3457 $return = '<div class="form-duration defaultsnext">';
3458 $return .= '<input type="text" size="5" id="' . $inputid . '" name="' . $this->get_full_name() .
3459 '[v]" value="' . s($data['v']) . '" />';
3460 $return .= '<label for="' . $this->get_id() . 'u" class="accesshide">' .
3461 get_string('durationunits', 'admin') . '</label>';
3462 $return .= '<select id="'.$this->get_id().'u" name="'.$this->get_full_name().'[u]">';
3463 foreach ($units as $val => $text) {
3465 if ($data['v'] == 0) {
3466 if ($val == $this->defaultunit) {
3467 $selected = ' selected="selected"';
3469 } else if ($val == $data['u']) {
3470 $selected = ' selected="selected"';
3472 $return .= '<option value="'.$val.'"'.$selected.'>'.$text.'</option>';
3474 $return .= '</select></div>';
3475 return format_admin_setting($this, $this->visiblename, $return, $this->description, $inputid, '', $defaultinfo, $query);
3481 * Seconds duration setting with an advanced checkbox, that controls a additional
3482 * $name.'_adv' setting.
3484 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3485 * @copyright 2014 The Open University
3487 class admin_setting_configduration_with_advanced extends admin_setting_configduration {
3490 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
3491 * or 'myplugin/mysetting' for ones in config_plugins.
3492 * @param string $visiblename localised name
3493 * @param string $description localised long description
3494 * @param array $defaultsetting array of int value, and bool whether it is
3495 * is advanced by default.
3496 * @param int $defaultunit - day, week, etc. (in seconds)
3498 public function __construct($name, $visiblename, $description, $defaultsetting, $defaultunit = 86400) {
3499 parent::__construct($name, $visiblename, $description, $defaultsetting['value'], $defaultunit);
3500 $this->set_advanced_flag_options(admin_setting_flag::ENABLED, !empty($defaultsetting['adv']));
3506 * Used to validate a textarea used for ip addresses
3508 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3509 * @copyright 2011 Petr Skoda (http://skodak.org)
3511 class admin_setting_configiplist extends admin_setting_configtextarea {
3514 * Validate the contents of the textarea as IP addresses
3516 * Used to validate a new line separated list of IP addresses collected from
3517 * a textarea control
3519 * @param string $data A list of IP Addresses separated by new lines
3520 * @return mixed bool true for success or string:error on failure
3522 public function validate($data) {
3524 $ips = explode("\n", $data);
3529 foreach($ips as $ip) {
3531 if (preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}$#', $ip, $match) ||
3532 preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}(\/\d{1,2})$#', $ip, $match) ||
3533 preg_match('#^(\d{1,3})(\.\d{1,3}){3}(-\d{1,3})$#', $ip, $match)) {
3543 return get_string('validateerror', 'admin');
3550 * An admin setting for selecting one or more users who have a capability
3551 * in the system context
3553 * An admin setting for selecting one or more users, who have a particular capability
3554 * in the system context. Warning, make sure the list will never be too long. There is
3555 * no paging or searching of this list.
3557 * To correctly get a list of users from this config setting, you need to call the
3558 * get_users_from_config($CFG->mysetting, $capability); function in moodlelib.php.
3560 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3562 class admin_setting_users_with_capability extends admin_setting_configmultiselect {
3563 /** @var string The capabilities name */
3564 protected $capability;
3565 /** @var int include admin users too */
3566 protected $includeadmins;
3571 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
3572 * @param string $visiblename localised name
3573 * @param string $description localised long description
3574 * @param array $defaultsetting array of usernames
3575 * @param string $capability string capability name.
3576 * @param bool $includeadmins include administrators
3578 function __construct($name, $visiblename, $description, $defaultsetting, $capability, $includeadmins = true) {
3579 $this->capability = $capability;
3580 $this->includeadmins = $includeadmins;
3581 parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
3585 * Load all of the uses who have the capability into choice array
3587 * @return bool Always returns true
3589 function load_choices() {
3590 if (is_array($this->choices)) {
3593 list($sort, $sortparams) = users_order_by_sql('u');
3594 if (!empty($sortparams)) {
3595 throw new coding_exception('users_order_by_sql returned some query parameters. ' .
3596 'This is unexpected, and a problem because there is no way to pass these ' .
3597 'parameters to get_users_by_capability. See MDL-34657.');
3599 $userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u');
3600 $users = get_users_by_capability(context_system::instance(), $this->capability, $userfields, $sort);
3601 $this->choices = array(
3602 '$@NONE@$' => get_string('nobody'),
3603 '$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string($this->capability)),
3605 if ($this->includeadmins) {
3606 $admins = get_admins();
3607 foreach ($admins as $user) {
3608 $this->choices[$user->id] = fullname($user);
3611 if (is_array($users)) {
3612 foreach ($users as $user) {
3613 $this->choices[$user->id] = fullname($user);
3620 * Returns the default setting for class
3622 * @return mixed Array, or string. Empty string if no default
3624 public function get_defaultsetting() {
3625 $this->load_choices();
3626 $defaultsetting = parent::get_defaultsetting();
3627 if (empty($defaultsetting)) {
3628 return array('$@NONE@$');
3629 } else if (array_key_exists($defaultsetting, $this->choices)) {
3630 return $defaultsetting;
3637 * Returns the current setting
3639 * @return mixed array or string
3641 public function get_setting() {
3642 $result = parent::get_setting();
3643 if ($result === null) {
3644 // this is necessary for settings upgrade
3647 if (empty($result)) {
3648 $result = array('$@NONE@$');