Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
More improvements to isteacher() logic (and isadmin now uses $USER->a…
…dmin too)
  • Loading branch information
moodler committed Apr 6, 2005
1 parent 5e04ee0 commit dcc17b6
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/moodlelib.php
Expand Up @@ -1651,6 +1651,10 @@ function isadmin($userid=0) {
$userid = $USER->id;
}

if (!empty($USER->id) and ($userid == $USER->id)) { // Check session cache
return !empty($USER->admin);
}

if (in_array($userid, $admins)) {
return true;
} else if (in_array($userid, $nonadmins)) {
Expand Down Expand Up @@ -1682,23 +1686,28 @@ function isteacher($courseid=0, $userid=0, $includeadmin=true) {
if (empty($USER) or empty($USER->id)) { // not logged in so can't be a teacher
return false;
}
if (!empty($USER->teacher) and $courseid) { // look in session cache
return !empty($USER->teacher[$courseid]);
if (!empty($USER->teacher) and $courseid) { // look in session cache
if (!empty($USER->teacher[$courseid])) { // Explicitly a teacher, good
return true;
}
}
$userid = $USER->id;
$userid = $USER->id; // we need to make further checks
}

if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can
if ($includeadmin and isadmin($userid)) { // admins can do anything the teacher can
return true;
}

if (empty($courseid)) {
if (empty($courseid)) { // should not happen, but we handle it
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.');
notify('Coding error: isteacher() should not be used without a valid course id '.
'as argument. Please notify the developer for this module.');
}
return isteacherinanycourse($userid, $includeadmin);
}

/// Last resort, check the database

return record_exists('user_teachers', 'userid', $userid, 'course', $courseid);
}

Expand All @@ -1718,6 +1727,9 @@ function isteacherinanycourse($userid=0, $includeadmin=true) {
if (empty($USER) or empty($USER->id)) {
return false;
}
if (!empty($USER->teacher)) { // look in session cache
return true;
}
$userid = $USER->id;
}

Expand Down

0 comments on commit dcc17b6

Please sign in to comment.