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