}
if ($doanything) {
+
// Check site
- $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
- if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) {
- return (0 < $capabilities[$sitecontext->id]['moodle/site:doanything']);
+ if (empty($USER->switchrole[$context->id])) { // Ignore site setting if switchrole is active
+ $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
+ if (isset($capabilities[$sitecontext->id]['moodle/site:doanything'])) {
+ return (0 < $capabilities[$sitecontext->id]['moodle/site:doanything']);
+ }
}
switch ($context->aggregatelevel) {
/// If we can't use this or are already using it or no role was specified then bail completely and reset
if (empty($roleid) || !has_capability('moodle/role:switchroles', $context)
- || !empty($USER->switchrole) || !confirm_sesskey()) {
- load_user_capability(); // Reset all permissions to normal
- unset($USER->switchrole); // Delete old capabilities
+ || !empty($USER->switchrole[$context->id]) || !confirm_sesskey()) {
+ load_user_capability('', $context); // Reset all permissions for this context to normal
+ unset($USER->switchrole[$context->id]); // Delete old capabilities
return true;
}
/// We have a valid roleid that this user can switch to, so let's set up the session
- $USER->switchrole = $roleid; // So we know later what state we are in
+ $USER->switchrole[$context->id] = $roleid; // So we know later what state we are in
unset($USER->capabilities[$context->id]); // Delete old capabilities
}
}
-/// Add some capabilities we are really going to always need, even if the role doesn't have them!
+/// Add some permissions we are really going to always need, even if the role doesn't have them!
$USER->capabilities[$context->id]['moodle/course:view'] = CAP_ALLOW;
// $course->id is not defined during installation
return '';
} else if (isset($user->id) and $user->id) {
+ $context = get_context_instance(CONTEXT_COURSE, $course->id);
+
$fullname = fullname($user, true);
$username = "<a target=\"{$CFG->framename}\" href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>";
if (isset($user->username) && $user->username == 'guest') {
$loggedinas = $realuserinfo.get_string('loggedinasguest').
" (<a target=\"{$CFG->framename}\" href=\"$wwwroot/login/index.php\">".get_string('login').'</a>)';
- } else if (!empty($user->switchrole)) {
+ } else if (!empty($user->switchrole[$context->id])) {
$rolename = '';
- if ($role = get_record('role', 'id', $user->switchrole)) {
+ if ($role = get_record('role', 'id', $user->switchrole[$context->id])) {
$rolename = ': '.format_string($role->name);
}
$loggedinas = get_string('loggedinas', 'moodle', $username).$rolename.