Revision [1238]
This is an old revision of UserAdmin made by DarTar on 2004-09-15 20:08:23.
User Administration Tool
I've started writing some code for a user administration tool. I think this might be interesting for wikka-based projects with a large number of registered users.
This action will enable Wikka administrators (or, in the future, users belonging to an Admin group) to have access to several user management functions from a single page:
- Display a pageable, sortable and searchable list of registered users -- done;
- Perform specific actions on single users, like:
- delete users (might be useful in the case of spammers, or obsolete users);
- send user feedback -- done;
- manage user privileges (forthcoming);
- Display user's owned pages (requires a small modification of mypages.php);
- Display user's comments (requires a small modification of recentcomments.php).
- Display user's changes (requires a small modification of mychanges.php);
The code below (to be saved as actions/useradmin.php and used as {{useradmin}} ) is just a very first draft. Contributions and improvements are welcome (especially to make the code lighter).
-- DarTar
Current features (2004-09-15):
- users can be sorted by different fields (click on table header to reverse sort order), searched and paged;
- if the current user is not an administrator, then the lastuser action is displayed
- feedback can be sent to single users. This requires an FeedbackActionUpgrade upgraded version of FeedbackAction
<?php
// USER ADMINISTRATION TOOL
// Displays to the Wikka admins a list of registered users
$page = $PHP_SELF.$this->GetPageTag();
// admin only code
if ($this->IsAdmin($this->GetUser())) {
// Perform specific actions on single users
// A. Delete user (forthcoming...)
// B. Send user feedback
if ($_GET["action"] == "feedback" || $_GET["mail"]) {
echo $this->Action("feedback");
} else {
// C. Main screen: display user table
// limit records per page
$l = (!$_POST["l"])? $_GET["l"] : $_POST["l"];
if (!$_POST["l"] && !$_GET["l"]) $l = 10;
// Set defaults:
// sort field
$sort = (!$_GET["sort"])? "signuptime" : $_GET["sort"];
// sort order
$d = (!$_GET["d"])? "desc" : $_GET["d"];
// start record
$s = (!$_GET["s"])? "0" : $_GET["s"];
// Restrict query to search string
if ($_POST["search"]) $where = "WHERE name LIKE \"%".$_POST["search"]."%\"";
echo $this->Format("==== User Administration ==== --- ---");
// Build pager
// 1. get total number of users
$users = $this->LoadSingle("select count(*) as n FROM ".$this->config["table_prefix"]."users ".$where);
$numusers = $users['n'];
// 2. displays pager form
$form = "<form action=\"".$page."\" method=\"post\">Show\n
<select name=\"l\" onChange=\"form.submit()\">\n";
// 3. pager step set to 10
for ($rec=10; $rec < ($numusers+10); $rec+=10) {
$selected = ($l == $rec)? "selected" : "";
$form .= "<option value=\"".$rec."\" ".$selected.">".$rec."</option>\n";
}
$form .= "</select> records per page :: \n".
"Search user: <input type =\"text\" name=\"search\" size=\"20\" maxlength=\"50\" value=\"".$_POST["search"]."\"/>\n".
"</form>\n";
echo $form;
// 4. build "previous records" link
if($s > 0)
$prev = "<a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s - $l) .
"\">".($s - $l +1)."-".$s."</a> | ";
// 5. build "next records" link
if($numusers > ($s + $l))
$next = " | <a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s + $l) .
"\">".($s + $l + 1)."-".($s + 2 * $l)."</a>";
// 6. print navigation links
echo "[Records (".$numusers."): ".$prev."<b>".($s+1)."-".($s+$l)."</b>".$next." (sorted by: <em>".$sort.", ".$d."</em> )]<br /><br />\n";
// get user data
$userdata = $this->LoadAll("select name, email, signuptime from ".$this->config["table_prefix"]."users ".$where." order by ".$sort." ".$d." limit ".$s.", ".$l);
if ($userdata) {
// print table header
$htmlout = "<table width=\"100%\" border=\"1px\">\n".
" <tr>\n".
" <th><a href=\"".$page.(($sort == "name" && $d == "asc")? "?l=".$l."&sort=name&d=desc" : "?l=".$l."&sort=name&d=asc")."\">Name</a></th>\n".
" <th><a href=\"".$page.(($sort == "email" && $d == "asc")? "?l=".$l."&sort=email&d=desc" : "?l=".$l."&sort=email&d=asc")."\">Email</a></th>\n".
" <th><a href=\"".$page.(($sort == "signuptime" && $d == "desc")? "?l=".$l."&sort=signuptime&d=asc" : "?l=".$l."")."\">Signup Date/Time</a></th>\n".
" <th>Owned Pages</th>\n".
" <th>Actions</th>\n".
" </tr>\n";
// print user table
foreach($userdata as $user) {
$htmlout .= " <tr>\n";
$num = $this->LoadSingle("select count(*) as n from ".$this->config["table_prefix"]."pages where owner='".$user["name"]."' AND latest = 'Y'");
$htmlout .= " <td>".$this->Link($user["name"])."</td>\n <td>".$user["email"]."</td>\n
<td>(".$user["signuptime"].")</td>\n <td><center />"."(".$num["n"].")"."</td>\n <td><center />delete :: <a
href=\"".$page."?user=".$user["name"]."&action=feedback\">feedback</a></td>\n";
$htmlout .= " </tr>\n";
}
$htmlout .= "</table>\n";
print($htmlout);
} else {
echo "<p><span class=\"error\">Sorry, there are no users matching</span> <strong>\"".$_POST["search"]."\"</strong></p>";
}
}
} else {
// displays standard list to normal users
echo $this->Action("lastusers");
}
?>
// USER ADMINISTRATION TOOL
// Displays to the Wikka admins a list of registered users
$page = $PHP_SELF.$this->GetPageTag();
// admin only code
if ($this->IsAdmin($this->GetUser())) {
// Perform specific actions on single users
// A. Delete user (forthcoming...)
// B. Send user feedback
if ($_GET["action"] == "feedback" || $_GET["mail"]) {
echo $this->Action("feedback");
} else {
// C. Main screen: display user table
// limit records per page
$l = (!$_POST["l"])? $_GET["l"] : $_POST["l"];
if (!$_POST["l"] && !$_GET["l"]) $l = 10;
// Set defaults:
// sort field
$sort = (!$_GET["sort"])? "signuptime" : $_GET["sort"];
// sort order
$d = (!$_GET["d"])? "desc" : $_GET["d"];
// start record
$s = (!$_GET["s"])? "0" : $_GET["s"];
// Restrict query to search string
if ($_POST["search"]) $where = "WHERE name LIKE \"%".$_POST["search"]."%\"";
echo $this->Format("==== User Administration ==== --- ---");
// Build pager
// 1. get total number of users
$users = $this->LoadSingle("select count(*) as n FROM ".$this->config["table_prefix"]."users ".$where);
$numusers = $users['n'];
// 2. displays pager form
$form = "<form action=\"".$page."\" method=\"post\">Show\n
<select name=\"l\" onChange=\"form.submit()\">\n";
// 3. pager step set to 10
for ($rec=10; $rec < ($numusers+10); $rec+=10) {
$selected = ($l == $rec)? "selected" : "";
$form .= "<option value=\"".$rec."\" ".$selected.">".$rec."</option>\n";
}
$form .= "</select> records per page :: \n".
"Search user: <input type =\"text\" name=\"search\" size=\"20\" maxlength=\"50\" value=\"".$_POST["search"]."\"/>\n".
"</form>\n";
echo $form;
// 4. build "previous records" link
if($s > 0)
$prev = "<a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s - $l) .
"\">".($s - $l +1)."-".$s."</a> | ";
// 5. build "next records" link
if($numusers > ($s + $l))
$next = " | <a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s + $l) .
"\">".($s + $l + 1)."-".($s + 2 * $l)."</a>";
// 6. print navigation links
echo "[Records (".$numusers."): ".$prev."<b>".($s+1)."-".($s+$l)."</b>".$next." (sorted by: <em>".$sort.", ".$d."</em> )]<br /><br />\n";
// get user data
$userdata = $this->LoadAll("select name, email, signuptime from ".$this->config["table_prefix"]."users ".$where." order by ".$sort." ".$d." limit ".$s.", ".$l);
if ($userdata) {
// print table header
$htmlout = "<table width=\"100%\" border=\"1px\">\n".
" <tr>\n".
" <th><a href=\"".$page.(($sort == "name" && $d == "asc")? "?l=".$l."&sort=name&d=desc" : "?l=".$l."&sort=name&d=asc")."\">Name</a></th>\n".
" <th><a href=\"".$page.(($sort == "email" && $d == "asc")? "?l=".$l."&sort=email&d=desc" : "?l=".$l."&sort=email&d=asc")."\">Email</a></th>\n".
" <th><a href=\"".$page.(($sort == "signuptime" && $d == "desc")? "?l=".$l."&sort=signuptime&d=asc" : "?l=".$l."")."\">Signup Date/Time</a></th>\n".
" <th>Owned Pages</th>\n".
" <th>Actions</th>\n".
" </tr>\n";
// print user table
foreach($userdata as $user) {
$htmlout .= " <tr>\n";
$num = $this->LoadSingle("select count(*) as n from ".$this->config["table_prefix"]."pages where owner='".$user["name"]."' AND latest = 'Y'");
$htmlout .= " <td>".$this->Link($user["name"])."</td>\n <td>".$user["email"]."</td>\n
<td>(".$user["signuptime"].")</td>\n <td><center />"."(".$num["n"].")"."</td>\n <td><center />delete :: <a
href=\"".$page."?user=".$user["name"]."&action=feedback\">feedback</a></td>\n";
$htmlout .= " </tr>\n";
}
$htmlout .= "</table>\n";
print($htmlout);
} else {
echo "<p><span class=\"error\">Sorry, there are no users matching</span> <strong>\"".$_POST["search"]."\"</strong></p>";
}
}
} else {
// displays standard list to normal users
echo $this->Action("lastusers");
}
?>