Skip to content

Commit

Permalink
FIxed up function get_user() which now takes lots of parameters and
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
moodler committed May 14, 2003
1 parent 9a2efc9 commit 5a74165
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 57 deletions.
104 changes: 52 additions & 52 deletions admin/admin.php
Expand Up @@ -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, "");
Expand Down Expand Up @@ -46,10 +48,16 @@
$$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();
Expand Down Expand Up @@ -96,83 +104,75 @@


/// 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();

Expand Down
4 changes: 2 additions & 2 deletions admin/user.php
Expand Up @@ -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 {
Expand Down
30 changes: 27 additions & 3 deletions lib/datalib.php
Expand Up @@ -942,17 +942,42 @@ 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%'
OR lastname LIKE '%$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;

Expand Down Expand Up @@ -1016,7 +1041,6 @@ function get_users_longtimenosee($cutofftime) {
}



/// MODULE FUNCTIONS /////////////////////////////////////////////////

function get_course_mods($courseid) {
Expand Down

0 comments on commit 5a74165

Please sign in to comment.