}
if (!$userid) {
- return !empty($USER->teacher[$courseid]);
+ if ($courseid) {
+ return !empty($USER->teacher[$courseid]);
+ }
+ if (!isset($USER->id)) {
+ return false;
+ }
+ $userid = $USER->id;
}
if (!$courseid) {
function isstudent($courseid, $userid=0) {
/// Is the user a student in this course?
/// If course is site, is the user a confirmed user on the site?
- global $USER;
+ global $USER, $CFG;
if (empty($USER->id) and !$userid) {
return false;
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)
- and !record_exists('user_teachers', 'userid', $userid, 'course', SITEID)));
+ or record_exists('user_teachers', 'userid', $userid));
}
}
return ($method == "email" || $method == "none" || $method == "manual");
}
-function create_user_record($username, $password) {
+function create_user_record($username, $password, $auth='') {
/// Creates a bare-bones user record
global $REMOTE_ADDR, $CFG;
+
//just in case check text case
$username = trim(moodle_strtolower($username));
+
if (function_exists('auth_get_userinfo')) {
if ($newinfo = auth_get_userinfo($username)) {
foreach ($newinfo as $key => $value){
}
}
- $newuser->auth = $CFG->auth;
+ $newuser->auth = (empty($auth)) ? $CFG->auth : $auth;
$newuser->username = $username;
$newuser->password = md5($password);
$newuser->lang = $CFG->lang;
if (empty($user->auth)) { // For some reason it isn't set yet
if (isadmin($user->id) or isguest($user->id)) {
- $user->auth = 'manual'; // Always assume these guys are internal
+ $auth = 'manual'; // Always assume these guys are internal
} else {
- $user->auth = $CFG->auth; // Normal users default to site method
+ $auth = $CFG->auth; // Normal users default to site method
}
+ } else {
+ $auth = $user->auth;
}
- if (!file_exists("$CFG->dirroot/auth/$user->auth/lib.php")) {
- $user->auth = "manual"; // Can't find auth module, default to internal
+ if (!file_exists("$CFG->dirroot/auth/$auth/lib.php")) {
+ $auth = "manual"; // Can't find auth module, default to internal
}
- require_once("$CFG->dirroot/auth/$user->auth/lib.php");
+ require_once("$CFG->dirroot/auth/$auth/lib.php");
if (auth_user_login($username, $password)) { // Successful authentication
- if ($user) {
+ if ($user) { // 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);
+ set_field('user', 'password', $md5password, 'username', $username);
}
} else {
- $user = create_user_record($username, $password);
+ $user = create_user_record($username, $password, $auth);
}
if (function_exists('auth_iscreator')) { // Check if the user is a creator