3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Functions and classes used during installation, upgrades and for admin settings.
21 * ADMIN SETTINGS TREE INTRODUCTION
23 * This file performs the following tasks:
24 * -it defines the necessary objects and interfaces to build the Moodle
26 * -it defines the admin_externalpage_setup()
28 * ADMIN_SETTING OBJECTS
30 * Moodle settings are represented by objects that inherit from the admin_setting
31 * class. These objects encapsulate how to read a setting, how to write a new value
32 * to a setting, and how to appropriately display the HTML to modify the setting.
34 * ADMIN_SETTINGPAGE OBJECTS
36 * The admin_setting objects are then grouped into admin_settingpages. The latter
37 * appear in the Moodle admin tree block. All interaction with admin_settingpage
38 * objects is handled by the admin/settings.php file.
40 * ADMIN_EXTERNALPAGE OBJECTS
42 * There are some settings in Moodle that are too complex to (efficiently) handle
43 * with admin_settingpages. (Consider, for example, user management and displaying
44 * lists of users.) In this case, we use the admin_externalpage object. This object
45 * places a link to an external PHP file in the admin tree block.
47 * If you're using an admin_externalpage object for some settings, you can take
48 * advantage of the admin_externalpage_* functions. For example, suppose you wanted
49 * to add a foo.php file into admin. First off, you add the following line to
50 * admin/settings/first.php (at the end of the file) or to some other file in
53 * $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
54 * $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
57 * Next, in foo.php, your file structure would resemble the following:
59 * require(__DIR__.'/../../config.php');
60 * require_once($CFG->libdir.'/adminlib.php');
61 * admin_externalpage_setup('foo');
62 * // functionality like processing form submissions goes here
63 * echo $OUTPUT->header();
64 * // your HTML goes here
65 * echo $OUTPUT->footer();
68 * The admin_externalpage_setup() function call ensures the user is logged in,
69 * and makes sure that they have the proper role permission to access the page.
70 * It also configures all $PAGE properties needed for navigation.
72 * ADMIN_CATEGORY OBJECTS
74 * Above and beyond all this, we have admin_category objects. These objects
75 * appear as folders in the admin tree block. They contain admin_settingpage's,
76 * admin_externalpage's, and other admin_category's.
80 * admin_settingpage's, admin_externalpage's, and admin_category's all inherit
81 * from part_of_admin_tree (a pseudointerface). This interface insists that
82 * a class has a check_access method for access permissions, a locate method
83 * used to find a specific node in the admin tree and find parent path.
85 * admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
86 * interface ensures that the class implements a recursive add function which
87 * accepts a part_of_admin_tree object and searches for the proper place to
88 * put it. parentable_part_of_admin_tree implies part_of_admin_tree.
90 * Please note that the $this->name field of any part_of_admin_tree must be
91 * UNIQUE throughout the ENTIRE admin tree.
93 * The $this->name field of an admin_setting object (which is *not* part_of_
94 * admin_tree) must be unique on the respective admin_settingpage where it is
97 * Original author: Vincenzo K. Marcovecchio
98 * Maintainer: Petr Skoda
102 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
103 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
106 defined('MOODLE_INTERNAL') || die();
109 require_once($CFG->libdir.'/ddllib.php');
110 require_once($CFG->libdir.'/xmlize.php');
111 require_once($CFG->libdir.'/messagelib.php');
113 define('INSECURE_DATAROOT_WARNING', 1);
114 define('INSECURE_DATAROOT_ERROR', 2);
117 * Automatically clean-up all plugin data and remove the plugin DB tables
119 * NOTE: do not call directly, use new /admin/plugins.php?uninstall=component instead!
121 * @param string $type The plugin type, eg. 'mod', 'qtype', 'workshopgrading' etc.
122 * @param string $name The plugin name, eg. 'forum', 'multichoice', 'accumulative' etc.
123 * @uses global $OUTPUT to produce notices and other messages
126 function uninstall_plugin($type, $name) {
127 global $CFG, $DB, $OUTPUT;
129 // This may take a long time.
130 core_php_time_limit::raise();
132 // Recursively uninstall all subplugins first.
133 $subplugintypes = core_component::get_plugin_types_with_subplugins();
134 if (isset($subplugintypes[$type])) {
135 $base = core_component::get_plugin_directory($type, $name);
136 if (file_exists("$base/db/subplugins.php")) {
137 $subplugins = array();
138 include("$base/db/subplugins.php");
139 foreach ($subplugins as $subplugintype=>$dir) {
140 $instances = core_component::get_plugin_list($subplugintype);
141 foreach ($instances as $subpluginname => $notusedpluginpath) {
142 uninstall_plugin($subplugintype, $subpluginname);
149 $component = $type . '_' . $name; // eg. 'qtype_multichoice' or 'workshopgrading_accumulative' or 'mod_forum'
151 if ($type === 'mod') {
152 $pluginname = $name; // eg. 'forum'
153 if (get_string_manager()->string_exists('modulename', $component)) {
154 $strpluginname = get_string('modulename', $component);
156 $strpluginname = $component;
160 $pluginname = $component;
161 if (get_string_manager()->string_exists('pluginname', $component)) {
162 $strpluginname = get_string('pluginname', $component);
164 $strpluginname = $component;
168 echo $OUTPUT->heading($pluginname);
170 // Delete all tag areas, collections and instances associated with this plugin.
171 core_tag_area::uninstall($component);
173 // Custom plugin uninstall.
174 $plugindirectory = core_component::get_plugin_directory($type, $name);
175 $uninstalllib = $plugindirectory . '/db/uninstall.php';
176 if (file_exists($uninstalllib)) {
177 require_once($uninstalllib);
178 $uninstallfunction = 'xmldb_' . $pluginname . '_uninstall'; // eg. 'xmldb_workshop_uninstall()'
179 if (function_exists($uninstallfunction)) {
180 // Do not verify result, let plugin complain if necessary.
181 $uninstallfunction();
185 // Specific plugin type cleanup.
186 $plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
188 $plugininfo->uninstall_cleanup();
189 core_plugin_manager::reset_caches();
193 // perform clean-up task common for all the plugin/subplugin types
195 //delete the web service functions and pre-built services
196 require_once($CFG->dirroot.'/lib/externallib.php');
197 external_delete_descriptions($component);
199 // delete calendar events
200 $DB->delete_records('event', array('modulename' => $pluginname));
202 // Delete scheduled tasks.
203 $DB->delete_records('task_scheduled', array('component' => $component));
205 // Delete Inbound Message datakeys.
206 $DB->delete_records_select('messageinbound_datakeys',
207 'handler IN (SELECT id FROM {messageinbound_handlers} WHERE component = ?)', array($component));
209 // Delete Inbound Message handlers.
210 $DB->delete_records('messageinbound_handlers', array('component' => $component));
212 // delete all the logs
213 $DB->delete_records('log', array('module' => $pluginname));
215 // delete log_display information
216 $DB->delete_records('log_display', array('component' => $component));
218 // delete the module configuration records
219 unset_all_config_for_plugin($component);
220 if ($type === 'mod') {
221 unset_all_config_for_plugin($pluginname);
224 // delete message provider
225 message_provider_uninstall($component);
227 // delete the plugin tables
228 $xmldbfilepath = $plugindirectory . '/db/install.xml';
229 drop_plugin_tables($component, $xmldbfilepath, false);
230 if ($type === 'mod' or $type === 'block') {
231 // non-frankenstyle table prefixes
232 drop_plugin_tables($name, $xmldbfilepath, false);
235 // delete the capabilities that were defined by this module
236 capabilities_cleanup($component);
238 // Delete all remaining files in the filepool owned by the component.
239 $fs = get_file_storage();
240 $fs->delete_component_files($component);
242 // Finally purge all caches.
245 // Invalidate the hash used for upgrade detections.
246 set_config('allversionshash', '');
248 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
252 * Returns the version of installed component
254 * @param string $component component name
255 * @param string $source either 'disk' or 'installed' - where to get the version information from
256 * @return string|bool version number or false if the component is not found
258 function get_component_version($component, $source='installed') {
261 list($type, $name) = core_component::normalize_component($component);
263 // moodle core or a core subsystem
264 if ($type === 'core') {
265 if ($source === 'installed') {
266 if (empty($CFG->version)) {
269 return $CFG->version;
272 if (!is_readable($CFG->dirroot.'/version.php')) {
275 $version = null; //initialize variable for IDEs
276 include($CFG->dirroot.'/version.php');
283 if ($type === 'mod') {
284 if ($source === 'installed') {
285 if ($CFG->version < 2013092001.02) {
286 return $DB->get_field('modules', 'version', array('name'=>$name));
288 return get_config('mod_'.$name, 'version');
292 $mods = core_component::get_plugin_list('mod');
293 if (empty($mods[$name]) or !is_readable($mods[$name].'/version.php')) {
296 $plugin = new stdClass();
297 $plugin->version = null;
299 include($mods[$name].'/version.php');
300 return $plugin->version;
306 if ($type === 'block') {
307 if ($source === 'installed') {
308 if ($CFG->version < 2013092001.02) {
309 return $DB->get_field('block', 'version', array('name'=>$name));
311 return get_config('block_'.$name, 'version');
314 $blocks = core_component::get_plugin_list('block');
315 if (empty($blocks[$name]) or !is_readable($blocks[$name].'/version.php')) {
318 $plugin = new stdclass();
319 include($blocks[$name].'/version.php');
320 return $plugin->version;
325 // all other plugin types
326 if ($source === 'installed') {
327 return get_config($type.'_'.$name, 'version');
329 $plugins = core_component::get_plugin_list($type);
330 if (empty($plugins[$name])) {
333 $plugin = new stdclass();
334 include($plugins[$name].'/version.php');
335 return $plugin->version;
341 * Delete all plugin tables
343 * @param string $name Name of plugin, used as table prefix
344 * @param string $file Path to install.xml file
345 * @param bool $feedback defaults to true
346 * @return bool Always returns true
348 function drop_plugin_tables($name, $file, $feedback=true) {
351 // first try normal delete
352 if (file_exists($file) and $DB->get_manager()->delete_tables_from_xmldb_file($file)) {
356 // then try to find all tables that start with name and are not in any xml file
357 $used_tables = get_used_table_names();
359 $tables = $DB->get_tables();
361 /// Iterate over, fixing id fields as necessary
362 foreach ($tables as $table) {
363 if (in_array($table, $used_tables)) {
367 if (strpos($table, $name) !== 0) {
371 // found orphan table --> delete it
372 if ($DB->get_manager()->table_exists($table)) {
373 $xmldb_table = new xmldb_table($table);
374 $DB->get_manager()->drop_table($xmldb_table);
382 * Returns names of all known tables == tables that moodle knows about.
384 * @return array Array of lowercase table names
386 function get_used_table_names() {
387 $table_names = array();
388 $dbdirs = get_db_directories();
390 foreach ($dbdirs as $dbdir) {
391 $file = $dbdir.'/install.xml';
393 $xmldb_file = new xmldb_file($file);
395 if (!$xmldb_file->fileExists()) {
399 $loaded = $xmldb_file->loadXMLStructure();
400 $structure = $xmldb_file->getStructure();
402 if ($loaded and $tables = $structure->getTables()) {
403 foreach($tables as $table) {
404 $table_names[] = strtolower($table->getName());
413 * Returns list of all directories where we expect install.xml files
414 * @return array Array of paths
416 function get_db_directories() {
421 /// First, the main one (lib/db)
422 $dbdirs[] = $CFG->libdir.'/db';
424 /// Then, all the ones defined by core_component::get_plugin_types()
425 $plugintypes = core_component::get_plugin_types();
426 foreach ($plugintypes as $plugintype => $pluginbasedir) {
427 if ($plugins = core_component::get_plugin_list($plugintype)) {
428 foreach ($plugins as $plugin => $plugindir) {
429 $dbdirs[] = $plugindir.'/db';
438 * Try to obtain or release the cron lock.
439 * @param string $name name of lock
440 * @param int $until timestamp when this lock considered stale, null means remove lock unconditionally
441 * @param bool $ignorecurrent ignore current lock state, usually extend previous lock, defaults to false
442 * @return bool true if lock obtained
444 function set_cron_lock($name, $until, $ignorecurrent=false) {
447 debugging("Tried to get a cron lock for a null fieldname");
451 // remove lock by force == remove from config table
452 if (is_null($until)) {
453 set_config($name, null);
457 if (!$ignorecurrent) {
458 // read value from db - other processes might have changed it
459 $value = $DB->get_field('config', 'value', array('name'=>$name));
461 if ($value and $value > time()) {
467 set_config($name, $until);
472 * Test if and critical warnings are present
475 function admin_critical_warnings_present() {
478 if (!has_capability('moodle/site:config', context_system::instance())) {
482 if (!isset($SESSION->admin_critical_warning)) {
483 $SESSION->admin_critical_warning = 0;
484 if (is_dataroot_insecure(true) === INSECURE_DATAROOT_ERROR) {
485 $SESSION->admin_critical_warning = 1;
489 return $SESSION->admin_critical_warning;
493 * Detects if float supports at least 10 decimal digits
495 * Detects if float supports at least 10 decimal digits
496 * and also if float-->string conversion works as expected.
498 * @return bool true if problem found
500 function is_float_problem() {
501 $num1 = 2009010200.01;
502 $num2 = 2009010200.02;
504 return ((string)$num1 === (string)$num2 or $num1 === $num2 or $num2 <= (string)$num1);
508 * Try to verify that dataroot is not accessible from web.
510 * Try to verify that dataroot is not accessible from web.
511 * It is not 100% correct but might help to reduce number of vulnerable sites.
512 * Protection from httpd.conf and .htaccess is not detected properly.
514 * @uses INSECURE_DATAROOT_WARNING
515 * @uses INSECURE_DATAROOT_ERROR
516 * @param bool $fetchtest try to test public access by fetching file, default false
517 * @return mixed empty means secure, INSECURE_DATAROOT_ERROR found a critical problem, INSECURE_DATAROOT_WARNING might be problematic
519 function is_dataroot_insecure($fetchtest=false) {
522 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
524 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
525 $rp = strrev(trim($rp, '/'));
526 $rp = explode('/', $rp);
528 if (strpos($siteroot, '/'.$r.'/') === 0) {
529 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
531 break; // probably alias root
535 $siteroot = strrev($siteroot);
536 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
538 if (strpos($dataroot, $siteroot) !== 0) {
543 return INSECURE_DATAROOT_WARNING;
546 // now try all methods to fetch a test file using http protocol
548 $httpdocroot = str_replace('\\', '/', strrev($CFG->dirroot.'/'));
549 preg_match('|(https?://[^/]+)|i', $CFG->wwwroot, $matches);
550 $httpdocroot = $matches[1];
551 $datarooturl = $httpdocroot.'/'. substr($dataroot, strlen($siteroot));
552 make_upload_directory('diag');
553 $testfile = $CFG->dataroot.'/diag/public.txt';
554 if (!file_exists($testfile)) {
555 file_put_contents($testfile, 'test file, do not delete');
556 @chmod($testfile, $CFG->filepermissions);
558 $teststr = trim(file_get_contents($testfile));
559 if (empty($teststr)) {
561 return INSECURE_DATAROOT_WARNING;
564 $testurl = $datarooturl.'/diag/public.txt';
565 if (extension_loaded('curl') and
566 !(stripos(ini_get('disable_functions'), 'curl_init') !== FALSE) and
567 !(stripos(ini_get('disable_functions'), 'curl_setop') !== FALSE) and
568 ($ch = @curl_init($testurl)) !== false) {
569 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
570 curl_setopt($ch, CURLOPT_HEADER, false);
571 $data = curl_exec($ch);
572 if (!curl_errno($ch)) {
574 if ($data === $teststr) {
576 return INSECURE_DATAROOT_ERROR;
582 if ($data = @file_get_contents($testurl)) {
584 if ($data === $teststr) {
585 return INSECURE_DATAROOT_ERROR;
589 preg_match('|https?://([^/]+)|i', $testurl, $matches);
590 $sitename = $matches[1];
592 if ($fp = @fsockopen($sitename, 80, $error)) {
593 preg_match('|https?://[^/]+(.*)|i', $testurl, $matches);
594 $localurl = $matches[1];
595 $out = "GET $localurl HTTP/1.1\r\n";
596 $out .= "Host: $sitename\r\n";
597 $out .= "Connection: Close\r\n\r\n";
603 $data .= fgets($fp, 1024);
604 } else if (@fgets($fp, 1024) === "\r\n") {
610 if ($data === $teststr) {
611 return INSECURE_DATAROOT_ERROR;
615 return INSECURE_DATAROOT_WARNING;
619 * Enables CLI maintenance mode by creating new dataroot/climaintenance.html file.
621 function enable_cli_maintenance_mode() {
624 if (file_exists("$CFG->dataroot/climaintenance.html")) {
625 unlink("$CFG->dataroot/climaintenance.html");
628 if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) {
629 $data = $CFG->maintenance_message;
630 $data = bootstrap_renderer::early_error_content($data, null, null, null);
631 $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);
633 } else if (file_exists("$CFG->dataroot/climaintenance.template.html")) {
634 $data = file_get_contents("$CFG->dataroot/climaintenance.template.html");
637 $data = get_string('sitemaintenance', 'admin');
638 $data = bootstrap_renderer::early_error_content($data, null, null, null);
639 $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data);
642 file_put_contents("$CFG->dataroot/climaintenance.html", $data);
643 chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions);
646 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
650 * Interface for anything appearing in the admin tree
652 * The interface that is implemented by anything that appears in the admin tree
653 * block. It forces inheriting classes to define a method for checking user permissions
654 * and methods for finding something in the admin tree.
656 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
658 interface part_of_admin_tree {
661 * Finds a named part_of_admin_tree.
663 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
664 * and not parentable_part_of_admin_tree, then this function should only check if
665 * $this->name matches $name. If it does, it should return a reference to $this,
666 * otherwise, it should return a reference to NULL.
668 * If a class inherits parentable_part_of_admin_tree, this method should be called
669 * recursively on all child objects (assuming, of course, the parent object's name
670 * doesn't match the search criterion).
672 * @param string $name The internal name of the part_of_admin_tree we're searching for.
673 * @return mixed An object reference or a NULL reference.
675 public function locate($name);
678 * Removes named part_of_admin_tree.
680 * @param string $name The internal name of the part_of_admin_tree we want to remove.
681 * @return bool success.
683 public function prune($name);
687 * @param string $query
688 * @return mixed array-object structure of found settings and pages
690 public function search($query);
693 * Verifies current user's access to this part_of_admin_tree.
695 * Used to check if the current user has access to this part of the admin tree or
696 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
697 * then this method is usually just a call to has_capability() in the site context.
699 * If a class inherits parentable_part_of_admin_tree, this method should return the
700 * logical OR of the return of check_access() on all child objects.
702 * @return bool True if the user has access, false if she doesn't.
704 public function check_access();
707 * Mostly useful for removing of some parts of the tree in admin tree block.
709 * @return True is hidden from normal list view
711 public function is_hidden();
714 * Show we display Save button at the page bottom?
717 public function show_save();
722 * Interface implemented by any part_of_admin_tree that has children.
724 * The interface implemented by any part_of_admin_tree that can be a parent
725 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
726 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
727 * include an add method for adding other part_of_admin_tree objects as children.
729 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
731 interface parentable_part_of_admin_tree extends part_of_admin_tree {
734 * Adds a part_of_admin_tree object to the admin tree.
736 * Used to add a part_of_admin_tree object to this object or a child of this
737 * object. $something should only be added if $destinationname matches
738 * $this->name. If it doesn't, add should be called on child objects that are
739 * also parentable_part_of_admin_tree's.
741 * $something should be appended as the last child in the $destinationname. If the
742 * $beforesibling is specified, $something should be prepended to it. If the given
743 * sibling is not found, $something should be appended to the end of $destinationname
744 * and a developer debugging message should be displayed.
746 * @param string $destinationname The internal name of the new parent for $something.
747 * @param part_of_admin_tree $something The object to be added.
748 * @return bool True on success, false on failure.
750 public function add($destinationname, $something, $beforesibling = null);
756 * The object used to represent folders (a.k.a. categories) in the admin tree block.
758 * Each admin_category object contains a number of part_of_admin_tree objects.
760 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
762 class admin_category implements parentable_part_of_admin_tree {
764 /** @var part_of_admin_tree[] An array of part_of_admin_tree objects that are this object's children */
766 /** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */
768 /** @var string The displayed name for this category. Usually obtained through get_string() */
770 /** @var bool Should this category be hidden in admin tree block? */
772 /** @var mixed Either a string or an array or strings */
774 /** @var mixed Either a string or an array or strings */
777 /** @var array fast lookup category cache, all categories of one tree point to one cache */
778 protected $category_cache;
780 /** @var bool If set to true children will be sorted when calling {@link admin_category::get_children()} */
781 protected $sort = false;
782 /** @var bool If set to true children will be sorted in ascending order. */
783 protected $sortasc = true;
784 /** @var bool If set to true sub categories and pages will be split and then sorted.. */
785 protected $sortsplit = true;
786 /** @var bool $sorted True if the children have been sorted and don't need resorting */
787 protected $sorted = false;
790 * Constructor for an empty admin category
792 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
793 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
794 * @param bool $hidden hide category in admin tree block, defaults to false
796 public function __construct($name, $visiblename, $hidden=false) {
797 $this->children = array();
799 $this->visiblename = $visiblename;
800 $this->hidden = $hidden;
804 * Returns a reference to the part_of_admin_tree object with internal name $name.
806 * @param string $name The internal name of the object we want.
807 * @param bool $findpath initialize path and visiblepath arrays
808 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
811 public function locate($name, $findpath=false) {
812 if (!isset($this->category_cache[$this->name])) {
813 // somebody much have purged the cache
814 $this->category_cache[$this->name] = $this;
817 if ($this->name == $name) {
819 $this->visiblepath[] = $this->visiblename;
820 $this->path[] = $this->name;
825 // quick category lookup
826 if (!$findpath and isset($this->category_cache[$name])) {
827 return $this->category_cache[$name];
831 foreach($this->children as $childid=>$unused) {
832 if ($return = $this->children[$childid]->locate($name, $findpath)) {
837 if (!is_null($return) and $findpath) {
838 $return->visiblepath[] = $this->visiblename;
839 $return->path[] = $this->name;
848 * @param string query
849 * @return mixed array-object structure of found settings and pages
851 public function search($query) {
853 foreach ($this->get_children() as $child) {
854 $subsearch = $child->search($query);
855 if (!is_array($subsearch)) {
856 debugging('Incorrect search result from '.$child->name);
859 $result = array_merge($result, $subsearch);
865 * Removes part_of_admin_tree object with internal name $name.
867 * @param string $name The internal name of the object we want to remove.
868 * @return bool success
870 public function prune($name) {
872 if ($this->name == $name) {
873 return false; //can not remove itself
876 foreach($this->children as $precedence => $child) {
877 if ($child->name == $name) {
878 // clear cache and delete self
879 while($this->category_cache) {
880 // delete the cache, but keep the original array address
881 array_pop($this->category_cache);
883 unset($this->children[$precedence]);
885 } else if ($this->children[$precedence]->prune($name)) {
893 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
895 * By default the new part of the tree is appended as the last child of the parent. You
896 * can specify a sibling node that the new part should be prepended to. If the given
897 * sibling is not found, the part is appended to the end (as it would be by default) and
898 * a developer debugging message is displayed.
900 * @throws coding_exception if the $beforesibling is empty string or is not string at all.
901 * @param string $destinationame The internal name of the immediate parent that we want for $something.
902 * @param mixed $something A part_of_admin_tree or setting instance to be added.
903 * @param string $beforesibling The name of the parent's child the $something should be prepended to.
904 * @return bool True if successfully added, false if $something can not be added.
906 public function add($parentname, $something, $beforesibling = null) {
909 $parent = $this->locate($parentname);
910 if (is_null($parent)) {
911 debugging('parent does not exist!');
915 if ($something instanceof part_of_admin_tree) {
916 if (!($parent instanceof parentable_part_of_admin_tree)) {
917 debugging('error - parts of tree can be inserted only into parentable parts');
920 if ($CFG->debugdeveloper && !is_null($this->locate($something->name))) {
921 // The name of the node is already used, simply warn the developer that this should not happen.
922 // It is intentional to check for the debug level before performing the check.
923 debugging('Duplicate admin page name: ' . $something->name, DEBUG_DEVELOPER);
925 if (is_null($beforesibling)) {
926 // Append $something as the parent's last child.
927 $parent->children[] = $something;
929 if (!is_string($beforesibling) or trim($beforesibling) === '') {
930 throw new coding_exception('Unexpected value of the beforesibling parameter');
932 // Try to find the position of the sibling.
933 $siblingposition = null;
934 foreach ($parent->children as $childposition => $child) {
935 if ($child->name === $beforesibling) {
936 $siblingposition = $childposition;
940 if (is_null($siblingposition)) {
941 debugging('Sibling '.$beforesibling.' not found', DEBUG_DEVELOPER);
942 $parent->children[] = $something;
944 $parent->children = array_merge(
945 array_slice($parent->children, 0, $siblingposition),
947 array_slice($parent->children, $siblingposition)
951 if ($something instanceof admin_category) {
952 if (isset($this->category_cache[$something->name])) {
953 debugging('Duplicate admin category name: '.$something->name);
955 $this->category_cache[$something->name] = $something;
956 $something->category_cache =& $this->category_cache;
957 foreach ($something->children as $child) {
958 // just in case somebody already added subcategories
959 if ($child instanceof admin_category) {
960 if (isset($this->category_cache[$child->name])) {
961 debugging('Duplicate admin category name: '.$child->name);
963 $this->category_cache[$child->name] = $child;
964 $child->category_cache =& $this->category_cache;
973 debugging('error - can not add this element');
980 * Checks if the user has access to anything in this category.
982 * @return bool True if the user has access to at least one child in this category, false otherwise.
984 public function check_access() {
985 foreach ($this->children as $child) {
986 if ($child->check_access()) {
994 * Is this category hidden in admin tree block?
996 * @return bool True if hidden
998 public function is_hidden() {
999 return $this->hidden;
1003 * Show we display Save button at the page bottom?
1006 public function show_save() {
1007 foreach ($this->children as $child) {
1008 if ($child->show_save()) {
1016 * Sets sorting on this category.
1018 * Please note this function doesn't actually do the sorting.
1019 * It can be called anytime.
1020 * Sorting occurs when the user calls get_children.
1021 * Code using the children array directly won't see the sorted results.
1023 * @param bool $sort If set to true children will be sorted, if false they won't be.
1024 * @param bool $asc If true sorting will be ascending, otherwise descending.
1025 * @param bool $split If true we sort pages and sub categories separately.
1027 public function set_sorting($sort, $asc = true, $split = true) {
1028 $this->sort = (bool)$sort;
1029 $this->sortasc = (bool)$asc;
1030 $this->sortsplit = (bool)$split;
1034 * Returns the children associated with this category.
1036 * @return part_of_admin_tree[]
1038 public function get_children() {
1039 // If we should sort and it hasn't already been sorted.
1040 if ($this->sort && !$this->sorted) {
1041 if ($this->sortsplit) {
1042 $categories = array();
1044 foreach ($this->children as $child) {
1045 if ($child instanceof admin_category) {
1046 $categories[] = $child;
1051 core_collator::asort_objects_by_property($categories, 'visiblename');
1052 core_collator::asort_objects_by_property($pages, 'visiblename');
1053 if (!$this->sortasc) {
1054 $categories = array_reverse($categories);
1055 $pages = array_reverse($pages);
1057 $this->children = array_merge($pages, $categories);
1059 core_collator::asort_objects_by_property($this->children, 'visiblename');
1060 if (!$this->sortasc) {
1061 $this->children = array_reverse($this->children);
1064 $this->sorted = true;
1066 return $this->children;
1070 * Magically gets a property from this object.
1073 * @return part_of_admin_tree[]
1074 * @throws coding_exception
1076 public function __get($property) {
1077 if ($property === 'children') {
1078 return $this->get_children();
1080 throw new coding_exception('Invalid property requested.');
1084 * Magically sets a property against this object.
1086 * @param string $property
1087 * @param mixed $value
1088 * @throws coding_exception
1090 public function __set($property, $value) {
1091 if ($property === 'children') {
1092 $this->sorted = false;
1093 $this->children = $value;
1095 throw new coding_exception('Invalid property requested.');
1100 * Checks if an inaccessible property is set.
1102 * @param string $property
1104 * @throws coding_exception
1106 public function __isset($property) {
1107 if ($property === 'children') {
1108 return isset($this->children);
1110 throw new coding_exception('Invalid property requested.');
1116 * Root of admin settings tree, does not have any parent.
1118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1120 class admin_root extends admin_category {
1121 /** @var array List of errors */
1123 /** @var string search query */
1125 /** @var bool full tree flag - true means all settings required, false only pages required */
1127 /** @var bool flag indicating loaded tree */
1129 /** @var mixed site custom defaults overriding defaults in settings files*/
1130 public $custom_defaults;
1133 * @param bool $fulltree true means all settings required,
1134 * false only pages required
1136 public function __construct($fulltree) {
1139 parent::__construct('root', get_string('administration'), false);
1140 $this->errors = array();
1142 $this->fulltree = $fulltree;
1143 $this->loaded = false;
1145 $this->category_cache = array();
1147 // load custom defaults if found
1148 $this->custom_defaults = null;
1149 $defaultsfile = "$CFG->dirroot/local/defaults.php";
1150 if (is_readable($defaultsfile)) {
1151 $defaults = array();
1152 include($defaultsfile);
1153 if (is_array($defaults) and count($defaults)) {
1154 $this->custom_defaults = $defaults;
1160 * Empties children array, and sets loaded to false
1162 * @param bool $requirefulltree
1164 public function purge_children($requirefulltree) {
1165 $this->children = array();
1166 $this->fulltree = ($requirefulltree || $this->fulltree);
1167 $this->loaded = false;
1168 //break circular dependencies - this helps PHP 5.2
1169 while($this->category_cache) {
1170 array_pop($this->category_cache);
1172 $this->category_cache = array();
1178 * Links external PHP pages into the admin tree.
1180 * See detailed usage example at the top of this document (adminlib.php)
1182 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1184 class admin_externalpage implements part_of_admin_tree {
1186 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1189 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1190 public $visiblename;
1192 /** @var string The external URL that we should link to when someone requests this external page. */
1195 /** @var string The role capability/permission a user must have to access this external page. */
1196 public $req_capability;
1198 /** @var object The context in which capability/permission should be checked, default is site context. */
1201 /** @var bool hidden in admin tree block. */
1204 /** @var mixed either string or array of string */
1207 /** @var array list of visible names of page parents */
1208 public $visiblepath;
1211 * Constructor for adding an external page into the admin tree.
1213 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1214 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1215 * @param string $url The external URL that we should link to when someone requests this external page.
1216 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1217 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1218 * @param stdClass $context The context the page relates to. Not sure what happens
1219 * if you specify something other than system or front page. Defaults to system.
1221 public function __construct($name, $visiblename, $url, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1222 $this->name = $name;
1223 $this->visiblename = $visiblename;
1225 if (is_array($req_capability)) {
1226 $this->req_capability = $req_capability;
1228 $this->req_capability = array($req_capability);
1230 $this->hidden = $hidden;
1231 $this->context = $context;
1235 * Returns a reference to the part_of_admin_tree object with internal name $name.
1237 * @param string $name The internal name of the object we want.
1238 * @param bool $findpath defaults to false
1239 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1241 public function locate($name, $findpath=false) {
1242 if ($this->name == $name) {
1244 $this->visiblepath = array($this->visiblename);
1245 $this->path = array($this->name);
1255 * This function always returns false, required function by interface
1257 * @param string $name
1260 public function prune($name) {
1265 * Search using query
1267 * @param string $query
1268 * @return mixed array-object structure of found settings and pages
1270 public function search($query) {
1272 if (strpos(strtolower($this->name), $query) !== false) {
1274 } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) {
1278 $result = new stdClass();
1279 $result->page = $this;
1280 $result->settings = array();
1281 return array($this->name => $result);
1288 * Determines if the current user has access to this external page based on $this->req_capability.
1290 * @return bool True if user has access, false otherwise.
1292 public function check_access() {
1294 $context = empty($this->context) ? context_system::instance() : $this->context;
1295 foreach($this->req_capability as $cap) {
1296 if (has_capability($cap, $context)) {
1304 * Is this external page hidden in admin tree block?
1306 * @return bool True if hidden
1308 public function is_hidden() {
1309 return $this->hidden;
1313 * Show we display Save button at the page bottom?
1316 public function show_save() {
1323 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1325 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1327 class admin_settingpage implements part_of_admin_tree {
1329 /** @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects */
1332 /** @var string The displayed name for this external page. Usually obtained through get_string(). */
1333 public $visiblename;
1335 /** @var mixed An array of admin_setting objects that are part of this setting page. */
1338 /** @var string The role capability/permission a user must have to access this external page. */
1339 public $req_capability;
1341 /** @var object The context in which capability/permission should be checked, default is site context. */
1344 /** @var bool hidden in admin tree block. */
1347 /** @var mixed string of paths or array of strings of paths */
1350 /** @var array list of visible names of page parents */
1351 public $visiblepath;
1354 * see admin_settingpage for details of this function
1356 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1357 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1358 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1359 * @param boolean $hidden Is this external page hidden in admin tree block? Default false.
1360 * @param stdClass $context The context the page relates to. Not sure what happens
1361 * if you specify something other than system or front page. Defaults to system.
1363 public function __construct($name, $visiblename, $req_capability='moodle/site:config', $hidden=false, $context=NULL) {
1364 $this->settings = new stdClass();
1365 $this->name = $name;
1366 $this->visiblename = $visiblename;
1367 if (is_array($req_capability)) {
1368 $this->req_capability = $req_capability;
1370 $this->req_capability = array($req_capability);
1372 $this->hidden = $hidden;
1373 $this->context = $context;
1377 * see admin_category
1379 * @param string $name
1380 * @param bool $findpath
1381 * @return mixed Object (this) if name == this->name, else returns null
1383 public function locate($name, $findpath=false) {
1384 if ($this->name == $name) {
1386 $this->visiblepath = array($this->visiblename);
1387 $this->path = array($this->name);
1397 * Search string in settings page.
1399 * @param string $query
1402 public function search($query) {
1405 foreach ($this->settings as $setting) {
1406 if ($setting->is_related($query)) {
1407 $found[] = $setting;
1412 $result = new stdClass();
1413 $result->page = $this;
1414 $result->settings = $found;
1415 return array($this->name => $result);
1419 if (strpos(strtolower($this->name), $query) !== false) {
1421 } else if (strpos(core_text::strtolower($this->visiblename), $query) !== false) {
1425 $result = new stdClass();
1426 $result->page = $this;
1427 $result->settings = array();
1428 return array($this->name => $result);
1435 * This function always returns false, required by interface
1437 * @param string $name
1438 * @return bool Always false
1440 public function prune($name) {
1445 * adds an admin_setting to this admin_settingpage
1447 * 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
1448 * n.b. each admin_setting in an admin_settingpage must have a unique internal name
1450 * @param object $setting is the admin_setting object you want to add
1451 * @return bool true if successful, false if not
1453 public function add($setting) {
1454 if (!($setting instanceof admin_setting)) {
1455 debugging('error - not a setting instance');
1459 $name = $setting->name;
1460 if ($setting->plugin) {
1461 $name = $setting->plugin . $name;
1463 $this->settings->{$name} = $setting;
1468 * see admin_externalpage
1470 * @return bool Returns true for yes false for no
1472 public function check_access() {
1474 $context = empty($this->context) ? context_system::instance() : $this->context;
1475 foreach($this->req_capability as $cap) {
1476 if (has_capability($cap, $context)) {
1484 * outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1485 * @return string Returns an XHTML string
1487 public function output_html() {
1488 $adminroot = admin_get_root();
1489 $return = '<fieldset>'."\n".'<div class="clearer"><!-- --></div>'."\n";
1490 foreach($this->settings as $setting) {
1491 $fullname = $setting->get_full_name();
1492 if (array_key_exists($fullname, $adminroot->errors)) {
1493 $data = $adminroot->errors[$fullname]->data;
1495 $data = $setting->get_setting();
1496 // do not use defaults if settings not available - upgrade settings handles the defaults!
1498 $return .= $setting->output_html($data);
1500 $return .= '</fieldset>';
1505 * Is this settings page hidden in admin tree block?
1507 * @return bool True if hidden
1509 public function is_hidden() {
1510 return $this->hidden;
1514 * Show we display Save button at the page bottom?
1517 public function show_save() {
1518 foreach($this->settings as $setting) {
1519 if (empty($setting->nosave)) {
1529 * Admin settings class. Only exists on setting pages.
1530 * Read & write happens at this level; no authentication.
1532 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1534 abstract class admin_setting {
1535 /** @var string unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins. */
1537 /** @var string localised name */
1538 public $visiblename;
1539 /** @var string localised long description in Markdown format */
1540 public $description;
1541 /** @var mixed Can be string or array of string */
1542 public $defaultsetting;
1544 public $updatedcallback;
1545 /** @var mixed can be String or Null. Null means main config table */
1546 public $plugin; // null means main config table
1547 /** @var bool true indicates this setting does not actually save anything, just information */
1548 public $nosave = false;
1549 /** @var bool if set, indicates that a change to this setting requires rebuild course cache */
1550 public $affectsmodinfo = false;
1551 /** @var array of admin_setting_flag - These are extra checkboxes attached to a setting. */
1552 private $flags = array();
1553 /** @var bool Whether this field must be forced LTR. */
1554 private $forceltr = null;
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 is_callable($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) {
1908 * Get whether this should be displayed in LTR mode.
1912 public function get_force_ltr() {
1913 return $this->forceltr;
1917 * Set whether to force LTR or not.
1919 * @param bool $value True when forced, false when not force, null when unknown.
1921 public function set_force_ltr($value) {
1922 $this->forceltr = $value;
1927 * An additional option that can be applied to an admin setting.
1928 * The currently supported options are 'ADVANCED' and 'LOCKED'.
1930 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1932 class admin_setting_flag {
1933 /** @var bool Flag to indicate if this option can be toggled for this setting */
1934 private $enabled = false;
1935 /** @var bool Flag to indicate if this option defaults to true or false */
1936 private $default = false;
1937 /** @var string Short string used to create setting name - e.g. 'adv' */
1938 private $shortname = '';
1939 /** @var string String used as the label for this flag */
1940 private $displayname = '';
1941 /** @const Checkbox for this flag is displayed in admin page */
1942 const ENABLED = true;
1943 /** @const Checkbox for this flag is not displayed in admin page */
1944 const DISABLED = false;
1949 * @param bool $enabled Can this option can be toggled.
1950 * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED.
1951 * @param bool $default The default checked state for this setting option.
1952 * @param string $shortname The shortname of this flag. Currently supported flags are 'locked' and 'adv'
1953 * @param string $displayname The displayname of this flag. Used as a label for the flag.
1955 public function __construct($enabled, $default, $shortname, $displayname) {
1956 $this->shortname = $shortname;
1957 $this->displayname = $displayname;
1958 $this->set_options($enabled, $default);
1962 * Update the values of this setting options class
1964 * @param bool $enabled Can this option can be toggled.
1965 * Should be one of admin_setting_flag::ENABLED or admin_setting_flag::DISABLED.
1966 * @param bool $default The default checked state for this setting option.
1968 public function set_options($enabled, $default) {
1969 $this->enabled = $enabled;
1970 $this->default = $default;
1974 * Should this option appear in the interface and be toggleable?
1976 * @return bool Is it enabled?
1978 public function is_enabled() {
1979 return $this->enabled;
1983 * Should this option be checked by default?
1985 * @return bool Is it on by default?
1987 public function get_default() {
1988 return $this->default;
1992 * Return the short name for this flag. e.g. 'adv' or 'locked'
1996 public function get_shortname() {
1997 return $this->shortname;
2001 * Return the display name for this flag. e.g. 'Advanced' or 'Locked'
2005 public function get_displayname() {
2006 return $this->displayname;
2010 * Save the submitted data for this flag - or set it to the default if $data is null.
2012 * @param admin_setting $setting - The admin setting for this flag
2013 * @param array $data - The data submitted from the form or null to set the default value for new installs.
2016 public function write_setting_flag(admin_setting $setting, $data) {
2018 if ($this->is_enabled()) {
2019 if (!isset($data)) {
2020 $value = $this->get_default();
2022 $value = !empty($data[$setting->get_full_name() . '_' . $this->get_shortname()]);
2024 $result = $setting->config_write($setting->name . '_' . $this->get_shortname(), $value);
2032 * Output the checkbox for this setting flag. Should only be called if the flag is enabled.
2034 * @param admin_setting $setting - The admin setting for this flag
2035 * @return string - The html for the checkbox.
2037 public function output_setting_flag(admin_setting $setting) {
2040 $value = $setting->get_setting_flag_value($this);
2042 $context = new stdClass();
2043 $context->id = $setting->get_id() . '_' . $this->get_shortname();
2044 $context->name = $setting->get_full_name() . '_' . $this->get_shortname();
2045 $context->value = 1;
2046 $context->checked = $value ? true : false;
2047 $context->label = $this->get_displayname();
2049 return $OUTPUT->render_from_template('core_admin/setting_flag', $context);
2055 * No setting - just heading and text.
2057 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2059 class admin_setting_heading extends admin_setting {
2062 * not a setting, just text
2063 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2064 * @param string $heading heading
2065 * @param string $information text in box
2067 public function __construct($name, $heading, $information) {
2068 $this->nosave = true;
2069 parent::__construct($name, $heading, $information, '');
2073 * Always returns true
2074 * @return bool Always returns true
2076 public function get_setting() {
2081 * Always returns true
2082 * @return bool Always returns true
2084 public function get_defaultsetting() {
2089 * Never write settings
2090 * @return string Always returns an empty string
2092 public function write_setting($data) {
2093 // do not write any setting
2098 * Returns an HTML string
2099 * @return string Returns an HTML string
2101 public function output_html($data, $query='') {
2103 $context = new stdClass();
2104 $context->title = $this->visiblename;
2105 $context->description = $this->description;
2106 $context->descriptionformatted = highlight($query, markdown_to_html($this->description));
2107 return $OUTPUT->render_from_template('core_admin/setting_heading', $context);
2113 * The most flexible setting, the user enters text.
2115 * This type of field should be used for config settings which are using
2116 * English words and are not localised (passwords, database name, list of values, ...).
2118 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2120 class admin_setting_configtext extends admin_setting {
2122 /** @var mixed int means PARAM_XXX type, string is a allowed format in regex */
2124 /** @var int default field size */
2128 * Config text constructor
2130 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2131 * @param string $visiblename localised
2132 * @param string $description long localised info
2133 * @param string $defaultsetting
2134 * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex
2135 * @param int $size default field size
2137 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $size=null) {
2138 $this->paramtype = $paramtype;
2139 if (!is_null($size)) {
2140 $this->size = $size;
2142 $this->size = ($paramtype === PARAM_INT) ? 5 : 30;
2144 parent::__construct($name, $visiblename, $description, $defaultsetting);
2148 * Get whether this should be displayed in LTR mode.
2150 * Try to guess from the PARAM type unless specifically set.
2152 public function get_force_ltr() {
2153 $forceltr = parent::get_force_ltr();
2154 if ($forceltr === null) {
2155 return !is_rtl_compatible($this->paramtype);
2161 * Return the setting
2163 * @return mixed returns config if successful else null
2165 public function get_setting() {
2166 return $this->config_read($this->name);
2169 public function write_setting($data) {
2170 if ($this->paramtype === PARAM_INT and $data === '') {
2171 // do not complain if '' used instead of 0
2174 // $data is a string
2175 $validated = $this->validate($data);
2176 if ($validated !== true) {
2179 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2183 * Validate data before storage
2184 * @param string data
2185 * @return mixed true if ok string if error found
2187 public function validate($data) {
2188 // allow paramtype to be a custom regex if it is the form of /pattern/
2189 if (preg_match('#^/.*/$#', $this->paramtype)) {
2190 if (preg_match($this->paramtype, $data)) {
2193 return get_string('validateerror', 'admin');
2196 } else if ($this->paramtype === PARAM_RAW) {
2200 $cleaned = clean_param($data, $this->paramtype);
2201 if ("$data" === "$cleaned") { // implicit conversion to string is needed to do exact comparison
2204 return get_string('validateerror', 'admin');
2210 * Return an XHTML string for the setting
2211 * @return string Returns an XHTML string
2213 public function output_html($data, $query='') {
2216 $default = $this->get_defaultsetting();
2217 $context = (object) [
2218 'size' => $this->size,
2219 'id' => $this->get_id(),
2220 'name' => $this->get_full_name(),
2222 'forceltr' => $this->get_force_ltr(),
2224 $element = $OUTPUT->render_from_template('core_admin/setting_configtext', $context);
2226 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query);
2231 * Text input with a maximum length constraint.
2233 * @copyright 2015 onwards Ankit Agarwal
2234 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2236 class admin_setting_configtext_with_maxlength extends admin_setting_configtext {
2238 /** @var int maximum number of chars allowed. */
2239 protected $maxlength;
2242 * Config text constructor
2244 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
2245 * or 'myplugin/mysetting' for ones in config_plugins.
2246 * @param string $visiblename localised
2247 * @param string $description long localised info
2248 * @param string $defaultsetting
2249 * @param mixed $paramtype int means PARAM_XXX type, string is a allowed format in regex
2250 * @param int $size default field size
2251 * @param mixed $maxlength int maxlength allowed, 0 for infinite.
2253 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW,
2254 $size=null, $maxlength = 0) {
2255 $this->maxlength = $maxlength;
2256 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $size);
2260 * Validate data before storage
2262 * @param string $data data
2263 * @return mixed true if ok string if error found
2265 public function validate($data) {
2266 $parentvalidation = parent::validate($data);
2267 if ($parentvalidation === true) {
2268 if ($this->maxlength > 0) {
2269 // Max length check.
2270 $length = core_text::strlen($data);
2271 if ($length > $this->maxlength) {
2272 return get_string('maximumchars', 'moodle', $this->maxlength);
2276 return true; // No max length check needed.
2279 return $parentvalidation;
2285 * General text area without html editor.
2287 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2289 class admin_setting_configtextarea extends admin_setting_configtext {
2294 * @param string $name
2295 * @param string $visiblename
2296 * @param string $description
2297 * @param mixed $defaultsetting string or array
2298 * @param mixed $paramtype
2299 * @param string $cols The number of columns to make the editor
2300 * @param string $rows The number of rows to make the editor
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);
2309 * Returns an XHTML string for the editor
2311 * @param string $data
2312 * @param string $query
2313 * @return string XHTML string for the editor
2315 public function output_html($data, $query='') {
2318 $default = $this->get_defaultsetting();
2319 $defaultinfo = $default;
2320 if (!is_null($default) and $default !== '') {
2321 $defaultinfo = "\n".$default;
2324 $context = (object) [
2325 'cols' => $this->cols,
2326 'rows' => $this->rows,
2327 'id' => $this->get_id(),
2328 'name' => $this->get_full_name(),
2330 'forceltr' => $this->get_force_ltr(),
2332 $element = $OUTPUT->render_from_template('core_admin/setting_configtextarea', $context);
2334 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query);
2339 * General text area with html editor.
2341 class admin_setting_confightmleditor extends admin_setting_configtextarea {
2344 * @param string $name
2345 * @param string $visiblename
2346 * @param string $description
2347 * @param mixed $defaultsetting string or array
2348 * @param mixed $paramtype
2350 public function __construct($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW, $cols='60', $rows='8') {
2351 parent::__construct($name, $visiblename, $description, $defaultsetting, $paramtype, $cols, $rows);
2352 $this->set_force_ltr(false);
2353 editors_head_setup();
2357 * Returns an XHTML string for the editor
2359 * @param string $data
2360 * @param string $query
2361 * @return string XHTML string for the editor
2363 public function output_html($data, $query='') {
2364 $editor = editors_get_preferred_editor(FORMAT_HTML);
2365 $editor->set_text($data);
2366 $editor->use_editor($this->get_id(), array('noclean'=>true));
2367 return parent::output_html($data, $query);
2373 * Password field, allows unmasking of password
2375 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2377 class admin_setting_configpasswordunmask extends admin_setting_configtext {
2381 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2382 * @param string $visiblename localised
2383 * @param string $description long localised info
2384 * @param string $defaultsetting default password
2386 public function __construct($name, $visiblename, $description, $defaultsetting) {
2387 parent::__construct($name, $visiblename, $description, $defaultsetting, PARAM_RAW, 30);
2391 * Log config changes if necessary.
2392 * @param string $name
2393 * @param string $oldvalue
2394 * @param string $value
2396 protected function add_to_config_log($name, $oldvalue, $value) {
2397 if ($value !== '') {
2398 $value = '********';
2400 if ($oldvalue !== '' and $oldvalue !== null) {
2401 $oldvalue = '********';
2403 parent::add_to_config_log($name, $oldvalue, $value);
2407 * Returns HTML for the field.
2409 * @param string $data Value for the field
2410 * @param string $query Passed as final argument for format_admin_setting
2411 * @return string Rendered HTML
2413 public function output_html($data, $query='') {
2415 $context = (object) [
2416 'id' => $this->get_id(),
2417 'name' => $this->get_full_name(),
2418 'size' => $this->size,
2420 'forceltr' => $this->get_force_ltr(),
2422 $element = $OUTPUT->render_from_template('core_admin/setting_configpasswordunmask', $context);
2423 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', null, $query);
2429 * Empty setting used to allow flags (advanced) on settings that can have no sensible default.
2430 * Note: Only advanced makes sense right now - locked does not.
2432 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2434 class admin_setting_configempty extends admin_setting_configtext {
2437 * @param string $name
2438 * @param string $visiblename
2439 * @param string $description
2441 public function __construct($name, $visiblename, $description) {
2442 parent::__construct($name, $visiblename, $description, '', PARAM_RAW);
2446 * Returns an XHTML string for the hidden field
2448 * @param string $data
2449 * @param string $query
2450 * @return string XHTML string for the editor
2452 public function output_html($data, $query='') {
2455 $context = (object) [
2456 'id' => $this->get_id(),
2457 'name' => $this->get_full_name()
2459 $element = $OUTPUT->render_from_template('core_admin/setting_configempty', $context);
2461 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', get_string('none'), $query);
2469 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2471 class admin_setting_configfile extends admin_setting_configtext {
2474 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2475 * @param string $visiblename localised
2476 * @param string $description long localised info
2477 * @param string $defaultdirectory default directory location
2479 public function __construct($name, $visiblename, $description, $defaultdirectory) {
2480 parent::__construct($name, $visiblename, $description, $defaultdirectory, PARAM_RAW, 50);
2484 * Returns XHTML for the field
2486 * Returns XHTML for the field and also checks whether the file
2487 * specified in $data exists using file_exists()
2489 * @param string $data File name and path to use in value attr
2490 * @param string $query
2491 * @return string XHTML field
2493 public function output_html($data, $query='') {
2494 global $CFG, $OUTPUT;
2496 $default = $this->get_defaultsetting();
2497 $context = (object) [
2498 'id' => $this->get_id(),
2499 'name' => $this->get_full_name(),
2500 'size' => $this->size,
2502 'showvalidity' => !empty($data),
2503 'valid' => $data && file_exists($data),
2504 'readonly' => !empty($CFG->preventexecpath),
2505 'forceltr' => $this->get_force_ltr(),
2508 if ($context->readonly) {
2509 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2512 $element = $OUTPUT->render_from_template('core_admin/setting_configfile', $context);
2514 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query);
2518 * Checks if execpatch has been disabled in config.php
2520 public function write_setting($data) {
2522 if (!empty($CFG->preventexecpath)) {
2523 if ($this->get_setting() === null) {
2524 // Use default during installation.
2525 $data = $this->get_defaultsetting();
2526 if ($data === null) {
2533 return parent::write_setting($data);
2540 * Path to executable file
2542 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2544 class admin_setting_configexecutable extends admin_setting_configfile {
2547 * Returns an XHTML field
2549 * @param string $data This is the value for the field
2550 * @param string $query
2551 * @return string XHTML field
2553 public function output_html($data, $query='') {
2554 global $CFG, $OUTPUT;
2555 $default = $this->get_defaultsetting();
2556 require_once("$CFG->libdir/filelib.php");
2558 $context = (object) [
2559 'id' => $this->get_id(),
2560 'name' => $this->get_full_name(),
2561 'size' => $this->size,
2563 'showvalidity' => !empty($data),
2564 'valid' => $data && file_exists($data) && !is_dir($data) && file_is_executable($data),
2565 'readonly' => !empty($CFG->preventexecpath),
2566 'forceltr' => $this->get_force_ltr()
2569 if (!empty($CFG->preventexecpath)) {
2570 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2573 $element = $OUTPUT->render_from_template('core_admin/setting_configexecutable', $context);
2575 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query);
2583 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2585 class admin_setting_configdirectory extends admin_setting_configfile {
2588 * Returns an XHTML field
2590 * @param string $data This is the value for the field
2591 * @param string $query
2592 * @return string XHTML
2594 public function output_html($data, $query='') {
2595 global $CFG, $OUTPUT;
2596 $default = $this->get_defaultsetting();
2598 $context = (object) [
2599 'id' => $this->get_id(),
2600 'name' => $this->get_full_name(),
2601 'size' => $this->size,
2603 'showvalidity' => !empty($data),
2604 'valid' => $data && file_exists($data) && is_dir($data),
2605 'readonly' => !empty($CFG->preventexecpath),
2606 'forceltr' => $this->get_force_ltr()
2609 if (!empty($CFG->preventexecpath)) {
2610 $this->visiblename .= '<div class="form-overridden">'.get_string('execpathnotallowed', 'admin').'</div>';
2613 $element = $OUTPUT->render_from_template('core_admin/setting_configdirectory', $context);
2615 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $default, $query);
2623 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2625 class admin_setting_configcheckbox extends admin_setting {
2626 /** @var string Value used when checked */
2628 /** @var string Value used when not checked */
2633 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2634 * @param string $visiblename localised
2635 * @param string $description long localised info
2636 * @param string $defaultsetting
2637 * @param string $yes value used when checked
2638 * @param string $no value used when not checked
2640 public function __construct($name, $visiblename, $description, $defaultsetting, $yes='1', $no='0') {
2641 parent::__construct($name, $visiblename, $description, $defaultsetting);
2642 $this->yes = (string)$yes;
2643 $this->no = (string)$no;
2647 * Retrieves the current setting using the objects name
2651 public function get_setting() {
2652 return $this->config_read($this->name);
2656 * Sets the value for the setting
2658 * Sets the value for the setting to either the yes or no values
2659 * of the object by comparing $data to yes
2661 * @param mixed $data Gets converted to str for comparison against yes value
2662 * @return string empty string or error
2664 public function write_setting($data) {
2665 if ((string)$data === $this->yes) { // convert to strings before comparison
2670 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
2674 * Returns an XHTML checkbox field
2676 * @param string $data If $data matches yes then checkbox is checked
2677 * @param string $query
2678 * @return string XHTML field
2680 public function output_html($data, $query='') {
2683 $context = (object) [
2684 'id' => $this->get_id(),
2685 'name' => $this->get_full_name(),
2687 'value' => $this->yes,
2688 'checked' => (string) $data === $this->yes,
2691 $default = $this->get_defaultsetting();
2692 if (!is_null($default)) {
2693 if ((string)$default === $this->yes) {
2694 $defaultinfo = get_string('checkboxyes', 'admin');
2696 $defaultinfo = get_string('checkboxno', 'admin');
2699 $defaultinfo = NULL;
2702 $element = $OUTPUT->render_from_template('core_admin/setting_configcheckbox', $context);
2704 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query);
2710 * Multiple checkboxes, each represents different value, stored in csv format
2712 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2714 class admin_setting_configmulticheckbox extends admin_setting {
2715 /** @var array Array of choices value=>label */
2719 * Constructor: uses parent::__construct
2721 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2722 * @param string $visiblename localised
2723 * @param string $description long localised info
2724 * @param array $defaultsetting array of selected
2725 * @param array $choices array of $value=>$label for each checkbox
2727 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2728 $this->choices = $choices;
2729 parent::__construct($name, $visiblename, $description, $defaultsetting);
2733 * This public function may be used in ancestors for lazy loading of choices
2735 * @todo Check if this function is still required content commented out only returns true
2736 * @return bool true if loaded, false if error
2738 public function load_choices() {
2740 if (is_array($this->choices)) {
2743 .... load choices here
2749 * Is setting related to query text - used when searching
2751 * @param string $query
2752 * @return bool true on related, false on not or failure
2754 public function is_related($query) {
2755 if (!$this->load_choices() or empty($this->choices)) {
2758 if (parent::is_related($query)) {
2762 foreach ($this->choices as $desc) {
2763 if (strpos(core_text::strtolower($desc), $query) !== false) {
2771 * Returns the current setting if it is set
2773 * @return mixed null if null, else an array
2775 public function get_setting() {
2776 $result = $this->config_read($this->name);
2778 if (is_null($result)) {
2781 if ($result === '') {
2784 $enabled = explode(',', $result);
2786 foreach ($enabled as $option) {
2787 $setting[$option] = 1;
2793 * Saves the setting(s) provided in $data
2795 * @param array $data An array of data, if not array returns empty str
2796 * @return mixed empty string on useless data or bool true=success, false=failed
2798 public function write_setting($data) {
2799 if (!is_array($data)) {
2800 return ''; // ignore it
2802 if (!$this->load_choices() or empty($this->choices)) {
2805 unset($data['xxxxx']);
2807 foreach ($data as $key => $value) {
2808 if ($value and array_key_exists($key, $this->choices)) {
2812 return $this->config_write($this->name, implode(',', $result)) ? '' : get_string('errorsetting', 'admin');
2816 * Returns XHTML field(s) as required by choices
2818 * Relies on data being an array should data ever be another valid vartype with
2819 * acceptable value this may cause a warning/error
2820 * if (!is_array($data)) would fix the problem
2822 * @todo Add vartype handling to ensure $data is an array
2824 * @param array $data An array of checked values
2825 * @param string $query
2826 * @return string XHTML field
2828 public function output_html($data, $query='') {
2831 if (!$this->load_choices() or empty($this->choices)) {
2835 $default = $this->get_defaultsetting();
2836 if (is_null($default)) {
2839 if (is_null($data)) {
2843 $context = (object) [
2844 'id' => $this->get_id(),
2845 'name' => $this->get_full_name(),
2849 $defaults = array();
2850 foreach ($this->choices as $key => $description) {
2851 if (!empty($default[$key])) {
2852 $defaults[] = $description;
2857 'checked' => !empty($data[$key]),
2858 'label' => highlightfast($query, $description)
2862 if (is_null($default)) {
2863 $defaultinfo = null;
2864 } else if (!empty($defaults)) {
2865 $defaultinfo = implode(', ', $defaults);
2867 $defaultinfo = get_string('none');
2870 $context->options = $options;
2871 $context->hasoptions = !empty($options);
2873 $element = $OUTPUT->render_from_template('core_admin/setting_configmulticheckbox', $context);
2875 return format_admin_setting($this, $this->visiblename, $element, $this->description, false, '', $defaultinfo, $query);
2882 * Multiple checkboxes 2, value stored as string 00101011
2884 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2886 class admin_setting_configmulticheckbox2 extends admin_setting_configmulticheckbox {
2889 * Returns the setting if set
2891 * @return mixed null if not set, else an array of set settings
2893 public function get_setting() {
2894 $result = $this->config_read($this->name);
2895 if (is_null($result)) {
2898 if (!$this->load_choices()) {
2901 $result = str_pad($result, count($this->choices), '0');
2902 $result = preg_split('//', $result, -1, PREG_SPLIT_NO_EMPTY);
2904 foreach ($this->choices as $key=>$unused) {
2905 $value = array_shift($result);
2914 * Save setting(s) provided in $data param
2916 * @param array $data An array of settings to save
2917 * @return mixed empty string for bad data or bool true=>success, false=>error
2919 public function write_setting($data) {
2920 if (!is_array($data)) {
2921 return ''; // ignore it
2923 if (!$this->load_choices() or empty($this->choices)) {
2927 foreach ($this->choices as $key=>$unused) {
2928 if (!empty($data[$key])) {
2934 return $this->config_write($this->name, $result) ? '' : get_string('errorsetting', 'admin');
2940 * Select one value from list
2942 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2944 class admin_setting_configselect extends admin_setting {
2945 /** @var array Array of choices value=>label */
2947 /** @var array Array of choices grouped using optgroups */
2952 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
2953 * @param string $visiblename localised
2954 * @param string $description long localised info
2955 * @param string|int $defaultsetting
2956 * @param array $choices array of $value=>$label for each selection
2958 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
2959 // Look for optgroup and single options.
2960 if (is_array($choices)) {
2961 $this->choices = [];
2962 foreach ($choices as $key => $val) {
2963 if (is_array($val)) {
2964 $this->optgroups[$key] = $val;
2965 $this->choices = array_merge($this->choices, $val);
2967 $this->choices[$key] = $val;
2972 parent::__construct($name, $visiblename, $description, $defaultsetting);
2976 * This function may be used in ancestors for lazy loading of choices
2978 * Override this method if loading of choices is expensive, such
2979 * as when it requires multiple db requests.
2981 * @return bool true if loaded, false if error
2983 public function load_choices() {
2985 if (is_array($this->choices)) {
2988 .... load choices here
2994 * Check if this is $query is related to a choice
2996 * @param string $query
2997 * @return bool true if related, false if not
2999 public function is_related($query) {
3000 if (parent::is_related($query)) {
3003 if (!$this->load_choices()) {
3006 foreach ($this->choices as $key=>$value) {
3007 if (strpos(core_text::strtolower($key), $query) !== false) {
3010 if (strpos(core_text::strtolower($value), $query) !== false) {
3018 * Return the setting
3020 * @return mixed returns config if successful else null
3022 public function get_setting() {
3023 return $this->config_read($this->name);
3029 * @param string $data
3030 * @return string empty of error string
3032 public function write_setting($data) {
3033 if (!$this->load_choices() or empty($this->choices)) {
3036 if (!array_key_exists($data, $this->choices)) {
3037 return ''; // ignore it
3040 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
3044 * Returns XHTML select field
3046 * Ensure the options are loaded, and generate the XHTML for the select
3047 * element and any warning message. Separating this out from output_html
3048 * makes it easier to subclass this class.
3050 * @param string $data the option to show as selected.
3051 * @param string $current the currently selected option in the database, null if none.
3052 * @param string $default the default selected option.
3053 * @return array the HTML for the select element, and a warning message.
3054 * @deprecated since Moodle 3.2
3056 public function output_select_html($data, $current, $default, $extraname = '') {
3057 debugging('The method admin_setting_configselect::output_select_html is depreacted, do not use any more.', DEBUG_DEVELOPER);
3061 * Returns XHTML select field and wrapping div(s)
3063 * @see output_select_html()
3065 * @param string $data the option to show as selected
3066 * @param string $query
3067 * @return string XHTML field and wrapping div
3069 public function output_html($data, $query='') {
3072 $default = $this->get_defaultsetting();
3073 $current = $this->get_setting();
3075 if (!$this->load_choices() || empty($this->choices)) {
3079 $context = (object) [
3080 'id' => $this->get_id(),
3081 'name' => $this->get_full_name(),
3084 if (!is_null($default) && array_key_exists($default, $this->choices)) {
3085 $defaultinfo = $this->choices[$default];
3087 $defaultinfo = NULL;
3092 if ($current === null) {
3094 } else if (empty($current) && (array_key_exists('', $this->choices) || array_key_exists(0, $this->choices))) {
3096 } else if (!array_key_exists($current, $this->choices)) {
3097 $warning = get_string('warningcurrentsetting', 'admin', $current);
3098 if (!is_null($default) && $data == $current) {
3099 $data = $default; // Use default instead of first value when showing the form.
3104 $template = 'core_admin/setting_configselect';
3106 if (!empty($this->optgroups)) {
3108 foreach ($this->optgroups as $label => $choices) {
3109 $optgroup = array('label' => $label, 'options' => []);
3110 foreach ($choices as $value => $name) {
3111 $optgroup['options'][] = [
3114 'selected' => (string) $value == $data
3116 unset($this->choices[$value]);
3118 $optgroups[] = $optgroup;
3120 $context->options = $options;
3121 $context->optgroups = $optgroups;
3122 $template = 'core_admin/setting_configselect_optgroup';
3125 foreach ($this->choices as $value => $name) {
3129 'selected' => (string) $value == $data
3132 $context->options = $options;
3134 $element = $OUTPUT->render_from_template($template, $context);
3136 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, $warning, $defaultinfo, $query);
3142 * Select multiple items from list
3144 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3146 class admin_setting_configmultiselect extends admin_setting_configselect {
3149 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
3150 * @param string $visiblename localised
3151 * @param string $description long localised info
3152 * @param array $defaultsetting array of selected items
3153 * @param array $choices array of $value=>$label for each list item
3155 public function __construct($name, $visiblename, $description, $defaultsetting, $choices) {
3156 parent::__construct($name, $visiblename, $description, $defaultsetting, $choices);
3160 * Returns the select setting(s)
3162 * @return mixed null or array. Null if no settings else array of setting(s)
3164 public function get_setting() {
3165 $result = $this->config_read($this->name);
3166 if (is_null($result)) {
3169 if ($result === '') {
3172 return explode(',', $result);
3176 * Saves setting(s) provided through $data
3178 * Potential bug in the works should anyone call with this function
3179 * using a vartype that is not an array
3181 * @param array $data
3183 public function write_setting($data) {
3184 if (!is_array($data)) {
3185 return ''; //ignore it
3187 if (!$this->load_choices() or empty($this->choices)) {
3191 unset($data['xxxxx']);
3194 foreach ($data as $value) {
3195 if (!array_key_exists($value, $this->choices)) {
3196 continue; // ignore it
3201 return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin'));
3205 * Is setting related to query text - used when searching
3207 * @param string $query
3208 * @return bool true if related, false if not
3210 public function is_related($query) {
3211 if (!$this->load_choices() or empty($this->choices)) {
3214 if (parent::is_related($query)) {
3218 foreach ($this->choices as $desc) {
3219 if (strpos(core_text::strtolower($desc), $query) !== false) {
3227 * Returns XHTML multi-select field
3229 * @todo Add vartype handling to ensure $data is an array
3230 * @param array $data Array of values to select by default
3231 * @param string $query
3232 * @return string XHTML multi-select field
3234 public function output_html($data, $query='') {
3237 if (!$this->load_choices() or empty($this->choices)) {
3241 $default = $this->get_defaultsetting();
3242 if (is_null($default)) {
3245 if (is_null($data)) {
3249 $context = (object) [
3250 'id' => $this->get_id(),
3251 'name' => $this->get_full_name(),
3252 'size' => min(10, count($this->choices))
3257 $template = 'core_admin/setting_configmultiselect';
3259 if (!empty($this->optgroups)) {
3261 foreach ($this->optgroups as $label => $choices) {
3262 $optgroup = array('label' => $label, 'options' => []);
3263 foreach ($choices as $value => $name) {
3264 if (in_array($value, $default)) {
3265 $defaults[] = $name;
3267 $optgroup['options'][] = [
3270 'selected' => in_array($value, $data)
3272 unset($this->choices[$value]);
3274 $optgroups[] = $optgroup;
3276 $context->optgroups = $optgroups;
3277 $template = 'core_admin/setting_configmultiselect_optgroup';
3280 foreach ($this->choices as $value => $name) {
3281 if (in_array($value, $default)) {
3282 $defaults[] = $name;
3287 'selected' => in_array($value, $data)
3290 $context->options = $options;
3292 if (is_null($default)) {
3293 $defaultinfo = NULL;
3294 } if (!empty($defaults)) {
3295 $defaultinfo = implode(', ', $defaults);
3297 $defaultinfo = get_string('none');
3300 $element = $OUTPUT->render_from_template($template, $context);
3302 return format_admin_setting($this, $this->visiblename, $element, $this->description, true, '', $defaultinfo, $query);
3309 * This is a liiitle bit messy. we're using two selects, but we're returning
3310 * them as an array named after $name (so we only use $name2 internally for the setting)
3312 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3314 class admin_setting_configtime extends admin_setting {
3315 /** @var string Used for setting second select (minutes) */
3320 * @param string $hoursname setting for hours
3321 * @param string $minutesname setting for hours
3322 * @param string $visiblename localised
3323 * @param string $description long localised info
3324 * @param array $defaultsetting array representing default time 'h'=>hours, 'm'=>minutes
3326 public function __construct($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
3327 $this->name2 = $minutesname;
3328 parent::__construct($hoursname, $visiblename, $description, $defaultsetting);
3332 * Get the selected time
3334 * @return mixed An array containing 'h'=>xx, 'm'=>xx, or null if not set
3336 public function get_setting() {
3337 $result1 = $this->config_read($this->name);
3338 $result2 = $this->config_read($this->name2);
3339 if (is_null($result1) or is_null($result2)) {
3343 return array('h' => $result1, 'm' => $result2);
3347 * Store the time (hours and minutes)
3349 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3350 * @return bool true if success, false if not
3352 public function write_setting($data) {
3353 if (!is_array($data)) {
3357 $result = $this->config_write($this->name, (int)$data['h']) && $this->config_write($this->name2, (int)$data['m']);
3358 return ($result ? '' : get_string('errorsetting', 'admin'));
3362 * Returns XHTML time select fields
3364 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3365 * @param string $query
3366 * @return string XHTML time select fields and wrapping div(s)
3368 public function output_html($data, $query='') {
3371 $default = $this->get_defaultsetting();
3372 if (is_array($default)) {
3373 $defaultinfo = $default['h'].':'.$default['m'];
3375 $defaultinfo = NULL;
3378 $context = (object) [
3379 'id' => $this->get_id(),
3380 'name' => $this->get_full_name(),
3381 'hours' => array_map(function($i) use ($data) {
3385 'selected' => $i == $data['h']
3388 'minutes' => array_map(function($i) use ($data) {
3392 'selected' => $i == $data['m']
3397 $element = $OUTPUT->render_from_template('core_admin/setting_configtime', $context);
3399 return format_admin_setting($this, $this->visiblename, $element, $this->description,
3400 $this->get_id() . 'h', '', $defaultinfo, $query);
3407 * Seconds duration setting.
3409 * @copyright 2012 Petr Skoda (http://skodak.org)
3410 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3412 class admin_setting_configduration extends admin_setting {
3414 /** @var int default duration unit */
3415 protected $defaultunit;
3419 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
3420 * or 'myplugin/mysetting' for ones in config_plugins.
3421 * @param string $visiblename localised name
3422 * @param string $description localised long description
3423 * @param mixed $defaultsetting string or array depending on implementation
3424 * @param int $defaultunit - day, week, etc. (in seconds)
3426 public function __construct($name, $visiblename, $description, $defaultsetting, $defaultunit = 86400) {
3427 if (is_number($defaultsetting)) {
3428 $defaultsetting = self::parse_seconds($defaultsetting);
3430 $units = self::get_units();
3431 if (isset($units[$defaultunit])) {
3432 $this->defaultunit = $defaultunit;
3434 $this->defaultunit = 86400;
3436 parent::__construct($name, $visiblename, $description, $defaultsetting);
3440 * Returns selectable units.
3444 protected static function get_units() {
3446 604800 => get_string('weeks'),
3447 86400 => get_string('days'),
3448 3600 => get_string('hours'),
3449 60 => get_string('minutes'),
3450 1 => get_string('seconds'),
3455 * Converts seconds to some more user friendly string.
3457 * @param int $seconds
3460 protected static function get_duration_text($seconds) {
3461 if (empty($seconds)) {
3462 return get_string('none');
3464 $data = self::parse_seconds($seconds);
3465 switch ($data['u']) {
3467 return get_string('numweeks', '', $data['v']);
3469 return get_string('numdays', '', $data['v']);
3471 return get_string('numhours', '', $data['v']);
3473 return get_string('numminutes', '', $data['v']);
3475 return get_string('numseconds', '', $data['v']*$data['u']);
3480 * Finds suitable units for given duration.
3482 * @param int $seconds
3485 protected static function parse_seconds($seconds) {
3486 foreach (self::get_units() as $unit => $unused) {
3487 if ($seconds % $unit === 0) {
3488 return array('v'=>(int)($seconds/$unit), 'u'=>$unit);
3491 return array('v'=>(int)$seconds, 'u'=>1);
3495 * Get the selected duration as array.
3497 * @return mixed An array containing 'v'=>xx, 'u'=>xx, or null if not set
3499 public function get_setting() {
3500 $seconds = $this->config_read($this->name);
3501 if (is_null($seconds)) {
3505 return self::parse_seconds($seconds);
3509 * Store the duration as seconds.
3511 * @param array $data Must be form 'h'=>xx, 'm'=>xx
3512 * @return bool true if success, false if not
3514 public function write_setting($data) {
3515 if (!is_array($data)) {
3519 $seconds = (int)($data['v']*$data['u']);
3521 return get_string('errorsetting', 'admin');
3524 $result = $this->config_write($this->name, $seconds);
3525 return ($result ? '' : get_string('errorsetting', 'admin'));
3529 * Returns duration text+select fields.
3531 * @param array $data Must be form 'v'=>xx, 'u'=>xx
3532 * @param string $query
3533 * @return string duration text+select fields and wrapping div(s)
3535 public function output_html($data, $query='') {
3538 $default = $this->get_defaultsetting();
3539 if (is_number($default)) {
3540 $defaultinfo = self::get_duration_text($default);
3541 } else if (is_array($default)) {
3542 $defaultinfo = self::get_duration_text($default['v']*$default['u']);
3544 $defaultinfo = null;
3547 $inputid = $this->get_id() . 'v';
3548 $units = self::get_units();
3549 $defaultunit = $this->defaultunit;
3551 $context = (object) [
3552 'id' => $this->get_id(),
3553 'name' => $this->get_full_name(),
3554 'value' => $data['v'],
3555 'options' => array_map(function($unit) use ($units, $data, $defaultunit) {
3558 'name' => $units[$unit],
3559 'selected' => ($data['v'] == 0 && $unit == $defaultunit) || $unit == $data['u']
3561 }, array_keys($units))
3564 $element = $OUTPUT->render_from_template('core_admin/setting_configduration', $context);
3566 return format_admin_setting($this, $this->visiblename, $element, $this->description, $inputid, '', $defaultinfo, $query);
3572 * Seconds duration setting with an advanced checkbox, that controls a additional
3573 * $name.'_adv' setting.
3575 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3576 * @copyright 2014 The Open University
3578 class admin_setting_configduration_with_advanced extends admin_setting_configduration {
3581 * @param string $name unique ascii name, either 'mysetting' for settings that in config,
3582 * or 'myplugin/mysetting' for ones in config_plugins.
3583 * @param string $visiblename localised name
3584 * @param string $description localised long description
3585 * @param array $defaultsetting array of int value, and bool whether it is
3586 * is advanced by default.
3587 * @param int $defaultunit - day, week, etc. (in seconds)
3589 public function __construct($name, $visiblename, $description, $defaultsetting, $defaultunit = 86400) {
3590 parent::__construct($name, $visiblename, $description, $defaultsetting['value'], $defaultunit);
3591 $this->set_advanced_flag_options(admin_setting_flag::ENABLED, !empty($defaultsetting['adv']));
3597 * Used to validate a textarea used for ip addresses
3599 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3600 * @copyright 2011 Petr Skoda (http://skodak.org)
3602 class admin_setting_configiplist extends admin_setting_configtextarea {
3605 * Validate the contents of the textarea as IP addresses
3607 * Used to validate a new line separated list of IP addresses collected from
3608 * a textarea control
3610 * @param string $data A list of IP Addresses separated by new lines
3611 * @return mixed bool true for success or string:error on failure
3613 public function validate($data) {
3615 $lines = explode("\n", $data);
3621 foreach ($lines as $line) {
3622 $tokens = explode('#', $line);
3623 $ip = trim($tokens[0]);
3627 if (preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}$#', $ip, $match) ||
3628 preg_match('#^(\d{1,3})(\.\d{1,3}){0,3}(\/\d{1,2})$#', $ip, $match) ||
3629 preg_match('#^(\d{1,3})(\.\d{1,3}){3}(-\d{1,3})$#', $ip, $match)) {
3638 return get_string('validateiperror', 'admin', join(', ', $badips));
3644 * Used to validate a textarea used for domain names, wildcard domain names and IP addresses/ranges (both IPv4 and IPv6 format).
3646 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3647 * @copyright 2016 Jake Dallimore (jrhdallimore@gmail.com)
3649 class admin_setting_configmixedhostiplist extends admin_setting_configtextarea {
3652 * Validate the contents of the textarea as either IP addresses, domain name or wildcard domain name (RFC 4592).
3653 * Used to validate a new line separated list of entries collected from a textarea control.
3655 * This setting provides support for internationalised domain names (IDNs), however, such UTF-8 names will be converted to
3656 * their ascii-compatible encoding (punycode) on save, and converted back to their UTF-8 representation when fetched
3657 * via the get_setting() method, which has been overriden.
3659 * @param string $data A list of FQDNs, DNS wildcard format domains, and IP addresses, separated by new lines.
3660 * @return mixed bool true for success or string:error on failure
3662 public function validate($data) {
3666 $entries = explode("\n", $data);
3669 foreach ($entries as $key => $entry) {
3670 $entry = trim($entry);
3671 if (empty($entry)) {
3672 return get_string('validateemptylineerror', 'admin');
3675 // Validate each string entry against the supported formats.
3676 if (\core\ip_utils::is_ip_address($entry) || \core\ip_utils::is_ipv6_range($entry)
3677 || \core\ip_utils::is_ipv4_range($entry) || \core\ip_utils::is_domain_name($entry)
3678 || \core\ip_utils::is_domain_matching_pattern($entry)) {
3682 // Otherwise, the entry is invalid.
3683 $badentries[] = $entry;
3687 return get_string('validateerrorlist', 'admin', join(', ', $badentries));
3693 * Convert any lines containing international domain names (IDNs) to their ascii-compatible encoding (ACE).
3695 * @param string $data the setting data, as sent from the web form.
3696 * @return string $data the setting data, with all IDNs converted (using punycode) to their ascii encoded version.
3698 protected function ace_encode($data) {
3702 $entries = explode("\n", $data);
3703 foreach ($entries as $key => $entry) {
3704 $entry = trim($entry);
3705 // This regex matches any string that has non-ascii character.
3706 if (preg_match('/[^\x00-\x7f]/', $entry)) {
3707 // If we can convert the unicode string to an idn, do so.
3708 // Otherwise, leave the original unicode string alone and let the validation function handle it (it will fail).
3709 $val = idn_to_ascii($entry, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
3710 $entries[$key] = $val ? $val : $entry;
3713 return implode("\n", $entries);
3717 * Decode any ascii-encoded domain names back to their utf-8 representation for display.
3719 * @param string $data the setting data, as found in the database.
3720 * @return string $data the setting data, with all ascii-encoded IDNs decoded back to their utf-8 representation.
3722 protected function ace_decode($data) {
3723 $entries = explode("\n", $data);
3724 foreach ($entries as $key => $entry) {
3725 $entry = trim($entry);
3726 if (strpos($entry, 'xn--') !== false) {
3727 $entries[$key] = idn_to_utf8($entry, IDNA_NONTRANSITIONAL_TO_ASCII, INTL_IDNA_VARIANT_UTS46);
3730 return implode("\n", $entries);
3734 * Override, providing utf8-decoding for ascii-encoded IDN strings.
3736 * @return mixed returns punycode-converted setting string if successful, else null.
3738 public function get_setting() {
3739 // Here, we need to decode any ascii-encoded IDNs back to their native, utf-8 representation.
3740 $data = $this->config_read($this->name);
3741 if (function_exists('idn_to_utf8') && !is_null($data)) {
3742 $data = $this->ace_decode($data);
3748 * Override, providing ascii-encoding for utf8 (native) IDN strings.
3750 * @param string $data
3753 public function write_setting($data) {
3754 if ($this->paramtype === PARAM_INT and $data === '') {
3755 // Do not complain if '' used instead of 0.
3759 // Try to convert any non-ascii domains to ACE prior to validation - we can't modify anything in validate!
3760 if (function_exists('idn_to_ascii')) {
3761 $data = $this->ace_encode($data);
3764 $validated = $this->validate($data);
3765 if ($validated !== true) {
3768 return ($this->config_write($this->name, $data) ? '' : get_string('errorsetting', 'admin'));
3773 * Used to validate a textarea used for port numbers.
3775 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3776 * @copyright 2016 Jake Dallimore (jrhdallimore@gmail.com)
3778 class admin_setting_configportlist extends admin_setting_configtextarea {
3781 * Validate the contents of the textarea as port numbers.
3782 * Used to validate a new line separated list of ports collected from a textarea control.
3784 * @param string $data A list of ports separated by new lines
3785 * @return mixed bool true for success or string:error on failure
3787 public function validate($data) {
3791 $ports = explode("\n", $data);
3793 foreach ($ports as $port) {
3794 $port = trim($port);
3796 return get_string('validateemptylineerror', 'admin');
3799 // Is the string a valid integer number?
3800 if (strval(intval($port)) !== $port || intval($port) <= 0) {
3801 $badentries[] = $port;
3805 return get_string('validateerrorlist', 'admin', $badentries);
3813 * An admin setting for selecting one or more users who have a capability
3814 * in the system context
3816 * An admin setting for selecting one or more users, who have a particular capability
3817 * in the system context. Warning, make sure the list will never be too long. There is
3818 * no paging or searching of this list.
3820 * To correctly get a list of users from this config setting, you need to call the
3821 * get_users_from_config($CFG->mysetting, $capability); function in moodlelib.php.
3823 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3825 class admin_setting_users_with_capability extends admin_setting_configmultiselect {
3826 /** @var string The capabilities name */
3827 protected $capability;
3828 /** @var int include admin users too */
3829 protected $includeadmins;
3834 * @param string $name unique ascii name, either 'mysetting' for settings that in config, or 'myplugin/mysetting' for ones in config_plugins.
3835 * @param string $visiblename localised name
3836 * @param string $description localised long description
3837 * @param array $defaultsetting array of usernames
3838 * @param string $capability string capability name.
3839 * @param bool $includeadmins include administrators
3841 function __construct($name, $visiblename, $description, $defaultsetting, $capability, $includeadmins = true) {
3842 $this->capability = $capability;
3843 $this->includeadmins = $includeadmins;
3844 parent::__construct($name, $visiblename, $description, $defaultsetting, NULL);
3848 * Load all of the uses who have the capability into choice array
3850 * @return bool Always returns true
3852 function load_choices() {
3853 if (is_array($this->choices)) {
3856 list($sort, $sortparams) = users_order_by_sql('u');
3857 if (!empty($sortparams)) {
3858 throw new coding_exception('users_order_by_sql returned some query parameters. ' .
3859 'This is unexpected, and a problem because there is no way to pass these ' .
3860 'parameters to get_users_by_capability. See MDL-34657.');
3862 $userfields = 'u.id, u.username, ' . get_all_user_name_fields(true, 'u');
3863 $users = get_users_by_capability(context_system::instance(), $this->capability, $userfields, $sort);
3864 $this->choices = array(
3865 '$@NONE@$' => get_string('nobody'),
3866 '$@ALL@$' => get_string('everyonewhocan', 'admin', get_capability_string($this->capability)),
3868 if ($this->includeadmins) {
3869 $admins = get_admins();
3870 foreach ($admins as $user) {
3871 $this->choices[$user->id] = fullname($user);
3874 if (is_array($users)) {
3875 foreach ($users as $user) {
3876 $this->choices[$user->id] = fullname($user);
3883 * Returns the default setting for class
3885 * @return mixed Array, or string. Empty string if no default
3887 public function get_defaultsetting() {
3888 $this->load_choices();
3889 $defaultsetting = parent::get_defaultsetting();
3890 if (empty($defaultsetting)) {
3891 return array('$@NONE@$');
3892 } else if (array_key_exists($defaultsetting, $this->choices)) {
3893 return $defaultsetting;
3900 * Returns the current setting
3902 * @return mixed array or string
3904 public function get_setting() {
3905 $result = parent::get_setting();
3906 if ($result === null) {
3907 // this is necessary for settings upgrade
3910 if (empty($result)) {
3911 $result = array('$@NONE@$');
3917 * Save the chosen setting provided as $data
3919 * @param array $data
3920 * @return mixed string or array
3922 public function write_setting($data) {
3923 // If all is selected, remove any explicit options.
3924 if (in_array('$@ALL@$', $data)) {
3925 $data = array('$@ALL@$');
3927 // None never needs to be written to the DB.
3928 if (in_array('$@NONE@$', $data)) {
3929 unset($data[array_search('$@NONE@$', $data)]);
3931 return parent::write_setting($data);
3937 * Special checkbox for calendar - resets SESSION vars.
3939 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3941 class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
3943 * Calls the parent::__construct with default values
3945 * name => calendar_adminseesall
3946 * visiblename => get_string('adminseesall', 'admin')
3947 * description => get_string('helpadminseesall', 'admin')
3948 * defaultsetting => 0
3950 public function __construct() {
3951 parent::__construct('calendar_adminseesall', get_string('adminseesall', 'admin'),
3952 get_string('helpadminseesall', 'admin'), '0');
3956 * Stores the setting passed in $data
3958 * @param mixed gets converted to string for comparison
3959 * @return string empty string or error message
3961 public function write_setting($data) {
3963 return parent::write_setting($data);
3968 * Special select for settings that are altered in setup.php and can not be altered on the fly
3970 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3972 class admin_setting_special_selectsetup extends admin_setting_configselect {
3974 * Reads the setting directly from the database
3978 public function get_setting() {
3979 // read directly from db!
3980 return get_config(NULL, $this->name);
3984 * Save the setting passed in $data
3986 * @param string $data The setting to save
3987 * @return string empty or error message
3989 public function write_setting($data) {
3991 // do not change active CFG setting!
3992 $current = $CFG->{$this->name};
3993 $result = parent::write_setting($data);
3994 $CFG->{$this->name} = $current;
4001 * Special select for frontpage - stores data in course table
4003 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4005 class admin_setting_sitesetselect extends admin_setting_configselect {
4007 * Returns the site name for the selected site
4010 * @return string The site name of the selected site
4012 public function get_setting() {
4013 $site = course_get_format(get_site())->get_course();
4014 return $site->{$this->name};
4018 * Updates the database and save the setting
4020 * @param string data
4021 * @return string empty or error message
4023 public function write_setting($data) {
4024 global $DB, $SITE, $COURSE;
4025 if (!in_array($data, array_keys($this->choices))) {
4026 return get_string('errorsetting', 'admin');
4028 $record = new stdClass();
4029 $record->id = SITEID;
4030 $temp = $this->name;
4031 $record->$temp = $data;
4032 $record->timemodified = time();
4034 course_get_format($SITE)->update_course_format_options($record);
4035 $DB->update_record('course', $record);
4038 $SITE = $DB->get_record('course', array('id'=>$SITE->id), '*', MUST_EXIST);
4039 if ($SITE->id == $COURSE->id) {
4042 format_base::reset_course_cache($SITE->id);
4051 * Select for blog's bloglevel setting: if set to 0, will set blog_menu
4054 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4056 class admin_setting_bloglevel extends admin_setting_configselect {
4058 * Updates the database and save the setting
4060 * @param string data
4061 * @return string empty or error message
4063 public function write_setting($data) {
4066 $blogblocks = $DB->get_records_select('block', "name LIKE 'blog_%' AND visible = 1");
4067 foreach ($blogblocks as $block) {
4068 $DB->set_field('block', 'visible', 0, array('id' => $block->id));
4071 // reenable all blocks only when switching from disabled blogs
4072 if (isset($CFG->bloglevel) and $CFG->bloglevel == 0) {
4073 $blogblocks = $DB->get_records_select('block', "name LIKE 'blog_%' AND visible = 0");
4074 foreach ($blogblocks as $block) {
4075 $DB->set_field('block', 'visible', 1, array('id' => $block->id));
4079 return parent::write_setting($data);
4085 * Special select - lists on the frontpage - hacky
4087 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4089 class admin_setting_courselist_frontpage extends admin_setting {
4090 /** @var array Array of choices value=>label */
4094 * Construct override, requires one param
4096 * @param bool $loggedin Is the user logged in
4098 public function __construct($loggedin) {
4100 require_once($CFG->dirroot.'/course/lib.php');
4101 $name = 'frontpage'.($loggedin ? 'loggedin' : '');
4102 $visiblename = get_string('frontpage'.($loggedin ? 'loggedin' : ''),'admin');
4103 $description = get_string('configfrontpage'.($loggedin ? 'loggedin' : ''),'admin');
4104 $defaults = array(FRONTPAGEALLCOURSELIST);
4105 parent::__construct($name, $visiblename, $description, $defaults);
4109 * Loads the choices available
4111 * @return bool always returns true
4113 public function load_choices() {
4114 if (is_array($this->choices)) {
4117 $this->choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
4118 FRONTPAGEALLCOURSELIST => get_string('frontpagecourselist'),
4119 FRONTPAGEENROLLEDCOURSELIST => get_string('frontpageenrolledcourselist'),
4120 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
4121 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
4122 FRONTPAGECOURSESEARCH => get_string('frontpagecoursesearch'),
4123 'none' => get_string('none'));
4124 if ($this->name === 'frontpage') {
4125 unset($this->choices[FRONTPAGEENROLLEDCOURSELIST]);
4131 * Returns the selected settings
4133 * @param mixed array or setting or null
4135 public function get_setting() {
4136 $result = $this->config_read($this->name);
4137 if (is_null($result)) {
4140 if ($result === '') {
4143 return explode(',', $result);
4147 * Save the selected options
4149 * @param array $data
4150 * @return mixed empty string (data is not an array) or bool true=success false=failure
4152 public function write_setting($data) {
4153 if (!is_array($data)) {
4156 $this->load_choices();
4158 foreach($data as $datum) {
4159 if ($datum == 'none' or !array_key_exists($datum, $this->choices)) {
4162 $save[$datum] = $datum; // no duplicates
4164 return ($this->config_write($this->name, implode(',', $save)) ? '' : get_string('errorsetting', 'admin'));
4168 * Return XHTML select field and wrapping div
4170 * @todo Add vartype handling to make sure $data is an array
4171 * @param array $data Array of elements to select by default
4172 * @return string XHTML select field and wrapping div
4174 public function output_html($data, $query='') {
4177 $this->load_choices();
4178 $currentsetting = array();
4179 foreach ($data as $key) {
4180 if ($key != 'none' and array_key_exists($key, $this->choices)) {
4181 $currentsetting[] = $key; // already selected first
4185 $context = (object) [
4186 'id' => $this->get_id(),
4187 'name' => $this->get_full_name(),
4190 $options = $this->choices;
4192 for ($i = 0; $i < count($this->choices) - 1; $i++) {
4193 if (!array_key_exists($i, $currentsetting)) {
4194 $currentsetting[$i] = 'none';
4198 'options' => array_map(function($option) use ($options, $currentsetting, $i) {
4200 'name' => $options[$option],
4202 'selected' => $currentsetting[$i] == $option
4204 }, array_keys($options))
4207 $context->selects = $selects;
4209 $element = $OUTPUT->render_from_template('core_admin/setting_courselist_frontpage', $context);
4211 return format_admin_setting($this, $this->visiblename, $element, $this->description, false, '', null, $query);
4217 * Special checkbox for frontpage - stores data in course table
4219 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4221 class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
4223 * Returns the current sites name
4227 public function get_setting() {
4228 $site = course_get_format(get_site())->get_course();
4229 return $site->{$this->name};
4233 * Save the selected setting
4235 * @param string $data The selected site
4236 * @return string empty string or error message
4238 public function write_setting($data) {
4239 global $DB, $SITE, $COURSE;
4240 $record = new stdClass();
4241 $record->id = $SITE->id;
4242 $record->{$this->name} = ($data == '1' ? 1 : 0);
4243 $record->timemodified = time();
4245 course_get_format($SITE)->update_course_format_options($record);
4246 $DB->update_record('course', $record);
4249 $SITE = $DB->get_record('course', array('id'=>$SITE->id), '*', MUST_EXIST);
4250 if ($SITE->id == $COURSE->id) {
4253 format_base::reset_course_cache($SITE->id);
4260 * Special text for frontpage - stores data in course table.
4261 * Empty string means not set here. Manual setting is required.
4263 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4265 class admin_setting_sitesettext extends admin_setting_configtext {
4270 public function __construct() {
4271 call_user_func_array(['parent', '__construct'], func_get_args());
4272 $this->set_force_ltr(false);
4276 * Return the current setting
4278 * @return mixed string or null
4280 public function get_setting() {
4281 $site = course_get_format(get_site())->get_course();
4282 return $site->{$this->name} != '' ? $site->{$this->name} : NULL;
4286 * Validate the selected data
4288 * @param string $data The selected value to validate
4289 * @return mixed true or message string
4291 public function validate($data) {
4293 $cleaned = clean_param($data, PARAM_TEXT);
4294 if ($cleaned === '') {
4295 return get_string('required');
4297 if ($this->name ==='shortname' &&
4298 $DB->record_exists_sql('SELECT id from {course} WHERE shortname = ? AND id <> ?', array($data, $SITE->id))) {
4299 return get_string('shortnametaken', 'error', $data);
4301 if ("$data" == "$cleaned") { // implicit conversion to string is needed to do exact comparison
4304 return get_string('validateerror', 'admin');
4309 * Save the selected setting
4311 * @param string $data The selected value
4312 * @return string empty or error message
4314 public function write_setting($data) {
4315 global $DB, $SITE, $COURSE;
4316 $data = trim($data);
4317 $validated = $this->validate($data);
4318 if ($validated !== true) {
4322 $record = new stdClass();
4323 $record->id = $SITE->id;
4324 $record->{$this->name} = $data;
4325 $record->timemodified = time();
4327 course_get_format($SITE)->update_course_format_options($record);
4328 $DB->update_record('course', $record);
4331 $SITE = $DB->get_record('course', array('id'=>$SITE->id), '*', MUST_EXIST);
4332 if ($SITE->id == $COURSE->id) {
4335 format_base::reset_course_cache($SITE->id);
4343 * Special text editor for site description.
4345 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4347 class admin_setting_special_frontpagedesc extends admin_setting_confightmleditor {