Commit 9bb25d28 authored by Ruslan Kabalin's avatar Ruslan Kabalin
Browse files

Added search box and pagination to Administer Institutions page


Signed-off-by: default avatarRuslan Kabalin <ruslan.kabalin@luns.net.uk>
parent 9c0894f8
<?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('INSTITUTIONALADMIN', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
$query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = 20;
// Get a list of institutions
require_once(get_config('libroot') . 'institution.php');
if (!$USER->get('admin')) { // Filter the list for institutional admins
$filter = $USER->get('admininstitutions');
$showdefault = false;
}
else {
$filter = false;
$showdefault = true;
}
$data = build_institutions_html($filter, $showdefault, $query, $limit, $offset, $count);
$data['count'] = $count;
$data['limit'] = $limit;
$data['offset'] = $offset;
$data['query'] = $query;
json_reply(false, array('data' => $data));
?>
......@@ -33,12 +33,17 @@ define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'institutions');
require_once('pieforms/pieform.php');
define('MENUITEM', 'manageinstitutions/institutions');
$smarty = smarty(array('lib/pieforms/static/core/pieforms.js', 'paginator'));
$institution = param_variable('i', '');
$add = param_boolean('add');
$edit = param_boolean('edit');
$delete = param_boolean('delete');
$query = param_variable('query', '');
$offset = param_integer('offset', 0);
$limit = 20;
if (!$USER->get('admin')) {
// Institutional admins with only 1 institution go straight to the edit page for that institution
// They cannot add or delete institutions, or edit an institution they don't administer
......@@ -353,7 +358,40 @@ else {
$filter = false;
$showdefault = true;
}
$institutions = Institution::count_members($filter, $showdefault);
$data = build_institutions_html($filter, $showdefault, $query, $limit, $offset, $count);
$smarty->assign('results', $data);
$smarty->assign('countinstitutions', $count);
/*search institution form*/
$searchform = pieform(array(
'name' => 'search',
'renderer' => 'oneline',
'elements' => array(
'query' => array(
'type' => 'text',
'defaultvalue' => $query
),
'submit' => array(
'type' => 'submit',
'value' => get_string('search')
)
)
));
$smarty->assign('searchform', $searchform);
$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;
$smarty->assign('INLINEJAVASCRIPT', $js);
}
function institution_validate(Pieform $form, $values) {
......@@ -573,8 +611,6 @@ if ($institution && $institution != 'mahara') {
}
}
$smarty = smarty();
if (isset($institutionform)) {
$smarty->assign('institution_form', $institutionform);
$smarty->assign('instancestring', $instancestring);
......@@ -582,7 +618,6 @@ if (isset($institutionform)) {
}
else {
$smarty->assign('siteadmin', $USER->get('admin'));
$smarty->assign('institutions', $institutions);
}
if (isset($suspended)) {
......
......@@ -401,14 +401,55 @@ class Institution {
return ($this->maxuseraccounts != '') && ($this->countMembers() >= $this->maxuseraccounts);
}
public static function count_members($filter, $showdefault) {
/**
* Returns the list of institutions, implements institution searching
*
* @param array Limit the output to only institutions in this array (used for institution admins).
* @param bool Whether default institution should be listed in results.
* @param string Searching query string.
* @param int Limit of results (used for pagination).
* @param int Offset of results (used for pagination).
* @param int Returns the total number of results.
* @return array A data structure containing results looking like ...
* $institutions = array(
* name => array(
* displayname => string
* maxuseraccounts => integer
* members => integer
* staff => integer
* admins => integer
* name => string
* ),
* name => array(...),
* );
*/
public static function count_members($filter, $showdefault, $query='', $limit=null, $offset=null, &$count=null) {
if ($filter) {
$where = '
WHERE ii.name IN (' . join(',', array_map('db_quote', $filter)) . ')';
AND ii.name IN (' . join(',', array_map('db_quote', $filter)) . ')';
}
else {
$where = '';
}
$querydata = split(' ', preg_replace('/\s\s+/', ' ', strtolower(trim($query))));
$namesql = '(
ii.name ' . db_ilike() . ' \'%\' || ? || \'%\'
)
OR (
ii.displayname ' . db_ilike() . ' \'%\' || ? || \'%\'
)';
$namesql = join(' OR ', array_fill(0, count($querydata), $namesql));
$queryvalues = array();
foreach ($querydata as $w) {
$queryvalues = array_pad($queryvalues, count($queryvalues) + 2, $w);
}
$count = count_records_sql('SELECT COUNT(ii.name)
FROM {institution} ii
WHERE' . $namesql, $queryvalues
);
$institutions = get_records_sql_assoc('
SELECT
ii.name,
......@@ -432,11 +473,12 @@ class Institution {
(u.deleted = 0 OR u.id IS NULL)
GROUP BY
i.name, i.displayname, i.maxuseraccounts
) a ON (a.name = ii.name)' . $where . '
) a ON (a.name = ii.name)
WHERE (' . $namesql . ')' . $where . '
ORDER BY
ii.name = \'mahara\', ii.displayname', array());
ii.name = \'mahara\', ii.displayname', $queryvalues, $offset, $limit);
if ($showdefault) {
if ($showdefault && $institutions && array_key_exists('mahara', $institutions)) {
$defaultinstmembers = count_records_sql('
SELECT COUNT(u.id) FROM {usr} u LEFT OUTER JOIN {usr_institution} i ON u.id = i.usr
WHERE u.deleted = 0 AND i.usr IS NULL AND u.id != 0
......@@ -533,4 +575,32 @@ EOF;
'institutionselectorjs' => $js
);
}
function build_institutions_html($filter, $showdefault, $query, $limit, $offset, &$count=null) {
global $USER;
$institutions = Institution::count_members($filter, $showdefault, $query, $limit, $offset, $count);
$smarty = smarty_core();
$smarty->assign('institutions', $institutions);
$smarty->assign('siteadmin', $USER->get('admin'));
$data['tablerows'] = $smarty->fetch('admin/users/institutionsresults.tpl');
$pagination = build_pagination(array(
'id' => 'adminstitutionslist_pagination',
'datatable' => 'adminstitutionslist',
'url' => get_config('wwwroot') . 'admin/users/institutions.php' . (!empty($query) ? '?query=' . urlencode($query) : ''),
'jsonscript' => 'admin/users/institutions.json.php',
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'resultcounttextsingular' => get_string('institution', 'admin'),
'resultcounttextplural' => get_string('institutions', 'admin'),
));
$data['pagination'] = $pagination['html'];
$data['pagination_js'] = $pagination['javascript'];
return $data;
}
?>
......@@ -34,6 +34,7 @@
{/if}
{else}
{$searchform|safe}
<table id="adminstitutionslist" class="fullwidth">
<thead>
<tr>
......@@ -55,7 +56,7 @@
</form>
{/if}
</td>
<td colspan="5" class="institutionedituserbuttons right">{if count($institutions) > 1}
<td colspan="5" class="institutionedituserbuttons right">{if $countinstitutions > 1}
<form action="{$WWWROOT}admin/users/institutionusers.php" method="post">
<input type="submit" class="submit" name="editmembers" value="{str tag="editmembers" section="admin"}">
</form>
......@@ -70,32 +71,12 @@
</tr>
</tfoot>
<tbody>
{foreach from=$institutions item=institution}
<tr class="{cycle values='r0,r1'}">
<td>{$institution->displayname}</td>
<td class="center">
{if $institution->name != 'mahara'}
<a href="{$WWWROOT}admin/users/institutionusers.php?usertype=members&amp;institution={$institution->name}">{$institution->members}</a>
{else}
<a href="{$WWWROOT}admin/users/search.php?institution=mahara">{$institution->members}</a>
{/if}
</td>
<td class="center">{$institution->maxuseraccounts}</td>
<td class="center"><a href="{$WWWROOT}admin/users/institutionstaff.php?institution={$institution->name}">{$institution->staff}</a></td>
<td class="center"><a href="{$WWWROOT}admin/users/institutionadmins.php?institution={$institution->name}">{$institution->admins}</a></td>
<td class="right">
<form action="" method="post">
<input type="hidden" name="i" value="{$institution->name}">
<input type="submit" class="btn-edit icon s" name="edit" value="{str tag="edit"}">
{if $siteadmin && !$institution->members && $institution->name != 'mahara'}<input type="submit" class="btn-del icon s" name="delete" value="{str tag="delete"}">{/if}
</form>
</td>
<td class="center">{if $institution->suspended}<span class="suspended">{str tag="suspendedinstitution" section=admin}</span>{/if}</td>
</tr>
{/foreach}
{$results.tablerows|safe}
</tbody>
</table>
<div class="center">
{$results.pagination|safe}
</div>
{/if}
{include file="footer.tpl"}
{foreach from=$institutions item=institution}
<tr class="{cycle values='r0,r1'}">
<td>{$institution->displayname}</td>
<td class="center">
{if $institution->name != 'mahara'}
<a href="{$WWWROOT}admin/users/institutionusers.php?usertype=members&amp;institution={$institution->name}">{$institution->members}</a>
{else}
<a href="{$WWWROOT}admin/users/search.php?institution=mahara">{$institution->members}</a>
{/if}
</td>
<td class="center">{$institution->maxuseraccounts}</td>
<td class="center"><a href="{$WWWROOT}admin/users/institutionstaff.php?institution={$institution->name}">{$institution->staff}</a></td>
<td class="center"><a href="{$WWWROOT}admin/users/institutionadmins.php?institution={$institution->name}">{$institution->admins}</a></td>
<td class="right">
<form action="" method="post">
<input type="hidden" name="i" value="{$institution->name}">
{if $siteadmin && !$institution->members && $institution->name != 'mahara'}<input type="submit" class="btn-del icon s" name="delete" value="{str tag="delete"}">{/if}
<input type="submit" class="btn-edit icon s" name="edit" value="{str tag="edit"}">
</form>
</td>
<td class="center">{if $institution->suspended}<span class="suspended">{str tag="suspendedinstitution" section=admin}</span>{/if}</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