]> git.mjollnir.org Git - moodle.git/commitdiff
role assign: MDL-17323 Show users with the role in a parent context as disabled opito...
authortjhunt <tjhunt>
Fri, 21 Nov 2008 05:46:45 +0000 (05:46 +0000)
committertjhunt <tjhunt>
Fri, 21 Nov 2008 05:46:45 +0000 (05:46 +0000)
admin/roles/lib.php
lang/en_utf8/role.php
lib/accesslib.php

index 9f8088d5ca3a6d065587414d49b737ef3a46b387..f65ee7efb20cf180e0215964cd736f59d936c99a 100644 (file)
@@ -1114,22 +1114,95 @@ class existing_role_holders extends role_assign_user_selector_base {
     }
 
     public function find_users($search) {
+        global $DB;
+
         list($wherecondition, $params) = $this->search_sql($search, 'u');
-        $contextusers = get_role_users($this->roleid, $this->context, false,
-                $this->required_fields_sql('u') . ', ra.hidden', 'u.lastname, u.firstname',
-                true, '', '', '', $wherecondition, $params);
+        list($ctxcondition, $ctxparams) = $DB->get_in_or_equal(get_parent_contexts($this->context, true));
+        $params = array_merge($params, $ctxparams);
+        $params[] = $this->roleid;
 
+        $sql = "SELECT ra.id as raid," . $this->required_fields_sql('u') . ",ra.hidden,ra.contextid
+                FROM {role_assignments} ra
+                JOIN {user} u ON u.id = ra.userid
+                JOIN {context} ctx ON ra.contextid = ctx.id
+                WHERE
+                    $wherecondition AND
+                    ctx.id $ctxcondition AND
+                    ra.roleid = ?
+                ORDER BY ctx.depth DESC, u.lastname, u.firstname";
+        $contextusers = $DB->get_records_sql($sql, $params);
+
+        // No users at all.
         if (empty($contextusers)) {
             return array();
         }
 
+        // We have users. Out put them in groups by context depth.
+        // To help the loop below, tack a dummy user on the end of the results
+        // array, to trigger output of the last group.
+        $dummyuser = new stdClass;
+        $dummyuser->contextid = 0;
+        $dummyuser->id = 0;
+        $contextusers[] = $dummyuser;
+        $results = array(); // The results array we are building up.
+        $doneusers = array(); // Ensures we only list each user at most once.
+        $currentcontextid = $this->context->id;
+        $currentgroup = array();
+        foreach ($contextusers as $user) {
+            if (isset($doneusers[$user->id])) {
+                continue;
+            }
+            $doneusers[$user->id] = 1;
+            if ($user->contextid != $currentcontextid) {
+                // We have got to the end of the previous group. Add it to the results array.
+                if ($currentcontextid == $this->context->id) {
+                    $groupname = $this->this_con_group_name($search, count($currentgroup));
+                } else {
+                    $groupname = $this->parent_con_group_name($search, $currentcontextid);
+                }
+                $results[$groupname] = $currentgroup;
+                // Get ready for the next group.
+                $currentcontextid = $user->contextid;
+                $currentgroup = array();
+            }
+            // Add this user to the group we are building up.
+            unset($user->contextid);
+            if ($currentcontextid != $this->context->id) {
+                $user->disabled = true;
+            }
+            $currentgroup[$user->id] = $user;
+        }
+
+        return $results;
+    }
+
+    protected function this_con_group_name($search, $numusers) {
         if ($search) {
-            $groupname = get_string('extusersmatching', 'role', $search);
+            if ($numusers) {
+                return get_string('usersinthiscontextmatching', 'role', $search);
+            } else {
+                return get_string('noneinthiscontextmatching', 'role', $search);
+            }
         } else {
-            $groupname = get_string('extusers', 'role');
+            if ($numusers) {
+                return get_string('usersinthiscontext', 'role');
+            } else {
+                return get_string('noneinthiscontext', 'role');
+            }
         }
+    }
 
-        return array($groupname => $contextusers);
+    protected function parent_con_group_name($search, $contextid) {
+        $context = get_context_instance_by_id($contextid);
+        $contextname = print_context_name($context, true, true);
+        if ($search) {
+            $a = new stdClass;
+            $a->contextname = $contextname;
+            $a->search = $search;
+            return get_string('usersfrommatching', 'role', $a);
+        } else {
+            return get_string('usersfrom', 'role', $contextname);
+        }
     }
 
     // Override to add (hidden) to hidden role assignments.
index 18a28ece88681080d60341c0a2a247976d757852..f88d9ababac4769cad7f83bff4403b803223b479 100644 (file)
@@ -134,6 +134,8 @@ $string['morethan'] = 'More than $a';
 $string['multipleroles'] = 'Multiple roles';
 $string['my:manageblocks'] = 'Manage myMoodle page blocks';
 $string['nocapabilitiesincontext'] = 'No capabilities available in this context';
+$string['noneinthiscontext'] = 'None in this context';
+$string['noneinthiscontextmatching'] = 'No users matching \'$a\' in this context';
 $string['notabletoassignroleshere'] = 'You are not able to assign any roles here';
 $string['notabletooverrideroleshere'] = 'You are not able to override the permissions on any roles here';
 $string['notset'] = 'Not set';
@@ -226,6 +228,10 @@ $string['user:viewdetails'] = 'View user profiles';
 $string['user:viewhiddendetails'] = 'View hidden details of users';
 $string['user:viewuseractivitiesreport'] = 'See user activity reports';
 $string['userhashiddenassignments'] = 'This user has one or more hidden role assignments in this course';
+$string['usersfrom'] = 'Users from $a';
+$string['usersfrommatching'] = 'Users from $a->contextname matching \'$a->search\'';
+$string['usersinthiscontext'] = 'Users in this context';
+$string['usersinthiscontextmatching'] = 'Users in this context matching \'$a\'';
 $string['userswiththisrole'] = 'Users with role';
 $string['userswithrole'] = 'All users with a role';
 $string['useshowadvancedtochange'] = 'Use \'Show advanced\' to change';
index 451cc1f2c1f18cf1899f5b4573d9a9069e21f912..b2d9a2119e3f8007b90b8143eb7484b9532b1df4 100755 (executable)
@@ -3640,7 +3640,7 @@ function role_context_capabilities($roleid, $context, $cap='') {
  * @param object $context
  * @return array()
  */
-function get_parent_contexts($context) {
+function get_parent_contexts($context, $includeself = false) {
 
     if ($context->path == '') {
         return array();
@@ -3648,7 +3648,9 @@ function get_parent_contexts($context) {
 
     $parentcontexts = substr($context->path, 1); // kill leading slash
     $parentcontexts = explode('/', $parentcontexts);
-    array_pop($parentcontexts); // and remove its own id
+    if (!$includeself) {
+        array_pop($parentcontexts); // and remove its own id
+    }
 
     return array_reverse($parentcontexts);
 }