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