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