Revision [1475]
This is an old revision of UserAdmin made by DarTar on 2004-09-29 17:38:13.
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 -- done;
- Display user's comments -- done.
- Display user's changes -- done;
- other...
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-16):
- 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.php action is displayed
- feedback can be sent to single users.
- displays pages owned by each user.
- displays pages changed by each user.
- displays pages commented by each user.
Files needed (2004-09-16):
- actions/useradmin.php: see code on this page;
- FeedbackActionUpgrade actions/userfeedback.php (updated version of FeedbackAction);
- UserPagesAction actions/userpages.php (updated version of mypages.php);
- UserChangesAction actions/userchanges.php (updated version of mychanges.php);
- UserCommentsAction actions/usercomments.php (new action);
actions/useradmin.php
<?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("userfeedback");
// C. Show user owned pages
} else if ($_GET["action"] == "owned") {
echo $this->Action("userpages");
// D. Show user modified pages
} else if ($_GET["action"] == "changes") {
echo $this->Action("userchanges");
// E. Show user comments
} else if ($_GET["action"] == "comments") {
echo $this->Action("usercomments");
} else {
// F. 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. display pager form
$form = "<form action=\"".$page."\" method=\"post\">Show\n
<select name=\"l\" onChange=\"form.submit()\">\n";
// 3. build pager menu (option 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. last step: print navigation bar
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) {
// 1. 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</th>\n".
" <th>Modified</th>\n".
" <th>Comments</th>\n".
" <th>Actions</th>\n".
" </tr>\n";
// 2. print user table
foreach($userdata as $user) {
$htmlout .= " <tr>\n";
// get number n of owned pages
$num = $this->LoadSingle("select count(*) as n from ".$this->config["table_prefix"]."pages where owner='".$user["name"]."' AND latest = 'Y'");
// get number m of modified pages
$numchanges = $this->LoadSingle("select count(*) as m from ".$this->config["table_prefix"]."pages where user='".$user["name"]."'");
// get number q of comments
$numcomments = $this->LoadSingle("select count(*) as q from ".$this->config["table_prefix"]."comments where user='".$user["name"]."'");
$htmlout .= " <td>".$this->Link($user["name"])."</td>\n <td>".$user["email"]."</td>\n <td>(".$user["signuptime"].")</td>\n <td><center />"."(<a href=\"".$page."?user=".$user["name"]."&action=owned\">".$num["n"]."</a>)"."</td>\n <td><center />(<a href=\"".$page."?user=".$user["name"]."&action=changes\">".$numchanges["m"]."</a>)</td>\n <td><center />(<a href=\"".$page."?user=".$user["name"]."&action=comments\">".$numcomments["q"]."</a>)</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 {
// 3. print error message for search results
echo "<p><span class=\"error\">Sorry, there are no users matching</span> <strong>\"".$_POST["search"]."\"</strong></p>";
}
}
} else {
// G. Displays standard user list to non-admins
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("userfeedback");
// C. Show user owned pages
} else if ($_GET["action"] == "owned") {
echo $this->Action("userpages");
// D. Show user modified pages
} else if ($_GET["action"] == "changes") {
echo $this->Action("userchanges");
// E. Show user comments
} else if ($_GET["action"] == "comments") {
echo $this->Action("usercomments");
} else {
// F. 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. display pager form
$form = "<form action=\"".$page."\" method=\"post\">Show\n
<select name=\"l\" onChange=\"form.submit()\">\n";
// 3. build pager menu (option 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. last step: print navigation bar
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) {
// 1. 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</th>\n".
" <th>Modified</th>\n".
" <th>Comments</th>\n".
" <th>Actions</th>\n".
" </tr>\n";
// 2. print user table
foreach($userdata as $user) {
$htmlout .= " <tr>\n";
// get number n of owned pages
$num = $this->LoadSingle("select count(*) as n from ".$this->config["table_prefix"]."pages where owner='".$user["name"]."' AND latest = 'Y'");
// get number m of modified pages
$numchanges = $this->LoadSingle("select count(*) as m from ".$this->config["table_prefix"]."pages where user='".$user["name"]."'");
// get number q of comments
$numcomments = $this->LoadSingle("select count(*) as q from ".$this->config["table_prefix"]."comments where user='".$user["name"]."'");
$htmlout .= " <td>".$this->Link($user["name"])."</td>\n <td>".$user["email"]."</td>\n <td>(".$user["signuptime"].")</td>\n <td><center />"."(<a href=\"".$page."?user=".$user["name"]."&action=owned\">".$num["n"]."</a>)"."</td>\n <td><center />(<a href=\"".$page."?user=".$user["name"]."&action=changes\">".$numchanges["m"]."</a>)</td>\n <td><center />(<a href=\"".$page."?user=".$user["name"]."&action=comments\">".$numcomments["q"]."</a>)</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 {
// 3. print error message for search results
echo "<p><span class=\"error\">Sorry, there are no users matching</span> <strong>\"".$_POST["search"]."\"</strong></p>";
}
}
} else {
// G. Displays standard user list to non-admins
echo $this->Action("lastusers");
}
?>
CategoryDevelopment