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