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