Revision [1228]

This is an old revision of UserAdmin made by DarTar on 2004-09-15 18:50:45.

 

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:


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
 




<?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
    // 1. Send user feedback
    if ($_GET["action"] == "feedback" || $_GET["mail"]) {
        echo $this->Action("feedback");
    } else {
   
        // Main screen: displays user table    
   
        // setting 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"];
       
        // limit records per page
        $l = (!$_POST["l"])? $_GET["l"] : $_POST["l"];
        if (!$_POST["l"] && !$_GET["l"]) $l = 10;
           
        // restrict to search query
        if ($_POST["search"]) $where = "WHERE name LIKE \"%".$_POST["search"]."%\"";
       
        echo $this->Format("==== User Administration ==== --- ---");
       
        // 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"
;
        for ($rec=10; $rec < $numusers; $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\"\n
        maxlength=\"50\" value=\""
.$_POST["search"]."\"/>\n".
        "</form>\n";
        echo $form;
       
        if($s > 0)
        $prev = "<a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s - $l) .
        "\">".($s - $l +1)."-".$s."</a> |  ";
       
        if($numusers > ($s + $l))
        $next = " | <a href=\"" . $page . "?l=".$l."&sort=".$sort."&d=".$d."&s=" . ($s + $l) .
        "\">".($s + $l + 1)."-".($s + 2 * $l)."</a>";
       
        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");

}
?>
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki