]> git.mjollnir.org Git - moodle.git/commitdiff
FIxed up function get_user() which now takes lots of parameters and
authormoodler <moodler>
Wed, 14 May 2003 15:19:04 +0000 (15:19 +0000)
committermoodler <moodler>
Wed, 14 May 2003 15:19:04 +0000 (15:19 +0000)
can be used all over the place.

Also, related fixes to "add admin" (MUCH better performance) and the user listings

More to come for "add teacher" and "add creator"

admin/admin.php
admin/user.php
lib/datalib.php

index 9383709a0af381aa4fe5c48a98bb08065f1192cf..9a5caff48adaa360172aa8c95e798250bcabd3dc 100644 (file)
@@ -2,6 +2,8 @@
       // Admin-only script to assign administrative rights to users
       // !!! based on ../course/teacher.php (cut and pasted, then mangled)
 
+    define("MAX_USERS_PER_PAGE", 30);
+
        require_once("../config.php");
 
     optional_variable($add, "");
         $$strstringtoload = get_string($stringtoload);
     }
 
+    if ($search) {
+        $searchstring = $strsearchagain;
+    } else {
+        $searchstring = $strsearch;
+    }
+
        print_header("$site->shortname: $course->shortname: $strassignadmins", 
                  "$site->fullname", 
-                 "<A HREF=\"index.php\">$stradministration</A> -> 
-                  <A HREF=\"{$_SERVER['PHP_SELF']}\">$strassignadmins</A>", "");
+                 "<a href=\"index.php\">$stradministration</a> -> 
+                  <a href=\"{$_server['php_self']}\">$strassignadmins</a>", "");
 
 /// Get all existing admins
     $admins = get_admins();
 
 
 /// Print the lists of existing and potential admins
-    echo "<TABLE CELLPADDING=2 CELLSPACING=10 ALIGN=CENTER>";
-    echo "<TR><TH WIDTH=50%>$strexistingadmins</TH><TH WIDTH=50%>$strpotentialadmins</TH></TR>";
-    echo "<TR><TD WIDTH=50% NOWRAP VALIGN=TOP>";
+    echo "<table cellpadding=2 cellspacing=10 align=center>";
+    echo "<tr><th width=50%>$strexistingadmins</th><th width=50%>$strpotentialadmins</th></tr>";
+    echo "<tr><td width=50% nowrap valign=top>";
 
 /// First, show existing admins
 
     if (! $admins) { 
-        echo "<P ALIGN=CENTER>$strnoexistingadmins</A>";
+        echo "<p align=center>$strnoexistingadmins</a>";
+
+        $adminlist = "";
 
     } else {
+        $adminarray = array();
+
         foreach ($admins as $admin) {
-            echo "<P ALIGN=right>$admin->firstname $admin->lastname,
+            $adminarray[] = $admin->id;
+            echo "<p align=right>$admin->firstname $admin->lastname,
             $admin->email &nbsp;&nbsp; ";
             if ($primaryadmin->id == $admin->id){
                 print_spacer(10, 9, false);
             } else {
-                echo "<A HREF=\"{$_SERVER['PHP_SELF']}?remove=$admin->id\"
-                TITLE=\"$strremoveadmin\"><IMG SRC=\"../pix/t/right.gif\"
-                BORDER=0></A>";
+                echo "<a href=\"{$_SERVER['PHP_SELF']}?remove=$admin->id\"
+                title=\"$strremoveadmin\"><img src=\"../pix/t/right.gif\"
+                border=0></A>";
             }
-            echo "</P>";
+            echo "</p>";
         }
+        
+        $adminlist = implode(",",$adminarray);
+        unset($adminarray);
     }
 
-    echo "<TD WIDTH=50% NOWRAP VALIGN=TOP>";
+    echo "<td width=50% nowrap valign=top>";
 
 /// Print list of potential admins
 
-    if ($search) {
-        $users = get_users_search($search);
-    } else {
-        $users = get_users_confirmed();
-    }
+    $usercount = get_users(false, $search, true, $adminlist);
 
