Commit f3162f80 authored by Son Nguyen's avatar Son Nguyen
Browse files

Be flexible in the number to results shown on a page Bug#781989



1. Added a new feature to build_pagination:
 - setlimit: enable=true/disable=false choosing limit values
 from the array limitoptions
 - the array limitoptions (page sizes) has been hardcoded
inside build_pagination function.

2. Enable this feature to group member search and porfolio pages search
Note: The default number of pages in Porfolio/Pages has been changed to
20

To enable this feature
1. pass 'setlimit' => true to build_pagination function
2. $js = "addLoadEvent(function() {p = {$pagination['javascript']}});";
3. $smarty->assign('INLINEJAVASCRIPT', $js);

Change-Id: Ib44b28d50895c6ea604bb1aedaebae3a3f1bfc1a
Signed-off-by: default avatarSon Nguyen <son.nguyen@catalyst.net.nz>
parent 6bb50530
......@@ -75,11 +75,13 @@ if ($remove && $member) {
// Search related stuff for member pager
$query = trim(param_variable('query', ''));
// pagination params
$setlimit = true; //Enable choosing page size; list of page sizes has been predefined in function build_pagination()
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$results = get_group_user_search_results($group->id, $query, $offset, $limit, $membershiptype);
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $group->id, $query, $membershiptype);
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $group->id, $query, $membershiptype, $setlimit);
// Type-specific instructions
$instructions = '';
......@@ -104,6 +106,10 @@ $searchform = pieform(array(
'type' => 'hidden',
'value' => $membershiptype
),
'setlimit' => array(
'type' => 'hidden',
'value' => $setlimit
),
'query' => array(
'type' => 'text',
'defaultvalue' => $query
......@@ -120,7 +126,10 @@ addLoadEvent(function () {
p = {$pagination['javascript']}
connect('search_submit', 'onclick', function (event) {
replaceChildNodes('messages');
var params = {'query': $('search_query').value, 'id':$('search_id').value, 'membershiptype':$('search_membershiptype').value};
var params = {'query': $('search_query').value, 'id':$('search_id').value,
'membershiptype':$('search_membershiptype').value,
'setlimit':$('search_setlimit').value
};
p.sendQuery(params);
event.stop();
});
......@@ -162,5 +171,8 @@ $smarty->assign('membershiptype', $membershiptype);
$smarty->display('group/members.tpl');
function search_submit(Pieform $form, $values) {
redirect('/group/members.php?id=' . $values['id'] . (!empty($values['query']) ? '&query=' . urlencode($values['query']) : '') . (!empty($values['membershiptype']) ? '&membershiptype=' . urlencode($values['membershiptype']) : ''));
redirect('/group/members.php?id=' . $values['id'] .
(!empty($values['query']) ? '&query=' . urlencode($values['query']) : '') .
(!empty($values['membershiptype']) ? '&membershiptype=' . urlencode($values['membershiptype']) : '') .
(!empty($values['setlimit']) ? '&setlimit=' . urlencode($values['setlimit']) : ''));
}
......@@ -34,6 +34,7 @@ require_once('searchlib.php');
$id = param_integer('id');
$query = trim(param_variable('query', ''));
$setlimit = param_boolean('setlimit', false);
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
......@@ -77,7 +78,7 @@ if (!param_integer('html', 1)) {
json_reply(false, $results);
}
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $id, $query, $membershiptype);
list($html, $pagination, $count, $offset, $membershiptype) = group_get_membersearch_data($results, $id, $query, $membershiptype, $setlimit);
json_reply(false, array(
'message' => null,
......@@ -88,6 +89,7 @@ json_reply(false, array(
'count' => $count,
'results' => $count . ' ' . ($count == 1 ? get_string('result') : get_string('results')),
'offset' => $offset,
'setlimit' => $setlimit,
'membershiptype' => $membershiptype,
)
));
......@@ -36,11 +36,17 @@ var Paginator = function(id, datatable, script, extradata) {
this.init = function(id, datatable, script, extradata) {
self.id = id;
self.datatable = $(datatable);
self.jsonScript = config['wwwroot'] + script;
self.extraData = extradata;
if (script && script.length !== 0) {
self.datatable = $(datatable);
self.jsonScript = config['wwwroot'] + script;
self.extraData = extradata;
self.rewritePaginatorLinks();
self.rewritePaginatorLinks();
self.rewritePaginatorSelectForm();
}
else {
self.rewritePaginatorSelectFormWithoutJSON();
}
};
this.rewritePaginatorLinks = function() {
......@@ -54,6 +60,50 @@ var Paginator = function(id, datatable, script, extradata) {
});
};
this.rewritePaginatorSelectFormWithoutJSON = function() {
var setlimitform = getFirstElementByTagAndClassName('form', 'pagination', self.id);
// If there is a form for choosing page size(page limit)
if (setlimitform) {
var setlimitselect = getFirstElementByTagAndClassName('select', 'pagination', setlimitform);
connect (setlimitselect, 'onchange', function(e) {
e.stop();
var url = setlimitform.action;
if (url.indexOf('?') != -1) {
url += "&";
}
else {
url += "?";
}
url += this.name + "=" + this.value;
location.assign(url);
});
}
};
this.rewritePaginatorSelectForm = function() {
var setlimitform = getFirstElementByTagAndClassName('form', 'pagination', self.id);
// If there is a form for choosing page size(page limit)
if (setlimitform) {
var setlimitselect = getFirstElementByTagAndClassName('select', 'pagination', setlimitform);
connect (setlimitselect, 'onchange', function(e) {
e.stop();
var url = setlimitform.action
var loc = url.indexOf('?');
var queryData = [];
if (loc != -1) {
queryData = parseQueryString(url.substring(loc + 1, url.length));
queryData.setlimit = "1";
queryData.limit = setlimitselect.value;
queryData.extradata = serializeJSON(self.extraData);
}
self.sendQuery(queryData);
});
}
};
this.updateResults = function (data) {
var container = self.datatable;
if (self.datatable.tagName == 'TABLE') {
......
......@@ -92,6 +92,7 @@ $string['first'] = 'First';
$string['firstpage'] = 'First page';
$string['last'] = 'Last';
$string['lastpage'] = 'Last page';
$string['maxitemsperpage'] = 'Max items per page:';
$string['accept'] = 'Accept';
$string['memberofinstitutions'] = 'Member of %s';
......
......@@ -1423,14 +1423,13 @@ function build_grouplist_html($query, $limit, $offset, &$count=null) {
return $data;
}
function group_get_membersearch_data($results, $group, $query, $membershiptype) {
function group_get_membersearch_data($results, $group, $query, $membershiptype, $setlimit=false) {
global $USER;
$params = array();
if (!empty($query)) {
$params[] = 'query=' . $query;
}
$params[] = 'limit=' . $results['limit'];
if (!empty($membershiptype)) {
$params[] = 'membershiptype=' . $membershiptype;
}
......@@ -1481,6 +1480,7 @@ function group_get_membersearch_data($results, $group, $query, $membershiptype)
'class' => 'center',
'url' => $searchurl,
'count' => $results['count'],
'setlimit' => $setlimit,
'limit' => $results['limit'],
'offset' => $results['offset'],
'jumplinks' => 8,
......
......@@ -2656,8 +2656,11 @@ class View {
public static function views_by_owner($group=null, $institution=null) {
$limit = param_integer('limit', 25);
// Pagination configuration
$setlimit = true;
$limit = param_integer('limit', 20);
$offset = param_integer('offset', 0);
$query = param_variable('query', null);
$tag = param_variable('tag', null);
......@@ -2690,6 +2693,10 @@ class View {
'options' => $searchoptions,
'defaultvalue' => $searchtype,
),
'setlimit' => array(
'type' => 'hidden',
'value' => $setlimit
),
'submit' => array(
'type' => 'submit',
'value' => get_string('search')
......@@ -2714,6 +2721,7 @@ class View {
'url' => $url,
'count' => $data->count,
'limit' => $limit,
'setlimit' => $setlimit,
'offset' => $offset,
'jumplinks' => 6,
'numbersincludeprevnext' => 2,
......
......@@ -3035,6 +3035,7 @@ function str_shorten_text($str, $maxlen=100, $truncate=false) {
*
* - url: The base URL to use for all links
* - count: The total number of results to paginate for
* - setlimit: toggle variable for enabling/disabling limit dropbox, default value = false
* - limit: How many to show per page
* - offset: At which result to start showing results
*
......@@ -3055,6 +3056,7 @@ function str_shorten_text($str, $maxlen=100, $truncate=false) {
and current- and last page
* - resultcounttextsingular: The text to use for 'result'
* - resultcounttextplural: The text to use for 'results'
* - limittext: The text to use for the limitoption, e.g. "Max items per page" or "Page size"
*
* Optional options to support javascript pagination include:
*
......@@ -3069,6 +3071,7 @@ function str_shorten_text($str, $maxlen=100, $truncate=false) {
* @param array $params Options for the pagination
*/
function build_pagination($params) {
$limitoptions = array(10, 20, 50, 100, 500);
// Bail if the required attributes are not present
$required = array('url', 'count', 'limit', 'offset');
foreach ($required as $option) {
......@@ -3077,6 +3080,18 @@ function build_pagination($params) {
}
}
if (isset($params['setlimit']) && $params['setlimit']) {
if (!in_array($params['limit'], $limitoptions)) {
$params['limit'] = 10;
}
if (!isset($params['limittext'])) {
$params['limittext'] = get_string('maxitemsperpage');
}
}
else {
$params['setlimit'] = false;
}
// Work out default values for parameters
if (!isset($params['id'])) {
$params['id'] = substr(md5(microtime()), 0, 4);
......@@ -3205,9 +3220,8 @@ function build_pagination($params) {
// Build the first/previous links
$isfirst = $page == 0;
$output .= build_pagination_pagelink('first', $params['url'], 0, '&laquo; ' . $params['firsttext'], get_string('firstpage'), $isfirst, $params['offsetname']);
$output .= build_pagination_pagelink('prev', $params['url'], $params['limit'] * $prev,
'&larr; ' . $params['previoustext'], get_string('prevpage'), $isfirst, $params['offsetname']);
$output .= build_pagination_pagelink('first', $params['url'], $params['setlimit'], $params['limit'], 0, '&laquo; ' . $params['firsttext'], get_string('firstpage'), $isfirst, $params['offsetname']);
$output .= build_pagination_pagelink('prev', $params['url'], $params['setlimit'], $params['limit'], $params['limit'] * $prev, '&larr; ' . $params['previoustext'], get_string('prevpage'), $isfirst, $params['offsetname']);
// Build the pagenumbers in the middle
foreach ($pagenumbers as $k => $i) {
......@@ -3218,7 +3232,7 @@ function build_pagination($params) {
$output .= '<span class="selected">' . ($i + 1) . '</span>';
}
else {
$output .= build_pagination_pagelink('', $params['url'],
$output .= build_pagination_pagelink('', $params['url'], $params['setlimit'], $params['limit'],
$params['limit'] * $i, $i + 1, '', false, $params['offsetname']);
}
$prevpagenum = $i;
......@@ -3226,22 +3240,46 @@ function build_pagination($params) {
// Build the next/last links
$islast = $page == $last;
$output .= build_pagination_pagelink('next', $params['url'], $params['limit'] * $next,
$output .= build_pagination_pagelink('next', $params['url'], $params['setlimit'], $params['limit'], $params['limit'] * $next,
$params['nexttext'] . ' &rarr;', get_string('nextpage'), $islast, $params['offsetname']);
$output .= build_pagination_pagelink('last', $params['url'], $params['limit'] * $last,
$output .= build_pagination_pagelink('last', $params['url'], $params['setlimit'], $params['limit'], $params['limit'] * $last,
$params['lasttext'] . ' &raquo;', get_string('lastpage'), $islast, $params['offsetname']);
}
// Build limitoptions dropbox
if ($params['setlimit']) {
$strlimitoptions = array();
$limit = $params['limit'];
for ($i = 0; $i < count($limitoptions); $i++) {
if ($limit == $limitoptions[$i]) {
$strlimitoptions[] = "<option value = '$limit' selected='selected'> $limit </option>";
}
else {
$strlimitoptions[] = "<option value = '$limitoptions[$i]'> $limitoptions[$i] </option>";
}
}
$output .= '<form class="pagination" action="' . $params['url'] . '" method="POST">
<span class="pagination"> ' . $params['limittext'] . '</span>' .
'<select id="setlimitselect" class="pagination" name="limit"> '.
join(' ', $strlimitoptions) .
'</select>
<input class="pagination js-hidden" type="submit" name="submit" value="' . get_string('change') . '"/>
</form>';
}
// Work out what javascript we need for the paginator
$js = '';
$id = json_encode($params['id']);
if (isset($params['jsonscript']) && isset($params['datatable'])) {
$paginator_js = hsc(get_config('wwwroot') . 'js/paginator.js');
$id = json_encode($params['id']);
$datatable = json_encode($params['datatable']);
$jsonscript = json_encode($params['jsonscript']);
$extradata = json_encode($params['extradata']);
$js .= "new Paginator($id, $datatable, $jsonscript, $extradata);";
}
else {
$js .= "new Paginator($id, null, null, null);";
}
// Output the count of results
$resultsstr = ($params['count'] == 1) ? $params['resultcounttextsingular'] : $params['resultcounttextplural'];
......@@ -3258,12 +3296,16 @@ function build_pagination($params) {
* Used by build_pagination to build individual links. Shouldn't be used
* elsewhere.
*/
function build_pagination_pagelink($class, $url, $offset, $text, $title, $disabled=false, $offsetname='offset') {
function build_pagination_pagelink($class, $url, $setlimit, $limit, $offset, $text, $title, $disabled=false, $offsetname='offset') {
$return = '<span class="pagination';
$return .= ($class) ? " $class" : '';
$url = (false === strpos($url, '?')) ? $url . '?' : $url . '&amp;';
$url .= "$offsetname=$offset";
if ($setlimit) {
$url .= '&amp;' . "setlimit=$setlimit";
$url .= '&amp;' . "limit=$limit";
}
if ($disabled) {
$return .= ' disabled">' . $text . '</span>';
......
......@@ -317,8 +317,10 @@ tr.profileicon td.filename {
font-style: italic;
}
/**************************** Pagination ****************************/
.pagination {
font-size: .9167em;
}
div.pagination {
font-size: .9167em;
text-align: center;
margin: 3px 0;
padding: 5px;
......@@ -352,6 +354,9 @@ div.pagination .results {
div.pagination .results a {
border: 0;
}
form.pagination {
display: inline;
}
#feedback_pagination span.pagination a,
.blockinstance-content div.center div.pagination span.pagination a {
background-color: #FFFFFF;
......
......@@ -38,10 +38,15 @@ require_once('pieforms/pieform.php');
define('TITLE', get_string('Views', 'view'));
list($searchform, $data, $pagination) = View::views_by_owner();
$js = <<< EOF
addLoadEvent(function () {
p = {$pagination['javascript']}
});
EOF;
$createviewform = pieform(create_view_form());
$smarty = smarty();
$smarty = smarty(array('paginator'));
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('views', $data->data);
$smarty->assign('pagination', $pagination['html']);
$smarty->assign('PAGEHEADING', TITLE);
......
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