]> git.mjollnir.org Git - moodle.git/commitdiff
Improvements to switchroles
authormoodler <moodler>
Thu, 21 Sep 2006 15:58:59 +0000 (15:58 +0000)
committermoodler <moodler>
Thu, 21 Sep 2006 15:58:59 +0000 (15:58 +0000)
1) You can have separate switchroles active in every course

2) Admins get a better view (doanything is supressed when switchroles active)

lib/accesslib.php
lib/weblib.php

index 1e5cf4a482e36d96789d39c90cc8b6846a75afc2..de53594e0ec246397a62f5b0bd2d77daac1be9f7 100755 (executable)
@@ -301,10 +301,13 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
     }
 
     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) {
@@ -2650,9 +2653,9 @@ function role_switch($roleid, $context) {
 
 /// 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;
     }
 
@@ -2671,7 +2674,7 @@ function role_switch($roleid, $context) {
 
 /// 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
 
@@ -2681,7 +2684,7 @@ function role_switch($roleid, $context) {
         }
     }
 
-/// 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;
 
index 73f949b29788e5b16a3db913fa9e4f4d3aba4618..a60ec085a04b68e8ab2f9313cc6954d5b9f85230 100644 (file)
@@ -2604,14 +2604,16 @@ function user_login_string($course=NULL, $user=NULL) {
         // $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&amp;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.