Splitted all the scorm/aicc library in type separeted files
[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
12/**
ead29342 13 * Upgrade plugins
88a7228a 14 *
15 * @uses $db
16 * @uses $CFG
ead29342 17 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
18 * @param string $dir The directory where the plugins are located (e.g. 'question/questiontypes')
19 * @param string $return The url to prompt the user to continue to
eef868d1 20 */
ead29342 21function upgrade_plugins($type, $dir, $return) {
e69ef14b 22 global $CFG, $db;
173cc1c3 23
ead29342 24 if (!$plugs = get_list_of_plugins($dir) ) {
25 error('No '.$type.' plugins installed!');
173cc1c3 26 }
27
583fad99 28 $updated_plugins = false;
29 $strpluginsetup = get_string('pluginsetup');
30
ead29342 31 foreach ($plugs as $plug) {
173cc1c3 32
ead29342 33 $fullplug = $CFG->dirroot .'/'.$dir.'/'. $plug;
173cc1c3 34
ead29342 35 unset($plugin);
173cc1c3 36
bbbf2d40 37 if (is_readable($fullplug .'/version.php')) {
ead29342 38 include_once($fullplug .'/version.php'); // defines $plugin with version etc
173cc1c3 39 } else {
40 continue; // Nothing to do.
41 }
42
e79a09a2 43 $oldupgrade = false;
44 $newupgrade = false;
7c006e34 45 if (is_readable($fullplug . '/db/'. $CFG->dbtype . '.php')) {
46 include_once($fullplug . '/db/'. $CFG->dbtype . '.php'); // defines old upgrading function
e79a09a2 47 $oldupgrade = true;
48 }
db8bd7a6 49 if (is_readable($fullplug . '/db/upgrade.php')) {
7c006e34 50 include_once($fullplug . '/db/upgrade.php'); // defines new upgrading function
e79a09a2 51 $newupgrade = true;
52 }
53
ead29342 54 if (!isset($plugin)) {
173cc1c3 55 continue;
56 }
57
ead29342 58 if (!empty($plugin->requires)) {
59 if ($plugin->requires > $CFG->version) {
60 $info->pluginname = $plug;
61 $info->pluginversion = $plugin->version;
173cc1c3 62 $info->currentmoodle = $CFG->version;
ead29342 63 $info->requiremoodle = $plugin->requires;
583fad99 64 if (!$updated_plugins) {
eef868d1 65 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
583fad99 66 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
67 false, '&nbsp;', '&nbsp;');
68 }
69 upgrade_log_start();
ead29342 70 notify(get_string('pluginrequirementsnotmet', 'error', $info));
583fad99 71 $updated_plugins = true;
173cc1c3 72 unset($info);
73 continue;
74 }
75 }
76
ead29342 77 $plugin->name = $plug; // The name MUST match the directory
173cc1c3 78
ead29342 79 $pluginversion = $type.'_'.$plug.'_version';
173cc1c3 80
ead29342 81 if (!isset($CFG->$pluginversion)) {
82 set_config($pluginversion, 0);
173cc1c3 83 }
eef868d1 84
ead29342 85 if ($CFG->$pluginversion == $plugin->version) {
173cc1c3 86 // do nothing
ead29342 87 } else if ($CFG->$pluginversion < $plugin->version) {
583fad99 88 if (!$updated_plugins) {
eef868d1 89 print_header($strpluginsetup, $strpluginsetup, $strpluginsetup, '',
a36f058e 90 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
91 false, '&nbsp;', '&nbsp;');
173cc1c3 92 }
e79a09a2 93 $updated_plugins = true;
583fad99 94 upgrade_log_start();
ead29342 95 print_heading($plugin->name .' plugin needs upgrading');
e79a09a2 96 $db->debug = true;
97 @set_time_limit(0); // To allow slow databases to complete the long SQL
98
d87a9d73 99 if ($CFG->$pluginversion == 0) { // It's a new install of this plugin
e79a09a2 100 /// Both old .sql files and new install.xml are supported
101 /// but we priorize install.xml (XMLDB) if present
102 $status = false;
db8bd7a6 103 if (file_exists($fullplug . '/db/install.xml')) {
450cf307 104 $status = install_from_xmldb_file($fullplug . '/db/install.xml'); //New method
e79a09a2 105 } else if (file_exists($fullplug .'/db/'. $CFG->dbtype .'.sql')) {
106 $status = modify_database($fullplug .'/db/'. $CFG->dbtype .'.sql'); //Old method
eef868d1 107 } else {
7c006e34 108 $status = true;
d87a9d73 109 }
e79a09a2 110
111 $db->debug = false;
eef868d1 112 /// Continue with the instalation, roles and other stuff
e79a09a2 113 if ($status) {
114 // OK so far, now update the plugins record
115 set_config($pluginversion, $plugin->version);
116 if (!update_capabilities($dir.'/'.$plug)) {
117 error('Could not set up the capabilities for '.$module->name.'!');
118 }
119 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
120 } else {
121 notify('Installing '. $plugin->name .' FAILED!');
122 }
d87a9d73 123 } else { // Upgrade existing install
e79a09a2 124 /// Run de old and new upgrade functions for the module
125 $oldupgrade_function = $type.'_'.$plugin->name .'_upgrade';
126 $newupgrade_function = 'xmldb_' . $type.'_'.$plugin->name .'_upgrade';
127
128 /// First, the old function if exists
129 $oldupgrade_status = true;
130 if ($oldupgrade && function_exists($oldupgrade_function)) {
131 $db->debug = true;
132 $oldupgrade_status = $oldupgrade_function($CFG->$pluginversion);
133 } else if ($oldupgrade) {
134 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
135 $fullplug . '/db/' . $CFG->dbtype . '.php');
136 }
137
138 /// Then, the new function if exists and the old one was ok
139 $newupgrade_status = true;
140 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
141 $db->debug = true;
142 $newupgrade_status = $newupgrade_function($CFG->$pluginversion);
143 } else if ($newupgrade) {
144 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
145 $fullplug . '/db/upgrade.php');
146 }
147
148 $db->debug=false;
149 /// Now analyze upgrade results
150 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
151 // OK so far, now update the plugins record
152 set_config($pluginversion, $plugin->version);
153 if (!update_capabilities($dir.'/'.$plug)) {
154 error('Could not update '.$plugin->name.' capabilities!');
d87a9d73 155 }
e79a09a2 156 notify(get_string('modulesuccess', '', $plugin->name), 'notifysuccess');
157 } else {
158 notify('Upgrading '. $plugin->name .' from '. $CFG->$pluginversion .' to '. $plugin->version .' FAILED!');
173cc1c3 159 }
160 }
d87a9d73 161 echo '<hr />';
173cc1c3 162 } else {
583fad99 163 upgrade_log_start();
ead29342 164 error('Version mismatch: '. $plugin->name .' can\'t downgrade '. $CFG->$pluginversion .' -> '. $plugin->version .' !');
173cc1c3 165 }
166 }
167
583fad99 168 upgrade_log_finish();
169
170 if ($updated_plugins) {
173cc1c3 171 print_continue($return);
172 die;
173 }
174}
175
88a7228a 176/**
177 * Find and check all modules and load them up or upgrade them if necessary
178 *
179 * @uses $db
180 * @uses $CFG
181 * @param string $return The url to prompt the user to continue to
182 * @todo Finish documenting this function
eef868d1 183 */
173cc1c3 184function upgrade_activity_modules($return) {
173cc1c3 185
e69ef14b 186 global $CFG, $db;
173cc1c3 187
88a7228a 188 if (!$mods = get_list_of_plugins('mod') ) {
189 error('No modules installed!');
173cc1c3 190 }
191
583fad99 192 $updated_modules = false;
193 $strmodulesetup = get_string('modulesetup');
194
173cc1c3 195 foreach ($mods as $mod) {
196
88a7228a 197 if ($mod == 'NEWMODULE') { // Someone has unzipped the template, ignore it
173cc1c3 198 continue;
199 }
200
88a7228a 201 $fullmod = $CFG->dirroot .'/mod/'. $mod;
173cc1c3 202
203 unset($module);
204
88a7228a 205 if ( is_readable($fullmod .'/version.php')) {
206 include_once($fullmod .'/version.php'); // defines $module with version etc
173cc1c3 207 } else {
88a7228a 208 notify('Module '. $mod .': '. $fullmod .'/version.php was not readable');
173cc1c3 209 continue;
210 }
211
d6eb06b6 212 $oldupgrade = false;
213 $newupgrade = false;
7c006e34 214 if ( is_readable($fullmod .'/db/' . $CFG->dbtype . '.php')) {
215 include_once($fullmod .'/db/' . $CFG->dbtype . '.php'); // defines old upgrading function
d6eb06b6 216 $oldupgrade = true;
217 }
db8bd7a6 218 if ( is_readable($fullmod . '/db/upgrade.php')) {
7c006e34 219 include_once($fullmod . '/db/upgrade.php'); // defines new upgrading function
d6eb06b6 220 $newupgrade = true;
173cc1c3 221 }
222
223 if (!isset($module)) {
224 continue;
225 }
226
227 if (!empty($module->requires)) {
228 if ($module->requires > $CFG->version) {
229 $info->modulename = $mod;
230 $info->moduleversion = $module->version;
231 $info->currentmoodle = $CFG->version;
232 $info->requiremoodle = $module->requires;
583fad99 233 if (!$updated_modules) {
eef868d1 234 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
583fad99 235 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
236 false, '&nbsp;', '&nbsp;');
237 }
238 upgrade_log_start();
173cc1c3 239 notify(get_string('modulerequirementsnotmet', 'error', $info));
583fad99 240 $updated_modules = true;
173cc1c3 241 unset($info);
242 continue;
243 }
244 }
245
246 $module->name = $mod; // The name MUST match the directory
eef868d1 247
88a7228a 248 if ($currmodule = get_record('modules', 'name', $module->name)) {
173cc1c3 249 if ($currmodule->version == $module->version) {
250 // do nothing
251 } else if ($currmodule->version < $module->version) {
d6eb06b6 252 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
253 if (!$oldupgrade && !$newupgrade) {
254 notify('Upgrade files ' . $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php or ' .
255 $fullmod . '/db/upgrade.php were not readable');
256 continue;
257 }
583fad99 258 if (!$updated_modules) {
eef868d1 259 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
a36f058e 260 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
261 false, '&nbsp;', '&nbsp;');
173cc1c3 262 }
583fad99 263 upgrade_log_start();
88a7228a 264 print_heading($module->name .' module needs upgrading');
d6eb06b6 265
266 /// Run de old and new upgrade functions for the module
267 $oldupgrade_function = $module->name . '_upgrade';
268 $newupgrade_function = 'xmldb_' . $module->name . '_upgrade';
269
270 /// First, the old function if exists
271 $oldupgrade_status = true;
272 if ($oldupgrade && function_exists($oldupgrade_function)) {
273 $db->debug = true;
274 $oldupgrade_status = $oldupgrade_function($currmodule->version, $module);
ba05965e 275 } else if ($oldupgrade) {
d6eb06b6 276 notify ('Upgrade function ' . $oldupgrade_function . ' was not available in ' .
277 $mod . ': ' . $fullmod . '/db/' . $CFG->dbtype . '.php');
d6eb06b6 278 }
279
280 /// Then, the new function if exists and the old one was ok
281 $newupgrade_status = true;
ba05965e 282 if ($newupgrade && function_exists($newupgrade_function) && $oldupgrade_status) {
d6eb06b6 283 $db->debug = true;
284 $newupgrade_status = $newupgrade_function($currmodule->version, $module);
ba05965e 285 } else if ($newupgrade) {
d6eb06b6 286 notify ('Upgrade function ' . $newupgrade_function . ' was not available in ' .
287 $mod . ': ' . $fullmod . '/db/upgrade.php');
d6eb06b6 288 }
289
e79a09a2 290 $db->debug=false;
d6eb06b6 291 /// Now analyze upgrade results
668896e5 292 if ($oldupgrade_status && $newupgrade_status) { // No upgrading failed
d6eb06b6 293 // OK so far, now update the modules record
294 $module->id = $currmodule->id;
295 if (! update_record('modules', $module)) {
296 error('Could not update '. $module->name .' record in modules table!');
173cc1c3 297 }
d6eb06b6 298 remove_dir($CFG->dataroot . '/cache', true); // flush cache
299 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
300 echo '<hr />';
301 } else {
d6eb06b6 302 notify('Upgrading '. $module->name .' from '. $currmodule->version .' to '. $module->version .' FAILED!');
173cc1c3 303 }
bbbf2d40 304
d6eb06b6 305 /// Update the capabilities table?
bbbf2d40 306 if (!update_capabilities('mod/'.$module->name)) {
307 error('Could not update '.$module->name.' capabilities!');
308 }
309
173cc1c3 310 $updated_modules = true;
eef868d1 311
173cc1c3 312 } else {
583fad99 313 upgrade_log_start();
88a7228a 314 error('Version mismatch: '. $module->name .' can\'t downgrade '. $currmodule->version .' -> '. $module->version .' !');
173cc1c3 315 }
eef868d1 316
173cc1c3 317 } else { // module not installed yet, so install it
583fad99 318 if (!$updated_modules) {
eef868d1 319 print_header($strmodulesetup, $strmodulesetup, $strmodulesetup, '',
a36f058e 320 '<script type="text/javascript" src="' . $CFG->wwwroot . '/lib/scroll_to_errors.js"></script>',
321 false, '&nbsp;', '&nbsp;');
173cc1c3 322 }
583fad99 323 upgrade_log_start();
173cc1c3 324 print_heading($module->name);
325 $updated_modules = true;
326 $db->debug = true;
327 @set_time_limit(0); // To allow slow databases to complete the long SQL
d6eb06b6 328
329 /// Both old .sql files and new install.xml are supported
330 /// but we priorize install.xml (XMLDB) if present
db8bd7a6 331 if (file_exists($fullmod . '/db/install.xml')) {
d6eb06b6 332 $status = install_from_xmldb_file($fullmod . '/db/install.xml'); //New method
333 } else {
334 $status = modify_database($fullmod .'/db/'. $CFG->dbtype .'.sql'); //Old method
335 }
336
e79a09a2 337 $db->debug = false;
d6eb06b6 338 /// Continue with the instalation, roles and other stuff
339 if ($status) {
88a7228a 340 if ($module->id = insert_record('modules', $module)) {
bbbf2d40 341 if (!update_capabilities('mod/'.$module->name)) {
342 error('Could not set up the capabilities for '.$module->name.'!');
343 }
a8f68426 344 notify(get_string('modulesuccess', '', $module->name), 'notifysuccess');
88a7228a 345 echo '<hr />';
173cc1c3 346 } else {
88a7228a 347 error($module->name .' module could not be added to the module list!');
173cc1c3 348 }
eef868d1 349 } else {
88a7228a 350 error($module->name .' tables could NOT be set up successfully!');
173cc1c3 351 }
352 }
e5bd4e58 353
354 /// Check submodules of this module if necessary
355
356 include_once($fullmod.'/lib.php'); // defines upgrading function
357
358 $submoduleupgrade = $module->name.'_upgrade_submodules';
359 if (function_exists($submoduleupgrade)) {
360 $submoduleupgrade();
361 }
362
363
364 /// Run any defaults or final code that is necessary for this module
365
a5c0990e 366 if ( is_readable($fullmod .'/defaults.php')) {
367 // Insert default values for any important configuration variables
9e6e7502 368 unset($defaults);
eef868d1 369 include_once($fullmod .'/defaults.php');
f9a2e515 370 if (!empty($defaults)) {
371 foreach ($defaults as $name => $value) {
372 if (!isset($CFG->$name)) {
373 set_config($name, $value);
374 }
a5c0990e 375 }
376 }
377 }
173cc1c3 378 }
379
583fad99 380 upgrade_log_finish(); // finish logging if started
381
382 if ($updated_modules) {
173cc1c3 383 print_continue($return);
384 die;
385 }
386}
387
eef868d1 388/**
f3221af9 389 * This function will return FALSE if the lock fails to be set (ie, if it's already locked)
80be7ee3 390 *
391 * @param string $name ?
392 * @param bool $value ?
393 * @param int $staleafter ?
394 * @param bool $clobberstale ?
395 * @todo Finish documenting this function
f3221af9 396 */
397function set_cron_lock($name,$value=true,$staleafter=7200,$clobberstale=false) {
398
399 if (empty($name)) {
400 mtrace("Tried to get a cron lock for a null fieldname");
401 return false;
402 }
403
404 if (empty($value)) {
405 set_config($name,0);
406 return true;
407 }
408
409 if ($config = get_record('config','name',$name)) {
410 if (empty($config->value)) {
411 set_config($name,time());
412 } else {
413 // check for stale.
414 if ((time() - $staleafter) > $config->value) {
415 mtrace("STALE LOCKFILE FOR $name - was $config->value");
416 if (!empty($clobberstale)) {
417 set_config($name,time());
418 return true;
419 }
420 } else {
421 return false; // was not stale - ie, we're ok to still be running.
422 }
423 }
424 }
425 else {
426 set_config($name,time());
427 }
428 return true;
429}
a597f8a8 430
fb06b255 431function print_progress($done, $total, $updatetime=5, $sleeptime=1, $donetext='') {
a597f8a8 432 static $starttime;
433 static $lasttime;
434
435 if (empty($starttime)) {
436 $starttime = $lasttime = time();
437 $lasttime = $starttime - $updatetime;
438 echo '<table width="500" cellpadding="0" cellspacing="0" align="center"><tr><td width="500">';
439 echo '<div id="bar" style="border-style:solid;border-width:1px;width:500px;height:50px;">';
440 echo '<div id="slider" style="border-style:solid;border-width:1px;height:48px;width:10px;background-color:green;"></div>';
441 echo '</div>';
442 echo '<div id="text" align="center" style="width:500px;"></div>';
443 echo '</td></tr></table>';
444 echo '</div>';
445 }
446
a597f8a8 447 $now = time();
448
449 if ($done && (($now - $lasttime) >= $updatetime)) {
450 $elapsedtime = $now - $starttime;
451 $projectedtime = (int)(((float)$total / (float)$done) * $elapsedtime) - $elapsedtime;
452 $percentage = format_float((float)$done / (float)$total, 2);
453 $width = (int)(500 * $percentage);
454
fb06b255 455 if ($projectedtime > 10) {
456 $projectedtext = ' Ending: '.format_time($projectedtime);
457 } else {
458 $projectedtext = '';
459 }
460
a597f8a8 461 echo '<script>';
fb06b255 462 echo 'document.getElementById("text").innerHTML = "'.addslashes($donetext).' '.$done.' done.'.$projectedtext.'";'."\n";
a597f8a8 463 echo 'document.getElementById("slider").style.width = \''.$width.'px\';'."\n";
464 echo '</script>';
465
466 $lasttime = $now;
467 sleep($sleeptime);
468 }
469}
583fad99 470
471////////////////////////////////////////////////
472/// upgrade logging functions
473////////////////////////////////////////////////
474
475$upgradeloghandle = false;
26c91c73 476$upgradelogbuffer = '';
477// I did not find out how to use static variable in callback function,
478// the problem was that I could not flush the static buffer :-(
479global $upgradeloghandle, $upgradelogbuffer;
583fad99 480
481/**
482 * Check if upgrade is already running.
483 *
484 * If anything goes wrong due to missing call to upgrade_log_finish()
485 * just restart the browser.
486 *
487 * @param string warning message indicating upgrade is already running
488 * @param int page reload timeout
489 */
490function upgrade_check_running($message, $timeout) {
491 if (!empty($_SESSION['upgraderunning'])) {
492 print_header();
493 redirect(me(), $message, $timeout);
494 }
495}
496
497/**
498 * Start logging of output into file (if not disabled) and
499 * prevent aborting and concurrent execution of upgrade script.
500 *
501 * Please note that you can not write into session variables after calling this function!
502 *
503 * This function may be called repeatedly.
504 */
505function upgrade_log_start() {
426a369b 506 global $CFG, $upgradeloghandle;
583fad99 507
508 if (!empty($_SESSION['upgraderunning'])) {
509 return; // logging already started
510 }
511
512 @ignore_user_abort(true); // ignore if user stops or otherwise aborts page loading
513 $_SESSION['upgraderunning'] = 1; // set upgrade indicator
426a369b 514 if (empty($CFG->dbsessions)) { // workaround for bug in adodb, db session can not be restarted
515 session_write_close(); // from now on user can reload page - will be displayed warning
516 }
583fad99 517 make_upload_directory('upgradelogs');
518 ob_start('upgrade_log_callback', 2); // function for logging to disk; flush each line of text ASAP
dedb2304 519 register_shutdown_function('upgrade_log_finish'); // in case somebody forgets to stop logging
583fad99 520}
521
522/**
523 * Terminate logging of output, flush all data, allow script aborting
524 * and reopen session for writing. Function error() does terminate the logging too.
525 *
526 * Please make sure that each upgrade_log_start() is properly terminated by
527 * this function or error().
528 *
529 * This function may be called repeatedly.
530 */
531function upgrade_log_finish() {
426a369b 532 global $CFG, $upgradeloghandle, $upgradelogbuffer;
583fad99 533
534 if (empty($_SESSION['upgraderunning'])) {
535 return; // logging already terminated
536 }
537
538 @ob_end_flush();
26c91c73 539 if ($upgradelogbuffer !== '') {
540 @fwrite($upgradeloghandle, $upgradelogbuffer);
40896537 541 $upgradelogbuffer = '';
26c91c73 542 }
543 if ($upgradeloghandle and ($upgradeloghandle !== 'error')) {
544 @fclose($upgradeloghandle);
40896537 545 $upgradeloghandle = false;
26c91c73 546 }
426a369b 547 if (empty($CFG->dbsessions)) {
548 @session_start(); // ignore header errors, we only need to reopen session
549 }
583fad99 550 $_SESSION['upgraderunning'] = 0; // clear upgrade indicator
551 if (connection_aborted()) {
552 die;
553 }
554 @ignore_user_abort(false);
555}
556
557/**
558 * Callback function for logging into files. Not more than one file is created per minute,
559 * upgrade session (terminated by upgrade_log_finish()) is always stored in one file.
560 *
561 * This function must not output any characters or throw warnigns and errors!
562 */
563function upgrade_log_callback($string) {
26c91c73 564 global $CFG, $upgradeloghandle, $upgradelogbuffer;
583fad99 565
566 if (empty($CFG->disableupgradelogging) and ($string != '') and ($upgradeloghandle !== 'error')) {
567 if ($upgradeloghandle or ($upgradeloghandle = @fopen($CFG->dataroot.'/upgradelogs/upg_'.date('Ymd-Hi').'.html', 'a'))) {
26c91c73 568 $upgradelogbuffer .= $string;
569 if (strlen($upgradelogbuffer) > 2048) { // 2kB write buffer
570 @fwrite($upgradeloghandle, $upgradelogbuffer);
571 $upgradelogbuffer = '';
572 }
583fad99 573 } else {
574 $upgradeloghandle = 'error';
575 }
576 }
577 return $string;
578}
579
57e35f32 580/**
581 * Try to verify that dataroot is not accessible from web.
582 * It is not 100% correct but might help to reduce number of vulnerable sites.
583 *
584 * Protection from httpd.conf and .htaccess is not detected properly.
585 */
586function is_dataroot_insecure() {
587 global $CFG;
588
589 $siteroot = str_replace('\\', '/', strrev($CFG->dirroot.'/')); // win32 backslash workaround
590
591 $rp = preg_replace('|https?://[^/]+|i', '', $CFG->wwwroot, 1);
592 $rp = strrev(trim($rp, '/'));
593 $rp = explode('/', $rp);
594 foreach($rp as $r) {
595 if (strpos($siteroot, '/'.$r.'/') === 0) {
596 $siteroot = substr($siteroot, strlen($r)+1); // moodle web in subdirectory
597 } else {
598 break; // probably alias root
599 }
600 }
601
602 $siteroot = strrev($siteroot);
603 $dataroot = str_replace('\\', '/', $CFG->dataroot.'/');
604
605 if (strpos($dataroot, $siteroot) === 0) {
606 return true;
607 }
608 return false;
609}
6e4dc10f 610
611/// =============================================================================================================
612/// administration tree classes and functions
613
614
615// n.b. documentation is still in progress for this code
616
617/// INTRODUCTION
618
619/// This file performs the following tasks:
620/// -it defines the necessary objects and interfaces to build the Moodle
621/// admin hierarchy
eef868d1 622/// -it defines the admin_externalpage_setup(), admin_externalpage_print_header(),
6e4dc10f 623/// and admin_externalpage_print_footer() functions used on admin pages
624
625/// ADMIN_SETTING OBJECTS
626
eef868d1 627/// Moodle settings are represented by objects that inherit from the admin_setting
6e4dc10f 628/// class. These objects encapsulate how to read a setting, how to write a new value
629/// to a setting, and how to appropriately display the HTML to modify the setting.
630
631/// ADMIN_SETTINGPAGE OBJECTS
632
633/// The admin_setting objects are then grouped into admin_settingpages. The latter
634/// appear in the Moodle admin tree block. All interaction with admin_settingpage
635/// objects is handled by the admin/settings.php file.
636
637/// ADMIN_EXTERNALPAGE OBJECTS
638
639/// There are some settings in Moodle that are too complex to (efficiently) handle
640/// with admin_settingpages. (Consider, for example, user management and displaying
641/// lists of users.) In this case, we use the admin_externalpage object. This object
642/// places a link to an external PHP file in the admin tree block.
643
644/// If you're using an admin_externalpage object for some settings, you can take
645/// advantage of the admin_externalpage_* functions. For example, suppose you wanted
646/// to add a foo.php file into admin. First off, you add the following line to
647/// admin/settings/first.php (at the end of the file) or to some other file in
648/// admin/settings:
649
eef868d1 650/// $ADMIN->add('userinterface', new admin_externalpage('foo', get_string('foo'),
6e4dc10f 651/// $CFG->wwwdir . '/' . '$CFG->admin . '/foo.php', 'some_role_permission'));
652
653/// Next, in foo.php, your file structure would resemble the following:
654
655/// require_once('.../config.php');
656/// require_once($CFG->libdir.'/adminlib.php');
657/// $adminroot = admin_get_root();
658/// admin_externalpage_setup('foo', $adminroot);
659/// // functionality like processing form submissions goes here
660/// admin_externalpage_print_header($adminroot);
661/// // your HTML goes here
662/// admin_externalpage_print_footer($adminroot);
663
664/// The admin_externalpage_setup() function call ensures the user is logged in,
665/// and makes sure that they have the proper role permission to access the page.
666
667/// The admin_externalpage_print_header() function prints the header (it figures
668/// out what category and subcategories the page is classified under) and ensures
669/// that you're using the admin pagelib (which provides the admin tree block and
670/// the admin bookmarks block).
671
672/// The admin_externalpage_print_footer() function properly closes the tables
673/// opened up by the admin_externalpage_print_header() function and prints the
674/// standard Moodle footer.
675
676/// ADMIN_CATEGORY OBJECTS
677
678/// Above and beyond all this, we have admin_category objects. These objects
679/// appear as folders in the admin tree block. They contain admin_settingpage's,
680/// admin_externalpage's, and other admin_category's.
681
682/// OTHER NOTES
683
684/// admin_settingpage's, admin_externalpage's, and admin_category's all inherit
685/// from part_of_admin_tree (a pseudointerface). This interface insists that
686/// a class has a check_access method for access permissions, a locate method
687/// used to find a specific node in the admin tree, and a path method used
688/// to determine the path to a specific node in the $ADMIN tree.
689
690/// admin_category's inherit from parentable_part_of_admin_tree. This pseudo-
691/// interface ensures that the class implements a recursive add function which
692/// accepts a part_of_admin_tree object and searches for the proper place to
693/// put it. parentable_part_of_admin_tree implies part_of_admin_tree.
694
695/// Please note that the $this->name field of any part_of_admin_tree must be
696/// UNIQUE throughout the ENTIRE admin tree.
697
698/// The $this->name field of an admin_setting object (which is *not* part_of_
699/// admin_tree) must be unique on the respective admin_settingpage where it is
700/// used.
701
702
703/// MISCELLANEOUS STUFF (used by classes defined below) ///////////////////////
704include_once($CFG->dirroot . '/backup/lib.php');
705
706/// CLASS DEFINITIONS /////////////////////////////////////////////////////////
707
708/**
709 * Pseudointerface for anything appearing in the admin tree
710 *
711 * The pseudointerface that is implemented by anything that appears in the admin tree
712 * block. It forces inheriting classes to define a method for checking user permissions
713 * and methods for finding something in the admin tree.
714 *
715 * @author Vincenzo K. Marcovecchio
716 * @package admin
717 */
718class part_of_admin_tree {
719
720 /**
721 * Finds a named part_of_admin_tree.
722 *
723 * Used to find a part_of_admin_tree. If a class only inherits part_of_admin_tree
724 * and not parentable_part_of_admin_tree, then this function should only check if
725 * $this->name matches $name. If it does, it should return a reference to $this,
726 * otherwise, it should return a reference to NULL.
727 *
728 * If a class inherits parentable_part_of_admin_tree, this method should be called
729 * recursively on all child objects (assuming, of course, the parent object's name
730 * doesn't match the search criterion).
731 *
732 * @param string $name The internal name of the part_of_admin_tree we're searching for.
733 * @return mixed An object reference or a NULL reference.
734 */
eef868d1 735 function &locate($name) {
736 trigger_error('Admin class does not implement method <strong>locate()</strong>', E_USER_WARNING);
737 return;
6e4dc10f 738 }
4672d955 739
740 /**
741 * Removes named part_of_admin_tree.
742 *
743 * @param string $name The internal name of the part_of_admin_tree we want to remove.
a8a66c96 744 * @return bool success.
4672d955 745 */
746 function prune($name) {
eef868d1 747 trigger_error('Admin class does not implement method <strong>prune()</strong>', E_USER_WARNING);
4672d955 748 return;
eef868d1 749 }
4672d955 750
6e4dc10f 751 /**
752 * Verifies current user's access to this part_of_admin_tree.
753 *
754 * Used to check if the current user has access to this part of the admin tree or
755 * not. If a class only inherits part_of_admin_tree and not parentable_part_of_admin_tree,
756 * then this method is usually just a call to has_capability() in the site context.
757 *
758 * If a class inherits parentable_part_of_admin_tree, this method should return the
759 * logical OR of the return of check_access() on all child objects.
760 *
761 * @return bool True if the user has access, false if she doesn't.
762 */
eef868d1 763 function check_access() {
764 trigger_error('Admin class does not implement method <strong>check_access()</strong>', E_USER_WARNING);
765 return;
6e4dc10f 766 }
eef868d1 767
a8a66c96 768 /**
769 * Mostly usefull for removing of some parts of the tree in admin tree block.
770 *
771 * @return True is hidden from normal list view
772 */
773 function is_hidden() {
774 trigger_error('Admin class does not implement method <strong>is_hidden()</strong>', E_USER_WARNING);
775 return;
776 }
777
6e4dc10f 778 /**
779 * Determines the path to $name in the admin tree.
780 *
781 * Used to determine the path to $name in the admin tree. If a class inherits only
782 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
783 * check if $this->name matches $name. If it does, $name is pushed onto the $path
784 * array (at the end), and $path should be returned. If it doesn't, NULL should be
785 * returned.
786 *
787 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
788 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
789 * recursively calls path() on its child objects. If any are non-NULL, it should
790 * return $path (being certain that the last element of $path is equal to $name).
791 * If they are all NULL, it returns NULL.
792 *
793 * @param string $name The internal name of the part_of_admin_tree we're searching for.
794 * @param array $path Not used on external calls. Defaults to empty array.
795 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
796 */
eef868d1 797 function path($name, $path = array()) {
798 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING);
799 return;
6e4dc10f 800 }
801}
802
803/**
804 * Pseudointerface implemented by any part_of_admin_tree that has children.
805 *
806 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
807 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
eef868d1 808 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
6e4dc10f 809 * include an add method for adding other part_of_admin_tree objects as children.
810 *
811 * @author Vincenzo K. Marcovecchio
812 * @package admin
813 */
814class parentable_part_of_admin_tree extends part_of_admin_tree {
eef868d1 815
6e4dc10f 816 /**
817 * Adds a part_of_admin_tree object to the admin tree.
818 *
819 * Used to add a part_of_admin_tree object to this object or a child of this
820 * object. $something should only be added if $destinationname matches
821 * $this->name. If it doesn't, add should be called on child objects that are
822 * also parentable_part_of_admin_tree's.
823 *
824 * @param string $destinationname The internal name of the new parent for $something.
825 * @param part_of_admin_tree &$something The object to be added.
826 * @return bool True on success, false on failure.
827 */
eef868d1 828 function add($destinationname, &$something) {
829 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING);
830 return;
6e4dc10f 831 }
eef868d1 832
6e4dc10f 833}
834
835/**
836 * The object used to represent folders (a.k.a. categories) in the admin tree block.
eef868d1 837 *
6e4dc10f 838 * Each admin_category object contains a number of part_of_admin_tree objects.
839 *
840 * @author Vincenzo K. Marcovecchio
841 * @package admin
842 */
843class admin_category extends parentable_part_of_admin_tree {
844
845 /**
846 * @var mixed An array of part_of_admin_tree objects that are this object's children
847 */
848 var $children;
eef868d1 849
6e4dc10f 850 /**
851 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
852 */
853 var $name;
eef868d1 854
6e4dc10f 855 /**
856 * @var string The displayed name for this category. Usually obtained through get_string()
857 */
858 var $visiblename;
eef868d1 859
a8a66c96 860 /**
861 * @var bool Should this category be hidden in admin tree block?
862 */
863 var $hidden;
864
6e4dc10f 865 // constructor for an empty admin category
866 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
867 // $visiblename is the displayed name of the category. use a get_string for this
868
869 /**
870 * Constructor for an empty admin category
871 *
872 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
873 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
a8a66c96 874 * @param bool $hidden hide category in admin tree block
6e4dc10f 875 * @return mixed Returns the new object.
876 */
a8a66c96 877 function admin_category($name, $visiblename, $hidden = false) {
6e4dc10f 878 $this->children = array();
879 $this->name = $name;
880 $this->visiblename = $visiblename;
a8a66c96 881 $this->hidden = $hidden;
6e4dc10f 882 }
eef868d1 883
6e4dc10f 884 /**
885 * Finds the path to the part_of_admin_tree called $name.
886 *
887 * @param string $name The internal name that we're searching for.
888 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
889 * @return mixed An array of internal names that leads to $name, or NULL if not found.
890 */
891 function path($name, $path = array()) {
eef868d1 892
6e4dc10f 893 $path[count($path)] = $this->name;
eef868d1 894
6e4dc10f 895 if ($this->name == $name) {
896 return $path;
897 }
eef868d1 898
6e4dc10f 899 foreach($this->children as $child) {
900 if ($return = $child->path($name, $path)) {
901 return $return;
902 }
903 }
eef868d1 904
6e4dc10f 905 return NULL;
eef868d1 906
6e4dc10f 907 }
908
909 /**
910 * Returns a reference to the part_of_admin_tree object with internal name $name.
911 *
912 * @param string $name The internal name of the object we want.
913 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
914 */
915 function &locate($name) {
eef868d1 916
6e4dc10f 917 if ($this->name == $name) {
918 return $this;
919 }
eef868d1 920
6e4dc10f 921 foreach($this->children as $child) {
922 if ($return =& $child->locate($name)) {
923 return $return;
924 }
925 }
926 $return = NULL;
927 return $return;
928 }
929
4672d955 930 /**
931 * Removes part_of_admin_tree object with internal name $name.
932 *
933 * @param string $name The internal name of the object we want to remove.
a8a66c96 934 * @return bool success
4672d955 935 */
936 function prune($name) {
937
938 if ($this->name == $name) {
939 return false; //can not remove itself
940 }
941
942 foreach($this->children as $precedence => $child) {
943 if ($child->name == $name) {
944 // found it!
eef868d1 945 unset($this->children[$precedence]);
4672d955 946 return true;
947 }
948 if ($this->children[$precedence]->prune($name)) {
949 return true;
950 }
951 }
952 return false;
953 }
954
6e4dc10f 955 /**
956 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
957 *
958 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
959 * @param mixed &$something A part_of_admin_tree object to be added.
960 * @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".
961 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
962 */
963 function add($destinationname, &$something, $precedence = '') {
eef868d1 964
6e4dc10f 965 if (!is_a($something, 'part_of_admin_tree')) {
966 return false;
967 }
968
969 if ($destinationname == $this->name) {
970 if ($precedence === '') {
971 $this->children[] = $something;
972 } else {
973 if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
974 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 />');
975 }
976 $this->children[$precedence] = $something;
977 }
978 return true;
979 }
eef868d1 980
6e4dc10f 981 unset($entries);
eef868d1 982
6e4dc10f 983 $entries = array_keys($this->children);
eef868d1 984
6e4dc10f 985 foreach($entries as $entry) {
986 $child =& $this->children[$entry];
987 if (is_a($child, 'parentable_part_of_admin_tree')) {
988 if ($child->add($destinationname, $something, $precedence)) {
989 return true;
990 }
991 }
992 }
eef868d1 993
6e4dc10f 994 return false;
eef868d1 995
6e4dc10f 996 }
eef868d1 997
6e4dc10f 998 /**
999 * Checks if the user has access to anything in this category.
1000 *
1001 * @return bool True if the user has access to atleast one child in this category, false otherwise.
1002 */
1003 function check_access() {
eef868d1 1004
6e4dc10f 1005 $return = false;
1006 foreach ($this->children as $child) {
1007 $return = $return || $child->check_access();
1008 }
eef868d1 1009
6e4dc10f 1010 return $return;
eef868d1 1011
6e4dc10f 1012 }
eef868d1 1013
a8a66c96 1014 /**
1015 * Is this category hidden in admin tree block?
1016 *
1017 * @return bool True if hidden
1018 */
1019 function is_hidden() {
1020 return $this->hidden;
1021 }
6e4dc10f 1022}
1023
1024/**
1025 * Links external PHP pages into the admin tree.
1026 *
1027 * See detailed usage example at the top of this document (adminlib.php)
1028 *
1029 * @author Vincenzo K. Marcovecchio
1030 * @package admin
1031 */
1032class admin_externalpage extends part_of_admin_tree {
1033
eef868d1 1034 /**
6e4dc10f 1035 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1036 */
1037 var $name;
eef868d1 1038
6e4dc10f 1039 /**
1040 * @var string The displayed name for this external page. Usually obtained through get_string().
1041 */
1042 var $visiblename;
eef868d1 1043
6e4dc10f 1044 /**
1045 * @var string The external URL that we should link to when someone requests this external page.
1046 */
1047 var $url;
eef868d1 1048
6e4dc10f 1049 /**
1050 * @var string The role capability/permission a user must have to access this external page.
1051 */
2ce38b70 1052 var $req_capability;
eef868d1 1053
a8a66c96 1054 /**
1055 * @var bool hidden in admin tree block.
1056 */
1057 var $hidden;
1058
6e4dc10f 1059 /**
1060 * Constructor for adding an external page into the admin tree.
1061 *
1062 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1063 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1064 * @param string $url The external URL that we should link to when someone requests this external page.
38d2d43b 1065 * @param mixed $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
6e4dc10f 1066 */
a8a66c96 1067 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config', $hidden=false) {
6e4dc10f 1068 $this->name = $name;
1069 $this->visiblename = $visiblename;
1070 $this->url = $url;
38d2d43b 1071 if (is_array($req_capability)) {
1072 $this->req_capability = $req_capability;
1073 } else {
1074 $this->req_capability = array($req_capability);
1075 }
a8a66c96 1076 $this->hidden = $hidden;
6e4dc10f 1077 }
eef868d1 1078
6e4dc10f 1079 /**
1080 * Finds the path to the part_of_admin_tree called $name.
1081 *
1082 * @param string $name The internal name that we're searching for.
1083 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1084 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1085 */
1086 function path($name, $path = array()) {
1087 if ($name == $this->name) {
1088 array_push($path, $this->name);
1089 return $path;
1090 } else {
1091 return NULL;
1092 }
1093 }
eef868d1 1094
6e4dc10f 1095 /**
1096 * Returns a reference to the part_of_admin_tree object with internal name $name.
1097 *
1098 * @param string $name The internal name of the object we want.
1099 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1100 */
1101 function &locate($name) {
1102 $return = ($this->name == $name ? $this : NULL);
1103 return $return;
1104 }
4672d955 1105
1106 function prune($name) {
1107 return false;
1108 }
1109
6e4dc10f 1110 /**
2ce38b70 1111 * Determines if the current user has access to this external page based on $this->req_capability.
6e4dc10f 1112 *
1113 * @uses CONTEXT_SYSTEM
1114 * @uses SITEID
1115 * @return bool True if user has access, false otherwise.
1116 */
1117 function check_access() {
1118 if (!get_site()) {
1119 return true; // no access check before site is fully set up
1120 }
eef868d1 1121 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
38d2d43b 1122 foreach($this->req_capability as $cap) {
1123 if (has_capability($cap, $context)) {
1124 return true;
1125 }
1126 }
1127 return false;
6e4dc10f 1128 }
1129
a8a66c96 1130 /**
1131 * Is this external page hidden in admin tree block?
1132 *
1133 * @return bool True if hidden
1134 */
1135 function is_hidden() {
1136 return $this->hidden;
1137 }
1138
6e4dc10f 1139}
1140
1141/**
1142 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1143 *
1144 * @author Vincenzo K. Marcovecchio
1145 * @package admin
1146 */
1147class admin_settingpage extends part_of_admin_tree {
1148
eef868d1 1149 /**
6e4dc10f 1150 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1151 */
1152 var $name;
eef868d1 1153
6e4dc10f 1154 /**
1155 * @var string The displayed name for this external page. Usually obtained through get_string().
1156 */
1157 var $visiblename;
1158 /**
1159 * @var mixed An array of admin_setting objects that are part of this setting page.
1160 */
1161 var $settings;
eef868d1 1162
6e4dc10f 1163 /**
1164 * @var string The role capability/permission a user must have to access this external page.
1165 */
2ce38b70 1166 var $req_capability;
eef868d1 1167
a8a66c96 1168 /**
1169 * @var bool hidden in admin tree block.
1170 */
1171 var $hidden;
1172
6e4dc10f 1173 // see admin_category
1174 function path($name, $path = array()) {
1175 if ($name == $this->name) {
1176 array_push($path, $this->name);
1177 return $path;
1178 } else {
1179 return NULL;
1180 }
1181 }
eef868d1 1182
6e4dc10f 1183 // see admin_category
1184 function &locate($name) {
1185 $return = ($this->name == $name ? $this : NULL);
1186 return $return;
1187 }
4672d955 1188
1189 function prune($name) {
1190 return false;
1191 }
1192
6e4dc10f 1193 // see admin_externalpage
a8a66c96 1194 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config', $hidden=false) {
6e4dc10f 1195 global $CFG;
1196 $this->settings = new stdClass();
1197 $this->name = $name;
1198 $this->visiblename = $visiblename;
38d2d43b 1199 if (is_array($req_capability)) {
1200 $this->req_capability = $req_capability;
1201 } else {
1202 $this->req_capability = array($req_capability);
1203 }
a8a66c96 1204 $this->hidden = false;
6e4dc10f 1205 }
eef868d1 1206
6e4dc10f 1207 // 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
1208 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1209 // &$setting is the admin_setting object you want to add
1210 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1211 function add(&$setting) {
1212 if (is_a($setting, 'admin_setting')) {
1213 $this->settings->{$setting->name} =& $setting;
1214 return true;
1215 }
1216 return false;
1217 }
eef868d1 1218
6e4dc10f 1219 // see admin_externalpage
1220 function check_access() {
1221 if (!get_site()) {
1222 return true; // no access check before site is fully set up
1223 }
eef868d1 1224 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
38d2d43b 1225 foreach($this->req_capability as $cap) {
1226 if (has_capability($cap, $context)) {
1227 return true;
1228 }
1229 }
1230 return false;
6e4dc10f 1231 }
eef868d1 1232
6e4dc10f 1233 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1234 // returns a string of the html
1235 function output_html() {
6153cf58 1236 $return = '<fieldset class="adminsettings">' . "\n";
1237 $return .= '<div class="clearer"><!-- --></div>' . "\n";
6e4dc10f 1238 foreach($this->settings as $setting) {
1239 $return .= $setting->output_html();
1240 }
6153cf58 1241 $return .= '</fieldset>';
6e4dc10f 1242 return $return;
1243 }
1244
1245 // 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
1246 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1247 // $data should be the result from data_submitted()
1248 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1249 function write_settings($data) {
1250 $return = '';
1251 foreach($this->settings as $setting) {
1252 if (isset($data['s_' . $setting->name])) {
1253 $return .= $setting->write_setting($data['s_' . $setting->name]);
1254 } else {
1255 $return .= $setting->write_setting('');
1256 }
1257 }
1258 return $return;
1259 }
1260
a8a66c96 1261 /**
1262 * Is this settigns page hidden in admin tree block?
1263 *
1264 * @return bool True if hidden
1265 */
1266 function is_hidden() {
1267 return $this->hidden;
1268 }
1269
6e4dc10f 1270}
1271
1272
1273// read & write happens at this level; no authentication
1274class admin_setting {
1275
1276 var $name;
1277 var $visiblename;
1278 var $description;
1279 var $defaultsetting;
1280
1281 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1282 $this->name = $name;
1283 $this->visiblename = $visiblename;
1284 $this->description = $description;
1285 $this->defaultsetting = $defaultsetting;
1286 }
eef868d1 1287
6e4dc10f 1288 function get_setting() {
1289 return NULL; // has to be overridden
1290 }
eef868d1 1291
6e4dc10f 1292 function write_setting($data) {
1293 return; // has to be overridden
1294 }
eef868d1 1295
6e4dc10f 1296 function output_html() {
1297 return; // has to be overridden
1298 }
eef868d1 1299
6e4dc10f 1300}
1301
1302
1303class admin_setting_configtext extends admin_setting {
1304
1305 var $paramtype;
1306
50999a0b 1307 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
6e4dc10f 1308 $this->paramtype = $paramtype;
1309 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1310 }
1311
cc73de71 1312 // returns a string or NULL
6e4dc10f 1313 function get_setting() {
1314 global $CFG;
c8218a42 1315 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
6e4dc10f 1316 }
eef868d1 1317
cc73de71 1318 // $data is a string
6e4dc10f 1319 function write_setting($data) {
c235598d 1320 if (is_string($this->paramtype)) {
1321 if (!$this->validate($data)) {
1322 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1323 }
1324 } else {
cc73de71 1325 if ($data != clean_param($data, $this->paramtype)) {
1326 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1327 }
c235598d 1328 }
6e4dc10f 1329 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1330 }
1331
c235598d 1332 function validate($data) {
1333 return preg_match($this->paramtype, $data);
1334 }
1335
6e4dc10f 1336 function output_html() {
c8218a42 1337 if ($this->get_setting() === NULL) {
1338 $current = $this->defaultsetting;
1339 } else {
1340 $current = $this->get_setting();
1341 }
fc52aa6e 1342 return format_admin_setting($this->name, $this->visiblename,
3c6f7a78 1343 '<input type="text" class="form-text" id="id_s_'.$this->name.'" name="s_'.$this->name.'" value="'.$current.'" />',
6153cf58 1344 $this->description);
6e4dc10f 1345 }
1346
1347}
1348
1349class admin_setting_configcheckbox extends admin_setting {
1350
1351 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1352 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1353 }
1354
1355 function get_setting() {
1356 global $CFG;
1357 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1358 }
eef868d1 1359
6e4dc10f 1360 function write_setting($data) {
1361 if ($data == '1') {
1362 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1363 } else {
1364 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1365 }
1366 }
1367
1368 function output_html() {
c8218a42 1369 if ($this->get_setting() === NULL) {
1370 $current = $this->defaultsetting;
1371 } else {
1372 $current = $this->get_setting();
1373 }
fc52aa6e 1374 return format_admin_setting($this->name, $this->visiblename,
3c6f7a78 1375 '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($current == true ? 'checked="checked"' : '') . ' />',
6153cf58 1376 $this->description);
6e4dc10f 1377 }
1378
1379}
1380
1381class admin_setting_configselect extends admin_setting {
1382
1383 var $choices;
eef868d1 1384
6e4dc10f 1385 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1386 $this->choices = $choices;
1387 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1388 }
1389
1390 function get_setting() {
1391 global $CFG;
1392 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1393 }
eef868d1 1394
6e4dc10f 1395 function write_setting($data) {
1396 // check that what we got was in the original choices
1397 // or that the data is the default setting - needed during install when choices can not be constructed yet
1398 if ($data != $this->defaultsetting and ! in_array($data, array_keys($this->choices))) {
1399 return 'Error setting ' . $this->visiblename . '<br />';
1400 }
eef868d1 1401
6e4dc10f 1402 return (set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1403 }
eef868d1 1404
6e4dc10f 1405 function output_html() {
c8218a42 1406 if ($this->get_setting() === NULL) {
1407 $current = $this->defaultsetting;
1408 } else {
1409 $current = $this->get_setting();
1410 }
afee9864 1411 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'">';
6e4dc10f 1412 foreach ($this->choices as $key => $value) {
6153cf58 1413 $return .= '<option value="'.$key.'"'.($key==$current ? ' selected="selected"' : '').'>'.$value.'</option>';
6e4dc10f 1414 }
6153cf58 1415 $return .= '</select>';
1416
fc52aa6e 1417 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1418 }
1419
1420}
1421
1422// 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
1423// internally for the setting)
1424class admin_setting_configtime extends admin_setting {
1425
1426 var $name2;
1427 var $choices;
1428 var $choices2;
6e4dc10f 1429
1430 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1431 $this->name2 = $minutesname;
1432 $this->choices = array();
1433 for ($i = 0; $i < 24; $i++) {
1434 $this->choices[$i] = $i;
1435 }
1436 $this->choices2 = array();
1437 for ($i = 0; $i < 60; $i += 5) {
1438 $this->choices2[$i] = $i;
1439 }
1440 parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1441 }
1442
1443 function get_setting() {
1444 global $CFG;
cc73de71 1445 return (isset($CFG->{$this->name}) && isset($CFG->{$this->name2}) ? array('h' => $CFG->{$this->name}, 'm' => $CFG->{$this->name2}) : NULL);
6e4dc10f 1446 }
eef868d1 1447
6e4dc10f 1448 function write_setting($data) {
1449 // check that what we got was in the original choices
1450 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
1451 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1452 }
eef868d1 1453
6e4dc10f 1454 return (set_config($this->name, $data['h']) && set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1455 }
eef868d1 1456
6e4dc10f 1457 function output_html() {
cc73de71 1458 if ($this->get_setting() === NULL) {
1459 $currentsetting = $this->defaultsetting;
1460 } else {
1461 $currentsetting = $this->get_setting();
6e4dc10f 1462 }
2e0c11dd 1463 $return = '<div class="form-group">'.
1464 '<select class="form-select" id="id_s_'.$this->name.'h" name="s_' . $this->name .'[h]">';
6e4dc10f 1465 foreach ($this->choices as $key => $value) {
cc73de71 1466 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
6e4dc10f 1467 }
2e0c11dd 1468 $return .= '</select>:<select class="form-select" id="id_s_'.$this->name.'m" name="s_' . $this->name . '[m]">';
6e4dc10f 1469 foreach ($this->choices2 as $key => $value) {
cc73de71 1470 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
eef868d1 1471 }
2e0c11dd 1472 $return .= '</select></div>';
fc52aa6e 1473 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1474 }
1475
1476}
1477
1478class admin_setting_configmultiselect extends admin_setting_configselect {
1479
1480 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1481 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1482 }
1483
1484 function get_setting() {
1485 global $CFG;
1486 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : NULL);;
1487 }
eef868d1 1488
6e4dc10f 1489 function write_setting($data) {
1490 foreach ($data as $datum) {
1491 if (! in_array($datum, array_keys($this->choices))) {
1492 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1493 }
1494 }
eef868d1 1495
6e4dc10f 1496 return (set_config($this->name, implode(',',$data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1497 }
eef868d1 1498
6e4dc10f 1499 function output_html() {
cc73de71 1500 if ($this->get_setting() === NULL) {
1501 $currentsetting = $this->defaultsetting;
1502 } else {
1503 $currentsetting = $this->get_setting();
6e4dc10f 1504 }
afee9864 1505 $return = '<select class="form-select" id="id_s_'.$this->name.'" name="s_' . $this->name .'[]" size="10" multiple="multiple">';
6e4dc10f 1506 foreach ($this->choices as $key => $value) {
1507 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ? ' selected="selected"' : '') . '>' . $value . '</option>';
1508 }
6153cf58 1509 $return .= '</select>';
fc52aa6e 1510 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1511 }
1512
1513}
1514
1515class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
eef868d1 1516
6e4dc10f 1517 function admin_setting_special_adminseesall() {
1518 $name = 'calendar_adminseesall';
1519 $visiblename = get_string('adminseesall', 'admin');
1520 $description = get_string('helpadminseesall', 'admin');
1521 parent::admin_setting($name, $visiblename, $description, 0);
1522 }
1523
1524 function write_setting($data) {
1525 global $SESSION;
1526 unset($SESSION->cal_courses_shown);
1527 parent::write_setting($data);
1528 }
1529}
1530
1531class admin_setting_sitesetselect extends admin_setting_configselect {
1532
1533 var $id;
1534
1535 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1536
1537 $this->id = SITEID;
1538 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
eef868d1 1539
6e4dc10f 1540 }
eef868d1 1541
6e4dc10f 1542 function get_setting() {
1543 $site = get_site();
1544 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1545 }
eef868d1 1546
6e4dc10f 1547 function write_setting($data) {
1548 if (!in_array($data, array_keys($this->choices))) {
1549 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1550 }
1551 $record = new stdClass();
1552 $record->id = $this->id;
1553 $temp = $this->name;
1554 $record->$temp = $data;
1555 $record->timemodified = time();
1556 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1557 }
eef868d1 1558
6e4dc10f 1559}
1560
1561
e0f6e995 1562class admin_setting_courselist_frontpage extends admin_setting_configselect {
6e4dc10f 1563
e0f6e995 1564 function admin_setting_courselist_frontpage($loggedin) {
6e4dc10f 1565 global $CFG;
1566 require_once($CFG->dirroot . '/course/lib.php');
1567 $name = 'frontpage' . ($loggedin ? 'loggedin' : '');
1568 $visiblename = get_string('frontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1569 $description = get_string('configfrontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1570 $choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
1571 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
1572 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
1573 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
1574 '' => get_string('none'));
1575 if (count_records("course") > FRONTPAGECOURSELIMIT) {
1576 unset($choices[FRONTPAGECOURSELIST]);
1577 }
4672d955 1578 $defaults = FRONTPAGECOURSELIST.',,,';
e0f6e995 1579 parent::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
6e4dc10f 1580 }
eef868d1 1581
6e4dc10f 1582 function get_setting() {
1583 global $CFG;
1c1e7af4 1584 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : ',1,,');
6e4dc10f 1585 }
eef868d1 1586
6e4dc10f 1587 function write_setting($data) {
1588 if (empty($data)) {
1589 $data = array();
e0f6e995 1590 } if (!is_array($data)) {
1591 $data = explode(',', $data);
6e4dc10f 1592 }
1593 foreach($data as $datum) {
1594 if (! in_array($datum, array_keys($this->choices))) {
1595 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1596 }
1597 }
1598 return (set_config($this->name, implode(',', $data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1599 }
eef868d1 1600
6e4dc10f 1601 function output_html() {
cc73de71 1602 if ($this->get_setting() === NULL) {
1603 $currentsetting = $this->defaultsetting;
1604 } else {
1605 $currentsetting = $this->get_setting();
6e4dc10f 1606 }
1607 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1608 if (!isset($currentsetting[$i])) {
1609 $currentsetting[$i] = 0;
1610 }
1611 }
0a7e84c3 1612 $return = '<div class="form-group">';
6e4dc10f 1613 for ($i = 0; $i < count($this->choices) - 1; $i++) {
a1d52643 1614 $return .='<select class="form-select" id="id_s_'.$this->name.$i.'" name="s_' . $this->name .'[]">';
6e4dc10f 1615 foreach ($this->choices as $key => $value) {
1616 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1617 }
1618 $return .= '</select>';
1619 if ($i !== count($this->choices) - 2) {
975211bb 1620 $return .= '<br />';
6e4dc10f 1621 }
1622 }
0a7e84c3 1623 $return .= '</div>';
1624
fc52aa6e 1625 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1626 }
1627}
1628
1629class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
1630
1631 var $id;
1632
1633 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1634
1635 $this->id = SITEID;
1636 parent::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
eef868d1 1637
6e4dc10f 1638 }
eef868d1 1639
6e4dc10f 1640 function get_setting() {
1641 $site = get_site();
1642 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1643 }
eef868d1 1644
6e4dc10f 1645 function write_setting($data) {
1646 $record = new stdClass();
1647 $record->id = $this->id;
1648 $temp = $this->name;
1649 $record->$temp = ($data == '1' ? 1 : 0);
1650 $record->timemodified = time();
1651 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1652 }
eef868d1 1653
6e4dc10f 1654}
1655
1656class admin_setting_sitesettext extends admin_setting_configtext {
1657
1658 var $id;
1659
50999a0b 1660 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
6e4dc10f 1661
1662 $this->id = SITEID;
1663 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
eef868d1 1664
6e4dc10f 1665 }
eef868d1 1666
6e4dc10f 1667 function get_setting() {
1668 $site = get_site();
1669 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1670 }
90cfbd0a 1671
6e4dc10f 1672 function write_setting($data) {
90cfbd0a 1673 if (is_string($this->paramtype)) {
1674 if (!$this->validate($data)) {
1675 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1676 }
1677 } else {
1678 $data = clean_param($data, $this->paramtype);
1679 }
eef868d1 1680
6e4dc10f 1681 $record = new stdClass();
1682 $record->id = $this->id;
90cfbd0a 1683 $record->{$this->name} = $data;
6e4dc10f 1684 $record->timemodified = time();
1685 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1686 }
90cfbd0a 1687
1688 function validate($data) {
1689 return preg_match($this->paramtype, $data);
1690 }
eef868d1 1691
6e4dc10f 1692}
1693
1694class admin_setting_special_frontpagedesc extends admin_setting {
1695
1696 var $id;
eef868d1 1697
6e4dc10f 1698 function admin_setting_special_frontpagedesc() {
1699 $this->id = SITEID;
1700 $name = 'summary';
1701 $visiblename = get_string('frontpagedescription');
1702 $description = get_string('frontpagedescriptionhelp');
1703 parent::admin_setting($name, $visiblename, $description, '');
1704 }
1705
1706 function output_html() {
eef868d1 1707
fa040029 1708 global $CFG;
1709
cc73de71 1710 if ($this->get_setting() === NULL) {
1711 $currentsetting = $this->defaultsetting;
1712 } else {
1713 $currentsetting = $this->get_setting();
1714 }
eef868d1 1715
fa040029 1716 $CFG->adminusehtmleditor = can_use_html_editor();
eef868d1 1717
fa040029 1718 $return = print_textarea($CFG->adminusehtmleditor, 15, 60, 0, 0, 's_' . $this->name, $currentsetting, 0, true);
eef868d1 1719
fc52aa6e 1720 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1721 }
eef868d1 1722
6e4dc10f 1723 function get_setting() {
eef868d1 1724
6e4dc10f 1725 $site = get_site();
1726 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
eef868d1 1727
6e4dc10f 1728 }
eef868d1 1729
6e4dc10f 1730 function write_setting($data) {
eef868d1 1731
6e4dc10f 1732 $data = addslashes(clean_param($data, PARAM_CLEANHTML));
eef868d1 1733
6e4dc10f 1734 $record = new stdClass();
1735 $record->id = $this->id;
1736 $temp = $this->name;
1737 $record->$temp = $data;
1738 $record->timemodified = time();
eef868d1 1739
6e4dc10f 1740 return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
eef868d1 1741
6e4dc10f 1742 }
1743
1744}
1745
1746
1747class admin_setting_special_editorfontlist extends admin_setting {
1748
1749 var $items;
1750
1751 function admin_setting_special_editorfontlist() {
1752 global $CFG;
1753 $name = 'editorfontlist';
1754 $visiblename = get_string('editorfontlist', 'admin');
1755 $description = get_string('configeditorfontlist', 'admin');
6e4dc10f 1756 $defaults = array('k0' => 'Trebuchet',
1757 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1758 'k1' => 'Arial',
1759 'v1' => 'arial,helvetica,sans-serif',
1760 'k2' => 'Courier New',
1761 'v2' => 'courier new,courier,monospace',
1762 'k3' => 'Georgia',
1763 'v3' => 'georgia,times new roman,times,serif',
1764 'k4' => 'Tahoma',
1765 'v4' => 'tahoma,arial,helvetica,sans-serif',
1766 'k5' => 'Times New Roman',
1767 'v5' => 'times new roman,times,serif',
1768 'k6' => 'Verdana',
1769 'v6' => 'verdana,arial,helvetica,sans-serif',
1770 'k7' => 'Impact',
1771 'v7' => 'impact',
1772 'k8' => 'Wingdings',
1773 'v8' => 'wingdings');
1774 parent::admin_setting($name, $visiblename, $description, $defaults);
1775 }
eef868d1 1776
6e4dc10f 1777 function get_setting() {
cc73de71 1778 global $CFG;
1779 if (isset($CFG->editorfontlist)) {
1780 $i = 0;
1781 $currentsetting = array();
1782 $items = explode(';', $CFG->editorfontlist);
1783 foreach ($items as $item) {
1784 $item = explode(':', $item);
1785 $currentsetting['k' . $i] = $item[0];
1786 $currentsetting['v' . $i] = $item[1];
1787 $i++;
1788 }
1789 return $currentsetting;
1790 } else {
1791 return NULL;
1792 }
6e4dc10f 1793 }
eef868d1 1794
6e4dc10f 1795 function write_setting($data) {
eef868d1 1796
6e4dc10f 1797 // there miiight be an easier way to do this :)
1798 // if this is changed, make sure the $defaults array above is modified so that this
1799 // function processes it correctly
eef868d1 1800
6e4dc10f 1801 $keys = array();
1802 $values = array();
eef868d1 1803
6e4dc10f 1804 foreach ($data as $key => $value) {
1805 if (substr($key,0,1) == 'k') {
1806 $keys[substr($key,1)] = $value;
1807 } elseif (substr($key,0,1) == 'v') {
1808 $values[substr($key,1)] = $value;
1809 }
1810 }
eef868d1 1811
6e4dc10f 1812 $result = '';
1813 for ($i = 0; $i < count($keys); $i++) {
1814 if (($keys[$i] !== '') && ($values[$i] !== '')) {
1815 $result .= clean_param($keys[$i],PARAM_NOTAGS) . ':' . clean_param($values[$i], PARAM_NOTAGS) . ';';
1816 }
1817 }
eef868d1 1818
6e4dc10f 1819 $result = substr($result, 0, -1); // trim the last semicolon
eef868d1 1820
6e4dc10f 1821 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1822 }
eef868d1 1823
6e4dc10f 1824 function output_html() {
eef868d1 1825
cc73de71 1826 if ($this->get_setting() === NULL) {
1827 $currentsetting = $this->defaultsetting;
1828 } else {
1829 $currentsetting = $this->get_setting();
6e4dc10f 1830 }
eef868d1 1831
1beed35f 1832 $return = '<div class="form-group">';
cc73de71 1833 for ($i = 0; $i < count($currentsetting) / 2; $i++) {
3c6f7a78 1834 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" />';
6e4dc10f 1835 $return .= '&nbsp;&nbsp;';
3c6f7a78 1836 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" /><br />';
6e4dc10f 1837 }
3c6f7a78 1838 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . $i . ']" value="" />';
6e4dc10f 1839 $return .= '&nbsp;&nbsp;';
3c6f7a78 1840 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . $i . ']" value="" /><br />';
1841 $return .= '<input type="text" class="form-text" name="s_editorfontlist[k' . ($i + 1) . ']" value="" />';
6e4dc10f 1842 $return .= '&nbsp;&nbsp;';
3c6f7a78 1843 $return .= '<input type="text" class="form-text" name="s_editorfontlist[v' . ($i + 1) . ']" value="" />';
1beed35f 1844 $return .= '</div>';
6153cf58 1845
fc52aa6e 1846 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 1847 }
eef868d1 1848
6e4dc10f 1849}
1850
1851class admin_setting_special_editordictionary extends admin_setting_configselect {
1852
1853 function admin_setting_special_editordictionary() {
1854 $name = 'editordictionary';
1855 $visiblename = get_string('editordictionary','admin');
1856 $description = get_string('configeditordictionary', 'admin');
1857 $choices = $this->editor_get_dictionaries();
1858 if (! is_array($choices)) {
1859 $choices = array('');
1860 }
eef868d1 1861
6e4dc10f 1862 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
1863 }
1864
1865 // function borrowed from the old moodle/admin/editor.php, slightly modified
1866 function editor_get_dictionaries () {
1867 /// Get all installed dictionaries in the system
1868
1869 global $CFG;
eef868d1 1870
6e4dc10f 1871// error_reporting(E_ALL); // for debug, final version shouldn't have this...
1872 clearstatcache();
1873
1874 // If aspellpath isn't set don't even bother ;-)
1875 if (empty($CFG->aspellpath)) {
1876 return 'Empty aspell path!';
1877 }
1878
1879 // Do we have access to popen function?
1880 if (!function_exists('popen')) {
1881 return 'Popen function disabled!';
1882 }
eef868d1 1883
6e4dc10f 1884 $cmd = $CFG->aspellpath;
1885 $output = '';
1886 $dictionaries = array();
1887 $dicts = array();
1888
1889 if(!($handle = @popen(escapeshellarg($cmd) .' dump dicts', 'r'))) {
1890 return 'Couldn\'t create handle!';
1891 }
1892
1893 while(!feof($handle)) {
1894 $output .= fread($handle, 1024);
1895 }
1896 @pclose($handle);
1897
1898 $dictionaries = explode(chr(10), $output);
1899
1900 // Get rid of possible empty values
1901 if (is_array($dictionaries)) {
1902
1903 $cnt = count($dictionaries);
1904
1905 for ($i = 0; $i < $cnt; $i++) {
1906 if (!empty($dictionaries[$i])) {
1907 $dicts[] = $dictionaries[$i];
1908 }
1909 }
1910 }
1911
1912 if (count($dicts) >= 1) {
1913 return $dicts;
1914 }
1915
1916 return 'Error! Check your aspell installation!';
1917 }
1918
eef868d1 1919
6e4dc10f 1920
1921}
1922
1923
1924class admin_setting_special_editorhidebuttons extends admin_setting {
1925
1926 var $name;
1927 var $visiblename;
1928 var $description;
1929 var $items;
1930
1931 function admin_setting_special_editorhidebuttons() {
1932 $this->name = 'editorhidebuttons';
1933 $this->visiblename = get_string('editorhidebuttons', 'admin');
1934 $this->description = get_string('confeditorhidebuttons', 'admin');
1935 $this->defaultsetting = array();
1936 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
1937 $this->items = array('fontname' => '',
1938 'fontsize' => '',
1939 'formatblock' => '',
1940 'bold' => 'ed_format_bold.gif',
1941 'italic' => 'ed_format_italic.gif',
1942 'underline' => 'ed_format_underline.gif',
1943 'strikethrough' => 'ed_format_strike.gif',
1944 'subscript' => 'ed_format_sub.gif',
1945 'superscript' => 'ed_format_sup.gif',
1946 'copy' => 'ed_copy.gif',
1947 'cut' => 'ed_cut.gif',
1948 'paste' => 'ed_paste.gif',
1949 'clean' => 'ed_wordclean.gif',
1950 'undo' => 'ed_undo.gif',
1951 'redo' => 'ed_redo.gif',
1952 'justifyleft' => 'ed_align_left.gif',
1953 'justifycenter' => 'ed_align_center.gif',
1954 'justifyright' => 'ed_align_right.gif',
1955 'justifyfull' => 'ed_align_justify.gif',
1956 'lefttoright' => 'ed_left_to_right.gif',
1957 'righttoleft' => 'ed_right_to_left.gif',
1958 'insertorderedlist' => 'ed_list_num.gif',
1959 'insertunorderedlist' => 'ed_list_bullet.gif',
1960 'outdent' => 'ed_indent_less.gif',
1961 'indent' => 'ed_indent_more.gif',
1962 'forecolor' => 'ed_color_fg.gif',
1963 'hilitecolor' => 'ed_color_bg.gif',
1964 'inserthorizontalrule' => 'ed_hr.gif',
1965 'createanchor' => 'ed_anchor.gif',
1966 'createlink' => 'ed_link.gif',
1967 'unlink' => 'ed_unlink.gif',
1968 'insertimage' => 'ed_image.gif',
1969 'inserttable' => 'insert_table.gif',
1970 'insertsmile' => 'em.icon.smile.gif',
1971 'insertchar' => 'icon_ins_char.gif',
1972 'spellcheck' => 'spell-check.gif',
1973 'htmlmode' => 'ed_html.gif',
1974 'popupeditor' => 'fullscreen_maximize.gif',
1975 'search_replace' => 'ed_replace.gif');
1976 }
1977
1978 function get_setting() {
1979 global $CFG;
1980 return (isset($CFG->{$this->name}) ? explode(' ', $CFG->{$this->name}) : NULL);
1981 }
1982
1983 function write_setting($data) {
1984 $result = array();
1985 if (empty($data)) { $data = array(); }
1986 foreach ($data as $key => $value) {
1987 if (!in_array($key, array_keys($this->items))) {
1988 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1989 }
1990 if ($value == '1') {
1991 $result[] = $key;
1992 }
1993 }
1994 return (set_config($this->name, implode(' ',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1995 }
1996
1997 function output_html() {
eef868d1 1998
6e4dc10f 1999 global $CFG;
eef868d1 2000
6e4dc10f 2001 // checkboxes with input name="$this->name[$key]" value="1"
2002 // we do 15 fields per column
eef868d1 2003
cc73de71 2004 if ($this->get_setting() === NULL) {
2005 $currentsetting = $this->defaultsetting;
2006 } else {
2007 $currentsetting = $this->get_setting();
6e4dc10f 2008 }
eef868d1 2009
1beed35f 2010 $return = '<div class="form-group">';
2011 $return .= '<table><tr><td valign="top" align="right">';
eef868d1 2012
6e4dc10f 2013 $count = 0;
eef868d1 2014
6e4dc10f 2015 foreach($this->items as $key => $value) {
2016 if ($count % 15 == 0) {
2017 $return .= '</div></td><td valign="top" align="right">';
2018 }
eef868d1 2019
6e4dc10f 2020 $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 2021 $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 2022 $count++;
2023 if ($count % 15 != 0) {
2024 $return .= '<br /><br />';
2025 }
2026 }
eef868d1 2027
2028 $return .= '</td></tr>';
6e4dc10f 2029 $return .= '</table>';
1beed35f 2030 $return .= '</div>';
6e4dc10f 2031
fc52aa6e 2032 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 2033 }
2034
2035}
2036
2037class admin_setting_backupselect extends admin_setting_configselect {
2038
2039 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
2040 parent::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
2041 }
2042
2043 function get_setting() {
2044 $backup_config = backup_get_config();
2045 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2046 }
eef868d1 2047
6e4dc10f 2048 function write_setting($data) {
2049 // check that what we got was in the original choices
2050 if (! in_array($data, array_keys($this->choices))) {
2051 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2052 }
eef868d1 2053
6e4dc10f 2054 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2055 }
2056
2057}
2058
2059class admin_setting_special_backupsaveto extends admin_setting_configtext {
2060
2061 function admin_setting_special_backupsaveto() {
2062 $name = 'backup_sche_destination';
2063 $visiblename = get_string('saveto');
2064 $description = get_string('backupsavetohelp');
2065 parent::admin_setting_configtext($name, $visiblename, $description, '', PARAM_PATH);
2066 }
eef868d1 2067
6e4dc10f 2068 function get_setting() {
2069 $backup_config = backup_get_config();
2070 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2071 }
eef868d1 2072
6e4dc10f 2073 function write_setting($data) {
2074 $data = clean_param($data, PARAM_PATH);
2075 if (!empty($data) and (substr($data,-1) == '/' or substr($data,-1) == '\\')) {
2076 return get_string('pathslasherror') . '<br />';
2077 } else if (!empty($data) and !is_dir($data)) {
2078 return get_string('pathnotexists') . '<br />';
2079 }
2080 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2081 }
2082
2083}
2084
2085class admin_setting_backupcheckbox extends admin_setting_configcheckbox {
2086
2087 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2088 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2089 }
2090
2091 function write_setting($data) {
2092 if ($data == '1') {
2093 return (backup_set_config($this->name, 1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2094 } else {
2095 return (backup_set_config($this->name, 0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2096 }
2097 }
eef868d1 2098
6e4dc10f 2099 function get_setting() {
2100 $backup_config = backup_get_config();
2101 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2102 }
2103
2104}
2105
2106class admin_setting_special_backuptime extends admin_setting_configtime {
2107
2108 function admin_setting_special_backuptime() {
2109 $name = 'backup_sche_hour';
2110 $name2 = 'backup_sche_minute';
2111 $visiblename = get_string('executeat');
2112 $description = get_string('backupexecuteathelp');
2113 $default = array('h' => 0, 'm' => 0);
2114 parent::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2115 }
eef868d1 2116
6e4dc10f 2117 function get_setting() {
2118 $backup_config = backup_get_config();
9fd9df20 2119 return (isset($backup_config->{$this->name}) && isset($backup_config->{$this->name}) ? array('h'=>$backup_config->{$this->name}, 'm'=>$backup_config->{$this->name2}) : NULL);
6e4dc10f 2120 }
eef868d1 2121
6e4dc10f 2122 function write_setting($data) {
2123 // check that what we got was in the original choices
2124 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
2125 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2126 }
eef868d1 2127
2128 return (backup_set_config($this->name, $data['h']) && backup_set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2129 }
eef868d1 2130
6e4dc10f 2131}
2132
2133class admin_setting_special_backupdays extends admin_setting {
2134
2135 function admin_setting_special_backupdays() {
2136 $name = 'backup_sche_weekdays';
2137 $visiblename = get_string('schedule');
2138 $description = get_string('backupschedulehelp');
2139 parent::admin_setting($name, $visiblename, $description, array());
2140 }
eef868d1 2141
6e4dc10f 2142 function get_setting() {
2143 $backup_config = backup_get_config();
cc73de71 2144 if (isset($backup_config->{$this->name})) {
2145 $currentsetting = $backup_config->{$this->name};
2146 return array('u' => substr($currentsetting, 0, 1),
2147 'm' => substr($currentsetting, 1, 1),
2148 't' => substr($currentsetting, 2, 1),
2149 'w' => substr($currentsetting, 3, 1),
2150 'r' => substr($currentsetting, 4, 1),
2151 'f' => substr($currentsetting, 5, 1),
2152 's' => substr($currentsetting, 6, 1));
2153 } else {
2154 return NULL;
2155 }
6e4dc10f 2156 }
eef868d1 2157
6e4dc10f 2158 function output_html() {
eef868d1 2159
cc73de71 2160 if ($this->get_setting() === NULL) {
2161 $currentsetting = $this->defaultsetting;
2162 } else {
2163 $currentsetting = $this->get_setting();
6e4dc10f 2164 }
eef868d1 2165
cc73de71 2166 // rewrite for simplicity
eef868d1 2167 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
cc73de71 2168 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
eef868d1 2169
6153cf58 2170 $return = '<table><tr><td><div align="center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
6e4dc10f 2171 get_string('monday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('tuesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
2172 get_string('wednesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('thursday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
2173 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
afee9864 2174 '<td><div 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>' .
2175 '<td><div 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>' .
2176 '<td><div 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>' .
2177 '<td><div 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>' .
2178 '<td><div 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>' .
2179 '<td><div 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>' .
2180 '<td><div 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 2181 '</tr></table>';
2182
fc52aa6e 2183 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
eef868d1 2184
6e4dc10f 2185 }
eef868d1 2186
6e4dc10f 2187 // 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
2188 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2189 // they MUST go into an array named $this->name, or else we won't receive them here
2190 function write_setting($data) {
2191 $week = 'umtwrfs';
2192 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
9fd9df20 2193 if (!empty($data)) {
2194 foreach($data as $key => $value) {
2195 if ($value == '1') {
2196 $result[strpos($week, $key)] = 1;
2197 }
2198 }
6e4dc10f 2199 }
2200 return (backup_set_config($this->name, implode('',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2201 }
2202}
2203
ee437bbc 2204class admin_setting_special_debug extends admin_setting_configselect {
6e4dc10f 2205
2206 function admin_setting_special_debug() {
2207 $name = 'debug';
2208 $visiblename = get_string('debug', 'admin');
2209 $description = get_string('configdebug', 'admin');
7eb0b60a 2210 $choices = array( DEBUG_NONE => get_string('debugnone', 'admin'),
2211 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
2212 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
38d2d43b 2213 DEBUG_ALL => get_string('debugall', 'admin'),
2214 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')
ee437bbc 2215 );
2216 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
6e4dc10f 2217 }
2218
ee437bbc 2219 function get_setting() {
2220 global $CFG;
cc73de71 2221 if (isset($CFG->debug)) {
cc73de71 2222 return $CFG->debug;
2223 } else {
2224 return NULL;
ee437bbc 2225 }
6e4dc10f 2226 }
2227
ee437bbc 2228 function write_setting($data) {
2229 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2230 }
2231
2232}
2233
2234
2235class admin_setting_special_calendar_weekend extends admin_setting {
2236
2237 function admin_setting_special_calendar_weekend() {
2238 $name = 'calendar_weekend';
2239 $visiblename = get_string('calendar_weekend', 'admin');
2240 $description = get_string('helpweekenddays', 'admin');
2241 parent::admin_setting($name, $visiblename, $description, array('u' => 1, 's' => 1));
2242 }
2243
2244 function get_setting() {
2245 global $CFG;
2246 if (isset($CFG->{$this->name})) {
2247 $setting = intval($CFG->{$this->name});
2248 return array('u' => $setting & 1, 'm' => $setting & 2, 't' => $setting & 4, 'w' => $setting & 8, 'r' => $setting & 16, 'f' => $setting & 32, 's' => $setting & 64);
2249 } else {
2250 return NULL;
2251 }
2252 }
eef868d1 2253
6e4dc10f 2254 function write_setting($data) {
2255 $week = 'umtwrfs';
2256 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2257 foreach($data as $key => $value) {
eef868d1 2258 if ($value == '1') {
6e4dc10f 2259 $result[strpos($week, $key)] = 1;
2260 }
2261 }
2262 return (set_config($this->name, bindec(implode('',$result))) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2263 }
eef868d1 2264
6e4dc10f 2265 function output_html() {
2266
cc73de71 2267 if ($this->get_setting() === NULL) {
2268 $currentsetting = $this->defaultsetting;
2269 } else {
2270 $currentsetting = $this->get_setting();
6e4dc10f 2271 }
cc73de71 2272
6153cf58 2273 $return = '<table><tr><td><div align="center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
6e4dc10f 2274 get_string('monday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('tuesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
2275 get_string('wednesday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('thursday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
2276 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
afee9864 2277 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'u" name="s_'. $this->name .'[u]" value="1" ' . ($currentsetting['u'] ? 'checked="checked"' : '') . ' /></div></td>' .
2278 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'m" name="s_'. $this->name .'[m]" value="1" ' . ($currentsetting['m'] ? 'checked="checked"' : '') . ' /></div></td>' .
2279 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'t" name="s_'. $this->name .'[t]" value="1" ' . ($currentsetting['t'] ? 'checked="checked"' : '') . ' /></div></td>' .
2280 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'w" name="s_'. $this->name .'[w]" value="1" ' . ($currentsetting['w'] ? 'checked="checked"' : '') . ' /></div></td>' .
2281 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'r" name="s_'. $this->name .'[r]" value="1" ' . ($currentsetting['r'] ? 'checked="checked"' : '') . ' /></div></td>' .
2282 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'f" name="s_'. $this->name .'[f]" value="1" ' . ($currentsetting['f'] ? 'checked="checked"' : '') . ' /></div></td>' .
2283 '<td><div align="center"><input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'s" name="s_'. $this->name .'[s]" value="1" ' . ($currentsetting['s'] ? 'checked="checked"' : '') . ' /></div></td>' .
6153cf58 2284 '</tr></table>';
2285
fc52aa6e 2286 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
eef868d1 2287
6e4dc10f 2288 }
2289
2290}
2291
2292
2293class admin_setting_special_perfdebug extends admin_setting_configcheckbox {
2294
2295 function admin_setting_special_perfdebug() {
2296 $name = 'perfdebug';
2297 $visiblename = get_string('perfdebug', 'admin');
2298 $description = get_string('configperfdebug', 'admin');
2299 parent::admin_setting_configcheckbox($name, $visiblename, $description, '');
2300 }
2301
2302 function write_setting($data) {
2303 if ($data == '1') {
2304 return (set_config($this->name,15) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2305 } else {
2306 return (set_config($this->name,7) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2307 }
2308 }
2309
2310 function output_html() {
cc73de71 2311 if ($this->get_setting() === NULL) {
2312 $currentsetting = $this->defaultsetting;
2313 } else {
2314 $currentsetting = $this->get_setting();
2315 }
eef868d1 2316
3c6f7a78 2317 $return = '<input type="checkbox" class="form-checkbox" id="id_s_'.$this->name.'" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 15 ? 'checked="checked"' : '') . ' />';
fc52aa6e 2318 return format_admin_setting($this->name, $this->visiblename, $return, $this->description);
6e4dc10f 2319 }
2320
2321}
2322
2323// Code for a function that helps externalpages print proper headers and footers
2324// N.B.: THIS FUNCTION HANDLES AUTHENTICATION
2325function admin_externalpage_setup($section, $adminroot) {
2326
2327 global $CFG, $PAGE, $USER;
eef868d1 2328
6e4dc10f 2329 require_once($CFG->libdir . '/blocklib.php');
02cc05a7 2330 require_once($CFG->dirroot . '/'.$CFG->admin.'/pagelib.php');
eef868d1 2331
02cc05a7 2332 page_map_class(PAGE_ADMIN, 'page_admin');
6e4dc10f 2333
e9a20759 2334 $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number
6e4dc10f 2335
02cc05a7 2336 $PAGE->init_extra($section); // hack alert!
6e4dc10f 2337
2338 $root = $adminroot->locate($PAGE->section);
2339
2340 if ($site = get_site()) {
2341 require_login();
2342 } else {
2343 redirect($CFG->wwwroot . '/admin/index.php');
2344 die;
2345 }
2346
2347 if (!is_a($root, 'admin_externalpage')) {
2348 error(get_string('sectionerror','admin'));
2349 die;
2350 }
2351
2352 // this eliminates our need to authenticate on the actual pages
2353 if (!($root->check_access())) {
2354 error(get_string('accessdenied', 'admin'));
2355 die;
2356 }
eef868d1 2357
6e4dc10f 2358 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
eef868d1 2359
6e4dc10f 2360 if (!isset($USER->adminediting)) {
2361 $USER->adminediting = false;
2362 }
eef868d1 2363
6e4dc10f 2364 if ($PAGE->user_allowed_editing()) {
2365 if ($adminediting == 1) {
2366 $USER->adminediting = true;
2367 } elseif ($adminediting == 0) {
2368 $USER->adminediting = false;
2369 }
2370 }
eef868d1 2371
6e4dc10f 2372}
2373
2374function admin_externalpage_print_header($adminroot) {
2375
b1ce7811 2376 global $CFG, $PAGE, $SITE;
eef868d1 2377
b1ce7811 2378 if (!empty($SITE->fullname)) {
2379 $pageblocks = blocks_setup($PAGE);
6e4dc10f 2380
eef868d1 2381 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
b1ce7811 2382 BLOCK_L_MAX_WIDTH);
eef868d1 2383
b1ce7811 2384 $PAGE->print_header();
2385 echo '<table id="layout-table"><tr>';
2386 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
2387 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
2388 echo '</td>';
a06c8c2c 2389 echo '<td id="middle-column">';
b1ce7811 2390 } else {
2391 print_header();
2392 }
6e4dc10f 2393
2394}
2395
2396function admin_externalpage_print_footer($adminroot) {
2397
b1ce7811 2398 global $CFG, $PAGE, $SITE;
2399
2400 if (!empty($SITE->fullname)) {
2401 $pageblocks = blocks_setup($PAGE);
2402 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
2403 BLOCK_R_MAX_WIDTH);
2404 echo '</td>';
2405 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
2406 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
2407 echo '</td></tr></table>';
2408 }
2409
6e4dc10f 2410 print_footer();
6e4dc10f 2411}
2412
2413function admin_get_root() {
2414 global $CFG;
2415
2416 static $ADMIN;
2417
2418 if (!isset($ADMIN)) {
2419 // start the admin tree!
2420 $ADMIN = new admin_category('root','Administration');
2421 // we process this file first to get categories up and running
50999a0b 2422 include($CFG->dirroot . '/admin/settings/top.php');
6e4dc10f 2423
2424 // now we process all other files in admin/settings to build the
2425 // admin tree
2426 foreach (glob($CFG->dirroot . '/admin/settings/*.php') as $file) {
50999a0b 2427 if ($file != $CFG->dirroot . '/admin/settings/top.php') {
6e4dc10f 2428 include_once($file);
2429 }
2430 }
2431 }
eef868d1 2432
6e4dc10f 2433 return $ADMIN;
2434}
2435
2436/// settings utiliti functions
2437
2438// n.b. this function unconditionally applies default settings
2439function apply_default_settings(&$node) {
2440
2441 global $CFG;
2442
2443 if (is_a($node, 'admin_category')) {
2444 $entries = array_keys($node->children);
2445 foreach ($entries as $entry) {
2446 apply_default_settings($node->children[$entry]);
2447 }
2448 return;
eef868d1 2449 }
6e4dc10f 2450
eef868d1 2451 if (is_a($node, 'admin_settingpage')) {
6e4dc10f 2452 foreach ($node->settings as $setting) {
2453 $CFG->{$setting->name} = $setting->defaultsetting;
2454 $setting->write_setting($setting->defaultsetting);
2455 unset($setting); // needed to prevent odd (imho) reference behaviour
2456 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
2457 }
2458 return;
2459 }
2460
2461 return;
2462
2463}
2464
2465// n.b. this function unconditionally applies default settings
2466function apply_default_exception_settings($defaults) {
2467
2468 global $CFG;
2469
2470 foreach($defaults as $key => $value) {
2471 $CFG->$key = $value;
2472 set_config($key, $value);
2473 }
2474
2475}
2476
fc52aa6e 2477function format_admin_setting($name, $title='', $form='', $description='') {
6153cf58 2478 return "\n".
fc52aa6e 2479 '<div class="form-item" id="admin-'.$name.'">'."\n".
6153cf58 2480 '<label for="id_s_'.$name.'">'.$title."\n".
2481 ' <span class="form-shortname">'.$name.'</span>'."\n".
2482 '</label>'."\n".
2483 $form."\n".
edb6cba4 2484 '<div class="description">'.$description.'</div>'."\n".
23586aca 2485 '</div>'.
2486 "\n\n";
2c8766df 2487}
2488
6153cf58 2489?>