Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / adminlib.php
bloba3f1093afc01aab41c6b6351da5a3cb2daa3bbba
1 <?php
3 /**
4 * adminlib.php - Contains functions that only administrators will ever need to use
6 * @author Martin Dougiamas and many others
7 * @version $Id$
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package moodlecore
12 /**
13 * Upgrade plugins
15 * @uses $db
16 * @uses $CFG
17 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
18 * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
19 * @param string $return The url to prompt the user to continue to
21 function upgrade_plugins($type, $dir, $return) {
22 global $CFG, $db;
24 if (!$plugs = get_list_of_plugins($dir) ) {
25 debugging('No '.$type.' plugins installed!');
26 return false;
29 $updated_plugins = false;
30 $strpluginsetup = get_string('pluginsetup');
32 foreach ($plugs as $plug) {
34 $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
36 unset($plugin);
38 if (is_readable($fullplug .'/version.php')) {
39 include_once($fullplug .'/version.php'); // defines $plugin with version etc
40 } else {
41 continue; // Nothing to do.
44 $oldupgrade = false;
45 $newupgrade = false;
46 if (is_readable($fullplug . '/db/'. $CFG->dbtype . '.php')) {
47 include_once($fullplug . '/db/'. $CFG->dbtype . '.php'); // defines old upgrading function
48 $oldupgrade = true;
50 if (is_readable($fullplug . '/db/upgrade.php')) {
51 include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
52 $newupgrade = true;
55 if (!isset($plugin)) {
56 continue;
59 if (!empty($plugin->requires)) {
60 if ($plugin->requires > $CFG->version) {
61 $info = new object();
62 $info->pluginname = $plug;
63 $info->pluginversion = $plugin->version;
64 $info->currentmoodle = $CFG->version;
65 $info->requiremoodle = $plugin->requires;
66 if (!$updated_plugins) {
67 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
68 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
70 upgrade_log_start();
71 notify(get_string('pluginrequirementsnotmet', 'error', $info));
72 $updated_plugins = true;
73 continue;
77 $plugin->name = $plug; // The name MUST match the directory
79 $pluginversion = $type.'_'.$plug.'_version';
81 if (!isset($CFG->$pluginversion)) {
82 set_config($pluginversion, 0);
85 if ($CFG->$pluginversion == $plugin->version) {
86 // do nothing
87 } else if ($CFG->$pluginversion < $plugin->version) {
88 if (!$updated_plugins) {
89 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
90 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
92 $updated_plugins = true;
93 upgrade_log_start();
94 print_heading($dir.'/'. $plugin->name .' plugin needs upgrading');
95 $db->debug = true;
96 @set_time_limit(0); // To allow slow databases to complete the long SQL
98 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
99 /// Both old .sql files and new install.xml are supported
100 /// but we priorize install.xml (XMLDB) if present
101 $status = false;
102 if (file_exists($fullplug . '/db/install.xml')) {
103 $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
104 } else if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
105 $status = modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql'); //Old method
106 } else {
107 $status = true;
110 $db->debug = false;
111 /// Continue with the instalation, roles and other stuff
112 if ($status) {
113 /// OK so far, now update the plugins record
114 set_config($pluginversion, $plugin->version);
116 /// Install capabilities
117 if (!update_capabilities($type.'/'.$plug)) {
118 error('Could not set up the capabilities for '.$plugin->name.'!');
120 /// Install events
121 events_update_definition($type.'/'.$plug);
123 /// Run local install function if there is one
124 if (is_readable($fullplug .'/lib.php')) {
125 include_once($fullplug .'/lib.php');
126 $installfunction = $plugin->name.'_install';
127 if (function_exists($installfunction)) {
128 if (! $installfunction() ) {
129 notify('Encountered a problem running install function for '.$module->name.'!');
134 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
135 } else {
136 notify('Installing '. $plugin->name .' FAILED!');
138 } else { // Upgrade existing install
139 /// Run de old and new upgrade functions for the module
140 $oldupgrade_function = $type.'_'.$plugin->name .'_upgrade';
141 $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name .'_upgrade';
143 /// First, the old function if exists
144 $oldupgrade_status = true;
145 if ($oldupgrade && function_exists($oldupgrade_function)) {
146 $db->debug = true;
147 $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion);
148 } else if ($oldupgrade) {
149 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
150 $fullplug . '/db/' . $CFG->dbtype . '.php');
153 /// Then, the new function if exists and the old one was ok
154 $newupgrade_status = true;
155 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
156 $db->debug = true;
157 $newupgrade_status = $newupgrade_function($CFG->$pluginversion);
158 } else if ($newupgrade) {
159 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
160 $fullplug . '/db/upgrade.php');
163 $db->debug=false;
164 /// Now analyze upgrade results
165 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
166 // OK so far, now update the plugins record
167 set_config($pluginversion, $plugin->version);
168 if (!update_capabilities($type.'/'.$plug)) {
169 error('Could not update '.$plugin->name.' capabilities!');
171 events_update_definition($type.'/'.$plug);
172 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
173 } else {
174 notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
177 echo '<hr />';
178 } else {
179 upgrade_log_start();
180 error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
184 upgrade_log_finish();
186 if ($updated_plugins) {
187 print_continue($return);
188 print_footer('none');
189 die;
194 * Find and check all modules and load them up or upgrade them if necessary
196 * @uses $db
197 * @uses $CFG
198 * @param string $return The url to prompt the user to continue to
199 * @todo Finish documenting this function
201 function upgrade_activity_modules($return) {
203 global $CFG, $db;
205 if (!$mods = get_list_of_plugins('mod') ) {
206 error('No modules installed!');
209 $updated_modules = false;
210 $strmodulesetup = get_string('modulesetup');
212 foreach ($mods as $mod) {
214 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
215 continue;
218 $fullmod = $CFG->dirroot .'/mod/'. $mod;
220 unset($module);
222 if ( is_readable($fullmod .'/version.php')) {
223 include_once($fullmod .'/version.php'); // defines $module with version etc
224 } else {
225 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
226 continue;
229 $oldupgrade = false;
230 $newupgrade = false;
231 if ( is_readable($fullmod .'/db/' . $CFG->dbtype . '.php')) {
232 include_once($fullmod .'/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
233 $oldupgrade = true;
235 if ( is_readable($fullmod . '/db/upgrade.php')) {
236 include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
237 $newupgrade = true;
240 if (!isset($module)) {
241 continue;
244 if (!empty($module->requires)) {
245 if ($module->requires > $CFG->version) {
246 $info = new object();
247 $info->modulename = $mod;
248 $info->moduleversion = $module->version;
249 $info->currentmoodle = $CFG->version;
250 $info->requiremoodle = $module->requires;
251 if (!$updated_modules) {
252 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
253 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
255 upgrade_log_start();
256 notify(get_string('modulerequirementsnotmet', 'error', $info));
257 $updated_modules = true;
258 continue;
262 $module->name = $mod; // The name MUST match the directory
264 include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions
266 if ($currmodule = get_record('modules', 'name', $module->name)) {
267 if ($currmodule->version == $module->version) {
268 // do nothing
269 } else if ($currmodule->version < $module->version) {
270 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
271 if (!$oldupgrade && !$newupgrade) {
272 notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php or ' .
273 $fullmod . '/db/upgrade.php were not readable');
274 continue;
276 if (!$updated_modules) {
277 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
278 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
280 upgrade_log_start();
281 print_heading($module->name .' module needs upgrading');
283 /// Run de old and new upgrade functions for the module
284 $oldupgrade_function = $module->name . '_upgrade';
285 $newupgrade_function = 'xmldb_' . $module->name . '_upgrade';
287 /// First, the old function if exists
288 $oldupgrade_status = true;
289 if ($oldupgrade && function_exists($oldupgrade_function)) {
290 $db->debug = true;
291 $oldupgrade_status = $oldupgrade_function($currmodule->version, $module);
292 } else if ($oldupgrade) {
293 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
294 $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php');
297 /// Then, the new function if exists and the old one was ok
298 $newupgrade_status = true;
299 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
300 $db->debug = true;
301 $newupgrade_status = $newupgrade_function($currmodule->version, $module);
302 } else if ($newupgrade) {
303 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
304 $mod . ': ' . $fullmod . '/db/upgrade.php');
307 $db->debug=false;
308 /// Now analyze upgrade results
309 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
310 // OK so far, now update the modules record
311 $module->id = $currmodule->id;
312 if (! update_record('modules', $module)) {
313 error('Could not update '. $module->name .' record in modules table!');
315 remove_dir($CFG->dataroot . '/cache', true); // flush cache
316 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
317 echo '<hr />';
318 } else {
319 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
322 /// Update the capabilities table?
323 if (!update_capabilities('mod/'.$module->name)) {
324 error('Could not update '.$module->name.' capabilities!');
326 events_update_definition('mod/'.$module->name);
328 $updated_modules = true;
330 } else {
331 upgrade_log_start();
332 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
335 } else { // module not installed yet, so install it
336 if (!$updated_modules) {
337 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
338 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
340 upgrade_log_start();
341 print_heading($module->name);
342 $updated_modules = true;
343 $db->debug = true;
344 @set_time_limit(0); // To allow slow databases to complete the long SQL
346 /// Both old .sql files and new install.xml are supported
347 /// but we priorize install.xml (XMLDB) if present
348 if (file_exists($fullmod . '/db/install.xml')) {
349 $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
350 } else {
351 $status = modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql'); //Old method
354 $db->debug = false;
356 /// Continue with the installation, roles and other stuff
357 if ($status) {
358 if ($module->id = insert_record('modules', $module)) {
360 /// Capabilities
361 if (!update_capabilities('mod/'.$module->name)) {
362 error('Could not set up the capabilities for '.$module->name.'!');
365 /// Events
366 events_update_definition('mod/'.$module->name);
368 /// Run local install function if there is one
369 $installfunction = $module->name.'_install';
370 if (function_exists($installfunction)) {
371 if (! $installfunction() ) {
372 notify('Encountered a problem running install function for '.$module->name.'!');
376 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
377 echo '<hr />';
378 } else {
379 error($module->name .' module could not be added to the module list!');
381 } else {
382 error($module->name .' tables could NOT be set up successfully!');
386 /// Check submodules of this module if necessary
388 $submoduleupgrade = $module->name.'_upgrade_submodules';
389 if (function_exists($submoduleupgrade)) {
390 $submoduleupgrade();
394 /// Run any defaults or final code that is necessary for this module
396 if ( is_readable($fullmod .'/defaults.php')) {
397 // Insert default values for any important configuration variables
398 unset($defaults);
399 include_once($fullmod .'/defaults.php');
400 if (!empty($defaults)) {
401 foreach ($defaults as $name => $value) {
402 if (!isset($CFG->$name)) {
403 set_config($name, $value);
410 upgrade_log_finish(); // finish logging if started
412 if ($updated_modules) {
413 print_continue($return);
414 print_footer('none');
415 die;
420 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
422 * @param string $name ?
423 * @param bool $value ?
424 * @param int $staleafter ?
425 * @param bool $clobberstale ?
426 * @todo Finish documenting this function
428 function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
430 if (empty($name)) {
431 mtrace("Tried to get a cron lock for a null fieldname");
432 return false;
435 if (empty($value)) {
436 set_config($name,0);
437 return true;
440 if ($config = get_record('config','name',$name)) {
441 if (empty($config->value)) {
442 set_config($name,time());
443 } else {
444 // check for stale.
445 if ((time() - $staleafter) > $config->value) {
446 mtrace("STALE LOCKFILE FOR $name - was $config->value");
447 if (!empty($clobberstale)) {
448 set_config($name,time());
449 return true;
451 } else {
452 return false; // was not stale - ie, we're ok to still be running.
456 else {
457 set_config($name,time());
459 return true;
462 function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
463 static $starttime;
464 static $lasttime;
466 if ($total < 2) { // No need to show anything
467 return;
470 if (empty($starttime)) {
471 $starttime = $lasttime = time();
472 $lasttime = $starttime - $updatetime;
473 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
474 echo '<div id="bar'.$total.'" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
475 echo '<div id="slider'.$total.'" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
476 echo '</div>';
477 echo '<div id="text'.$total.'" align="center" style="width:500px;"></div>';
478 echo '</td></tr></table>';
479 echo '</div>';
482 $now = time();
484 if ($done && (($now - $lasttime) >= $updatetime)) {
485 $elapsedtime = $now - $starttime;
486 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
487 $percentage = format_float((float)$done / (float)$total, 2);
488 $width = (int)(500 * $percentage);
490 if ($projectedtime > 10) {
491 $projectedtext = ' Ending: '.format_time($projectedtime);
492 } else {
493 $projectedtext = '';
496 echo '<script>';
497 echo 'document.getElementById("text'.$total.'").innerHTML = "'.addslashes($donetext).' ('.$done.'/'.$total.') '.$projectedtext.'";'."\n";
498 echo 'document.getElementById("slider'.$total.'").style.width = \''.$width.'px\';'."\n";
499 echo '</script>';
501 $lasttime = $now;
502 sleep($sleeptime);
506 function upgrade_get_javascript() {
507 global $CFG;
509 if (!empty($_SESSION['installautopilot'])) {
510 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = true;</script>'."\n";
511 } else {
512 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = false;</script>'."\n";
514 $linktoscrolltoerrors .= '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>';
516 return $linktoscrolltoerrors;
519 function create_admin_user() {
520 global $CFG, $USER;
522 if (empty($CFG->rolesactive)) { // No admin user yet.
524 $user = new object();
525 $user->auth = 'manual';
526 $user->firstname = get_string('admin');
527 $user->lastname = get_string('user');
528 $user->username = 'admin';
529 $user->password = hash_internal_user_password('admin');
530 $user->email = 'root@localhost';
531 $user->confirmed = 1;
532 $user->mnethostid = $CFG->mnet_localhost_id;
533 $user->lang = $CFG->lang;
534 $user->maildisplay = 1;
535 $user->timemodified = time();
537 if (!$user->id = insert_record('user', $user)) {
538 error('SERIOUS ERROR: Could not create admin user record !!!');
541 if (!$user = get_record('user', 'id', $user->id)) { // Double check.
542 error('User ID was incorrect (can\'t find it)');
545 // Assign the default admin roles to the new user.
546 if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW)) {
547 error('No admin role could be found');
549 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
550 foreach ($adminroles as $adminrole) {
551 role_assign($adminrole->id, $user->id, 0, $sitecontext->id);
554 set_config('rolesactive', 1);
556 // Log the user in.
557 $USER = get_complete_user_data('username', 'admin');
558 $USER->newadminuser = 1;
559 load_all_capabilities();
561 redirect("$CFG->wwwroot/user/editadvanced.php?id=$user->id"); // Edit thyself
562 } else {
563 error('Can not create admin!');
567 ////////////////////////////////////////////////
568 /// upgrade logging functions
569 ////////////////////////////////////////////////
571 $upgradeloghandle = false;
572 $upgradelogbuffer = '';
573 // I did not find out how to use static variable in callback function,
574 // the problem was that I could not flush the static buffer :-(
575 global $upgradeloghandle, $upgradelogbuffer;
578 * Check if upgrade is already running.
580 * If anything goes wrong due to missing call to upgrade_log_finish()
581 * just restart the browser.
583 * @param string warning message indicating upgrade is already running
584 * @param int page reload timeout
586 function upgrade_check_running($message, $timeout) {
587 if (!empty($_SESSION['upgraderunning'])) {
588 print_header();
589 redirect(me(), $message, $timeout);
594 * Start logging of output into file (if not disabled) and
595 * prevent aborting and concurrent execution of upgrade script.
597 * Please note that you can not write into session variables after calling this function!
599 * This function may be called repeatedly.
601 function upgrade_log_start() {
602 global $CFG, $upgradeloghandle;
604 if (!empty($_SESSION['upgraderunning'])) {
605 return; // logging already started
608 @ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading
609 $_SESSION['upgraderunning'] = 1; // set upgrade indicator
610 if (empty($CFG->dbsessions)) { // workaround for bug in adodb, db session can not be restarted
611 session_write_close(); // from now on user can reload page - will be displayed warning
613 make_upload_directory('upgradelogs');
614 ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
615 register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging
619 * Terminate logging of output, flush all data, allow script aborting
620 * and reopen session for writing. Function error() does terminate the logging too.
622 * Please make sure that each upgrade_log_start() is properly terminated by
623 * this function or error().
625 * This function may be called repeatedly.
627 function upgrade_log_finish() {
628 global $CFG, $upgradeloghandle, $upgradelogbuffer;
630 if (empty($_SESSION['upgraderunning'])) {
631 return; // logging already terminated
634 @ob_end_flush();
635 if ($upgradelogbuffer !== '') {
636 @fwrite($upgradeloghandle, $upgradelogbuffer);
637 $upgradelogbuffer = '';
639 if ($upgradeloghandle and ($upgradeloghandle !== 'error')) {
640 @fclose($upgradeloghandle);
641 $upgradeloghandle = false;
643 if (empty($CFG->dbsessions)) {
644 @session_start(); // ignore header errors, we only need to reopen session
646 $_SESSION['upgraderunning'] = 0; // clear upgrade indicator
647 if (connection_aborted()) {
648 die;
650 @ignore_user_abort(false);
654 * Callback function for logging into files. Not more than one file is created per minute,
655 * upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
657 * This function must not output any characters or throw warnigns and errors!
659 function upgrade_log_callback($string) {
660 global $CFG, $upgradeloghandle, $upgradelogbuffer;
662 if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) {
663 if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
664 $upgradelogbuffer .= $string;
665 if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer
666 @fwrite($upgradeloghandle, $upgradelogbuffer);
667 $upgradelogbuffer = '';
669 } else {
670 $upgradeloghandle = 'error';
673 return $string;
677 * Try to verify that dataroot is not accessible from web.
678 * It is not 100% correct but might help to reduce number of vulnerable sites.
680 * Protection from httpd.conf and .htaccess is not detected properly.
682 function is_dataroot_insecure() {
683 global $CFG;
685 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
687 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
688 $rp = strrev(trim($rp, '/'));
689 $rp = explode('/', $rp);
690 foreach($rp as $r) {
691 if (strpos($siteroot, '/'.$r.'/') === 0) {
692 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
693 } else {
694 break; // probably alias root
698 $siteroot = strrev($siteroot);
699 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
701 if (strpos($dataroot, $siteroot) === 0) {
702 return true;
704 return false;
707 /// =============================================================================================================
708 /// administration tree classes and functions
711 // n.b. documentation is still in progress for this code
713 /// INTRODUCTION
715 /// This file performs the following tasks:
716 /// -it defines the necessary objects and interfaces to build the Moodle
717 /// admin hierarchy
718 /// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
719 /// and admin_externalpage_print_footer() functions used on admin pages
721 /// ADMIN_SETTING OBJECTS
723 /// Moodle settings are represented by objects that inherit from the admin_setting
724 /// class. These objects encapsulate how to read a setting, how to write a new value
725 /// to a setting, and how to appropriately display the HTML to modify the setting.
727 /// ADMIN_SETTINGPAGE OBJECTS
729 /// The admin_setting objects are then grouped into admin_settingpages. The latter
730 /// appear in the Moodle admin tree block. All interaction with admin_settingpage
731 /// objects is handled by the admin/settings.php file.
733 /// ADMIN_EXTERNALPAGE OBJECTS
735 /// There are some settings in Moodle that are too complex to (efficiently) handle
736 /// with admin_settingpages. (Consider, for example, user management and displaying
737 /// lists of users.) In this case, we use the admin_externalpage object. This object
738 /// places a link to an external PHP file in the admin tree block.
740 /// If you're using an admin_externalpage object for some settings, you can take
741 /// advantage of the admin_externalpage_* functions. For example, suppose you wanted
742 /// to add a foo.php file into admin. First off, you add the following line to
743 /// admin/settings/first.php (at the end of the file) or to some other file in
744 /// admin/settings:
746 /// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
747 /// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
749 /// Next, in foo.php, your file structure would resemble the following:
751 /// require_once('.../config.php');
752 /// require_once($CFG->libdir.'/adminlib.php');
753 /// admin_externalpage_setup('foo');
754 /// // functionality like processing form submissions goes here
755 /// admin_externalpage_print_header();
756 /// // your HTML goes here
757 /// admin_externalpage_print_footer();
759 /// The admin_externalpage_setup() function call ensures the user is logged in,
760 /// and makes sure that they have the proper role permission to access the page.
762 /// The admin_externalpage_print_header() function prints the header (it figures
763 /// out what category and subcategories the page is classified under) and ensures
764 /// that you're using the admin pagelib (which provides the admin tree block and
765 /// the admin bookmarks block).
767 /// The admin_externalpage_print_footer() function properly closes the tables
768 /// opened up by the admin_externalpage_print_header() function and prints the
769 /// standard Moodle footer.
771 /// ADMIN_CATEGORY OBJECTS
773 /// Above and beyond all this, we have admin_category objects. These objects
774 /// appear as folders in the admin tree block. They contain admin_settingpage's,
775 /// admin_externalpage's, and other admin_category's.
777 /// OTHER NOTES
779 /// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
780 /// from part_of_admin_tree (a pseudointerface). This interface insists that
781 /// a class has a check_access method for access permissions, a locate method
782 /// used to find a specific node in the admin tree, and a path method used
783 /// to determine the path to a specific node in the $ADMIN tree.
785 /// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
786 /// interface ensures that the class implements a recursive add function which
787 /// accepts a part_of_admin_tree object and searches for the proper place to
788 /// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
790 /// Please note that the $this->name field of any part_of_admin_tree must be
791 /// UNIQUE throughout the ENTIRE admin tree.
793 /// The $this->name field of an admin_setting object (which is *not* part_of_
794 /// admin_tree) must be unique on the respective admin_settingpage where it is
795 /// used.
798 /// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
799 include_once($CFG->dirroot . '/backup/lib.php');
801 /// CLASS DEFINITIONS /////////////////////////////////////////////////////////
804 * Pseudointerface for anything appearing in the admin tree
806 * The pseudointerface that is implemented by anything that appears in the admin tree
807 * block. It forces inheriting classes to define a method for checking user permissions
808 * and methods for finding something in the admin tree.
810 * @author Vincenzo K. Marcovecchio
811 * @package admin
813 class part_of_admin_tree {
816 * Finds a named part_of_admin_tree.
818 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
819 * and not parentable_part_of_admin_tree, then this function should only check if
820 * $this->name matches $name. If it does, it should return a reference to $this,
821 * otherwise, it should return a reference to NULL.
823 * If a class inherits parentable_part_of_admin_tree, this method should be called
824 * recursively on all child objects (assuming, of course, the parent object's name
825 * doesn't match the search criterion).
827 * @param string $name The internal name of the part_of_admin_tree we're searching for.
828 * @return mixed An object reference or a NULL reference.
830 function &locate($name) {
831 trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING);
832 return;
836 * Removes named part_of_admin_tree.
838 * @param string $name The internal name of the part_of_admin_tree we want to remove.
839 * @return bool success.
841 function prune($name) {
842 trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING);
843 return;
847 * Verifies current user's access to this part_of_admin_tree.
849 * Used to check if the current user has access to this part of the admin tree or
850 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
851 * then this method is usually just a call to has_capability() in the site context.
853 * If a class inherits parentable_part_of_admin_tree, this method should return the
854 * logical OR of the return of check_access() on all child objects.
856 * @return bool True if the user has access, false if she doesn't.
858 function check_access() {
859 trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING);
860 return;
864 * Mostly usefull for removing of some parts of the tree in admin tree block.
866 * @return True is hidden from normal list view
868 function is_hidden() {
869 trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING);
870 return;
874 * Determines the path to $name in the admin tree.
876 * Used to determine the path to $name in the admin tree. If a class inherits only
877 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
878 * check if $this->name matches $name. If it does, $name is pushed onto the $path
879 * array (at the end), and $path should be returned. If it doesn't, NULL should be
880 * returned.
882 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
883 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
884 * recursively calls path() on its child objects. If any are non-NULL, it should
885 * return $path (being certain that the last element of $path is equal to $name).
886 * If they are all NULL, it returns NULL.
888 * @param string $name The internal name of the part_of_admin_tree we're searching for.
889 * @param array $path Not used on external calls. Defaults to empty array.
890 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
892 function path($name, $path = array()) {
893 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING);
894 return;
899 * Pseudointerface implemented by any part_of_admin_tree that has children.
901 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
902 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
903 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
904 * include an add method for adding other part_of_admin_tree objects as children.
906 * @author Vincenzo K. Marcovecchio
907 * @package admin
909 class parentable_part_of_admin_tree extends part_of_admin_tree {
912 * Adds a part_of_admin_tree object to the admin tree.
914 * Used to add a part_of_admin_tree object to this object or a child of this
915 * object. $something should only be added if $destinationname matches
916 * $this->name. If it doesn't, add should be called on child objects that are
917 * also parentable_part_of_admin_tree's.
919 * @param string $destinationname The internal name of the new parent for $something.
920 * @param part_of_admin_tree &$something The object to be added.
921 * @return bool True on success, false on failure.
923 function add($destinationname, &$something) {
924 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING);
925 return;
931 * The object used to represent folders (a.k.a. categories) in the admin tree block.
933 * Each admin_category object contains a number of part_of_admin_tree objects.
935 * @author Vincenzo K. Marcovecchio
936 * @package admin
938 class admin_category extends parentable_part_of_admin_tree {
941 * @var mixed An array of part_of_admin_tree objects that are this object's children
943 var $children;
946 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
948 var $name;
951 * @var string The displayed name for this category. Usually obtained through get_string()
953 var $visiblename;
956 * @var bool Should this category be hidden in admin tree block?
958 var $hidden;
960 // constructor for an empty admin category
961 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
962 // $visiblename is the displayed name of the category. use a get_string for this
965 * Constructor for an empty admin category
967 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
968 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
969 * @param bool $hidden hide category in admin tree block
970 * @return mixed Returns the new object.
972 function admin_category($name, $visiblename, $hidden = false) {
973 $this->children = array();
974 $this->name = $name;
975 $this->visiblename = $visiblename;
976 $this->hidden = $hidden;
980 * Finds the path to the part_of_admin_tree called $name.
982 * @param string $name The internal name that we're searching for.
983 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
984 * @return mixed An array of internal names that leads to $name, or NULL if not found.
986 function path($name, $path = array()) {
988 $path[count($path)] = $this->name;
990 if ($this->name == $name) {
991 return $path;
994 foreach($this->children as $child) {
995 if ($return = $child->path($name, $path)) {
996 return $return;
1000 return NULL;
1005 * Returns a reference to the part_of_admin_tree object with internal name $name.
1007 * @param string $name The internal name of the object we want.
1008 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1010 function &locate($name) {
1012 if ($this->name == $name) {
1013 return $this;
1016 foreach($this->children as $child) {
1017 if ($return =& $child->locate($name)) {
1018 return $return;
1021 $return = NULL;
1022 return $return;
1026 * Removes part_of_admin_tree object with internal name $name.
1028 * @param string $name The internal name of the object we want to remove.
1029 * @return bool success
1031 function prune($name) {
1033 if ($this->name == $name) {
1034 return false; //can not remove itself
1037 foreach($this->children as $precedence => $child) {
1038 if ($child->name == $name) {
1039 // found it!
1040 unset($this->children[$precedence]);
1041 return true;
1043 if ($this->children[$precedence]->prune($name)) {
1044 return true;
1047 return false;
1051 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
1053 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
1054 * @param mixed &$something A part_of_admin_tree object to be added.
1055 * @param int $precedence The precedence of &$something when displayed. Smaller numbers mean it'll be displayed higher up in the admin menu. Defaults to '', meaning "next available position".
1056 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
1058 function add($destinationname, &$something, $precedence = '') {
1060 if (!is_a($something, 'part_of_admin_tree')) {
1061 return false;
1064 if ($destinationname == $this->name) {
1065 if ($precedence === '') {
1066 $this->children[] = $something;
1067 } else {
1068 if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
1069 echo ('<font style="color: red;">There is a precedence conflict in the category ' . $this->name . '. The object named ' . $something->name . ' is overwriting the object named ' . $this->children[$precedence]->name . '.</font><br />');
1071 $this->children[$precedence] = $something;
1073 return true;
1076 unset($entries);
1078 $entries = array_keys($this->children);
1080 foreach($entries as $entry) {
1081 $child =& $this->children[$entry];
1082 if (is_a($child, 'parentable_part_of_admin_tree')) {
1083 if ($child->add($destinationname, $something, $precedence)) {
1084 return true;
1089 return false;
1094 * Checks if the user has access to anything in this category.
1096 * @return bool True if the user has access to atleast one child in this category, false otherwise.
1098 function check_access() {
1100 $return = false;
1101 foreach ($this->children as $child) {
1102 $return = $return || $child->check_access();
1105 return $return;
1110 * Is this category hidden in admin tree block?
1112 * @return bool True if hidden
1114 function is_hidden() {
1115 return $this->hidden;
1120 * Links external PHP pages into the admin tree.
1122 * See detailed usage example at the top of this document (adminlib.php)
1124 * @author Vincenzo K. Marcovecchio
1125 * @package admin
1127 class admin_externalpage extends part_of_admin_tree {
1130 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1132 var $name;
1135 * @var string The displayed name for this external page. Usually obtained through get_string().
1137 var $visiblename;
1140 * @var string The external URL that we should link to when someone requests this external page.
1142 var $url;
1145 * @var string The role capability/permission a user must have to access this external page.
1147 var $req_capability;
1150 * @var object The context in which capability/permission should be checked, default is site context.
1152 var $context;
1155 * @var bool hidden in admin tree block.
1157 var $hidden;
1160 * Constructor for adding an external page into the admin tree.
1162 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1163 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1164 * @param string $url The external URL that we should link to when someone requests this external page.
1165 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
1167 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1168 $this->name = $name;
1169 $this->visiblename = $visiblename;
1170 $this->url = $url;
1171 if (is_array($req_capability)) {
1172 $this->req_capability = $req_capability;
1173 } else {
1174 $this->req_capability = array($req_capability);
1176 $this->hidden = $hidden;
1177 $this->context = $context;
1181 * Finds the path to the part_of_admin_tree called $name.
1183 * @param string $name The internal name that we're searching for.
1184 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1185 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1187 function path($name, $path = array()) {
1188 if ($name == $this->name) {
1189 array_push($path, $this->name);
1190 return $path;
1191 } else {
1192 return NULL;
1197 * Returns a reference to the part_of_admin_tree object with internal name $name.
1199 * @param string $name The internal name of the object we want.
1200 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1202 function &locate($name) {
1203 $return = ($this->name == $name ? $this : NULL);
1204 return $return;
1207 function prune($name) {
1208 return false;
1212 * Determines if the current user has access to this external page based on $this->req_capability.
1214 * @uses CONTEXT_SYSTEM
1215 * @uses SITEID
1216 * @return bool True if user has access, false otherwise.
1218 function check_access() {
1219 if (!get_site()) {
1220 return true; // no access check before site is fully set up
1222 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1223 foreach($this->req_capability as $cap) {
1224 if (has_capability($cap, $context)) {
1225 return true;
1228 return false;
1232 * Is this external page hidden in admin tree block?
1234 * @return bool True if hidden
1236 function is_hidden() {
1237 return $this->hidden;
1243 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1245 * @author Vincenzo K. Marcovecchio
1246 * @package admin
1248 class admin_settingpage extends part_of_admin_tree {
1251 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1253 var $name;
1256 * @var string The displayed name for this external page. Usually obtained through get_string().
1258 var $visiblename;
1260 * @var mixed An array of admin_setting objects that are part of this setting page.
1262 var $settings;
1265 * @var string The role capability/permission a user must have to access this external page.
1267 var $req_capability;
1270 * @var object The context in which capability/permission should be checked, default is site context.
1272 var $context;
1275 * @var bool hidden in admin tree block.
1277 var $hidden;
1279 // see admin_category
1280 function path($name, $path = array()) {
1281 if ($name == $this->name) {
1282 array_push($path, $this->name);
1283 return $path;
1284 } else {
1285 return NULL;
1289 // see admin_category
1290 function &locate($name) {
1291 $return = ($this->name == $name ? $this : NULL);
1292 return $return;
1295 function prune($name) {
1296 return false;
1299 // see admin_externalpage
1300 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
1301 global $CFG;
1302 $this->settings = new stdClass();
1303 $this->name = $name;
1304 $this->visiblename = $visiblename;
1305 if (is_array($req_capability)) {
1306 $this->req_capability = $req_capability;
1307 } else {
1308 $this->req_capability = array($req_capability);
1310 $this->hidden = false;
1311 $this->context = $context;
1314 // 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
1315 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1316 // &$setting is the admin_setting object you want to add
1317 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1318 function add(&$setting) {
1319 if (is_a($setting, 'admin_setting')) {
1320 $this->settings->{$setting->name} =& $setting;
1321 return true;
1323 return false;
1326 // see admin_externalpage
1327 function check_access() {
1328 if (!get_site()) {
1329 return true; // no access check before site is fully set up
1331 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
1332 foreach($this->req_capability as $cap) {
1333 if (has_capability($cap, $context)) {
1334 return true;
1337 return false;
1340 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1341 // returns a string of the html
1342 function output_html() {
1343 $return = '<fieldset>' . "\n";
1344 $return .= '<div class="clearer"><!-- --></div>' . "\n";
1345 foreach($this->settings as $setting) {
1346 $return .= $setting->output_html();
1348 $return .= '</fieldset>';
1349 return $return;
1352 // writes settings (the ones that have been added to this admin_settingpage) to the database, or wherever else they're supposed to be written to
1353 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1354 // $data should be the result from data_submitted()
1355 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1356 function write_settings($data) {
1357 $return = '';
1358 foreach($this->settings as $setting) {
1359 if (isset($data['s_' . $setting->name])) {
1360 $return .= $setting->write_setting($data['s_' . $setting->name]);
1361 } else {
1362 $return .= $setting->write_setting('');
1365 return $return;
1369 * Is this settigns page hidden in admin tree block?
1371 * @return bool True if hidden
1373 function is_hidden() {
1374 return $this->hidden;
1380 // read & write happens at this level; no authentication
1381 class admin_setting {
1383 var $name;
1384 var $visiblename;
1385 var $description;
1386 var $defaultsetting;
1388 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1389 $this->name = $name;
1390 $this->visiblename = $visiblename;
1391 $this->description = $description;
1392 $this->defaultsetting = $defaultsetting;
1395 function get_setting() {
1396 return NULL; // has to be overridden
1399 function write_setting($data) {
1400 return; // has to be overridden
1403 function output_html() {
1404 return; // has to be overridden
1410 class admin_setting_configtext extends admin_setting {
1412 var $paramtype;
1414 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
1415 $this->paramtype = $paramtype;
1416 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1419 // returns a string or NULL
1420 function get_setting() {
1421 global $CFG;
1422 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1425 // $data is a string
1426 function write_setting($data) {
1427 if (!$this->validate($data)) {
1428 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1430 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1433 function validate($data) {
1434 if (is_string($this->paramtype)) {
1435 return preg_match($this->paramtype, $data);
1436 } else if ($this->paramtype === PARAM_RAW) {
1437 return true;
1438 } else {
1439 $cleaned = clean_param($data, $this->paramtype);
1440 return ("$data" == "$cleaned"); // implicit conversion to string is needed to do exact comparison
1444 function output_html() {
1445 if ($this->get_setting() === NULL) {
1446 $current = $this->defaultsetting;
1447 } else {
1448 $current = $this->get_setting();
1450 return format_admin_setting($this->name, $this->visiblename,
1451 '<input type="text" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />',
1452 $this->description);
1457 class admin_setting_configpasswordunmask extends admin_setting_configtext {
1459 function admin_setting_configpasswordunmask($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
1460 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
1463 function output_html() {
1464 if ($this->get_setting() === NULL) {
1465 $current = $this->defaultsetting;
1466 } else {
1467 $current = $this->get_setting();
1469 $id = 'id_s_'.$this->name;
1470 $unmask = get_string('unmaskpassword', 'form');
1471 $unmaskjs = '<script type="text/javascript">
1472 //<![CDATA[
1473 document.write(\'<div class="unmask"><input id="'.$id.'unmask" value="1" type="checkbox" onclick="unmaskPassword(\\\''.$id.'\\\')"/><label for="'.$id.'unmask">'.addslashes_js($unmask).'<\/label><\/div>\');
1474 //]]>
1475 </script>';
1476 return format_admin_setting($this->name, $this->visiblename,
1477 '<input type="password" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />'.$unmaskjs,
1478 $this->description);
1483 class admin_setting_configcheckbox extends admin_setting {
1485 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1486 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1489 function get_setting() {
1490 global $CFG;
1491 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1494 function write_setting($data) {
1495 if ($data == '1') {
1496 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1497 } else {
1498 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1502 function output_html() {
1503 if ($this->get_setting() === NULL) {
1504 $current = $this->defaultsetting;
1505 } else {
1506 $current = $this->get_setting();
1508 return format_admin_setting($this->name, $this->visiblename,
1509 '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($current == true ? 'checked="checked"' : '') . ' />',
1510 $this->description);
1515 class admin_setting_configselect extends admin_setting {
1517 var $choices;
1519 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1520 $this->choices = $choices;
1521 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1524 function get_setting() {
1525 global $CFG;
1526 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1529 function write_setting($data) {
1530 // check that what we got was in the original choices
1531 // or that the data is the default setting - needed during install when choices can not be constructed yet
1532 if ($data != $this->defaultsetting and ! in_array($data, array_keys($this->choices))) {
1533 return 'Error setting ' . $this->visiblename . '<br />';
1536 return (set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1539 function output_html() {
1540 if ($this->get_setting() === NULL) {
1541 $current = $this->defaultsetting;
1542 } else {
1543 $current = $this->get_setting();
1545 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'">';
1546 foreach ($this->choices as $key => $value) {
1547 // the string cast is needed because key may be integer - 0 is equal to most strings!
1548 $return .= '<option value="'.$key.'"'.((string)$key==$current ? ' selected="selected"' : '').'>'.$value.'</option>';
1550 $return .= '</select>';
1552 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
1557 // this is a liiitle bit messy. we're using two selects, but we're returning them as an array named after $name (so we only use $name2
1558 // internally for the setting)
1559 class admin_setting_configtime extends admin_setting {
1561 var $name2;
1562 var $choices;
1563 var $choices2;
1565 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1566 $this->name2 = $minutesname;
1567 $this->choices = array();
1568 for ($i = 0; $i < 24; $i++) {
1569 $this->choices[$i] = $i;
1571 $this->choices2 = array();
1572 for ($i = 0; $i < 60; $i += 5) {
1573 $this->choices2[$i] = $i;
1575 parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1578 function get_setting() {
1579 global $CFG;
1580 return (isset($CFG->{$this->name}) && isset($CFG->{$this->name2}) ? array('h' => $CFG->{$this->name}, 'm' => $CFG->{$this->name2}) : NULL);
1583 function write_setting($data) {
1584 // check that what we got was in the original choices
1585 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
1586 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1589 return (set_config($this->name, $data['h']) && set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1592 function output_html() {
1593 if ($this->get_setting() === NULL) {
1594 $currentsetting = $this->defaultsetting;
1595 } else {
1596 $currentsetting = $this->get_setting();
1598 $return = '<div class="form-group">'.
1599 '<select class="form-select" id="id_s_'.$this->name.'h" name="s_' . $this->name .'[h]">';
1600 foreach ($this->choices as $key => $value) {
1601 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1603 $return .= '</select>:<select class="form-select" id="id_s_'.$this->name.'m" name="s_' . $this->name . '[m]">';
1604 foreach ($this->choices2 as $key => $value) {
1605 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1607 $return .= '</select></div>';
1608 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1613 class admin_setting_configmultiselect extends admin_setting_configselect {
1615 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1616 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1619 function get_setting() {
1620 global $CFG;
1621 if (isset($CFG->{$this->name})) {
1622 if ($CFG->{$this->name}) {
1623 return explode(',', $CFG->{$this->name});
1624 } else {
1625 return array();
1627 } else {
1628 return NULL;
1632 function write_setting($data) {
1633 if (empty($data)) {
1634 $data = array();
1636 foreach ($data as $datum) {
1637 if (! in_array($datum, array_keys($this->choices))) {
1638 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1642 return (set_config($this->name, implode(',',$data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1645 function output_html() {
1646 if ($this->get_setting() === NULL) {
1647 $currentsetting = $this->defaultsetting;
1648 if (!$currentsetting) {
1649 $currentsetting = array();
1651 } else {
1652 $currentsetting = $this->get_setting();
1654 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'[]" size="10" multiple="multiple">';
1655 foreach ($this->choices as $key => $value) {
1656 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ? ' selected="selected"' : '') . '>' . $value . '</option>';
1658 $return .= '</select>';
1659 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
1664 class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
1666 function admin_setting_special_adminseesall() {
1667 $name = 'calendar_adminseesall';
1668 $visiblename = get_string('adminseesall', 'admin');
1669 $description = get_string('helpadminseesall', 'admin');
1670 parent::admin_setting($name, $visiblename, $description, 0);
1673 function write_setting($data) {
1674 global $SESSION;
1675 unset($SESSION->cal_courses_shown);
1676 parent::write_setting($data);
1680 class admin_setting_sitesetselect extends admin_setting_configselect {
1682 var $id;
1684 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1686 $this->id = SITEID;
1687 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1691 function get_setting() {
1692 $site = get_site();
1693 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1696 function write_setting($data) {
1697 if (!in_array($data, array_keys($this->choices))) {
1698 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1700 $record = new stdClass();
1701 $record->id = $this->id;
1702 $temp = $this->name;
1703 $record->$temp = $data;
1704 $record->timemodified = time();
1705 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1711 class admin_setting_courselist_frontpage extends admin_setting_configselect {
1713 function admin_setting_courselist_frontpage($loggedin) {
1714 global $CFG;
1715 require_once($CFG->dirroot . '/course/lib.php');
1716 $name = 'frontpage' . ($loggedin ? 'loggedin' : '');
1717 $visiblename = get_string('frontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1718 $description = get_string('configfrontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1719 $choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
1720 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
1721 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
1722 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
1723 '' => get_string('none'));
1724 if (!$loggedin and count_records("course") > FRONTPAGECOURSELIMIT) {
1725 unset($choices[FRONTPAGECOURSELIST]);
1727 $defaults = FRONTPAGECOURSELIST.',,,';
1728 parent::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
1731 function get_setting() {
1732 global $CFG;
1733 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : ',1,,');
1736 function write_setting($data) {
1737 if (empty($data)) {
1738 $data = array();
1739 } if (!is_array($data)) {
1740 $data = explode(',', $data);
1742 foreach($data as $datum) {
1743 if (! in_array($datum, array_keys($this->choices))) {
1744 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1747 return (set_config($this->name, implode(',', $data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1750 function output_html() {
1751 if ($this->get_setting() === NULL) {
1752 $currentsetting = $this->defaultsetting;
1753 } else {
1754 $currentsetting = $this->get_setting();
1756 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1757 if (!isset($currentsetting[$i])) {
1758 $currentsetting[$i] = 0;
1761 $return = '<div class="form-group">';
1762 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1763 $return .='<select class="form-select" id="id_s_'.$this->name.$i.'" name="s_' . $this->name .'[]">';
1764 foreach ($this->choices as $key => $value) {
1765 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1767 $return .= '</select>';
1768 if ($i !== count($this->choices) - 2) {
1769 $return .= '<br />';
1772 $return .= '</div>';
1774 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1778 class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
1780 var $id;
1782 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1784 $this->id = SITEID;
1785 parent::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
1789 function get_setting() {
1790 $site = get_site();
1791 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1794 function write_setting($data) {
1795 $record = new stdClass();
1796 $record->id = $this->id;
1797 $temp = $this->name;
1798 $record->$temp = ($data == '1' ? 1 : 0);
1799 $record->timemodified = time();
1800 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1805 class admin_setting_sitesettext extends admin_setting_configtext {
1807 var $id;
1809 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting) {
1811 $this->id = SITEID;
1812 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting);
1816 function get_setting() {
1817 $site = get_site();
1818 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1821 function validate($data) {
1822 $cleaned = stripslashes(clean_param($data, PARAM_MULTILANG));
1823 if ($cleaned == '') {
1824 return false; // can not be empty
1826 return ($data == $cleaned); // implicit conversion to string is needed to do exact comparison
1829 function write_setting($data) {
1830 $data = trim($data);
1831 if (!$this->validate($data)) {
1832 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1835 $record = new stdClass();
1836 $record->id = $this->id;
1837 $record->{$this->name} = addslashes($data);
1838 $record->timemodified = time();
1839 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1844 class admin_setting_special_frontpagedesc extends admin_setting {
1846 var $id;
1848 function admin_setting_special_frontpagedesc() {
1849 $this->id = SITEID;
1850 $name = 'summary';
1851 $visiblename = get_string('frontpagedescription');
1852 $description = get_string('frontpagedescriptionhelp');
1853 parent::admin_setting($name, $visiblename, $description, '');
1856 function output_html() {
1858 global $CFG;
1860 if ($this->get_setting() === NULL) {
1861 $currentsetting = $this->defaultsetting;
1862 } else {
1863 $currentsetting = $this->get_setting();
1866 $CFG->adminusehtmleditor = can_use_html_editor();
1868 $return = print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, 's_' . $this->name, $currentsetting, 0, true);
1870 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
1873 function get_setting() {
1875 $site = get_site();
1876 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1880 function write_setting($data) {
1882 $data = addslashes(clean_param($data, PARAM_CLEANHTML));
1884 $record = new stdClass();
1885 $record->id = $this->id;
1886 $temp = $this->name;
1887 $record->$temp = $data;
1888 $record->timemodified = time();
1890 return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1897 class admin_setting_special_editorfontlist extends admin_setting {
1899 var $items;
1901 function admin_setting_special_editorfontlist() {
1902 global $CFG;
1903 $name = 'editorfontlist';
1904 $visiblename = get_string('editorfontlist', 'admin');
1905 $description = get_string('configeditorfontlist', 'admin');
1906 $defaults = array('k0' => 'Trebuchet',
1907 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1908 'k1' => 'Arial',
1909 'v1' => 'arial,helvetica,sans-serif',
1910 'k2' => 'Courier New',
1911 'v2' => 'courier new,courier,monospace',
1912 'k3' => 'Georgia',
1913 'v3' => 'georgia,times new roman,times,serif',
1914 'k4' => 'Tahoma',
1915 'v4' => 'tahoma,arial,helvetica,sans-serif',
1916 'k5' => 'Times New Roman',
1917 'v5' => 'times new roman,times,serif',
1918 'k6' => 'Verdana',
1919 'v6' => 'verdana,arial,helvetica,sans-serif',
1920 'k7' => 'Impact',
1921 'v7' => 'impact',
1922 'k8' => 'Wingdings',
1923 'v8' => 'wingdings');
1924 parent::admin_setting($name, $visiblename, $description, $defaults);
1927 function get_setting() {
1928 global $CFG;
1929 if (isset($CFG->editorfontlist)) {
1930 $i = 0;
1931 $currentsetting = array();
1932 $items = explode(';', $CFG->editorfontlist);
1933 foreach ($items as $item) {
1934 $item = explode(':', $item);
1935 $currentsetting['k' . $i] = $item[0];
1936 $currentsetting['v' . $i] = $item[1];
1937 $i++;
1939 return $currentsetting;
1940 } else {
1941 return NULL;
1945 function write_setting($data) {
1947 // there miiight be an easier way to do this :)
1948 // if this is changed, make sure the $defaults array above is modified so that this
1949 // function processes it correctly
1951 $keys = array();
1952 $values = array();
1954 foreach ($data as $key => $value) {
1955 if (substr($key,0,1) == 'k') {
1956 $keys[substr($key,1)] = $value;
1957 } elseif (substr($key,0,1) == 'v') {
1958 $values[substr($key,1)] = $value;
1962 $result = '';
1963 for ($i = 0; $i < count($keys); $i++) {
1964 if (($keys[$i] !== '') && ($values[$i] !== '')) {
1965 $result .= clean_param($keys[$i],PARAM_NOTAGS) . ':' . clean_param($values[$i], PARAM_NOTAGS) . ';';
1969 $result = substr($result, 0, -1); // trim the last semicolon
1971 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1974 function output_html() {
1976 if ($this->get_setting() === NULL) {
1977 $currentsetting = $this->defaultsetting;
1978 } else {
1979 $currentsetting = $this->get_setting();
1982 $return = '<div class="form-group">';
1983 for ($i = 0; $i < count($currentsetting) / 2; $i++) {
1984 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
1985 $return .= '&nbsp;&nbsp;';
1986 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
1988 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="" />';
1989 $return .= '&nbsp;&nbsp;';
1990 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="" /><br />';
1991 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . ($i + 1) . ']" value="" />';
1992 $return .= '&nbsp;&nbsp;';
1993 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . ($i + 1) . ']" value="" />';
1994 $return .= '</div>';
1996 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2001 class admin_setting_special_editordictionary extends admin_setting_configselect {
2003 function admin_setting_special_editordictionary() {
2004 $name = 'editordictionary';
2005 $visiblename = get_string('editordictionary','admin');
2006 $description = get_string('configeditordictionary', 'admin');
2007 $choices = $this->editor_get_dictionaries();
2008 if (! is_array($choices)) {
2009 $choices = array('');
2012 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2015 // function borrowed from the old moodle/admin/editor.php, slightly modified
2016 function editor_get_dictionaries () {
2017 /// Get all installed dictionaries in the system
2019 global $CFG;
2021 // error_reporting(E_ALL); // for debug, final version shouldn't have this...
2022 clearstatcache();
2024 // If aspellpath isn't set don't even bother ;-)
2025 if (empty($CFG->aspellpath)) {
2026 return 'Empty aspell path!';
2029 // Do we have access to popen function?
2030 if (!function_exists('popen')) {
2031 return 'Popen function disabled!';
2034 $cmd = $CFG->aspellpath;
2035 $output = '';
2036 $dictionaries = array();
2037 $dicts = array();
2039 if(!($handle = @popen(escapeshellarg($cmd) .' dump dicts', 'r'))) {
2040 return 'Couldn\'t create handle!';
2043 while(!feof($handle)) {
2044 $output .= fread($handle, 1024);
2046 @pclose($handle);
2048 $dictionaries = explode(chr(10), $output);
2050 // Get rid of possible empty values
2051 if (is_array($dictionaries)) {
2053 $cnt = count($dictionaries);
2055 for ($i = 0; $i < $cnt; $i++) {
2056 if (!empty($dictionaries[$i])) {
2057 $dicts[$dictionaries[$i]] = $dictionaries[$i];
2062 if (count($dicts) >= 1) {
2063 return $dicts;
2066 return 'Error! Check your aspell installation!';
2074 class admin_setting_special_editorhidebuttons extends admin_setting {
2076 var $name;
2077 var $visiblename;
2078 var $description;
2079 var $items;
2081 function admin_setting_special_editorhidebuttons() {
2082 $this->name = 'editorhidebuttons';
2083 $this->visiblename = get_string('editorhidebuttons', 'admin');
2084 $this->description = get_string('confeditorhidebuttons', 'admin');
2085 $this->defaultsetting = array();
2086 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
2087 $this->items = array('fontname' => '',
2088 'fontsize' => '',
2089 'formatblock' => '',
2090 'bold' => 'ed_format_bold.gif',
2091 'italic' => 'ed_format_italic.gif',
2092 'underline' => 'ed_format_underline.gif',
2093 'strikethrough' => 'ed_format_strike.gif',
2094 'subscript' => 'ed_format_sub.gif',
2095 'superscript' => 'ed_format_sup.gif',
2096 'copy' => 'ed_copy.gif',
2097 'cut' => 'ed_cut.gif',
2098 'paste' => 'ed_paste.gif',
2099 'clean' => 'ed_wordclean.gif',
2100 'undo' => 'ed_undo.gif',
2101 'redo' => 'ed_redo.gif',
2102 'justifyleft' => 'ed_align_left.gif',
2103 'justifycenter' => 'ed_align_center.gif',
2104 'justifyright' => 'ed_align_right.gif',
2105 'justifyfull' => 'ed_align_justify.gif',
2106 'lefttoright' => 'ed_left_to_right.gif',
2107 'righttoleft' => 'ed_right_to_left.gif',
2108 'insertorderedlist' => 'ed_list_num.gif',
2109 'insertunorderedlist' => 'ed_list_bullet.gif',
2110 'outdent' => 'ed_indent_less.gif',
2111 'indent' => 'ed_indent_more.gif',
2112 'forecolor' => 'ed_color_fg.gif',
2113 'hilitecolor' => 'ed_color_bg.gif',
2114 'inserthorizontalrule' => 'ed_hr.gif',
2115 'createanchor' => 'ed_anchor.gif',
2116 'createlink' => 'ed_link.gif',
2117 'unlink' => 'ed_unlink.gif',
2118 'insertimage' => 'ed_image.gif',
2119 'inserttable' => 'insert_table.gif',
2120 'insertsmile' => 'em.icon.smile.gif',
2121 'insertchar' => 'icon_ins_char.gif',
2122 'spellcheck' => 'spell-check.gif',
2123 'htmlmode' => 'ed_html.gif',
2124 'popupeditor' => 'fullscreen_maximize.gif',
2125 'search_replace' => 'ed_replace.gif');
2128 function get_setting() {
2129 global $CFG;
2130 return (isset($CFG->{$this->name}) ? explode(' ', $CFG->{$this->name}) : NULL);
2133 function write_setting($data) {
2134 $result = array();
2135 if (empty($data)) { $data = array(); }
2136 foreach ($data as $key => $value) {
2137 if (!in_array($key, array_keys($this->items))) {
2138 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2140 if ($value == '1') {
2141 $result[] = $key;
2144 return (set_config($this->name, implode(' ',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2147 function output_html() {
2149 global $CFG;
2151 // checkboxes with input name="$this->name[$key]" value="1"
2152 // we do 15 fields per column
2154 if ($this->get_setting() === NULL) {
2155 $currentsetting = $this->defaultsetting;
2156 } else {
2157 $currentsetting = $this->get_setting();
2160 $return = '<div class="form-group">';
2161 $return .= '<table><tr><td valign="top" align="right">';
2163 $count = 0;
2165 foreach($this->items as $key => $value) {
2166 if ($count % 15 == 0 and $count != 0) {
2167 $return .= '</td><td valign="top" align="right">';
2170 $return .= ($value == '' ? get_string($key,'editor') : '<img width="18" height="18" src="' . $CFG->wwwroot . '/lib/editor/htmlarea/images/' . $value . '" alt="' . get_string($key,'editor') . '" title="' . get_string($key,'editor') . '" />') . '&nbsp;';
2171 $return .= '<input type="checkbox" class="form-checkbox" value="1" id="id_s_'.$this->name.$key.'" name="s_' . $this->name . '[' . $key . ']"' . (in_array($key,$currentsetting) ? ' checked="checked"' : '') . ' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
2172 $count++;
2173 if ($count % 15 != 0) {
2174 $return .= '<br /><br />';
2178 $return .= '</td></tr>';
2179 $return .= '</table>';
2180 $return .= '</div>';
2182 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2187 class admin_setting_langlist extends admin_setting_configtext {
2188 function admin_setting_langlist() {
2189 parent::admin_setting_configtext('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS);
2192 function write_setting($data) {
2193 $return = parent::write_setting($data);
2194 get_list_of_languages(true);//refresh the list
2195 return $return;
2199 class admin_setting_backupselect extends admin_setting_configselect {
2201 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
2202 parent::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
2205 function get_setting() {
2206 $backup_config = backup_get_config();
2207 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2210 function write_setting($data) {
2211 // check that what we got was in the original choices
2212 if (! in_array($data, array_keys($this->choices))) {
2213 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2216 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2221 class admin_setting_special_backupsaveto extends admin_setting_configtext {
2223 function admin_setting_special_backupsaveto() {
2224 $name = 'backup_sche_destination';
2225 $visiblename = get_string('saveto');
2226 $description = get_string('backupsavetohelp');
2227 parent::admin_setting_configtext($name, $visiblename, $description, '');
2230 function get_setting() {
2231 $backup_config = backup_get_config();
2232 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2235 function write_setting($data) {
2236 $data = trim($data);
2237 if (!empty($data) and !is_dir($data)) {
2238 return get_string('pathnotexists') . '<br />';
2240 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2245 class admin_setting_backupcheckbox extends admin_setting_configcheckbox {
2247 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2248 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2251 function write_setting($data) {
2252 if ($data == '1') {
2253 return (backup_set_config($this->name, 1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2254 } else {
2255 return (backup_set_config($this->name, 0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2259 function get_setting() {
2260 $backup_config = backup_get_config();
2261 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2266 class admin_setting_special_backuptime extends admin_setting_configtime {
2268 function admin_setting_special_backuptime() {
2269 $name = 'backup_sche_hour';
2270 $name2 = 'backup_sche_minute';
2271 $visiblename = get_string('executeat');
2272 $description = get_string('backupexecuteathelp');
2273 $default = array('h' => 0, 'm' => 0);
2274 parent::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2277 function get_setting() {
2278 $backup_config = backup_get_config();
2279 return (isset($backup_config->{$this->name}) && isset($backup_config->{$this->name}) ? array('h'=>$backup_config->{$this->name}, 'm'=>$backup_config->{$this->name2}) : NULL);
2282 function write_setting($data) {
2283 // check that what we got was in the original choices
2284 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
2285 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2288 return (backup_set_config($this->name, $data['h']) && backup_set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2293 class admin_setting_special_backupdays extends admin_setting {
2295 function admin_setting_special_backupdays() {
2296 $name = 'backup_sche_weekdays';
2297 $visiblename = get_string('schedule');
2298 $description = get_string('backupschedulehelp');
2299 $default = array('u' => 0, 'm' => 0, 't' => 0, 'w' => 0, 'r' => 0, 'f' => 0, 's' => 0);
2300 parent::admin_setting($name, $visiblename, $description, $default);
2303 function get_setting() {
2304 $backup_config = backup_get_config();
2305 if (isset($backup_config->{$this->name})) {
2306 $currentsetting = $backup_config->{$this->name};
2307 return array('u' => substr($currentsetting, 0, 1),
2308 'm' => substr($currentsetting, 1, 1),
2309 't' => substr($currentsetting, 2, 1),
2310 'w' => substr($currentsetting, 3, 1),
2311 'r' => substr($currentsetting, 4, 1),
2312 'f' => substr($currentsetting, 5, 1),
2313 's' => substr($currentsetting, 6, 1));
2314 } else {
2315 return NULL;
2319 function output_html() {
2321 if ($this->get_setting() === NULL) {
2322 $currentsetting = $this->defaultsetting;
2323 } else {
2324 $currentsetting = $this->get_setting();
2327 // rewrite for simplicity
2328 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
2329 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
2331 $return = '<table><tr><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2332 get_string('monday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('tuesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2333 get_string('wednesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('thursday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' .
2334 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div style="text-align:center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
2335 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'u" name="s_'. $this->name .'[u]" value="1" ' . (substr($currentsetting,0,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2336 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'m" name="s_'. $this->name .'[m]" value="1" ' . (substr($currentsetting,1,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2337 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'t" name="s_'. $this->name .'[t]" value="1" ' . (substr($currentsetting,2,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2338 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'w" name="s_'. $this->name .'[w]" value="1" ' . (substr($currentsetting,3,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2339 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'r" name="s_'. $this->name .'[r]" value="1" ' . (substr($currentsetting,4,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2340 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'f" name="s_'. $this->name .'[f]" value="1" ' . (substr($currentsetting,5,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2341 '<td><div style="text-align:center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'s" name="s_'. $this->name .'[s]" value="1" ' . (substr($currentsetting,6,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2342 '</tr></table>';
2344 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2348 // we're using the array trick (see http://ca.php.net/manual/en/faq.html.php#faq.html.arrays) to get the data passed to use without having to modify
2349 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2350 // they MUST go into an array named $this->name, or else we won't receive them here
2351 function write_setting($data) {
2352 $week = 'umtwrfs';
2353 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2354 if (!empty($data)) {
2355 foreach($data as $key => $value) {
2356 if ($value == '1') {
2357 $result[strpos($week, $key)] = 1;
2361 return (backup_set_config($this->name, implode('',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2365 class admin_setting_special_debug extends admin_setting_configselect {
2367 function admin_setting_special_debug() {
2368 $name = 'debug';
2369 $visiblename = get_string('debug', 'admin');
2370 $description = get_string('configdebug', 'admin');
2371 $choices = array( DEBUG_NONE => get_string('debugnone', 'admin'),
2372 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
2373 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
2374 DEBUG_ALL => get_string('debugall', 'admin'),
2375 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')
2377 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2380 function get_setting() {
2381 global $CFG;
2382 if (isset($CFG->debug)) {
2383 return $CFG->debug;
2384 } else {
2385 return NULL;
2389 function write_setting($data) {
2390 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2396 class admin_setting_special_calendar_weekend extends admin_setting {
2398 function admin_setting_special_calendar_weekend() {
2399 $name = 'calendar_weekend';
2400 $visiblename = get_string('calendar_weekend', 'admin');
2401 $description = get_string('helpweekenddays', 'admin');
2402 $default = array ('0', '6'); // Saturdays and Sundays
2403 parent::admin_setting($name, $visiblename, $description, $default);
2406 function get_setting() {
2407 global $CFG;
2408 return isset($CFG->{$this->name}) ? $CFG->{$this->name} : 0;
2411 function write_setting($data) {
2412 $result = 0;
2413 if (!empty($data)) {
2414 foreach($data as $index) {
2415 $result |= 1 << $index;
2418 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2421 function output_html() {
2422 if ($this->get_setting() === NULL) {
2423 $currentsetting = $this->defaultsetting;
2424 } else {
2425 $currentsetting = $this->get_setting();
2428 // The order matters very much because of the implied numeric keys
2429 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
2430 $return = '<table><thead><tr>';
2431 foreach($days as $index => $day) {
2432 $return .= '<td><label for="id_s_'.$this->name.$index.'">'.get_string($day, 'calendar').'</label></td>';
2434 $return .= '</tr></thead><tbody><tr>';
2435 foreach($days as $index => $day) {
2436 $return .= '<td><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.$index.'" name="s_'.$this->name.'[]" value="'.$index.'" '.($currentsetting & (1 << $index) ? 'checked="checked"' : '') . ' /></td>';
2438 $return .= '</tr></tbody></table>';
2440 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2447 * this is used in config->appearance->gradeconfig
2449 class admin_setting_special_gradebookroles extends admin_setting {
2451 function admin_setting_special_gradebookroles() {
2452 $name = 'gradebookroles';
2453 $visiblename = get_string('gradebookroles', 'admin');
2454 $description = get_string('configgradebookroles', 'admin');
2455 $default = array(5=>'1'); // The student role in a default install
2456 parent::admin_setting($name, $visiblename, $description, $default);
2459 function get_setting() {
2460 global $CFG;
2461 if (!empty($CFG->{$this->name})) {
2462 $result = explode(',', $CFG->{$this->name});
2463 foreach ($result as $roleid) {
2464 $array[$roleid] = 1;
2466 return $array;
2467 } else {
2468 return null;
2472 function write_setting($data) {
2473 if (!empty($data)) {
2474 $str = '';
2475 foreach ($data as $key => $value) {
2476 if ($value) {
2477 $str .= $key.',';
2480 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2481 } else {
2482 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2486 function output_html() {
2488 if ($this->get_setting() === NULL) {
2489 $currentsetting = $this->defaultsetting;
2490 } else {
2491 $currentsetting = $this->get_setting();
2493 // from to process which roles to display
2494 if ($roles = get_records('role')) {
2495 $return = '<div class="form-group">';
2496 $first = true;
2497 foreach ($roles as $roleid=>$role) {
2498 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2499 $checked = ' checked="checked"';
2500 } else {
2501 $checked = '';
2503 if ($first) {
2504 $first = false;
2505 } else {
2506 $return .= '<br />';
2508 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1"'.$checked.' />&nbsp;'.format_string($role->name);
2510 $return .= '</div>';
2513 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2520 * this is used in config->appearance->coursemanager
2521 * (which roles to show on course decription page)
2523 class admin_setting_special_coursemanager extends admin_setting {
2525 function admin_setting_special_coursemanager() {
2526 $name = 'coursemanager';
2527 $visiblename = get_string('coursemanager', 'admin');
2528 $description = get_string('configcoursemanager', 'admin');
2529 $default = array(3=>'1'); // The teahcer role in a default install
2530 parent::admin_setting($name, $visiblename, $description, $default);
2533 function get_setting() {
2535 global $CFG;
2536 if (!empty($CFG->{$this->name})) {
2537 $result = explode(',', $CFG->{$this->name});
2538 foreach ($result as $roleid) {
2539 $array[$roleid] = 1;
2541 return $array;
2542 } else if (isset($CFG->{$this->name})) {
2543 return array();
2544 } else {
2545 return null;
2549 function write_setting($data) {
2551 if (!empty($data)) {
2552 $str = '';
2553 foreach ($data as $key => $value) {
2554 if ($value) {
2555 $str .= $key.',';
2558 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2559 } else {
2560 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2564 function output_html() {
2566 if ($this->get_setting() === NULL) {
2567 $currentsetting = $this->defaultsetting;
2568 } else {
2569 $currentsetting = $this->get_setting();
2571 // from to process which roles to display
2572 if ($roles = get_records_select('role', '', 'sortorder')) {
2573 $return = '<div class="form-group">';
2574 $first = true;
2575 foreach ($roles as $roleid=>$role) {
2576 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2577 $checked = 'checked="checked"';
2578 } else {
2579 $checked = '';
2581 if ($first) {
2582 $first = false;
2583 } else {
2584 $return .= '<br />';
2586 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1" '.$checked.' />&nbsp;'.$role->name;
2588 $return .= '</div>';
2590 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2595 * this is used in config->courses->gradeexports
2596 * (which roles to show on course decription page)
2598 class admin_setting_special_gradeexport extends admin_setting {
2600 function admin_setting_special_gradeexport() {
2601 $name = 'gradeexport';
2602 $visiblename = get_string('gradeexport', 'admin');
2603 $description = get_string('configgradeexport', 'admin');
2604 $default = array(3=>'1'); // The teahcer role in a default install
2605 parent::admin_setting($name, $visiblename, $description, $default);
2608 function get_setting() {
2610 global $CFG;
2611 if (!empty($CFG->{$this->name})) {
2612 $result = explode(',', $CFG->{$this->name});
2613 foreach ($result as $plugin) {
2614 $array[$plugin] = 1;
2616 return $array;
2617 } else if (isset($CFG->{$this->name})) {
2618 return array();
2619 } else {
2620 return null;
2624 function write_setting($data) {
2626 if (!empty($data)) {
2627 $str = '';
2628 foreach ($data as $key => $value) {
2629 if ($value) {
2630 $str .= $key.',';
2633 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2634 } else {
2635 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2639 function output_html() {
2641 if ($this->get_setting() === NULL) {
2642 $currentsetting = $this->defaultsetting;
2643 } else {
2644 $currentsetting = $this->get_setting();
2646 // from to process which roles to display
2647 if ($exports = get_list_of_plugins('grade/export')) {
2648 $return = '<div class="form-group">';
2649 $first = true;
2650 foreach ($exports as $export) {
2651 if (is_array($currentsetting) && in_array($export, array_keys($currentsetting))) {
2652 $checked = 'checked="checked"';
2653 } else {
2654 $checked = '';
2656 if ($first) {
2657 $first = false;
2658 } else {
2659 $return .= '<br />';
2661 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$export.']" value="1" '.$checked.' />&nbsp;'.$export;
2663 $return .= '</div>';
2665 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2672 class admin_setting_special_perfdebug extends admin_setting_configcheckbox {
2674 function admin_setting_special_perfdebug() {
2675 $name = 'perfdebug';
2676 $visiblename = get_string('perfdebug', 'admin');
2677 $description = get_string('configperfdebug', 'admin');
2678 parent::admin_setting_configcheckbox($name, $visiblename, $description, '');
2681 function write_setting($data) {
2683 if ($data == '1') {
2684 return (set_config($this->name,15) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2685 } else {
2686 return (set_config($this->name,7) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2690 function output_html() {
2692 if ($this->get_setting() === NULL) {
2693 $currentsetting = $this->defaultsetting;
2694 } else {
2695 $currentsetting = $this->get_setting();
2698 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 15 ? 'checked="checked"' : '') . ' />';
2699 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
2704 class admin_setting_special_debugdisplay extends admin_setting_configcheckbox {
2706 function admin_setting_special_debugdisplay() {
2707 $name = 'debugdisplay';
2708 $visiblename = get_string('debugdisplay', 'admin');
2709 $description = get_string('configdebugdisplay', 'admin');
2710 $default = ini_get('display_errors');
2711 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2714 function write_setting($data) {
2716 if ($data == '1') {
2717 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2718 } else {
2719 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2723 function output_html() {
2725 if ($this->get_setting() === NULL) {
2726 $currentsetting = $this->defaultsetting;
2727 } else {
2728 $currentsetting = $this->get_setting();
2731 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 1 ? 'checked="checked"' : '') . ' />';
2732 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
2738 // Code for a function that helps externalpages print proper headers and footers
2739 // N.B.: THIS FUNCTION HANDLES AUTHENTICATION
2740 function admin_externalpage_setup($section) {
2742 global $CFG, $PAGE, $USER;
2744 $adminroot = admin_get_root();
2746 require_once($CFG->libdir . '/blocklib.php');
2747 require_once($CFG->dirroot . '/'.$CFG->admin.'/pagelib.php');
2749 page_map_class(PAGE_ADMIN, 'page_admin');
2751 $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number
2753 $PAGE->init_extra($section); // hack alert!
2755 $root = $adminroot->locate($PAGE->section);
2757 if ($site = get_site()) {
2758 require_login();
2759 } else {
2760 redirect($CFG->wwwroot . '/'.$CFG->admin.'/index.php');
2761 die;
2764 if (!is_a($root, 'admin_externalpage')) {
2765 error(get_string('sectionerror','admin'));
2766 die;
2769 // this eliminates our need to authenticate on the actual pages
2770 if (!($root->check_access())) {
2771 error(get_string('accessdenied', 'admin'));
2772 die;
2775 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
2777 if (!isset($USER->adminediting)) {
2778 $USER->adminediting = false;
2781 if ($PAGE->user_allowed_editing()) {
2782 if ($adminediting == 1) {
2783 $USER->adminediting = true;
2784 } elseif ($adminediting == 0) {
2785 $USER->adminediting = false;
2791 function admin_externalpage_print_header() {
2793 global $CFG, $PAGE, $SITE, $THEME;
2795 if (!empty($THEME->customcorners)) {
2796 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
2799 define('ADMIN_EXT_HEADER_PRINTED', 'true');
2801 if (!empty($SITE->fullname)) {
2802 $pageblocks = blocks_setup($PAGE);
2804 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH,
2805 blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
2806 BLOCK_L_MAX_WIDTH);
2807 $PAGE->print_header();
2808 echo '<table id="layout-table" summary=""><tr>';
2809 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
2810 if (!empty($THEME->customcorners)) print_custom_corners_start();
2811 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
2812 if (!empty($THEME->customcorners)) print_custom_corners_end();
2813 echo '</td>';
2814 echo '<td id="middle-column">';
2815 if (!empty($THEME->customcorners)) print_custom_corners_start();
2816 } else {
2817 print_header();
2821 function admin_externalpage_print_footer() {
2823 global $CFG, $PAGE, $SITE, $THEME;
2825 if (!empty($THEME->customcorners)) {
2826 require_once($CFG->dirroot.'/lib/custom_corners_lib.php');
2829 define('ADMIN_EXT_FOOTER_PRINTED', 'true');
2831 if (!empty($SITE->fullname)) {
2832 $pageblocks = blocks_setup($PAGE);
2833 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH,
2834 blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
2835 BLOCK_R_MAX_WIDTH);
2836 if (!empty($THEME->customcorners)) print_custom_corners_end();
2837 echo '</td>';
2838 if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT)) {
2839 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
2840 if (!empty($THEME->customcorners)) print_custom_corners_start();
2841 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
2842 if (!empty($THEME->customcorners)) print_custom_corners_end();
2843 echo '</td>';
2845 echo '</tr></table>';
2847 print_footer();
2850 function admin_get_root() {
2851 global $CFG;
2853 static $ADMIN;
2855 if (!isset($ADMIN)) {
2856 // start the admin tree!
2857 $ADMIN = new admin_category('root', get_string("administration"));
2858 // we process this file first to get categories up and running
2859 include($CFG->dirroot . '/'.$CFG->admin.'/settings/top.php');
2861 // now we process all other files in admin/settings to build the
2862 // admin tree
2863 foreach (glob($CFG->dirroot . '/'.$CFG->admin.'/settings/*.php') as $file) {
2864 if ($file != $CFG->dirroot . '/'.$CFG->admin.'/settings/top.php') {
2865 include_once($file);
2870 return $ADMIN;
2873 /// settings utiliti functions
2875 // n.b. this function unconditionally applies default settings
2876 function apply_default_settings(&$node) {
2878 global $CFG;
2880 if (is_a($node, 'admin_category')) {
2881 $entries = array_keys($node->children);
2882 foreach ($entries as $entry) {
2883 apply_default_settings($node->children[$entry]);
2885 return;
2888 if (is_a($node, 'admin_settingpage')) {
2889 foreach ($node->settings as $setting) {
2890 $CFG->{$setting->name} = $setting->defaultsetting;
2891 $setting->write_setting($setting->defaultsetting);
2892 unset($setting); // needed to prevent odd (imho) reference behaviour
2893 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
2895 return;
2898 return;
2902 // n.b. this function unconditionally applies default settings
2903 function apply_default_exception_settings($defaults) {
2905 global $CFG;
2907 foreach($defaults as $key => $value) {
2908 $CFG->$key = $value;
2909 set_config($key, $value);
2914 function format_admin_setting($name, $title='', $form='', $description='', $label=true) {
2916 // sometimes the id is not id_s_name, but id_s_name_m or something, and this does not validate
2917 if ($label) {
2918 $labelfor = 'for = "id_s_'.$name.'"';
2919 } else {
2920 $labelfor = '';
2923 $str = "\n".
2924 '<div class="form-item" id="admin-'.$name.'">'."\n".
2925 '<label '.$labelfor.'>'.$title."\n".
2926 ' <span class="form-shortname">'.$name.'</span>'."\n".
2927 '</label>'."\n".
2928 $form."\n".
2929 '<div class="description">'.$description.'</div>'."\n".
2930 '</div>'.
2931 "\n\n";
2933 return $str;
2937 * Try to upgrade the given language pack (or current language)
2938 * If it doesn't work, fail silently and return false
2940 function upgrade_language_pack($lang='') {
2941 global $CFG;
2943 if (empty($lang)) {
2944 $lang = current_language();
2947 if ($lang == 'en_utf8') {
2948 return true; // Nothing to do
2951 notify(get_string('langimport', 'admin').': '.$lang.' ... ', 'notifysuccess');
2953 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
2954 @mkdir ($CFG->dataroot.'/lang/');
2956 require_once($CFG->libdir.'/componentlib.class.php');
2958 if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang.'.zip', 'languages.md5', 'lang')) {
2959 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
2961 if ($status == INSTALLED) {
2962 debugging('Downloading successful: '.$lang);
2963 @unlink($CFG->dataroot.'/cache/languages');
2964 return true;
2968 return false;
2972 * Based on find_new_settings{@link ()} in upgradesettings.php
2973 * Looks to find any admin settings that have not been initialized. Returns 1 if it finds any.
2975 * @param string &$node The node at which to start searching.
2976 * @return int Returns 1 if any settings haven't been initialised, 0 if they all have
2978 function any_new_admin_settings(&$node) {
2980 if (is_a($node, 'admin_category')) {
2981 $entries = array_keys($node->children);
2982 foreach ($entries as $entry) {
2983 if( any_new_admin_settings($node->children[$entry]) ){
2984 return 1;
2989 if (is_a($node, 'admin_settingpage')) {
2990 foreach ($node->settings as $setting) {
2991 if ($setting->get_setting() === NULL) {
2992 return 1;
2998 return 0;