Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

5431 lines (4643 sloc) 169.161 kb
<?php // $Id$
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
/**
* moodlelib.php - Moodle main library
*
* Main library file of miscellaneous general-purpose Moodle functions.
* Other main libraries:
* - weblib.php - functions that produce web output
* - datalib.php - functions that access the database
* @author Martin Dougiamas
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package moodlecore
*/
/// CONSTANTS /////////////////////////////////////////////////////////////
/**
* Used by some scripts to check they are being called by Moodle
*/
define('MOODLE_INTERNAL', true);
/**
* No groups used?
*/
define('NOGROUPS', 0);
/**
* Groups used?
*/
define('SEPARATEGROUPS', 1);
/**
* Groups visible?
*/
define('VISIBLEGROUPS', 2);
/**
* Time constant - the number of seconds in a week
*/
define('WEEKSECS', 604800);
/**
* Time constant - the number of seconds in a day
*/
define('DAYSECS', 86400);
/**
* Time constant - the number of seconds in an hour
*/
define('HOURSECS', 3600);
/**
* Time constant - the number of seconds in a minute
*/
define('MINSECS', 60);
/**
* Time constant - the number of minutes in a day
*/
define('DAYMINS', 1440);
/**
* Time constant - the number of minutes in an hour
*/
define('HOURMINS', 60);
/**
* Parameter constants - if set then the parameter is cleaned of scripts etc
*/
define('PARAM_RAW', 0x0000);
define('PARAM_CLEAN', 0x0001);
define('PARAM_INT', 0x0002);
define('PARAM_INTEGER', 0x0002); // Alias for PARAM_INT
define('PARAM_ALPHA', 0x0004);
define('PARAM_ACTION', 0x0004); // Alias for PARAM_ALPHA
define('PARAM_FORMAT', 0x0004); // Alias for PARAM_ALPHA
define('PARAM_NOTAGS', 0x0008);
define('PARAM_FILE', 0x0010);
define('PARAM_PATH', 0x0020);
define('PARAM_HOST', 0x0040); // FQDN or IPv4 dotted quad
define('PARAM_URL', 0x0080);
define('PARAM_LOCALURL',0x0180); // NOT orthogonal to the others! Implies PARAM_URL!
define('PARAM_CLEANFILE',0x0200);
define('PARAM_ALPHANUM',0x0400); //numbers or letters only
define('PARAM_BOOL', 0x0800); //convert to value 1 or 0 using empty()
/**
* Definition of page types
*/
define('PAGE_COURSE_VIEW', 'course-view');
/// PARAMETER HANDLING ////////////////////////////////////////////////////
/**
* Returns a particular value for the named variable, taken from
* POST or GET. If the parameter doesn't exist then an error is
* thrown because we require this variable.
*
* This function should be used to initialise all required values
* in a script that are based on parameters. Usually it will be
* used like this:
* $id = required_param('id');
*
* @param string $varname the name of the parameter variable we want
* @param integer $options a bit field that specifies any cleaning needed
* @return mixed
*/
function required_param($varname, $options=PARAM_CLEAN) {
if (isset($_POST[$varname])) { // POST has precedence
$param = $_POST[$varname];
} else if (isset($_GET[$varname])) {
$param = $_GET[$varname];
} else {
error('A required parameter ('.$varname.') was missing');
}
return clean_param($param, $options);
}
/**
* Returns a particular value for the named variable, taken from
* POST or GET, otherwise returning a given default.
*
* This function should be used to initialise all optional values
* in a script that are based on parameters. Usually it will be
* used like this:
* $name = optional_param('name', 'Fred');
*
* @param string $varname the name of the parameter variable we want
* @param mixed $default the default value to return if nothing is found
* @param integer $options a bit field that specifies any cleaning needed
* @return mixed
*/
function optional_param($varname, $default=NULL, $options=PARAM_CLEAN) {
if (isset($_POST[$varname])) { // POST has precedence
$param = $_POST[$varname];
} else if (isset($_GET[$varname])) {
$param = $_GET[$varname];
} else {
return $default;
}
return clean_param($param, $options);
}
/**
* Used by {@link optional_param()} and {@link required_param()} to
* clean the variables and/or cast to specific types, based on
* an options field.
*
* @param mixed $param the variable we are cleaning
* @param integer $options a bit field that specifies the cleaning needed
* @return mixed
*/
function clean_param($param, $options) {
global $CFG;
if (!$options) {
return $param; // Return raw value
}
if ((string)$param == (string)(int)$param) { // It's just an integer
return (int)$param;
}
if ($options & PARAM_CLEAN) {
$param = clean_text($param); // Sweep for scripts, etc
}
if ($options & PARAM_INT) {
$param = (int)$param; // Convert to integer
}
if ($options & PARAM_ALPHA) { // Remove everything not a-z
$param = eregi_replace('[^a-zA-Z]', '', $param);
}
if ($options & PARAM_ALPHANUM) { // Remove everything not a-zA-Z0-9
$param = eregi_replace('[^A-Za-z0-9]', '', $param);
}
if ($options & PARAM_BOOL) { // Convert to 1 or 0
$param = empty($param) ? 0 : 1;
}
if ($options & PARAM_NOTAGS) { // Strip all tags completely
$param = strip_tags($param);
}
if ($options & PARAM_CLEANFILE) { // allow only safe characters
$param = clean_filename($param);
}
if ($options & PARAM_FILE) { // Strip all suspicious characters from filename
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':\\/]', '', $param);
$param = ereg_replace('\.\.+', '', $param);
}
if ($options & PARAM_PATH) { // Strip all suspicious characters from file path
$param = str_replace('\\\'', '\'', $param);
$param = str_replace('\\"', '"', $param);
$param = str_replace('\\', '/', $param);
$param = ereg_replace('[[:cntrl:]]|[<>"`\|\':]', '', $param);
$param = ereg_replace('\.\.+', '', $param);
$param = ereg_replace('//+', '/', $param);
}
if ($options & PARAM_HOST) { // allow FQDN or IPv4 dotted quad
preg_replace('/[^\.\d\w-]/','', $param ); // only allowed chars
// match ipv4 dotted quad
if (preg_match('/(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/',$param, $match)){
// confirm values are ok
if ( $match[0] > 255
|| $match[1] > 255
|| $match[3] > 255
|| $match[4] > 255 ) {
// hmmm, what kind of dotted quad is this?
$param = '';
}
} elseif ( preg_match('/^[\w\d\.-]+$/', $param) // dots, hyphens, numbers
&& !preg_match('/^[\.-]/', $param) // no leading dots/hyphens
&& !preg_match('/[\.-]$/', $param) // no trailing dots/hyphens
) {
// all is ok - $param is respected
} else {
// all is not ok...
$param='';
}
}
if ($options & PARAM_URL) { // allow safe ftp, http, mailto urls
include_once($CFG->dirroot . '/lib/validateurlsyntax.php');
//
// Parameters to validateurlsyntax()
//
// s? scheme is optional
// H? http optional
// S? https optional
// F? ftp optional
// E? mailto optional
// u- user section not allowed
// P- password not allowed
// a? address optional
// I? Numeric IP address optional (can use IP or domain)
// p- port not allowed -- restrict to default port
// f? "file" path section optional
// q? query section optional
// r? fragment (anchor) optional
//
if (!empty($param) && validateUrlSyntax($param, 's?H?S?F?E?u-P-a?I?p-f?q?r?')) {
// all is ok, param is respected
} else {
$param =''; // not really ok
}
$options ^= PARAM_URL; // Turn off the URL bit so that simple PARAM_URLs don't test true for PARAM_LOCALURL
}
if ($options & PARAM_LOCALURL) {
// assume we passed the PARAM_URL test...
// allow http absolute, root relative and relative URLs within wwwroot
if (!empty($param)) {
if (preg_match(':^/:', $param)) {
// root-relative, ok!
} elseif (preg_match('/^'.preg_quote($CFG->wwwroot, '/').'/i',$param)) {
// absolute, and matches our wwwroot
} else {
// relative - let's make sure there are no tricks
if (validateUrlSyntax($param, 's-u-P-a-p-f+q?r?')) {
// looks ok.
} else {
$param = '';
}
}
}
}
return $param;
}
/**
* For security purposes, this function will check that the currently
* given sesskey (passed as a parameter to the script or this function)
* matches that of the current user.
*
* @param string $sesskey optionally provided sesskey
* @return boolean
*/
function confirm_sesskey($sesskey=NULL) {
global $USER;
if (!empty($USER->ignoresesskey)) {
return true;
}
if (empty($sesskey)) {
$sesskey = required_param('sesskey'); // Check script parameters
}
if (!isset($USER->sesskey)) {
return false;
}
return ($USER->sesskey === $sesskey);
}
/**
* Ensure that a variable is set
*
* If $var is undefined throw an error, otherwise return $var.
* This function will soon be made obsolete by {@link required_param()}
*
* @param mixed $var the variable which may be unset
* @param mixed $default the value to return if $var is unset
*/
function require_variable($var) {
if (! isset($var)) {
error('A required parameter was missing');
}
}
/**
* Ensure that a variable is set
*
* If $var is undefined set it (by reference), otherwise return $var.
* This function will soon be made obsolete by {@link optional_param()}
*
* @param mixed $var the variable which may be unset
* @param mixed $default the value to return if $var is unset
*/
function optional_variable(&$var, $default=0) {
if (! isset($var)) {
$var = $default;
}
}
/**
* Set a key in global configuration
*
* Set a key/value pair in both this session's {@link $CFG} global variable
* and in the 'config' database table for future sessions.
*
* @param string $name the key to set
* @param string $value the value to set
* @uses $CFG
* @return bool
*/
function set_config($name, $value) {
/// No need for get_config because they are usually always available in $CFG
global $CFG;
$CFG->$name = $value; // So it's defined for this invocation at least
if (get_field('config', 'name', 'name', $name)) {
return set_field('config', 'value', $value, 'name', $name);
} else {
$config->name = $name;
$config->value = $value;
return insert_record('config', $config);
}
}
/**
* Refresh current $USER session global variable with all their current preferences.
* @uses $USER
*/
function reload_user_preferences() {
global $USER;
if(empty($USER) || empty($USER->id)) {
return false;
}
unset($USER->preference);
if ($preferences = get_records('user_preferences', 'userid', $USER->id)) {
foreach ($preferences as $preference) {
$USER->preference[$preference->name] = $preference->value;
}
} else {
//return empty preference array to hold new values
$USER->preference = array();
}
}
/**
* Sets a preference for the current user
* Optionally, can set a preference for a different user object
* @uses $USER
* @todo Add a better description and include usage examples.
* @param string $name The key to set as preference for the specified user
* @param string $value The value to set forthe $name key in the specified user's record
* @param int $userid A moodle user ID
* @todo Add inline links to $USER and user functions in above line.
* @return boolean
*/
function set_user_preference($name, $value, $otheruser=NULL) {
global $USER;
if (empty($otheruser)){
if (!empty($USER) && !empty($USER->id)) {
$userid = $USER->id;
} else {
return false;
}
} else {
$userid = $otheruser;
}
if (empty($name)) {
return false;
}
if ($preference = get_record('user_preferences', 'userid', $userid, 'name', $name)) {
if (set_field('user_preferences', 'value', $value, 'id', $preference->id)) {
if (empty($otheruser) and !empty($USER)) {
$USER->preference[$name] = $value;
}
return true;
} else {
return false;
}
} else {
$preference->userid = $userid;
$preference->name = $name;
$preference->value = (string)$value;
if (insert_record('user_preferences', $preference)) {
if (empty($otheruser) and !empty($USER)) {
$USER->preference[$name] = $value;
}
return true;
} else {
return false;
}
}
}
/**
* Unsets a preference completely by deleting it from the database
* Optionally, can set a preference for a different user id
* @uses $USER
* @param string $name The key to unset as preference for the specified user
* @param int $userid A moodle user ID
* @return boolean
*/
function unset_user_preference($name, $userid=NULL) {
global $USER;
if (empty($userid)){
if(!empty($USER) && !empty($USER->id)) {
$userid = $USER->id;
}
else {
return false;
}
}
return delete_records('user_preferences', 'userid', $userid, 'name', $name);
}
/**
* Sets a whole array of preferences for the current user
* @param array $prefarray An array of key/value pairs to be set
* @param int $userid A moodle user ID
* @return boolean
*/
function set_user_preferences($prefarray, $userid=NULL) {
global $USER;
if (!is_array($prefarray) or empty($prefarray)) {
return false;
}
if (empty($userid)){
if (!empty($USER) && !empty($USER->id)) {
$userid = NULL; // Continue with the current user below
} else {
return false; // No-one to set!
}
}
$return = true;
foreach ($prefarray as $name => $value) {
// The order is important; if the test for return is done first, then
// if one function call fails all the remaining ones will be "optimized away"
$return = set_user_preference($name, $value, $userid) and $return;
}
return $return;
}
/**
* If no arguments are supplied this function will return
* all of the current user preferences as an array.
* If a name is specified then this function
* attempts to return that particular preference value. If
* none is found, then the optional value $default is returned,
* otherwise NULL.
* @param string $name Name of the key to use in finding a preference value
* @param string $default Value to be returned if the $name key is not set in the user preferences
* @param int $userid A moodle user ID
* @uses $USER
* @return string
*/
function get_user_preferences($name=NULL, $default=NULL, $userid=NULL) {
global $USER;
if (empty($userid)) { // assume current user
if (empty($USER->preference)) {
return $default; // Default value (or NULL)
}
if (empty($name)) {
return $USER->preference; // Whole array
}
if (!isset($USER->preference[$name])) {
return $default; // Default value (or NULL)
}
return $USER->preference[$name]; // The single value
} else {
$preference = get_records_menu('user_preferences', 'userid', $userid, 'name', 'name,value');
if (empty($name)) {
return $preference;
}
if (!isset($preference[$name])) {
return $default; // Default value (or NULL)
}
return $preference[$name]; // The single value
}
}
/// FUNCTIONS FOR HANDLING TIME ////////////////////////////////////////////
/**
* Given date parts in user time produce a GMT timestamp.
*
* @param int $year The year part to create timestamp of.
* @param int $month The month part to create timestamp of.
* @param int $day The day part to create timestamp of.
* @param int $hour The hour part to create timestamp of.
* @param int $minute The minute part to create timestamp of.
* @param int $second The second part to create timestamp of.
* @param int $timezone ?
* @return ?
* @todo Finish documenting this function
*/
function make_timestamp($year, $month=1, $day=1, $hour=0, $minute=0, $second=0, $timezone=99, $applydst=true) {
$timezone = get_user_timezone($timezone);
if (abs($timezone) > 13) {
$time = mktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year, 0);
} else {
$time = gmmktime((int)$hour,(int)$minute,(int)$second,(int)$month,(int)$day,(int)$year, 0);
$time = usertime($time, $timezone);
}
if($applydst) {
$time -= dst_offset_on($time);
}
return $time;
}
/**
* Given an amount of time in seconds, returns string
* formatted nicely as months, days, hours etc as needed
*
* @uses MINSECS
* @uses HOURSECS
* @uses DAYSECS
* @param int $totalsecs ?
* @param array $str ?
* @return string
* @todo Finish documenting this function
*/
function format_time($totalsecs, $str=NULL) {
$totalsecs = abs($totalsecs);
if (!$str) { // Create the str structure the slow way
$str->day = get_string('day');
$str->days = get_string('days');
$str->hour = get_string('hour');
$str->hours = get_string('hours');
$str->min = get_string('min');
$str->mins = get_string('mins');
$str->sec = get_string('sec');
$str->secs = get_string('secs');
}
$days = floor($totalsecs/DAYSECS);
$remainder = $totalsecs - ($days*DAYSECS);
$hours = floor($remainder/HOURSECS);
$remainder = $remainder - ($hours*HOURSECS);
$mins = floor($remainder/MINSECS);
$secs = $remainder - ($mins*MINSECS);
$ss = ($secs == 1) ? $str->sec : $str->secs;
$sm = ($mins == 1) ? $str->min : $str->mins;
$sh = ($hours == 1) ? $str->hour : $str->hours;
$sd = ($days == 1) ? $str->day : $str->days;
$odays = '';
$ohours = '';
$omins = '';
$osecs = '';
if ($days) $odays = $days .' '. $sd;
if ($hours) $ohours = $hours .' '. $sh;
if ($mins) $omins = $mins .' '. $sm;
if ($secs) $osecs = $secs .' '. $ss;
if ($days) return $odays .' '. $ohours;
if ($hours) return $ohours .' '. $omins;
if ($mins) return $omins .' '. $osecs;
if ($secs) return $osecs;
return get_string('now');
}
/**
* Returns a formatted string that represents a date in user time
* <b>WARNING: note that the format is for strftime(), not date().</b>
* Because of a bug in most Windows time libraries, we can't use
* the nicer %e, so we have to use %d which has leading zeroes.
* A lot of the fuss in the function is just getting rid of these leading
* zeroes as efficiently as possible.
*
* If parameter fixday = true (default), then take off leading
* zero from %d, else mantain it.
*
* @uses HOURSECS
* @param int $date ?
* @param string $format ?
* @param int $timezone ?
* @param boolean $fixday If true (default) then the leading
* zero from %d is removed. If false then the leading zero is mantained.
* @return string
* @todo Finish documenting this function
*/
function userdate($date, $format='', $timezone=99, $fixday = true) {
if ($format == '') {
$format = get_string('strftimedaydatetime');
}
$formatnoday = str_replace('%d', 'DD', $format);
if ($fixday) {
$fixday = ($formatnoday != $format);
}
$date += dst_offset_on($date);
$timezone = get_user_timezone($timezone);
if (abs($timezone) > 13) { /// Server time
if ($fixday) {
$datestring = strftime($formatnoday, $date);
$daystring = str_replace(' 0', '', strftime(' %d', $date));
$datestring = str_replace('DD', $daystring, $datestring);
} else {
$datestring = strftime($format, $date);
}
} else {
$date += (int)($timezone * 3600);
if ($fixday) {
$datestring = gmstrftime($formatnoday, $date);
$daystring = str_replace(' 0', '', gmstrftime(' %d', $date));
$datestring = str_replace('DD', $daystring, $datestring);
} else {
$datestring = gmstrftime($format, $date);
}
}
return $datestring;
}
/**
* Given a $time timestamp in GMT (seconds since epoch),
* returns an array that represents the date in user time
*
* @uses HOURSECS
* @param int $time Timestamp in GMT
* @param int $timezone ?
* @return array An array that represents the date in user time
* @todo Finish documenting this function
*/
function usergetdate($time, $timezone=99) {
$timezone = get_user_timezone($timezone);
$time += dst_offset_on($time);
if (abs($timezone) > 13) {
$time += intval((float)date('O') * HOURSECS);
}
else {
$time += intval((float)$timezone * HOURSECS);
}
// This is independent of the server's TZ settings,
// unlike gmstrftime. It's also a bit faster this way.
list(
$getdate['seconds'],
$getdate['minutes'],
$getdate['hours'],
$getdate['mday'],
$getdate['mon'],
$getdate['year'],
$getdate['wday'],
$getdate['yday'],
$getdate['weekday'],
$getdate['month']
) = explode(' ', gmdate('s i H d m Y w z l F', $time));
return $getdate;
}
/**
* Given a GMT timestamp (seconds since epoch), offsets it by
* the timezone. eg 3pm in India is 3pm GMT - 7 * 3600 seconds
*
* @uses HOURSECS
* @param int $date Timestamp in GMT
* @param int $timezone ?
* @return int
* @todo Finish documenting this function
*/
function usertime($date, $timezone=99) {
$timezone = get_user_timezone($timezone);
if (abs($timezone) > 13) {
return $date;
}
return $date - (int)($timezone * HOURSECS);
}
/**
* Given a time, return the GMT timestamp of the most recent midnight
* for the current user.
*
* @param int $date Timestamp in GMT
* @param int $timezone ?
* @return ?
* @todo Finish documenting this function. Is timezone an int or float?
*/
function usergetmidnight($date, $timezone=99) {
$timezone = get_user_timezone($timezone);
$userdate = usergetdate($date, $timezone);
// Time of midnight of this user's day, in GMT
return make_timestamp($userdate['year'], $userdate['mon'], $userdate['mday'], 0, 0, 0, $timezone);
}
/**
* Returns a string that prints the user's timezone
*
* @param float $timezone The user's timezone
* @return string
* @todo is $timezone an int or a float?
*/
function usertimezone($timezone=99) {
$timezone = get_user_timezone($timezone);
if (abs($timezone) > 13) {
return 'server time';
}
if (abs($timezone) < 0.5) {
return 'GMT';
}
if ($timezone > 0) {
return 'GMT+'. $timezone;
} else {
return 'GMT'. $timezone;
}
}
/**
* Returns a float which represents the user's timezone difference from GMT in hours
* Checks various settings and picks the most dominant of those which have a value
*
* @uses $CFG
* @uses $USER
* @param int $tz The user's timezone
* @return int
* @todo is $tz an int or a float?
*/
function get_user_timezone($tz = 99) {
// Variables declared explicitly global here so that if we add
// something later we won't forget to global it...
$timezones = array(
isset($GLOBALS['USER']->timezone) ? $GLOBALS['USER']->timezone : 99,
isset($GLOBALS['CFG']->timezone) ? $GLOBALS['CFG']->timezone : 99,
);
while($tz == 99 && $next = each($timezones)) {
$tz = (float)$next['value'];
}
return $tz;
}
function get_user_dst_preset() {
global $CFG, $USER;
static $preset = NULL;
if ($preset !== NULL) {
return $preset;
}
if (empty($CFG->calendar_dstforusers)) {
if (empty($USER)) {
return NULL;
}
$presetid = $USER->dstpreset;
} else {
$presetid = $CFG->calendar_dstforusers;
}
$preset = get_record('dst_preset', 'id', $presetid);
if (!empty($preset) && (time() >= $preset->next_change || time() < $preset->last_change)) {
$preset = dst_update_preset($preset);
update_record('dst_preset', $preset);
}
return $preset;
}
function dst_update_preset($dstpreset) {
// What's the date according to our user right now?
$now = time();
$date = usergetdate($now);
$changes = dst_changes_for_year($date['year'], $dstpreset);
// Great... let's see where exactly we are now.
if ($now < $changes['activate']) {
// DST has not been turned on this year
$dstpreset->next_change = $changes['activate'];
// For the last change, we need to fetch the previous year's DST deactivation timestamp
$prevchanges = dst_changes_for_year($date['year'] - 1, $dstpreset);
$dstpreset->last_change = $prevchanges['deactivate'];
$dstpreset->current_offset = 0;
} else if($now < $changes['deactivate']) {
// DST is on for this year right now
$dstpreset->last_change = $changes['activate'];
$dstpreset->next_change = $changes['deactivate'];
$dstpreset->current_offset = $dstpreset->apply_offset;
} else {
// DST has already been turned off; we are nearing the end of the year
$dstpreset->last_change = $changes['deactivate'];
// For the next change, we need to fetch next year's DST activation timestamp
$nextchanges = dst_changes_for_year($date['year'] + 1, $dstpreset);
$dstpreset->next_change = $nextchanges['activate'];
$dstpreset->current_offset = 0;
}
return $dstpreset;
}
function dst_changes_for_year($year, $dstpreset) {
$monthdayactivate = find_day_in_month($dstpreset->activate_index, $dstpreset->activate_day, $dstpreset->activate_month, $year);
$monthdaydeactivate = find_day_in_month($dstpreset->deactivate_index, $dstpreset->deactivate_day, $dstpreset->deactivate_month, $year);
if (isset($dstpreset->activate_time)) {
list($activate_hour, $activate_minute) = explode(':', $dstpreset->activate_time);
} else {
$activate_hour = $dstpreset->activate_hour;
$activate_minute = $dstpreset->activate_minute;
}
if (isset($dstpreset->deactivate_time)) {
list($deactivate_hour, $deactivate_minute) = explode(':', $dstpreset->deactivate_time);
} else {
$deactivate_hour = $dstpreset->deactivate_hour;
$deactivate_minute = $dstpreset->deactivate_minute;
}
$timezone = get_user_timezone(99);
$timeactivate = make_timestamp($year, $dstpreset->activate_month, $monthdayactivate, $activate_hour, $activate_minute, 0, $timezone, false);
$timedeactivate = make_timestamp($year, $dstpreset->deactivate_month, $monthdaydeactivate, $deactivate_hour, $deactivate_minute, 0, $timezone, false);
return array('activate' => $timeactivate, 0 => $timeactivate, 'deactivate' => $timedeactivate, 1 => $timedeactivate);
}
// $time must NOT be compensated at all, it has to be a pure timestamp
function dst_offset_on($time) {
$preset = get_user_dst_preset();
if (empty($preset)) {
return 0;
}
if ($time >= $preset->last_change && $time < $preset->next_change) {
return $preset->current_offset * MINSECS;
}
// It's in some other time period, we need to recalculate
$changes = dst_changes_for_year(gmdate('Y', $time), $preset);
if ($time >= $changes['activate'] && $time < $changes['deactivate']) {
// Compensation required
return $preset->apply_offset * MINSECS;
} else {
return 0;
}
}
// "Find the ($index as int, 1st, 2nd, etc, -1 = last) ($weekday as int, sunday = 0) in ($month) of ($year)"
function find_day_in_month($index, $weekday, $month, $year) {
if($weekday == -1) {
// Any day of the week will do
if($index == -1) {
// Last day of that month
$targetday = days_in_month($month, $year);
}
else {
// Not last day; a straight index value
$targetday = $index;
}
}
else {
// We need to calculate when exactly that weekday is
// Fist of all, what day of the week is the first of that month?
$firstmonthweekday = strftime('%w', mktime(12, 0, 0, $month, 1, $year, 0));
$daysinmonth = days_in_month($month, $year);
// This is the first such-named weekday of the month
$targetday = 1 + $weekday - $firstmonthweekday;
if($targetday <= 0) {
$targetday += 7;
}
if($index == -1) {
// To find the LAST such weekday, just keep adding 7 days at a time
while($targetday + 7 <= $daysinmonth) {
$targetday += 7;
}
}
else {
// For a specific week, add as many weeks as required
$targetday += $index > 1 ? ($index - 1) * 7 : 0;
}
}
return $targetday;
}
function days_in_month($month, $year) {
return intval(date('t', mktime(12, 0, 0, $month, 1, $year, 0)));
}
/// USER AUTHENTICATION AND LOGIN ////////////////////////////////////////
// Makes sure that $USER->sesskey exists, if $USER itself exists. It sets a new sesskey
// if one does not already exist, but does not overwrite existing sesskeys. Returns the
// sesskey string if $USER exists, or boolean false if not.
function sesskey() {
global $USER;
if(!isset($USER)) {
return false;
}
if (empty($USER->sesskey)) {
$USER->sesskey = random_string(10);
}
return $USER->sesskey;
}
/**
* This function checks that the current user is logged in and has the
* required privileges
*
* This function checks that the current user is logged in, and optionally
* whether they are allowed to be in a particular course and view a particular
* course module.
* If they are not logged in, then it redirects them to the site login unless
* $autologinguest is set and {@link $CFG}->autologinguests is set to 1 in which
* case they are automatically logged in as guests.
* If $courseid is given and the user is not enrolled in that course then the
* user is redirected to the course enrolment page.
* If $cm is given and the coursemodule is hidden and the user is not a teacher
* in the course then the user is redirected to the course home page.
*
* @uses $CFG
* @uses $SESSION
* @uses $USER
* @uses $FULLME
* @uses SITEID
* @uses $MoodleSession
* @param int $courseid id of the course
* @param boolean $autologinguest
* @param $cm course module object
*/
function require_login($courseid=0, $autologinguest=true, $cm=null) {
global $CFG, $SESSION, $USER, $FULLME, $MoodleSession;
// First check that the user is logged in to the site.
if (! (isset($USER->loggedin) and $USER->confirmed and ($USER->site == $CFG->wwwroot)) ) { // They're not
$SESSION->wantsurl = $FULLME;
if (!empty($_SERVER['HTTP_REFERER'])) {
$SESSION->fromurl = $_SERVER['HTTP_REFERER'];
}
$USER = NULL;
if ($autologinguest and $CFG->autologinguests and $courseid and get_field('course','guest','id',$courseid)) {
$loginguest = '?loginguest=true';
} else {
$loginguest = '';
}
if (empty($CFG->loginhttps)) {
redirect($CFG->wwwroot .'/login/index.php'. $loginguest);
} else {
$wwwroot = str_replace('http','https', $CFG->wwwroot);
redirect($wwwroot .'/login/index.php'. $loginguest);
}
exit;
}
// check whether the user should be changing password
// reload_user_preferences(); // Why is this necessary? Seems wasteful. - MD
if (!empty($USER->preference['auth_forcepasswordchange'])){
if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
$SESSION->wantsurl = $FULLME;
redirect($CFG->wwwroot .'/login/change_password.php');
} elseif($CFG->changepassword) {
redirect($CFG->changepassword);
} else {
error('You cannot proceed without changing your password.
However there is no available page for changing it.
Please contact your Moodle Administrator.');
}
}
// Check that the user account is properly set up
if (user_not_fully_set_up($USER)) {
$SESSION->wantsurl = $FULLME;
redirect($CFG->wwwroot .'/user/edit.php?id='. $USER->id .'&amp;course='. SITEID);
}
// Make sure current IP matches the one for this session (if required)
if (!empty($CFG->tracksessionip)) {
if ($USER->sessionIP != md5(getremoteaddr())) {
error(get_string('sessionipnomatch', 'error'));
}
}
// Make sure the USER has a sesskey set up. Used for checking script parameters.
sesskey();
// Check that the user has agreed to a site policy if there is one
if (!empty($CFG->sitepolicy)) {
if (!$USER->policyagreed) {
$SESSION->wantsurl = $FULLME;
redirect($CFG->wwwroot .'/user/policy.php');
}
}
// If the site is currently under maintenance, then print a message
if (!isadmin()) {
if (file_exists($CFG->dataroot.'/1/maintenance.html')) {
print_maintenance_message();
exit;
}
}
// Next, check if the user can be in a particular course
if ($courseid) {
if ($courseid == SITEID) { // Anyone can be in the site course
if (isset($cm) and !$cm->visible and !isteacher(SITEID)) { // Not allowed to see module, send to course page
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
}
return;
}
if (!empty($USER->student[$courseid]) or !empty($USER->teacher[$courseid]) or !empty($USER->admin)) {
if (isset($USER->realuser)) { // Make sure the REAL person can also access this course
if (!isteacher($courseid, $USER->realuser)) {
print_header();
notice(get_string('studentnotallowed', '', fullname($USER, true)), $CFG->wwwroot .'/');
}
}
if (isset($cm) and !$cm->visible and !isteacher($courseid)) { // Not allowed to see module, send to course page
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
}
return; // user is a member of this course.
}
if (! $course = get_record('course', 'id', $courseid)) {
error('That course doesn\'t exist');
}
if (!$course->visible) {
print_header();
notice(get_string('coursehidden'), $CFG->wwwroot .'/');
}
if ($USER->username == 'guest') {
switch ($course->guest) {
case 0: // Guests not allowed
print_header();
notice(get_string('guestsnotallowed', '', $course->fullname), "$CFG->wwwroot/login/index.php");
break;
case 1: // Guests allowed
if (isset($cm) and !$cm->visible) { // Not allowed to see module, send to course page
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
}
return;
case 2: // Guests allowed with key (drop through)
break;
}
}
//User is not enrolled in the course, wants to access course content
//as a guest, and course setting allow unlimited guest access
//Code cribbed from course/loginas.php
if (strstr($FULLME,"username=guest") && ($course->guest==1)) {
$realuser = $USER->id;
$realname = fullname($USER, true);
$USER = guest_user();
$USER->loggedin = true;
$USER->site = $CFG->wwwroot;
$USER->realuser = $realuser;
if (isset($SESSION->currentgroup[$course->id])) { // Remember current setting for later
$SESSION->oldcurrentgroup = $SESSION->currentgroup[$course->id];
unset($SESSION->currentgroup[$course->id]);
}
$guest_name = fullname($USER, true);
add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&$USER->id$", "$realname -> $guest_name");
if (isset($cm) and !$cm->visible) { // Not allowed to see module, send to course page
redirect($CFG->wwwroot.'/course/view.php?id='.$cm->course, get_string('activityiscurrentlyhidden'));
}
return;
}
// Currently not enrolled in the course, so see if they want to enrol
$SESSION->wantsurl = $FULLME;
redirect($CFG->wwwroot .'/course/enrol.php?id='. $courseid);
die;
}
}
/**
* This is a weaker version of {@link require_login()} which only requires login
* when called from within a course rather than the site page, unless
* the forcelogin option is turned on.
*
* @uses $CFG
* @param int $courseid The course in question
* @param boolean $autologinguest Allow autologin guests if that is wanted
*/
function require_course_login($course, $autologinguest=true, $cm=null) {
global $CFG;
if ($CFG->forcelogin) {
require_login();
}
if ($course->category) {
require_login($course->id, $autologinguest, $cm);
}
}
/**
* Modify the user table by setting the currently logged in user's
* last login to now.
*
* @uses $USER
* @return boolean
*/
function update_user_login_times() {
global $USER;
$USER->lastlogin = $user->lastlogin = $USER->currentlogin;
$USER->currentlogin = $user->lastaccess = $user->currentlogin = time();
$user->id = $USER->id;
return update_record('user', $user);
}
/**
* Determines if a user has completed setting up their account.
*
* @param user $user A {@link $USER} object to test for the existance of a valid name and email
* @return boolean
*/
function user_not_fully_set_up($user) {
return ($user->username != 'guest' and (empty($user->firstname) or empty($user->lastname) or empty($user->email) or over_bounce_threshold($user)));
}
function over_bounce_threshold($user) {
global $CFG;
if (empty($CFG->handlebounces)) {
return false;
}
// set sensible defaults
if (empty($CFG->minbounces)) {
$CFG->minbounces = 10;
}
if (empty($CFG->bounceratio)) {
$CFG->bounceratio = .20;
}
$bouncecount = 0;
$sendcount = 0;
if ($bounce = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
$bouncecount = $bounce->value;
}
if ($send = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
$sendcount = $send->value;
}
return ($bouncecount >= $CFG->minbounces && $bouncecount/$sendcount >= $CFG->bounceratio);
}
/**
* @param $user - object containing an id
* @param $reset - will reset the count to 0
*/
function set_send_count($user,$reset=false) {
if ($pref = get_record('user_preferences','userid',$user->id,'name','email_send_count')) {
$pref->value = (!empty($reset)) ? 0 : $pref->value+1;
update_record('user_preferences',$pref);
}
else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
// make a new one
$pref->name = 'email_send_count';
$pref->value = 1;
$pref->userid = $user->id;
insert_record('user_preferences',$pref);
}
}
/**
* @param $user - object containing an id
* @param $reset - will reset the count to 0
*/
function set_bounce_count($user,$reset=false) {
if ($pref = get_record('user_preferences','userid',$user->id,'name','email_bounce_count')) {
$pref->value = (!empty($reset)) ? 0 : $pref->value+1;
update_record('user_preferences',$pref);
}
else if (!empty($reset)) { // if it's not there and we're resetting, don't bother.
// make a new one
$pref->name = 'email_bounce_count';
$pref->value = 1;
$pref->userid = $user->id;
insert_record('user_preferences',$pref);
}
}
/**
* Keeps track of login attempts
*
* @uses $SESSION
*/
function update_login_count() {
global $SESSION;
$max_logins = 10;
if (empty($SESSION->logincount)) {
$SESSION->logincount = 1;
} else {
$SESSION->logincount++;
}
if ($SESSION->logincount > $max_logins) {
unset($SESSION->wantsurl);
error(get_string('errortoomanylogins'));
}
}
/**
* Resets login attempts
*
* @uses $SESSION
*/
function reset_login_count() {
global $SESSION;
$SESSION->logincount = 0;
}
/**
* check_for_restricted_user
*
* @uses $CFG
* @uses $USER
* @param string $username ?
* @param string $redirect ?
* @todo Finish documenting this function
*/
function check_for_restricted_user($username=NULL, $redirect='') {
global $CFG, $USER;
if (!$username) {
if (!empty($USER->username)) {
$username = $USER->username;
} else {
return false;
}
}
if (!empty($CFG->restrictusers)) {
$names = explode(',', $CFG->restrictusers);
if (in_array($username, $names)) {
error(get_string('restricteduser', 'error', fullname($USER)), $redirect);
}
}
}
function sync_metacourses() {
global $CFG;
if (!$courses = get_records_sql("SELECT DISTINCT parent_course,1 FROM {$CFG->prefix}course_meta")) {
return;
}
foreach ($courses as $course) {
sync_metacourse($course->parent_course);
}
}
/**
* Goes through all enrolment records for the courses inside the metacourse and sync with them.
*/
function sync_metacourse($metacourseid) {
global $CFG,$db;
if (!$metacourse = get_record("course","id",$metacourseid)) {
return false;
}
if (count_records('course_meta','parent_course',$metacourseid) == 0) { // if there are no child courses for this meta course, nuke the enrolments
if ($enrolments = get_records('user_students','course',$metacourseid,'','userid,1')) {
foreach ($enrolments as $enrolment) {
unenrol_student($enrolment->userid,$metacourseid);
}
}
return true;
}
// this will return a list of userids from user_student for enrolments in the metacourse that shouldn't be there.
$sql = "SELECT parent.userid,max(child.course) as course
FROM {$CFG->prefix}course_meta meta
JOIN {$CFG->prefix}user_students parent
ON meta.parent_course = parent.course
LEFT OUTER JOIN {$CFG->prefix}user_students child
ON child.course = meta.child_course
AND child.userid = parent.userid
WHERE meta.parent_course = $metacourseid
GROUP BY child.course,parent.userid
ORDER BY parent.userid,child.course";
$res = $db->Execute($sql);
//iterate results
$enrolmentstodelete = array();
while( !$res->EOF && isset($res->fields) ) {
$enrolmentstodelete[] = $res->fields;
$res->MoveNext();
}
if (!empty($enrolmentstodelete)) {
$last->id = 0;
$last->course = 0;
foreach ($enrolmentstodelete as $enrolment) {
$enrolment = (object)$enrolment;
if (count($enrolmentstodelete) == 1 && empty($enrolment->course)) {
unenrol_student($enrolment->userid,$metacourseid);
break;
}
if ($last->id != $enrolment->userid) { // we've changed
if (empty($last->course) && !empty($last->id)) {
unenrol_student($last->id,$metacourseid); // doing it this way for forum subscriptions etc.
}
$last->course = 0;
$last->id = $enrolment->userid;
}
if (!empty($enrolment->course)) {
$last->course = $enrolment->course;
}
}
if (!empty($last->id) && empty($last->course)) {
unenrol_student($last->id,$metacourseid); // doing it this way for forum subscriptions etc.
}
}
// this will return a list of userids that need to be enrolled in the metacourse
$sql = "SELECT DISTINCT child.userid,1
FROM {$CFG->prefix}course_meta meta
JOIN {$CFG->prefix}user_students child
ON meta.child_course = child.course
LEFT OUTER JOIN {$CFG->prefix}user_students parent
ON meta.parent_course = parent.course
AND parent.userid = child.userid
WHERE parent.course IS NULL
AND meta.parent_course = $metacourseid";
if ($userstoadd = get_records_sql($sql)) {
foreach ($userstoadd as $user) {
enrol_student($user->userid,$metacourseid);
}
}
// and next make sure that we have the right start time and end time (ie max and min) for them all.
if ($enrolments = get_records('user_students','course',$metacourseid,'','id,userid')) {
foreach ($enrolments as $enrol) {
if ($maxmin = get_record_sql("SELECT min(timestart) AS timestart, max(timeend) AS timeend
FROM {$CFG->prefix}user_students u JOIN {$CFG->prefix}course_meta mc ON u.course = mc.child_course WHERE userid = $enrol->userid
AND mc.parent_course = $metacourseid")) {
$enrol->timestart = $maxmin->timestart;
$enrol->timeend = $maxmin->timeend;
update_record('user_students',$enrol);
}
}
}
return true;
}
/**
* Adds a record to the metacourse table and calls sync_metacoures
*/
function add_to_metacourse ($metacourseid, $courseid) {
if (!$metacourse = get_record("course","id",$metacourseid)) {
return false;
}
if (!$course = get_record("course","id",$courseid)) {
return false;
}
if (!$record = get_record("course_meta","parent_course",$metacourseid,"child_course",$courseid)) {
$rec->parent_course = $metacourseid;
$rec->child_course = $courseid;
if (!insert_record('course_meta',$rec)) {
return false;
}
return sync_metacourse($metacourseid);
}
return true;
}
/**
* Removes the record from the metacourse table and calls sync_metacourse
*/
function remove_from_metacourse($metacourseid, $courseid) {
if (delete_records('course_meta','parent_course',$metacourseid,'child_course',$courseid)) {
return sync_metacourse($metacourseid);
}
return false;
}
/**
* Determines if a user an admin
*
* @uses $USER
* @param int $userid The id of the user as is found in the 'user' table
* @staticvar array $admin ?
* @staticvar array $nonadmins ?
* @return boolean
* @todo Complete documentation for this function
*/
function isadmin($userid=0) {
global $USER;
static $admins = array();
static $nonadmins = array();
if (!$userid){
if (empty($USER->id)) {
return false;
}
$userid = $USER->id;
}
if (in_array($userid, $admins)) {
return true;
} else if (in_array($userid, $nonadmins)) {
return false;
} else if (record_exists('user_admins', 'userid', $userid)){
$admins[] = $userid;
return true;
} else {
$nonadmins[] = $userid;
return false;
}
}
/**
* Determines if a user is a teacher or an admin
*
* @uses $USER
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param boolean $includeadmin If true this function will return true when it encounters an admin user.
* @return boolean
* @todo Finish documenting this function
*/
function isteacher($courseid=0, $userid=0, $includeadmin=true) {
/// Is the user a teacher or admin?
global $USER, $CFG;
if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can
return true;
}
if (empty($courseid)) {
if (isadmin() or $CFG->debug > 7) {
notify('Coding error: isteacher() should not be used without a valid course id as argument. Please notify a developer.');
}
return isteacherinanycourse($userid, $includeadmin);
}
if (!$userid) {
if ($courseid) {
return !empty($USER->teacher[$courseid]);
}
if (!isset($USER->id)) {
return false;
}
$userid = $USER->id;
}
return record_exists('user_teachers', 'userid', $userid, 'course', $courseid);
}
/**
* Determines if a user is a teacher in any course, or an admin
*
* @uses $USER
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param boolean $includeadmin If true this function will return true when it encounters an admin user.
* @return boolean
* @todo Finish documenting this function
*/
function isteacherinanycourse($userid = 0, $includeadmin = true) {
global $USER;
if(empty($userid)) {
if(empty($USER) || empty($USER->id)) {
return false;
}
$userid = $USER->id;
}
if (isadmin($userid) && $includeadmin) { // admins can do anything
return true;
}
return record_exists('user_teachers', 'userid', $userid);
}
/**
* Determines if a user is allowed to edit a given course
*
* @uses $USER
* @param int $courseid The id of the course that is being edited
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @return boolean
*/
function isteacheredit($courseid, $userid=0) {
global $USER;
if (isadmin($userid)) { // admins can do anything
return true;
}
if (!$userid) {
return !empty($USER->teacheredit[$courseid]);
}
return get_field('user_teachers', 'editall', 'userid', $userid, 'course', $courseid);
}
/**
* Determines if a user can create new courses
*
* @uses $USER
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return boolean
*/
function iscreator ($userid=0) {
global $USER;
if (empty($USER->id)) {
return false;
}
if (isadmin($userid)) { // admins can do anything
return true;
}
if (empty($userid)) {
return record_exists('user_coursecreators', 'userid', $USER->id);
}
return record_exists('user_coursecreators', 'userid', $userid);
}
/**
* Determines if a user is a student in the specified course
*
* If the course id specifies the site then the function determines
* if the user is a confirmed and valid user of this site.
*
* @uses $USER
* @uses $CFG
* @uses SITEID
* @param int $courseid The id of the course being tested
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return boolean
*/
function isstudent($courseid, $userid=0) {
global $USER, $CFG;
if (empty($USER->id) and !$userid) {
return false;
}
if ($courseid == SITEID) {
if (!$userid) {
$userid = $USER->id;
}
if (isguest($userid)) {
return false;
}
// a site teacher can never be a site student
if (isteacher($courseid, $userid)) {
return false;
}
if ($CFG->allusersaresitestudents) {
return record_exists('user', 'id', $userid);
} else {
return (record_exists('user_students', 'userid', $userid)
or record_exists('user_teachers', 'userid', $userid));
}
}
if (!$userid) {
return !empty($USER->student[$courseid]);
}
// $timenow = time(); // todo: add time check below
return record_exists('user_students', 'userid', $userid, 'course', $courseid);
}
/**
* Determines if the specified user is logged in as guest.
*
* @uses $USER
* @param int $userid The user being tested. You can set this to 0 or leave it blank to test the currently logged in user.
* @return boolean
*/
function isguest($userid=0) {
global $USER;
if (!$userid) {
if (empty($USER->username)) {
return false;
}
return ($USER->username == 'guest');
}
return record_exists('user', 'id', $userid, 'username', 'guest');
}
/**
* Determines if the currently logged in user is in editing mode
*
* @uses $USER
* @param int $courseid The id of the course being tested
* @param user $user A {@link $USER} object. If null then the currently logged in user is used.
* @return boolean
*/
function isediting($courseid, $user=NULL) {
global $USER;
if (!$user){
$user = $USER;
}
if (empty($user->editing)) {
return false;
}
return ($user->editing and isteacher($courseid, $user->id));
}
/**
* Determines if the logged in user is currently moving an activity
*
* @uses $USER
* @param int $courseid The id of the course being tested
* @return boolean
*/
function ismoving($courseid) {
global $USER;
if (!empty($USER->activitycopy)) {
return ($USER->activitycopycourse == $courseid);
}
return false;
}
/**
* Given an object containing firstname and lastname
* values, this function returns a string with the
* full name of the person.
* The result may depend on system settings
* or language. 'override' will force both names
* to be used even if system settings specify one.
* @uses $CFG
* @uses $SESSION
* @param type description
* @todo Finish documenting this function
*/
function fullname($user, $override=false) {
global $CFG, $SESSION;
if (!isset($user->firstname) and !isset($user->lastname)) {
return '';
}
if (!empty($SESSION->fullnamedisplay)) {
$CFG->fullnamedisplay = $SESSION->fullnamedisplay;
}
if ($CFG->fullnamedisplay == 'firstname lastname') {
return $user->firstname .' '. $user->lastname;
} else if ($CFG->fullnamedisplay == 'lastname firstname') {
return $user->lastname .' '. $user->firstname;
} else if ($CFG->fullnamedisplay == 'firstname') {
if ($override) {
return get_string('fullnamedisplay', '', $user);
} else {
return $user->firstname;
}
}
return get_string('fullnamedisplay', '', $user);
}
/**
* Sets a moodle cookie with an encrypted string
*
* @uses $CFG
* @uses DAYSECS
* @uses HOURSECS
* @param string $thing The string to encrypt and place in a cookie
*/
function set_moodle_cookie($thing) {
global $CFG;
$cookiename = 'MOODLEID_'.$CFG->sessioncookie;
$days = 60;
$seconds = DAYSECS*$days;
setCookie($cookiename, '', time() - HOURSECS, '/');
setCookie($cookiename, rc4encrypt($thing), time()+$seconds, '/');
}
/**
* Gets a moodle cookie with an encrypted string
*
* @uses $CFG
* @return string
*/
function get_moodle_cookie() {
global $CFG;
$cookiename = 'MOODLEID_'.$CFG->sessioncookie;
if (empty($_COOKIE[$cookiename])) {
return '';
} else {
return rc4decrypt($_COOKIE[$cookiename]);
}
}
/**
* Returns true if an internal authentication method is being used.
* if method not specified then, global default is assumed
*
* @uses $CFG
* @param string $auth Form of authentication required
* @return boolean
* @todo Outline auth types and provide code example
*/
function is_internal_auth($auth='') {
/// Returns true if an internal authentication method is being used.
/// If auth not specified then global default is assumed
global $CFG;
if (empty($auth)) {
$auth = $CFG->auth;
}
return ($auth == "email" || $auth == "none" || $auth == "manual");
}
/**
* Returns an array of user fields
*
* @uses $CFG
* @uses $db
* @return array User field/column names
* @todo Finish documenting this function
*/
function get_user_fieldnames() {
global $CFG, $db;
$fieldarray = $db->MetaColumnNames($CFG->prefix.'user');
unset($fieldarray['ID']);
return $fieldarray;
}
/**
* Creates a bare-bones user record
*
* @uses $CFG
* @param string $username New user's username to add to record
* @param string $password New user's password to add to record
* @param string $auth Form of authentication required
* @return user A {@link $USER} object
* @todo Outline auth types and provide code example
*/
function create_user_record($username, $password, $auth='') {
global $CFG;
//just in case check text case
$username = trim(moodle_strtolower($username));
if (function_exists('auth_get_userinfo')) {
if ($newinfo = auth_get_userinfo($username)) {
$newinfo = truncate_userinfo($newinfo);
foreach ($newinfo as $key => $value){
$newuser->$key = addslashes(stripslashes($value)); // Just in case
}
}
}
if (!empty($newuser->email)) {
if (email_is_not_allowed($newuser->email)) {
unset($newuser->email);
}
}
$newuser->auth = (empty($auth)) ? $CFG->auth : $auth;
$newuser->username = $username;
$newuser->password = md5($password);
$newuser->lang = $CFG->lang;
$newuser->confirmed = 1;
$newuser->lastIP = getremoteaddr();
$newuser->timemodified = time();
if (insert_record('user', $newuser)) {
$user = get_user_info_from_db('username', $newuser->username);
if($CFG->{'auth_'.$newuser->auth.'_forcechangepassword'}){
set_user_preference('auth_forcepasswordchange', 1, $user);
}
return $user;
}
return false;
}
/**
* Will update a local user record from an external source
*
* @uses $CFG
* @param string $username New user's username to add to record
* @return user A {@link $USER} object
*/
function update_user_record($username) {
global $CFG;
if (function_exists('auth_get_userinfo')) {
$username = trim(moodle_strtolower($username)); /// just in case check text case
if ($newinfo = auth_get_userinfo($username)) {
foreach ($newinfo as $key => $value){
if (!empty($CFG->{'auth_user_' . $key. '_updatelocal'})) {
$value = addslashes(stripslashes($value)); // Just in case
set_field('user', $key, $value, 'username', $username);
}
}
}
}
return get_user_info_from_db('username', $username);
}
function truncate_userinfo($info) {
/// will truncate userinfo as it comes from auth_get_userinfo (from external auth)
/// which may have large fields
// define the limits
$limit = array(
'username' => 100,
'idnumber' => 64,
'firstname' => 20,
'lastname' => 20,
'email' => 100,
'icq' => 15,
'phone1' => 20,
'phone2' => 20,
'institution' => 40,
'department' => 30,
'address' => 70,
'city' => 20,
'country' => 2,
'url' => 255,
);
// apply where needed
foreach (array_keys($info) as $key) {
if (!empty($limit[$key])) {
$info[$key] = trim(substr($info[$key],0, $limit[$key]));
}
}
return $info;
}
/**
* Retrieve the guest user object
*
* @uses $CFG
* @return user A {@link $USER} object
*/
function guest_user() {
global $CFG;
if ($newuser = get_record('user', 'username', 'guest')) {
$newuser->loggedin = true;
$newuser->confirmed = 1;
$newuser->site = $CFG->wwwroot;
$newuser->lang = $CFG->lang;
$newuser->lastIP = getremoteaddr();
}
return $newuser;
}
/**
* Given a username and password, this function looks them
* up using the currently selected authentication mechanism,
* and if the authentication is successful, it returns a
* valid $user object from the 'user' table.
*
* Uses auth_ functions from the currently active auth module
*
* @uses $CFG
* @param string $username User's username
* @param string $password User's password
* @return user|flase A {@link $USER} object or false if error
*/
function authenticate_user_login($username, $password) {
global $CFG;
$md5password = md5($password);
// First try to find the user in the database
if (!$user = get_user_info_from_db('username', $username)) {
$user->id = 0; // Not a user
$user->auth = $CFG->auth;
}
// Sort out the authentication method we are using.
if (empty($CFG->auth)) {
$CFG->auth = 'manual'; // Default authentication module
}
if (empty($user->auth)) { // For some reason it isn't set yet
if (isadmin($user->id) or isguest($user->id)) {
$auth = 'manual'; // Always assume these guys are internal
} else {
$auth = $CFG->auth; // Normal users default to site method
}
// update user record from external DB
if ($user->auth != 'manual' && $user->auth != 'email') {
$user = update_user_record($username);
}
} else {
$auth = $user->auth;
}
if (detect_munged_arguments($auth, 0)) { // For safety on the next require
return false;
}
if (!file_exists($CFG->dirroot .'/auth/'. $auth .'/lib.php')) {
$auth = 'manual'; // Can't find auth module, default to internal
}
require_once($CFG->dirroot .'/auth/'. $auth .'/lib.php');
if (auth_user_login($username, $password)) { // Successful authentication
if ($user->id) { // User already exists in database
if (empty($user->auth)) { // For some reason auth isn't set yet
set_field('user', 'auth', $auth, 'username', $username);
}
if ($md5password <> $user->password) { // Update local copy of password for reference
set_field('user', 'password', $md5password, 'username', $username);
}
if (!is_internal_auth()) { // update user record from external DB
$user = update_user_record($username);
}
} else {
$user = create_user_record($username, $password, $auth);
}
if (function_exists('auth_iscreator')) { // Check if the user is a creator
$useriscreator = auth_iscreator($username);
if (!is_null($useriscreator)) {
if ($useriscreator) {
if (! record_exists('user_coursecreators', 'userid', $user->id)) {
$cdata->userid = $user->id;
if (! insert_record('user_coursecreators', $cdata)) {
error('Cannot add user to course creators.');
}
}
} else {
if (record_exists('user_coursecreators', 'userid', $user->id)) {
if (! delete_records('user_coursecreators', 'userid', $user->id)) {
error('Cannot remove user from course creators.');
}
}
}
}
}
if ($user) {
$user->sessionIP = md5(getremoteaddr()); // Store the current IP in the session
}
return $user;
} else {
add_to_log(0, 'login', 'error', $_SERVER['HTTP_REFERER'], $username);
error_log('[client '.$_SERVER['REMOTE_ADDR']."]\t$CFG->wwwroot\tFailed Login:\t$username\t".$_SERVER['HTTP_USER_AGENT']);
return false;
}
}
/**
* Enrols (or re-enrols) a student in a given course
*
* @param int $courseid The id of the course that is being viewed
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param int $timestart ?
* @param int $timeend ?
* @return boolean
* @todo Finish documenting this function
*/
function enrol_student($userid, $courseid, $timestart=0, $timeend=0, $enrol='') {
global $CFG;
if (!$course = get_record('course', 'id', $courseid)) { // Check course
return false;
}
if (!$user = get_record('user', 'id', $userid)) { // Check user
return false;
}
// enrol the student in any parent meta courses...
if ($parents = get_records('course_meta','child_course',$courseid)) {
foreach ($parents as $parent) {
enrol_student($userid, $parent->parent_course,$timestart,$timeend,$enrol);
}
}
if (empty($enrol)) {
$enrol = $CFG->enrol; // Default current method
}
if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) {
$student->timestart = $timestart;
$student->timeend = $timeend;
$student->time = time();
$student->enrol = $enrol;
return update_record('user_students', $student);
} else {
require_once("$CFG->dirroot/mod/forum/lib.php");
forum_add_user($userid, $courseid);
$student->userid = $userid;
$student->course = $courseid;
$student->timestart = $timestart;
$student->timeend = $timeend;
$student->time = time();
$student->enrol = $enrol;
return insert_record('user_students', $student);
}
}
/**
* Unenrols a student from a given course
*
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function unenrol_student($userid, $courseid=0) {
if ($courseid) {
/// First delete any crucial stuff that might still send mail
if ($forums = get_records('forum', 'course', $courseid)) {
foreach ($forums as $forum) {
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
}
}
if ($groups = get_groups($courseid, $userid)) {
foreach ($groups as $group) {
delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
}
}
// enrol the student in any parent meta courses...
if ($parents = get_records('course_meta','child_course',$courseid)) {
foreach ($parents as $parent) {
unenrol_student($userid, $parent->parent_course);
}
}
return delete_records('user_students', 'userid', $userid, 'course', $courseid);
} else {
delete_records('forum_subscriptions', 'userid', $userid);
delete_records('groups_members', 'userid', $userid);
return delete_records('user_students', 'userid', $userid);
}
}
/**
* Add a teacher to a given course
*
* @uses $USER
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against. Set this to 0 if you would just like to test against the currently logged in user.
* @param int $editall ?
* @param string $role ?
* @param int $timestart ?
* @param int $timeend ?
* @return boolean
* @todo Finish documenting this function
*/
function add_teacher($userid, $courseid, $editall=1, $role='', $timestart=0, $timeend=0, $enrol='manual') {
global $CFG;
if ($teacher = get_record('user_teachers', 'userid', $userid, 'course', $courseid)) {
$newteacher = NULL;
$newteacher->id = $teacher->id;
$newteacher->editall = $editall;
$newteacher->enrol = $enrol;
if ($role) {
$newteacher->role = $role;
}
if ($timestart) {
$newteacher->timestart = $timestart;
}
if ($timeend) {
$newteacher->timeend = $timeend;
}
return update_record('user_teachers', $newteacher);
}
if (!record_exists('user', 'id', $userid)) {
return false; // no such user
}
if (!record_exists('course', 'id', $courseid)) {
return false; // no such course
}
$teacher = NULL;
$teacher->userid = $userid;
$teacher->course = $courseid;
$teacher->editall = $editall;
$teacher->role = $role;
$teacher->timemodified = time();
$newteacher->timestart = $timestart;
$newteacher->timeend = $timeend;
if ($student = get_record('user_students', 'userid', $userid, 'course', $courseid)) {
$teacher->timestart = $student->timestart;
$teacher->timeend = $student->timeend;
$teacher->timeaccess = $student->timeaccess;
}
if (record_exists('user_teachers', 'course', $courseid)) {
$teacher->authority = 2;
} else {
$teacher->authority = 1;
}
delete_records('user_students', 'userid', $userid, 'course', $courseid); // Unenrol as student
/// Add forum subscriptions for new users
require_once('../mod/forum/lib.php');
forum_add_user($userid, $courseid);
return insert_record('user_teachers', $teacher);
}
/**
* Removes a teacher from a given course (or ALL courses)
* Does not delete the user account
*
* @param int $courseid The id of the course that is being viewed, if any
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function remove_teacher($userid, $courseid=0) {
if ($courseid) {
/// First delete any crucial stuff that might still send mail
if ($forums = get_records('forum', 'course', $courseid)) {
foreach ($forums as $forum) {
delete_records('forum_subscriptions', 'forum', $forum->id, 'userid', $userid);
}
}
/// Next if the teacher is not registered as a student, but is
/// a member of a group, remove them from the group.
if (!isstudent($courseid, $userid)) {
if ($groups = get_groups($courseid, $userid)) {
foreach ($groups as $group) {
delete_records('groups_members', 'groupid', $group->id, 'userid', $userid);
}
}
}
return delete_records('user_teachers', 'userid', $userid, 'course', $courseid);
} else {
delete_records('forum_subscriptions', 'userid', $userid);
return delete_records('user_teachers', 'userid', $userid);
}
}
/**
* Add a creator to the site
*
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function add_creator($userid) {
if (!record_exists('user_admins', 'userid', $userid)) {
if (record_exists('user', 'id', $userid)) {
$creator->userid = $userid;
return insert_record('user_coursecreators', $creator);
}
return false;
}
return true;
}
/**
* Remove a creator from a site
*
* @uses $db
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function remove_creator($userid) {
global $db;
return delete_records('user_coursecreators', 'userid', $userid);
}
/**
* Add an admin to a site
*
* @uses SITEID
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function add_admin($userid) {
if (!record_exists('user_admins', 'userid', $userid)) {
if (record_exists('user', 'id', $userid)) {
$admin->userid = $userid;
// any admin is also a teacher on the site course
if (!record_exists('user_teachers', 'course', SITEID, 'userid', $userid)) {
if (!add_teacher($userid, SITEID)) {
return false;
}
}
return insert_record('user_admins', $admin);
}
return false;
}
return true;
}
/**
* Removes an admin from a site
*
* @uses $db
* @uses SITEID
* @param int $userid The id of the user that is being tested against.
* @return boolean
*/
function remove_admin($userid) {
global $db;
// remove also from the list of site teachers
remove_teacher($userid, SITEID);
return delete_records('user_admins', 'userid', $userid);
}
/**
* Clear a course out completely, deleting all content
* but don't delete the course itself
*
* @uses $USER
* @uses $SESSION
* @uses $CFG
* @param int $courseid The id of the course that is being viewed
* @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
* @return boolean
*/
function remove_course_contents($courseid, $showfeedback=true) {
global $CFG, $USER, $SESSION;
$result = true;
if (! $course = get_record('course', 'id', $courseid)) {
error('Course ID was incorrect (can\'t find it)');
}
$strdeleted = get_string('deleted');
// First delete every instance of every module
if ($allmods = get_records('modules') ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
$moddelete = $modname .'_delete_instance'; // Delete everything connected to an instance
$moddeletecourse = $modname .'_delete_course'; // Delete other stray stuff (uncommon)
$count=0;
if (file_exists($modfile)) {
include_once($modfile);
if (function_exists($moddelete)) {
if ($instances = get_records($modname, 'course', $course->id)) {
foreach ($instances as $instance) {
if ($moddelete($instance->id)) {
$count++;
} else {
notify('Could not delete '. $modname .' instance '. $instance->id .' ('. $instance->name .')');
$result = false;
}
}
}
} else {
notify('Function '. $moddelete() .'doesn\'t exist!');
$result = false;
}
if (function_exists($moddeletecourse)) {
$moddeletecourse($course);
}
}
if ($showfeedback) {
notify($strdeleted .' '. $count .' x '. $modname);
}
}
} else {
error('No modules are installed!');
}
// Delete any user stuff
if (delete_records('user_students', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' user_students');
}
} else {
$result = false;
}
if (delete_records('user_teachers', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' user_teachers');
}
} else {
$result = false;
}
// Delete any groups
if ($groups = get_records('groups', 'courseid', $course->id)) {
foreach ($groups as $group) {
if (delete_records('groups_members', 'groupid', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups_members');
}
} else {
$result = false;
}
if (delete_records('groups', 'id', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups');
}
} else {
$result = false;
}
}
}
// Delete events
if (delete_records('event', 'courseid', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' event');
}
} else {
$result = false;
}
// Delete logs
if (delete_records('log', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' log');
}
} else {
$result = false;
}
// Delete any course stuff
if (delete_records('course_sections', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' course_sections');
}
} else {
$result = false;
}
if (delete_records('course_modules', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' course_modules');
}
} else {
$result = false;
}
if ($course->metacourse) {
delete_records("course_meta","parent_course",$course->id);
sync_metacourse($course->id); // have to do it here so the enrolments get nuked. sync_metacourses won't find it without the id.
if ($showfeedback) {
notify("$strdeleted course_meta");
}
}
else {
if ($parents = get_records("course_meta","child_course",$course->id)) {
foreach ($parents as $parent) {
remove_from_metacourse($parent->parent_course,$parent->child_course); // this will do the unenrolments as well.
}
if ($showfeedback) {
notify("$strdeleted course_meta");
}
}
}
return $result;
}
/**
* This function will empty a course of USER data as much as
/// possible. It will retain the activities and the structure
/// of the course.
*
* @uses $USER
* @uses $SESSION
* @uses $CFG
* @param int $courseid The id of the course that is being viewed
* @param boolean $showfeedback Set this to false to suppress notifications from being printed as the functions performs its steps.
* @param boolean $removestudents ?
* @param boolean $removeteachers ?
* @param boolean $removegroups ?
* @param boolean $removeevents ?
* @param boolean $removelogs ?
* @return boolean
* @todo Finish documenting this function
*/
function remove_course_userdata($courseid, $showfeedback=true,
$removestudents=true, $removeteachers=false, $removegroups=true,
$removeevents=true, $removelogs=false) {
global $CFG, $USER, $SESSION;
$result = true;
if (! $course = get_record('course', 'id', $courseid)) {
error('Course ID was incorrect (can\'t find it)');
}
$strdeleted = get_string('deleted');
// Look in every instance of every module for data to delete
if ($allmods = get_records('modules') ) {
foreach ($allmods as $mod) {
$modname = $mod->name;
$modfile = $CFG->dirroot .'/mod/'. $modname .'/lib.php';
$moddeleteuserdata = $modname .'_delete_userdata'; // Function to delete user data
$count=0;
if (file_exists($modfile)) {
@include_once($modfile);
if (function_exists($moddeleteuserdata)) {
$moddeleteuserdata($course, $showfeedback);
}
}
}
} else {
error('No modules are installed!');
}
// Delete other stuff
if ($removestudents) {
/// Delete student enrolments
if (delete_records('user_students', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' user_students');
}
} else {
$result = false;
}
/// Delete group members (but keep the groups)
if ($groups = get_records('groups', 'courseid', $course->id)) {
foreach ($groups as $group) {
if (delete_records('groups_members', 'groupid', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups_members');
}
} else {
$result = false;
}
}
}
}
if ($removeteachers) {
if (delete_records('user_teachers', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' user_teachers');
}
} else {
$result = false;
}
}
if ($removegroups) {
if ($groups = get_records('groups', 'courseid', $course->id)) {
foreach ($groups as $group) {
if (delete_records('groups', 'id', $group->id)) {
if ($showfeedback) {
notify($strdeleted .' groups');
}
} else {
$result = false;
}
}
}
}
if ($removeevents) {
if (delete_records('event', 'courseid', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' event');
}
} else {
$result = false;
}
}
if ($removelogs) {
if (delete_records('log', 'course', $course->id)) {
if ($showfeedback) {
notify($strdeleted .' log');
}
} else {
$result = false;
}
}
return $result;
}
/// GROUPS /////////////////////////////////////////////////////////
/**
* Returns a boolean: is the user a member of the given group?
*
* @param type description
* @todo Finish documenting this function
*/
function ismember($groupid, $userid=0) {
global $USER;
if (!$groupid) { // No point doing further checks
return false;
}
if (!$userid) {
if (empty($USER->groupmember)) {
return false;
}
foreach ($USER->groupmember as $courseid => $mgroupid) {
if ($mgroupid == $groupid) {
return true;
}
}
return false;
}
return record_exists('groups_members', 'groupid', $groupid, 'userid', $userid);
}
/**
* Add a user to a group, return true upon success or if user already a group member
*
* @param groupid The group id
* @param userid The user id
* @todo Finish documenting this function
*/
function add_user_to_group ($groupid, $userid) {
if (ismember($groupid, $userid)) return true;
$record->groupid = $groupid;
$record->userid = $userid;
$record->timeadded = time();
return (insert_record('groups_members', $record) !== false);
}
/**
* Returns the group ID of the current user in the given course
*
* @uses $USER
* @param int $courseid The course being examined - relates to id field in 'course' table.
* @todo Finish documenting this function
*/
function mygroupid($courseid) {
global $USER;
if (empty($USER->groupmember[$courseid])) {
return 0;
} else {
return $USER->groupmember[$courseid];
}
}
/**
* For a given course, and possibly course module, determine
* what the current default groupmode is:
* NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
*
* @param course $course A {@link $COURSE} object
* @param array? $cm A course module object
* @return int A group mode (NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS)
*/
function groupmode($course, $cm=null) {
if ($cm and !$course->groupmodeforce) {
return $cm->groupmode;
}
return $course->groupmode;
}
/**
* Sets the current group in the session variable
*
* @uses $SESSION
* @param int $courseid The course being examined - relates to id field in 'course' table.
* @param int $groupid The group being examined.
* @return int Current group id which was set by this function
* @todo Finish documenting this function
*/
function set_current_group($courseid, $groupid) {
global $SESSION;
return $SESSION->currentgroup[$courseid] = $groupid;
}
/**
* Gets the current group for the current user as an id or an object
*
* @uses $CFG
* @uses $SESSION
* @param int $courseid The course being examined - relates to id field in 'course' table.
* @param boolean $full If true, the return value is a full record object. If false, just the id of the record.
* @todo Finish documenting this function
*/
function get_current_group($courseid, $full=false) {
global $SESSION, $USER;
if (!isset($SESSION->currentgroup[$courseid])) {
if (empty($USER->groupmember[$courseid])) {
return 0;
} else {
$SESSION->currentgroup[$courseid] = $USER->groupmember[$courseid];
}
}
if ($full) {
return get_record('groups', 'id', $SESSION->currentgroup[$courseid]);
} else {
return $SESSION->currentgroup[$courseid];
}
}
/**
* A combination function to make it easier for modules
* to set up groups.
*
* It will use a given "groupid" parameter and try to use
* that to reset the current group for the user.
*
* @uses VISIBLEGROUPS
* @param course $course A {@link $COURSE} object
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
* @param int $groupid Will try to use this optional parameter to
* reset the current group for the user
* @return int|false Returns the current group id or false if error.
* @todo Finish documenting this function
*/
function get_and_set_current_group($course, $groupmode, $groupid=-1) {
if (!$groupmode) { // Groups don't even apply
return false;
}
$currentgroupid = get_current_group($course->id);
if ($groupid < 0) { // No change was specified
return $currentgroupid;
}
if ($groupid) { // Try to change the current group to this groupid
if ($group = get_record('groups', 'id', $groupid, 'courseid', $course->id)) { // Exists
if (isteacheredit($course->id)) { // Sets current default group
$currentgroupid = set_current_group($course->id, $group->id);
} else if ($groupmode == VISIBLEGROUPS) { // All groups are visible
$currentgroupid = $group->id;
}
}
} else { // When groupid = 0 it means show ALL groups
if (isteacheredit($course->id)) { // Sets current default group
$currentgroupid = set_current_group($course->id, 0);
} else if ($groupmode == VISIBLEGROUPS) { // All groups are visible
$currentgroupid = 0;
}
}
return $currentgroupid;
}
/**
* A big combination function to make it easier for modules
* to set up groups.
*
* Terminates if the current user shouldn't be looking at this group
* Otherwise returns the current group if there is one
* Otherwise returns false if groups aren't relevant
*
* @uses SEPARATEGROUPS
* @uses VISIBLEGROUPS
* @param course $course A {@link $COURSE} object
* @param int $groupmode Either NOGROUPS, SEPARATEGROUPS or VISIBLEGROUPS
* @param string $urlroot ?
* @todo Finish documenting this function
*/
function setup_and_print_groups($course, $groupmode, $urlroot) {
if (isset($_GET['group'])) {
$changegroup = $_GET['group']; /// 0 or higher
} else {
$changegroup = -1; /// This means no group change was specified
}
$currentgroup = get_and_set_current_group($course, $groupmode, $changegroup);
if ($currentgroup === false) {
return false;
}
if ($groupmode == SEPARATEGROUPS and !isteacheredit($course->id) and !$currentgroup) {
print_heading(get_string('notingroup'));
print_footer($course);
exit;
}
if ($groupmode == VISIBLEGROUPS or ($groupmode and isteacheredit($course->id))) {
if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) {
echo '<div align="center">';
print_group_menu($groups, $groupmode, $currentgroup, $urlroot);
echo '</div>';
}
}
return $currentgroup;
}
function generate_email_processing_address($modid,$modargs) {
global $CFG;
if (empty($CFG->siteidentifier)) { // Unique site identification code
set_config('siteidentifier', random_string(32));
}
$header = $CFG->mailprefix . substr(base64_encode(pack('C',$modid)),0,2).$modargs;
return $header . substr(md5($header.$CFG->sitesecret),0,16).'@'.$CFG->maildomain;
}
function moodle_process_email($modargs,$body) {
// the first char should be an unencoded letter. We'll take this as an action
switch ($modargs{0}) {
case 'B': { // bounce
list(,$userid) = unpack('V',base64_decode(substr($modargs,1,8)));
if ($user = get_record_select("user","id=$userid","id,email")) {
// check the half md5 of their email
$md5check = substr(md5($user->email),0,16);
if ($md5check = substr($modargs, -16)) {
set_bounce_count($user);
}
// else maybe they've already changed it?
}
}
break;
// maybe more later?
}
}
/// CORRESPONDENCE ////////////////////////////////////////////////
/**
* Send an email to a specified user
*
* @uses $CFG
* @uses $_SERVER
* @uses SITEID
* @param user $user A {@link $USER} object
* @param user $from A {@link $USER} object
* @param string $subject plain text subject line of the email
* @param string $messagetext plain text version of the message
* @param string $messagehtml complete html version of the message (optional)
* @param string $attachment a file on the filesystem, relative to $CFG->dataroot
* @param string $attachname the name of the file (extension indicates MIME)
* @param boolean $usetrueaddress determines whether $from email address should
* be sent out. Will be overruled by user profile setting for maildisplay
* @return boolean|string Returns "true" if mail was sent OK, "emailstop" if email
* was blocked by user and "false" if there was another sort of error.
*/
function email_to_user($user, $from, $subject, $messagetext, $messagehtml='', $attachment='', $attachname='', $usetrueaddress=true, $repyto='', $replytoname='') {
global $CFG, $FULLME;
global $course; // This is a bit of an ugly hack to be gotten rid of later
if (!empty($course->lang)) { // Course language is defined
$CFG->courselang = $course->lang;
}
if (!empty($course->theme)) { // Course language is defined
$CFG->coursetheme = $course->theme;
}
include_once($CFG->libdir .'/phpmailer/class.phpmailer.php');
if (empty($user)) {
return false;
}
if (!empty($user->emailstop)) {
return 'emailstop';
}
if (over_bounce_threshold($user)) {
error_log("User $user->id (".fullname($user).") is over bounce threshold! Not sending.");
return false;
}
$mail = new phpmailer;
$mail->Version = 'Moodle '. $CFG->version; // mailer version
$mail->PluginDir = $CFG->libdir .'/phpmailer/'; // plugin directory (eg smtp plugin)
if (current_language() != 'en') {
$mail->CharSet = get_string('thischarset');
}
if ($CFG->smtphosts == 'qmail') {
$mail->IsQmail(); // use Qmail system
} else if (empty($CFG->smtphosts)) {
$mail->IsMail(); // use PHP mail() = sendmail
} else {
$mail->IsSMTP(); // use SMTP directly
if ($CFG->debug > 7) {
echo '<pre>' . "\n";
$mail->SMTPDebug = true;
}
$mail->Host = $CFG->smtphosts; // specify main and backup servers
if ($CFG->smtpuser) { // Use SMTP authentication
$mail->SMTPAuth = true;
$mail->Username = $CFG->smtpuser;
$mail->Password = $CFG->smtppass;
}
}
$adminuser = get_admin();
// make up an email address for handling bounces
if (!empty($CFG->handlebounces)) {
$modargs = 'B'.base64_encode(pack('V',$user->id)).substr(md5($user->email),0,16);
$mail->Sender = generate_email_processing_address(0,$modargs);
}
else {
$mail->Sender = $adminuser->email;
}
if (is_string($from)) { // So we can pass whatever we want if there is need
$mail->From = $CFG->noreplyaddress;
$mail->FromName = $from;
} else if ($usetrueaddress and $from->maildisplay) {
$mail->From = $from->email;
$mail->FromName = fullname($from);
} else {
$mail->From = $CFG->noreplyaddress;
$mail->FromName = fullname($from);
if (empty($replyto)) {
$mail->AddReplyTo($CFG->noreplyaddress,get_string('noreplyname'));
}
}
if (!empty($replyto)) {
$mail->AddReplyTo($replyto,$replytoname);
}
$mail->Subject = stripslashes($subject);
$mail->AddAddress($user->email, fullname($user) );
$mail->WordWrap = 79; // set word wrap
if (!empty($from->customheaders)) { // Add custom headers
if (is_array($from->customheaders)) {
foreach ($from->customheaders as $customheader) {
$mail->AddCustomHeader($customheader);
}
} else {
$mail->AddCustomHeader($from->customheaders);
}
}
if ($messagehtml && $user->mailformat == 1) { // Don't ever send HTML to users who don't want it
$mail->IsHTML(