Commit fdef4406 authored by Richard Mansfield's avatar Richard Mansfield Committed by Gerrit Code Review
Browse files

Merge "Refactored admin user search pagination"

parents af850a3c 839e0068
......@@ -41,14 +41,14 @@ if ($action == 'search') {
$params->institution = param_alphanum('institution', null);
$params->f = param_alpha('f', null);
$params->l = param_alpha('l', null);
$params->sortby = param_alpha('sortby', 'firstname');
$params->sortdir = param_alpha('sortdir', 'asc');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
$data = array();
$data['data'] = build_admin_user_search_results($params, $offset, $limit, $sortby, $sortdir);
$data['data'] = build_admin_user_search_results($params, $offset, $limit);
$data['error'] = false;
$data['message'] = null;
json_reply(false, $data);
......
......@@ -39,10 +39,10 @@ $search = (object) array(
'query' => trim(param_variable('query', '')),
'f' => param_alpha('f', null), // first initial
'l' => param_alpha('l', null), // last initial
'sortby' => param_alpha('sortby', 'firstname'),
'sortdir' => param_alpha('sortdir', 'asc'),
);
$sortby = param_alpha('sortby', 'firstname');
$sortdir = param_alpha('sortdir', 'asc');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
......@@ -57,6 +57,6 @@ $smarty = smarty(array('adminusersearch'));
$smarty->assign('search', $search);
$smarty->assign('alphabet', explode(',', get_string('alphabet')));
$smarty->assign('institutions', $institutions);
$smarty->assign('results', build_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir));
$smarty->assign('results', build_admin_user_search_results($search, $offset, $limit));
$smarty->assign('PAGEHEADING', TITLE);
$smarty->display('admin/users/search.tpl');
<?php
/**
* Dwoo {mahara_pagelinks} function pluging
*
* appends an 'offset=n' parameter to the url to get the url for a different page
* @param integer $limit number of items per page
* @param integer $offset offset of first data item on this page
* @param integer $count total number of items
* @param string $url where to get results from
* @param string $assign the template var to assign the result to
* @return string
*
* THIS IS DEPRECATED. Pagination is done differently almost everywhere else
* (e.g. find friends). One place this is being used is the admin user search.
* Hopefully that can be converted away soon.
*/
function Dwoo_Plugin_mahara_pagelinks(Dwoo $dwoo, $offset, $limit, $count, $url, $assign='') {
$offset = param_integer('offset', 0);
$limit = intval($limit);
$count = intval($count);
$url = hsc($url);
$id = substr(md5(microtime()), 0, 4);
$output = '<div class="pagination" id="' . $id . '">';
if ($limit <= $count) {
$pages = ceil($count / $limit);
$page = $offset / $limit;
$last = $pages - 1;
$next = min($last, $page + 1);
$prev = max(0, $page - 1);
// Build a list of what pagenumbers will be put between the previous/next links
$pagenumbers = array(0, $prev, $page, $next, $last);
$pagenumbers = array_unique($pagenumbers);
// Build the first/previous links
$isfirst = $page == 0;
$output .= mahara_pagelink('first', $url, 0, '&laquo; ' . get_string('first'), get_string('firstpage'), $isfirst);
$output .= mahara_pagelink('prev', $url, $limit * $prev, '&larr; ' . get_string('previous'), get_string('prevpage'), $isfirst);
// Build the pagenumbers in the middle
foreach ($pagenumbers as $k => $i) {
if ($k != 0 && $prevpagenum < $i - 1) {
$output .= '…';
}
if ($i == $page) {
$output .= '<span class="selected">' . ($i + 1) . '</span>';
}
else {
$output .= mahara_pagelink('', $url, $limit * $i, $i + 1, '', false);
}
$prevpagenum = $i;
}
// Build the next/last links
$islast = $page == $last;
$output .= mahara_pagelink('next', $url, $limit * $next, get_string('next') . ' &rarr;', get_string('nextpage'), $islast);
$output .= mahara_pagelink('last', $url, $limit * $last, get_string('last') . ' &raquo;', get_string('lastpage'), $islast);
}
// Close the container div
$output .= '</div>';
if (!empty($assign)) {
$dwoo->assignInScope($output, $assign);
return;
}
return $output;
}
function mahara_pagelink($class, $url, $offset, $text, $title, $disabled=false) {
$return = '<span class="pagination';
$return .= ($class) ? " $class" : '';
if ($disabled) {
$return .= ' disabled">' . $text . '</span>';
}
else {
$return .= '">'
. '<a href="' . $url . '&offset=' . $offset
. '" title="' . $title . '">' . $text . '</a></span>';
}
return $return;
}
......@@ -149,7 +149,7 @@ function parse_name_query($text) {
}
function get_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir) {
function get_admin_user_search_results($search, $offset, $limit) {
$plugin = get_config('searchplugin');
safe_require('search', $plugin);
......@@ -247,7 +247,7 @@ function get_admin_user_search_results($search, $offset, $limit, $sortby, $sortd
$results = call_static_method(
generate_class_name('search', $plugin), 'admin_search_user',
$queries, $constraints, $offset, $limit, $sortby, $sortdir
$queries, $constraints, $offset, $limit, $search->sortby, $search->sortdir
);
if ($results['count']) {
......@@ -263,11 +263,9 @@ function get_admin_user_search_results($search, $offset, $limit, $sortby, $sortd
}
function build_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir) {
function build_admin_user_search_results($search, $offset, $limit) {
global $USER, $THEME;
$results = get_admin_user_search_results($search, $offset, $limit, $sortby, $sortdir);
$params = array();
foreach ($search as $k => $v) {
if (!empty($v)) {
......@@ -276,6 +274,19 @@ function build_admin_user_search_results($search, $offset, $limit, $sortby, $sor
}
$searchurl = get_config('wwwroot') . 'admin/users/search.php?' . join('&', $params) . '&limit=' . $limit;
$results = get_admin_user_search_results($search, $offset, $limit);
$results['pagination'] = build_pagination(array(
'id' => 'admin_usersearch_pagination',
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'limit' => $limit,
'offset' => $offset,
'datatable' => 'searchresults',
'jsonscript' => 'admin/users/search.json.php',
));
$cols = array(
'icon' => array(
'template' => 'admin/users/searchiconcolumn.tpl',
......@@ -320,9 +331,8 @@ function build_admin_user_search_results($search, $offset, $limit, $sortby, $sor
$smarty->assign_by_ref('institutions', $institutions);
$smarty->assign('USER', $USER);
$smarty->assign('searchurl', $searchurl);
$smarty->assign('sortby', $sortby);
$smarty->assign('sortdir', $sortdir);
$smarty->assign('pagebaseurl', $searchurl . '&sortby=' . $sortby . '&sortdir=' . $sortdir);
$smarty->assign('sortby', $search->sortby);
$smarty->assign('sortdir', $search->sortdir);
$smarty->assign('limitoptions', array(10, 50, 100, 200, 500));
$smarty->assign('cols', $cols);
$smarty->assign('ncols', count($cols));
......
......@@ -8,14 +8,11 @@
<h2>{str tag="Results"}</h2>
<table id="searchresults" class="tablerenderer fullwidth listing">
<thead>
{mahara_pagelinks offset=$results.offset limit=$results.limit count=$results.count url=$pagebaseurl assign=pagelinks}
{if ($pagelinks)}
<tr class="search-results-pages">
<td colspan="{$ncols}">
{$pagelinks|safe}
{$results.pagination.html|safe}
</td>
</tr>
{/if}
<tr>
{foreach from=$cols key=f item=c}
<th class="{if $c.sort}search-results-sort-column{if $f == $sortby} {$sortdir}{/if}{/if}{if $c.class} {$c.class}{/if}">
......@@ -44,15 +41,13 @@
</tr>
{/foreach}
</tbody>
{if $pagelinks}
<tfoot>
<tr class="search-results-pages">
<td colspan={$ncols}>
{$pagelinks|safe}
{$results.pagination.html|safe}
</td>
</tr>
</tfoot>
{/if}
</table>
{else}
<div>{str tag="noresultsfound"}</div>
......
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