From 488acd1be935c137b2741b94df8567b9088aba75 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 20 Mar 2004 06:58:52 +0000 Subject: [PATCH] Some fixes to the functions for fetching users (some of it is backing out Scott's changes, sorry Scott!) and some fixes to the interface for the admin user page. --- admin/user.php | 223 +++++++++++++++++++++++++----------------------- lib/datalib.php | 86 +++++++++++-------- 2 files changed, 167 insertions(+), 142 deletions(-) diff --git a/admin/user.php b/admin/user.php index 232c52d0f2..e00a668981 100644 --- a/admin/user.php +++ b/admin/user.php @@ -106,8 +106,16 @@ $strsearch = get_string("search"); $strshowallusers = get_string("showallusers"); - print_header("$site->shortname: $stredituser", $site->fullname, - "$stradministration -> $strusers -> $stredituser"); + if ($firstinitial or $lastinitial or $search or $page) { + print_header("$site->shortname: $stredituser", $site->fullname, + "$stradministration -> ". + "$strusers -> ". + "$stredituser"); + } else { + print_header("$site->shortname: $stredituser", $site->fullname, + "$stradministration -> ". + "$strusers -> $stredituser"); + } if ($confirmuser) { if (!$user = get_record("user", "id", "$confirmuser")) { @@ -189,143 +197,148 @@ $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 "

"; - echo get_string("firstname")." : "; - if ($firstinitial) { - echo " $strall "; + echo "

"; + echo get_string("firstname")." : "; + if ($firstinitial) { + echo " $strall "; + } else { + echo " $strall "; + } + foreach ($alphabet as $letter) { + if ($letter == $firstinitial) { + echo " $letter "; } else { - echo " $strall "; - } - foreach ($alphabet as $letter) { - if ($letter == $firstinitial) { - echo " $letter "; - } else { - echo " $letter "; - } + echo " $letter "; } - echo "
"; + } + echo "
"; /// Bar of last initials - echo get_string("lastname")." : "; - if ($lastinitial) { - echo " $strall "; + echo get_string("lastname")." : "; + if ($lastinitial) { + echo " $strall "; + } else { + echo " $strall "; + } + foreach ($alphabet as $letter) { + if ($letter == $lastinitial) { + echo " $letter "; } else { - echo " $strall "; - } - foreach ($alphabet as $letter) { - if ($letter == $lastinitial) { - echo " $letter "; - } else { - echo " $letter "; - } + echo " $letter "; } - echo "

"; - echo "
"; - - print_paging_bar($usercount, $page, $perpage, - "user.php?sort=$sort&dir=$dir&perpage=$perpage&firstinitial=$firstinitial&lastinitial=$lastinitial&"); } + echo "

"; + echo "
"; + + 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 = "id\">$strdelete"; + } 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 = "id\">" . get_string("confirm") . ""; - } 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 = "id\">$strdelete"; + } + if ($user->lastaccess) { + $strlastaccess = format_time(time() - $user->lastaccess); + } else { + $strlastaccess = get_string("never"); + } + if ($user->confirmed == 0) { + $confirmbutton = "id\">" . get_string("confirm") . ""; + } else { + $confirmbutton = ""; + } + $fullname = fullname($user, true); + $table->data[] = array ("id&course=$site->id\">$fullname", + "$user->email", + "$user->city", + "$user->country", + $strlastaccess, + "id&course=$site->id\">$stredit", + $deletebutton, + $confirmbutton); } - $fullname = fullname($user, true); - $table->data[] = array ("id&course=$site->id\">$fullname", - "$user->email", - "$user->city", - "$user->country", - $strlastaccess, - "id&course=$site->id\">$stredit", - $deletebutton, - $confirmbutton); - } - echo "
"; - echo "
"; - echo ""; - echo ""; - if ($search) { - echo ""; - } - echo "
"; - echo "
"; + echo "
"; + echo "
"; + echo ""; + echo ""; + if ($search) { + echo ""; + } + echo "
"; + echo "
"; - 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("".get_string("addnewuser").""); diff --git a/lib/datalib.php b/lib/datalib.php index b442d51307..50845f7961 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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 * -- 2.39.5