MDL-26647 (3) Support showuseridentity on Browse Users page
authorsam marshall <s.marshall@open.ac.uk>
Wed, 13 Apr 2011 14:44:42 +0000 (15:44 +0100)
committersam marshall <s.marshall@open.ac.uk>
Fri, 4 Nov 2011 13:57:11 +0000 (13:57 +0000)
admin/user.php
lib/datalib.php

index 2827ccf..9453dbf 100644 (file)
@@ -7,7 +7,7 @@
     $delete       = optional_param('delete', 0, PARAM_INT);
     $confirm      = optional_param('confirm', '', PARAM_ALPHANUM);   //md5 confirmation hash
     $confirmuser  = optional_param('confirmuser', 0, PARAM_INT);
-    $sort         = optional_param('sort', 'name', PARAM_ALPHA);
+    $sort         = optional_param('sort', 'name', PARAM_ALPHANUM);
     $dir          = optional_param('dir', 'ASC', PARAM_ALPHA);
     $page         = optional_param('page', 0, PARAM_INT);
     $perpage      = optional_param('perpage', 30, PARAM_INT);        // how many per page
     echo $OUTPUT->header();
 
     // Carry on with the user listing
-
-    $columns = array("firstname", "lastname", "email", "city", "country", "lastaccess");
+    $context = context_system::instance();
+    $extracolumns = get_extra_user_fields($context);
+    $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
+            array('city', 'country', 'lastaccess'));
 
     foreach ($columns as $column) {
-        $string[$column] = get_string("$column");
+        $string[$column] = get_user_field_name($column);
         if ($sort != $column) {
             $columnicon = "";
             if ($column == "lastaccess") {
     }
 
     list($extrasql, $params) = $ufiltering->get_sql_filter();
-    $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '', $extrasql, $params);
+    $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
+            $extrasql, $params, $context);
     $usercount = get_users(false);
     $usersearchcount = get_users(false, '', true, null, "", '', '', '', '', '*', $extrasql, $params);
 
         }
 
         $table = new html_table();
-        $table->head = array ($fullnamedisplay, $email, $city, $country, $lastaccess, "", "", "");
-        $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
+        $table->head = array ();
+        $table->align = array();
+        $table->head[] = $fullnamedisplay;
+        $table->align[] = 'left';
+        foreach ($extracolumns as $field) {
+            $table->head[] = ${$field};
+            $table->align[] = 'left';
+        }
+        $table->head[] = $city;
+        $table->align[] = 'left';
+        $table->head[] = $country;
+        $table->align[] = 'left';
+        $table->head[] = $lastaccess;
+        $table->align[] = 'left';
+        $table->head[] = "";
+        $table->align[] = 'center';
+        $table->head[] = "";
+        $table->align[] = 'center';
+        $table->head[] = "";
+        $table->align[] = 'center';
+
         $table->width = "95%";
         foreach ($users as $user) {
             if (isguestuser($user)) {
             }
             $fullname = fullname($user, true);
 
-            $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>",
-                                "$user->email",
-                                "$user->city",
-                                "$user->country",
-                                $strlastaccess,
-                                $editbutton,
-                                $deletebutton,
-                                $confirmbutton);
+            $row = array ();
+            $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
+            foreach ($extracolumns as $field) {
+                $row[] = $user->{$field};
+            }
+            $row[] = $user->city;
+            $row[] = $user->country;
+            $row[] = $strlastaccess;
+            $row[] = $editbutton;
+            $row[] = $deletebutton;
+            $row[] = $confirmbutton;
+            $table->data[] = $row;
         }
     }
 
index d6b0988..285729c 100644 (file)
@@ -249,11 +249,13 @@ function get_users($get=true, $search='', $confirmed=false, array $exceptions=nu
  * @param string $lastinitial Users whose last name starts with $lastinitial
  * @param string $extraselect An additional SQL select statement to append to the query
  * @param array $extraparams Additional parameters to use for the above $extraselect
+ * @param object $extracontext If specified, will include user 'extra fields'
+ *   as appropriate for current user and given context
  * @return array Array of {@link $USER} records
  */
-
 function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperpage=0,
-                           $search='', $firstinitial='', $lastinitial='', $extraselect='', array $extraparams=null) {
+                           $search='', $firstinitial='', $lastinitial='', $extraselect='',
+                           array $extraparams=null, $extracontext = null) {
     global $DB;
 
     $fullname  = $DB->sql_fullname();
@@ -289,8 +291,18 @@ function get_users_listing($sort='lastaccess', $dir='ASC', $page=0, $recordsperp
         $sort = " ORDER BY $sort $dir";
     }
 
-/// warning: will return UNCONFIRMED USERS
-    return $DB->get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed, mnethostid
+    // If a context is specified, get extra user fields that the current user
+    // is supposed to see.
+    $extrafields = '';
+    if ($extracontext) {
+        $extrafields = get_extra_user_fields_sql($extracontext, '', '',
+                array('id', 'username', 'email', 'firstname', 'lastname', 'city', 'country',
+                'lastaccess', 'confirmed', 'mnethostid'));
+    }
+
+    // warning: will return UNCONFIRMED USERS
+    return $DB->get_records_sql("SELECT id, username, email, firstname, lastname, city, country,
+                                        lastaccess, confirmed, mnethostid$extrafields
                                    FROM {user}
                                   WHERE $select
                                   $sort", $params, $page, $recordsperpage);