wrap search/replace in a function so that it could be used in cron
[moodle.git] / lib / adminlib.php
CommitLineData
a4e10845 1<?php
88a7228a 2
3/**
4 * adminlib.php - Contains functions that only administrators will ever need to use
5 *
a4e10845 6 * @author Martin Dougiamas and many others
88a7228a 7 * @version $Id$
8 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
9 * @package moodlecore
10 */
11
8ddcdd86 12function upgrade_main_savepoint($result, $version) {
13 global $CFG;
14
15 if ($result) {
16 if ($CFG->version >= $version) {
17 // something really wrong is going on in main upgrade script
18 error("Upgrade savepoint: Can not upgrade main version from $CFG->version to $version.");
19 }
20 set_config('version', $version);
21 } else {
22 notify ("Upgrade savepoint: Error during main upgrade to version $version");
23 }
24}
25
26function upgrade_mod_savepoint($result, $version, $type) {
27 //TODO
28}
29
30function upgrade_plugin_savepoint($result, $version, $type, $dir) {
31 //TODO
32}
33
34function upgrade_backup_savepoint($result, $version) {
35 //TODO
36}
37
38function upgrade_blocks_savepoint($result, $version, $type) {
39 //TODO
40}
41
88a7228a 42/**
ead29342 43 * Upgrade plugins
88a7228a 44 *
45 * @uses $db
46 * @uses $CFG
ead29342 47 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
48 * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
49 * @param string $return The url to prompt the user to continue to
eef868d1 50 */
ead29342 51function upgrade_plugins($type, $dir, $return) {
e69ef14b 52 global $CFG, $db;
173cc1c3 53
15999a93 54/// Let's know if the header has been printed, so the funcion is being called embedded in an outer page
55 $embedded = defined('HEADER_PRINTED');
56
bb7053f3 57 $plugs = get_list_of_plugins($dir);
583fad99 58 $updated_plugins = false;
59 $strpluginsetup = get_string('pluginsetup');
60
ead29342 61 foreach ($plugs as $plug) {
173cc1c3 62
ead29342 63 $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
173cc1c3 64
ead29342 65 unset($plugin);
173cc1c3 66
bbbf2d40 67 if (is_readable($fullplug .'/version.php')) {
ead29342 68 include_once($fullplug .'/version.php'); // defines $plugin with version etc
173cc1c3 69 } else {
70 continue; // Nothing to do.
71 }
72
e79a09a2 73 $oldupgrade = false;
74 $newupgrade = false;
7c006e34 75 if (is_readable($fullplug . '/db/'. $CFG->dbtype . '.php')) {
76 include_once($fullplug . '/db/'. $CFG->dbtype . '.php'); // defines old upgrading function
e79a09a2 77 $oldupgrade = true;
78 }
db8bd7a6 79 if (is_readable($fullplug . '/db/upgrade.php')) {
7c006e34 80 include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
e79a09a2 81 $newupgrade = true;
82 }
83
ead29342 84 if (!isset($plugin)) {
173cc1c3 85 continue;
86 }
87
ead29342 88 if (!empty($plugin->requires)) {
89 if ($plugin->requires > $CFG->version) {
acdd790f 90 $info = new object();
ead29342 91 $info->pluginname = $plug;
92 $info->pluginversion = $plugin->version;
173cc1c3 93 $info->currentmoodle = $CFG->version;
ead29342 94 $info->requiremoodle = $plugin->requires;
15999a93 95 if (!$updated_plugins && !$embedded) {
0be6f678 96 print_header($strpluginsetup, $strpluginsetup,
97 build_navigation(array(array('name' => $strpluginsetup, 'link' => null, 'type' => 'misc'))), '',
371a32e3 98 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
583fad99 99 }
100 upgrade_log_start();
ead29342 101 notify(get_string('pluginrequirementsnotmet', 'error', $info));
583fad99 102 $updated_plugins = true;
173cc1c3 103 continue;
104 }
105 }
106
ead29342 107 $plugin->name = $plug; // The name MUST match the directory
173cc1c3 108
ead29342 109 $pluginversion = $type.'_'.$plug.'_version';
173cc1c3 110
ead29342 111 if (!isset($CFG->$pluginversion)) {
112 set_config($pluginversion, 0);
173cc1c3 113 }
eef868d1 114
ead29342 115 if ($CFG->$pluginversion == $plugin->version) {
173cc1c3 116 // do nothing
ead29342 117 } else if ($CFG->$pluginversion < $plugin->version) {
15999a93 118 if (!$updated_plugins && !$embedded) {
0be6f678 119 print_header($strpluginsetup, $strpluginsetup,
120 build_navigation(array(array('name' => $strpluginsetup, 'link' => null, 'type' => 'misc'))), '',
371a32e3 121 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
173cc1c3 122 }
e79a09a2 123 $updated_plugins = true;
583fad99 124 upgrade_log_start();
05b42119 125 print_heading($dir.'/'. $plugin->name .' plugin needs upgrading');
e79a09a2 126 $db->debug = true;
127 @set_time_limit(0); // To allow slow databases to complete the long SQL
128
d87a9d73 129 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
e79a09a2 130 /// Both old .sql files and new install.xml are supported
131 /// but we priorize install.xml (XMLDB) if present
132 $status = false;
db8bd7a6 133 if (file_exists($fullplug . '/db/install.xml')) {
450cf307 134 $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
e79a09a2 135 } else if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
136 $status = modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql'); //Old method
eef868d1 137 } else {
7c006e34 138 $status = true;
d87a9d73 139 }
e79a09a2 140
141 $db->debug = false;
eef868d1 142 /// Continue with the instalation, roles and other stuff
e79a09a2 143 if ($status) {
3f59a54e 144 /// OK so far, now update the plugins record
e79a09a2 145 set_config($pluginversion, $plugin->version);
3f59a54e 146
147 /// Install capabilities
ae628043 148 if (!update_capabilities($type.'/'.$plug)) {
3f59a54e 149 error('Could not set up the capabilities for '.$plugin->name.'!');
e79a09a2 150 }
3f59a54e 151 /// Install events
0856223c 152 events_update_definition($type.'/'.$plug);
3f59a54e 153
154 /// Run local install function if there is one
155 if (is_readable($fullplug .'/lib.php')) {
9ba38673 156 include_once($fullplug .'/lib.php');
3f59a54e 157 $installfunction = $plugin->name.'_install';
158 if (function_exists($installfunction)) {
159 if (! $installfunction() ) {
160 notify('Encountered a problem running install function for '.$module->name.'!');
161 }
162 }
163 }
9ba38673 164
e79a09a2 165 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
166 } else {
167 notify('Installing '. $plugin->name .' FAILED!');
168 }
d87a9d73 169 } else { // Upgrade existing install
e79a09a2 170 /// Run de old and new upgrade functions for the module
171 $oldupgrade_function = $type.'_'.$plugin->name .'_upgrade';
172 $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name .'_upgrade';
173
174 /// First, the old function if exists
175 $oldupgrade_status = true;
176 if ($oldupgrade && function_exists($oldupgrade_function)) {
177 $db->debug = true;
178 $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion);
179 } else if ($oldupgrade) {
180 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
181 $fullplug . '/db/' . $CFG->dbtype . '.php');
182 }
183
184 /// Then, the new function if exists and the old one was ok
185 $newupgrade_status = true;
186 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
187 $db->debug = true;
188 $newupgrade_status = $newupgrade_function($CFG->$pluginversion);
189 } else if ($newupgrade) {
190 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
191 $fullplug . '/db/upgrade.php');
192 }
193
194 $db->debug=false;
195 /// Now analyze upgrade results
196 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
197 // OK so far, now update the plugins record
198 set_config($pluginversion, $plugin->version);
ae628043 199 if (!update_capabilities($type.'/'.$plug)) {
e79a09a2 200 error('Could not update '.$plugin->name.' capabilities!');
d87a9d73 201 }
0856223c 202 events_update_definition($type.'/'.$plug);
e79a09a2 203 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
204 } else {
205 notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
173cc1c3 206 }
207 }
d87a9d73 208 echo '<hr />';
173cc1c3 209 } else {
583fad99 210 upgrade_log_start();
ead29342 211 error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
173cc1c3 212 }
213 }
214
583fad99 215 upgrade_log_finish();
216
15999a93 217 if ($updated_plugins && !$embedded) {
173cc1c3 218 print_continue($return);
acdd790f 219 print_footer('none');
173cc1c3 220 die;
221 }
222}
223
88a7228a 224/**
225 * Find and check all modules and load them up or upgrade them if necessary
226 *
227 * @uses $db
228 * @uses $CFG
229 * @param string $return The url to prompt the user to continue to
230 * @todo Finish documenting this function
eef868d1 231 */
173cc1c3 232function upgrade_activity_modules($return) {
173cc1c3 233
e69ef14b 234 global $CFG, $db;
173cc1c3 235
88a7228a 236 if (!$mods = get_list_of_plugins('mod') ) {
237 error('No modules installed!');
173cc1c3 238 }
239
583fad99 240 $updated_modules = false;
241 $strmodulesetup = get_string('modulesetup');
242
173cc1c3 243 foreach ($mods as $mod) {
244
88a7228a 245 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
173cc1c3 246 continue;
247 }
248
88a7228a 249 $fullmod = $CFG->dirroot .'/mod/'. $mod;
173cc1c3 250
251 unset($module);
252
88a7228a 253 if ( is_readable($fullmod .'/version.php')) {
254 include_once($fullmod .'/version.php'); // defines $module with version etc
173cc1c3 255 } else {
88a7228a 256 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
173cc1c3 257 continue;
258 }
259
d6eb06b6 260 $oldupgrade = false;
261 $newupgrade = false;
7c006e34 262 if ( is_readable($fullmod .'/db/' . $CFG->dbtype . '.php')) {
263 include_once($fullmod .'/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
d6eb06b6 264 $oldupgrade = true;
265 }
db8bd7a6 266 if ( is_readable($fullmod . '/db/upgrade.php')) {
7c006e34 267 include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
d6eb06b6 268 $newupgrade = true;
173cc1c3 269 }
270
271 if (!isset($module)) {
272 continue;
273 }
274
275 if (!empty($module->requires)) {
276 if ($module->requires > $CFG->version) {
acdd790f 277 $info = new object();
173cc1c3 278 $info->modulename = $mod;
279 $info->moduleversion = $module->version;
280 $info->currentmoodle = $CFG->version;
281 $info->requiremoodle = $module->requires;
583fad99 282 if (!$updated_modules) {
0be6f678 283 print_header($strmodulesetup, $strmodulesetup,
284 build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '',
371a32e3 285 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
583fad99 286 }
287 upgrade_log_start();
173cc1c3 288 notify(get_string('modulerequirementsnotmet', 'error', $info));
583fad99 289 $updated_modules = true;
173cc1c3 290 continue;
291 }
292 }
293
294 $module->name = $mod; // The name MUST match the directory
eef868d1 295
11e4a24b 296 include_once($fullmod.'/lib.php'); // defines upgrading and/or installing functions
297
88a7228a 298 if ($currmodule = get_record('modules', 'name', $module->name)) {
173cc1c3 299 if ($currmodule->version == $module->version) {
300 // do nothing
301 } else if ($currmodule->version < $module->version) {
d6eb06b6 302 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
303 if (!$oldupgrade && !$newupgrade) {
304 notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php or ' .
305 $fullmod . '/db/upgrade.php were not readable');
306 continue;
307 }
583fad99 308 if (!$updated_modules) {
0be6f678 309 print_header($strmodulesetup, $strmodulesetup,
310 build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '',
371a32e3 311 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
173cc1c3 312 }
583fad99 313 upgrade_log_start();
88a7228a 314 print_heading($module->name .' module needs upgrading');
d6eb06b6 315
316 /// Run de old and new upgrade functions for the module
317 $oldupgrade_function = $module->name . '_upgrade';
318 $newupgrade_function = 'xmldb_' . $module->name . '_upgrade';
319
320 /// First, the old function if exists
321 $oldupgrade_status = true;
322 if ($oldupgrade && function_exists($oldupgrade_function)) {
323 $db->debug = true;
324 $oldupgrade_status = $oldupgrade_function($currmodule->version, $module);
8bc9ebc4 325 if (!$oldupgrade_status) {
326 notify ('Upgrade function ' . $oldupgrade_function .
327 ' did not complete successfully.');
328 }
ba05965e 329 } else if ($oldupgrade) {
d6eb06b6 330 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
331 $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php');
d6eb06b6 332 }
333
334 /// Then, the new function if exists and the old one was ok
335 $newupgrade_status = true;
ba05965e 336 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
d6eb06b6 337 $db->debug = true;
338 $newupgrade_status = $newupgrade_function($currmodule->version, $module);
8bc9ebc4 339 } else if ($newupgrade && $oldupgrade_status) {
d6eb06b6 340 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
341 $mod . ': ' . $fullmod . '/db/upgrade.php');
d6eb06b6 342 }
343
e79a09a2 344 $db->debug=false;
d6eb06b6 345 /// Now analyze upgrade results
668896e5 346 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
d6eb06b6 347 // OK so far, now update the modules record
348 $module->id = $currmodule->id;
349 if (! update_record('modules', $module)) {
350 error('Could not update '. $module->name .' record in modules table!');
173cc1c3 351 }
d6eb06b6 352 remove_dir($CFG->dataroot . '/cache', true); // flush cache
353 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
354 echo '<hr />';
355 } else {
d6eb06b6 356 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
173cc1c3 357 }
bbbf2d40 358
d6eb06b6 359 /// Update the capabilities table?
bbbf2d40 360 if (!update_capabilities('mod/'.$module->name)) {
361 error('Could not update '.$module->name.' capabilities!');
362 }
0856223c 363 events_update_definition('mod/'.$module->name);
bbbf2d40 364
173cc1c3 365 $updated_modules = true;
eef868d1 366
173cc1c3 367 } else {
583fad99 368 upgrade_log_start();
88a7228a 369 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
173cc1c3 370 }
eef868d1 371
173cc1c3 372 } else { // module not installed yet, so install it
583fad99 373 if (!$updated_modules) {
0be6f678 374 print_header($strmodulesetup, $strmodulesetup,
375 build_navigation(array(array('name' => $strmodulesetup, 'link' => null, 'type' => 'misc'))), '',
371a32e3 376 upgrade_get_javascript(), false, '&nbsp;', '&nbsp;');
173cc1c3 377 }
583fad99 378 upgrade_log_start();
173cc1c3 379 print_heading($module->name);
380 $updated_modules = true;
381 $db->debug = true;
382 @set_time_limit(0); // To allow slow databases to complete the long SQL
d6eb06b6 383
384 /// Both old .sql files and new install.xml are supported
385 /// but we priorize install.xml (XMLDB) if present
db8bd7a6 386 if (file_exists($fullmod . '/db/install.xml')) {
d6eb06b6 387 $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
388 } else {
389 $status = modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql'); //Old method
390 }
391
e79a09a2 392 $db->debug = false;
11e4a24b 393
394 /// Continue with the installation, roles and other stuff
d6eb06b6 395 if ($status) {
88a7228a 396 if ($module->id = insert_record('modules', $module)) {
11e4a24b 397
9ba38673 398 /// Capabilities
bbbf2d40 399 if (!update_capabilities('mod/'.$module->name)) {
400 error('Could not set up the capabilities for '.$module->name.'!');
401 }
9ba38673 402
11e4a24b 403 /// Events
0856223c 404 events_update_definition('mod/'.$module->name);
11e4a24b 405
406 /// Run local install function if there is one
407 $installfunction = $module->name.'_install';
408 if (function_exists($installfunction)) {
409 if (! $installfunction() ) {
410 notify('Encountered a problem running install function for '.$module->name.'!');
411 }
412 }
413
a8f68426 414 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 415 echo '<hr />';
173cc1c3 416 } else {
88a7228a 417 error($module->name .' module could not be added to the module list!');
173cc1c3 418 }
eef868d1 419 } else {
88a7228a 420 error($module->name .' tables could NOT be set up successfully!');
173cc1c3 421 }
422 }
e5bd4e58 423
424 /// Check submodules of this module if necessary
425
e5bd4e58 426 $submoduleupgrade = $module->name.'_upgrade_submodules';
427 if (function_exists($submoduleupgrade)) {
428 $submoduleupgrade();
429 }
430
431
432 /// Run any defaults or final code that is necessary for this module
433
a5c0990e 434 if ( is_readable($fullmod .'/defaults.php')) {
435 // Insert default values for any important configuration variables
9e6e7502 436 unset($defaults);
eef868d1 437 include_once($fullmod .'/defaults.php');
f9a2e515 438 if (!empty($defaults)) {
439 foreach ($defaults as $name => $value) {
440 if (!isset($CFG->$name)) {
441 set_config($name, $value);
442 }
a5c0990e 443 }
444 }
445 }
173cc1c3 446 }
447
583fad99 448 upgrade_log_finish(); // finish logging if started
449
450 if ($updated_modules) {
173cc1c3 451 print_continue($return);
acdd790f 452 print_footer('none');
173cc1c3 453 die;
454 }
455}
456
eef868d1 457/**
f3221af9 458 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
80be7ee3 459 *
460 * @param string $name ?
461 * @param bool $value ?
462 * @param int $staleafter ?
463 * @param bool $clobberstale ?
464 * @todo Finish documenting this function
f3221af9 465 */
466function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
467
468 if (empty($name)) {
469 mtrace("Tried to get a cron lock for a null fieldname");
470 return false;
471 }
472
473 if (empty($value)) {
474 set_config($name,0);
475 return true;
476 }
477
478 if ($config = get_record('config','name',$name)) {
479 if (empty($config->value)) {
480 set_config($name,time());
481 } else {
482 // check for stale.
483 if ((time() - $staleafter) > $config->value) {
484 mtrace("STALE LOCKFILE FOR $name - was $config->value");
485 if (!empty($clobberstale)) {
486 set_config($name,time());
487 return true;
488 }
489 } else {
490 return false; // was not stale - ie, we're ok to still be running.
491 }
492 }
493 }
494 else {
495 set_config($name,time());
496 }
497 return true;
498}
a597f8a8 499
fb06b255 500function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
a597f8a8 501 static $starttime;
502 static $lasttime;
503
26ea4888 504 if ($total < 2) { // No need to show anything
505 return;
506 }
507
a597f8a8 508 if (empty($starttime)) {
509 $starttime = $lasttime = time();
510 $lasttime = $starttime - $updatetime;
511 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
72da5046 512 echo '<div id="bar'.$total.'" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
513 echo '<div id="slider'.$total.'" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
a597f8a8 514 echo '</div>';
72da5046 515 echo '<div id="text'.$total.'" align="center" style="width:500px;"></div>';
a597f8a8 516 echo '</td></tr></table>';
517 echo '</div>';
518 }
519
a597f8a8 520 $now = time();
521
522 if ($done && (($now - $lasttime) >= $updatetime)) {
523 $elapsedtime = $now - $starttime;
524 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
76317c73 525 $percentage = round((float)$done / (float)$total, 2);
a597f8a8 526 $width = (int)(500 * $percentage);
527
fb06b255 528 if ($projectedtime > 10) {
529 $projectedtext = ' Ending: '.format_time($projectedtime);
530 } else {
531 $projectedtext = '';
532 }
533
a597f8a8 534 echo '<script>';
aae37b63 535 echo 'document.getElementById("text'.$total.'").innerHTML = "'.addslashes($donetext).' ('.$done.'/'.$total.') '.$projectedtext.'";'."\n";
536 echo 'document.getElementById("slider'.$total.'").style.width = \''.$width.'px\';'."\n";
a597f8a8 537 echo '</script>';
538
539 $lasttime = $now;
540 sleep($sleeptime);
541 }
542}
583fad99 543
371a32e3 544function upgrade_get_javascript() {
545 global $CFG;
546
547 if (!empty($_SESSION['installautopilot'])) {
548 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = true;</script>'."\n";
549 } else {
550 $linktoscrolltoerrors = '<script type="text/javascript">var installautopilot = false;</script>'."\n";
551 }
552 $linktoscrolltoerrors .= '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>';
553
554 return $linktoscrolltoerrors;
555}
ad6226fb 556
557function create_admin_user() {
558 global $CFG, $USER;
559
560 if (empty($CFG->rolesactive)) { // No admin user yet.
561
562 $user = new object();
563 $user->auth = 'manual';
564 $user->firstname = get_string('admin');
565 $user->lastname = get_string('user');
566 $user->username = 'admin';
567 $user->password = hash_internal_user_password('admin');
568 $user->email = 'root@localhost';
569 $user->confirmed = 1;
570 $user->mnethostid = $CFG->mnet_localhost_id;
571 $user->lang = $CFG->lang;
572 $user->maildisplay = 1;
573 $user->timemodified = time();
574
575 if (!$user->id = insert_record('user', $user)) {
576 error('SERIOUS ERROR: Could not create admin user record !!!');
577 }
578
579 if (!$user = get_record('user', 'id', $user->id)) { // Double check.
580 error('User ID was incorrect (can\'t find it)');
581 }
582
583 // Assign the default admin roles to the new user.
584 if (!$adminroles = get_roles_with_capability('moodle/legacy:admin', CAP_ALLOW)) {
585 error('No admin role could be found');
586 }
84c8ede0 587 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
ad6226fb 588 foreach ($adminroles as $adminrole) {
589 role_assign($adminrole->id, $user->id, 0, $sitecontext->id);
590 }
591
592 set_config('rolesactive', 1);
593
594 // Log the user in.
595 $USER = get_complete_user_data('username', 'admin');
596 $USER->newadminuser = 1;
597 load_all_capabilities();
598
599 redirect("$CFG->wwwroot/user/editadvanced.php?id=$user->id"); // Edit thyself
600 } else {
601 error('Can not create admin!');
602 }
603}
604
583fad99 605////////////////////////////////////////////////
606/// upgrade logging functions
607////////////////////////////////////////////////
608
609$upgradeloghandle = false;
26c91c73 610$upgradelogbuffer = '';
611// I did not find out how to use static variable in callback function,
612// the problem was that I could not flush the static buffer :-(
613global $upgradeloghandle, $upgradelogbuffer;
583fad99 614
615/**
616 * Check if upgrade is already running.
617 *
618 * If anything goes wrong due to missing call to upgrade_log_finish()
619 * just restart the browser.
620 *
621 * @param string warning message indicating upgrade is already running
622 * @param int page reload timeout
623 */
624function upgrade_check_running($message, $timeout) {
625 if (!empty($_SESSION['upgraderunning'])) {
626 print_header();
627 redirect(me(), $message, $timeout);
628 }
629}
630
631/**
632 * Start logging of output into file (if not disabled) and
633 * prevent aborting and concurrent execution of upgrade script.
634 *
635 * Please note that you can not write into session variables after calling this function!
636 *
637 * This function may be called repeatedly.
638 */
639function upgrade_log_start() {
426a369b 640 global $CFG, $upgradeloghandle;
583fad99 641
642 if (!empty($_SESSION['upgraderunning'])) {
643 return; // logging already started
644 }
645
646 @ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading
647 $_SESSION['upgraderunning'] = 1; // set upgrade indicator
426a369b 648 if (empty($CFG->dbsessions)) { // workaround for bug in adodb, db session can not be restarted
649 session_write_close(); // from now on user can reload page - will be displayed warning
650 }
583fad99 651 make_upload_directory('upgradelogs');
652 ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
dedb2304 653 register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging
583fad99 654}
655
656/**
657 * Terminate logging of output, flush all data, allow script aborting
658 * and reopen session for writing. Function error() does terminate the logging too.
659 *
660 * Please make sure that each upgrade_log_start() is properly terminated by
661 * this function or error().
662 *
663 * This function may be called repeatedly.
664 */
665function upgrade_log_finish() {
426a369b 666 global $CFG, $upgradeloghandle, $upgradelogbuffer;
583fad99 667
668 if (empty($_SESSION['upgraderunning'])) {
669 return; // logging already terminated
670 }
671
672 @ob_end_flush();
26c91c73 673 if ($upgradelogbuffer !== '') {
674 @fwrite($upgradeloghandle, $upgradelogbuffer);
40896537 675 $upgradelogbuffer = '';
26c91c73 676 }
677 if ($upgradeloghandle and ($upgradeloghandle !== 'error')) {
678 @fclose($upgradeloghandle);
40896537 679 $upgradeloghandle = false;
26c91c73 680 }
426a369b 681 if (empty($CFG->dbsessions)) {
682 @session_start(); // ignore header errors, we only need to reopen session
683 }
583fad99 684 $_SESSION['upgraderunning'] = 0; // clear upgrade indicator
685 if (connection_aborted()) {
686 die;
687 }
688 @ignore_user_abort(false);
689}
690
691/**
692 * Callback function for logging into files. Not more than one file is created per minute,
693 * upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
694 *
695 * This function must not output any characters or throw warnigns and errors!
696 */
697function upgrade_log_callback($string) {
26c91c73 698 global $CFG, $upgradeloghandle, $upgradelogbuffer;
583fad99 699
700 if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) {
701 if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
26c91c73 702 $upgradelogbuffer .= $string;
703 if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer
704 @fwrite($upgradeloghandle, $upgradelogbuffer);
705 $upgradelogbuffer = '';
706 }
583fad99 707 } else {
708 $upgradeloghandle = 'error';
709 }
710 }
711 return $string;
712}
713
57e35f32 714/**
715 * Try to verify that dataroot is not accessible from web.
716 * It is not 100% correct but might help to reduce number of vulnerable sites.
717 *
718 * Protection from httpd.conf and .htaccess is not detected properly.
719 */
720function is_dataroot_insecure() {
721 global $CFG;
722
723 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
724
725 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
726 $rp = strrev(trim($rp, '/'));
727 $rp = explode('/', $rp);
728 foreach($rp as $r) {
729 if (strpos($siteroot, '/'.$r.'/') === 0) {
730 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
731 } else {
732 break; // probably alias root
733 }
734 }
735
736 $siteroot = strrev($siteroot);
737 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
738
739 if (strpos($dataroot, $siteroot) === 0) {
740 return true;
741 }
742 return false;
743}
6e4dc10f 744
745/// =============================================================================================================
746/// administration tree classes and functions
747
748
749// n.b. documentation is still in progress for this code
750
751/// INTRODUCTION
752
753/// This file performs the following tasks:
754/// -it defines the necessary objects and interfaces to build the Moodle
755/// admin hierarchy
eef868d1 756/// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
6e4dc10f 757/// and admin_externalpage_print_footer() functions used on admin pages
758
759/// ADMIN_SETTING OBJECTS
760
eef868d1 761/// Moodle settings are represented by objects that inherit from the admin_setting
6e4dc10f 762/// class. These objects encapsulate how to read a setting, how to write a new value
763/// to a setting, and how to appropriately display the HTML to modify the setting.
764
765/// ADMIN_SETTINGPAGE OBJECTS
766
767/// The admin_setting objects are then grouped into admin_settingpages. The latter
768/// appear in the Moodle admin tree block. All interaction with admin_settingpage
769/// objects is handled by the admin/settings.php file.
770
771/// ADMIN_EXTERNALPAGE OBJECTS
772
773/// There are some settings in Moodle that are too complex to (efficiently) handle
774/// with admin_settingpages. (Consider, for example, user management and displaying
775/// lists of users.) In this case, we use the admin_externalpage object. This object
776/// places a link to an external PHP file in the admin tree block.
777
778/// If you're using an admin_externalpage object for some settings, you can take
779/// advantage of the admin_externalpage_* functions. For example, suppose you wanted
780/// to add a foo.php file into admin. First off, you add the following line to
781/// admin/settings/first.php (at the end of the file) or to some other file in
782/// admin/settings:
783
eef868d1 784/// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
6e4dc10f 785/// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
786
787/// Next, in foo.php, your file structure would resemble the following:
788
789/// require_once('.../config.php');
790/// require_once($CFG->libdir.'/adminlib.php');
1ae083e4 791/// admin_externalpage_setup('foo');
6e4dc10f 792/// // functionality like processing form submissions goes here
1ae083e4 793/// admin_externalpage_print_header();
6e4dc10f 794/// // your HTML goes here
1ae083e4 795/// admin_externalpage_print_footer();
6e4dc10f 796
797/// The admin_externalpage_setup() function call ensures the user is logged in,
798/// and makes sure that they have the proper role permission to access the page.
799
800/// The admin_externalpage_print_header() function prints the header (it figures
801/// out what category and subcategories the page is classified under) and ensures
802/// that you're using the admin pagelib (which provides the admin tree block and
803/// the admin bookmarks block).
804
805/// The admin_externalpage_print_footer() function properly closes the tables
806/// opened up by the admin_externalpage_print_header() function and prints the
807/// standard Moodle footer.
808
809/// ADMIN_CATEGORY OBJECTS
810
811/// Above and beyond all this, we have admin_category objects. These objects
812/// appear as folders in the admin tree block. They contain admin_settingpage's,
813/// admin_externalpage's, and other admin_category's.
814
815/// OTHER NOTES
816
817/// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
818/// from part_of_admin_tree (a pseudointerface). This interface insists that
819/// a class has a check_access method for access permissions, a locate method
820/// used to find a specific node in the admin tree, and a path method used
821/// to determine the path to a specific node in the $ADMIN tree.
822
823/// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
824/// interface ensures that the class implements a recursive add function which
825/// accepts a part_of_admin_tree object and searches for the proper place to
826/// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
827
828/// Please note that the $this->name field of any part_of_admin_tree must be
829/// UNIQUE throughout the ENTIRE admin tree.
830
831/// The $this->name field of an admin_setting object (which is *not* part_of_
832/// admin_tree) must be unique on the respective admin_settingpage where it is
833/// used.
834
835
836/// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
837include_once($CFG->dirroot . '/backup/lib.php');
838
839/// CLASS DEFINITIONS /////////////////////////////////////////////////////////
840
841/**
842 * Pseudointerface for anything appearing in the admin tree
843 *
844 * The pseudointerface that is implemented by anything that appears in the admin tree
845 * block. It forces inheriting classes to define a method for checking user permissions
846 * and methods for finding something in the admin tree.
847 *
848 * @author Vincenzo K. Marcovecchio
849 * @package admin
850 */
851class part_of_admin_tree {
852
853 /**
854 * Finds a named part_of_admin_tree.
855 *
856 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
857 * and not parentable_part_of_admin_tree, then this function should only check if
858 * $this->name matches $name. If it does, it should return a reference to $this,
859 * otherwise, it should return a reference to NULL.
860 *
861 * If a class inherits parentable_part_of_admin_tree, this method should be called
862 * recursively on all child objects (assuming, of course, the parent object's name
863 * doesn't match the search criterion).
864 *
865 * @param string $name The internal name of the part_of_admin_tree we're searching for.
866 * @return mixed An object reference or a NULL reference.
867 */
eef868d1 868 function &locate($name) {
869 trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING);
870 return;
6e4dc10f 871 }
4672d955 872
873 /**
874 * Removes named part_of_admin_tree.
875 *
876 * @param string $name The internal name of the part_of_admin_tree we want to remove.
a8a66c96 877 * @return bool success.
4672d955 878 */
879 function prune($name) {
eef868d1 880 trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING);
4672d955 881 return;
eef868d1 882 }
4672d955 883
6e4dc10f 884 /**
885 * Verifies current user's access to this part_of_admin_tree.
886 *
887 * Used to check if the current user has access to this part of the admin tree or
888 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
889 * then this method is usually just a call to has_capability() in the site context.
890 *
891 * If a class inherits parentable_part_of_admin_tree, this method should return the
892 * logical OR of the return of check_access() on all child objects.
893 *
894 * @return bool True if the user has access, false if she doesn't.
895 */
eef868d1 896 function check_access() {
897 trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING);
898 return;
6e4dc10f 899 }
eef868d1 900
a8a66c96 901 /**
902 * Mostly usefull for removing of some parts of the tree in admin tree block.
903 *
904 * @return True is hidden from normal list view
905 */
906 function is_hidden() {
907 trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING);
908 return;
909 }
910
6e4dc10f 911 /**
912 * Determines the path to $name in the admin tree.
913 *
914 * Used to determine the path to $name in the admin tree. If a class inherits only
915 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
916 * check if $this->name matches $name. If it does, $name is pushed onto the $path
917 * array (at the end), and $path should be returned. If it doesn't, NULL should be
918 * returned.
919 *
920 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
921 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
922 * recursively calls path() on its child objects. If any are non-NULL, it should
923 * return $path (being certain that the last element of $path is equal to $name).
924 * If they are all NULL, it returns NULL.
925 *
926 * @param string $name The internal name of the part_of_admin_tree we're searching for.
927 * @param array $path Not used on external calls. Defaults to empty array.
928 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
929 */
eef868d1 930 function path($name, $path = array()) {
931 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING);
932 return;
6e4dc10f 933 }
934}
935
936/**
937 * Pseudointerface implemented by any part_of_admin_tree that has children.
938 *
939 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
940 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
eef868d1 941 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
6e4dc10f 942 * include an add method for adding other part_of_admin_tree objects as children.
943 *
944 * @author Vincenzo K. Marcovecchio
945 * @package admin
946 */
947class parentable_part_of_admin_tree extends part_of_admin_tree {
eef868d1 948
6e4dc10f 949 /**
950 * Adds a part_of_admin_tree object to the admin tree.
951 *
952 * Used to add a part_of_admin_tree object to this object or a child of this
953 * object. $something should only be added if $destinationname matches
954 * $this->name. If it doesn't, add should be called on child objects that are
955 * also parentable_part_of_admin_tree's.
956 *
957 * @param string $destinationname The internal name of the new parent for $something.
958 * @param part_of_admin_tree &$something The object to be added.
959 * @return bool True on success, false on failure.
960 */
eef868d1 961 function add($destinationname, &$something) {
962 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING);
963 return;
6e4dc10f 964 }
eef868d1 965
6e4dc10f 966}
967
968/**
969 * The object used to represent folders (a.k.a. categories) in the admin tree block.
eef868d1 970 *
6e4dc10f 971 * Each admin_category object contains a number of part_of_admin_tree objects.
972 *
973 * @author Vincenzo K. Marcovecchio
974 * @package admin
975 */
976class admin_category extends parentable_part_of_admin_tree {
977
978 /**
979 * @var mixed An array of part_of_admin_tree objects that are this object's children
980 */
981 var $children;
eef868d1 982
6e4dc10f 983 /**
984 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
985 */
986 var $name;
eef868d1 987
6e4dc10f 988 /**
989 * @var string The displayed name for this category. Usually obtained through get_string()
990 */
991 var $visiblename;
eef868d1 992
a8a66c96 993 /**
994 * @var bool Should this category be hidden in admin tree block?
995 */
996 var $hidden;
997
6e4dc10f 998 // constructor for an empty admin category
999 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
1000 // $visiblename is the displayed name of the category. use a get_string for this
1001
1002 /**
1003 * Constructor for an empty admin category
1004 *
1005 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
1006 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
a8a66c96 1007 * @param bool $hidden hide category in admin tree block
6e4dc10f 1008 * @return mixed Returns the new object.
1009 */
a8a66c96 1010 function admin_category($name, $visiblename, $hidden = false) {
6e4dc10f 1011 $this->children = array();
1012 $this->name = $name;
1013 $this->visiblename = $visiblename;
a8a66c96 1014 $this->hidden = $hidden;
6e4dc10f 1015 }
eef868d1 1016
6e4dc10f 1017 /**
1018 * Finds the path to the part_of_admin_tree called $name.
1019 *
1020 * @param string $name The internal name that we're searching for.
1021 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1022 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1023 */
1024 function path($name, $path = array()) {
eef868d1 1025
6e4dc10f 1026 $path[count($path)] = $this->name;
eef868d1 1027
6e4dc10f 1028 if ($this->name == $name) {
1029 return $path;
1030 }
eef868d1 1031
6e4dc10f 1032 foreach($this->children as $child) {
1033 if ($return = $child->path($name, $path)) {
1034 return $return;
1035 }
1036 }
eef868d1 1037
6e4dc10f 1038 return NULL;
eef868d1 1039
6e4dc10f 1040 }
1041
1042 /**
1043 * Returns a reference to the part_of_admin_tree object with internal name $name.
1044 *
1045 * @param string $name The internal name of the object we want.
1046 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1047 */
1048 function &locate($name) {
eef868d1 1049
6e4dc10f 1050 if ($this->name == $name) {
1051 return $this;
1052 }
eef868d1 1053
6e4dc10f 1054 foreach($this->children as $child) {
1055 if ($return =& $child->locate($name)) {
1056 return $return;
1057 }
1058 }
1059 $return = NULL;
1060 return $return;
1061 }
1062
4672d955 1063 /**
1064 * Removes part_of_admin_tree object with internal name $name.
1065 *
1066 * @param string $name The internal name of the object we want to remove.
a8a66c96 1067 * @return bool success
4672d955 1068 */
1069 function prune($name) {
1070
1071 if ($this->name == $name) {
1072 return false; //can not remove itself
1073 }
1074
1075 foreach($this->children as $precedence => $child) {
1076 if ($child->name == $name) {
1077 // found it!
eef868d1 1078 unset($this->children[$precedence]);
4672d955 1079 return true;
1080 }
1081 if ($this->children[$precedence]->prune($name)) {
1082 return true;
1083 }
1084 }
1085 return false;
1086 }
1087
6e4dc10f 1088 /**
1089 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
1090 *
1091 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
1092 * @param mixed &$something A part_of_admin_tree object to be added.
1093 * @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".
1094 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
1095 */
1096 function add($destinationname, &$something, $precedence = '') {
eef868d1 1097
6e4dc10f 1098 if (!is_a($something, 'part_of_admin_tree')) {
1099 return false;
1100 }
1101
1102 if ($destinationname == $this->name) {
1103 if ($precedence === '') {
1104 $this->children[] = $something;
1105 } else {
1106 if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
1107 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 />');
1108 }
1109 $this->children[$precedence] = $something;
1110 }
1111 return true;
1112 }
eef868d1 1113
6e4dc10f 1114 unset($entries);
eef868d1 1115
6e4dc10f 1116 $entries = array_keys($this->children);
eef868d1 1117
6e4dc10f 1118 foreach($entries as $entry) {
1119 $child =& $this->children[$entry];
1120 if (is_a($child, 'parentable_part_of_admin_tree')) {
1121 if ($child->add($destinationname, $something, $precedence)) {
1122 return true;
1123 }
1124 }
1125 }
eef868d1 1126
6e4dc10f 1127 return false;
eef868d1 1128
6e4dc10f 1129 }
eef868d1 1130
6e4dc10f 1131 /**
1132 * Checks if the user has access to anything in this category.
1133 *
1134 * @return bool True if the user has access to atleast one child in this category, false otherwise.
1135 */
1136 function check_access() {
eef868d1 1137
6e4dc10f 1138 $return = false;
1139 foreach ($this->children as $child) {
1140 $return = $return || $child->check_access();
1141 }
eef868d1 1142
6e4dc10f 1143 return $return;
eef868d1 1144
6e4dc10f 1145 }
eef868d1 1146
a8a66c96 1147 /**
1148 * Is this category hidden in admin tree block?
1149 *
1150 * @return bool True if hidden
1151 */
1152 function is_hidden() {
1153 return $this->hidden;
1154 }
6e4dc10f 1155}
1156
1157/**
1158 * Links external PHP pages into the admin tree.
1159 *
1160 * See detailed usage example at the top of this document (adminlib.php)
1161 *
1162 * @author Vincenzo K. Marcovecchio
1163 * @package admin
1164 */
1165class admin_externalpage extends part_of_admin_tree {
1166
eef868d1 1167 /**
6e4dc10f 1168 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1169 */
1170 var $name;
eef868d1 1171
6e4dc10f 1172 /**
1173 * @var string The displayed name for this external page. Usually obtained through get_string().
1174 */
1175 var $visiblename;
eef868d1 1176
6e4dc10f 1177 /**
1178 * @var string The external URL that we should link to when someone requests this external page.
1179 */
1180 var $url;
eef868d1 1181
6e4dc10f 1182 /**
1183 * @var string The role capability/permission a user must have to access this external page.
1184 */
2ce38b70 1185 var $req_capability;
eef868d1 1186
84c8ede0 1187 /**
1188 * @var object The context in which capability/permission should be checked, default is site context.
1189 */
1190 var $context;
1191
a8a66c96 1192 /**
1193 * @var bool hidden in admin tree block.
1194 */
1195 var $hidden;
1196
6e4dc10f 1197 /**
1198 * Constructor for adding an external page into the admin tree.
1199 *
1200 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1201 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1202 * @param string $url The external URL that we should link to when someone requests this external page.
38d2d43b 1203 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
6e4dc10f 1204 */
01e85af9 1205 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
6e4dc10f 1206 $this->name = $name;
1207 $this->visiblename = $visiblename;
1208 $this->url = $url;
38d2d43b 1209 if (is_array($req_capability)) {
1210 $this->req_capability = $req_capability;
1211 } else {
1212 $this->req_capability = array($req_capability);
1213 }
a8a66c96 1214 $this->hidden = $hidden;
84c8ede0 1215 $this->context = $context;
6e4dc10f 1216 }
eef868d1 1217
6e4dc10f 1218 /**
1219 * Finds the path to the part_of_admin_tree called $name.
1220 *
1221 * @param string $name The internal name that we're searching for.
1222 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1223 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1224 */
1225 function path($name, $path = array()) {
1226 if ($name == $this->name) {
1227 array_push($path, $this->name);
1228 return $path;
1229 } else {
1230 return NULL;
1231 }
1232 }
eef868d1 1233
6e4dc10f 1234 /**
1235 * Returns a reference to the part_of_admin_tree object with internal name $name.
1236 *
1237 * @param string $name The internal name of the object we want.
1238 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1239 */
1240 function &locate($name) {
1241 $return = ($this->name == $name ? $this : NULL);
1242 return $return;
1243 }
4672d955 1244
1245 function prune($name) {
1246 return false;
1247 }
1248
6e4dc10f 1249 /**
2ce38b70 1250 * Determines if the current user has access to this external page based on $this->req_capability.
6e4dc10f 1251 *
1252 * @uses CONTEXT_SYSTEM
1253 * @uses SITEID
1254 * @return bool True if user has access, false otherwise.
1255 */
1256 function check_access() {
1257 if (!get_site()) {
1258 return true; // no access check before site is fully set up
1259 }
84c8ede0 1260 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
38d2d43b 1261 foreach($this->req_capability as $cap) {
1262 if (has_capability($cap, $context)) {
1263 return true;
1264 }
1265 }
1266 return false;
6e4dc10f 1267 }
1268
a8a66c96 1269 /**
1270 * Is this external page hidden in admin tree block?
1271 *
1272 * @return bool True if hidden
1273 */
1274 function is_hidden() {
1275 return $this->hidden;
1276 }
1277
6e4dc10f 1278}
1279
1280/**
1281 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1282 *
1283 * @author Vincenzo K. Marcovecchio
1284 * @package admin
1285 */
1286class admin_settingpage extends part_of_admin_tree {
1287
eef868d1 1288 /**
6e4dc10f 1289 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1290 */
1291 var $name;
eef868d1 1292
6e4dc10f 1293 /**
1294 * @var string The displayed name for this external page. Usually obtained through get_string().
1295 */
1296 var $visiblename;
1297 /**
1298 * @var mixed An array of admin_setting objects that are part of this setting page.
1299 */
1300 var $settings;
eef868d1 1301
6e4dc10f 1302 /**
1303 * @var string The role capability/permission a user must have to access this external page.
1304 */
2ce38b70 1305 var $req_capability;
eef868d1 1306
84c8ede0 1307 /**
1308 * @var object The context in which capability/permission should be checked, default is site context.
1309 */
1310 var $context;
1311
a8a66c96 1312 /**
1313 * @var bool hidden in admin tree block.
1314 */
1315 var $hidden;
1316
6e4dc10f 1317 // see admin_category
1318 function path($name, $path = array()) {
1319 if ($name == $this->name) {
1320 array_push($path, $this->name);
1321 return $path;
1322 } else {
1323 return NULL;
1324 }
1325 }
eef868d1 1326
6e4dc10f 1327 // see admin_category
1328 function &locate($name) {
1329 $return = ($this->name == $name ? $this : NULL);
1330 return $return;
1331 }
4672d955 1332
1333 function prune($name) {
1334 return false;
1335 }
1336
6e4dc10f 1337 // see admin_externalpage
01e85af9 1338 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config', $hidden=false, $context=NULL) {
6e4dc10f 1339 global $CFG;
1340 $this->settings = new stdClass();
1341 $this->name = $name;
1342 $this->visiblename = $visiblename;
38d2d43b 1343 if (is_array($req_capability)) {
1344 $this->req_capability = $req_capability;
1345 } else {
1346 $this->req_capability = array($req_capability);
1347 }
a8a66c96 1348 $this->hidden = false;
84c8ede0 1349 $this->context = $context;
6e4dc10f 1350 }
eef868d1 1351
6e4dc10f 1352 // 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
1353 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1354 // &$setting is the admin_setting object you want to add
1355 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1356 function add(&$setting) {
1357 if (is_a($setting, 'admin_setting')) {
1358 $this->settings->{$setting->name} =& $setting;
1359 return true;
1360 }
1361 return false;
1362 }
eef868d1 1363
6e4dc10f 1364 // see admin_externalpage
1365 function check_access() {
1366 if (!get_site()) {
1367 return true; // no access check before site is fully set up
1368 }
84c8ede0 1369 $context = empty($this->context) ? get_context_instance(CONTEXT_SYSTEM) : $this->context;
38d2d43b 1370 foreach($this->req_capability as $cap) {
1371 if (has_capability($cap, $context)) {
1372 return true;
1373 }
1374 }
1375 return false;
6e4dc10f 1376 }
eef868d1 1377
6e4dc10f 1378 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1379 // returns a string of the html
1380 function output_html() {
15a16922 1381 $return = '<fieldset>' . "\n";
6153cf58 1382 $return .= '<div class="clearer"><!-- --></div>' . "\n";
6e4dc10f 1383 foreach($this->settings as $setting) {
15a16922 1384 $return .= $setting->output_html();
6e4dc10f 1385 }
6153cf58 1386 $return .= '</fieldset>';
6e4dc10f 1387 return $return;
1388 }
1389
1390 // 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
1391 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1392 // $data should be the result from data_submitted()
1393 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1394 function write_settings($data) {
1395 $return = '';
1396 foreach($this->settings as $setting) {
1397 if (isset($data['s_' . $setting->name])) {
1398 $return .= $setting->write_setting($data['s_' . $setting->name]);
1399 } else {
1400 $return .= $setting->write_setting('');
1401 }
1402 }
1403 return $return;
1404 }
1405
a8a66c96 1406 /**
1407 * Is this settigns page hidden in admin tree block?
1408 *
1409 * @return bool True if hidden
1410 */
1411 function is_hidden() {
1412 return $this->hidden;
1413 }
1414
6e4dc10f 1415}
1416
1417
1418// read & write happens at this level; no authentication
1419class admin_setting {
1420
1421 var $name;
1422 var $visiblename;
1423 var $description;
1424 var $defaultsetting;
1425
1426 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1427 $this->name = $name;
1428 $this->visiblename = $visiblename;
1429 $this->description = $description;
1430 $this->defaultsetting = $defaultsetting;
1431 }
eef868d1 1432
6e4dc10f 1433 function get_setting() {
1434 return NULL; // has to be overridden
1435 }
eef868d1 1436
8e5da17a 1437 function get_defaultsetting() {
1438 return $this->defaultsetting;
1439 }
1440
6e4dc10f 1441 function write_setting($data) {
1442 return; // has to be overridden
1443 }
eef868d1 1444
6e4dc10f 1445 function output_html() {
1446 return; // has to be overridden
1447 }
eef868d1 1448
6e4dc10f 1449}
1450
1451
1452class admin_setting_configtext extends admin_setting {
1453
1454 var $paramtype;
1455
50999a0b 1456 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
6e4dc10f 1457 $this->paramtype = $paramtype;
1458 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1459 }
1460
cc73de71 1461 // returns a string or NULL
6e4dc10f 1462 function get_setting() {
1463 global $CFG;
c8218a42 1464 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
6e4dc10f 1465 }
eef868d1 1466
cc73de71 1467 // $data is a string
6e4dc10f 1468 function write_setting($data) {
9e24fbd1 1469 if (!$this->validate($data)) {
1470 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
c235598d 1471 }
6e4dc10f 1472 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1473 }
1474
c235598d 1475 function validate($data) {
9e24fbd1 1476 if (is_string($this->paramtype)) {
1477 return preg_match($this->paramtype, $data);
1478 } else if ($this->paramtype === PARAM_RAW) {
1479 return true;
1480 } else {
1481 $cleaned = clean_param($data, $this->paramtype);
1482 return ("$data" == "$cleaned"); // implicit conversion to string is needed to do exact comparison
1483 }
c235598d 1484 }
1485
6e4dc10f 1486 function output_html() {
c8218a42 1487 if ($this->get_setting() === NULL) {
1488 $current = $this->defaultsetting;
1489 } else {
1490 $current = $this->get_setting();
1491 }
9e24fbd1 1492 return format_admin_setting($this->name, $this->visiblename,
5259d5d8 1493 '<input type="text" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />',
6153cf58 1494 $this->description);
6e4dc10f 1495 }
1496
1497}
1498
54bb33eb 1499class admin_setting_configpasswordunmask extends admin_setting_configtext {
eba8cd63 1500
54bb33eb 1501 function admin_setting_configpasswordunmask($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
eba8cd63 1502 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
1503 }
1504
1505 function output_html() {
1506 if ($this->get_setting() === NULL) {
1507 $current = $this->defaultsetting;
1508 } else {
1509 $current = $this->get_setting();
1510 }
1511 $id = 'id_s_'.$this->name;
54bb33eb 1512 $unmask = get_string('unmaskpassword', 'form');
1513 $unmaskjs = '<script type="text/javascript">
eba8cd63 1514//<![CDATA[
54bb33eb 1515document.write(\'<div class="unmask"><input id="'.$id.'unmask" value="1" type="checkbox" onclick="unmaskPassword(\\\''.$id.'\\\')"/><label for="'.$id.'unmask">'.addslashes_js($unmask).'<\/label><\/div>\');
eba8cd63 1516//]]>
1517</script>';
1518 return format_admin_setting($this->name, $this->visiblename,
54bb33eb 1519 '<input type="password" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.s($current).'" />'.$unmaskjs,
eba8cd63 1520 $this->description);
1521 }
9ba38673 1522
eba8cd63 1523}
1524
6e4dc10f 1525class admin_setting_configcheckbox extends admin_setting {
1526
1527 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1528 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1529 }
1530
1531 function get_setting() {
1532 global $CFG;
1533 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1534 }
eef868d1 1535
6e4dc10f 1536 function write_setting($data) {
1537 if ($data == '1') {
1538 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1539 } else {
1540 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1541 }
1542 }
1543
1544 function output_html() {
c8218a42 1545 if ($this->get_setting() === NULL) {
1546 $current = $this->defaultsetting;
1547 } else {
1548 $current = $this->get_setting();
1549 }
9e24fbd1 1550 return format_admin_setting($this->name, $this->visiblename,
3c6f7a78 1551 '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($current == true ? 'checked="checked"' : '') . ' />',
6153cf58 1552 $this->description);
6e4dc10f 1553 }
1554
1555}
1556
1557class admin_setting_configselect extends admin_setting {
1558
1559 var $choices;
eef868d1 1560
6e4dc10f 1561 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1562 $this->choices = $choices;
1563 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1564 }
1565
0a784551 1566 /**
1567 * This function may be used in ancestors for lazy loading of choices
1568 */
1569 function load_choices() {
1570 /*
1571 if (!empty($this->choices)) {
1572 return;
1573 }
1574 .... load choices here
1575 */
1576 }
1577
6e4dc10f 1578 function get_setting() {
1579 global $CFG;
1580 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1581 }
eef868d1 1582
6e4dc10f 1583 function write_setting($data) {
0a784551 1584 $this->load_choices();
6e4dc10f 1585 // check that what we got was in the original choices
1586 // or that the data is the default setting - needed during install when choices can not be constructed yet
1587 if ($data != $this->defaultsetting and ! in_array($data, array_keys($this->choices))) {
1588 return 'Error setting ' . $this->visiblename . '<br />';
1589 }
eef868d1 1590
6e4dc10f 1591 return (set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1592 }
eef868d1 1593
6e4dc10f 1594 function output_html() {
0a784551 1595 $this->load_choices();
c8218a42 1596 if ($this->get_setting() === NULL) {
1597 $current = $this->defaultsetting;
1598 } else {
1599 $current = $this->get_setting();
1600 }
afee9864 1601 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'">';
6e4dc10f 1602 foreach ($this->choices as $key => $value) {
a1069b07 1603 // the string cast is needed because key may be integer - 0 is equal to most strings!
1604 $return .= '<option value="'.$key.'"'.((string)$key==$current ? ' selected="selected"' : '').'>'.$value.'</option>';
6e4dc10f 1605 }
6153cf58 1606 $return .= '</select>';
1607
fc52aa6e 1608 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1609 }
1610
1611}
1612
1613// 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
1614// internally for the setting)
1615class admin_setting_configtime extends admin_setting {
1616
1617 var $name2;
1618 var $choices;
1619 var $choices2;
6e4dc10f 1620
1621 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1622 $this->name2 = $minutesname;
1623 $this->choices = array();
1624 for ($i = 0; $i < 24; $i++) {
1625 $this->choices[$i] = $i;
1626 }
1627 $this->choices2 = array();
1628 for ($i = 0; $i < 60; $i += 5) {
1629 $this->choices2[$i] = $i;
1630 }
1631 parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1632 }
1633
1634 function get_setting() {
1635 global $CFG;
cc73de71 1636 return (isset($CFG->{$this->name}) && isset($CFG->{$this->name2}) ? array('h' => $CFG->{$this->name}, 'm' => $CFG->{$this->name2}) : NULL);
6e4dc10f 1637 }
eef868d1 1638
6e4dc10f 1639 function write_setting($data) {
1640 // check that what we got was in the original choices
1641 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
1642 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1643 }
eef868d1 1644
6e4dc10f 1645 return (set_config($this->name, $data['h']) && set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1646 }
eef868d1 1647
6e4dc10f 1648 function output_html() {
cc73de71 1649 if ($this->get_setting() === NULL) {
1650 $currentsetting = $this->defaultsetting;
1651 } else {
1652 $currentsetting = $this->get_setting();
6e4dc10f 1653 }
2e0c11dd 1654 $return = '<div class="form-group">'.
1655 '<select class="form-select" id="id_s_'.$this->name.'h" name="s_' . $this->name .'[h]">';
6e4dc10f 1656 foreach ($this->choices as $key => $value) {
cc73de71 1657 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
6e4dc10f 1658 }
2e0c11dd 1659 $return .= '</select>:<select class="form-select" id="id_s_'.$this->name.'m" name="s_' . $this->name . '[m]">';
6e4dc10f 1660 foreach ($this->choices2 as $key => $value) {
cc73de71 1661 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
eef868d1 1662 }
2e0c11dd 1663 $return .= '</select></div>';
fbe31d22 1664 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
6e4dc10f 1665 }
1666
1667}
1668
1669class admin_setting_configmultiselect extends admin_setting_configselect {
1670
1671 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1672 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1673 }
1674
1675 function get_setting() {
1676 global $CFG;
d7933a55 1677 if (isset($CFG->{$this->name})) {
1678 if ($CFG->{$this->name}) {
1679 return explode(',', $CFG->{$this->name});
1680 } else {
1681 return array();
9ba38673 1682 }
d7933a55 1683 } else {
1684 return NULL;
1685 }
6e4dc10f 1686 }
eef868d1 1687
6e4dc10f 1688 function write_setting($data) {
d7933a55 1689 if (empty($data)) {
1690 $data = array();
1691 }
6e4dc10f 1692 foreach ($data as $datum) {
1693 if (! in_array($datum, array_keys($this->choices))) {
1694 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1695 }
1696 }
eef868d1 1697
6e4dc10f 1698 return (set_config($this->name, implode(',',$data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1699 }
eef868d1 1700
6e4dc10f 1701 function output_html() {
cc73de71 1702 if ($this->get_setting() === NULL) {
1703 $currentsetting = $this->defaultsetting;
0b0f7c00 1704 if (!$currentsetting) {
1705 $currentsetting = array();
1706 }
cc73de71 1707 } else {
1708 $currentsetting = $this->get_setting();
6e4dc10f 1709 }
afee9864 1710 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'[]" size="10" multiple="multiple">';
6e4dc10f 1711 foreach ($this->choices as $key => $value) {
1712 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ? ' selected="selected"' : '') . '>' . $value . '</option>';
1713 }
6153cf58 1714 $return .= '</select>';
fc52aa6e 1715 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1716 }
1717
1718}
1719
1720class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
eef868d1 1721
6e4dc10f 1722 function admin_setting_special_adminseesall() {
1723 $name = 'calendar_adminseesall';
1724 $visiblename = get_string('adminseesall', 'admin');
1725 $description = get_string('helpadminseesall', 'admin');
1726 parent::admin_setting($name, $visiblename, $description, 0);
1727 }
1728
1729 function write_setting($data) {
1730 global $SESSION;
1731 unset($SESSION->cal_courses_shown);
1732 parent::write_setting($data);
1733 }
1734}
1735
1736class admin_setting_sitesetselect extends admin_setting_configselect {
1737
1738 var $id;
1739
1740 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1741
1742 $this->id = SITEID;
1743 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
eef868d1 1744
6e4dc10f 1745 }
eef868d1 1746
6e4dc10f 1747 function get_setting() {
1748 $site = get_site();
1749 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1750 }
eef868d1 1751
6e4dc10f 1752 function write_setting($data) {
1753 if (!in_array($data, array_keys($this->choices))) {
1754 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1755 }
1756 $record = new stdClass();
1757 $record->id = $this->id;
1758 $temp = $this->name;
1759 $record->$temp = $data;
1760 $record->timemodified = time();
1761 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1762 }
eef868d1 1763
6e4dc10f 1764}
1765
1766
e0f6e995 1767class admin_setting_courselist_frontpage extends admin_setting_configselect {
6e4dc10f 1768
e0f6e995 1769 function admin_setting_courselist_frontpage($loggedin) {
6e4dc10f 1770 global $CFG;
1771 require_once($CFG->dirroot . '/course/lib.php');
1772 $name = 'frontpage' . ($loggedin ? 'loggedin' : '');
1773 $visiblename = get_string('frontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1774 $description = get_string('configfrontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1775 $choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
1776 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
1777 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
1778 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
1779 '' => get_string('none'));
9fec4e33 1780 if (!$loggedin and count_records("course") > FRONTPAGECOURSELIMIT) {
6e4dc10f 1781 unset($choices[FRONTPAGECOURSELIST]);
1782 }
4672d955 1783 $defaults = FRONTPAGECOURSELIST.',,,';
e0f6e995 1784 parent::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
6e4dc10f 1785 }
eef868d1 1786
6e4dc10f 1787 function get_setting() {
1788 global $CFG;
1c1e7af4 1789 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : ',1,,');
6e4dc10f 1790 }
eef868d1 1791
6e4dc10f 1792 function write_setting($data) {
1793 if (empty($data)) {
1794 $data = array();
e0f6e995 1795 } if (!is_array($data)) {
1796 $data = explode(',', $data);
6e4dc10f 1797 }
1798 foreach($data as $datum) {
1799 if (! in_array($datum, array_keys($this->choices))) {
1800 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1801 }
1802 }
1803 return (set_config($this->name, implode(',', $data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1804 }
eef868d1 1805
6e4dc10f 1806 function output_html() {
cc73de71 1807 if ($this->get_setting() === NULL) {
1808 $currentsetting = $this->defaultsetting;
1809 } else {
1810 $currentsetting = $this->get_setting();
6e4dc10f 1811 }
1812 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1813 if (!isset($currentsetting[$i])) {
1814 $currentsetting[$i] = 0;
1815 }
1816 }
0a7e84c3 1817 $return = '<div class="form-group">';
6e4dc10f 1818 for ($i = 0; $i < count($this->choices) - 1; $i++) {
a1d52643 1819 $return .='<select class="form-select" id="id_s_'.$this->name.$i.'" name="s_' . $this->name .'[]">';
6e4dc10f 1820 foreach ($this->choices as $key => $value) {
1821 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1822 }
1823 $return .= '</select>';
1824 if ($i !== count($this->choices) - 2) {
975211bb 1825 $return .= '<br />';
6e4dc10f 1826 }
1827 }
0a7e84c3 1828 $return .= '</div>';
1829
fbe31d22 1830 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
6e4dc10f 1831 }
1832}
1833
1834class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
1835
1836 var $id;
1837
1838 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1839
1840 $this->id = SITEID;
1841 parent::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
eef868d1 1842
6e4dc10f 1843 }
eef868d1 1844
6e4dc10f 1845 function get_setting() {
1846 $site = get_site();
8e5da17a 1847 return $site->{$this->name};
6e4dc10f 1848 }
eef868d1 1849
6e4dc10f 1850 function write_setting($data) {
1851 $record = new stdClass();
1852 $record->id = $this->id;
1853 $temp = $this->name;
1854 $record->$temp = ($data == '1' ? 1 : 0);
1855 $record->timemodified = time();
1856 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1857 }
eef868d1 1858
6e4dc10f 1859}
1860
1861class admin_setting_sitesettext extends admin_setting_configtext {
1862
1863 var $id;
1864
b89639f9 1865 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting) {
6e4dc10f 1866
1867 $this->id = SITEID;
b89639f9 1868 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting);
eef868d1 1869
6e4dc10f 1870 }
eef868d1 1871
6e4dc10f 1872 function get_setting() {
1873 $site = get_site();
8e5da17a 1874 return $site->{$this->name} != '' ? $site->{$this->name} : NULL;
6e4dc10f 1875 }
90cfbd0a 1876
b89639f9 1877 function validate($data) {
b0abd282 1878 $cleaned = stripslashes(clean_param($data, PARAM_MULTILANG));
b89639f9 1879 if ($cleaned == '') {
1880 return false; // can not be empty
1881 }
b0abd282 1882 return ($data == $cleaned); // implicit conversion to string is needed to do exact comparison
b89639f9 1883 }
1884
6e4dc10f 1885 function write_setting($data) {
b89639f9 1886 $data = trim($data);
9e24fbd1 1887 if (!$this->validate($data)) {
1888 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
90cfbd0a 1889 }
eef868d1 1890
6e4dc10f 1891 $record = new stdClass();
1892 $record->id = $this->id;
87fa8a17 1893 $record->{$this->name} = addslashes($data);
6e4dc10f 1894 $record->timemodified = time();
23a97d83 1895 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 1896 }
90cfbd0a 1897
6e4dc10f 1898}
1899
1900class admin_setting_special_frontpagedesc extends admin_setting {
1901
1902 var $id;
eef868d1 1903
6e4dc10f 1904 function admin_setting_special_frontpagedesc() {
1905 $this->id = SITEID;
1906 $name = 'summary';
1907 $visiblename = get_string('frontpagedescription');
1908 $description = get_string('frontpagedescriptionhelp');
1909 parent::admin_setting($name, $visiblename, $description, '');
1910 }
1911
1912 function output_html() {
fa040029 1913 global $CFG;
1914
fa040029 1915 $CFG->adminusehtmleditor = can_use_html_editor();
c626c2f4 1916 $return = print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, 's_' . $this->name, $this->get_setting(), 0, true);
eef868d1 1917
fbe31d22 1918 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
6e4dc10f 1919 }
eef868d1 1920
6e4dc10f 1921 function get_setting() {
6e4dc10f 1922 $site = get_site();
c626c2f4 1923 return $site->{$this->name};
1924 }
eef868d1 1925
c626c2f4 1926 function get_defaultsetting() {
1927 return NULL;
6e4dc10f 1928 }
eef868d1 1929
6e4dc10f 1930 function write_setting($data) {
c626c2f4 1931 $record = new object();
1932 $record->id = $this->id;
1933 $record->{$this->name} = addslashes($data);
1934 $record->timemodified = time();
eef868d1 1935
6e4dc10f 1936 return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 1937 }
1938
1939}
1940
1941
1942class admin_setting_special_editorfontlist extends admin_setting {
1943
1944 var $items;
1945
1946 function admin_setting_special_editorfontlist() {
1947 global $CFG;
1948 $name = 'editorfontlist';
1949 $visiblename = get_string('editorfontlist', 'admin');
1950 $description = get_string('configeditorfontlist', 'admin');
6e4dc10f 1951 $defaults = array('k0' => 'Trebuchet',
1952 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1953 'k1' => 'Arial',
1954 'v1' => 'arial,helvetica,sans-serif',
1955 'k2' => 'Courier New',
1956 'v2' => 'courier new,courier,monospace',
1957 'k3' => 'Georgia',
1958 'v3' => 'georgia,times new roman,times,serif',
1959 'k4' => 'Tahoma',
1960 'v4' => 'tahoma,arial,helvetica,sans-serif',
1961 'k5' => 'Times New Roman',
1962 'v5' => 'times new roman,times,serif',
1963 'k6' => 'Verdana',
1964 'v6' => 'verdana,arial,helvetica,sans-serif',
1965 'k7' => 'Impact',
1966 'v7' => 'impact',
1967 'k8' => 'Wingdings',
1968 'v8' => 'wingdings');
1969 parent::admin_setting($name, $visiblename, $description, $defaults);
1970 }
eef868d1 1971
6e4dc10f 1972 function get_setting() {
cc73de71 1973 global $CFG;
1974 if (isset($CFG->editorfontlist)) {
1975 $i = 0;
1976 $currentsetting = array();
1977 $items = explode(';', $CFG->editorfontlist);
1978 foreach ($items as $item) {
1979 $item = explode(':', $item);
1980 $currentsetting['k' . $i] = $item[0];
1981 $currentsetting['v' . $i] = $item[1];
1982 $i++;
1983 }
1984 return $currentsetting;
1985 } else {
1986 return NULL;
1987 }
6e4dc10f 1988 }
eef868d1 1989
6e4dc10f 1990 function write_setting($data) {
eef868d1 1991
6e4dc10f 1992 // there miiight be an easier way to do this :)
1993 // if this is changed, make sure the $defaults array above is modified so that this
1994 // function processes it correctly
eef868d1 1995
6e4dc10f 1996 $keys = array();
1997 $values = array();
eef868d1 1998
6e4dc10f 1999 foreach ($data as $key => $value) {
2000 if (substr($key,0,1) == 'k') {
2001 $keys[substr($key,1)] = $value;
2002 } elseif (substr($key,0,1) == 'v') {
2003 $values[substr($key,1)] = $value;
2004 }
2005 }
eef868d1 2006
6e4dc10f 2007 $result = '';
2008 for ($i = 0; $i < count($keys); $i++) {
2009 if (($keys[$i] !== '') && ($values[$i] !== '')) {
2010 $result .= clean_param($keys[$i],PARAM_NOTAGS) . ':' . clean_param($values[$i], PARAM_NOTAGS) . ';';
2011 }
2012 }
eef868d1 2013
6e4dc10f 2014 $result = substr($result, 0, -1); // trim the last semicolon
eef868d1 2015
6e4dc10f 2016 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2017 }
eef868d1 2018
6e4dc10f 2019 function output_html() {
eef868d1 2020
cc73de71 2021 if ($this->get_setting() === NULL) {
2022 $currentsetting = $this->defaultsetting;
2023 } else {
2024 $currentsetting = $this->get_setting();
6e4dc10f 2025 }
eef868d1 2026
1beed35f 2027 $return = '<div class="form-group">';
cc73de71 2028 for ($i = 0; $i < count($currentsetting) / 2; $i++) {
3c6f7a78 2029 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
6e4dc10f 2030 $return .= '&nbsp;&nbsp;';
3c6f7a78 2031 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
6e4dc10f 2032 }
3c6f7a78 2033 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="" />';
6e4dc10f 2034 $return .= '&nbsp;&nbsp;';
3c6f7a78 2035 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="" /><br />';
2036 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . ($i + 1) . ']" value="" />';
6e4dc10f 2037 $return .= '&nbsp;&nbsp;';
3c6f7a78 2038 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . ($i + 1) . ']" value="" />';
1beed35f 2039 $return .= '</div>';
6153cf58 2040
60a9a6ea 2041 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
6e4dc10f 2042 }
eef868d1 2043
6e4dc10f 2044}
2045
2046class admin_setting_special_editordictionary extends admin_setting_configselect {
2047
2048 function admin_setting_special_editordictionary() {
2049 $name = 'editordictionary';
2050 $visiblename = get_string('editordictionary','admin');
2051 $description = get_string('configeditordictionary', 'admin');
2052 $choices = $this->editor_get_dictionaries();
2053 if (! is_array($choices)) {
2054 $choices = array('');
2055 }
eef868d1 2056
6e4dc10f 2057 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
2058 }
2059
2060 // function borrowed from the old moodle/admin/editor.php, slightly modified
2061 function editor_get_dictionaries () {
2062 /// Get all installed dictionaries in the system
2063
2064 global $CFG;
eef868d1 2065
6e4dc10f 2066// error_reporting(E_ALL); // for debug, final version shouldn't have this...
2067 clearstatcache();
2068
2069 // If aspellpath isn't set don't even bother ;-)
2070 if (empty($CFG->aspellpath)) {
2071 return 'Empty aspell path!';
2072 }
2073
2074 // Do we have access to popen function?
2075 if (!function_exists('popen')) {
2076 return 'Popen function disabled!';
2077 }
eef868d1 2078
6e4dc10f 2079 $cmd = $CFG->aspellpath;
2080 $output = '';
2081 $dictionaries = array();
2082 $dicts = array();
2083
2084 if(!($handle = @popen(escapeshellarg($cmd) .' dump dicts', 'r'))) {
2085 return 'Couldn\'t create handle!';
2086 }
2087
2088 while(!feof($handle)) {
2089 $output .= fread($handle, 1024);
2090 }
2091 @pclose($handle);
2092
2093 $dictionaries = explode(chr(10), $output);
2094
2095 // Get rid of possible empty values
2096 if (is_array($dictionaries)) {
2097
2098 $cnt = count($dictionaries);
2099
2100 for ($i = 0; $i < $cnt; $i++) {
2101 if (!empty($dictionaries[$i])) {
2c41b2d7 2102 $dicts[$dictionaries[$i]] = $dictionaries[$i];
6e4dc10f 2103 }
2104 }
2105 }
2106
2107 if (count($dicts) >= 1) {
2108 return $dicts;
2109 }
2110
2111 return 'Error! Check your aspell installation!';
2112 }
2113
eef868d1 2114
6e4dc10f 2115
2116}
2117
2118
2119class admin_setting_special_editorhidebuttons extends admin_setting {
2120
2121 var $name;
2122 var $visiblename;
2123 var $description;
2124 var $items;
2125
2126 function admin_setting_special_editorhidebuttons() {
2127 $this->name = 'editorhidebuttons';
2128 $this->visiblename = get_string('editorhidebuttons', 'admin');
2129 $this->description = get_string('confeditorhidebuttons', 'admin');
2130 $this->defaultsetting = array();
2131 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
2132 $this->items = array('fontname' => '',
2133 'fontsize' => '',
2134 'formatblock' => '',
2135 'bold' => 'ed_format_bold.gif',
2136 'italic' => 'ed_format_italic.gif',
2137 'underline' => 'ed_format_underline.gif',
2138 'strikethrough' => 'ed_format_strike.gif',
2139 'subscript' => 'ed_format_sub.gif',
2140 'superscript' => 'ed_format_sup.gif',
2141 'copy' => 'ed_copy.gif',
2142 'cut' => 'ed_cut.gif',
2143 'paste' => 'ed_paste.gif',
2144 'clean' => 'ed_wordclean.gif',
2145 'undo' => 'ed_undo.gif',
2146 'redo' => 'ed_redo.gif',
2147 'justifyleft' => 'ed_align_left.gif',
2148 'justifycenter' => 'ed_align_center.gif',
2149 'justifyright' => 'ed_align_right.gif',
2150 'justifyfull' => 'ed_align_justify.gif',
2151 'lefttoright' => 'ed_left_to_right.gif',
2152 'righttoleft' => 'ed_right_to_left.gif',
2153 'insertorderedlist' => 'ed_list_num.gif',
2154 'insertunorderedlist' => 'ed_list_bullet.gif',
2155 'outdent' => 'ed_indent_less.gif',
2156 'indent' => 'ed_indent_more.gif',
2157 'forecolor' => 'ed_color_fg.gif',
2158 'hilitecolor' => 'ed_color_bg.gif',
2159 'inserthorizontalrule' => 'ed_hr.gif',
2160 'createanchor' => 'ed_anchor.gif',
2161 'createlink' => 'ed_link.gif',
2162 'unlink' => 'ed_unlink.gif',
2163 'insertimage' => 'ed_image.gif',
2164 'inserttable' => 'insert_table.gif',
2165 'insertsmile' => 'em.icon.smile.gif',
2166 'insertchar' => 'icon_ins_char.gif',
2167 'spellcheck' => 'spell-check.gif',
2168 'htmlmode' => 'ed_html.gif',
2169 'popupeditor' => 'fullscreen_maximize.gif',
2170 'search_replace' => 'ed_replace.gif');
2171 }
2172
2173 function get_setting() {
2174 global $CFG;
2175 return (isset($CFG->{$this->name}) ? explode(' ', $CFG->{$this->name}) : NULL);
2176 }
2177
2178 function write_setting($data) {
2179 $result = array();
2180 if (empty($data)) { $data = array(); }
2181 foreach ($data as $key => $value) {
2182 if (!in_array($key, array_keys($this->items))) {
2183 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2184 }
2185 if ($value == '1') {
2186 $result[] = $key;
2187 }
2188 }
2189 return (set_config($this->name, implode(' ',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2190 }
2191
2192 function output_html() {
eef868d1 2193
6e4dc10f 2194 global $CFG;
eef868d1 2195
6e4dc10f 2196 // checkboxes with input name="$this->name[$key]" value="1"
2197 // we do 15 fields per column
eef868d1 2198
cc73de71 2199 if ($this->get_setting() === NULL) {
2200 $currentsetting = $this->defaultsetting;
2201 } else {
2202 $currentsetting = $this->get_setting();
6e4dc10f 2203 }
eef868d1 2204
1beed35f 2205 $return = '<div class="form-group">';
2206 $return .= '<table><tr><td valign="top" align="right">';
eef868d1 2207
6e4dc10f 2208 $count = 0;
eef868d1 2209
6e4dc10f 2210 foreach($this->items as $key => $value) {
8ddbd7a6 2211 if ($count % 15 == 0 and $count != 0) {
2212 $return .= '</td><td valign="top" align="right">';
6e4dc10f 2213 }
eef868d1 2214
6e4dc10f 2215 $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;';
afee9864 2216 $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;';
6e4dc10f 2217 $count++;
2218 if ($count % 15 != 0) {
2219 $return .= '<br /><br />';
2220 }
2221 }
eef868d1 2222
2223 $return .= '</td></tr>';
6e4dc10f 2224 $return .= '</table>';
1beed35f 2225 $return .= '</div>';
6e4dc10f 2226
60a9a6ea 2227 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
6e4dc10f 2228 }
2229
2230}
2231
4642650f 2232class admin_setting_langlist extends admin_setting_configtext {
2233 function admin_setting_langlist() {
2234 parent::admin_setting_configtext('langlist', get_string('langlist', 'admin'), get_string('configlanglist', 'admin'), '', PARAM_NOTAGS);
2235 }
2236
2237 function write_setting($data) {
2238 $return = parent::write_setting($data);
2239 get_list_of_languages(true);//refresh the list
2240 return $return;
2241 }
2242}
2243
6e4dc10f 2244class admin_setting_backupselect extends admin_setting_configselect {
2245
2246 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
2247 parent::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
2248 }
2249
2250 function get_setting() {
2251 $backup_config = backup_get_config();
2252 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2253 }
eef868d1 2254
6e4dc10f 2255 function write_setting($data) {
2256 // check that what we got was in the original choices
2257 if (! in_array($data, array_keys($this->choices))) {
2258 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2259 }
eef868d1 2260
6e4dc10f 2261 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2262 }
2263
2264}
2265
2266class admin_setting_special_backupsaveto extends admin_setting_configtext {
2267
2268 function admin_setting_special_backupsaveto() {
2269 $name = 'backup_sche_destination';
2270 $visiblename = get_string('saveto');
2271 $description = get_string('backupsavetohelp');
348d6827 2272 parent::admin_setting_configtext($name, $visiblename, $description, '');
6e4dc10f 2273 }
eef868d1 2274
6e4dc10f 2275 function get_setting() {
2276 $backup_config = backup_get_config();
2277 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2278 }
eef868d1 2279
6e4dc10f 2280 function write_setting($data) {
348d6827 2281 $data = trim($data);
2282 if (!empty($data) and !is_dir($data)) {
6e4dc10f 2283 return get_string('pathnotexists') . '<br />';
2284 }
2285 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2286 }
2287
2288}
2289
2290class admin_setting_backupcheckbox extends admin_setting_configcheckbox {
2291
2292 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2293 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2294 }
2295
2296 function write_setting($data) {
2297 if ($data == '1') {
2298 return (backup_set_config($this->name, 1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2299 } else {
2300 return (backup_set_config($this->name, 0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2301 }
2302 }
eef868d1 2303
6e4dc10f 2304 function get_setting() {
2305 $backup_config = backup_get_config();
2306 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2307 }
2308
2309}
2310
2311class admin_setting_special_backuptime extends admin_setting_configtime {
2312
2313 function admin_setting_special_backuptime() {
2314 $name = 'backup_sche_hour';
2315 $name2 = 'backup_sche_minute';
2316 $visiblename = get_string('executeat');
2317 $description = get_string('backupexecuteathelp');
2318 $default = array('h' => 0, 'm' => 0);
2319 parent::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2320 }
eef868d1 2321
6e4dc10f 2322 function get_setting() {
2323 $backup_config = backup_get_config();
9fd9df20 2324 return (isset($backup_config->{$this->name}) && isset($backup_config->{$this->name}) ? array('h'=>$backup_config->{$this->name}, 'm'=>$backup_config->{$this->name2}) : NULL);
6e4dc10f 2325 }
eef868d1 2326
6e4dc10f 2327 function write_setting($data) {
2328 // check that what we got was in the original choices
2329 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
2330 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2331 }
eef868d1 2332
2333 return (backup_set_config($this->name, $data['h']) && backup_set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2334 }
eef868d1 2335
6e4dc10f 2336}
2337
2338class admin_setting_special_backupdays extends admin_setting {
2339
2340 function admin_setting_special_backupdays() {
2341 $name = 'backup_sche_weekdays';
2342 $visiblename = get_string('schedule');
2343 $description = get_string('backupschedulehelp');
6727c175 2344 $default = array('u' => 0, 'm' => 0, 't' => 0, 'w' => 0, 'r' => 0, 'f' => 0, 's' => 0);
2345 parent::admin_setting($name, $visiblename, $description, $default);
6e4dc10f 2346 }
eef868d1 2347
6e4dc10f 2348 function get_setting() {
2349 $backup_config = backup_get_config();
cc73de71 2350 if (isset($backup_config->{$this->name})) {
2351 $currentsetting = $backup_config->{$this->name};
2352 return array('u' => substr($currentsetting, 0, 1),
2353 'm' => substr($currentsetting, 1, 1),
2354 't' => substr($currentsetting, 2, 1),
2355 'w' => substr($currentsetting, 3, 1),
2356 'r' => substr($currentsetting, 4, 1),
2357 'f' => substr($currentsetting, 5, 1),
2358 's' => substr($currentsetting, 6, 1));
2359 } else {
2360 return NULL;
2361 }
6e4dc10f 2362 }
eef868d1 2363
6e4dc10f 2364 function output_html() {
eef868d1 2365
cc73de71 2366 if ($this->get_setting() === NULL) {
2367 $currentsetting = $this->defaultsetting;
2368 } else {
2369 $currentsetting = $this->get_setting();
6e4dc10f 2370 }
eef868d1 2371
cc73de71 2372 // rewrite for simplicity
eef868d1 2373 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
cc73de71 2374 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
eef868d1 2375
60a9a6ea 2376 $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;' .
2377 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;' .
2378 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;' .
2379 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>' .
2380 '<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>' .
2381 '<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>' .
2382 '<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>' .
2383 '<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>' .
2384 '<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>' .
2385 '<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>' .
2386 '<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>' .
6153cf58 2387 '</tr></table>';
2388
60a9a6ea 2389 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
eef868d1 2390
6e4dc10f 2391 }
eef868d1 2392
6e4dc10f 2393 // 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
2394 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2395 // they MUST go into an array named $this->name, or else we won't receive them here
2396 function write_setting($data) {
2397 $week = 'umtwrfs';
2398 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
9fd9df20 2399 if (!empty($data)) {
2400 foreach($data as $key => $value) {
2401 if ($value == '1') {
2402 $result[strpos($week, $key)] = 1;
2403 }
2404 }
6e4dc10f 2405 }
2406 return (backup_set_config($this->name, implode('',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2407 }
2408}
2409
ee437bbc 2410class admin_setting_special_debug extends admin_setting_configselect {
6e4dc10f 2411
2412 function admin_setting_special_debug() {
2413 $name = 'debug';
2414 $visiblename = get_string('debug', 'admin');
2415 $description = get_string('configdebug', 'admin');
7eb0b60a 2416 $choices = array( DEBUG_NONE => get_string('debugnone', 'admin'),
2417 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
2418 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
38d2d43b 2419 DEBUG_ALL => get_string('debugall', 'admin'),
2420 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')
ee437bbc 2421 );
2422 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
6e4dc10f 2423 }
2424
ee437bbc 2425 function get_setting() {
2426 global $CFG;
cc73de71 2427 if (isset($CFG->debug)) {
cc73de71 2428 return $CFG->debug;
2429 } else {
2430 return NULL;
ee437bbc 2431 }
6e4dc10f 2432 }
2433
ee437bbc 2434 function write_setting($data) {
2435 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2436 }
2437
2438}
2439
2440
2441class admin_setting_special_calendar_weekend extends admin_setting {
2442
2443 function admin_setting_special_calendar_weekend() {
2444 $name = 'calendar_weekend';
2445 $visiblename = get_string('calendar_weekend', 'admin');
2446 $description = get_string('helpweekenddays', 'admin');
5eaa6aa0 2447 $default = array ('0', '6'); // Saturdays and Sundays
b91b1f92 2448 parent::admin_setting($name, $visiblename, $description, $default);
6e4dc10f 2449 }
2450
2451 function get_setting() {
2452 global $CFG;
4af8d5d3 2453 return isset($CFG->{$this->name}) ? $CFG->{$this->name} : 0;
6e4dc10f 2454 }
eef868d1 2455
6e4dc10f 2456 function write_setting($data) {
4af8d5d3 2457 $result = 0;
b91b1f92 2458 if (!empty($data)) {
4af8d5d3 2459 foreach($data as $index) {
2460 $result |= 1 << $index;
b91b1f92 2461 }
6e4dc10f 2462 }
4af8d5d3 2463 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2464 }
eef868d1 2465
6e4dc10f 2466 function output_html() {
cc73de71 2467 if ($this->get_setting() === NULL) {
2468 $currentsetting = $this->defaultsetting;
2469 } else {
2470 $currentsetting = $this->get_setting();
6e4dc10f 2471 }
cc73de71 2472
4af8d5d3 2473 // The order matters very much because of the implied numeric keys
2474 $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
2475 $return = '<table><thead><tr>';
2476 foreach($days as $index => $day) {
2477 $return .= '<td><label for="id_s_'.$this->name.$index.'">'.get_string($day, 'calendar').'</label></td>';
2478 }
2479 $return .= '</tr></thead><tbody><tr>';
2480 foreach($days as $index => $day) {
2481 $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>';
2482 }
2483 $return .= '</tr></tbody></table>';
6153cf58 2484
038bf621 2485 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
eef868d1 2486
6e4dc10f 2487 }
2488
2489}
2490
3d5c00b3 2491/**
2492 * this is used in config->grades
5a412dbf 2493 */
0909d44e 2494class admin_setting_special_gradebookroles extends admin_setting {
5a412dbf 2495
0909d44e 2496 function admin_setting_special_gradebookroles() {
2497 $name = 'gradebookroles';
73afaf5b 2498 $visiblename = get_string('gradebookroles', 'admin');
2499 $description = get_string('configgradebookroles', 'admin');
7aa3e196 2500 $default = array(5=>'1'); // The student role in a default install
8a2cb2fa 2501 parent::admin_setting($name, $visiblename, $description, $default);
5a412dbf 2502 }
2503
2504 function get_setting() {
2505 global $CFG;
8a2cb2fa 2506 if (!empty($CFG->{$this->name})) {
60f7d402 2507 $result = explode(',', $CFG->{$this->name});
2508 foreach ($result as $roleid) {
9ba38673 2509 $array[$roleid] = 1;
60f7d402 2510 }
2511 return $array;
5a412dbf 2512 } else {
3fae9f57 2513 return null;
5a412dbf 2514 }
2515 }
2516
2517 function write_setting($data) {
2518 if (!empty($data)) {
2519 $str = '';
0909d44e 2520 foreach ($data as $key => $value) {
5a412dbf 2521 if ($value) {
2522 $str .= $key.',';
2523 }
2524 }
2525 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2526 } else {
73afaf5b 2527 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2528 }
5a412dbf 2529 }
2530
2531 function output_html() {
2532
2533 if ($this->get_setting() === NULL) {
2534 $currentsetting = $this->defaultsetting;
2535 } else {
2536 $currentsetting = $this->get_setting();
2537 }
5a412dbf 2538 // from to process which roles to display
2539 if ($roles = get_records('role')) {
73afaf5b 2540 $return = '<div class="form-group">';
2541 $first = true;
2542 foreach ($roles as $roleid=>$role) {
60f7d402 2543 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
fb132873 2544 $checked = ' checked="checked"';
5a412dbf 2545 } else {
73afaf5b 2546 $checked = '';
5a412dbf 2547 }
73afaf5b 2548 if ($first) {
2549 $first = false;
2550 } else {
2551 $return .= '<br />';
2552 }
b0abd282 2553 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1"'.$checked.' />&nbsp;'.format_string($role->name);
5a412dbf 2554 }
73afaf5b 2555 $return .= '</div>';
5a412dbf 2556 }
73afaf5b 2557
60a9a6ea 2558 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
5a412dbf 2559
2560 }
2561
2562}
6e4dc10f 2563
d42c64ba 2564/*
2565 * this is used in config->appearance->coursemanager
2566 * (which roles to show on course decription page)
2567 */
2568class admin_setting_special_coursemanager extends admin_setting {
2569
2570 function admin_setting_special_coursemanager() {
2571 $name = 'coursemanager';
2572 $visiblename = get_string('coursemanager', 'admin');
2573 $description = get_string('configcoursemanager', 'admin');
2574 $default = array(3=>'1'); // The teahcer role in a default install
2575 parent::admin_setting($name, $visiblename, $description, $default);
2576 }
2577
2578 function get_setting() {
2579
2580 global $CFG;
2581 if (!empty($CFG->{$this->name})) {
2582 $result = explode(',', $CFG->{$this->name});
2583 foreach ($result as $roleid) {
9ba38673 2584 $array[$roleid] = 1;
d42c64ba 2585 }
2586 return $array;
b107549c 2587 } else if (isset($CFG->{$this->name})) {
2588 return array();
d42c64ba 2589 } else {
2590 return null;
2591 }
2592 }
2593
2594 function write_setting($data) {
2595
2596 if (!empty($data)) {
2597 $str = '';
2598 foreach ($data as $key => $value) {
2599 if ($value) {
2600 $str .= $key.',';
2601 }
2602 }
2603 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2604 } else {
2605 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2606 }
2607 }
2608
2609 function output_html() {
2610
2611 if ($this->get_setting() === NULL) {
2612 $currentsetting = $this->defaultsetting;
2613 } else {
2614 $currentsetting = $this->get_setting();
2615 }
2616 // from to process which roles to display
d717df34 2617 if ($roles = get_records_select('role', '', 'sortorder')) {
d42c64ba 2618 $return = '<div class="form-group">';
2619 $first = true;
2620 foreach ($roles as $roleid=>$role) {
2621 if (is_array($currentsetting) && in_array($roleid, array_keys($currentsetting))) {
2622 $checked = 'checked="checked"';
2623 } else {
2624 $checked = '';
2625 }
2626 if ($first) {
2627 $first = false;
2628 } else {
2629 $return .= '<br />';
2630 }
7203167d 2631 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$roleid.']" value="1" '.$checked.' />&nbsp;'.$role->name;
d42c64ba 2632 }
2633 $return .= '</div>';
2634 }
60a9a6ea 2635 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
d42c64ba 2636 }
2637}
2638
76127f37 2639/*
2640 * this is used in config->courses->gradeexports
2641 * (which roles to show on course decription page)
2642 */
2643class admin_setting_special_gradeexport extends admin_setting {
2644
2645 function admin_setting_special_gradeexport() {
2646 $name = 'gradeexport';
2647 $visiblename = get_string('gradeexport', 'admin');
2648 $description = get_string('configgradeexport', 'admin');
2649 $default = array(3=>'1'); // The teahcer role in a default install
2650 parent::admin_setting($name, $visiblename, $description, $default);
2651 }
2652
2653 function get_setting() {
2654
2655 global $CFG;
2656 if (!empty($CFG->{$this->name})) {
2657 $result = explode(',', $CFG->{$this->name});
2658 foreach ($result as $plugin) {
9ba38673 2659 $array[$plugin] = 1;
76127f37 2660 }
2661 return $array;
2662 } else if (isset($CFG->{$this->name})) {
2663 return array();
2664 } else {
2665 return null;
2666 }
2667 }
2668
2669 function write_setting($data) {
2670
2671 if (!empty($data)) {
2672 $str = '';
2673 foreach ($data as $key => $value) {
2674 if ($value) {
2675 $str .= $key.',';
2676 }
2677 }
2678 return set_config($this->name, rtrim($str, ","))?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2679 } else {
2680 return set_config($this->name, '')?'':get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2681 }
2682 }
2683
2684 function output_html() {
2685
2686 if ($this->get_setting() === NULL) {
2687 $currentsetting = $this->defaultsetting;
2688 } else {
2689 $currentsetting = $this->get_setting();
2690 }
2691 // from to process which roles to display
2692 if ($exports = get_list_of_plugins('grade/export')) {
2693 $return = '<div class="form-group">';
2694 $first = true;
2695 foreach ($exports as $export) {
2696 if (is_array($currentsetting) && in_array($export, array_keys($currentsetting))) {
2697 $checked = 'checked="checked"';
2698 } else {
2699 $checked = '';
2700 }
2701 if ($first) {
2702 $first = false;
2703 } else {
2704 $return .= '<br />';
2705 }
2706 $return .= '<input type="checkbox" name="s_'.$this->name.'['.$export.']" value="1" '.$checked.' />&nbsp;'.$export;
2707 }
2708 $return .= '</div>';
2709 }
2710 return format_admin_setting($this->name, $this->visiblename, $return, $this->description, false);
2711 }
2712}
2713
2714
2715
2716
6e4dc10f 2717class admin_setting_special_perfdebug extends admin_setting_configcheckbox {
2718
2719 function admin_setting_special_perfdebug() {
2720 $name = 'perfdebug';
2721 $visiblename = get_string('perfdebug', 'admin');
2722 $description = get_string('configperfdebug', 'admin');
2723 parent::admin_setting_configcheckbox($name, $visiblename, $description, '');
2724 }
2725
2726 function write_setting($data) {
d42c64ba 2727
6e4dc10f 2728 if ($data == '1') {
2729 return (set_config($this->name,15) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2730 } else {
2731 return (set_config($this->name,7) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2732 }
2733 }
2734
2735 function output_html() {
d42c64ba 2736
cc73de71 2737 if ($this->get_setting() === NULL) {
2738 $currentsetting = $this->defaultsetting;
2739 } else {
2740 $currentsetting = $this->get_setting();
2741 }
eef868d1 2742
3c6f7a78 2743 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 15 ? 'checked="checked"' : '') . ' />';
fc52aa6e 2744 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 2745 }
2746
2747}
2748
47b8b9eb 2749class admin_setting_special_debugdisplay extends admin_setting_configcheckbox {
2750
2751 function admin_setting_special_debugdisplay() {
2752 $name = 'debugdisplay';
2753 $visiblename = get_string('debugdisplay', 'admin');
2754 $description = get_string('configdebugdisplay', 'admin');
6349a3ba 2755 $default = ini_get('display_errors');
2756 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
47b8b9eb 2757 }
2758
2759 function write_setting($data) {
2760
2761 if ($data == '1') {
2762 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2763 } else {
2764 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2765 }
2766 }
2767
2768 function output_html() {
2769
2770 if ($this->get_setting() === NULL) {
6349a3ba 2771 $currentsetting = $this->defaultsetting;
47b8b9eb 2772 } else {
2773 $currentsetting = $this->get_setting();
2774 }
2775
2776 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 1 ? 'checked="checked"' : '') . ' />';
2777 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
2778 }
2779
2780}
2781
89a5f827 2782
190af29f 2783class admin_setting_gradecat_combo extends admin_setting {
2784
2785 var $choices;
2786
2787 function admin_setting_gradecat_combo($name, $visiblename, $description, $defaultsetting, $choices) {
2788 $this->choices = $choices;
2789 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
2790 }
2791
2792 function get_setting() {
2793 global $CFG;
2794
2795 if (!isset($CFG->{$this->name}) or !isset($CFG->{$this->name.'_flag'})) {
2796 return NULL;
2797 }
2798
2799 $flag = (int)$CFG->{$this->name.'_flag'};
2800 $forced = (boolean)(1 & $flag); // first bit
2801 $adv = (boolean)(2 & $flag); // second bit
2802
2803 return array('value' => $CFG->{$this->name}, 'forced' => $forced, 'adv' => $adv);
89a5f827 2804 }
2805
2806 function write_setting($data) {
190af29f 2807 global $CFG;
89a5f827 2808
190af29f 2809 $value = $data['value'];
2810 $forced = empty($data['forced']) ? 0 : 1;
2811 $adv = empty($data['adv']) ? 0 : 2;
2812 $flag = ($forced | $adv); //bitwise or
2813
2814 if (!in_array($value, array_keys($this->choices))) {
2815 return 'Error setting ' . $this->visiblename . '<br />';
2816 }
89a5f827 2817
190af29f 2818 $oldvalue = get_config(NULL, $this->name);
2819 $oldflag = (int)get_config(NULL, $this->name.'_flag');
2820 $oldforced = (1 & $oldflag); // first bit
89a5f827 2821
190af29f 2822 $result = (boolean)set_config($this->name, $value);
2823 $result = $result && (boolean)set_config($this->name.'_flag', $flag);
2824
2825 if ($result) {
2826 // force regrade if needed
2827 if ($oldforced != $forced or ($forced and $value != $oldvalue)) {
2828 require_once($CFG->libdir.'/gradelib.php');
2829 grade_category::updated_forced_settings();
2830 }
2831
2832 return '';
2833 } else {
2834 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2835 }
2836 }
2837
2838 function output_html() {
2839
2840 if ($this->get_setting() === NULL) {
2841 $current = $this->defaultsetting;
2842 } else {
2843 $current = $this->get_setting();
2844 }
2845 $value = $current['value'];
2846 $forced = !empty($current['forced']);
2847 $adv = !empty($current['adv']);
2848
2849 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'[value]">';
2850 foreach ($this->choices as $key => $val) {
2851 // the string cast is needed because key may be integer - 0 is equal to most strings!
2852 $return .= '<option value="'.$key.'"'.((string)$key==$value ? ' selected="selected"' : '').'>'.$val.'</option>';
2853 }
3d5c00b3 2854 $return .= '</select>';
190af29f 2855 $return .= '<label for="id_s_'.$this->name.'force">'.get_string('force').'</label><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'force" name="s_'.$this->name.'[forced]" value="1" ' . ($forced ? 'checked="checked"' : '') . ' />';
2856 $return .= '<label for="id_s_'.$this->name.'adv">'.get_string('advanced').'</label><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'adv" name="s_'.$this->name.'[adv]" value="1" ' . ($adv ? 'checked="checked"' : '') . ' />';
2857
2858 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
89a5f827 2859 }
2860}
2861
3d5c00b3 2862
0a784551 2863/**
3d5c00b3 2864 * Selection of grade report in user profile
0a784551 2865 */
2866class admin_setting_grade_profilereport extends admin_setting_configselect {
2867 function admin_setting_grade_profilereport() {
2868 parent::admin_setting_configselect('grade_profilereport', get_string('profilereport', 'grades'), get_string('configprofilereport', 'grades'), 'user', null);
2869 }
2870
2871 function load_choices() {
2872 if (!empty($this->choices)) {
2873 return;
2874 }
2875 $this->choices = array();
2876
2877 global $CFG;
2878 require_once($CFG->libdir.'/gradelib.php');
2879
2880 foreach (get_list_of_plugins('grade/report') as $plugin) {
2881 if (file_exists($CFG->dirroot.'/grade/report/'.$plugin.'/lib.php')) {
2882 require_once($CFG->dirroot.'/grade/report/'.$plugin.'/lib.php');
2883 $functionname = 'grade_report_'.$plugin.'_profilereport';
2884 if (function_exists($functionname)) {
2885 $this->choices[$plugin] = get_string('modulename', 'gradereport_'.$plugin, NULL, $CFG->dirroot.'/grade/report/'.$plugin.'/lang/');
2886 }
2887 }
2888 }
2889 }
2890}
2891
47b8b9eb 2892
6e4dc10f 2893// Code for a function that helps externalpages print proper headers and footers
2894// N.B.: THIS FUNCTION HANDLES AUTHENTICATION
1ae083e4 2895function admin_externalpage_setup($section) {
6e4dc10f 2896
2897 global $CFG, $PAGE, $USER;
eef868d1 2898
1ae083e4 2899 $adminroot = admin_get_root();
2900
6e4dc10f 2901 require_once($CFG->libdir . '/blocklib.php');
02cc05a7 2902 require_once($CFG->dirroot . '/'.$CFG->admin.'/pagelib.php');
eef868d1 2903
02cc05a7 2904 page_map_class(PAGE_ADMIN, 'page_admin');
6e4dc10f 2905
e9a20759 2906 $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number
6e4dc10f 2907
02cc05a7 2908 $PAGE->init_extra($section); // hack alert!
6e4dc10f 2909
2910 $root = $adminroot->locate($PAGE->section);
2911
2912 if ($site = get_site()) {
2913 require_login();
2914 } else {
6859360c 2915 redirect($CFG->wwwroot . '/'.$CFG->admin.'/index.php');
6e4dc10f 2916 die;
2917 }
2918
2919 if (!is_a($root, 'admin_externalpage')) {
2920 error(get_string('sectionerror','admin'));
2921 die;
2922 }
2923
2924 // this eliminates our need to authenticate on the actual pages
2925 if (!($root->check_access())) {
2926 error(get_string('accessdenied', 'admin'));
2927 die;
2928 }
eef868d1 2929
6e4dc10f 2930 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
eef868d1 2931
6e4dc10f 2932 if (!isset($USER->adminediting)) {
2933 $USER->adminediting = false;
2934 }
eef868d1 2935
6e4dc10f 2936 if ($PAGE->user_allowed_editing()) {
2937 if ($adminediting == 1) {
2938 $USER->adminediting = true;
2939 } elseif ($adminediting == 0) {
2940 $USER->adminediting = false;
2941 }
2942 }
eef868d1 2943
6e4dc10f 2944}
2945
f1af7aaa 2946/**
2947 * TODO document
2948 */
1ae083e4 2949function admin_externalpage_print_header() {
6e4dc10f 2950
ce821fda 2951 global $CFG, $PAGE, $SITE, $THEME;
9ba38673 2952
1ae083e4 2953 define('ADMIN_EXT_HEADER_PRINTED', 'true');
2954
b1ce7811 2955 if (!empty($SITE->fullname)) {
2956 $pageblocks = blocks_setup($PAGE);
6e4dc10f 2957
e524c0ed 2958 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH,
2959 blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
b1ce7811 2960 BLOCK_L_MAX_WIDTH);
9f7f1a74 2961 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH,
2962 blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
2963 BLOCK_R_MAX_WIDTH);
2964
b1ce7811 2965 $PAGE->print_header();
5577ceb3 2966 echo '<table id="layout-table" summary=""><tr>';
f1af7aaa 2967
5730bc61 2968 $lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
2969 foreach ($lt as $column) {
2970 $lt1[] = $column;
2971 if ($column == 'middle') break;
2972 }
2973 foreach ($lt1 as $column) {
2974 switch ($column) {
2975 case 'left':
b1ce7811 2976 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
9f7f1a74 2977 print_container_start();
b1ce7811 2978 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
9f7f1a74 2979 print_container_end();
b1ce7811 2980 echo '</td>';
5730bc61 2981 break;
f1af7aaa 2982
5730bc61 2983 case 'middle':
a06c8c2c 2984 echo '<td id="middle-column">';
9f7f1a74 2985 print_container_start(true);
5730bc61 2986 break;
f1af7aaa 2987
5730bc61 2988 case 'right':
2989 if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT)) {
2990 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
9f7f1a74 2991 print_container_start();
5730bc61 2992 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
9f7f1a74 2993 print_container_end();
5730bc61 2994 echo '</td>';
2995 }
2996 break;
2997 }
2998 }
b1ce7811 2999 } else {
3000 print_header();
3001 }
6e4dc10f 3002}
3003
1ae083e4 3004function admin_externalpage_print_footer() {
6e4dc10f 3005
ce821fda 3006 global $CFG, $PAGE, $SITE, $THEME;
9ba38673 3007
1ae083e4 3008 define('ADMIN_EXT_FOOTER_PRINTED', 'true');
3009
b1ce7811 3010 if (!empty($SITE->fullname)) {
3011 $pageblocks = blocks_setup($PAGE);
9f7f1a74 3012 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH,
3013 blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
3014 BLOCK_L_MAX_WIDTH);
e524c0ed 3015 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH,
3016 blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
b1ce7811 3017 BLOCK_R_MAX_WIDTH);
5730bc61 3018
3019 $lt = (empty($THEME->layouttable)) ? array('left', 'middle', 'right') : $THEME->layouttable;
3020 foreach ($lt as $column) {
3021 if ($column != 'middle') {
3022 array_shift($lt);
3023 } else if ($column == 'middle') {
3024 break;
3025 }
3026 }
3027 foreach ($lt as $column) {
3028 switch ($column) {
3029 case 'left':
3030 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
9f7f1a74 3031 print_container_start();
5730bc61 3032 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
9f7f1a74 3033 print_container_end();
b1ce7811 3034 echo '</td>';
5730bc61 3035 break;
f1af7aaa 3036
5730bc61 3037 case 'middle':
9f7f1a74 3038 print_container_end();
5730bc61 3039 echo '</td>';
3040 break;
f1af7aaa 3041
5730bc61 3042 case 'right':
d5271d3d 3043 if (blocks_have_content($pageblocks, BLOCK_POS_RIGHT)) {
ec3938f3 3044 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
9f7f1a74 3045 print_container_start();
ec3938f3 3046 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
5730bc61 3047
9f7f1a74 3048 print_container_end();
ec3938f3 3049 echo '</td>';
5730bc61 3050 }
3051 break;
3052 }
ce821fda 3053 }
ec3938f3 3054 echo '</tr></table>';
b1ce7811 3055 }
6e4dc10f 3056 print_footer();
6e4dc10f 3057}
3058
3059function admin_get_root() {
3060 global $CFG;
3061
3062 static $ADMIN;
3063
3064 if (!isset($ADMIN)) {
3065 // start the admin tree!
2058a0a1 3066 $ADMIN = new admin_category('root', get_string("administration"));
6e4dc10f 3067 // we process this file first to get categories up and running
6859360c 3068 include($CFG->dirroot . '/'.$CFG->admin.'/settings/top.php');
6e4dc10f 3069
3070 // now we process all other files in admin/settings to build the
3071 // admin tree
6859360c 3072 foreach (glob($CFG->dirroot . '/'.$CFG->admin.'/settings/*.php') as $file) {
3073 if ($file != $CFG->dirroot . '/'.$CFG->admin.'/settings/top.php') {
6e4dc10f 3074 include_once($file);
3075 }
3076 }
3077 }
eef868d1 3078
6e4dc10f 3079 return $ADMIN;
3080}
3081
8e5da17a 3082/// settings utility functions
6e4dc10f 3083
8e5da17a 3084/**
3085 * This function applies default settings.
3086 * @param object $node
3087 * @param bool $uncoditional if true overrides all values with defaults
3088 * @return void
3089 */
3090function apply_default_settings(&$node, $unconditional=true) {
6e4dc10f 3091
3092 global $CFG;
3093
3094 if (is_a($node, 'admin_category')) {
3095 $entries = array_keys($node->children);
3096 foreach ($entries as $entry) {
3097 apply_default_settings($node->children[$entry]);
3098 }
3099 return;
eef868d1 3100 }
6e4dc10f 3101
eef868d1 3102 if (is_a($node, 'admin_settingpage')) {
6e4dc10f 3103 foreach ($node->settings as $setting) {
8e5da17a 3104 if (!$unconditional and !is_null($setting->get_setting)) {
3105 //do not override existing defaults
3106 continue;
3107 }
3108 $defaultsetting = $setting->get_defaultsetting();
3109 if (is_null($defaultsetting)) {
3110 // no value yet - default maybe applied after admin user creation or in upgradesettings
3111 continue;
3112 }
3113 $CFG->{$setting->name} = $defaultsetting;
3114 $setting->write_setting($defaultsetting);
6e4dc10f 3115 unset($setting); // needed to prevent odd (imho) reference behaviour
3116 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
3117 }
3118 return;
3119 }
3120
3121 return;
3122
3123}
3124
3125// n.b. this function unconditionally applies default settings
3126function apply_default_exception_settings($defaults) {
3127
3128 global $CFG;
3129
3130 foreach($defaults as $key => $value) {
3131 $CFG->$key = $value;
3132 set_config($key, $value);
3133 }
3134
3135}
3136
fbe31d22 3137function format_admin_setting($name, $title='', $form='', $description='', $label=true) {
9ba38673 3138
fbe31d22 3139 // sometimes the id is not id_s_name, but id_s_name_m or something, and this does not validate
3140 if ($label) {
9ba38673 3141 $labelfor = 'for = "id_s_'.$name.'"';
fbe31d22 3142 } else {
9ba38673 3143 $labelfor = '';
fbe31d22 3144 }
9ba38673 3145
fbe31d22 3146 $str = "\n".
fc52aa6e 3147 '<div class="form-item" id="admin-'.$name.'">'."\n".
fbe31d22 3148 '<label '.$labelfor.'>'.$title."\n".
6153cf58 3149 ' <span class="form-shortname">'.$name.'</span>'."\n".
3150 '</label>'."\n".
3151 $form."\n".
edb6cba4 3152 '<div class="description">'.$description.'</div>'."\n".
23586aca 3153 '</div>'.
3154 "\n\n";
9ba38673 3155
fbe31d22 3156 return $str;
2c8766df 3157}
3158
9ba38673 3159/*
d210e12e 3160 * Try to upgrade the given language pack (or current language)
3161 * If it doesn't work, fail silently and return false
3162 */
3163function upgrade_language_pack($lang='') {
3164 global $CFG;
3165
3166 if (empty($lang)) {
3167 $lang = current_language();
3168 }
3169
3170 if ($lang == 'en_utf8') {
3171 return true; // Nothing to do
3172 }
3173
3174 notify(get_string('langimport', 'admin').': '.$lang.' ... ', 'notifysuccess');
3175
3176 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
3177 @mkdir ($CFG->dataroot.'/lang/');
3178
3179 require_once($CFG->libdir.'/componentlib.class.php');
3180
3181 if ($cd = new component_installer('http://download.moodle.org', 'lang16', $lang.'.zip', 'languages.md5', 'lang')) {
3182 $status = $cd->install(); //returns ERROR | UPTODATE | INSTALLED
3183
3184 if ($status == INSTALLED) {
3185 debugging('Downloading successful: '.$lang);
3186 @unlink($CFG->dataroot.'/cache/languages');
3187 return true;
3188 }
3189 }
3190
3191 return false;
3192}
3193
2acc1f96 3194/**
3195 * Based on find_new_settings{@link ()} in upgradesettings.php
3196 * Looks to find any admin settings that have not been initialized. Returns 1 if it finds any.
3197 *
9ba38673 3198 * @param string &$node The node at which to start searching.
2acc1f96 3199 * @return int Returns 1 if any settings haven't been initialised, 0 if they all have
3200 */
3201function any_new_admin_settings(&$node) {
3202
3203 if (is_a($node, 'admin_category')) {
3204 $entries = array_keys($node->children);
3205 foreach ($entries as $entry) {
3206 if( any_new_admin_settings($node->children[$entry]) ){
3207 return 1;
3208 }
3209 }
3210 }
3211
3212 if (is_a($node, 'admin_settingpage')) {
3213 foreach ($node->settings as $setting) {
3214 if ($setting->get_setting() === NULL) {
3215 return 1;
3216 }
3217 }
3218 }
3219
3220
3221 return 0;
3222
3223}
3224
8f246961 3225
3226/**
3227 * Moved from admin/replace.php so that we can use this in cron
3228 * @param string $search - string to look for
3229 * @param string $replace - string to replace
3230 * @return bool - success or fail
3231 */
3232function db_replace($search, $replace) {
3233
3234 global $db, $CFG;
3235
3236 /// Turn off time limits, sometimes upgrades can be slow.
3237 @set_time_limit(0);
3238 @ob_implicit_flush(true);
3239 while(@ob_end_flush());
3240
3241 if (!$tables = $db->Metatables() ) { // No tables yet at all.
3242 return false;
3243 }
3244 foreach ($tables as $table) {
3245
3246 if (in_array($table, array($CFG->prefix.'config'))) { // Don't process these
3247 continue;
3248 }
3249
3250 if ($columns = $db->MetaColumns($table, false)) {
3251 foreach ($columns as $column => $data) {
3252 if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) { // Text stuff only
3253 $db->debug = true;
3254 execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');");
3255 $db->debug = false;
3256 }
3257 }
3258 }
3259 }
3260
3261 return true;
3262}
3263
6153cf58 3264?>