]> git.mjollnir.org Git - moodle.git/commitdiff
Some fixes to the functions for fetching users (some of it is backing
authormoodler <moodler>
Sat, 20 Mar 2004 06:58:52 +0000 (06:58 +0000)
committermoodler <moodler>
Sat, 20 Mar 2004 06:58:52 +0000 (06:58 +0000)
out Scott's changes, sorry Scott!) and some fixes to the interface
for the admin user page.

admin/user.php
lib/datalib.php

index 232c52d0f2f87f3ff0b0b0e601d96701cda61e78..e00a6689814f5897f2cf0988fb9b5b768b3f1248 100644 (file)
         $strsearch = get_string("search");
         $strshowallusers = get_string("showallusers");
 
-        print_header("$site->shortname: $stredituser", $site->fullname, 
-                     "<a href=\"index.php\">$stradministration</a> -> <a href=\"users.php\">$strusers</a> -> $stredituser");
+        if ($firstinitial or $lastinitial or $search or $page) {
+            print_header("$site->shortname: $stredituser", $site->fullname, 
+                         "<a href=\"index.php\">$stradministration</a> -> ".
+                         "<a href=\"users.php\">$strusers</a> -> ".
+                         "<a href=\"user.php\">$stredituser</a>");
+        } else {
+            print_header("$site->shortname: $stredituser", $site->fullname, 
+                         "<a href=\"index.php\">$stradministration</a> -> ".
+                         "<a href=\"users.php\">$strusers</a> -> $stredituser");
+        }
 
         if ($confirmuser) {
             if (!$user = get_record("user", "id", "$confirmuser")) {
             $sort = "firstname";
         }
 
-        if (!$users = get_course_students(0, $sort, $dir, $page*$perpage, $perpage, $firstinitial, $lastinitial, NULL, $search)) {
-            if (!$users = get_course_students(0, $sort, $dir, $page*$perpage, $perpage, $firstinitial, $lastinitial)) {
-                error("No users found!");
-            } else {
-                notify(get_string("nousersmatching", "", $search));
-            }
-            $search = "";
-        }
-
+        $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, $search, $firstinitial, $lastinitial);
         $usercount = get_users(false);
+        $usersearchcount = get_users(false, $search, true, "", "", $firstinitial, $lastinitial);
 
-        if ($firstinitial or $lastinitial) {
-            $course->id = 0; // don't look in user_students table, but just user table
-            $usercount = count_course_students($course, "", $firstinitial, $lastinitial);
-        }
-
-        if ($search) {
-            $usersearchcount = get_users(false, $search);
+        if ($search or $firstinitial or $lastinitial) {
             print_heading("$usersearchcount / $usercount ".get_string("users"));
             $usercount = $usersearchcount;
         } else {
             print_heading("$usercount ".get_string("users"));
         }
 
-        if ($usercount > $perpage) {
-            $alphabet = explode(',', get_string('alphabet'));
-            $strall = get_string("all");
+        $alphabet = explode(',', get_string('alphabet'));
+        $strall = get_string("all");
 
 
         /// Bar of first initials
 
-            echo "<center><p align=\"center\">";
-            echo get_string("firstname")." : ";
-            if ($firstinitial) {
-                echo " <a href=\"user.php?sort=firstname&dir=ASC&".
-                       "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
+        echo "<center><p align=\"center\">";
+        echo get_string("firstname")." : ";
+        if ($firstinitial) {
+            echo " <a href=\"user.php?sort=firstname&dir=ASC&".
+                 "perpage=$perpage&lastinitial=$lastinitial\">$strall</a> ";
+        } else {
+            echo " <b>$strall</b> ";
+        }
+        foreach ($alphabet as $letter) {
+            if ($letter == $firstinitial) {
+                echo " <b>$letter</b> ";
             } else {
-                echo " <b>$strall</b> ";
-            }
-            foreach ($alphabet as $letter) {
-                if ($letter == $firstinitial) {
-                    echo " <b>$letter</b> ";
-                } else {
-                    echo " <a href=\"user.php?sort=firstname&dir=ASC&".
-                           "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
-                }
+                echo " <a href=\"user.php?sort=firstname&dir=ASC&".
+                     "perpage=$perpage&lastinitial=$lastinitial&firstinitial=$letter\">$letter</a> ";
             }
-            echo "<br />";
+        }
+        echo "<br />";
 
         /// Bar of last initials
 
-            echo get_string("lastname")." : ";
-            if ($lastinitial) {
-                echo " <a href=\"user.php?sort=lastname&dir=ASC&".
-                       "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
+        echo get_string("lastname")." : ";
+        if ($lastinitial) {
+            echo " <a href=\"user.php?sort=lastname&dir=ASC&".
+                 "perpage=$perpage&firstinitial=$firstinitial\">$strall</a> ";
+        } else {
+            echo " <b>$strall</b> ";
+        }
+        foreach ($alphabet as $letter) {
+            if ($letter == $lastinitial) {
+                echo " <b>$letter</b> ";
             } else {
-                echo " <b>$strall</b> ";
-            }
-            foreach ($alphabet as $letter) {
-                if ($letter == $lastinitial) {
-                    echo " <b>$letter</b> ";
-                } else {
-                    echo " <a href=\"user.php?sort=lastname&dir=ASC&".
-                           "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
-                }
+                echo " <a href=\"user.php?sort=lastname&dir=ASC&".
+                     "perpage=$perpage&firstinitial=$firstinitial&lastinitial=$letter\">$letter</a> ";
             }
-            echo "</p>";
-            echo "</center>";
-    
-            print_paging_bar($usercount, $page, $perpage,
-                             "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
         }
+        echo "</p>";
+        echo "</center>";
+
+        print_paging_bar($usercount, $page, $perpage,
+                "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&search=$search&");
 
         flush();
 
-        $countries = get_list_of_countries();
 
-        foreach ($users as $key => $user) {
-            if (!empty($user->country)) {
-                $users[$key]->country = $countries[$user->country];
+        if (!$users) {
+            $match = array();
+            if ($search) {
+               $match[] = $search;
             }
-        }
-        if ($sort == "country") {  // Need to resort by full country name, not code
-            foreach ($users as $user) {
-                $susers[$user->id] = $user->country;
+            if ($firstinitial) {
+               $match[] = get_string("firstname").": $firstinitial"."___";
             }
-            asort($susers);
-            foreach ($susers as $key => $value) {
-                $nusers[] = $users[$key];
+            if ($lastinitial) {
+               $match[] = get_string("lastname").": $lastinitial"."___";
             }
-            $users = $nusers;
-        }
+            $matchstring = implode(", ", $match);
+            print_heading(get_string("nousersmatching", "", $matchstring));
 
-        $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
-        $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
-        $table->width = "95%";
-        foreach ($users as $user) {
-            if ($user->id == $USER->id or $user->username == "changeme") {
-                $deletebutton = "";
-            } else {
-                $deletebutton = "<a href=\"user.php?delete=$user->id\">$strdelete</a>";
+        } else {
+
+            $countries = get_list_of_countries();
+
+            foreach ($users as $key => $user) {
+                if (!empty($user->country)) {
+                    $users[$key]->country = $countries[$user->country];
+                }
             }
-            if ($user->lastaccess) {
-                $strlastaccess = format_time(time() - $user->lastaccess);
-            } else {
-                $strlastaccess = get_string("never");
+            if ($sort == "country") {  // Need to resort by full country name, not code
+                foreach ($users as $user) {
+                    $susers[$user->id] = $user->country;
+                }
+                asort($susers);
+                foreach ($susers as $key => $value) {
+                    $nusers[] = $users[$key];
+                }
+                $users = $nusers;
             }
-            if ($user->confirmed == 0) {
-                $confirmbutton = "<a href=\"user.php?confirmuser=$user->id\">" . get_string("confirm") . "</a>";
-            } else {
-                $confirmbutton = "";
+
+            $table->head = array ("$firstname / $lastname", $email, $city, $country, $lastaccess, "", "", "");
+            $table->align = array ("left", "left", "left", "left", "left", "center", "center", "center");
+            $table->width = "95%";
+            foreach ($users as $user) {
+                if ($user->id == $USER->id or $user->username == "changeme") {
+                    $deletebutton = "";
+                } else {
+                    $deletebutton = "<a href=\"user.php?delete=$user->id\">$strdelete</a>";
+                }
+                if ($user->lastaccess) {
+                    $strlastaccess = format_time(time() - $user->lastaccess);
+                } else {
+                    $strlastaccess = get_string("never");
+                }
+                if ($user->confirmed == 0) {
+                    $confirmbutton = "<a href=\"user.php?confirmuser=$user->id\">" . get_string("confirm") . "</a>";
+                } else {
+                    $confirmbutton = "";
+                }
+                $fullname = fullname($user, true);
+                $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>",
+                                        "$user->email",
+                                        "$user->city",
+                                        "$user->country",
+                                        $strlastaccess,
+                                        "<a href=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</a>",
+                                        $deletebutton,
+                                        $confirmbutton);
             }
-            $fullname = fullname($user, true);
-            $table->data[] = array ("<a href=\"../user/view.php?id=$user->id&course=$site->id\">$fullname</a>",
-                             "$user->email",
-                             "$user->city",
-                             "$user->country",
-                             $strlastaccess,
-                             "<a href=\"../user/edit.php?id=$user->id&course=$site->id\">$stredit</a>",
-                             $deletebutton,
-                             $confirmbutton);
-        }
 
-        echo "<table align=center cellpadding=10><tr><td>";
-        echo "<form action=user.php method=post>";
-        echo "<input type=text name=search value=\"$search\" size=20>";
-        echo "<input type=submit value=\"$strsearch\">";
-        if ($search) {
-            echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\">";
-        }
-        echo "</form>";    
-        echo "</td></tr></table>";
+            echo "<table align=center cellpadding=10><tr><td>";
+            echo "<form action=user.php method=post>";
+            echo "<input type=text name=search value=\"$search\" size=20>";
+            echo "<input type=submit value=\"$strsearch\">";
+            if ($search) {
+                echo "<input type=\"button\" onclick=\"document.location='user.php';\" value=\"$strshowallusers\">";
+            }
+            echo "</form>";    
+            echo "</td></tr></table>";
 
-        print_table($table);
+            print_table($table);
 
-        print_paging_bar($usercount, $page, $perpage,
-                         "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&");
+            print_paging_bar($usercount, $page, $perpage,
+                             "user.php?sort=$sort&dir=$dir&perpage=$perpage".
+                             "&firstinitial=$firstinitial&lastinitial=$lastinitial&search=$search&");
+
+        }
 
         if ($CFG->auth == "email" || $CFG->auth == "none" || $CFG->auth == "manual"){
             print_heading("<a href=\"user.php?newuser=true\">".get_string("addnewuser")."</a>");
index b442d51307e35b085173660bb569903d68e6a4b2..50845f7961774ad8503e75b0406640e8b91680b2 100644 (file)
@@ -1138,9 +1138,7 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
     }
 
     $groupmembers = '';
-    $userstudents = '';
-    $userstudentcolumns = '';
-    $select = " u.deleted = '0' ";
+    $select = "s.course = '$courseid' AND s.userid = u.id AND u.deleted = '0' ";
 
     if ($search) {
         $search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
@@ -1162,23 +1160,16 @@ function get_course_students($courseid, $sort="s.timeaccess", $dir="", $page=0,
         $select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
     }
 
-    if ($courseid != 0) {
-        $userstudents = ", {$CFG->prefix}user_students s ";
-        $select .= " AND s.course = '$courseid' AND s.userid = u.id";
-        $userstudentcolumns = ", s.timeaccess as lastaccess";
-    } else {
-        $userstudentcolumns = ", u.lastaccess as lastaccess";
-    }
-
-
     if ($sort) {
         $sort = " ORDER BY $sort ";
     }
 
     return get_records_sql("SELECT u.id, u.confirmed, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat,
                             u.email, u.city, u.country, u.picture, u.department, u.institution,
-                            u.emailstop, u.lang, u.timezone $userstudentcolumns
-                            FROM {$CFG->prefix}user u $userstudents $groupmembers
+                            u.emailstop, u.lang, u.timezone, s.timeaccess as lastaccess
+                            FROM {$CFG->prefix}user u,
+                                 {$CFG->prefix}user_students s
+                                 $groupmembers
                             WHERE $select $search $sort $dir $limit");
 }
 
@@ -1193,20 +1184,20 @@ function count_course_students($course, $search="", $firstinitial="", $lastiniti
 
     switch ($CFG->dbtype) {
         case "mysql":
+             $fullname = " CONCAT(firstname,\" \",lastname) ";
              $LIKE = "LIKE";
              break;
         default: 
+             $fullname = " firstname||\' \'||lastname ";
              $LIKE = "ILIKE";
     }
-
     
     $groupmembers = "";
-    $userstudents = "";
         
-    $select = " u.deleted = '0'";
+    $select = "s.course = '$course->id' AND s.userid = u.id AND u.deleted = '0'";
 
     if ($search) {
-        $select .= " AND u.firstname $LIKE '%$search%' OR u.lastname $LIKE '%$search%'";
+        $search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
     } 
     if ($firstinitial) {
         $select .= " AND u.firstname $LIKE '$firstinitial%'";
@@ -1223,13 +1214,9 @@ function count_course_students($course, $search="", $firstinitial="", $lastiniti
         $select .= " AND u.id = gm.userid AND gm.groupid = '$group'";
     }
 
-    if ($course->id != 0) {
-        $userstudents = ", {$CFG->prefix}user_students s ";
-        $select .= " AND s.course = '$course->id' AND s.userid = u.id";
-    }
-
     return count_records_sql("SELECT COUNT(*) 
-                              FROM {$CFG->prefix}user u $userstudents $groupmembers
+                              FROM {$CFG->prefix}user u,
+                                   {$CFG->prefix}user_students s $groupmembers
                               WHERE $select");
 }
 
@@ -1353,7 +1340,8 @@ function get_site_users($sort="u.lastaccess DESC", $select="") {
 * @param       array(int)      $exceptions     a list of IDs to ignore, eg 2,4,5,8,9,10
 * @param       string  $sort   a SQL snippet for the sorting criteria to use
 */
-function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sort="firstname ASC") {
+function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sort="firstname ASC",
+                   $firstinitial="", $lastinitial="") {
 
     global $CFG;
 
@@ -1371,18 +1359,27 @@ function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sor
              $LIKE = "ILIKE";
     }
 
+    $select = "username <> 'guest' AND deleted = 0";
+
     if ($search) {
-        $search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
+        $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
     }
 
     if ($confirmed) {
-        $confirmed = " AND confirmed = '1' ";
+        $select .= " AND confirmed = '1' ";
     }
 
     if ($exceptions) {
-        $exceptions = " AND id NOT IN ($exceptions) ";
+        $select .= " AND id NOT IN ($exceptions) ";
     }
 
+    if ($firstinitial) {
+        $select .= " AND firstname $LIKE '$firstinitial%'";
+    } 
+    if ($lastinitial) {
+        $select .= " AND lastname $LIKE '$lastinitial%'";
+    } 
+
     if ($sort and $get) {
         $sort = " ORDER BY $sort ";
     } else {
@@ -1390,9 +1387,9 @@ function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sor
     }
 
     if ($get) {
-        return get_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
+        return get_records_select("user", "$select $sort");
     } else {
-        return count_records_select("user", "username <> 'guest' AND deleted = 0 $search $confirmed $exceptions $sort");
+        return count_records_select("user", "$select $sort");
     }
 }
 
@@ -1404,7 +1401,9 @@ function get_users($get=true, $search="", $confirmed=false, $exceptions="", $sor
 *
 * @param       type description
 */
-function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20, $search="") {
+function get_users_listing($sort="lastaccess", $dir="ASC", $page=0, $recordsperpage=99999,
+                           $search="", $firstinitial="", $lastinitial="") {
+
     global $CFG;
 
     switch ($CFG->dbtype) {
@@ -1420,23 +1419,36 @@ function get_users_listing($sort, $dir="ASC", $page=1, $recordsperpage=20, $sear
              break;
         default: 
              $limit = "LIMIT $recordsperpage,$page";
-             $fullname = " firstname||\" \"||lastname ";
+             $fullname = " firstname||' '||lastname ";
              $LIKE = "LIKE";
     }
 
+    $select = 'deleted <> 1';
+
     if ($search) {
-        $search = " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
+        $select .= " AND ($fullname $LIKE '%$search%' OR email $LIKE '%$search%') ";
+    }
+
+    if ($firstinitial) {
+        $select .= " AND firstname $LIKE '$firstinitial%' ";
+    }
+
+    if ($lastinitial) {
+        $select .= " AND lastname $LIKE '$lastinitial%' ";
     }
 
-/// warning: will return unconfirmed users
+    if ($sort) {
+        $sort = " ORDER BY $sort $dir";
+    }
+
+/// warning: will return UNCONFIRMED USERS
     return get_records_sql("SELECT id, username, email, firstname, lastname, city, country, lastaccess, confirmed
                               FROM {$CFG->prefix}user 
-                             WHERE username <> 'guest' 
-                               AND deleted <> 1 $search
-                          ORDER BY $sort $dir $limit");
+                             WHERE $select $sort $limit ");
 
 }
 
+
 /**
 * shortdesc
 *