From: moodler Date: Wed, 14 May 2003 15:19:04 +0000 (+0000) Subject: FIxed up function get_user() which now takes lots of parameters and X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=5a7416552387fa8b48b669e3afba369063ccc0f5;p=moodle.git FIxed up function get_user() which now takes lots of parameters and 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" --- diff --git a/admin/admin.php b/admin/admin.php index 9383709a0a..9a5caff48a 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -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, ""); @@ -46,10 +48,16 @@ $$strstringtoload = get_string($stringtoload); } + if ($search) { + $searchstring = $strsearchagain; + } else { + $searchstring = $strsearch; + } + print_header("$site->shortname: $course->shortname: $strassignadmins", "$site->fullname", - "$stradministration -> - $strassignadmins", ""); + "$stradministration -> + $strassignadmins", ""); /// Get all existing admins $admins = get_admins(); @@ -96,83 +104,75 @@ /// Print the lists of existing and potential admins - echo ""; - echo ""; - echo "
$strexistingadmins$strpotentialadmins
"; + echo ""; + echo ""; + echo "
$strexistingadmins$strpotentialadmins
"; /// First, show existing admins if (! $admins) { - echo "

$strnoexistingadmins"; + echo "

$strnoexistingadmins"; + + $adminlist = ""; } else { + $adminarray = array(); + foreach ($admins as $admin) { - echo "

$admin->firstname $admin->lastname, + $adminarray[] = $admin->id; + echo "

$admin->firstname $admin->lastname, $admin->email    "; if ($primaryadmin->id == $admin->id){ print_spacer(10, 9, false); } else { - echo "id\" - TITLE=\"$strremoveadmin\">"; + echo "id\" + title=\"$strremoveadmin\">"; } - echo "

"; + echo "

"; } + + $adminlist = implode(",",$adminarray); + unset($adminarray); } - echo "
"; + echo ""; /// 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 "

$strnopotentialadmins

"; - if (! $potential) { - echo "

$strnopotentialadmins"; - if ($search) { - echo "

"; - echo ""; - echo ""; - echo "
"; - } + } else if ($usercount > MAX_USERS_PER_PAGE) { + echo "

$strtoomanytoshow

"; } else { + if ($search) { - echo "

($strsearchresults)

"; + echo "

($strsearchresults)

"; } - if (count($potential) <= 20) { - foreach ($potential as $user) { - echo "

id\" - TITLE=\"$straddadmin\">  $user->firstname $user->lastname, $user->email"; - } - } else { - echo "

There are too many users to show.
"; - echo "Enter a search word here."; - echo "

"; - echo ""; - echo ""; - echo "
"; + + if (!$users = get_users(true, $search, true, $adminlist)) { + error("Could not get users!"); + } + + foreach ($users as $user) { + echo "

id\"". + "title=\"$straddadmin\">  $user->firstname $user->lastname, $user->email"; } } - echo "

"; + if ($search or $usercount > MAX_USERS_PER_PAGE) { + echo "
"; + echo ""; + echo ""; + echo "
"; + } + + echo "
"; print_footer(); diff --git a/admin/user.php b/admin/user.php index f2d934777f..27070c1cdd 100644 --- a/admin/user.php +++ b/admin/user.php @@ -161,10 +161,10 @@ } } - $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 { diff --git a/lib/datalib.php b/lib/datalib.php index e18c27f1c3..39f8c7e7f5 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -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) {