Merged extension to check_browser_version from MDL-8417 (Nick)
[moodle.git] / lib / moodlelib.php
CommitLineData
ef1e97c7 1<?php // $Id$
f9903ed0 2
9fa49e22 3///////////////////////////////////////////////////////////////////////////
4// //
5// NOTICE OF COPYRIGHT //
6// //
7// Moodle - Modular Object-Oriented Dynamic Learning Environment //
abc3b857 8// http://moodle.org //
9fa49e22 9// //
abc3b857 10// Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
9fa49e22 11// //
12// This program is free software; you can redistribute it and/or modify //
13// it under the terms of the GNU General Public License as published by //
14// the Free Software Foundation; either version 2 of the License, or //
15// (at your option) any later version. //
16// //
17// This program is distributed in the hope that it will be useful, //
18// but WITHOUT ANY WARRANTY; without even the implied warranty of //
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20// GNU General Public License for more details: //
21// //
22// http://www.gnu.org/copyleft/gpl.html //
23// //
24///////////////////////////////////////////////////////////////////////////
65ccdd8c 25
7cf1c7bd 26/**
89dcb99d 27 * moodlelib.php - Moodle main library
7cf1c7bd 28 *
29 * Main library file of miscellaneous general-purpose Moodle functions.
30 * Other main libraries:
8c3dba73 31 * - weblib.php - functions that produce web output
32 * - datalib.php - functions that access the database
7cf1c7bd 33 * @author Martin Dougiamas
34 * @version $Id$
89dcb99d 35 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7cf1c7bd 36 * @package moodlecore
37 */
e1ecf0a0 38
bbd3f2c4 39/// CONSTANTS (Encased in phpdoc proper comments)/////////////////////////
f374fb10 40
6b94a807 41/**
42 * Used by some scripts to check they are being called by Moodle
43 */
44define('MOODLE_INTERNAL', true);
45
7cf1c7bd 46/**
47 * No groups used?
48 */
d8ba183c 49define('NOGROUPS', 0);
7cf1c7bd 50
51/**
52 * Groups used?
53 */
f374fb10 54define('SEPARATEGROUPS', 1);
7cf1c7bd 55
56/**
57 * Groups visible?
58 */
f374fb10 59define('VISIBLEGROUPS', 2);
60
bbd3f2c4 61/// Date and time constants ///
5602f7cf 62/**
63 * Time constant - the number of seconds in a year
64 */
65
66define('YEARSECS', 31536000);
67
7a5672c9 68/**
2f87145b 69 * Time constant - the number of seconds in a week
7a5672c9 70 */
361855e6 71define('WEEKSECS', 604800);
2f87145b 72
73/**
74 * Time constant - the number of seconds in a day
75 */
7a5672c9 76define('DAYSECS', 86400);
2f87145b 77
78/**
79 * Time constant - the number of seconds in an hour
80 */
7a5672c9 81define('HOURSECS', 3600);
2f87145b 82
83/**
84 * Time constant - the number of seconds in a minute
85 */
7a5672c9 86define('MINSECS', 60);
2f87145b 87
88/**
89 * Time constant - the number of minutes in a day
90 */
7a5672c9 91define('DAYMINS', 1440);
2f87145b 92
93/**
94 * Time constant - the number of minutes in an hour
95 */
7a5672c9 96define('HOURMINS', 60);
f9903ed0 97
c59733ef 98/// Parameter constants - every call to optional_param(), required_param() ///
99/// or clean_param() should have a specified type of parameter. //////////////
100
e0d346ff 101/**
038ba6aa 102 * PARAM_RAW specifies a parameter that is not cleaned/processed in any way;
103 * originally was 0, but changed because we need to detect unknown
104 * parameter types and swiched order in clean_param().
e0d346ff 105 */
038ba6aa 106define('PARAM_RAW', 666);
bbd3f2c4 107
108/**
c59733ef 109 * PARAM_CLEAN - obsoleted, please try to use more specific type of parameter.
110 * It was one of the first types, that is why it is abused so much ;-)
bbd3f2c4 111 */
2ae28153 112define('PARAM_CLEAN', 0x0001);
bbd3f2c4 113
114/**
c59733ef 115 * PARAM_INT - integers only, use when expecting only numbers.
bbd3f2c4 116 */
2ae28153 117define('PARAM_INT', 0x0002);
bbd3f2c4 118
119/**
120 * PARAM_INTEGER - an alias for PARAM_INT
121 */
122define('PARAM_INTEGER', 0x0002);
123
9dae915a 124/**
125 * PARAM_NUMBER - a real/floating point number.
126 */
127define('PARAM_NUMBER', 0x000a);
128
bbd3f2c4 129/**
c59733ef 130 * PARAM_ALPHA - contains only english letters.
bbd3f2c4 131 */
2ae28153 132define('PARAM_ALPHA', 0x0004);
bbd3f2c4 133
134/**
c59733ef 135 * PARAM_ACTION - an alias for PARAM_ALPHA, use for various actions in formas and urls
136 * @TODO: should we alias it to PARAM_ALPHANUM ?
bbd3f2c4 137 */
138define('PARAM_ACTION', 0x0004);
139
140/**
c59733ef 141 * PARAM_FORMAT - an alias for PARAM_ALPHA, use for names of plugins, formats, etc.
142 * @TODO: should we alias it to PARAM_ALPHANUM ?
bbd3f2c4 143 */
144define('PARAM_FORMAT', 0x0004);
145
146/**
c59733ef 147 * PARAM_NOTAGS - all html tags are stripped from the text. Do not abuse this type.
bbd3f2c4 148 */
2ae28153 149define('PARAM_NOTAGS', 0x0008);
bbd3f2c4 150
31f26796 151 /**
c4ea5e78 152 * PARAM_MULTILANG - alias of PARAM_TEXT.
31f26796 153 */
154define('PARAM_MULTILANG', 0x0009);
155
c4ea5e78 156 /**
157 * PARAM_TEXT - general plain text compatible with multilang filter, no other html tags.
158 */
159define('PARAM_TEXT', 0x0009);
160
bbd3f2c4 161/**
c59733ef 162 * PARAM_FILE - safe file name, all dangerous chars are stripped, protects against XSS, SQL injections and directory traversals
bbd3f2c4 163 */
2ae28153 164define('PARAM_FILE', 0x0010);
bbd3f2c4 165
166/**
c59733ef 167 * PARAM_PATH - safe relative path name, all dangerous chars are stripped, protects against XSS, SQL injections and directory traversals
168 * note: the leading slash is not removed, window drive letter is not allowed
bbd3f2c4 169 */
2ae28153 170define('PARAM_PATH', 0x0020);
bbd3f2c4 171
172/**
c59733ef 173 * PARAM_HOST - expected fully qualified domain name (FQDN) or an IPv4 dotted quad (IP address)
bbd3f2c4 174 */
175define('PARAM_HOST', 0x0040);
176
177/**
c59733ef 178 * PARAM_URL - expected properly formatted URL.
bbd3f2c4 179 */
2ae28153 180define('PARAM_URL', 0x0080);
bbd3f2c4 181
182/**
c59733ef 183 * PARAM_LOCALURL - expected properly formatted URL as well as one that refers to the local server itself. (NOT orthogonal to the others! Implies PARAM_URL!)
bbd3f2c4 184 */
185define('PARAM_LOCALURL', 0x0180);
186
187/**
c59733ef 188 * PARAM_CLEANFILE - safe file name, all dangerous and regional chars are removed,
189 * use when you want to store a new file submitted by students
bbd3f2c4 190 */
14d6c233 191define('PARAM_CLEANFILE',0x0200);
e0d346ff 192
8bd3fad3 193/**
c59733ef 194 * PARAM_ALPHANUM - expected numbers and letters only.
bbd3f2c4 195 */
196define('PARAM_ALPHANUM', 0x0400);
197
198/**
c59733ef 199 * PARAM_BOOL - converts input into 0 or 1, use for switches in forms and urls.
bbd3f2c4 200 */
201define('PARAM_BOOL', 0x0800);
202
203/**
c59733ef 204 * PARAM_CLEANHTML - cleans submitted HTML code and removes slashes
205 * note: do not forget to addslashes() before storing into database!
bbd3f2c4 206 */
207define('PARAM_CLEANHTML',0x1000);
208
209/**
c59733ef 210 * PARAM_ALPHAEXT the same contents as PARAM_ALPHA plus the chars in quotes: "/-_" allowed,
211 * suitable for include() and require()
212 * @TODO: should we rename this function to PARAM_SAFEDIRS??
bbd3f2c4 213 */
214define('PARAM_ALPHAEXT', 0x2000);
215
216/**
c59733ef 217 * PARAM_SAFEDIR - safe directory name, suitable for include() and require()
bbd3f2c4 218 */
219define('PARAM_SAFEDIR', 0x4000);
220
0e4af166 221/**
222 * PARAM_SEQUENCE - expects a sequence of numbers like 8 to 1,5,6,4,6,8,9. Numbers and comma only.
223 */
224define('PARAM_SEQUENCE', 0x8000);
225
03d820c7 226/**
227 * PARAM_PEM - Privacy Enhanced Mail format
228 */
229define('PARAM_PEM', 0x10000);
230
231/**
232 * PARAM_BASE64 - Base 64 encoded format
233 */
234define('PARAM_BASE64', 0x20000);
235
236
bbd3f2c4 237/// Page types ///
238/**
239 * PAGE_COURSE_VIEW is a definition of a page type. For more information on the page class see moodle/lib/pagelib.php.
8bd3fad3 240 */
241define('PAGE_COURSE_VIEW', 'course-view');
8bd3fad3 242
7eb0b60a 243/// Debug levels ///
244/** no warnings at all */
245define ('DEBUG_NONE', 0);
246/** E_ERROR | E_PARSE */
247define ('DEBUG_MINIMAL', 5);
248/** E_ERROR | E_PARSE | E_WARNING | E_NOTICE */
249define ('DEBUG_NORMAL', 15);
250/** E_ALL without E_STRICT and E_RECOVERABLE_ERROR for now */
251define ('DEBUG_ALL', 2047);
252/** DEBUG_ALL with extra Moodle debug messages - (DEBUG_ALL | 32768) */
253define ('DEBUG_DEVELOPER', 34815);
bbd3f2c4 254
feaf5d06 255/**
256 * Blog access level constant declaration
257 */
258define ('BLOG_USER_LEVEL', 1);
259define ('BLOG_GROUP_LEVEL', 2);
260define ('BLOG_COURSE_LEVEL', 3);
261define ('BLOG_SITE_LEVEL', 4);
262define ('BLOG_GLOBAL_LEVEL', 5);
263
dd0feda5 264/**
265 * Authentication - error codes for user confirm
266 */
267define('AUTH_CONFIRM_FAIL', 0);
268define('AUTH_CONFIRM_OK', 1);
269define('AUTH_CONFIRM_ALREADY', 2);
270define('AUTH_CONFIRM_ERROR', 3);
271
feaf5d06 272
03d820c7 273
9fa49e22 274/// PARAMETER HANDLING ////////////////////////////////////////////////////
6b174680 275
e0d346ff 276/**
361855e6 277 * Returns a particular value for the named variable, taken from
278 * POST or GET. If the parameter doesn't exist then an error is
e0d346ff 279 * thrown because we require this variable.
280 *
361855e6 281 * This function should be used to initialise all required values
282 * in a script that are based on parameters. Usually it will be
e0d346ff 283 * used like this:
284 * $id = required_param('id');
285 *
a083b93c 286 * @param string $parname the name of the page parameter we want
287 * @param int $type expected type of parameter
e0d346ff 288 * @return mixed
289 */
a083b93c 290function required_param($parname, $type=PARAM_CLEAN) {
e0d346ff 291
5d7a9f56 292 // detect_unchecked_vars addition
293 global $CFG;
294 if (!empty($CFG->detect_unchecked_vars)) {
295 global $UNCHECKED_VARS;
a083b93c 296 unset ($UNCHECKED_VARS->vars[$parname]);
5d7a9f56 297 }
298
a083b93c 299 if (isset($_POST[$parname])) { // POST has precedence
300 $param = $_POST[$parname];
301 } else if (isset($_GET[$parname])) {
302 $param = $_GET[$parname];
e0d346ff 303 } else {
a083b93c 304 error('A required parameter ('.$parname.') was missing');
e0d346ff 305 }
306
a083b93c 307 return clean_param($param, $type);
e0d346ff 308}
309
310/**
361855e6 311 * Returns a particular value for the named variable, taken from
e0d346ff 312 * POST or GET, otherwise returning a given default.
313 *
361855e6 314 * This function should be used to initialise all optional values
315 * in a script that are based on parameters. Usually it will be
e0d346ff 316 * used like this:
317 * $name = optional_param('name', 'Fred');
318 *
a083b93c 319 * @param string $parname the name of the page parameter we want
e0d346ff 320 * @param mixed $default the default value to return if nothing is found
a083b93c 321 * @param int $type expected type of parameter
e0d346ff 322 * @return mixed
323 */
a083b93c 324function optional_param($parname, $default=NULL, $type=PARAM_CLEAN) {
e0d346ff 325
5d7a9f56 326 // detect_unchecked_vars addition
327 global $CFG;
328 if (!empty($CFG->detect_unchecked_vars)) {
329 global $UNCHECKED_VARS;
a083b93c 330 unset ($UNCHECKED_VARS->vars[$parname]);
5d7a9f56 331 }
332
a083b93c 333 if (isset($_POST[$parname])) { // POST has precedence
334 $param = $_POST[$parname];
335 } else if (isset($_GET[$parname])) {
336 $param = $_GET[$parname];
e0d346ff 337 } else {
338 return $default;
339 }
340
a083b93c 341 return clean_param($param, $type);
e0d346ff 342}
343
344/**
361855e6 345 * Used by {@link optional_param()} and {@link required_param()} to
346 * clean the variables and/or cast to specific types, based on
e0d346ff 347 * an options field.
bbd3f2c4 348 * <code>
349 * $course->format = clean_param($course->format, PARAM_ALPHA);
350 * $selectedgrade_item = clean_param($selectedgrade_item, PARAM_CLEAN);
351 * </code>
e0d346ff 352 *
bbd3f2c4 353 * @uses $CFG
354 * @uses PARAM_CLEAN
355 * @uses PARAM_INT
356 * @uses PARAM_INTEGER
357 * @uses PARAM_ALPHA
358 * @uses PARAM_ALPHANUM
359 * @uses PARAM_NOTAGS
f4f65990 360 * @uses PARAM_ALPHAEXT
bbd3f2c4 361 * @uses PARAM_BOOL
362 * @uses PARAM_SAFEDIR
363 * @uses PARAM_CLEANFILE
364 * @uses PARAM_FILE
365 * @uses PARAM_PATH
366 * @uses PARAM_HOST
367 * @uses PARAM_URL
368 * @uses PARAM_LOCALURL
369 * @uses PARAM_CLEANHTML
0e4af166 370 * @uses PARAM_SEQUENCE
e0d346ff 371 * @param mixed $param the variable we are cleaning
a083b93c 372 * @param int $type expected format of param after cleaning.
e0d346ff 373 * @return mixed
374 */
a083b93c 375function clean_param($param, $type) {
e0d346ff 376
7744ea12 377 global $CFG;
378
80bfd470 379 if (is_array($param)) { // Let's loop
380 $newparam = array();
381 foreach ($param as $key => $value) {
a083b93c 382 $newparam[$key] = clean_param($value, $type);
80bfd470 383 }
384 return $newparam;
385 }
386
a083b93c 387 switch ($type) {
96e98ea6 388 case PARAM_RAW: // no cleaning at all
389 return $param;
390
a083b93c 391 case PARAM_CLEAN: // General HTML cleaning, try to use more specific type if possible
392 if (is_numeric($param)) {
393 return $param;
394 }
395 $param = stripslashes($param); // Needed for kses to work fine
396 $param = clean_text($param); // Sweep for scripts, etc
397 return addslashes($param); // Restore original request parameter slashes
3af57507 398
a083b93c 399 case PARAM_CLEANHTML: // prepare html fragment for display, do not store it into db!!
400 $param = stripslashes($param); // Remove any slashes
401 $param = clean_text($param); // Sweep for scripts, etc
402 return trim($param);
e0d346ff 403
a083b93c 404 case PARAM_INT:
405 return (int)$param; // Convert to integer
e0d346ff 406
9dae915a 407 case PARAM_NUMBER:
408 return (float)$param; // Convert to integer
409
a083b93c 410 case PARAM_ALPHA: // Remove everything not a-z
411 return eregi_replace('[^a-zA-Z]', '', $param);
e0d346ff 412
a083b93c 413 case PARAM_ALPHANUM: // Remove everything not a-zA-Z0-9
414 return eregi_replace('[^A-Za-z0-9]', '', $param);
f24148ef 415
a083b93c 416 case PARAM_ALPHAEXT: // Remove everything not a-zA-Z/_-
417 return eregi_replace('[^a-zA-Z/_-]', '', $param);
0ed442f8 418
0e4af166 419 case PARAM_SEQUENCE: // Remove everything not 0-9,
420 return eregi_replace('[^0-9,]', '', $param);
421
a083b93c 422 case PARAM_BOOL: // Convert to 1 or 0
423 $tempstr = strtolower($param);
eb59ac27 424 if ($tempstr == 'on' or $tempstr == 'yes' ) {
a083b93c 425 $param = 1;
eb59ac27 426 } else if ($tempstr == 'off' or $tempstr == 'no') {
a083b93c 427 $param = 0;
428 } else {
429 $param = empty($param) ? 0 : 1;
430 }
431 return $param;
f24148ef 432
a083b93c 433 case PARAM_NOTAGS: // Strip all tags
434 return strip_tags($param);
3af57507 435
c4ea5e78 436 case PARAM_TEXT: // leave only tags needed for multilang
31f26796 437 return clean_param(strip_tags($param, '<lang><span>'), PARAM_CLEAN);
438
a083b93c 439 case PARAM_SAFEDIR: // Remove everything not a-zA-Z0-9_-
440 return eregi_replace('[^a-zA-Z0-9_-]', '', $param);
95bfd207 441
a083b93c 442 case PARAM_CLEANFILE: // allow only safe characters
443 return clean_filename($param);
14d6c233 444
a083b93c 445 case PARAM_FILE: // Strip all suspicious characters from filename
446 $param = ereg_replace('[[:cntrl:]]|[<>"`\|\':\\/]', '', $param);
447 $param = ereg_replace('\.\.+', '', $param);
448 if($param == '.') {
371a2ed0 449 $param = '';
450 }
a083b93c 451 return $param;
452
453 case PARAM_PATH: // Strip all suspicious characters from file path
454 $param = str_replace('\\\'', '\'', $param);
455 $param = str_replace('\\"', '"', $param);
456 $param = str_replace('\\', '/', $param);
457 $param = ereg_replace('[[:cntrl:]]|[<>"`\|\':]', '', $param);
458 $param = ereg_replace('\.\.+', '', $param);
459 $param = ereg_replace('//+', '/', $param);
460 return ereg_replace('/(\./)+', '/', $param);
461
462 case PARAM_HOST: // allow FQDN or IPv4 dotted quad
463 preg_replace('/[^\.\d\w-]/','', $param ); // only allowed chars
464 // match ipv4 dotted quad
465 if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$param, $match)){
466 // confirm values are ok
467 if ( $match[0] > 255
468 || $match[1] > 255
469 || $match[3] > 255
470 || $match[4] > 255 ) {
471 // hmmm, what kind of dotted quad is this?
472 $param = '';
473 }
474 } elseif ( preg_match('/^[\w\d\.-]+$/', $param) // dots, hyphens, numbers
475 && !preg_match('/^[\.-]/', $param) // no leading dots/hyphens
476 && !preg_match('/[\.-]$/', $param) // no trailing dots/hyphens
477 ) {
478 // all is ok - $param is respected
479 } else {
480 // all is not ok...
481 $param='';
482 }
483 return $param;
7744ea12 484
a083b93c 485 case PARAM_URL: // allow safe ftp, http, mailto urls
486 include_once($CFG->dirroot . '/lib/validateurlsyntax.php');
5301205a 487 if (!empty($param) && validateUrlSyntax($param, 's?H?S?F?E?u-P-a?I?p?f?q?r?')) {
a083b93c 488 // all is ok, param is respected
d2a9f7cc 489 } else {
a083b93c 490 $param =''; // not really ok
491 }
492 return $param;
493
494 case PARAM_LOCALURL: // allow http absolute, root relative and relative URLs within wwwroot
495 clean_param($param, PARAM_URL);
496 if (!empty($param)) {
497 if (preg_match(':^/:', $param)) {
498 // root-relative, ok!
499 } elseif (preg_match('/^'.preg_quote($CFG->wwwroot, '/').'/i',$param)) {
500 // absolute, and matches our wwwroot
7744ea12 501 } else {
a083b93c 502 // relative - let's make sure there are no tricks
503 if (validateUrlSyntax($param, 's-u-P-a-p-f+q?r?')) {
504 // looks ok.
505 } else {
506 $param = '';
507 }
d2a9f7cc 508 }
7744ea12 509 }
a083b93c 510 return $param;
03d820c7 511 case PARAM_PEM:
512 $param = trim($param);
513 // PEM formatted strings may contain letters/numbers and the symbols
514 // forward slash: /
515 // plus sign: +
516 // equal sign: =
517 // , surrounded by BEGIN and END CERTIFICATE prefix and suffixes
518 if (preg_match('/^-----BEGIN CERTIFICATE-----([\s\w\/\+=]+)-----END CERTIFICATE-----$/', trim($param), $matches)) {
519 list($wholething, $body) = $matches;
520 unset($wholething, $matches);
521 $b64 = clean_param($body, PARAM_BASE64);
522 if (!empty($b64)) {
523 return "-----BEGIN CERTIFICATE-----\n$b64\n-----END CERTIFICATE-----\n";
524 } else {
525 return '';
526 }
527 }
528 return '';
529 case PARAM_BASE64:
530 if (!empty($param)) {
531 // PEM formatted strings may contain letters/numbers and the symbols
532 // forward slash: /
533 // plus sign: +
534 // equal sign: =
03d820c7 535 if (0 >= preg_match('/^([\s\w\/\+=]+)$/', trim($param))) {
536 return '';
537 }
538 $lines = preg_split('/[\s]+/', $param, -1, PREG_SPLIT_NO_EMPTY);
539 // Each line of base64 encoded data must be 64 characters in
540 // length, except for the last line which may be less than (or
541 // equal to) 64 characters long.
542 for ($i=0, $j=count($lines); $i < $j; $i++) {
543 if ($i + 1 == $j) {
544 if (64 < strlen($lines[$i])) {
545 return '';
546 }
547 continue;
548 }
7744ea12 549
03d820c7 550 if (64 != strlen($lines[$i])) {
551 return '';
552 }
553 }
554 return implode("\n",$lines);
555 } else {
556 return '';
557 }
a083b93c 558 default: // throw error, switched parameters in optional_param or another serious problem
559 error("Unknown parameter type: $type");
2ae28153 560 }
e0d346ff 561}
562
6b174680 563
7a530277 564
7cf1c7bd 565/**
566 * Set a key in global configuration
567 *
89dcb99d 568 * Set a key/value pair in both this session's {@link $CFG} global variable
7cf1c7bd 569 * and in the 'config' database table for future sessions.
e1ecf0a0 570 *
571 * Can also be used to update keys for plugin-scoped configs in config_plugin table.
572 * In that case it doesn't affect $CFG.
7cf1c7bd 573 *
574 * @param string $name the key to set
9cdb766d 575 * @param string $value the value to set (without magic quotes)
a4080313 576 * @param string $plugin (optional) the plugin scope
7cf1c7bd 577 * @uses $CFG
578 * @return bool
579 */
a4080313 580function set_config($name, $value, $plugin=NULL) {
9fa49e22 581/// No need for get_config because they are usually always available in $CFG
70812e39 582
42282810 583 global $CFG;
584
a4080313 585 if (empty($plugin)) {
586 $CFG->$name = $value; // So it's defined for this invocation at least
e1ecf0a0 587
a4080313 588 if (get_field('config', 'name', 'name', $name)) {
9cdb766d 589 return set_field('config', 'value', addslashes($value), 'name', $name);
a4080313 590 } else {
9cdb766d 591 $config = new object();
a4080313 592 $config->name = $name;
9cdb766d 593 $config->value = addslashes($value);
a4080313 594 return insert_record('config', $config);
595 }
596 } else { // plugin scope
597 if ($id = get_field('config_plugins', 'id', 'name', $name, 'plugin', $plugin)) {
9cdb766d 598 return set_field('config_plugins', 'value', addslashes($value), 'id', $id);
a4080313 599 } else {
9cdb766d 600 $config = new object();
601 $config->plugin = addslashes($plugin);
a4080313 602 $config->name = $name;
f855cdad 603 $config->value = addslashes($value);
a4080313 604 return insert_record('config_plugins', $config);
605 }
606 }
607}
608
609/**
e1ecf0a0 610 * Get configuration values from the global config table
a4080313 611 * or the config_plugins table.
612 *
613 * If called with no parameters it will do the right thing
614 * generating $CFG safely from the database without overwriting
e1ecf0a0 615 * existing values.
a4080313 616 *
9220fba5 617 * If called with 2 parameters it will return a $string single
618 * value or false of the value is not found.
619 *
e1ecf0a0 620 * @param string $plugin
621 * @param string $name
a4080313 622 * @uses $CFG
623 * @return hash-like object or single value
624 *
625 */
626function get_config($plugin=NULL, $name=NULL) {
7cf1c7bd 627
a4080313 628 global $CFG;
dfc9ba9b 629
a4080313 630 if (!empty($name)) { // the user is asking for a specific value
631 if (!empty($plugin)) {
9220fba5 632 return get_field('config_plugins', 'value', 'plugin' , $plugin, 'name', $name);
a4080313 633 } else {
9220fba5 634 return get_field('config', 'value', 'name', $name);
a4080313 635 }
636 }
637
638 // the user is after a recordset
639 if (!empty($plugin)) {
640 if ($configs=get_records('config_plugins', 'plugin', $plugin, '', 'name,value')) {
641 $configs = (array)$configs;
642 $localcfg = array();
643 foreach ($configs as $config) {
644 $localcfg[$config->name] = $config->value;
645 }
646 return (object)$localcfg;
647 } else {
648 return false;
649 }
d897cae4 650 } else {
a4080313 651 // this was originally in setup.php
652 if ($configs = get_records('config')) {
653 $localcfg = (array)$CFG;
654 foreach ($configs as $config) {
655 if (!isset($localcfg[$config->name])) {
656 $localcfg[$config->name] = $config->value;
657 } else {
e1ecf0a0 658 if ($localcfg[$config->name] != $config->value ) {
a4080313 659 // complain if the DB has a different
660 // value than config.php does
661 error_log("\$CFG->{$config->name} in config.php ({$localcfg[$config->name]}) overrides database setting ({$config->value})");
662 }
663 }
664 }
e1ecf0a0 665
a4080313 666 $localcfg = (object)$localcfg;
667 return $localcfg;
668 } else {
669 // preserve $CFG if DB returns nothing or error
670 return $CFG;
671 }
e1ecf0a0 672
39917a09 673 }
39917a09 674}
675
b0270f84 676/**
677 * Removes a key from global configuration
678 *
679 * @param string $name the key to set
680 * @param string $plugin (optional) the plugin scope
681 * @uses $CFG
682 * @return bool
683 */
684function unset_config($name, $plugin=NULL) {
685
686 global $CFG;
687
688 unset($CFG->$name);
689
690 if (empty($plugin)) {
691 return delete_records('config', 'name', $name);
692 } else {
693 return delete_records('config_plugins', 'name', $name, 'plugin', $plugin);
694 }
695}
696
a4080313 697
7cf1c7bd 698/**
699 * Refresh current $USER session global variable with all their current preferences.
700 * @uses $USER
701 */
70812e39 702function reload_user_preferences() {
70812e39 703
704 global $USER;
705
070e2616 706 if(empty($USER) || empty($USER->id)) {
707 return false;
708 }
709
d8ba183c 710 unset($USER->preference);
70812e39 711
712 if ($preferences = get_records('user_preferences', 'userid', $USER->id)) {
713 foreach ($preferences as $preference) {
714 $USER->preference[$preference->name] = $preference->value;
715 }
4586d60c 716 } else {
717 //return empty preference array to hold new values
718 $USER->preference = array();
c6d15803 719 }
70812e39 720}
721
7cf1c7bd 722/**
723 * Sets a preference for the current user
724 * Optionally, can set a preference for a different user object
725 * @uses $USER
68fbd8e1 726 * @todo Add a better description and include usage examples. Add inline links to $USER and user functions in above line.
727
7cf1c7bd 728 * @param string $name The key to set as preference for the specified user
729 * @param string $value The value to set forthe $name key in the specified user's record
c6d15803 730 * @param int $userid A moodle user ID
bbd3f2c4 731 * @return bool
7cf1c7bd 732 */
13af52a6 733function set_user_preference($name, $value, $otheruser=NULL) {
70812e39 734
735 global $USER;
736
13af52a6 737 if (empty($otheruser)){
738 if (!empty($USER) && !empty($USER->id)) {
070e2616 739 $userid = $USER->id;
13af52a6 740 } else {
070e2616 741 return false;
742 }
13af52a6 743 } else {
744 $userid = $otheruser;
d35757eb 745 }
746
70812e39 747 if (empty($name)) {
748 return false;
749 }
750
a3f1f815 751 if ($preference = get_record('user_preferences', 'userid', $userid, 'name', $name)) {
b0ccd3fb 752 if (set_field('user_preferences', 'value', $value, 'id', $preference->id)) {
29c7e083 753 if ($userid == $USER->id) {
070e2616 754 $USER->preference[$name] = $value;
755 }
066af654 756 return true;
757 } else {
758 return false;
759 }
70812e39 760
761 } else {
a3f1f815 762 $preference->userid = $userid;
70812e39 763 $preference->name = $name;
764 $preference->value = (string)$value;
066af654 765 if (insert_record('user_preferences', $preference)) {
29c7e083 766 if ($userid == $USER->id) {
070e2616 767 $USER->preference[$name] = $value;
768 }
70812e39 769 return true;
770 } else {
771 return false;
772 }
773 }
774}
775
6eb3e776 776/**
777 * Unsets a preference completely by deleting it from the database
778 * Optionally, can set a preference for a different user id
779 * @uses $USER
780 * @param string $name The key to unset as preference for the specified user
c6d15803 781 * @param int $userid A moodle user ID
bbd3f2c4 782 * @return bool
6eb3e776 783 */
784function unset_user_preference($name, $userid=NULL) {
785
786 global $USER;
787
361855e6 788 if (empty($userid)){
070e2616 789 if(!empty($USER) && !empty($USER->id)) {
790 $userid = $USER->id;
791 }
792 else {
793 return false;
794 }
6eb3e776 795 }
796
49d005ee 797 //Delete the preference from $USER
798 if (isset($USER->preference[$name])) {
799 unset($USER->preference[$name]);
800 }
e1ecf0a0 801
49d005ee 802 //Then from DB
6eb3e776 803 return delete_records('user_preferences', 'userid', $userid, 'name', $name);
804}
805
806
7cf1c7bd 807/**
808 * Sets a whole array of preferences for the current user
809 * @param array $prefarray An array of key/value pairs to be set
c6d15803 810 * @param int $userid A moodle user ID
bbd3f2c4 811 * @return bool
7cf1c7bd 812 */
a3f1f815 813function set_user_preferences($prefarray, $userid=NULL) {
814
815 global $USER;
70812e39 816
817 if (!is_array($prefarray) or empty($prefarray)) {
818 return false;
819 }
820
361855e6 821 if (empty($userid)){
108adee2 822 if (!empty($USER) && !empty($USER->id)) {
823 $userid = NULL; // Continue with the current user below
824 } else {
825 return false; // No-one to set!
070e2616 826 }
a3f1f815 827 }
828
70812e39 829 $return = true;
830 foreach ($prefarray as $name => $value) {
070e2616 831 // The order is important; if the test for return is done first, then
832 // if one function call fails all the remaining ones will be "optimized away"
a3f1f815 833 $return = set_user_preference($name, $value, $userid) and $return;
70812e39 834 }
835 return $return;
836}
837
7cf1c7bd 838/**
839 * If no arguments are supplied this function will return
361855e6 840 * all of the current user preferences as an array.
7cf1c7bd 841 * If a name is specified then this function
842 * attempts to return that particular preference value. If
843 * none is found, then the optional value $default is returned,
844 * otherwise NULL.
845 * @param string $name Name of the key to use in finding a preference value
846 * @param string $default Value to be returned if the $name key is not set in the user preferences
c6d15803 847 * @param int $userid A moodle user ID
7cf1c7bd 848 * @uses $USER
849 * @return string
850 */
a3f1f815 851function get_user_preferences($name=NULL, $default=NULL, $userid=NULL) {
70812e39 852
853 global $USER;
854
a3f1f815 855 if (empty($userid)) { // assume current user
856 if (empty($USER->preference)) {
857 return $default; // Default value (or NULL)
858 }
859 if (empty($name)) {
860 return $USER->preference; // Whole array
861 }
862 if (!isset($USER->preference[$name])) {
863 return $default; // Default value (or NULL)
864 }
865 return $USER->preference[$name]; // The single value
866
867 } else {
868 $preference = get_records_menu('user_preferences', 'userid', $userid, 'name', 'name,value');
869
870 if (empty($name)) {
871 return $preference;
872 }
873 if (!isset($preference[$name])) {
874 return $default; // Default value (or NULL)
875 }
876 return $preference[$name]; // The single value
70812e39 877 }
70812e39 878}
879
880
9fa49e22 881/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
39917a09 882
7cf1c7bd 883/**
c6d15803 884 * Given date parts in user time produce a GMT timestamp.
7cf1c7bd 885 *
68fbd8e1 886 * @param int $year The year part to create timestamp of
887 * @param int $month The month part to create timestamp of
888 * @param int $day The day part to create timestamp of
889 * @param int $hour The hour part to create timestamp of
890 * @param int $minute The minute part to create timestamp of
891 * @param int $second The second part to create timestamp of
892 * @param float $timezone ?
893 * @param bool $applydst ?
e34d817e 894 * @return int timestamp
7cf1c7bd 895 * @todo Finish documenting this function
896 */
9f1f6daf 897function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99, $applydst=true) {
39917a09 898
dddb014a 899 $timezone = get_user_timezone_offset($timezone);
900
94e34118 901 if (abs($timezone) > 13) {
68fbd8e1 902 $time = mktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
03c17ddf 903 } else {
68fbd8e1 904 $time = gmmktime((int)$hour, (int)$minute, (int)$second, (int)$month, (int)$day, (int)$year);
196f2619 905 $time = usertime($time, $timezone);
28c66824 906 if($applydst) {
907 $time -= dst_offset_on($time);
908 }
9f1f6daf 909 }
910
196f2619 911 return $time;
85cafb3e 912
39917a09 913}
914
7cf1c7bd 915/**
916 * Given an amount of time in seconds, returns string
5602f7cf 917 * formatted nicely as weeks, days, hours etc as needed
7cf1c7bd 918 *
2f87145b 919 * @uses MINSECS
920 * @uses HOURSECS
921 * @uses DAYSECS
5602f7cf 922 * @uses YEARSECS
c6d15803 923 * @param int $totalsecs ?
924 * @param array $str ?
89dcb99d 925 * @return string
7cf1c7bd 926 */
927 function format_time($totalsecs, $str=NULL) {
c7e3ac2a 928
6b174680 929 $totalsecs = abs($totalsecs);
c7e3ac2a 930
8dbed6be 931 if (!$str) { // Create the str structure the slow way
b0ccd3fb 932 $str->day = get_string('day');
933 $str->days = get_string('days');
934 $str->hour = get_string('hour');
935 $str->hours = get_string('hours');
936 $str->min = get_string('min');
937 $str->mins = get_string('mins');
938 $str->sec = get_string('sec');
939 $str->secs = get_string('secs');
5602f7cf 940 $str->year = get_string('year');
941 $str->years = get_string('years');
8dbed6be 942 }
943
5602f7cf 944
945 $years = floor($totalsecs/YEARSECS);
946 $remainder = $totalsecs - ($years*YEARSECS);
5602f7cf 947 $days = floor($remainder/DAYSECS);
7a5672c9 948 $remainder = $totalsecs - ($days*DAYSECS);
949 $hours = floor($remainder/HOURSECS);
950 $remainder = $remainder - ($hours*HOURSECS);
951 $mins = floor($remainder/MINSECS);
952 $secs = $remainder - ($mins*MINSECS);
8dbed6be 953
954 $ss = ($secs == 1) ? $str->sec : $str->secs;
955 $sm = ($mins == 1) ? $str->min : $str->mins;
956 $sh = ($hours == 1) ? $str->hour : $str->hours;
957 $sd = ($days == 1) ? $str->day : $str->days;
5602f7cf 958 $sy = ($years == 1) ? $str->year : $str->years;
8dbed6be 959
5602f7cf 960 $oyears = '';
b0ccd3fb 961 $odays = '';
962 $ohours = '';
963 $omins = '';
964 $osecs = '';
9c9f7d77 965
5602f7cf 966 if ($years) $oyears = $years .' '. $sy;
b0ccd3fb 967 if ($days) $odays = $days .' '. $sd;
968 if ($hours) $ohours = $hours .' '. $sh;
969 if ($mins) $omins = $mins .' '. $sm;
970 if ($secs) $osecs = $secs .' '. $ss;
6b174680 971
a59e5a1c 972 if ($years) return $oyears .' '. $odays;
b0ccd3fb 973 if ($days) return $odays .' '. $ohours;
974 if ($hours) return $ohours .' '. $omins;
975 if ($mins) return $omins .' '. $osecs;
976 if ($secs) return $osecs;
977 return get_string('now');
6b174680 978}
f9903ed0 979
7cf1c7bd 980/**
981 * Returns a formatted string that represents a date in user time
982 * <b>WARNING: note that the format is for strftime(), not date().</b>
983 * Because of a bug in most Windows time libraries, we can't use
984 * the nicer %e, so we have to use %d which has leading zeroes.
985 * A lot of the fuss in the function is just getting rid of these leading
986 * zeroes as efficiently as possible.
361855e6 987 *
8c3dba73 988 * If parameter fixday = true (default), then take off leading
7cf1c7bd 989 * zero from %d, else mantain it.
990 *
2f87145b 991 * @uses HOURSECS
e34d817e 992 * @param int $date timestamp in GMT
993 * @param string $format strftime format
d2a9f7cc 994 * @param float $timezone
bbd3f2c4 995 * @param bool $fixday If true (default) then the leading
c6d15803 996 * zero from %d is removed. If false then the leading zero is mantained.
997 * @return string
7cf1c7bd 998 */
b0ccd3fb 999function userdate($date, $format='', $timezone=99, $fixday = true) {
7a302afc 1000
1ac7ee24 1001 global $CFG;
1002
1003 static $strftimedaydatetime;
102dc313 1004
b0ccd3fb 1005 if ($format == '') {
1ac7ee24 1006 if (empty($strftimedaydatetime)) {
1007 $strftimedaydatetime = get_string('strftimedaydatetime');
1008 }
1009 $format = $strftimedaydatetime;
5fa51a39 1010 }
035cdbff 1011
c3a3c5b8 1012 if (!empty($CFG->nofixday)) { // Config.php can force %d not to be fixed.
1013 $fixday = false;
1014 } else if ($fixday) {
1015 $formatnoday = str_replace('%d', 'DD', $format);
61ae5d36 1016 $fixday = ($formatnoday != $format);
1017 }
dcde9f02 1018
88ec5b7c 1019 $date += dst_offset_on($date);
85351042 1020
494b9296 1021 $timezone = get_user_timezone_offset($timezone);
102dc313 1022
1023 if (abs($timezone) > 13) { /// Server time
d2a9f7cc 1024 if ($fixday) {
102dc313 1025 $datestring = strftime($formatnoday, $date);
1026 $daystring = str_replace(' 0', '', strftime(' %d', $date));
1027 $datestring = str_replace('DD', $daystring, $datestring);
1028 } else {
1029 $datestring = strftime($format, $date);
1030 }
88ec5b7c 1031 } else {
102dc313 1032 $date += (int)($timezone * 3600);
1033 if ($fixday) {
1034 $datestring = gmstrftime($formatnoday, $date);
1035 $daystring = str_replace(' 0', '', gmstrftime(' %d', $date));
1036 $datestring = str_replace('DD', $daystring, $datestring);
1037 } else {
1038 $datestring = gmstrftime($format, $date);
1039 }
88ec5b7c 1040 }
102dc313 1041
fb773106 1042/// If we are running under Windows convert from windows encoding to UTF-8
1043/// (because it's impossible to specify UTF-8 to fetch locale info in Win32)
11f7b25d 1044
fb773106 1045 if ($CFG->ostype == 'WINDOWS') {
11f7b25d 1046 if ($localewincharset = get_string('localewincharset')) {
1047 $textlib = textlib_get_instance();
810944af 1048 $datestring = $textlib->convert($datestring, $localewincharset, 'utf-8');
11f7b25d 1049 }
1050 }
1051
035cdbff 1052 return $datestring;
873960de 1053}
1054
7cf1c7bd 1055/**
196f2619 1056 * Given a $time timestamp in GMT (seconds since epoch),
c6d15803 1057 * returns an array that represents the date in user time
7cf1c7bd 1058 *
2f87145b 1059 * @uses HOURSECS
196f2619 1060 * @param int $time Timestamp in GMT
68fbd8e1 1061 * @param float $timezone ?
c6d15803 1062 * @return array An array that represents the date in user time
7cf1c7bd 1063 * @todo Finish documenting this function
1064 */
196f2619 1065function usergetdate($time, $timezone=99) {
6b174680 1066
494b9296 1067 $timezone = get_user_timezone_offset($timezone);
a36166d3 1068
e34d817e 1069 if (abs($timezone) > 13) { // Server time
ed1f69b0 1070 return getdate($time);
d2a9f7cc 1071 }
1072
e34d817e 1073 // There is no gmgetdate so we use gmdate instead
02f0527d 1074 $time += dst_offset_on($time);
e34d817e 1075 $time += intval((float)$timezone * HOURSECS);
3bba1e6e 1076
1077 $datestring = gmstrftime('%S_%M_%H_%d_%m_%Y_%w_%j_%A_%B', $time);
02f0527d 1078
9f1f6daf 1079 list(
1080 $getdate['seconds'],
1081 $getdate['minutes'],
1082 $getdate['hours'],
1083 $getdate['mday'],
1084 $getdate['mon'],
1085 $getdate['year'],
1086 $getdate['wday'],
1087 $getdate['yday'],
1088 $getdate['weekday'],
1089 $getdate['month']
3bba1e6e 1090 ) = explode('_', $datestring);
9f1f6daf 1091
d2d6171f 1092 return $getdate;
d552ead0 1093}
1094
7cf1c7bd 1095/**
1096 * Given a GMT timestamp (seconds since epoch), offsets it by
1097 * the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
1098 *
2f87145b 1099 * @uses HOURSECS
c6d15803 1100 * @param int $date Timestamp in GMT
e34d817e 1101 * @param float $timezone
c6d15803 1102 * @return int
7cf1c7bd 1103 */
d552ead0 1104function usertime($date, $timezone=99) {
a36166d3 1105
494b9296 1106 $timezone = get_user_timezone_offset($timezone);
2665e47a 1107
0431bd7c 1108 if (abs($timezone) > 13) {
d552ead0 1109 return $date;
1110 }
7a5672c9 1111 return $date - (int)($timezone * HOURSECS);
d552ead0 1112}
1113
8c3dba73 1114/**
1115 * Given a time, return the GMT timestamp of the most recent midnight
1116 * for the current user.
1117 *
e34d817e 1118 * @param int $date Timestamp in GMT
1119 * @param float $timezone ?
c6d15803 1120 * @return ?
8c3dba73 1121 */
edf7fe8c 1122function usergetmidnight($date, $timezone=99) {
edf7fe8c 1123
494b9296 1124 $timezone = get_user_timezone_offset($timezone);
edf7fe8c 1125 $userdate = usergetdate($date, $timezone);
4606d9bb 1126
02f0527d 1127 // Time of midnight of this user's day, in GMT
1128 return make_timestamp($userdate['year'], $userdate['mon'], $userdate['mday'], 0, 0, 0, $timezone);
edf7fe8c 1129
1130}
1131
7cf1c7bd 1132/**
1133 * Returns a string that prints the user's timezone
1134 *
1135 * @param float $timezone The user's timezone
1136 * @return string
1137 */
d552ead0 1138function usertimezone($timezone=99) {
d552ead0 1139
0c244315 1140 $tz = get_user_timezone($timezone);
f30fe8d0 1141
0c244315 1142 if (!is_float($tz)) {
1143 return $tz;
d552ead0 1144 }
0c244315 1145
1146 if(abs($tz) > 13) { // Server time
1147 return get_string('serverlocaltime');
1148 }
1149
1150 if($tz == intval($tz)) {
1151 // Don't show .0 for whole hours
1152 $tz = intval($tz);
1153 }
1154
1155 if($tz == 0) {
b0ccd3fb 1156 return 'GMT';
d552ead0 1157 }
0c244315 1158 else if($tz > 0) {
1159 return 'GMT+'.$tz;
1160 }
1161 else {
1162 return 'GMT'.$tz;
d552ead0 1163 }
e1ecf0a0 1164
f9903ed0 1165}
1166
7cf1c7bd 1167/**
1168 * Returns a float which represents the user's timezone difference from GMT in hours
1169 * Checks various settings and picks the most dominant of those which have a value
1170 *
7cf1c7bd 1171 * @uses $CFG
1172 * @uses $USER
b2b68362 1173 * @param float $tz If this value is provided and not equal to 99, it will be returned as is and no other settings will be checked
c6d15803 1174 * @return int
7cf1c7bd 1175 */
494b9296 1176function get_user_timezone_offset($tz = 99) {
f30fe8d0 1177
43b59916 1178 global $USER, $CFG;
1179
e8904995 1180 $tz = get_user_timezone($tz);
c9e55a25 1181
7b9e355e 1182 if (is_float($tz)) {
1183 return $tz;
1184 } else {
e8904995 1185 $tzrecord = get_timezone_record($tz);
7b9e355e 1186 if (empty($tzrecord)) {
e8904995 1187 return 99.0;
1188 }
4f2dbde9 1189 return (float)$tzrecord->gmtoff / HOURMINS;
e8904995 1190 }
1191}
1192
bbd3f2c4 1193/**
b2b68362 1194 * Returns a float or a string which denotes the user's timezone
1195 * A float value means that a simple offset from GMT is used, while a string (it will be the name of a timezone in the database)
1196 * means that for this timezone there are also DST rules to be taken into account
1197 * Checks various settings and picks the most dominant of those which have a value
bbd3f2c4 1198 *
1199 * @uses $USER
1200 * @uses $CFG
b2b68362 1201 * @param float $tz If this value is provided and not equal to 99, it will be returned as is and no other settings will be checked
1202 * @return mixed
bbd3f2c4 1203 */
e8904995 1204function get_user_timezone($tz = 99) {
1205 global $USER, $CFG;
43b59916 1206
f30fe8d0 1207 $timezones = array(
e8904995 1208 $tz,
1209 isset($CFG->forcetimezone) ? $CFG->forcetimezone : 99,
43b59916 1210 isset($USER->timezone) ? $USER->timezone : 99,
1211 isset($CFG->timezone) ? $CFG->timezone : 99,
f30fe8d0 1212 );
43b59916 1213
e8904995 1214 $tz = 99;
43b59916 1215
e8904995 1216 while(($tz == '' || $tz == 99) && $next = each($timezones)) {
1217 $tz = $next['value'];
43b59916 1218 }
e8904995 1219
1220 return is_numeric($tz) ? (float) $tz : $tz;
43b59916 1221}
1222
bbd3f2c4 1223/**
1224 * ?
1225 *
1226 * @uses $CFG
1227 * @uses $db
1228 * @param string $timezonename ?
1229 * @return object
1230 */
43b59916 1231function get_timezone_record($timezonename) {
1232 global $CFG, $db;
1233 static $cache = NULL;
1234
8edffd15 1235 if ($cache === NULL) {
43b59916 1236 $cache = array();
1237 }
1238
8edffd15 1239 if (isset($cache[$timezonename])) {
43b59916 1240 return $cache[$timezonename];
f30fe8d0 1241 }
1242
952d8dc8 1243 return $cache[$timezonename] = get_record_sql('SELECT * FROM '.$CFG->prefix.'timezone
1244 WHERE name = '.$db->qstr($timezonename).' ORDER BY year DESC', true);
f30fe8d0 1245}
f9903ed0 1246
bbd3f2c4 1247/**
1248 * ?
1249 *
1250 * @uses $CFG
1251 * @uses $USER
1252 * @param ? $fromyear ?
1253 * @param ? $to_year ?
1254 * @return bool
1255 */
830a2bbd 1256function calculate_user_dst_table($from_year = NULL, $to_year = NULL) {
2280ecf5 1257 global $CFG, $SESSION;
85cafb3e 1258
989585e9 1259 $usertz = get_user_timezone();
7cb29a3d 1260
989585e9 1261 if (is_float($usertz)) {
1262 // Trivial timezone, no DST
1263 return false;
1264 }
1265
2280ecf5 1266 if (!empty($SESSION->dst_offsettz) && $SESSION->dst_offsettz != $usertz) {
989585e9 1267 // We have precalculated values, but the user's effective TZ has changed in the meantime, so reset
2280ecf5 1268 unset($SESSION->dst_offsets);
1269 unset($SESSION->dst_range);
830a2bbd 1270 }
1271
2280ecf5 1272 if (!empty($SESSION->dst_offsets) && empty($from_year) && empty($to_year)) {
830a2bbd 1273 // Repeat calls which do not request specific year ranges stop here, we have already calculated the table
1274 // This will be the return path most of the time, pretty light computationally
1275 return true;
85cafb3e 1276 }
1277
830a2bbd 1278 // Reaching here means we either need to extend our table or create it from scratch
989585e9 1279
1280 // Remember which TZ we calculated these changes for
2280ecf5 1281 $SESSION->dst_offsettz = $usertz;
989585e9 1282
2280ecf5 1283 if(empty($SESSION->dst_offsets)) {
830a2bbd 1284 // If we 're creating from scratch, put the two guard elements in there
2280ecf5 1285 $SESSION->dst_offsets = array(1 => NULL, 0 => NULL);
830a2bbd 1286 }
2280ecf5 1287 if(empty($SESSION->dst_range)) {
830a2bbd 1288 // If creating from scratch
1289 $from = max((empty($from_year) ? intval(date('Y')) - 3 : $from_year), 1971);
1290 $to = min((empty($to_year) ? intval(date('Y')) + 3 : $to_year), 2035);
1291
1292 // Fill in the array with the extra years we need to process
1293 $yearstoprocess = array();
1294 for($i = $from; $i <= $to; ++$i) {
1295 $yearstoprocess[] = $i;
1296 }
1297
1298 // Take note of which years we have processed for future calls
2280ecf5 1299 $SESSION->dst_range = array($from, $to);
830a2bbd 1300 }
1301 else {
1302 // If needing to extend the table, do the same
1303 $yearstoprocess = array();
1304
2280ecf5 1305 $from = max((empty($from_year) ? $SESSION->dst_range[0] : $from_year), 1971);
1306 $to = min((empty($to_year) ? $SESSION->dst_range[1] : $to_year), 2035);
830a2bbd 1307
2280ecf5 1308 if($from < $SESSION->dst_range[0]) {
830a2bbd 1309 // Take note of which years we need to process and then note that we have processed them for future calls
2280ecf5 1310 for($i = $from; $i < $SESSION->dst_range[0]; ++$i) {
830a2bbd 1311 $yearstoprocess[] = $i;
1312 }
2280ecf5 1313 $SESSION->dst_range[0] = $from;
830a2bbd 1314 }
2280ecf5 1315 if($to > $SESSION->dst_range[1]) {
830a2bbd 1316 // Take note of which years we need to process and then note that we have processed them for future calls
2280ecf5 1317 for($i = $SESSION->dst_range[1] + 1; $i <= $to; ++$i) {
830a2bbd 1318 $yearstoprocess[] = $i;
1319 }
2280ecf5 1320 $SESSION->dst_range[1] = $to;
830a2bbd 1321 }
1322 }
1323
1324 if(empty($yearstoprocess)) {
1325 // This means that there was a call requesting a SMALLER range than we have already calculated
1326 return true;
1327 }
1328
1329 // From now on, we know that the array has at least the two guard elements, and $yearstoprocess has the years we need
1330 // Also, the array is sorted in descending timestamp order!
1331
1332 // Get DB data
989585e9 1333 $presetrecords = get_records('timezone', 'name', $usertz, 'year DESC', 'year, gmtoff, dstoff, dst_month, dst_startday, dst_weekday, dst_skipweeks, dst_time, std_month, std_startday, std_weekday, std_skipweeks, std_time');
e789650d 1334 if(empty($presetrecords)) {
1335 return false;
1336 }
57f1191c 1337
830a2bbd 1338 // Remove ending guard (first element of the array)
2280ecf5 1339 reset($SESSION->dst_offsets);
1340 unset($SESSION->dst_offsets[key($SESSION->dst_offsets)]);
830a2bbd 1341
1342 // Add all required change timestamps
1343 foreach($yearstoprocess as $y) {
1344 // Find the record which is in effect for the year $y
1345 foreach($presetrecords as $year => $preset) {
1346 if($year <= $y) {
1347 break;
c9e72798 1348 }
830a2bbd 1349 }
1350
1351 $changes = dst_changes_for_year($y, $preset);
1352
1353 if($changes === NULL) {
1354 continue;
1355 }
1356 if($changes['dst'] != 0) {
2280ecf5 1357 $SESSION->dst_offsets[$changes['dst']] = $preset->dstoff * MINSECS;
830a2bbd 1358 }
1359 if($changes['std'] != 0) {
2280ecf5 1360 $SESSION->dst_offsets[$changes['std']] = 0;
c9e72798 1361 }
85cafb3e 1362 }
42d36497 1363
830a2bbd 1364 // Put in a guard element at the top
2280ecf5 1365 $maxtimestamp = max(array_keys($SESSION->dst_offsets));
1366 $SESSION->dst_offsets[($maxtimestamp + DAYSECS)] = NULL; // DAYSECS is arbitrary, any "small" number will do
830a2bbd 1367
1368 // Sort again
2280ecf5 1369 krsort($SESSION->dst_offsets);
830a2bbd 1370
e789650d 1371 return true;
1372}
42d36497 1373
e789650d 1374function dst_changes_for_year($year, $timezone) {
7cb29a3d 1375
e789650d 1376 if($timezone->dst_startday == 0 && $timezone->dst_weekday == 0 && $timezone->std_startday == 0 && $timezone->std_weekday == 0) {
1377 return NULL;
42d36497 1378 }
7cb29a3d 1379
e789650d 1380 $monthdaydst = find_day_in_month($timezone->dst_startday, $timezone->dst_weekday, $timezone->dst_month, $year);
1381 $monthdaystd = find_day_in_month($timezone->std_startday, $timezone->std_weekday, $timezone->std_month, $year);
1382
1383 list($dst_hour, $dst_min) = explode(':', $timezone->dst_time);
1384 list($std_hour, $std_min) = explode(':', $timezone->std_time);
d2a9f7cc 1385
6dc8dddc 1386 $timedst = make_timestamp($year, $timezone->dst_month, $monthdaydst, 0, 0, 0, 99, false);
1387 $timestd = make_timestamp($year, $timezone->std_month, $monthdaystd, 0, 0, 0, 99, false);
830a2bbd 1388
1389 // Instead of putting hour and minute in make_timestamp(), we add them afterwards.
1390 // This has the advantage of being able to have negative values for hour, i.e. for timezones
1391 // where GMT time would be in the PREVIOUS day than the local one on which DST changes.
1392
1393 $timedst += $dst_hour * HOURSECS + $dst_min * MINSECS;
1394 $timestd += $std_hour * HOURSECS + $std_min * MINSECS;
42d36497 1395
e789650d 1396 return array('dst' => $timedst, 0 => $timedst, 'std' => $timestd, 1 => $timestd);
42d36497 1397}
1398
02f0527d 1399// $time must NOT be compensated at all, it has to be a pure timestamp
1400function dst_offset_on($time) {
2280ecf5 1401 global $SESSION;
02f0527d 1402
2280ecf5 1403 if(!calculate_user_dst_table() || empty($SESSION->dst_offsets)) {
c9e72798 1404 return 0;
85cafb3e 1405 }
1406
2280ecf5 1407 reset($SESSION->dst_offsets);
1408 while(list($from, $offset) = each($SESSION->dst_offsets)) {
59556d48 1409 if($from <= $time) {
c9e72798 1410 break;
1411 }
1412 }
1413
830a2bbd 1414 // This is the normal return path
1415 if($offset !== NULL) {
1416 return $offset;
02f0527d 1417 }
02f0527d 1418
830a2bbd 1419 // Reaching this point means we haven't calculated far enough, do it now:
1420 // Calculate extra DST changes if needed and recurse. The recursion always
1421 // moves toward the stopping condition, so will always end.
1422
1423 if($from == 0) {
2280ecf5 1424 // We need a year smaller than $SESSION->dst_range[0]
1425 if($SESSION->dst_range[0] == 1971) {
830a2bbd 1426 return 0;
1427 }
2280ecf5 1428 calculate_user_dst_table($SESSION->dst_range[0] - 5, NULL);
830a2bbd 1429 return dst_offset_on($time);
1430 }
1431 else {
2280ecf5 1432 // We need a year larger than $SESSION->dst_range[1]
1433 if($SESSION->dst_range[1] == 2035) {
830a2bbd 1434 return 0;
1435 }
2280ecf5 1436 calculate_user_dst_table(NULL, $SESSION->dst_range[1] + 5);
830a2bbd 1437 return dst_offset_on($time);
1438 }
85cafb3e 1439}
02f0527d 1440
28902d99 1441function find_day_in_month($startday, $weekday, $month, $year) {
8dc3f6cf 1442
1443 $daysinmonth = days_in_month($month, $year);
1444
42d36497 1445 if($weekday == -1) {
28902d99 1446 // Don't care about weekday, so return:
1447 // abs($startday) if $startday != -1
1448 // $daysinmonth otherwise
1449 return ($startday == -1) ? $daysinmonth : abs($startday);
8dc3f6cf 1450 }
1451
1452 // From now on we 're looking for a specific weekday
8dc3f6cf 1453
28902d99 1454 // Give "end of month" its actual value, since we know it
1455 if($startday == -1) {
1456 $startday = -1 * $daysinmonth;
1457 }
1458
1459 // Starting from day $startday, the sign is the direction
8dc3f6cf 1460
28902d99 1461 if($startday < 1) {
8dc3f6cf 1462
28902d99 1463 $startday = abs($startday);
8dc3f6cf 1464 $lastmonthweekday = strftime('%w', mktime(12, 0, 0, $month, $daysinmonth, $year, 0));
1465
1466 // This is the last such weekday of the month
1467 $lastinmonth = $daysinmonth + $weekday - $lastmonthweekday;
1468 if($lastinmonth > $daysinmonth) {
1469 $lastinmonth -= 7;
42d36497 1470 }
8dc3f6cf 1471
28902d99 1472 // Find the first such weekday <= $startday
1473 while($lastinmonth > $startday) {
8dc3f6cf 1474 $lastinmonth -= 7;
42d36497 1475 }
8dc3f6cf 1476
1477 return $lastinmonth;
e1ecf0a0 1478
42d36497 1479 }
1480 else {
42d36497 1481
28902d99 1482 $indexweekday = strftime('%w', mktime(12, 0, 0, $month, $startday, $year, 0));
42d36497 1483
8dc3f6cf 1484 $diff = $weekday - $indexweekday;
1485 if($diff < 0) {
1486 $diff += 7;
42d36497 1487 }
42d36497 1488
28902d99 1489 // This is the first such weekday of the month equal to or after $startday
1490 $firstfromindex = $startday + $diff;
42d36497 1491
8dc3f6cf 1492 return $firstfromindex;
1493
1494 }
42d36497 1495}
1496
bbd3f2c4 1497/**
1498 * Calculate the number of days in a given month
1499 *
1500 * @param int $month The month whose day count is sought
1501 * @param int $year The year of the month whose day count is sought
1502 * @return int
1503 */
42d36497 1504function days_in_month($month, $year) {
1505 return intval(date('t', mktime(12, 0, 0, $month, 1, $year, 0)));
1506}
1507
bbd3f2c4 1508/**
1509 * Calculate the position in the week of a specific calendar day
1510 *
1511 * @param int $day The day of the date whose position in the week is sought
1512 * @param int $month The month of the date whose position in the week is sought
1513 * @param int $year The year of the date whose position in the week is sought
1514 * @return int
1515 */
8dc3f6cf 1516function dayofweek($day, $month, $year) {
1517 // I wonder if this is any different from
1518 // strftime('%w', mktime(12, 0, 0, $month, $daysinmonth, $year, 0));
1519 return intval(date('w', mktime(12, 0, 0, $month, $day, $year, 0)));
1520}
1521
9fa49e22 1522/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
f9903ed0 1523
bbd3f2c4 1524/**
1525 * Makes sure that $USER->sesskey exists, if $USER itself exists. It sets a new sesskey
1526 * if one does not already exist, but does not overwrite existing sesskeys. Returns the
1527 * sesskey string if $USER exists, or boolean false if not.
1528 *
1529 * @uses $USER
1530 * @return string
1531 */
04280e85 1532function sesskey() {
1a33f699 1533 global $USER;
1534
1535 if(!isset($USER)) {
1536 return false;
1537 }
1538
1539 if (empty($USER->sesskey)) {
1540 $USER->sesskey = random_string(10);
1541 }
1542
1543 return $USER->sesskey;
1544}
1545
0302c52f 1546
c4d0753b 1547/**
1548 * For security purposes, this function will check that the currently
1549 * given sesskey (passed as a parameter to the script or this function)
1550 * matches that of the current user.
1551 *
1552 * @param string $sesskey optionally provided sesskey
1553 * @return bool
1554 */
1555function confirm_sesskey($sesskey=NULL) {
1556 global $USER;
0302c52f 1557
c4d0753b 1558 if (!empty($USER->ignoresesskey) || !empty($CFG->ignoresesskey)) {
1559 return true;
0302c52f 1560 }
1561
c4d0753b 1562 if (empty($sesskey)) {
1563 $sesskey = required_param('sesskey', PARAM_RAW); // Check script parameters
0302c52f 1564 }
1565
c4d0753b 1566 if (!isset($USER->sesskey)) {
1567 return false;
1568 }
0302c52f 1569
c4d0753b 1570 return ($USER->sesskey === $sesskey);
0302c52f 1571}
c4d0753b 1572
dcf6d93c 1573/**
9152fc99 1574 * Setup all global $CFG course variables, set locale and also themes
1575 * This function can be used on pages that do not require login instead of require_login()
1576 *
dcf6d93c 1577 * @param mixed $courseorid id of the course or course object
1578 */
1579function course_setup($courseorid=0) {
9152fc99 1580 global $COURSE, $CFG, $SITE, $USER;
dcf6d93c 1581
1582/// Redefine global $COURSE if needed
1583 if (empty($courseorid)) {
1584 // no change in global $COURSE - for backwards compatibiltiy
1585 // if require_rogin() used after require_login($courseid);
1586 } else if (is_object($courseorid)) {
1587 $COURSE = clone($courseorid);
1588 } else {
1589 global $course; // used here only to prevent repeated fetching from DB - may be removed later
9152fc99 1590 if (!empty($course->id) and $course->id == SITEID) {
1591 $COURSE = clone($SITE);
1592 } else if (!empty($course->id) and $course->id == $courseorid) {
dcf6d93c 1593 $COURSE = clone($course);
1594 } else {
1595 if (!$COURSE = get_record('course', 'id', $courseorid)) {
1596 error('Invalid course ID');
1597 }
1598 }
1599 }
1600
9152fc99 1601/// set locale and themes
dcf6d93c 1602 moodle_setlocale();
dcf6d93c 1603 theme_setup();
1604
dcf6d93c 1605}
c4d0753b 1606
7cf1c7bd 1607/**
ec81373f 1608 * This function checks that the current user is logged in and has the
1609 * required privileges
1610 *
7cf1c7bd 1611 * This function checks that the current user is logged in, and optionally
ec81373f 1612 * whether they are allowed to be in a particular course and view a particular
1613 * course module.
1614 * If they are not logged in, then it redirects them to the site login unless
d2a9f7cc 1615 * $autologinguest is set and {@link $CFG}->autologinguests is set to 1 in which
ec81373f 1616 * case they are automatically logged in as guests.
1617 * If $courseid is given and the user is not enrolled in that course then the
1618 * user is redirected to the course enrolment page.
1619 * If $cm is given and the coursemodule is hidden and the user is not a teacher
1620 * in the course then the user is redirected to the course home page.
7cf1c7bd 1621 *
7cf1c7bd 1622 * @uses $CFG
c6d15803 1623 * @uses $SESSION
7cf1c7bd 1624 * @uses $USER
1625 * @uses $FULLME
c6d15803 1626 * @uses SITEID
f07fa644 1627 * @uses $COURSE
33ebaf7c 1628 * @param mixed $courseorid id of the course or course object
bbd3f2c4 1629 * @param bool $autologinguest
1630 * @param object $cm course module object
7cf1c7bd 1631 */
33ebaf7c 1632function require_login($courseorid=0, $autologinguest=true, $cm=null) {
f9903ed0 1633
083c3743 1634 global $CFG, $SESSION, $USER, $COURSE, $FULLME;
d8ba183c 1635
083c3743 1636/// setup global $COURSE, themes, language and locale
dcf6d93c 1637 course_setup($courseorid);
be933850 1638
1845f8b8 1639/// If the user is not even logged in yet then make sure they are
083c3743 1640 if (!isloggedin()) {
1641 //NOTE: $USER->site check was obsoleted by session test cookie,
1642 // $USER->confirmed test is in login/index.php
f9903ed0 1643 $SESSION->wantsurl = $FULLME;
b0ccd3fb 1644 if (!empty($_SERVER['HTTP_REFERER'])) {
1645 $SESSION->fromurl = $_SERVER['HTTP_REFERER'];
9f44d972 1646 }
dcf6d93c 1647 if ($autologinguest and !empty($CFG->autologinguests) and ($COURSE->id == SITEID or $COURSE->guest) ) {
8e8d0524 1648 $loginguest = '?loginguest=true';
1649 } else {
1650 $loginguest = '';
a2ebe6a5 1651 }
083c3743 1652 if (empty($CFG->loginhttps) or $autologinguest) { //do not require https for guest logins
b0ccd3fb 1653 redirect($CFG->wwwroot .'/login/index.php'. $loginguest);
8a33e371 1654 } else {
2c3432e6 1655 $wwwroot = str_replace('http:','https:', $CFG->wwwroot);
083c3743 1656 redirect($wwwroot .'/login/index.php');
8a33e371 1657 }
20fde7b1 1658 exit;
f9903ed0 1659 }
808a3baa 1660
5602f7cf 1661/// check whether the user should be changing password (but only if it is REALLY them)
03d820c7 1662 $userauth = get_auth_plugin($USER->auth);
5602f7cf 1663 if (!empty($USER->preference['auth_forcepasswordchange']) && empty($USER->realuser)) {
03d820c7 1664 if ($userauth->can_change_password()) {
20fde7b1 1665 $SESSION->wantsurl = $FULLME;
9696bd89 1666 if (method_exists($userauth, 'change_password_url') and $userauth->change_password_url()) {
1667 //use plugin custom url
1668 redirect($userauth->change_password_url());
1437f0a5 1669 } else {
9696bd89 1670 //use moodle internal method
1671 if (empty($CFG->loginhttps)) {
1672 redirect($CFG->wwwroot .'/login/change_password.php');
1673 } else {
1674 $wwwroot = str_replace('http:','https:', $CFG->wwwroot);
1675 redirect($wwwroot .'/login/change_password.php');
1676 }
1437f0a5 1677 }
d35757eb 1678 } else {
9696bd89 1679 error(get_strin('nopasswordchangeforced', 'auth'));
d35757eb 1680 }
1681 }
083c3743 1682
1845f8b8 1683/// Check that the user account is properly set up
808a3baa 1684 if (user_not_fully_set_up($USER)) {
20fde7b1 1685 $SESSION->wantsurl = $FULLME;
b0ccd3fb 1686 redirect($CFG->wwwroot .'/user/edit.php?id='. $USER->id .'&amp;course='. SITEID);
808a3baa 1687 }
d8ba183c 1688
1845f8b8 1689/// Make sure current IP matches the one for this session (if required)
361855e6 1690 if (!empty($CFG->tracksessionip)) {
366dfa60 1691 if ($USER->sessionIP != md5(getremoteaddr())) {
1692 error(get_string('sessionipnomatch', 'error'));
1693 }
1694 }
6d8f47d6 1695
1845f8b8 1696/// Make sure the USER has a sesskey set up. Used for checking script parameters.
04280e85 1697 sesskey();
366dfa60 1698
027a1604 1699 // Check that the user has agreed to a site policy if there is one
1700 if (!empty($CFG->sitepolicy)) {
1701 if (!$USER->policyagreed) {
957b5198 1702 $SESSION->wantsurl = $FULLME;
027a1604 1703 redirect($CFG->wwwroot .'/user/policy.php');
027a1604 1704 }
1695b680 1705 }
1706
1845f8b8 1707/// If the site is currently under maintenance, then print a message
9c37662f 1708 if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM, SITEID))) {
eeefd0b0 1709 if (file_exists($CFG->dataroot.'/'.SITEID.'/maintenance.html')) {
1695b680 1710 print_maintenance_message();
20fde7b1 1711 exit;
1695b680 1712 }
027a1604 1713 }
1714
1845f8b8 1715
33ebaf7c 1716 if ($COURSE->id == SITEID) {
1717/// We can eliminate hidden site activities straight away
1718 if (!empty($cm) && !$cm->visible and !has_capability('moodle/course:viewhiddenactivities',
1719 get_context_instance(CONTEXT_SYSTEM, SITEID))) {
1720 redirect($CFG->wwwroot, get_string('activityiscurrentlyhidden'));
e3512050 1721 }
33ebaf7c 1722 return;
881a77bf 1723
33ebaf7c 1724 } else {
1725/// Check if the user can be in a particular course
1726 if (!$context = get_context_instance(CONTEXT_COURSE, $COURSE->id)) {
1845f8b8 1727 print_error('nocontext');
1728 }
1729
881a77bf 1730 if (empty($USER->switchrole[$context->id]) &&
33ebaf7c 1731 !($COURSE->visible && course_parent_visible($COURSE)) &&
1732 !has_capability('moodle/course:viewhiddencourses', get_context_instance(CONTEXT_COURSE, $COURSE->id)) ){
881a77bf 1733 print_header_simple();
1734 notice(get_string('coursehidden'), $CFG->wwwroot .'/');
1735 }
1736
f71346e2 1737 /// Non-guests who don't currently have access, check if they can be allowed in as a guest
1738
1739 if ($USER->username != 'guest' and !has_capability('moodle/course:view', $context)) {
33ebaf7c 1740 if ($COURSE->guest == 1) {
f71346e2 1741 // Temporarily assign them guest role for this context,
1742 // if it fails user is asked to enrol
1743 load_guest_role($context);
1744 }
1745 }
1746
1845f8b8 1747 /// If the user is a guest then treat them according to the course policy about guests
1748
1749 if (has_capability('moodle/legacy:guest', $context, NULL, false)) {
33ebaf7c 1750 switch ($COURSE->guest) { /// Check course policy about guest access
1845f8b8 1751
1752 case 1: /// Guests always allowed
1753 if (!has_capability('moodle/course:view', $context)) { // Prohibited by capability
1754 print_header_simple();
6ba65fa0 1755 notice(get_string('guestsnotallowed', '', format_string($COURSE->fullname)), "$CFG->wwwroot/login/index.php");
1845f8b8 1756 }
1757 if (!empty($cm) and !$cm->visible) { // Not allowed to see module, send to course page
1758 redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course,
1759 get_string('activityiscurrentlyhidden'));
1760 }
1761
1762 return; // User is allowed to see this course
1763
1764 break;
1765
b1f318a6 1766 case 2: /// Guests allowed with key
33ebaf7c 1767 if (!empty($USER->enrolkey[$COURSE->id])) { // Set by enrol/manual/enrol.php
b1f318a6 1768 return true;
1769 }
1770 // otherwise drop through to logic below (--> enrol.php)
1845f8b8 1771 break;
bbbf2d40 1772
1845f8b8 1773 default: /// Guests not allowed
21596567 1774 print_header_simple('', '', get_string('loggedinasguest'));
1775 if (empty($USER->switchrole[$context->id])) { // Normal guest
6ba65fa0 1776 notice(get_string('guestsnotallowed', '', format_string($COURSE->fullname)), "$CFG->wwwroot/login/index.php");
21596567 1777 } else {
6ba65fa0 1778 notify(get_string('guestsnotallowed', '', format_string($COURSE->fullname)));
33ebaf7c 1779 echo '<div class="notifyproblem">'.switchroles_form($COURSE->id).'</div>';
1780 print_footer($COURSE);
21596567 1781 exit;
1782 }
1845f8b8 1783 break;
1784 }
1785
1786 /// For non-guests, check if they have course view access
1787
1788 } else if (has_capability('moodle/course:view', $context)) {
1789 if (!empty($USER->realuser)) { // Make sure the REAL person can also access this course
d02eeded 1790 if (!has_capability('moodle/course:view', $context, $USER->realuser)) {
1845f8b8 1791 print_header_simple();
b0ccd3fb 1792 notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/');
cb909d74 1793 }
3ce2f1e0 1794 }
1845f8b8 1795
1796 /// Make sure they can read this activity too, if specified
1797
1798 if (!empty($cm) and !$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $context)) {
ec81373f 1799 redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
1800 }
1845f8b8 1801 return; // User is allowed to see this course
1802
da5c172a 1803 }
f9903ed0 1804
9ca3b4f3 1805
1845f8b8 1806 /// Currently not enrolled in the course, so see if they want to enrol
da5c172a 1807 $SESSION->wantsurl = $FULLME;
33ebaf7c 1808 redirect($CFG->wwwroot .'/course/enrol.php?id='. $COURSE->id);
da5c172a 1809 die;
1810 }
f9903ed0 1811}
1812
c4d0753b 1813
1814
1815/**
1816 * This function just makes sure a user is logged out.
1817 *
1818 * @uses $CFG
1819 * @uses $USER
1820 */
1821function require_logout() {
1822
2d4beaff 1823 global $USER, $CFG, $SESSION;
c4d0753b 1824
1825 if (isset($USER) and isset($USER->id)) {
1826 add_to_log(SITEID, "user", "logout", "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
1827
1828 if ($USER->auth == 'cas' && !empty($CFG->cas_enabled)) {
1829 require($CFG->dirroot.'/auth/cas/logout.php');
1830 }
03d820c7 1831
81693ac7 1832 if (extension_loaded('openssl')) {
1833 require($CFG->dirroot.'/auth/mnet/auth.php');
1834 $authplugin = new auth_plugin_mnet();
1835 $authplugin->logout();
1836 }
c4d0753b 1837 }
1838
1839 if (ini_get_bool("register_globals") and check_php_version("4.3.0")) {
1840 // This method is just to try to avoid silly warnings from PHP 4.3.0
1841 session_unregister("USER");
1842 session_unregister("SESSION");
1843 }
1844
1845 setcookie('MoodleSessionTest'.$CFG->sessioncookie, '', time() - 3600, $CFG->sessioncookiepath);
1846 unset($_SESSION['USER']);
1847 unset($_SESSION['SESSION']);
1848
1849 unset($SESSION);
1850 unset($USER);
1851
1852}
1853
7cf1c7bd 1854/**
1855 * This is a weaker version of {@link require_login()} which only requires login
1856 * when called from within a course rather than the site page, unless
1857 * the forcelogin option is turned on.
1858 *
1859 * @uses $CFG
33ebaf7c 1860 * @param mixed $courseorid The course object or id in question
bbd3f2c4 1861 * @param bool $autologinguest Allow autologin guests if that is wanted
4febb58f 1862 * @param object $cm Course activity module if known
7cf1c7bd 1863 */
33ebaf7c 1864function require_course_login($courseorid, $autologinguest=true, $cm=null) {
f950af3c 1865 global $CFG;
1596edff 1866 if (!empty($CFG->forcelogin)) {
33ebaf7c 1867 // login required for both SITE and courses
1868 require_login($courseorid, $autologinguest, $cm);
39de90ac 1869 } else if ((is_object($courseorid) and $courseorid->id == SITEID)
1870 or (!is_object($courseorid) and $courseorid == SITEID)) {
33ebaf7c 1871 //login for SITE not required
1872 } else {
1873 // course login always required
1874 require_login($courseorid, $autologinguest, $cm);
f950af3c 1875 }
1876}
1877
7cf1c7bd 1878/**
1879 * Modify the user table by setting the currently logged in user's
1880 * last login to now.
1881 *
1882 * @uses $USER
bbd3f2c4 1883 * @return bool
7cf1c7bd 1884 */
1d881d92 1885function update_user_login_times() {
1886 global $USER;
1887
53467aa6 1888 $user = new object();
1d881d92 1889 $USER->lastlogin = $user->lastlogin = $USER->currentlogin;
2a2f5f11 1890 $USER->currentlogin = $user->lastaccess = $user->currentlogin = time();
1d881d92 1891
1892 $user->id = $USER->id;
1893
b0ccd3fb 1894 return update_record('user', $user);
1d881d92 1895}
1896
7cf1c7bd 1897/**
1898 * Determines if a user has completed setting up their account.
1899 *
89dcb99d 1900 * @param user $user A {@link $USER} object to test for the existance of a valid name and email
bbd3f2c4 1901 * @return bool
7cf1c7bd 1902 */
808a3baa 1903function user_not_fully_set_up($user) {
bb64b51a 1904 return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email) or over_bounce_threshold($user)));
1905}
1906
1907function over_bounce_threshold($user) {
d2a9f7cc 1908
bb64b51a 1909 global $CFG;
d2a9f7cc 1910
bb64b51a 1911 if (empty($CFG->handlebounces)) {
1912 return false;
1913 }
1914 // set sensible defaults
1915 if (empty($CFG->minbounces)) {
1916 $CFG->minbounces = 10;
1917 }
1918 if (empty($CFG->bounceratio)) {
1919 $CFG->bounceratio = .20;
1920 }
1921 $bouncecount = 0;
1922 $sendcount = 0;
1923 if ($bounce = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
1924 $bouncecount = $bounce->value;
1925 }
1926 if ($send = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
1927 $sendcount = $send->value;
1928 }
1929 return ($bouncecount >= $CFG->minbounces && $bouncecount/$sendcount >= $CFG->bounceratio);
1930}
1931
d2a9f7cc 1932/**
bb64b51a 1933 * @param $user - object containing an id
1934 * @param $reset - will reset the count to 0
1935 */
1936function set_send_count($user,$reset=false) {
d2a9f7cc 1937 if ($pref = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
bb64b51a 1938 $pref->value = (!empty($reset)) ? 0 : $pref->value+1;
1939 update_record('user_preferences',$pref);
1940 }
1941 else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
1942 // make a new one
1943 $pref->name = 'email_send_count';
1944 $pref->value = 1;
1945 $pref->userid = $user->id;
06ba0b04 1946 insert_record('user_preferences',$pref, false);
bb64b51a 1947 }
1948}
1949
d2a9f7cc 1950/**
bb64b51a 1951* @param $user - object containing an id
1952 * @param $reset - will reset the count to 0
1953 */
1954function set_bounce_count($user,$reset=false) {
d2a9f7cc 1955 if ($pref = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
bb64b51a 1956 $pref->value = (!empty($reset)) ? 0 : $pref->value+1;
1957 update_record('user_preferences',$pref);
1958 }
1959 else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
1960 // make a new one
1961 $pref->name = 'email_bounce_count';
1962 $pref->value = 1;
1963 $pref->userid = $user->id;
06ba0b04 1964 insert_record('user_preferences',$pref, false);
bb64b51a 1965 }
808a3baa 1966}
f9903ed0 1967
7cf1c7bd 1968/**
1969 * Keeps track of login attempts
1970 *
1971 * @uses $SESSION
1972 */
f9903ed0 1973function update_login_count() {
9fa49e22 1974
f9903ed0 1975 global $SESSION;
1976
1977 $max_logins = 10;
1978
1979 if (empty($SESSION->logincount)) {
1980 $SESSION->logincount = 1;
1981 } else {
1982 $SESSION->logincount++;
1983 }
1984
1985 if ($SESSION->logincount > $max_logins) {
9fa49e22 1986 unset($SESSION->wantsurl);
b0ccd3fb 1987 error(get_string('errortoomanylogins'));
d578afc8 1988 }
1989}
1990
7cf1c7bd 1991/**
1992 * Resets login attempts
1993 *
1994 * @uses $SESSION
1995 */
9fa49e22 1996function reset_login_count() {
9fa49e22 1997 global $SESSION;
d578afc8 1998
9fa49e22 1999 $SESSION->logincount = 0;
d578afc8 2000}
2001
b61efafb 2002function sync_metacourses() {
2003
2004 global $CFG;
2005
1aad4310 2006 if (!$courses = get_records('course', 'metacourse', 1)) {
b61efafb 2007 return;
2008 }
d2a9f7cc 2009
b61efafb 2010 foreach ($courses as $course) {
1aad4310 2011 sync_metacourse($course);
b61efafb 2012 }
2013}
2014
b61efafb 2015/**
2016 * Goes through all enrolment records for the courses inside the metacourse and sync with them.
123545bc 2017 *
2018 * @param mixed $course the metacourse to synch. Either the course object itself, or the courseid.
d2a9f7cc 2019 */
1aad4310 2020function sync_metacourse($course) {
755c8d58 2021 global $CFG;
b61efafb 2022
123545bc 2023 // Check the course is valid.
1aad4310 2024 if (!is_object($course)) {
2025 if (!$course = get_record('course', 'id', $course)) {
2026 return false; // invalid course id
b61efafb 2027 }
b61efafb 2028 }
1aad4310 2029
123545bc 2030 // Check that we actually have a metacourse.
1aad4310 2031 if (empty($course->metacourse)) {
123545bc 2032 return false;
755c8d58 2033 }
87671466 2034
b3170072 2035 // Get a list of roles that should not be synced.
2036 if ($CFG->nonmetacoursesyncroleids) {
2037 $roleexclusions = 'ra.roleid NOT IN (' . $CFG->nonmetacoursesyncroleids . ') AND';
2038 } else {
2039 $roleexclusions = '';
2040 }
2041
123545bc 2042 // Get the context of the metacourse.
1aad4310 2043 $context = get_context_instance(CONTEXT_COURSE, $course->id); // SITEID can not be a metacourse
e1ecf0a0 2044
123545bc 2045 // We do not ever want to unassign the list of metacourse manager, so get a list of them.
b79da3ac 2046 if ($users = get_users_by_capability($context, 'moodle/course:managemetacourse')) {
1aad4310 2047 $managers = array_keys($users);
2048 } else {
2049 $managers = array();
b61efafb 2050 }
2051
123545bc 2052 // Get assignments of a user to a role that exist in a child course, but
2053 // not in the meta coure. That is, get a list of the assignments that need to be made.
2054 if (!$assignments = get_records_sql("
2055 SELECT
2056 ra.id, ra.roleid, ra.userid
2057 FROM
2058 {$CFG->prefix}role_assignments ra,
2059 {$CFG->prefix}context con,
2060 {$CFG->prefix}course_meta cm
2061 WHERE
2062 ra.contextid = con.id AND
2063 con.contextlevel = " . CONTEXT_COURSE . " AND
2064 con.instanceid = cm.child_course AND
2065 cm.parent_course = {$course->id} AND
b3170072 2066 $roleexclusions
123545bc 2067 NOT EXISTS (
2068 SELECT 1 FROM
2069 {$CFG->prefix}role_assignments ra2
2070 WHERE
2071 ra2.userid = ra.userid AND
2072 ra2.roleid = ra.roleid AND
2073 ra2.contextid = {$context->id}
2074 )
2075 ")) {
2076 $assignments = array();
2077 }
2078
2079 // Get assignments of a user to a role that exist in the meta course, but
2080 // not in any child courses. That is, get a list of the unassignments that need to be made.
2081 if (!$unassignments = get_records_sql("
2082 SELECT
2083 ra.id, ra.roleid, ra.userid
2084 FROM
2085 {$CFG->prefix}role_assignments ra
2086 WHERE
2087 ra.contextid = {$context->id} AND
b3170072 2088 $roleexclusions
123545bc 2089 NOT EXISTS (
2090 SELECT 1 FROM
2091 {$CFG->prefix}role_assignments ra2,
2092 {$CFG->prefix}context con2,
2093 {$CFG->prefix}course_meta cm
2094 WHERE
2095 ra2.userid = ra.userid AND
2096 ra2.roleid = ra.roleid AND
2097 ra2.contextid = con2.id AND
2098 con2.contextlevel = " . CONTEXT_COURSE . " AND
2099 con2.instanceid = cm.child_course AND
2100 cm.parent_course = {$course->id}
2101 )
2102 ")) {
2103 $unassignments = array();
2104 }
2105
2106 $success = true;
2107
2108 // Make the unassignments, if they are not managers.
2109 foreach ($unassignments as $unassignment) {
2110 if (!in_array($unassignment->userid, $managers)) {
2111 $success = role_unassign($unassignment->roleid, $unassignment->userid, 0, $context->id) && $success;
1aad4310 2112 }
755c8d58 2113 }
e1ecf0a0 2114
123545bc 2115 // Make the assignments.
2116 foreach ($assignments as $assignment) {
2117 $success = role_assign($assignment->roleid, $assignment->userid, 0, $context->id) && $success;
b61efafb 2118 }
755c8d58 2119
123545bc 2120 return $success;
2121
1aad4310 2122// TODO: finish timeend and timestart
2123// maybe we could rely on cron job to do the cleaning from time to time
b61efafb 2124}
2125
d2a9f7cc 2126/**
b61efafb 2127 * Adds a record to the metacourse table and calls sync_metacoures
2128 */
2129function add_to_metacourse ($metacourseid, $courseid) {
d2a9f7cc 2130
b61efafb 2131 if (!$metacourse = get_record("course","id",$metacourseid)) {
2132 return false;
2133 }
d2a9f7cc 2134
b61efafb 2135 if (!$course = get_record("course","id",$courseid)) {
2136 return false;
2137 }
2138
5f37b628 2139 if (!$record = get_record("course_meta","parent_course",$metacourseid,"child_course",$courseid)) {
53467aa6 2140 $rec = new object();
b61efafb 2141 $rec->parent_course = $metacourseid;
2142 $rec->child_course = $courseid;
5f37b628 2143 if (!insert_record('course_meta',$rec)) {
b61efafb 2144 return false;
2145 }
2146 return sync_metacourse($metacourseid);
2147 }
2148 return true;
d2a9f7cc 2149
b61efafb 2150}
2151
d2a9f7cc 2152/**
b61efafb 2153 * Removes the record from the metacourse table and calls sync_metacourse
2154 */
2155function remove_from_metacourse($metacourseid, $courseid) {
2156
5f37b628 2157 if (delete_records('course_meta','parent_course',$metacourseid,'child_course',$courseid)) {
b61efafb 2158 return sync_metacourse($metacourseid);
2159 }
2160 return false;
2161}
2162
2163
7c12949d 2164/**
2165 * Determines if a user is currently logged in
2166 *
2167 * @uses $USER
bbd3f2c4 2168 * @return bool
7c12949d 2169 */
2170function isloggedin() {
2171 global $USER;
2172
2173 return (!empty($USER->id));
2174}
2175
2a919fd7 2176/**
2177 * Determines if a user is logged in as real guest user with username 'guest'.
2178 * This function is similar to original isguest() in 1.6 and earlier.
2179 * Current isguest() is deprecated - do not use it anymore.
2180 *
2181 * @param $user mixed user object or id, $USER if not specified
2182 * @return bool true if user is the real guest user, false if not logged in or other user
2183 */
2184function isguestuser($user=NULL) {
2185 global $USER;
2186 if ($user === NULL) {
2187 $user = $USER;
2188 } else if (is_numeric($user)) {
2189 $user = get_record('user', 'id', $user, '', '', '', '', 'id, username');
2190 }
2191
2192 if (empty($user->id)) {
2193 return false; // not logged in, can not be guest
2194 }
2195
2196 return ($user->username == 'guest');
2197}
7c12949d 2198
7cf1c7bd 2199/**
2200 * Determines if the currently logged in user is in editing mode
2201 *
2202 * @uses $USER
c6d15803 2203 * @param int $courseid The id of the course being tested
89dcb99d 2204 * @param user $user A {@link $USER} object. If null then the currently logged in user is used.
bbd3f2c4 2205 * @return bool
7cf1c7bd 2206 */
2c309dc2 2207function isediting($courseid, $user=NULL) {
3cf4fa0c 2208 global $USER;
2209 if (!$user) {
2210 $user = $USER;
2c309dc2 2211 }
9c9f7d77 2212 if (empty($user->editing)) {
2213 return false;
2214 }
217a8ee9 2215
2216 $capcheck = false;
2217 $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
2218
2219 if (has_capability('moodle/course:manageactivities', $coursecontext) ||
2220 has_capability('moodle/site:manageblocks', $coursecontext)) {
2221 $capcheck = true;
2222 } else {
2223 // loop through all child context, see if user has moodle/course:manageactivities or moodle/site:manageblocks
2224 if ($children = get_child_contexts($coursecontext)) {
2225 foreach ($children as $child) {
2226 $childcontext = get_record('context', 'id', $child);
2227 if (has_capability('moodle/course:manageactivities', $childcontext) ||
2228 has_capability('moodle/site:manageblocks', $childcontext)) {
2229 $capcheck = true;
2230 break;
2231 }
2232 }
2233 }
2234 }
2235
2236 return ($user->editing && $capcheck);
2237 //return ($user->editing and has_capability('moodle/course:manageactivities', get_context_instance(CONTEXT_COURSE, $courseid)));
2c309dc2 2238}
2239
7cf1c7bd 2240/**
2241 * Determines if the logged in user is currently moving an activity
2242 *
2243 * @uses $USER
c6d15803 2244 * @param int $courseid The id of the course being tested
bbd3f2c4 2245 * @return bool
7cf1c7bd 2246 */
7977cffd 2247function ismoving($courseid) {
7977cffd 2248 global $USER;
2249
2250 if (!empty($USER->activitycopy)) {
2251 return ($USER->activitycopycourse == $courseid);
2252 }
2253 return false;
2254}
2255
7cf1c7bd 2256/**
2257 * Given an object containing firstname and lastname
2258 * values, this function returns a string with the
2259 * full name of the person.
2260 * The result may depend on system settings
2261 * or language. 'override' will force both names
361855e6 2262 * to be used even if system settings specify one.
68fbd8e1 2263 *
7cf1c7bd 2264 * @uses $CFG
2265 * @uses $SESSION
68fbd8e1 2266 * @param object $user A {@link $USER} object to get full name of
2267 * @param bool $override If true then the name will be first name followed by last name rather than adhering to fullnamedisplay setting.
7cf1c7bd 2268 */
e2cd5065 2269function fullname($user, $override=false) {
b5cbb64d 2270
f374fb10 2271 global $CFG, $SESSION;
2272
6527c077 2273 if (!isset($user->firstname) and !isset($user->lastname)) {
2274 return '';
2275 }
2276
4c202228 2277 if (!$override) {
2278 if (!empty($CFG->forcefirstname)) {
2279 $user->firstname = $CFG->forcefirstname;
2280 }
2281 if (!empty($CFG->forcelastname)) {
2282 $user->lastname = $CFG->forcelastname;
2283 }
2284 }
2285
f374fb10 2286 if (!empty($SESSION->fullnamedisplay)) {
2287 $CFG->fullnamedisplay = $SESSION->fullnamedisplay;
2288 }
e2cd5065 2289
b5cbb64d 2290 if ($CFG->fullnamedisplay == 'firstname lastname') {
b0ccd3fb 2291 return $user->firstname .' '. $user->lastname;
b5cbb64d 2292
2293 } else if ($CFG->fullnamedisplay == 'lastname firstname') {
b0ccd3fb 2294 return $user->lastname .' '. $user->firstname;
e2cd5065 2295
b5cbb64d 2296 } else if ($CFG->fullnamedisplay == 'firstname') {
2297 if ($override) {
2298 return get_string('fullnamedisplay', '', $user);
2299 } else {
2300 return $user->firstname;
2301 }
2302 }
e2cd5065 2303
b5cbb64d 2304 return get_string('fullnamedisplay', '', $user);
e2cd5065 2305}
2306
7cf1c7bd 2307/**
2308 * Sets a moodle cookie with an encrypted string
2309 *
2310 * @uses $CFG
2f87145b 2311 * @uses DAYSECS
2312 * @uses HOURSECS
7cf1c7bd 2313 * @param string $thing The string to encrypt and place in a cookie
2314 */
f9903ed0 2315function set_moodle_cookie($thing) {
7185e073 2316 global $CFG;
482b6e6e 2317
7cbe6afe 2318 if ($thing == 'guest') { // Ignore guest account
2319 return;
2320 }
2321
482b6e6e 2322 $cookiename = 'MOODLEID_'.$CFG->sessioncookie;
f9903ed0 2323
2324 $days = 60;
7a5672c9 2325 $seconds = DAYSECS*$days;
f9903ed0 2326
7a5672c9 2327 setCookie($cookiename, '', time() - HOURSECS, '/');
b0ccd3fb 2328 setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/');
f9903ed0 2329}
2330
7cf1c7bd 2331/**
2332 * Gets a moodle cookie with an encrypted string
2333 *
2334 * @uses $CFG
2335 * @return string
2336 */
f9903ed0 2337function get_moodle_cookie() {
7185e073 2338 global $CFG;
2339
482b6e6e 2340 $cookiename = 'MOODLEID_'.$CFG->sessioncookie;
7185e073 2341
1079c8a8 2342 if (empty($_COOKIE[$cookiename])) {
b0ccd3fb 2343 return '';
1079c8a8 2344 } else {
7cbe6afe 2345 $thing = rc4decrypt($_COOKIE[$cookiename]);
2346 return ($thing == 'guest') ? '': $thing; // Ignore guest account
1079c8a8 2347 }
f9903ed0 2348}
2349
7cf1c7bd 2350/**
03d820c7 2351 * Returns whether a given authentication plugin exists.
7cf1c7bd 2352 *
2353 * @uses $CFG
03d820c7 2354 * @param string $auth Form of authentication to check for. Defaults to the
2355 * global setting in {@link $CFG}.
2356 * @return boolean Whether the plugin is available.
7cf1c7bd 2357 */
16793340 2358function exists_auth_plugin($auth) {
03d820c7 2359 global $CFG;
2360
03d820c7 2361 if (file_exists("{$CFG->dirroot}/auth/$auth/auth.php")) {
2362 return is_readable("{$CFG->dirroot}/auth/$auth/auth.php");
2363 }
2364 return false;
2365}
ba7166c3 2366
03d820c7 2367/**
2368 * Checks if a given plugin is in the list of enabled authentication plugins.
2369 *
2370 * @param string $auth Authentication plugin.
2371 * @return boolean Whether the plugin is enabled.
2372 */
16793340 2373function is_enabled_auth($auth) {
ba7166c3 2374 global $CFG;
2375
16793340 2376 if (empty($auth)) {
2377 return false;
2378 } else if ($auth == 'manual') {
2379 return true;
03d820c7 2380 }
16793340 2381
2382 return in_array($auth, explode(',', $CFG->auth));
03d820c7 2383}
2384
2385/**
2386 * Returns an authentication plugin instance.
2387 *
2388 * @uses $CFG
9696bd89 2389 * @param string $auth name of authentication plugin
03d820c7 2390 * @return object An instance of the required authentication plugin.
2391 */
9696bd89 2392function get_auth_plugin($auth) {
03d820c7 2393 global $CFG;
2394
03d820c7 2395 // check the plugin exists first
2396 if (! exists_auth_plugin($auth)) {
2397 error("Authentication plugin '$auth' not found.");
2398 }
2399
2400 // return auth plugin instance
2401 require_once "{$CFG->dirroot}/auth/$auth/auth.php";
2402 $class = "auth_plugin_$auth";
2403 return new $class;
2404}
2405
2406/**
2407 * Returns true if an internal authentication method is being used.
2408 * if method not specified then, global default is assumed
2409 *
2410 * @uses $CFG
2411 * @param string $auth Form of authentication required
2412 * @return bool
03d820c7 2413 */
16793340 2414function is_internal_auth($auth) {
03d820c7 2415 $authplugin = get_auth_plugin($auth); // throws error if bad $auth
2416 return $authplugin->is_internal();
a3f1f815 2417}
2418
8c3dba73 2419/**
2420 * Returns an array of user fields
2421 *
c6d15803 2422 * @uses $CFG
2423 * @uses $db
2424 * @return array User field/column names
8c3dba73 2425 */
a3f1f815 2426function get_user_fieldnames() {
a3f1f815 2427
2428 global $CFG, $db;
2429
2430 $fieldarray = $db->MetaColumnNames($CFG->prefix.'user');
2431 unset($fieldarray['ID']);
2432
2433 return $fieldarray;
ba7166c3 2434}
f9903ed0 2435
7cf1c7bd 2436/**
2437 * Creates a bare-bones user record
2438 *
2439 * @uses $CFG
7cf1c7bd 2440 * @param string $username New user's username to add to record
2441 * @param string $password New user's password to add to record
2442 * @param string $auth Form of authentication required
68fbd8e1 2443 * @return object A {@link $USER} object
7cf1c7bd 2444 * @todo Outline auth types and provide code example
2445 */
71f9abf9 2446function create_user_record($username, $password, $auth='') {
366dfa60 2447 global $CFG;
71f9abf9 2448
1e22bc9c 2449 //just in case check text case
2450 $username = trim(moodle_strtolower($username));
71f9abf9 2451
03d820c7 2452 $authplugin = get_auth_plugin($auth);
2453
2454 if (method_exists($authplugin, 'get_userinfo')) {
2455 if ($newinfo = $authplugin->get_userinfo($username)) {
b36a8fc4 2456 $newinfo = truncate_userinfo($newinfo);
34daec9b 2457 foreach ($newinfo as $key => $value){
16793340 2458 $newuser->$key = addslashes($value);
e858f9da 2459 }
2460 }
2461 }
f9903ed0 2462
85a1d4c9 2463 if (!empty($newuser->email)) {
2464 if (email_is_not_allowed($newuser->email)) {
2465 unset($newuser->email);
2466 }
2467 }
2468
16793340 2469 $newuser->auth = (empty($auth)) ? 'manual' : $auth;
faebaf0f 2470 $newuser->username = $username;
e51917eb 2471
2472 // fix for MDL-8480
2473 // user CFG lang for user if $newuser->lang is empty
2474 // or $user->lang is not an installed language
2475 $sitelangs = array_keys(get_list_of_languages());
2476 if (empty($newuser->lang) || !in_array($newuser->lang, $sitelangs)) {
2477 $newuser -> lang = $CFG->lang;
2478 }
faebaf0f 2479 $newuser->confirmed = 1;
d96466d2 2480 $newuser->lastip = getremoteaddr();
faebaf0f 2481 $newuser->timemodified = time();
03d820c7 2482 $newuser->mnethostid = $CFG->mnet_localhost_id;
f9903ed0 2483
b0ccd3fb 2484 if (insert_record('user', $newuser)) {
16793340 2485 $user = get_complete_user_data('username', $newuser->username);
2486 if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){
2487 set_user_preference('auth_forcepasswordchange', 1, $user->id);
2488 }
2489 update_internal_user_password($user, $password);
2490 return $user;
faebaf0f 2491 }
2492 return false;
2493}
2494
7cf1c7bd 2495/**
2496 * Will update a local user record from an external source
2497 *
2498 * @uses $CFG
2499 * @param string $username New user's username to add to record
89dcb99d 2500 * @return user A {@link $USER} object
7cf1c7bd 2501 */
03d820c7 2502function update_user_record($username, $authplugin) {
2503 if (method_exists($authplugin, 'get_userinfo')) {
d35757eb 2504 $username = trim(moodle_strtolower($username)); /// just in case check text case
2505
b78fbdac 2506 $oldinfo = get_record('user', 'username', $username, '','','','', 'username, auth');
03d820c7 2507 $userauth = get_auth_plugin($oldinfo->auth);
b78fbdac 2508
03d820c7 2509 if ($newinfo = $authplugin->get_userinfo($username)) {
4b598ff4 2510 $newinfo = truncate_userinfo($newinfo);
d35757eb 2511 foreach ($newinfo as $key => $value){
b78fbdac 2512 $confkey = 'field_updatelocal_' . $key;
03d820c7 2513 if (!empty($userauth->config->$confkey) and $userauth->config->$confkey === 'onlogin') {
d35757eb 2514 $value = addslashes(stripslashes($value)); // Just in case
e1ecf0a0 2515 set_field('user', $key, $value, 'username', $username)
03d820c7 2516 or error_log("Error updating $key for $username");
d35757eb 2517 }
2518 }
2519 }
2520 }
7c12949d 2521 return get_complete_user_data('username', $username);
d35757eb 2522}
0609562b 2523
b36a8fc4 2524function truncate_userinfo($info) {
2525/// will truncate userinfo as it comes from auth_get_userinfo (from external auth)
2526/// which may have large fields
2527
2528 // define the limits
2529 $limit = array(
2530 'username' => 100,
1c66bf59 2531 'idnumber' => 64,
8bcd295c 2532 'firstname' => 100,
2533 'lastname' => 100,
b36a8fc4 2534 'email' => 100,
2535 'icq' => 15,
2536 'phone1' => 20,
2537 'phone2' => 20,
2538 'institution' => 40,
2539 'department' => 30,
2540 'address' => 70,
2541 'city' => 20,
2542 'country' => 2,
2543 'url' => 255,
2544 );
361855e6 2545
b36a8fc4 2546 // apply where needed
2547 foreach (array_keys($info) as $key) {
2548 if (!empty($limit[$key])) {
adfc03f9 2549 $info[$key] = trim(substr($info[$key],0, $limit[$key]));
361855e6 2550 }
b36a8fc4 2551 }
361855e6 2552
b36a8fc4 2553 return $info;
2554}
2555
7cf1c7bd 2556/**
2557 * Retrieve the guest user object
2558 *
2559 * @uses $CFG
89dcb99d 2560 * @return user A {@link $USER} object
7cf1c7bd 2561 */
0609562b 2562function guest_user() {
2563 global $CFG;
2564
b0ccd3fb 2565 if ($newuser = get_record('user', 'username', 'guest')) {
0609562b 2566 $newuser->confirmed = 1;
0609562b 2567 $newuser->lang = $CFG->lang;
d96466d2 2568 $newuser->lastip = getremoteaddr();
0609562b 2569 }
2570
2571 return $newuser;
2572}
2573
7cf1c7bd 2574/**
2575 * Given a username and password, this function looks them
2576 * up using the currently selected authentication mechanism,
2577 * and if the authentication is successful, it returns a
2578 * valid $user object from the 'user' table.
361855e6 2579 *
7cf1c7bd 2580 * Uses auth_ functions from the currently active auth module
2581 *
2582 * @uses $CFG
361855e6 2583 * @param string $username User's username
2584 * @param string $password User's password
89dcb99d 2585 * @return user|flase A {@link $USER} object or false if error
7cf1c7bd 2586 */
faebaf0f 2587function authenticate_user_login($username, $password) {
faebaf0f 2588
2589 global $CFG;
2590
03d820c7 2591 if (empty($CFG->auth)) {
16793340 2592 $authsenabled = array('manual');
2593 } else {
2594 $authsenabled = explode(',', 'manual,'.$CFG->auth);
18f16d61 2595 }
39a5a35d 2596
16793340 2597 if ($user = get_complete_user_data('username', $username)) {
2598 $auth = empty($user->auth) ? 'manual' : $user->auth; // use manual if auth not set
2599 if ($auth=='nologin' or !is_enabled_auth($auth)) {
2600 add_to_log(0, 'login', 'error', 'index.php', $username);
2601 error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Disabled Login: $username ".$_SERVER['HTTP_USER_AGENT']);
2602 return false;
27286aeb 2603 }
16793340 2604 if (!empty($user->deleted)) {
2605 add_to_log(0, 'login', 'error', 'index.php', $username);
2606 error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Deleted Login: $username ".$_SERVER['HTTP_USER_AGENT']);
2607 return false;
d35757eb 2608 }
16793340 2609 $auths = array($auth);
2610
71f9abf9 2611 } else {
16793340 2612 $auths = $authsenabled;
2613 $user = new object();
2614 $user->id = 0; // User does not exist
27286aeb 2615 }
8f0cd6ef 2616
03d820c7 2617 foreach ($auths as $auth) {
2618 $authplugin = get_auth_plugin($auth);
466558e3 2619
16793340 2620 // on auth fail fall through to the next plugin
03d820c7 2621 if (!$authplugin->user_login($username, $password)) {
03d820c7 2622 continue;
2623 }
faebaf0f 2624
03d820c7 2625 // successful authentication
d613daf0 2626 if ($user->id) { // User already exists in database
71f9abf9 2627 if (empty($user->auth)) { // For some reason auth isn't set yet
2628 set_field('user', 'auth', $auth, 'username', $username);
16793340 2629 $user->auth = $auth;
71f9abf9 2630 }
16793340 2631
2632 update_internal_user_password($user, $password); // just in case salt or encoding were changed (magic quotes too one day)
2633
03d820c7 2634 if (!$authplugin->is_internal()) { // update user record from external DB
2635 $user = update_user_record($username, get_auth_plugin($user->auth));
d35757eb 2636 }
faebaf0f 2637 } else {
16793340 2638 // if user not found, create him
71f9abf9 2639 $user = create_user_record($username, $password, $auth);
faebaf0f 2640 }
03d820c7 2641 // fix for MDL-6928
2642 if (method_exists($authplugin, 'iscreator')) {
c768b610 2643 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
2644 if ($creatorroles = get_roles_with_capability('moodle/legacy:coursecreator', CAP_ALLOW)) {
2645 $creatorrole = array_shift($creatorroles); // We can only use one, let's use the first one
2646 // Check if the user is a creator
03d820c7 2647 if ($authplugin->iscreator($username)) { // Following calls will not create duplicates
b6c59cf7 2648 role_assign($creatorrole->id, $user->id, 0, $sitecontext->id, 0, 0, 0, $auth);
4158fd81 2649 } else {
2650 role_unassign($creatorrole->id, $user->id, 0, $sitecontext->id);
c768b610 2651 }
2652 }
2653 }
01af6da6 2654
2655 /// Log in to a second system if necessary
2656 if (!empty($CFG->sso)) {
2657 include_once($CFG->dirroot .'/sso/'. $CFG->sso .'/lib.php');
2658 if (function_exists('sso_user_login')) {
2659 if (!sso_user_login($username, $password)) { // Perform the signon process
2660 notify('Second sign-on failed');
2661 }
2662 }
2663 }
2664
e582b65e 2665 return $user;
9d3c795c 2666
03d820c7 2667 }
2668
2669 // failed if all the plugins have failed
2670 add_to_log(0, 'login', 'error', 'index.php', $username);
2671 error_log('[client '.$_SERVER['REMOTE_ADDR']."] $CFG->wwwroot Failed Login: $username ".$_SERVER['HTTP_USER_AGENT']);
2672 return false;
f9903ed0 2673}
2674
df193157 2675/**
4908ad3e 2676 * Compare password against hash stored in internal user table.
df193157 2677 * If necessary it also updates the stored hash to new format.
2678 *
2679 * @param object user
2680 * @param string plain text password
2681 * @return bool is password valid?
2682 */
2683function validate_internal_user_password(&$user, $password) {
2684 global $CFG;
2685
4908ad3e 2686 if (!isset($CFG->passwordsaltmain)) {
2687 $CFG->passwordsaltmain = '';
2688 }
2689
df193157 2690 $validated = false;
2691
fb773106 2692 // get password original encoding in case it was not updated to unicode yet
2693 $textlib = textlib_get_instance();
810944af 2694 $convpassword = $textlib->convert($password, 'utf-8', get_string('oldcharset'));
df193157 2695
4908ad3e 2696 if ($user->password == md5($password.$CFG->passwordsaltmain) or $user->password == md5($password)
2697 or $user->password == md5($convpassword.$CFG->passwordsaltmain) or $user->password == md5($convpassword)) {
df193157 2698 $validated = true;
4908ad3e 2699 } else {
aaeaa4b0 2700 for ($i=1; $i<=20; $i++) { //20 alternative salts should be enough, right?
4908ad3e 2701 $alt = 'passwordsaltalt'.$i;
2702 if (!empty($CFG->$alt)) {
2703 if ($user->password == md5($password.$CFG->$alt) or $user->password == md5($convpassword.$CFG->$alt)) {
2704 $validated = true;
2705 break;
2706 }
2707 }
2708 }
df193157 2709 }
2710
2711 if ($validated) {
4908ad3e 2712 // force update of password hash using latest main password salt and encoding if needed
df193157 2713 update_internal_user_password($user, $password);
2714 }
2715
2716 return $validated;
2717}
2718
2719/**
2720 * Calculate hashed value from password using current hash mechanism.
df193157 2721 *
2722 * @param string password
2723 * @return string password hash
2724 */
2725function hash_internal_user_password($password) {
4908ad3e 2726 global $CFG;
2727
2728 if (isset($CFG->passwordsaltmain)) {
2729 return md5($password.$CFG->passwordsaltmain);
2730 } else {
2731 return md5($password);
2732 }
df193157 2733}
2734
2735/**
2736 * Update pssword hash in user object.
2737 *
2738 * @param object user
2739 * @param string plain text password
2740 * @param bool store changes also in db, default true
2741 * @return true if hash changed
2742 */
16793340 2743function update_internal_user_password(&$user, $password) {
df193157 2744 global $CFG;
2745
03d820c7 2746 $authplugin = get_auth_plugin($user->auth);
16793340 2747 if (!empty($authplugin->config->preventpassindb)) {
df193157 2748 $hashedpassword = 'not cached';
2749 } else {
2750 $hashedpassword = hash_internal_user_password($password);
2751 }
2752
b7b50143 2753 return set_field('user', 'password', $hashedpassword, 'id', $user->id);
df193157 2754}
2755
7c12949d 2756/**
2757 * Get a complete user record, which includes all the info
5c98bf9e 2758 * in the user record
7c12949d 2759 * Intended for setting as $USER session variable
2760 *
2761 * @uses $CFG
2762 * @uses SITEID
e1ecf0a0 2763 * @param string $field The user field to be checked for a given value.
7c12949d 2764 * @param string $value The value to match for $field.
2765 * @return user A {@link $USER} object.
2766 */
b7b50143 2767function get_complete_user_data($field, $value, $mnethostid=null) {
7c12949d 2768
2769 global $CFG;
2770
2771 if (!$field || !$value) {
2772 return false;
2773 }
2774
b7b50143 2775/// Build the WHERE clause for an SQL query
2776
2777 $constraints = $field .' = \''. $value .'\' AND deleted <> \'1\'';
2778
2779 if (null === $mnethostid) {
2780 $constraints .= ' AND auth != \'mnet\'';
2781 } elseif (is_numeric($mnethostid)) {
2782 $constraints .= ' AND mnethostid = \''.$mnethostid.'\'';
2783 } else {
2784 error_log('Call to get_complete_user_data for $field='.$field.', $value = '.$value.', with invalid $mnethostid: '. $mnethostid);
2785 print_error('invalidhostlogin','mnet', $CFG->wwwroot.'/login/index.php');
2786 exit;
2787 }
2788
7c12949d 2789/// Get all the basic user data
2790
b7b50143 2791 if (! $user = get_record_select('user', $constraints)) {
7c12949d 2792 return false;
2793 }
2794
7c12949d 2795/// Get various settings and preferences
2796
2797 if ($displays = get_records('course_display', 'userid', $user->id)) {
2798 foreach ($displays as $display) {
2799 $user->display[$display->course] = $display->display;
2800 }
2801 }
2802
2803 if ($preferences = get_records('user_preferences', 'userid', $user->id)) {
2804 foreach ($preferences as $preference) {
2805 $user->preference[$preference->name] = $preference->value;
2806 }
2807 }
2808
721d14cb 2809 if ($lastaccesses = get_records('user_lastaccess', 'userid', $user->id)) {
2810 foreach ($lastaccesses as $lastaccess) {
2811 $user->lastcourseaccess[$lastaccess->courseid] = $lastaccess->timeaccess;
2812 }
2813 }
2814
f4ba7e1a 2815 if ($groupids = groups_get_all_groups_for_user($user->id)) { //TODO:check.
2816 foreach ($groupids as $groupid) {
2817 $courseid = groups_get_course($groupid);
fa22fd5f 2818 //change this to 2D array so we can put multiple groups in a course
f4ba7e1a 2819 $user->groupmember[$courseid][] = $groupid;
7c12949d 2820 }
2821 }
2822
2823/// Rewrite some variables if necessary
2824 if (!empty($user->description)) {
2825 $user->description = true; // No need to cart all of it around
2826 }
2827 if ($user->username == 'guest') {
2828 $user->lang = $CFG->lang; // Guest language always same as site
2829 $user->firstname = get_string('guestuser'); // Name always in current language
2830 $user->lastname = ' ';
2831 }
2832
7c12949d 2833 $user->sesskey = random_string(10);
2834 $user->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
2835
2836 return $user;
7c12949d 2837}
2838
7c12949d 2839
5c98bf9e 2840
e1ecf0a0 2841/*
7c12949d 2842 * When logging in, this function is run to set certain preferences
2843 * for the current SESSION
2844 */
2845function set_login_session_preferences() {
7c7ca1b5 2846 global $SESSION, $CFG;
7c12949d 2847
2848 $SESSION->justloggedin = true;
2849
2850 unset($SESSION->lang);
7c12949d 2851
2852 // Restore the calendar filters, if saved
2853 if (intval(get_user_preferences('calendar_persistflt', 0))) {
2854 include_once($CFG->dirroot.'/calendar/lib.php');
a266c904 2855 calendar_set_filters_status(get_user_preferences('calendav_savedflt', 0xff));
7c12949d 2856 }
2857}
2858
2859
b97c4164 2860/**
2861 * Delete a course, including all related data from the database,
2862 * and any associated files from the moodledata folder.
e1ecf0a0 2863 *
b97c4164 2864 * @param int $courseid The id of the course to delete.
2865 * @param bool $showfeedback Whether to display notifications of each action the function performs.
2866 * @return bool true if all the removals succeeded. false if there were any failures. If this
2867 * method returns false, some of the removals will probably have succeeded, and others
2868 * failed, but you have no way of knowing which.
2869 */
2870function delete_course($courseid, $showfeedback = true) {
2871 global $CFG;
2872 $result = true;
e1ecf0a0 2873
b97c4164 2874 if (!remove_course_contents($courseid, $showfeedback)) {
2875 if ($showfeedback) {
2876 notify("An error occurred while deleting some of the course contents.");
2877 }
2878 $result = false;
2879 }
2880
2881 if (!delete_records("course", "id", $courseid)) {
2882 if ($showfeedback) {
2883 notify("An error occurred while deleting the main course record.");
2884 }
2885 $result = false;
2886 }
2887
9991d157 2888 if (!delete_records('context', 'contextlevel', CONTEXT_COURSE, 'instanceid', $courseid)) {
eaa79489 2889 if ($showfeedback) {
2890 notify("An error occurred while deleting the main context record.");
2891 }
2892 $result = false;
2893 }
2894
b97c4164 2895 if (!fulldelete($CFG->dataroot.'/'.$courseid)) {
2896 if ($showfeedback) {
2897 notify("An error occurred while deleting the course files.");
2898 }
2899 $result = false;
2900 }
e1ecf0a0 2901
b97c4164 2902 return $result;
2903}
2904
7cf1c7bd 2905/**
2906 * Clear a course out completely, deleting all content
2907 * but don't delete the course itself
2908 *
7cf1c7bd 2909 * @uses $CFG
1f8ede91 2910 * @param int $courseid The id of the course that is being deleted
2911 * @param bool $showfeedback Whether to display notifications of each action the function performs.
b97c4164 2912 * @return bool true if all the removals succeeded. false if there were any failures. If this
2913 * method returns false, some of the removals will probably have succeeded, and others
2914 * failed, but you have no way of knowing which.
7cf1c7bd 2915 */
07aeb7b0 2916function remove_course_contents($courseid, $showfeedback=true) {
07aeb7b0 2917
1f8ede91 2918 global $CFG;
07aeb7b0 2919
2920 $result = true;
2921
b0ccd3fb 2922 if (! $course = get_record('course', 'id', $courseid)) {
2923 error('Course ID was incorrect (can\'t find it)');
07aeb7b0 2924 }
2925
b0ccd3fb 2926 $strdeleted = get_string('deleted');
07aeb7b0 2927
9991d157 2928/// First delete every instance of every module
d8ba183c 2929
b0ccd3fb 2930 if ($allmods = get_records('modules') ) {
07aeb7b0 2931 foreach ($allmods as $mod) {
2932 $modname = $mod->name;
b0ccd3fb 2933 $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
2934 $moddelete = $modname .'_delete_instance'; // Delete everything connected to an instance
2935 $moddeletecourse = $modname .'_delete_course'; // Delete other stray stuff (uncommon)
07aeb7b0 2936 $count=0;
2937 if (file_exists($modfile)) {
2938 include_once($modfile);
2939 if (function_exists($moddelete)) {
b0ccd3fb 2940 if ($instances = get_records($modname, 'course', $course->id)) {
07aeb7b0 2941 foreach ($instances as $instance) {
eaa79489 2942 if ($cm = get_coursemodule_from_instance($modname, $instance->id, $course->id)) {
9991d157 2943 delete_context(CONTEXT_MODULE, $cm->id);
eaa79489 2944 }
07aeb7b0 2945 if ($moddelete($instance->id)) {
2946 $count++;
eaa79489 2947
07aeb7b0 2948 } else {
7eec3390 2949 notify('Could not delete '. $modname .' instance '. $instance->id .' ('. format_string($instance->name) .')');
07aeb7b0 2950 $result = false;
2951 }
2952 }
2953 }
2954 } else {
b0ccd3fb 2955 notify('Function '. $moddelete() .'doesn\'t exist!');
07aeb7b0 2956 $result = false;
2957 }
2958
ca952b03 2959 if (function_exists($moddeletecourse)) {
f67172b6 2960 $moddeletecourse($course, $showfeedback);
ca952b03 2961 }
07aeb7b0 2962 }
2963 if ($showfeedback) {
b0ccd3fb 2964 notify($strdeleted .' '. $count .' x '. $modname);
07aeb7b0 2965 }
2966 }
2967 } else {
b0ccd3fb 2968 error('No modules are installed!');
07aeb7b0 2969 }
2970
9991d157 2971/// Give local code a chance to delete its references to this course.
c0b5b31a 2972 require_once('locallib.php');
2973 notify_local_delete_course($courseid, $showfeedback);
2974
9991d157 2975/// Delete course blocks
7f869120 2976
2977 if ($blocks = get_records_sql("SELECT *
2978 FROM {$CFG->prefix}block_instance
2979 WHERE pagetype = '".PAGE_COURSE_VIEW."'
2980 AND pageid = $course->id")) {
eaa79489 2981 if (delete_records('block_instance', 'pagetype', PAGE_COURSE_VIEW, 'pageid', $course->id)) {
2982 if ($showfeedback) {
2983 notify($strdeleted .' block_instance');
2984 }
9991d157 2985 foreach ($blocks as $block) { /// Delete any associated contexts for this block
2986 delete_context(CONTEXT_BLOCK, $block->id);
2987 }
2988 } else {
2989 $result = false;
2990 }
2991 }
2992
2dee6abd 2993/// Delete any groups, removing members and grouping/course links first.
2994 //TODO: If groups or groupings are to be shared between courses, think again!
f4ba7e1a 2995 if ($groupids = groups_get_groups($course->id)) {
2996 foreach ($groupids as $groupid) {
2dee6abd 2997 if (groups_remove_all_members($groupid)) {
f4ba7e1a 2998 if ($showfeedback) {
2999 notify($strdeleted .' groups_members');
3000 }
3001 } else {
3002 $result = false;
9991d157 3003 }
f4ba7e1a 3004 /// Delete any associated context for this group ??
2dee6abd 3005 delete_context(CONTEXT_GROUP, $groupid);
f4ba7e1a 3006
3007 if (groups_delete_group($groupid)) {
3008 if ($showfeedback) {
3009 notify($strdeleted .' groups');
9991d157 3010 }
f4ba7e1a 3011 } else {
3012 $result = false;
9991d157 3013 }
251af423 3014 }
251af423 3015 }
2dee6abd 3016/// Delete any groupings.
3017 $result = groups_delete_all_groupings($course->id);
3018 if ($result && $showfeedback) {
3019 notify($strdeleted .' groupings');
3020 }
251af423 3021
9991d157 3022/// Delete all related records in other tables that may have a courseid
3023/// This array stores the tables that need to be cleared, as
3024/// table_name => column_name that contains the course id.
3025
09481160 3026 $tablestoclear = array(
09481160 3027 'event' => 'courseid', // Delete events
3028 'log' => 'course', // Delete logs
3029 'course_sections' => 'course', // Delete any course stuff
3030 'course_modules' => 'course',
3031 'grade_category' => 'courseid', // Delete gradebook stuff
3032 'grade_exceptions' => 'courseid',
3033 'grade_item' => 'courseid',
3034 'grade_letter' => 'courseid',
d148c0ed 3035 'grade_preferences' => 'courseid',
3036 'backup_courses' => 'courseid', // Delete scheduled backup stuff
3037 'backup_log' => 'courseid'
09481160 3038 );
3039 foreach ($tablestoclear as $table => $col) {
3040 if (delete_records($table, $col, $course->id)) {
3041 if ($showfeedback) {
3042 notify($strdeleted . ' ' . $table);
3043 }
3044 } else {
3045 $result = false;
07aeb7b0 3046 }
07aeb7b0 3047 }
3048
9991d157 3049
3050/// Clean up metacourse stuff
082e3ebc 3051
5f37b628 3052 if ($course->metacourse) {
3053 delete_records("course_meta","parent_course",$course->id);
b61efafb 3054 sync_metacourse($course->id); // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
3055 if ($showfeedback) {
5f37b628 3056 notify("$strdeleted course_meta");
b61efafb 3057 }
7ff9860d 3058 } else {
5f37b628 3059 if ($parents = get_records("course_meta","child_course",$course->id)) {
b61efafb 3060 foreach ($parents as $parent) {
3061 remove_from_metacourse($parent->parent_course,$parent->child_course); // this will do the unenrolments as well.
3062 }
3063 if ($showfeedback) {
5f37b628 3064 notify("$strdeleted course_meta");
b61efafb 3065 }
3066 }
3067 }
3068
9991d157 3069/// Delete questions and question categories
f67172b6 3070 include_once($CFG->libdir.'/questionlib.php');
3071 question_delete_course($course, $showfeedback);
3072
9991d157 3073/// Delete all roles and overiddes in the course context (but keep the course context)
39474844 3074 if ($courseid != SITEID) {
3075 delete_context(CONTEXT_COURSE, $course->id);
3076 }
3077
07aeb7b0 3078 return $result;
07aeb7b0 3079}
3080
ffd0fbb7 3081
7cf1c7bd 3082/**
ffd0fbb7 3083 * This function will empty a course of USER data as much as
3084/// possible. It will retain the activities and the structure
3085/// of the course.
7cf1c7bd 3086 *
ffd0fbb7 3087 * @uses $USER
3088 * @uses $SESSION
7cf1c7bd 3089 * @uses $CFG
ffd0fbb7 3090 * @param object $data an object containing all the boolean settings and courseid
3091 * @param bool $showfeedback if false then do it all silently
3092 * @return bool
3093 * @todo Finish documenting this function
7cf1c7bd 3094 */
ffd0fbb7 3095function reset_course_userdata($data, $showfeedback=true) {
3831de52 3096
ffd0fbb7 3097 global $CFG, $USER, $SESSION;
3831de52 3098
3099 $result = true;
3100
b0ccd3fb 3101 $strdeleted = get_string('deleted');
3831de52 3102
3103 // Look in every instance of every module for data to delete
3104
b0ccd3fb 3105 if ($allmods = get_records('modules') ) {
3831de52 3106 foreach ($allmods as $mod) {
3107 $modname = $mod->name;
b0ccd3fb 3108 $modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
3109 $moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data
3831de52 3110 if (file_exists($modfile)) {
3111 @include_once($modfile);
3112 if (function_exists($moddeleteuserdata)) {
ffd0fbb7 3113 $moddeleteuserdata($data, $showfeedback);
3831de52 3114 }
3115 }
3116 }
3117 } else {
b0ccd3fb 3118 error('No modules are installed!');
3831de52 3119 }
3120
3121 // Delete other stuff
20d55a1b 3122 $coursecontext = get_context_instance(CONTEXT_COURSE, $data->courseid);
3831de52 3123
20d55a1b 3124 if (!empty($data->reset_students) or !empty($data->reset_teachers)) {
3125 $teachers = array_keys(get_users_by_capability($coursecontext, 'moodle/course:update'));
3126 $participants = array_keys(get_users_by_capability($coursecontext, 'moodle/course:view'));
3127 $students = array_diff($participants, $teachers);
3128
3129 if (!empty($data->reset_students)) {
3130 foreach ($students as $studentid) {
3131 role_unassign(0, $studentid, 0, $coursecontext->id);
3132 }
3831de52 3133 if ($showfeedback) {
20d55a1b 3134 notify($strdeleted .' '.get_string('students'), 'notifysuccess');
3831de52 3135 }
d76a5a7f 3136
f4ba7e1a 3137 /// Delete group members (but keep the groups) TODO:check.
3138 if ($groupids = groups_get_groups($data->courseid)) {
3139 foreach ($groupids as $groupid) {
3140 if (groups_remove_all_group_members($groupid)) {
20d55a1b 3141 if ($showfeedback) {
3142 notify($strdeleted .' groups_members', 'notifysuccess');
3143 }
3144 } else {
3145 $result = false;
3831de52 3146 }
3831de52 3147 }
3148 }
3149 }
3831de52 3150
20d55a1b 3151 if (!empty($data->reset_teachers)) {
3152 foreach ($teachers as $teacherid) {
3153 role_unassign(0, $teacherid, 0, $coursecontext->id);
3154 }
3831de52 3155 if ($showfeedback) {
20d55a1b 3156 notify($strdeleted .' '.get_string('teachers'), 'notifysuccess');
3831de52 3157 }
3831de52 3158 }
3159 }
3160
ffd0fbb7 3161 if (!empty($data->reset_groups)) {
f4ba7e1a 3162 if ($groupids = groups_get_groups($data->courseid)) {
3163 foreach ($groupids as $groupid) {
3164 if (groups_delete_group($groupid)) {
3831de52 3165 if ($showfeedback) {
ffd0fbb7 3166 notify($strdeleted .' groups', 'notifysuccess');
3831de52 3167 }
3168 } else {
3169 $result = false;
3170 }
3171 }
3172 }
3173 }
3174
ffd0fbb7 3175 if (!empty($data->reset_events)) {
3176 if (delete_records('event', 'courseid', $data->courseid)) {
3831de52 3177 if ($showfeedback) {
ffd0fbb7 3178 notify($strdeleted .' event', 'notifysuccess');
3831de52 3179 }
3180 } else {
3181 $result = false;
3182 }
3183 }
3184
ffd0fbb7 3185 if (!empty($data->reset_logs)) {
3186 if (delete_records('log', 'course', $data->courseid)) {
3831de52 3187 if ($showfeedback) {
ffd0fbb7 3188 notify($strdeleted .' log', 'notifysuccess');
3831de52 3189 }
3190 } else {
3191 $result = false;
3192 }
3193 }
3194
ea8158c1 3195 // deletes all role assignments, and local override, these have no courseid in table and needs separate process
9fd73f0c 3196 $context = get_context_instance(CONTEXT_COURSE, $data->courseid);
1bacd276 3197 delete_records('role_capabilities', 'contextid', $context->id);
20d55a1b 3198
3831de52 3199 return $result;
3831de52 3200}
3201
ffd0fbb7 3202
f4ba7e1a 3203require_once($CFG->dirroot.'/group/lib.php');
3204/*TODO: functions moved to /group/lib/legacylib.php
f374fb10 3205
f4ba7e1a 3206ismember
3207add_user_to_group
3208mygroupid
3209groupmode
3210set_current_group
3211... */
f374fb10 3212
0d67c514 3213
bb64b51a 3214function generate_email_processing_address($modid,$modargs) {
3215 global $CFG;
303d0af1 3216
3217 if (empty($CFG->siteidentifier)) { // Unique site identification code
3218 set_config('siteidentifier', random_string(32));
bb64b51a 3219 }
d2a9f7cc 3220
bb64b51a 3221 $header = $CFG->mailprefix . substr(base64_encode(pack('C',$modid)),0,2).$modargs;
2019116e 3222 return $header . substr(md5($header.$CFG->siteidentifier),0,16).'@'.$CFG->maildomain;
bb64b51a 3223}
3224
f374fb10 3225
bb64b51a 3226function moodle_process_email($modargs,$body) {
3227 // the first char should be an unencoded letter. We'll take this as an action
3228 switch ($modargs{0}) {
3229 case 'B': { // bounce
3230 list(,$userid) = unpack('V',base64_decode(substr($modargs,1,8)));
3231 if ($user = get_record_select("user","id=$userid","id,email")) {
3232 // check the half md5 of their email
3233 $md5check = substr(md5($user->email),0,16);
e67755a1 3234 if ($md5check == substr($modargs, -16)) {
bb64b51a 3235 set_bounce_count($user);
3236 }
3237 // else maybe they've already changed it?
3238 }
3239 }
3240 break;
3241 // maybe more later?
3242 }
3243}
f374fb10 3244
f9903ed0 3245/// CORRESPONDENCE ////////////////////////////////////////////////
3246
7cf1c7bd 3247/**
3248 * Send an email to a specified user