-    
-    if ($users) {
-        foreach ($users as $user) {  // Remove users who are already admins
-            if ($admins) {
-                foreach ($admins as $admin) {
-                    if ($admin->id == $user->id) {
-                        continue 2;
-                    }
-                }
-            }
-            $potential[] = $user;
-        }
-    }
+    if ($usercount == 0) { 
+        echo "<p align=center>$strnopotentialadmins</p>";
 
-    if (! $potential) { 
-        echo "<P ALIGN=CENTER>$strnopotentialadmins</A>";
-        if ($search) {
-            echo "<FORM ACTION={$_SERVER['PHP_SELF']} METHOD=POST>";
-            echo "<INPUT TYPE=text NAME=search SIZE=20>";
-            echo "<INPUT TYPE=submit VALUE=\"$strsearchagain\">";
-            echo "</FORM>";
-        }
+    } else if ($usercount > MAX_USERS_PER_PAGE) { 
+        echo "<p align=center>$strtoomanytoshow</p>";
 
     } else {
+
         if ($search) {
-            echo "<P ALIGN=CENTER>($strsearchresults)</P>";
+            echo "<p align=center>($strsearchresults)</p>";
         }
-        if (count($potential) <= 20) {
-            foreach ($potential as $user) {
-                echo "<P ALIGN=LEFT><A HREF=\"{$_SERVER['PHP_SELF']}?add=$user->id\"
-                TITLE=\"$straddadmin\"><IMG SRC=\"../pix/t/left.gif\" BORDER=0></A>&nbsp;&nbsp;$user->firstname $user->lastname, $user->email";
-            }
-        } else {
-            echo "<P ALIGN=CENTER>There are too many users to show.<BR>";
-            echo "Enter a search word here.";
-            echo "<FORM ACTION={$_SERVER['PHP_SELF']} METHOD=POST>";
-            echo "<INPUT TYPE=text NAME=search SIZE=20>";
-            echo "<INPUT TYPE=submit VALUE=\"$strsearch\">";
-            echo "</FORM>";
+         
+        if (!$users = get_users(true, $search, true, $adminlist)) {
+            error("Could not get users!");
+        }
+
+        foreach ($users as $user) {
+            echo "<p align=left><A HREF=\"{$_SERVER['PHP_SELF']}?add=$user->id\"".
+                   "title=\"$straddadmin\"><img src=\"../pix/t/left.gif\"".
+                   "border=0></A>&nbsp;&nbsp;$user->firstname $user->lastname, $user->email";
         }
     }
 
-    echo "</TR></TABLE>";
+    if ($search or $usercount > MAX_USERS_PER_PAGE) {
+        echo "<form action={$_SERVER['PHP_SELF']} method=post>";
+        echo "<input type=text name=search size=20>";
+        echo "<input type=submit value=\"$searchstring\">";
+        echo "</form>";
+    }
+
+    echo "</tr></table>";
 
     print_footer();
 
index f2d934777f4b6a2610fec72803325fecef4a5572..27070c1cdd9f61aca79127689411298bc0ec035b 100644 (file)
             }
         }
 
-        $usercount = get_users_count();
+        $usercount = get_users(false);
 
         if ($search) {
-            $usersearchcount = get_users_count($search);
+            $usersearchcount = get_users(false, $search);
             print_heading("$usersearchcount / $usercount ".get_string("users"));
             $usercount = $usersearchcount;
         } else {
index e18c27f1c34b2b5c5e77463698cf8bd8280d3be8..39f8c7e7f54d09dae5d941704814782b14329652 100644 (file)
@@ -942,7 +942,13 @@ function get_users_search($search, $sort="u.firstname ASC") {
 }
 
 
-function get_users_count($search="") {
+function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sort="firstname ASC") {
+/// Returns a subset of users, 
+/// $get - if false then only a count of the records is returned
+/// $search is a simple string to search for
+/// $confirmed is a switch to allow/disallow unconfirmed users
+/// $exceptions is a list of IDs to ignore, eg 2,4,5,8,9,10
+/// $sort is a sorting criteria to use
 
     if ($search) {
         $search = " AND (firstname LIKE '%$search%'
@@ -950,9 +956,28 @@ function get_users_count($search="") {
                      OR email LIKE '%$search%') ";
     }
 
-    return count_records_select("user", "username <> 'guest' AND deleted <> 1 $search");
+    if ($confirmed) {
+        $confirmed = " AND confirmed = '1' ";
+    }
+
+    if ($exceptions) {
+        $exceptions = " AND id NOT IN ($exceptions) ";
+    }
+
+    if ($sort and $get) {
+        $sort = " ORDER BY $sort ";
+    } else {
+        $sort = "";
+    }
+
+    if ($get) {
+        return get_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
+    } else {
+        return count_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
+    }
 }
 
+
 function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20, $search="") {
     global $CFG;
 
@@ -1016,7 +1041,6 @@ function get_users_longtimenosee($cutofftime) {
 }
 
 
-
 /// MODULE FUNCTIONS /////////////////////////////////////////////////
 
 function get_course_mods($courseid) {