Commit d2dbbc79 authored by Richard Mansfield's avatar Richard Mansfield Committed by Nigel McNie
Browse files

Sort search results by clicking column headings

parent 7343764e
......@@ -50,13 +50,17 @@ if ($action == 'search') {
$params = new StdClass;
$params->query = trim(param_variable('query', ''));
$params->offset = param_integer('offset', 0);
$params->limit = param_integer('limit', 10);
$params->institution = param_alpha('institution', null);
$params->f = param_alpha('f', null);
$params->l = param_alpha('l', null);
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
json_headers();
$data['data'] = build_admin_user_search_results($params);
$data['data'] = build_admin_user_search_results($params, $offset, $limit, $sortby, $sortdir);
$data['error'] = false;
$data['message'] = null;
echo json_encode($data);
......
......@@ -36,19 +36,21 @@ require('searchlib.php');
$search = (object) array(
'query' => trim(param_variable('query', '')),
'sort' => param_alpha('sort', 'lastname'),
'institution' => param_alpha('institution', 'all'),
'f' => param_alpha('f', null),
'l' => param_alpha('l', null),
'offset' => param_integer('offset', 0),
'limit' => param_integer('limit', 10)
'f' => param_alpha('f', null), // first initial
'l' => param_alpha('l', null), // last initial
);
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$smarty = smarty(array('adminusersearch'));
$smarty->assign('search', $search);
$smarty->assign('alphabet', explode(',', get_string('alphabet')));
$smarty->assign('institutions', get_records_array('institution'));
$smarty->assign('results', build_admin_user_search_results($search));
$smarty->assign('results', build_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir));
$smarty->display('admin/users/search.tpl');
?>
......@@ -5,6 +5,7 @@ function UserSearch() {
self.rewriteInitials();
self.rewriteQueryButton();
self.rewritePaging();
self.rewriteSorting();
self.rewriteSuspendLinks();
self.params = {};
}
......@@ -60,20 +61,34 @@ function UserSearch() {
e.stop();
};
this.searchByChildLink = function (element) {
var children = getElementsByTagAndClassName('a', null, element);
if (children.length == 1) {
self.params = parseQueryString(getNodeAttribute(children[0], 'href'));
self.doSearch();
}
}
this.changePage = function(e) {
e.stop();
self.searchByChildLink(this);
}
this.rewritePaging = function() {
forEach(getElementsByTagAndClassName('span', 'search-results-page', 'searchresults'), function(i) {
connect(i, 'onclick', self.changePage);
});
}
this.changePage = function(e) {
var children = getElementsByTagAndClassName('a', null, this);
if (children.length == 1) {
var linkparams = parseQueryString(getNodeAttribute(children[0], 'href'));
self.params.offset = linkparams.offset;
self.doSearch();
}
this.sortColumn = function(e) {
e.stop();
self.searchByChildLink(this);
}
this.rewriteSorting = function() {
forEach(getElementsByTagAndClassName('th', 'search-results-sort-column', 'searchresults'), function(i) {
connect(i, 'onclick', self.sortColumn);
});
}
this.rewriteQueryButton = function() {
......@@ -93,6 +108,7 @@ function UserSearch() {
sendjsonrequest('search.json.php', self.params, 'POST', function(data) {
$('results').innerHTML = data.data;
self.rewritePaging();
self.rewriteSorting();
self.rewriteSuspendLinks();
});
}
......
......@@ -117,15 +117,24 @@ function search_user($query_string, $limit, $offset = 0) {
}
function build_admin_user_search_results($search) {
function build_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir) {
$smarty = smarty_core();
$params = array();
foreach ($search as $k => $v) {
if (!empty($v) && $k != 'offset') {
if (!empty($v)) {
$params[] = $k . '=' . $v;
}
}
$paramstring = join('&', $params);
$params[] = 'limit=' . $limit;
$params = join('&', $params);
$smarty->assign_by_ref('params', $params);
$smarty->assign_by_ref('offset', $offset);
$smarty->assign_by_ref('sortby', $sortby);
$smarty->assign_by_ref('sortdir', $sortdir);
$fieldnames = array('firstname','lastname','username','email','institution');
$smarty->assign_by_ref('fieldnames', $fieldnames);
// In admin search, the search string is interpreted as either a
// name search or an email search depending on its contents
......@@ -166,9 +175,8 @@ function build_admin_user_search_results($search) {
'string' => $search->institution);
}
$results = admin_user_search($queries, $constraints, $search->offset, $search->limit);
$results = admin_user_search($queries, $constraints, $offset, $limit, $sortby, $sortdir);
$smarty->assign_by_ref('params', $paramstring);
$results['pages'] = ceil($results['count'] / $results['limit']);
$results['page'] = $results['offset'] / $results['limit']; // $results['pages'];
$lastpage = $results['pages'] - 1;
......@@ -186,12 +194,12 @@ function build_admin_user_search_results($search) {
}
function admin_user_search($queries, $constraints, $offset, $limit) {
function admin_user_search($queries, $constraints, $offset, $limit, $sortfield, $sortdir) {
$plugin = get_config('searchplugin');
//$plugin='internal';
safe_require('search', $plugin);
return call_static_method(generate_class_name('search', $plugin), 'admin_search_user',
$queries, $constraints, $offset, $limit);
$queries, $constraints, $offset, $limit, $sortfield, $sortdir);
}
......
......@@ -237,9 +237,11 @@ class PluginSearchInternal extends PluginSearch {
}
public static function admin_search_user($queries, $constratints, $offset, $limit) {
public static function admin_search_user($queries, $constratints, $offset, $limit,
$sortfield, $sortdir) {
if (is_postgres()) {
return self::admin_search_user_pg($queries, $constratints, $offset, $limit);
return self::admin_search_user_pg($queries, $constratints, $offset, $limit,
$sortfield . ' ' . strtoupper($sortdir));
}
//else if (is_mysql()) {
// return self::admin_search_user_my($query_string, $limit, $offset);
......@@ -250,7 +252,7 @@ class PluginSearchInternal extends PluginSearch {
}
public static function admin_search_user_pg($queries, $constraints, $offset, $limit) {
public static function admin_search_user_pg($queries, $constraints, $offset, $limit, $sort) {
$where = 'WHERE u.id <> 0 AND u.deleted = 0';
$values = array();
......@@ -286,7 +288,7 @@ class PluginSearchInternal extends PluginSearch {
u.active, u.suspendedctime
FROM
{usr} u ' . $where . '
ORDER BY u.firstname, u.lastname, u.id',
ORDER BY ' . $sort,
$values,
$offset,
$limit);
......
......@@ -1571,3 +1571,4 @@ table#initials .initial-letters {
text-decoration: none;
font-weight: bold;
}
......@@ -4,11 +4,9 @@
<thead>
<tr>
<th></th>
<th>{str tag="username"}</th>
<th>{str tag="firstname"}</th>
<th>{str tag="lastname"}</th>
<th>{str tag=email}</th>
<th>{str tag=institution}</th>
{foreach from=$fieldnames item=f}
<th class="search-results-sort-column">{if $f == $sortby}{if $sortdir == 'asc'}&gt;{else}&lt;{/if} {/if}<a href="?{$params}&sortby={$f}&sortdir={if $f == $sortby && $sortdir == 'asc'}desc{else}asc{/if}">{str tag="$f"}</a></th>
{/foreach}
<th></th>
</tr>
</thead>
......@@ -16,9 +14,9 @@
{foreach from=$results.data item=r}
<tr>
<td><img src="{$WWWROOT}thumb.php?type=profileicon&size=40x40&id={$r.id}" alt="{str tag=profileimage}" /></td>
<td><a href="{$WWWROOT}user/view.php?id={$r.id}">{$r.username}</a></td>
<td>{$r.firstname}</td>
<td>{$r.lastname}</td>
<td><a href="{$WWWROOT}user/view.php?id={$r.id}">{$r.username}</a></td>
<td>{$r.email}</td>
<td>{$institutions[$r.institution]->displayname}</td>
<td><a class="suspend-user-link" href="{$WWWROOT}admin/users/suspend.php?id={$r.id}">{str tag=suspenduser section=admin}</a></td>
......@@ -30,15 +28,15 @@
<tr>
<td colspan=7>
{if $results.page > $results.prev}
<span class="search-results-page prev"><a href="?{$params}&amp;offset={$results.limit*$results.prev}">{str tag=prevpage}</a></span>
<span class="search-results-page prev"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$results.limit*$results.prev}">{str tag=prevpage}</a></span>
{/if}
{foreach from=$pagenumbers item=i name=pagenumbers}
{if !$smarty.foreach.pagenumbers.first && $prevpagenum < $i-1}...{/if}
<span class="search-results-page{if $i == $results.page} selected{/if}"><a href="?{$params}&amp;offset={$i*$results.limit}">{$i+1}</a></span>
<span class="search-results-page{if $i == $results.page} selected{/if}"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$i*$results.limit}">{$i+1}</a></span>
{assign var='prevpagenum' value=$i}
{/foreach}
{if $results.page < $results.next}
<span class="search-results-page next"><a href="?{$params}&amp;offset={$results.limit*$results.next}">{str tag=nextpage}</a></span>
<span class="search-results-page next"><a href="?{$params}&amp;sortby={$sortby}&amp;sortdir={$sortdir}&amp;offset={$results.limit*$results.next}">{str tag=nextpage}</a></span>
{/if}
</td>
</tr>
......
......@@ -35,7 +35,7 @@
<form action="" method="post">
<div class="searchform">
<label>Query:
<input type="text" name="query" id="query">
<input type="text" name="query" id="query"{if !empty($search->query)} value="{$search->query}"{/if}>
</label>
{if $USER->get('admin') && !empty($institutions)}
<span class="institutions">
......
{include file="header.tpl"}
{include file="columnfullstart.tpl"}
<h2>{str tag="suspenduser" section="admin"}</h2>
<h3><a href="{$WWWROOT}user/view.php?id={$user->id}">{$user->firstname} {$user->lastname} ({$user->username})</a></h3>
{$form}
{include file="columnfullend.tpl"}
{include file="footer.tpl"}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment