Commit 6296b731 authored by Ruslan Kabalin's avatar Ruslan Kabalin
Browse files

Using AJAX for search and pagination on Adminster Groups page.


Signed-off-by: default avatarRuslan Kabalin <ruslan.kabalin@luns.net.uk>
parent d2602939
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage core
* @author Ruslan Kabalin <ruslan.kabalin@luns.net.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2010 Lancaster University Network Services Limited
* http://www.luns.net.uk
*
*/
define('INTERNAL', 1);
define('ADMIN', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('group.php');
require_once('searchlib.php');
$query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = 10;
$data = build_grouplist_html($query, $limit, $offset, $count);
$data['count'] = $count;
$data['offset'] = $offset;
$data['query'] = $query;
json_reply(false, array('data' => $data));
?>
......@@ -38,67 +38,45 @@ require_once('searchlib.php');
$query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
$limit = 10;
$data = build_grouplist_html($query, $limit, $offset);
$searchform = pieform(array(
'name' => 'search',
'method' => 'post',
'renderer' => 'oneline',
'elements' => array(
'query' => array(
'type' => 'text',
'defaultvalue' => $query,
),
'search' => array(
'submit' => array(
'type' => 'submit',
'value' => get_string('search'),
),
),
));
function search_submit(Pieform $form, $values) {
redirect(get_config('wwwroot') . 'admin/groups/groups.php' . (!empty($values['query']) ? '?query=' . urlencode($values['query']) : ''));
}
$groups = search_group($query, $limit, $offset, 'all');
if ($ids = array_map(create_function('$a', 'return intval($a->id);'), $groups['data'])) {
$sumsql = "(m.role = 'admin')";
if (is_postgres()) {
$sumsql .= '::int';
}
// Member & admin counts
$ids = join(',', $ids);
$counts = get_records_sql_assoc("
SELECT m.group, COUNT(m.member) AS members, SUM($sumsql) AS admins
FROM {group_member} m
WHERE m.group IN ($ids)
GROUP BY m.group",
array()
);
}
$js = <<< EOF
addLoadEvent(function () {
p = {$data['pagination_js']}
connect('search_submit', 'onclick', function (event) {
replaceChildNodes('messages');
var params = {'query': $('search_query').value};
p.sendQuery(params);
event.stop();
});
});
EOF;
foreach ($groups['data'] as &$group) {
$group->visibility = $group->public ? get_string('Public', 'group') : get_string('Members', 'group');
$group->admins = empty($counts[$group->id]->admins) ? 0 : $counts[$group->id]->admins;
$group->members = empty($counts[$group->id]->members) ? 0 : $counts[$group->id]->members;
}
$pagination = build_pagination(array(
'url' => get_config('wwwroot') . 'admin/groups/groups.php?query=' . $query,
'count' => $groups['count'],
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('group', 'group'),
'resultcounttextplural' => get_string('groups', 'group'),
));
$smarty = smarty();
$smarty = smarty(array('paginator'));
$smarty->assign('PAGEHEADING', TITLE);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('searchform', $searchform);
$smarty->assign('groups', $groups);
$smarty->assign('pagination', $pagination);
$smarty->assign('results', $data);
$smarty->display('admin/groups/groups.tpl');
function search_submit(Pieform $form, $values) {
redirect(get_config('wwwroot') . 'admin/groups/groups.php' . (!empty($values['query']) ? '?query=' . urlencode($values['query']) : ''));
}
?>
......@@ -833,6 +833,59 @@ function group_prepare_usergroups_for_display($groups, $returnto='mygroups') {
}
}
/*
* Used by admin/groups/groups.php and admin/groups/groups.json.php for listing groups.
*/
function build_grouplist_html($query, $limit, $offset, &$count=null) {
$groups = search_group($query, $limit, $offset, 'all');
$count = $groups['count'];
if ($ids = array_map(create_function('$a', 'return intval($a->id);'), $groups['data'])) {
$sumsql = "(m.role = 'admin')";
if (is_postgres()) {
$sumsql .= '::int';
}
// Member & admin counts
$ids = join(',', $ids);
$counts = get_records_sql_assoc("
SELECT m.group, COUNT(m.member) AS members, SUM($sumsql) AS admins
FROM {group_member} m
WHERE m.group IN ($ids)
GROUP BY m.group",
array()
);
}
foreach ($groups['data'] as &$group) {
$group->visibility = $group->public ? get_string('Public', 'group') : get_string('Members', 'group');
$group->admins = empty($counts[$group->id]->admins) ? 0 : $counts[$group->id]->admins;
$group->members = empty($counts[$group->id]->members) ? 0 : $counts[$group->id]->members;
}
$smarty = smarty_core();
$smarty->assign('groups', $groups['data']);
$data = array();
$data['tablerows'] = $smarty->fetch('admin/groups/groupsresults.tpl');
$pagination = build_pagination(array(
'id' => 'admgroupslist_pagination',
'datatable' => 'admgroupslist',
'url' => get_config('wwwroot') . 'admin/groups/groups.php' . (!empty($query) ? '?query=' . urlencode($query) : ''),
'jsonscript' => 'admin/groups/groups.json.php',
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('group', 'group'),
'resultcounttextplural' => get_string('groups', 'group'),
));
$data['pagination'] = $pagination['html'];
$data['pagination_js'] = $pagination['javascript'];
return $data;
}
function group_get_membersearch_data($results, $group, $query, $membershiptype) {
global $USER;
......
{include file="header.tpl"}
{$searchform|safe}
<table id="adminstitutionslist" class="fullwidth">
<table id="admgroupslist" class="fullwidth">
<thead>
<tr>
<th>{str tag="groupname" section="admin"}</th>
......@@ -14,19 +14,9 @@
</tr>
</thead>
<tbody>
{foreach from=$groups.data item=group}
<tr class="{cycle values='r0,r1'}">
<td>{$group->name}</td>
<td class="center">{$group->members}</td>
<td class="center">{$group->admins}</td>
<td class="center">{str tag=name section=grouptype.$group->grouptype}: {str tag=membershiptype.$group->jointype section=group}</td>
<td class="center">{$group->visibility}</td>
<td class="center s"><a class="icon btn-manage" href="{$WWWROOT}admin/groups/manage.php?id={$group->id}">{str tag="groupmanage" section="admin"}</a></td>
<td class="center s"><a class="icon btn-del" href="{$WWWROOT}admin/groups/delete.php?id={$group->id}">{str tag="groupdelete" section="admin"}</a></td>
</tr>
{/foreach}
{$results.tablerows|safe}
</tbody>
</table>
{$pagination.html|safe}
{$results.pagination|safe}
{include file="footer.tpl"}
{foreach from=$groups item=group}
<tr class="{cycle values='r0,r1'}">
<td>{$group->name}</td>
<td class="center">{$group->members}</td>
<td class="center">{$group->admins}</td>
<td class="center">{str tag=name section=grouptype.$group->grouptype}: {str tag=membershiptype.$group->jointype section=group}</td>
<td class="center">{$group->visibility}</td>
<td class="center s"><a class="icon btn-manage" href="{$WWWROOT}admin/groups/manage.php?id={$group->id}">{str tag="groupmanage" section="admin"}</a></td>
<td class="center s"><a class="icon btn-del" href="{$WWWROOT}admin/groups/delete.php?id={$group->id}">{str tag="groupdelete" section="admin"}</a></td>
</tr>
{/foreach}
\ No newline at end of file
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