Handle site-level block names in capability settings too
[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.
744 * @return boolean success.
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
6e4dc10f 768 /**
769 * Determines the path to $name in the admin tree.
770 *
771 * Used to determine the path to $name in the admin tree. If a class inherits only
772 * part_of_admin_tree and not parentable_part_of_admin_tree, then this method should
773 * check if $this->name matches $name. If it does, $name is pushed onto the $path
774 * array (at the end), and $path should be returned. If it doesn't, NULL should be
775 * returned.
776 *
777 * If a class inherits parentable_part_of_admin_tree, it should do the above, but not
778 * return NULL on failure. Instead, it pushes $this->name onto $path, and then
779 * recursively calls path() on its child objects. If any are non-NULL, it should
780 * return $path (being certain that the last element of $path is equal to $name).
781 * If they are all NULL, it returns NULL.
782 *
783 * @param string $name The internal name of the part_of_admin_tree we're searching for.
784 * @param array $path Not used on external calls. Defaults to empty array.
785 * @return mixed If found, an array containing the internal names of each part_of_admin_tree that leads to $name. If not found, NULL.
786 */
eef868d1 787 function path($name, $path = array()) {
788 trigger_error('Admin class does not implement method <strong>path()</strong>', E_USER_WARNING);
789 return;
6e4dc10f 790 }
791}
792
793/**
794 * Pseudointerface implemented by any part_of_admin_tree that has children.
795 *
796 * The pseudointerface implemented by any part_of_admin_tree that can be a parent
797 * to other part_of_admin_tree's. (For now, this only includes admin_category.) Apart
eef868d1 798 * from ensuring part_of_admin_tree compliancy, it also ensures inheriting methods
6e4dc10f 799 * include an add method for adding other part_of_admin_tree objects as children.
800 *
801 * @author Vincenzo K. Marcovecchio
802 * @package admin
803 */
804class parentable_part_of_admin_tree extends part_of_admin_tree {
eef868d1 805
6e4dc10f 806 /**
807 * Adds a part_of_admin_tree object to the admin tree.
808 *
809 * Used to add a part_of_admin_tree object to this object or a child of this
810 * object. $something should only be added if $destinationname matches
811 * $this->name. If it doesn't, add should be called on child objects that are
812 * also parentable_part_of_admin_tree's.
813 *
814 * @param string $destinationname The internal name of the new parent for $something.
815 * @param part_of_admin_tree &$something The object to be added.
816 * @return bool True on success, false on failure.
817 */
eef868d1 818 function add($destinationname, &$something) {
819 trigger_error('Admin class does not implement method <strong>add()</strong>', E_USER_WARNING);
820 return;
6e4dc10f 821 }
eef868d1 822
6e4dc10f 823}
824
825/**
826 * The object used to represent folders (a.k.a. categories) in the admin tree block.
eef868d1 827 *
6e4dc10f 828 * Each admin_category object contains a number of part_of_admin_tree objects.
829 *
830 * @author Vincenzo K. Marcovecchio
831 * @package admin
832 */
833class admin_category extends parentable_part_of_admin_tree {
834
835 /**
836 * @var mixed An array of part_of_admin_tree objects that are this object's children
837 */
838 var $children;
eef868d1 839
6e4dc10f 840 /**
841 * @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
842 */
843 var $name;
eef868d1 844
6e4dc10f 845 /**
846 * @var string The displayed name for this category. Usually obtained through get_string()
847 */
848 var $visiblename;
eef868d1 849
6e4dc10f 850 // constructor for an empty admin category
851 // $name is the internal name of the category. it MUST be unique in the entire hierarchy
852 // $visiblename is the displayed name of the category. use a get_string for this
853
854 /**
855 * Constructor for an empty admin category
856 *
857 * @param string $name The internal name for this category. Must be unique amongst ALL part_of_admin_tree objects
858 * @param string $visiblename The displayed named for this category. Usually obtained through get_string()
859 * @return mixed Returns the new object.
860 */
861 function admin_category($name, $visiblename) {
862 $this->children = array();
863 $this->name = $name;
864 $this->visiblename = $visiblename;
865 }
eef868d1 866
6e4dc10f 867 /**
868 * Finds the path to the part_of_admin_tree called $name.
869 *
870 * @param string $name The internal name that we're searching for.
871 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
872 * @return mixed An array of internal names that leads to $name, or NULL if not found.
873 */
874 function path($name, $path = array()) {
eef868d1 875
6e4dc10f 876 $path[count($path)] = $this->name;
eef868d1 877
6e4dc10f 878 if ($this->name == $name) {
879 return $path;
880 }
eef868d1 881
6e4dc10f 882 foreach($this->children as $child) {
883 if ($return = $child->path($name, $path)) {
884 return $return;
885 }
886 }
eef868d1 887
6e4dc10f 888 return NULL;
eef868d1 889
6e4dc10f 890 }
891
892 /**
893 * Returns a reference to the part_of_admin_tree object with internal name $name.
894 *
895 * @param string $name The internal name of the object we want.
896 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
897 */
898 function &locate($name) {
eef868d1 899
6e4dc10f 900 if ($this->name == $name) {
901 return $this;
902 }
eef868d1 903
6e4dc10f 904 foreach($this->children as $child) {
905 if ($return =& $child->locate($name)) {
906 return $return;
907 }
908 }
909 $return = NULL;
910 return $return;
911 }
912
4672d955 913 /**
914 * Removes part_of_admin_tree object with internal name $name.
915 *
916 * @param string $name The internal name of the object we want to remove.
917 * @return boolean success
918 */
919 function prune($name) {
920
921 if ($this->name == $name) {
922 return false; //can not remove itself
923 }
924
925 foreach($this->children as $precedence => $child) {
926 if ($child->name == $name) {
927 // found it!
eef868d1 928 unset($this->children[$precedence]);
4672d955 929 return true;
930 }
931 if ($this->children[$precedence]->prune($name)) {
932 return true;
933 }
934 }
935 return false;
936 }
937
6e4dc10f 938 /**
939 * Adds a part_of_admin_tree to a child or grandchild (or great-grandchild, and so forth) of this object.
940 *
941 * @param string $destinationame The internal name of the immediate parent that we want for &$something.
942 * @param mixed &$something A part_of_admin_tree object to be added.
943 * @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".
944 * @return bool True if successfully added, false if &$something is not a part_of_admin_tree or if $name is not found.
945 */
946 function add($destinationname, &$something, $precedence = '') {
eef868d1 947
6e4dc10f 948 if (!is_a($something, 'part_of_admin_tree')) {
949 return false;
950 }
951
952 if ($destinationname == $this->name) {
953 if ($precedence === '') {
954 $this->children[] = $something;
955 } else {
956 if (isset($this->children[$precedence])) { // this should never, ever be triggered in a release version of moodle.
957 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 />');
958 }
959 $this->children[$precedence] = $something;
960 }
961 return true;
962 }
eef868d1 963
6e4dc10f 964 unset($entries);
eef868d1 965
6e4dc10f 966 $entries = array_keys($this->children);
eef868d1 967
6e4dc10f 968 foreach($entries as $entry) {
969 $child =& $this->children[$entry];
970 if (is_a($child, 'parentable_part_of_admin_tree')) {
971 if ($child->add($destinationname, $something, $precedence)) {
972 return true;
973 }
974 }
975 }
eef868d1 976
6e4dc10f 977 return false;
eef868d1 978
6e4dc10f 979 }
eef868d1 980
6e4dc10f 981 /**
982 * Checks if the user has access to anything in this category.
983 *
984 * @return bool True if the user has access to atleast one child in this category, false otherwise.
985 */
986 function check_access() {
eef868d1 987
6e4dc10f 988 $return = false;
989 foreach ($this->children as $child) {
990 $return = $return || $child->check_access();
991 }
eef868d1 992
6e4dc10f 993 return $return;
eef868d1 994
6e4dc10f 995 }
eef868d1 996
6e4dc10f 997}
998
999/**
1000 * Links external PHP pages into the admin tree.
1001 *
1002 * See detailed usage example at the top of this document (adminlib.php)
1003 *
1004 * @author Vincenzo K. Marcovecchio
1005 * @package admin
1006 */
1007class admin_externalpage extends part_of_admin_tree {
1008
eef868d1 1009 /**
6e4dc10f 1010 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1011 */
1012 var $name;
eef868d1 1013
6e4dc10f 1014 /**
1015 * @var string The displayed name for this external page. Usually obtained through get_string().
1016 */
1017 var $visiblename;
eef868d1 1018
6e4dc10f 1019 /**
1020 * @var string The external URL that we should link to when someone requests this external page.
1021 */
1022 var $url;
eef868d1 1023
6e4dc10f 1024 /**
1025 * @var string The role capability/permission a user must have to access this external page.
1026 */
2ce38b70 1027 var $req_capability;
eef868d1 1028
6e4dc10f 1029 /**
1030 * Constructor for adding an external page into the admin tree.
1031 *
1032 * @param string $name The internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects.
1033 * @param string $visiblename The displayed name for this external page. Usually obtained through get_string().
1034 * @param string $url The external URL that we should link to when someone requests this external page.
2ce38b70 1035 * @param string $req_capability The role capability/permission a user must have to access this external page. Defaults to 'moodle/site:config'.
6e4dc10f 1036 */
2ce38b70 1037 function admin_externalpage($name, $visiblename, $url, $req_capability = 'moodle/site:config') {
6e4dc10f 1038 $this->name = $name;
1039 $this->visiblename = $visiblename;
1040 $this->url = $url;
2ce38b70 1041 $this->req_capability = $req_capability;
6e4dc10f 1042 }
eef868d1 1043
6e4dc10f 1044 /**
1045 * Finds the path to the part_of_admin_tree called $name.
1046 *
1047 * @param string $name The internal name that we're searching for.
1048 * @param array $path Used internally for recursive calls. Do not specify on external calls. Defaults to array().
1049 * @return mixed An array of internal names that leads to $name, or NULL if not found.
1050 */
1051 function path($name, $path = array()) {
1052 if ($name == $this->name) {
1053 array_push($path, $this->name);
1054 return $path;
1055 } else {
1056 return NULL;
1057 }
1058 }
eef868d1 1059
6e4dc10f 1060 /**
1061 * Returns a reference to the part_of_admin_tree object with internal name $name.
1062 *
1063 * @param string $name The internal name of the object we want.
1064 * @return mixed A reference to the object with internal name $name if found, otherwise a reference to NULL.
1065 */
1066 function &locate($name) {
1067 $return = ($this->name == $name ? $this : NULL);
1068 return $return;
1069 }
4672d955 1070
1071 function prune($name) {
1072 return false;
1073 }
1074
6e4dc10f 1075 /**
2ce38b70 1076 * Determines if the current user has access to this external page based on $this->req_capability.
6e4dc10f 1077 *
1078 * @uses CONTEXT_SYSTEM
1079 * @uses SITEID
1080 * @return bool True if user has access, false otherwise.
1081 */
1082 function check_access() {
1083 if (!get_site()) {
1084 return true; // no access check before site is fully set up
1085 }
eef868d1 1086 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
2ce38b70 1087 return has_capability($this->req_capability, $context);
6e4dc10f 1088 }
1089
1090}
1091
1092/**
1093 * Used to group a number of admin_setting objects into a page and add them to the admin tree.
1094 *
1095 * @author Vincenzo K. Marcovecchio
1096 * @package admin
1097 */
1098class admin_settingpage extends part_of_admin_tree {
1099
eef868d1 1100 /**
6e4dc10f 1101 * @var string An internal name for this external page. Must be unique amongst ALL part_of_admin_tree objects
1102 */
1103 var $name;
eef868d1 1104
6e4dc10f 1105 /**
1106 * @var string The displayed name for this external page. Usually obtained through get_string().
1107 */
1108 var $visiblename;
1109 /**
1110 * @var mixed An array of admin_setting objects that are part of this setting page.
1111 */
1112 var $settings;
eef868d1 1113
6e4dc10f 1114 /**
1115 * @var string The role capability/permission a user must have to access this external page.
1116 */
2ce38b70 1117 var $req_capability;
eef868d1 1118
6e4dc10f 1119 // see admin_category
1120 function path($name, $path = array()) {
1121 if ($name == $this->name) {
1122 array_push($path, $this->name);
1123 return $path;
1124 } else {
1125 return NULL;
1126 }
1127 }
eef868d1 1128
6e4dc10f 1129 // see admin_category
1130 function &locate($name) {
1131 $return = ($this->name == $name ? $this : NULL);
1132 return $return;
1133 }
4672d955 1134
1135 function prune($name) {
1136 return false;
1137 }
1138
6e4dc10f 1139 // see admin_externalpage
2ce38b70 1140 function admin_settingpage($name, $visiblename, $req_capability = 'moodle/site:config') {
6e4dc10f 1141 global $CFG;
1142 $this->settings = new stdClass();
1143 $this->name = $name;
1144 $this->visiblename = $visiblename;
2ce38b70 1145 $this->req_capability = $req_capability;
6e4dc10f 1146 }
eef868d1 1147
6e4dc10f 1148 // 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
1149 // n.b. each admin_setting in an admin_settingpage must have a unique internal name
1150 // &$setting is the admin_setting object you want to add
1151 // returns true if successful, false if not (will fail if &$setting is an admin_setting or child thereof)
1152 function add(&$setting) {
1153 if (is_a($setting, 'admin_setting')) {
1154 $this->settings->{$setting->name} =& $setting;
1155 return true;
1156 }
1157 return false;
1158 }
eef868d1 1159
6e4dc10f 1160 // see admin_externalpage
1161 function check_access() {
1162 if (!get_site()) {
1163 return true; // no access check before site is fully set up
1164 }
eef868d1 1165 $context = get_context_instance(CONTEXT_SYSTEM, SITEID);
2ce38b70 1166 return has_capability($this->req_capability, $context);
6e4dc10f 1167 }
eef868d1 1168
6e4dc10f 1169 // outputs this page as html in a table (suitable for inclusion in an admin pagetype)
1170 // returns a string of the html
1171 function output_html() {
1172 $return = '<table class="generaltable" width="100%" border="0" align="center" cellpadding="5" cellspacing="1">' . "\n";
1173 foreach($this->settings as $setting) {
1174 $return .= $setting->output_html();
1175 }
1176 $return .= '</table>';
1177 return $return;
1178 }
1179
1180 // 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
1181 // -- calls write_setting() to each child setting, sending it only the data that matches each setting's internal name
1182 // $data should be the result from data_submitted()
1183 // returns an empty string if everything went well, otherwise returns a printable error string (that's language-specific)
1184 function write_settings($data) {
1185 $return = '';
1186 foreach($this->settings as $setting) {
1187 if (isset($data['s_' . $setting->name])) {
1188 $return .= $setting->write_setting($data['s_' . $setting->name]);
1189 } else {
1190 $return .= $setting->write_setting('');
1191 }
1192 }
1193 return $return;
1194 }
1195
1196}
1197
1198
1199// read & write happens at this level; no authentication
1200class admin_setting {
1201
1202 var $name;
1203 var $visiblename;
1204 var $description;
1205 var $defaultsetting;
1206
1207 function admin_setting($name, $visiblename, $description, $defaultsetting) {
1208 $this->name = $name;
1209 $this->visiblename = $visiblename;
1210 $this->description = $description;
1211 $this->defaultsetting = $defaultsetting;
1212 }
eef868d1 1213
6e4dc10f 1214 function get_setting() {
1215 return NULL; // has to be overridden
1216 }
eef868d1 1217
6e4dc10f 1218 function write_setting($data) {
1219 return; // has to be overridden
1220 }
eef868d1 1221
6e4dc10f 1222 function output_html() {
1223 return; // has to be overridden
1224 }
eef868d1 1225
6e4dc10f 1226}
1227
1228
1229class admin_setting_configtext extends admin_setting {
1230
1231 var $paramtype;
1232
50999a0b 1233 function admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
6e4dc10f 1234 $this->paramtype = $paramtype;
1235 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1236 }
1237
cc73de71 1238 // returns a string or NULL
6e4dc10f 1239 function get_setting() {
1240 global $CFG;
c8218a42 1241 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
6e4dc10f 1242 }
eef868d1 1243
cc73de71 1244 // $data is a string
6e4dc10f 1245 function write_setting($data) {
c235598d 1246 if (is_string($this->paramtype)) {
1247 if (!$this->validate($data)) {
1248 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1249 }
1250 } else {
cc73de71 1251 if ($data != clean_param($data, $this->paramtype)) {
1252 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1253 }
c235598d 1254 }
6e4dc10f 1255 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1256 }
1257
c235598d 1258 function validate($data) {
1259 return preg_match($this->paramtype, $data);
1260 }
1261
6e4dc10f 1262 function output_html() {
c8218a42 1263 if ($this->get_setting() === NULL) {
1264 $current = $this->defaultsetting;
1265 } else {
1266 $current = $this->get_setting();
1267 }
6e4dc10f 1268 return '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td>' .
c8218a42 1269 '<td align="left"><input type="text" size="50" name="s_'. $this->name .'" value="'. $current .'" /></td></tr>' .
6e4dc10f 1270 '<tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1271 }
1272
1273}
1274
1275class admin_setting_configcheckbox extends admin_setting {
1276
1277 function admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting) {
1278 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1279 }
1280
1281 function get_setting() {
1282 global $CFG;
1283 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1284 }
eef868d1 1285
6e4dc10f 1286 function write_setting($data) {
1287 if ($data == '1') {
1288 return (set_config($this->name,1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1289 } else {
1290 return (set_config($this->name,0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1291 }
1292 }
1293
1294 function output_html() {
c8218a42 1295 if ($this->get_setting() === NULL) {
1296 $current = $this->defaultsetting;
1297 } else {
1298 $current = $this->get_setting();
1299 }
6e4dc10f 1300 return '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td>' .
c8218a42 1301 '<td align="left"><input type="checkbox" size="50" name="s_'. $this->name .'" value="1" ' . ($current == true ? 'checked="checked"' : '') . ' /></td></tr>' .
6e4dc10f 1302 '<tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1303 }
1304
1305}
1306
1307class admin_setting_configselect extends admin_setting {
1308
1309 var $choices;
eef868d1 1310
6e4dc10f 1311 function admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices) {
1312 $this->choices = $choices;
1313 parent::admin_setting($name, $visiblename, $description, $defaultsetting);
1314 }
1315
1316 function get_setting() {
1317 global $CFG;
1318 return (isset($CFG->{$this->name}) ? $CFG->{$this->name} : NULL);
1319 }
eef868d1 1320
6e4dc10f 1321 function write_setting($data) {
1322 // check that what we got was in the original choices
1323 // or that the data is the default setting - needed during install when choices can not be constructed yet
1324 if ($data != $this->defaultsetting and ! in_array($data, array_keys($this->choices))) {
1325 return 'Error setting ' . $this->visiblename . '<br />';
1326 }
eef868d1 1327
6e4dc10f 1328 return (set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1329 }
eef868d1 1330
6e4dc10f 1331 function output_html() {
c8218a42 1332 if ($this->get_setting() === NULL) {
1333 $current = $this->defaultsetting;
1334 } else {
1335 $current = $this->get_setting();
1336 }
6e4dc10f 1337 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left"><select name="s_' . $this->name .'">';
1338 foreach ($this->choices as $key => $value) {
c8218a42 1339 $return .= '<option value="' . $key . '"' . ($key == $current ? ' selected="selected"' : '') . '>' . $value . '</option>';
6e4dc10f 1340 }
1341 $return .= '</select></td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1342 return $return;
1343 }
1344
1345}
1346
1347// 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
1348// internally for the setting)
1349class admin_setting_configtime extends admin_setting {
1350
1351 var $name2;
1352 var $choices;
1353 var $choices2;
6e4dc10f 1354
1355 function admin_setting_configtime($hoursname, $minutesname, $visiblename, $description, $defaultsetting) {
1356 $this->name2 = $minutesname;
1357 $this->choices = array();
1358 for ($i = 0; $i < 24; $i++) {
1359 $this->choices[$i] = $i;
1360 }
1361 $this->choices2 = array();
1362 for ($i = 0; $i < 60; $i += 5) {
1363 $this->choices2[$i] = $i;
1364 }
1365 parent::admin_setting($hoursname, $visiblename, $description, $defaultsetting);
1366 }
1367
1368 function get_setting() {
1369 global $CFG;
cc73de71 1370 return (isset($CFG->{$this->name}) && isset($CFG->{$this->name2}) ? array('h' => $CFG->{$this->name}, 'm' => $CFG->{$this->name2}) : NULL);
6e4dc10f 1371 }
eef868d1 1372
6e4dc10f 1373 function write_setting($data) {
1374 // check that what we got was in the original choices
1375 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
1376 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1377 }
eef868d1 1378
6e4dc10f 1379 return (set_config($this->name, $data['h']) && set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1380 }
eef868d1 1381
6e4dc10f 1382 function output_html() {
cc73de71 1383 if ($this->get_setting() === NULL) {
1384 $currentsetting = $this->defaultsetting;
1385 } else {
1386 $currentsetting = $this->get_setting();
6e4dc10f 1387 }
1388 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left"><select name="s_' . $this->name .'[h]">';
1389 foreach ($this->choices as $key => $value) {
cc73de71 1390 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['h'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
6e4dc10f 1391 }
1392 $return .= '</select>&nbsp;&nbsp;&nbsp;<select name="s_' . $this->name . '[m]">';
1393 foreach ($this->choices2 as $key => $value) {
cc73de71 1394 $return .= '<option value="' . $key . '"' . ($key == $currentsetting['m'] ? ' selected="selected"' : '') . '>' . $value . '</option>';
eef868d1 1395 }
6e4dc10f 1396 $return .= '</select></td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1397 return $return;
1398 }
1399
1400}
1401
1402class admin_setting_configmultiselect extends admin_setting_configselect {
1403
1404 function admin_setting_configmultiselect($name, $visiblename, $description, $defaultsetting, $choices) {
1405 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
1406 }
1407
1408 function get_setting() {
1409 global $CFG;
1410 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : NULL);;
1411 }
eef868d1 1412
6e4dc10f 1413 function write_setting($data) {
1414 foreach ($data as $datum) {
1415 if (! in_array($datum, array_keys($this->choices))) {
1416 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1417 }
1418 }
eef868d1 1419
6e4dc10f 1420 return (set_config($this->name, implode(',',$data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1421 }
eef868d1 1422
6e4dc10f 1423 function output_html() {
cc73de71 1424 if ($this->get_setting() === NULL) {
1425 $currentsetting = $this->defaultsetting;
1426 } else {
1427 $currentsetting = $this->get_setting();
6e4dc10f 1428 }
1429 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left"><select name="s_' . $this->name .'[]" size="10" multiple="multiple">';
1430 foreach ($this->choices as $key => $value) {
1431 $return .= '<option value="' . $key . '"' . (in_array($key,$currentsetting) ? ' selected="selected"' : '') . '>' . $value . '</option>';
1432 }
1433 $return .= '</select></td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1434 return $return;
1435 }
1436
1437}
1438
1439class admin_setting_special_adminseesall extends admin_setting_configcheckbox {
eef868d1 1440
6e4dc10f 1441 function admin_setting_special_adminseesall() {
1442 $name = 'calendar_adminseesall';
1443 $visiblename = get_string('adminseesall', 'admin');
1444 $description = get_string('helpadminseesall', 'admin');
1445 parent::admin_setting($name, $visiblename, $description, 0);
1446 }
1447
1448 function write_setting($data) {
1449 global $SESSION;
1450 unset($SESSION->cal_courses_shown);
1451 parent::write_setting($data);
1452 }
1453}
1454
1455class admin_setting_sitesetselect extends admin_setting_configselect {
1456
1457 var $id;
1458
1459 function admin_setting_sitesetselect($name, $visiblename, $description, $defaultsetting, $choices) {
1460
1461 $this->id = SITEID;
1462 parent::admin_setting_configselect($name, $visiblename, $description, $defaultsetting, $choices);
eef868d1 1463
6e4dc10f 1464 }
eef868d1 1465
6e4dc10f 1466 function get_setting() {
1467 $site = get_site();
1468 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1469 }
eef868d1 1470
6e4dc10f 1471 function write_setting($data) {
1472 if (!in_array($data, array_keys($this->choices))) {
1473 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1474 }
1475 $record = new stdClass();
1476 $record->id = $this->id;
1477 $temp = $this->name;
1478 $record->$temp = $data;
1479 $record->timemodified = time();
1480 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1481 }
eef868d1 1482
6e4dc10f 1483}
1484
1485
e0f6e995 1486class admin_setting_courselist_frontpage extends admin_setting_configselect {
6e4dc10f 1487
e0f6e995 1488 function admin_setting_courselist_frontpage($loggedin) {
6e4dc10f 1489 global $CFG;
1490 require_once($CFG->dirroot . '/course/lib.php');
1491 $name = 'frontpage' . ($loggedin ? 'loggedin' : '');
1492 $visiblename = get_string('frontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1493 $description = get_string('configfrontpage' . ($loggedin ? 'loggedin' : ''),'admin');
1494 $choices = array(FRONTPAGENEWS => get_string('frontpagenews'),
1495 FRONTPAGECOURSELIST => get_string('frontpagecourselist'),
1496 FRONTPAGECATEGORYNAMES => get_string('frontpagecategorynames'),
1497 FRONTPAGECATEGORYCOMBO => get_string('frontpagecategorycombo'),
1498 '' => get_string('none'));
1499 if (count_records("course") > FRONTPAGECOURSELIMIT) {
1500 unset($choices[FRONTPAGECOURSELIST]);
1501 }
4672d955 1502 $defaults = FRONTPAGECOURSELIST.',,,';
e0f6e995 1503 parent::admin_setting_configselect($name, $visiblename, $description, $defaults, $choices);
6e4dc10f 1504 }
eef868d1 1505
6e4dc10f 1506 function get_setting() {
1507 global $CFG;
1c1e7af4 1508 return (isset($CFG->{$this->name}) ? explode(',', $CFG->{$this->name}) : ',1,,');
6e4dc10f 1509 }
eef868d1 1510
6e4dc10f 1511 function write_setting($data) {
1512 if (empty($data)) {
1513 $data = array();
e0f6e995 1514 } if (!is_array($data)) {
1515 $data = explode(',', $data);
6e4dc10f 1516 }
1517 foreach($data as $datum) {
1518 if (! in_array($datum, array_keys($this->choices))) {
1519 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1520 }
1521 }
1522 return (set_config($this->name, implode(',', $data)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1523 }
eef868d1 1524
6e4dc10f 1525 function output_html() {
cc73de71 1526 if ($this->get_setting() === NULL) {
1527 $currentsetting = $this->defaultsetting;
1528 } else {
1529 $currentsetting = $this->get_setting();
6e4dc10f 1530 }
1531 for ($i = 0; $i < count($this->choices) - 1; $i++) {
1532 if (!isset($currentsetting[$i])) {
1533 $currentsetting[$i] = 0;
1534 }
1535 }
1536 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left">';
1537 for ($i = 0; $i < count($this->choices) - 1; $i++) {
eef868d1 1538 $return .='<select name="s_' . $this->name .'[]">';
6e4dc10f 1539 foreach ($this->choices as $key => $value) {
1540 $return .= '<option value="' . $key . '"' . ($key == $currentsetting[$i] ? ' selected="selected"' : '') . '>' . $value . '</option>';
1541 }
1542 $return .= '</select>';
1543 if ($i !== count($this->choices) - 2) {
975211bb 1544 $return .= '<br />';
6e4dc10f 1545 }
1546 }
1547 $return .= '</td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
eef868d1 1548 return $return;
6e4dc10f 1549 }
1550}
1551
1552class admin_setting_sitesetcheckbox extends admin_setting_configcheckbox {
1553
1554 var $id;
1555
1556 function admin_setting_sitesetcheckbox($name, $visiblename, $description, $defaultsetting) {
1557
1558 $this->id = SITEID;
1559 parent::admin_setting_configcheckbox($name, $visiblename, $description, $defaultsetting);
eef868d1 1560
6e4dc10f 1561 }
eef868d1 1562
6e4dc10f 1563 function get_setting() {
1564 $site = get_site();
1565 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1566 }
eef868d1 1567
6e4dc10f 1568 function write_setting($data) {
1569 $record = new stdClass();
1570 $record->id = $this->id;
1571 $temp = $this->name;
1572 $record->$temp = ($data == '1' ? 1 : 0);
1573 $record->timemodified = time();
1574 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1575 }
eef868d1 1576
6e4dc10f 1577}
1578
1579class admin_setting_sitesettext extends admin_setting_configtext {
1580
1581 var $id;
1582
50999a0b 1583 function admin_setting_sitesettext($name, $visiblename, $description, $defaultsetting, $paramtype=PARAM_RAW) {
6e4dc10f 1584
1585 $this->id = SITEID;
1586 parent::admin_setting_configtext($name, $visiblename, $description, $defaultsetting, $paramtype);
eef868d1 1587
6e4dc10f 1588 }
eef868d1 1589
6e4dc10f 1590 function get_setting() {
1591 $site = get_site();
1592 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
1593 }
90cfbd0a 1594
6e4dc10f 1595 function write_setting($data) {
90cfbd0a 1596 if (is_string($this->paramtype)) {
1597 if (!$this->validate($data)) {
1598 return get_string('validateerror', 'admin') . $this->visiblename . '<br />';
1599 }
1600 } else {
1601 $data = clean_param($data, $this->paramtype);
1602 }
eef868d1 1603
6e4dc10f 1604 $record = new stdClass();
1605 $record->id = $this->id;
90cfbd0a 1606 $record->{$this->name} = $data;
6e4dc10f 1607 $record->timemodified = time();
1608 return (update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1609 }
90cfbd0a 1610
1611 function validate($data) {
1612 return preg_match($this->paramtype, $data);
1613 }
eef868d1 1614
6e4dc10f 1615}
1616
1617class admin_setting_special_frontpagedesc extends admin_setting {
1618
1619 var $id;
eef868d1 1620
6e4dc10f 1621 function admin_setting_special_frontpagedesc() {
1622 $this->id = SITEID;
1623 $name = 'summary';
1624 $visiblename = get_string('frontpagedescription');
1625 $description = get_string('frontpagedescriptionhelp');
1626 parent::admin_setting($name, $visiblename, $description, '');
1627 }
1628
1629 function output_html() {
eef868d1 1630
cc73de71 1631 if ($this->get_setting() === NULL) {
1632 $currentsetting = $this->defaultsetting;
1633 } else {
1634 $currentsetting = $this->get_setting();
1635 }
eef868d1 1636
6e4dc10f 1637 $usehtmleditor = can_use_html_editor();
eef868d1 1638
6e4dc10f 1639 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td>' .
1640 '<td>';
eef868d1 1641
6e4dc10f 1642 ob_start(); // double-check the number of columns below... might overrun some screen resolutions
1c1e7af4 1643 print_textarea($usehtmleditor, 15, 60, 0, 0, 's_' . $this->name, $currentsetting);
eef868d1 1644
6e4dc10f 1645 if ($usehtmleditor) {
1646 use_html_editor();
eef868d1 1647 }
6e4dc10f 1648 $return .= ob_get_contents();
eef868d1 1649 ob_end_clean();
6e4dc10f 1650 $return .= '</td></tr><tr><td>&nbsp;</td><td>' . $this->description . '</td></tr>';
1651 return $return;
eef868d1 1652
6e4dc10f 1653 }
eef868d1 1654
6e4dc10f 1655 function get_setting() {
eef868d1 1656
6e4dc10f 1657 $site = get_site();
1658 return (isset($site->{$this->name}) ? $site->{$this->name} : NULL);
eef868d1 1659
6e4dc10f 1660 }
eef868d1 1661
6e4dc10f 1662 function write_setting($data) {
eef868d1 1663
6e4dc10f 1664 $data = addslashes(clean_param($data, PARAM_CLEANHTML));
eef868d1 1665
6e4dc10f 1666 $record = new stdClass();
1667 $record->id = $this->id;
1668 $temp = $this->name;
1669 $record->$temp = $data;
1670 $record->timemodified = time();
eef868d1 1671
6e4dc10f 1672 return(update_record('course', $record) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
eef868d1 1673
6e4dc10f 1674 }
1675
1676}
1677
1678
1679class admin_setting_special_editorfontlist extends admin_setting {
1680
1681 var $items;
1682
1683 function admin_setting_special_editorfontlist() {
1684 global $CFG;
1685 $name = 'editorfontlist';
1686 $visiblename = get_string('editorfontlist', 'admin');
1687 $description = get_string('configeditorfontlist', 'admin');
6e4dc10f 1688 $defaults = array('k0' => 'Trebuchet',
1689 'v0' => 'Trebuchet MS,Verdana,Arial,Helvetica,sans-serif',
1690 'k1' => 'Arial',
1691 'v1' => 'arial,helvetica,sans-serif',
1692 'k2' => 'Courier New',
1693 'v2' => 'courier new,courier,monospace',
1694 'k3' => 'Georgia',
1695 'v3' => 'georgia,times new roman,times,serif',
1696 'k4' => 'Tahoma',
1697 'v4' => 'tahoma,arial,helvetica,sans-serif',
1698 'k5' => 'Times New Roman',
1699 'v5' => 'times new roman,times,serif',
1700 'k6' => 'Verdana',
1701 'v6' => 'verdana,arial,helvetica,sans-serif',
1702 'k7' => 'Impact',
1703 'v7' => 'impact',
1704 'k8' => 'Wingdings',
1705 'v8' => 'wingdings');
1706 parent::admin_setting($name, $visiblename, $description, $defaults);
1707 }
eef868d1 1708
6e4dc10f 1709 function get_setting() {
cc73de71 1710 global $CFG;
1711 if (isset($CFG->editorfontlist)) {
1712 $i = 0;
1713 $currentsetting = array();
1714 $items = explode(';', $CFG->editorfontlist);
1715 foreach ($items as $item) {
1716 $item = explode(':', $item);
1717 $currentsetting['k' . $i] = $item[0];
1718 $currentsetting['v' . $i] = $item[1];
1719 $i++;
1720 }
1721 return $currentsetting;
1722 } else {
1723 return NULL;
1724 }
6e4dc10f 1725 }
eef868d1 1726
6e4dc10f 1727 function write_setting($data) {
eef868d1 1728
6e4dc10f 1729 // there miiight be an easier way to do this :)
1730 // if this is changed, make sure the $defaults array above is modified so that this
1731 // function processes it correctly
eef868d1 1732
6e4dc10f 1733 $keys = array();
1734 $values = array();
eef868d1 1735
6e4dc10f 1736 foreach ($data as $key => $value) {
1737 if (substr($key,0,1) == 'k') {
1738 $keys[substr($key,1)] = $value;
1739 } elseif (substr($key,0,1) == 'v') {
1740 $values[substr($key,1)] = $value;
1741 }
1742 }
eef868d1 1743
6e4dc10f 1744 $result = '';
1745 for ($i = 0; $i < count($keys); $i++) {
1746 if (($keys[$i] !== '') && ($values[$i] !== '')) {
1747 $result .= clean_param($keys[$i],PARAM_NOTAGS) . ':' . clean_param($values[$i], PARAM_NOTAGS) . ';';
1748 }
1749 }
eef868d1 1750
6e4dc10f 1751 $result = substr($result, 0, -1); // trim the last semicolon
eef868d1 1752
6e4dc10f 1753 return (set_config($this->name, $result) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1754 }
eef868d1 1755
6e4dc10f 1756 function output_html() {
eef868d1 1757
cc73de71 1758 if ($this->get_setting() === NULL) {
1759 $currentsetting = $this->defaultsetting;
1760 } else {
1761 $currentsetting = $this->get_setting();
6e4dc10f 1762 }
eef868d1 1763
cc73de71 1764 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left">';
1765 for ($i = 0; $i < count($currentsetting) / 2; $i++) {
1766 $return .= '<input type="text" name="s_editorfontlist[k' . $i . ']" value="' . $currentsetting['k' . $i] . '" size="20" />';
6e4dc10f 1767 $return .= '&nbsp;&nbsp;';
cc73de71 1768 $return .= '<input type="text" name="s_editorfontlist[v' . $i . ']" value="' . $currentsetting['v' . $i] . '" size="40" /><br />';
6e4dc10f 1769 }
cc73de71 1770 $return .= '<input type="text" name="s_editorfontlist[k' . $i . ']" value="" size="20" />';
6e4dc10f 1771 $return .= '&nbsp;&nbsp;';
cc73de71 1772 $return .= '<input type="text" name="s_editorfontlist[v' . $i . ']" value="" size="40" /><br />';
1773 $return .= '<input type="text" name="s_editorfontlist[k' . ($i + 1) . ']" value="" size="20" />';
6e4dc10f 1774 $return .= '&nbsp;&nbsp;';
cc73de71 1775 $return .= '<input type="text" name="s_editorfontlist[v' . ($i + 1) . ']" value="" size="40" />';
eef868d1 1776 $return .= '</td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
6e4dc10f 1777 return $return;
1778 }
eef868d1 1779
6e4dc10f 1780}
1781
1782class admin_setting_special_editordictionary extends admin_setting_configselect {
1783
1784 function admin_setting_special_editordictionary() {
1785 $name = 'editordictionary';
1786 $visiblename = get_string('editordictionary','admin');
1787 $description = get_string('configeditordictionary', 'admin');
1788 $choices = $this->editor_get_dictionaries();
1789 if (! is_array($choices)) {
1790 $choices = array('');
1791 }
eef868d1 1792
6e4dc10f 1793 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
1794 }
1795
1796 // function borrowed from the old moodle/admin/editor.php, slightly modified
1797 function editor_get_dictionaries () {
1798 /// Get all installed dictionaries in the system
1799
1800 global $CFG;
eef868d1 1801
6e4dc10f 1802// error_reporting(E_ALL); // for debug, final version shouldn't have this...
1803 clearstatcache();
1804
1805 // If aspellpath isn't set don't even bother ;-)
1806 if (empty($CFG->aspellpath)) {
1807 return 'Empty aspell path!';
1808 }
1809
1810 // Do we have access to popen function?
1811 if (!function_exists('popen')) {
1812 return 'Popen function disabled!';
1813 }
eef868d1 1814
6e4dc10f 1815 $cmd = $CFG->aspellpath;
1816 $output = '';
1817 $dictionaries = array();
1818 $dicts = array();
1819
1820 if(!($handle = @popen(escapeshellarg($cmd) .' dump dicts', 'r'))) {
1821 return 'Couldn\'t create handle!';
1822 }
1823
1824 while(!feof($handle)) {
1825 $output .= fread($handle, 1024);
1826 }
1827 @pclose($handle);
1828
1829 $dictionaries = explode(chr(10), $output);
1830
1831 // Get rid of possible empty values
1832 if (is_array($dictionaries)) {
1833
1834 $cnt = count($dictionaries);
1835
1836 for ($i = 0; $i < $cnt; $i++) {
1837 if (!empty($dictionaries[$i])) {
1838 $dicts[] = $dictionaries[$i];
1839 }
1840 }
1841 }
1842
1843 if (count($dicts) >= 1) {
1844 return $dicts;
1845 }
1846
1847 return 'Error! Check your aspell installation!';
1848 }
1849
eef868d1 1850
6e4dc10f 1851
1852}
1853
1854
1855class admin_setting_special_editorhidebuttons extends admin_setting {
1856
1857 var $name;
1858 var $visiblename;
1859 var $description;
1860 var $items;
1861
1862 function admin_setting_special_editorhidebuttons() {
1863 $this->name = 'editorhidebuttons';
1864 $this->visiblename = get_string('editorhidebuttons', 'admin');
1865 $this->description = get_string('confeditorhidebuttons', 'admin');
1866 $this->defaultsetting = array();
1867 // weird array... buttonname => buttonimage (assume proper path appended). if you leave buttomimage blank, text will be printed instead
1868 $this->items = array('fontname' => '',
1869 'fontsize' => '',
1870 'formatblock' => '',
1871 'bold' => 'ed_format_bold.gif',
1872 'italic' => 'ed_format_italic.gif',
1873 'underline' => 'ed_format_underline.gif',
1874 'strikethrough' => 'ed_format_strike.gif',
1875 'subscript' => 'ed_format_sub.gif',
1876 'superscript' => 'ed_format_sup.gif',
1877 'copy' => 'ed_copy.gif',
1878 'cut' => 'ed_cut.gif',
1879 'paste' => 'ed_paste.gif',
1880 'clean' => 'ed_wordclean.gif',
1881 'undo' => 'ed_undo.gif',
1882 'redo' => 'ed_redo.gif',
1883 'justifyleft' => 'ed_align_left.gif',
1884 'justifycenter' => 'ed_align_center.gif',
1885 'justifyright' => 'ed_align_right.gif',
1886 'justifyfull' => 'ed_align_justify.gif',
1887 'lefttoright' => 'ed_left_to_right.gif',
1888 'righttoleft' => 'ed_right_to_left.gif',
1889 'insertorderedlist' => 'ed_list_num.gif',
1890 'insertunorderedlist' => 'ed_list_bullet.gif',
1891 'outdent' => 'ed_indent_less.gif',
1892 'indent' => 'ed_indent_more.gif',
1893 'forecolor' => 'ed_color_fg.gif',
1894 'hilitecolor' => 'ed_color_bg.gif',
1895 'inserthorizontalrule' => 'ed_hr.gif',
1896 'createanchor' => 'ed_anchor.gif',
1897 'createlink' => 'ed_link.gif',
1898 'unlink' => 'ed_unlink.gif',
1899 'insertimage' => 'ed_image.gif',
1900 'inserttable' => 'insert_table.gif',
1901 'insertsmile' => 'em.icon.smile.gif',
1902 'insertchar' => 'icon_ins_char.gif',
1903 'spellcheck' => 'spell-check.gif',
1904 'htmlmode' => 'ed_html.gif',
1905 'popupeditor' => 'fullscreen_maximize.gif',
1906 'search_replace' => 'ed_replace.gif');
1907 }
1908
1909 function get_setting() {
1910 global $CFG;
1911 return (isset($CFG->{$this->name}) ? explode(' ', $CFG->{$this->name}) : NULL);
1912 }
1913
1914 function write_setting($data) {
1915 $result = array();
1916 if (empty($data)) { $data = array(); }
1917 foreach ($data as $key => $value) {
1918 if (!in_array($key, array_keys($this->items))) {
1919 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1920 }
1921 if ($value == '1') {
1922 $result[] = $key;
1923 }
1924 }
1925 return (set_config($this->name, implode(' ',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1926 }
1927
1928 function output_html() {
eef868d1 1929
6e4dc10f 1930 global $CFG;
eef868d1 1931
6e4dc10f 1932 // checkboxes with input name="$this->name[$key]" value="1"
1933 // we do 15 fields per column
eef868d1 1934
cc73de71 1935 if ($this->get_setting() === NULL) {
1936 $currentsetting = $this->defaultsetting;
1937 } else {
1938 $currentsetting = $this->get_setting();
6e4dc10f 1939 }
eef868d1 1940
6e4dc10f 1941 $return = '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left">';
eef868d1 1942
6e4dc10f 1943 $return .= '<table><tr><td valign="top" align="right">';
eef868d1 1944
6e4dc10f 1945 $count = 0;
eef868d1 1946
6e4dc10f 1947 foreach($this->items as $key => $value) {
1948 if ($count % 15 == 0) {
1949 $return .= '</div></td><td valign="top" align="right">';
1950 }
eef868d1 1951
6e4dc10f 1952 $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;';
1953 $return .= '<input type="checkbox" value="1" name="s_' . $this->name . '[' . $key . ']"' . (in_array($key,$currentsetting) ? ' checked="checked"' : '') . ' />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
1954 $count++;
1955 if ($count % 15 != 0) {
1956 $return .= '<br /><br />';
1957 }
1958 }
eef868d1 1959
1960 $return .= '</td></tr>';
6e4dc10f 1961 $return .= '</table>';
1962 $return .= '</td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
1963
1964 return $return;
1965 }
1966
1967}
1968
1969class admin_setting_backupselect extends admin_setting_configselect {
1970
1971 function admin_setting_backupselect($name, $visiblename, $description, $default, $choices) {
1972 parent::admin_setting_configselect($name, $visiblename, $description, $default, $choices);
1973 }
1974
1975 function get_setting() {
1976 $backup_config = backup_get_config();
1977 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
1978 }
eef868d1 1979
6e4dc10f 1980 function write_setting($data) {
1981 // check that what we got was in the original choices
1982 if (! in_array($data, array_keys($this->choices))) {
1983 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
1984 }
eef868d1 1985
6e4dc10f 1986 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
1987 }
1988
1989}
1990
1991class admin_setting_special_backupsaveto extends admin_setting_configtext {
1992
1993 function admin_setting_special_backupsaveto() {
1994 $name = 'backup_sche_destination';
1995 $visiblename = get_string('saveto');
1996 $description = get_string('backupsavetohelp');
1997 parent::admin_setting_configtext($name, $visiblename, $description, '', PARAM_PATH);
1998 }
eef868d1 1999
6e4dc10f 2000 function get_setting() {
2001 $backup_config = backup_get_config();
2002 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2003 }
eef868d1 2004
6e4dc10f 2005 function write_setting($data) {
2006 $data = clean_param($data, PARAM_PATH);
2007 if (!empty($data) and (substr($data,-1) == '/' or substr($data,-1) == '\\')) {
2008 return get_string('pathslasherror') . '<br />';
2009 } else if (!empty($data) and !is_dir($data)) {
2010 return get_string('pathnotexists') . '<br />';
2011 }
2012 return (backup_set_config($this->name, $data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2013 }
2014
2015}
2016
2017class admin_setting_backupcheckbox extends admin_setting_configcheckbox {
2018
2019 function admin_setting_backupcheckbox($name, $visiblename, $description, $default) {
2020 parent::admin_setting_configcheckbox($name, $visiblename, $description, $default);
2021 }
2022
2023 function write_setting($data) {
2024 if ($data == '1') {
2025 return (backup_set_config($this->name, 1) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2026 } else {
2027 return (backup_set_config($this->name, 0) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2028 }
2029 }
eef868d1 2030
6e4dc10f 2031 function get_setting() {
2032 $backup_config = backup_get_config();
2033 return (isset($backup_config->{$this->name}) ? $backup_config->{$this->name} : NULL);
2034 }
2035
2036}
2037
2038class admin_setting_special_backuptime extends admin_setting_configtime {
2039
2040 function admin_setting_special_backuptime() {
2041 $name = 'backup_sche_hour';
2042 $name2 = 'backup_sche_minute';
2043 $visiblename = get_string('executeat');
2044 $description = get_string('backupexecuteathelp');
2045 $default = array('h' => 0, 'm' => 0);
2046 parent::admin_setting_configtime($name, $name2, $visiblename, $description, $default);
2047 }
eef868d1 2048
6e4dc10f 2049 function get_setting() {
2050 $backup_config = backup_get_config();
2051 return (isset($backup_config->{$this->name}) && isset($backup_config->{$this->name}) ? array($backup_config->{$this->name}, $backup_config->{$this->name2}) : NULL);
2052 }
eef868d1 2053
6e4dc10f 2054 function write_setting($data) {
2055 // check that what we got was in the original choices
2056 if (!(in_array($data['h'], array_keys($this->choices)) && in_array($data['m'], array_keys($this->choices2)))) {
2057 return get_string('errorsetting', 'admin') . $this->visiblename . '<br />';
2058 }
eef868d1 2059
2060 return (backup_set_config($this->name, $data['h']) && backup_set_config($this->name2, $data['m']) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2061 }
eef868d1 2062
6e4dc10f 2063}
2064
2065class admin_setting_special_backupdays extends admin_setting {
2066
2067 function admin_setting_special_backupdays() {
2068 $name = 'backup_sche_weekdays';
2069 $visiblename = get_string('schedule');
2070 $description = get_string('backupschedulehelp');
2071 parent::admin_setting($name, $visiblename, $description, array());
2072 }
eef868d1 2073
6e4dc10f 2074 function get_setting() {
2075 $backup_config = backup_get_config();
cc73de71 2076 if (isset($backup_config->{$this->name})) {
2077 $currentsetting = $backup_config->{$this->name};
2078 return array('u' => substr($currentsetting, 0, 1),
2079 'm' => substr($currentsetting, 1, 1),
2080 't' => substr($currentsetting, 2, 1),
2081 'w' => substr($currentsetting, 3, 1),
2082 'r' => substr($currentsetting, 4, 1),
2083 'f' => substr($currentsetting, 5, 1),
2084 's' => substr($currentsetting, 6, 1));
2085 } else {
2086 return NULL;
2087 }
6e4dc10f 2088 }
eef868d1 2089
6e4dc10f 2090 function output_html() {
eef868d1 2091
cc73de71 2092 if ($this->get_setting() === NULL) {
2093 $currentsetting = $this->defaultsetting;
2094 } else {
2095 $currentsetting = $this->get_setting();
6e4dc10f 2096 }
eef868d1 2097
cc73de71 2098 // rewrite for simplicity
eef868d1 2099 $currentsetting = $currentsetting['u'] . $currentsetting['m'] . $currentsetting['t'] . $currentsetting['w'] .
cc73de71 2100 $currentsetting['r'] . $currentsetting['f'] . $currentsetting['s'];
eef868d1 2101
6e4dc10f 2102 return '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left">' .
eef868d1 2103 '<table><tr><td><div align="center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
6e4dc10f 2104 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;' .
2105 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;' .
2106 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
eef868d1 2107 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[u]" value="1" ' . (substr($currentsetting,0,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2108 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[m]" value="1" ' . (substr($currentsetting,1,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2109 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[t]" value="1" ' . (substr($currentsetting,2,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2110 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[w]" value="1" ' . (substr($currentsetting,3,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2111 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[r]" value="1" ' . (substr($currentsetting,4,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2112 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[f]" value="1" ' . (substr($currentsetting,5,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2113 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[s]" value="1" ' . (substr($currentsetting,6,1) == '1' ? 'checked="checked"' : '') . ' /></div></td>' .
2114 '</tr></table>' .
6e4dc10f 2115 '</td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
eef868d1 2116
6e4dc10f 2117 }
eef868d1 2118
6e4dc10f 2119 // 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
2120 // admin_settingpage (note that admin_settingpage only calls write_setting with the data that matches $this->name... so if we have multiple form fields,
2121 // they MUST go into an array named $this->name, or else we won't receive them here
2122 function write_setting($data) {
2123 $week = 'umtwrfs';
2124 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2125 foreach($data as $key => $value) {
eef868d1 2126 if ($value == '1') {
6e4dc10f 2127 $result[strpos($week, $key)] = 1;
2128 }
2129 }
2130 return (backup_set_config($this->name, implode('',$result)) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2131 }
2132}
2133
ee437bbc 2134class admin_setting_special_debug extends admin_setting_configselect {
6e4dc10f 2135
2136 function admin_setting_special_debug() {
2137 $name = 'debug';
2138 $visiblename = get_string('debug', 'admin');
2139 $description = get_string('configdebug', 'admin');
7eb0b60a 2140 $choices = array( DEBUG_NONE => get_string('debugnone', 'admin'),
2141 DEBUG_MINIMAL => get_string('debugminimal', 'admin'),
2142 DEBUG_NORMAL => get_string('debugnormal', 'admin'),
2143 DEBUG_ALL => get_string('debugall', 'admin'),
2144 DEBUG_DEVELOPER => get_string('debugdeveloper', 'admin')
ee437bbc 2145 );
2146 parent::admin_setting_configselect($name, $visiblename, $description, '', $choices);
6e4dc10f 2147 }
2148
ee437bbc 2149 function get_setting() {
2150 global $CFG;
cc73de71 2151 if (isset($CFG->debug)) {
cc73de71 2152 return $CFG->debug;
2153 } else {
2154 return NULL;
ee437bbc 2155 }
6e4dc10f 2156 }
2157
ee437bbc 2158 function write_setting($data) {
2159 return (set_config($this->name,$data) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
6e4dc10f 2160 }
2161
2162}
2163
2164
2165class admin_setting_special_calendar_weekend extends admin_setting {
2166
2167 function admin_setting_special_calendar_weekend() {
2168 $name = 'calendar_weekend';
2169 $visiblename = get_string('calendar_weekend', 'admin');
2170 $description = get_string('helpweekenddays', 'admin');
2171 parent::admin_setting($name, $visiblename, $description, array('u' => 1, 's' => 1));
2172 }
2173
2174 function get_setting() {
2175 global $CFG;
2176 if (isset($CFG->{$this->name})) {
2177 $setting = intval($CFG->{$this->name});
2178 return array('u' => $setting & 1, 'm' => $setting & 2, 't' => $setting & 4, 'w' => $setting & 8, 'r' => $setting & 16, 'f' => $setting & 32, 's' => $setting & 64);
2179 } else {
2180 return NULL;
2181 }
2182 }
eef868d1 2183
6e4dc10f 2184 function write_setting($data) {
2185 $week = 'umtwrfs';
2186 $result = array(0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 0, 5 => 0, 6 => 0);
2187 foreach($data as $key => $value) {
eef868d1 2188 if ($value == '1') {
6e4dc10f 2189 $result[strpos($week, $key)] = 1;
2190 }
2191 }
2192 return (set_config($this->name, bindec(implode('',$result))) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2193 }
eef868d1 2194
6e4dc10f 2195 function output_html() {
2196
cc73de71 2197 if ($this->get_setting() === NULL) {
2198 $currentsetting = $this->defaultsetting;
2199 } else {
2200 $currentsetting = $this->get_setting();
6e4dc10f 2201 }
cc73de71 2202
6e4dc10f 2203 return '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td><td align="left">' .
eef868d1 2204 '<table><tr><td><div align="center">&nbsp;&nbsp;' . get_string('sunday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' .
6e4dc10f 2205 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;' .
2206 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;' .
2207 get_string('friday', 'calendar') . '&nbsp;&nbsp;</div></td><td><div align="center">&nbsp;&nbsp;' . get_string('saturday', 'calendar') . '&nbsp;&nbsp;</div></td></tr><tr>' .
eef868d1 2208 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[u]" value="1" ' . ($currentsetting['u'] ? 'checked="checked"' : '') . ' /></div></td>' .
2209 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[m]" value="1" ' . ($currentsetting['m'] ? 'checked="checked"' : '') . ' /></div></td>' .
2210 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[t]" value="1" ' . ($currentsetting['t'] ? 'checked="checked"' : '') . ' /></div></td>' .
2211 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[w]" value="1" ' . ($currentsetting['w'] ? 'checked="checked"' : '') . ' /></div></td>' .
2212 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[r]" value="1" ' . ($currentsetting['r'] ? 'checked="checked"' : '') . ' /></div></td>' .
2213 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[f]" value="1" ' . ($currentsetting['f'] ? 'checked="checked"' : '') . ' /></div></td>' .
2214 '<td><div align="center"><input type="checkbox" name="s_'. $this->name .'[s]" value="1" ' . ($currentsetting['s'] ? 'checked="checked"' : '') . ' /></div></td>' .
2215 '</tr></table>' .
6e4dc10f 2216 '</td></tr><tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
eef868d1 2217
6e4dc10f 2218 }
2219
2220}
2221
2222
2223class admin_setting_special_perfdebug extends admin_setting_configcheckbox {
2224
2225 function admin_setting_special_perfdebug() {
2226 $name = 'perfdebug';
2227 $visiblename = get_string('perfdebug', 'admin');
2228 $description = get_string('configperfdebug', 'admin');
2229 parent::admin_setting_configcheckbox($name, $visiblename, $description, '');
2230 }
2231
2232 function write_setting($data) {
2233 if ($data == '1') {
2234 return (set_config($this->name,15) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2235 } else {
2236 return (set_config($this->name,7) ? '' : get_string('errorsetting', 'admin') . $this->visiblename . '<br />');
2237 }
2238 }
2239
2240 function output_html() {
cc73de71 2241 if ($this->get_setting() === NULL) {
2242 $currentsetting = $this->defaultsetting;
2243 } else {
2244 $currentsetting = $this->get_setting();
2245 }
eef868d1 2246
6e4dc10f 2247 return '<tr><td width="100" align="right" valign="top">' . $this->visiblename . '</td>' .
cc73de71 2248 '<td align="left"><input type="checkbox" size="50" name="s_'. $this->name .'" value="1" ' . ($currentsetting == 15 ? 'checked="checked"' : '') . ' /></td></tr>' .
6e4dc10f 2249 '<tr><td>&nbsp;</td><td align="left">' . $this->description . '</td></tr>';
2250 }
2251
2252}
2253
2254// Code for a function that helps externalpages print proper headers and footers
2255// N.B.: THIS FUNCTION HANDLES AUTHENTICATION
2256function admin_externalpage_setup($section, $adminroot) {
2257
2258 global $CFG, $PAGE, $USER;
eef868d1 2259
6e4dc10f 2260 require_once($CFG->libdir . '/blocklib.php');
02cc05a7 2261 require_once($CFG->dirroot . '/'.$CFG->admin.'/pagelib.php');
eef868d1 2262
02cc05a7 2263 page_map_class(PAGE_ADMIN, 'page_admin');
6e4dc10f 2264
e9a20759 2265 $PAGE = page_create_object(PAGE_ADMIN, 0); // there must be any constant id number
6e4dc10f 2266
02cc05a7 2267 $PAGE->init_extra($section); // hack alert!
6e4dc10f 2268
2269 $root = $adminroot->locate($PAGE->section);
2270
2271 if ($site = get_site()) {
2272 require_login();
2273 } else {
2274 redirect($CFG->wwwroot . '/admin/index.php');
2275 die;
2276 }
2277
2278 if (!is_a($root, 'admin_externalpage')) {
2279 error(get_string('sectionerror','admin'));
2280 die;
2281 }
2282
2283 // this eliminates our need to authenticate on the actual pages
2284 if (!($root->check_access())) {
2285 error(get_string('accessdenied', 'admin'));
2286 die;
2287 }
eef868d1 2288
6e4dc10f 2289 $adminediting = optional_param('adminedit', -1, PARAM_BOOL);
eef868d1 2290
6e4dc10f 2291 if (!isset($USER->adminediting)) {
2292 $USER->adminediting = false;
2293 }
eef868d1 2294
6e4dc10f 2295 if ($PAGE->user_allowed_editing()) {
2296 if ($adminediting == 1) {
2297 $USER->adminediting = true;
2298 } elseif ($adminediting == 0) {
2299 $USER->adminediting = false;
2300 }
2301 }
eef868d1 2302
6e4dc10f 2303}
2304
2305function admin_externalpage_print_header($adminroot) {
2306
b1ce7811 2307 global $CFG, $PAGE, $SITE;
eef868d1 2308
b1ce7811 2309 if (!empty($SITE->fullname)) {
2310 $pageblocks = blocks_setup($PAGE);
6e4dc10f 2311
eef868d1 2312 $preferred_width_left = bounded_number(BLOCK_L_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_LEFT]),
b1ce7811 2313 BLOCK_L_MAX_WIDTH);
eef868d1 2314
b1ce7811 2315 $PAGE->print_header();
2316 echo '<table id="layout-table"><tr>';
2317 echo '<td style="width: ' . $preferred_width_left . 'px;" id="left-column">';
2318 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_LEFT);
2319 echo '</td>';
2320 echo '<td id="middle-column" width="*">';
2321 } else {
2322 print_header();
2323 }
6e4dc10f 2324
2325}
2326
2327function admin_externalpage_print_footer($adminroot) {
2328
b1ce7811 2329 global $CFG, $PAGE, $SITE;
2330
2331 if (!empty($SITE->fullname)) {
2332 $pageblocks = blocks_setup($PAGE);
2333 $preferred_width_right = bounded_number(BLOCK_R_MIN_WIDTH, blocks_preferred_width($pageblocks[BLOCK_POS_RIGHT]),
2334 BLOCK_R_MAX_WIDTH);
2335 echo '</td>';
2336 echo '<td style="width: ' . $preferred_width_right . 'px;" id="right-column">';
2337 blocks_print_group($PAGE, $pageblocks, BLOCK_POS_RIGHT);
2338 echo '</td></tr></table>';
2339 }
2340
6e4dc10f 2341 print_footer();
6e4dc10f 2342}
2343
2344function admin_get_root() {
2345 global $CFG;
2346
2347 static $ADMIN;
2348
2349 if (!isset($ADMIN)) {
2350 // start the admin tree!
2351 $ADMIN = new admin_category('root','Administration');
2352 // we process this file first to get categories up and running
50999a0b 2353 include($CFG->dirroot . '/admin/settings/top.php');
6e4dc10f 2354
2355 // now we process all other files in admin/settings to build the
2356 // admin tree
2357 foreach (glob($CFG->dirroot . '/admin/settings/*.php') as $file) {
50999a0b 2358 if ($file != $CFG->dirroot . '/admin/settings/top.php') {
6e4dc10f 2359 include_once($file);
2360 }
2361 }
2362 }
eef868d1 2363
6e4dc10f 2364 return $ADMIN;
2365}
2366
2367/// settings utiliti functions
2368
2369// n.b. this function unconditionally applies default settings
2370function apply_default_settings(&$node) {
2371
2372 global $CFG;
2373
2374 if (is_a($node, 'admin_category')) {
2375 $entries = array_keys($node->children);
2376 foreach ($entries as $entry) {
2377 apply_default_settings($node->children[$entry]);
2378 }
2379 return;
eef868d1 2380 }
6e4dc10f 2381
eef868d1 2382 if (is_a($node, 'admin_settingpage')) {
6e4dc10f 2383 foreach ($node->settings as $setting) {
2384 $CFG->{$setting->name} = $setting->defaultsetting;
2385 $setting->write_setting($setting->defaultsetting);
2386 unset($setting); // needed to prevent odd (imho) reference behaviour
2387 // see http://www.php.net/manual/en/language.references.whatdo.php#AEN6399
2388 }
2389 return;
2390 }
2391
2392 return;
2393
2394}
2395
2396// n.b. this function unconditionally applies default settings
2397function apply_default_exception_settings($defaults) {
2398
2399 global $CFG;
2400
2401 foreach($defaults as $key => $value) {
2402 $CFG->$key = $value;
2403 set_config($key, $value);
2404 }
2405
2406}
2407
2c8766df 2408function format_admin_setting($name, $title='', $form='', $description='') {
2409 $output = '<div class="adminsetting">';
2410 $output .= 'etc';
2411 $output .= '</div>';
2412
2413 return $output;
2414}
2415
9e6e7502 2416?>