Commit 25022deb authored by Hugh Davenport's avatar Hugh Davenport
Browse files

Add institution comparison stats page



Change-Id: I14eb8147f454027876bfdbc251b25b3f12b52769
Signed-off-by: default avatarHugh Davenport <hugh@catalyst.net.nz>
parent 4e1cc460
......@@ -37,7 +37,7 @@ $offset = param_integer('offset', 0);
$extradata = json_decode(param_variable('extradata'));
$type = param_alpha('type', 'users');
$subpages = array('users', 'groups', 'views', 'registration', 'historical');
$subpages = array('users', 'groups', 'views', 'registration', 'historical', 'institution');
if (!in_array($type, $subpages)) {
$type = 'users';
}
......@@ -46,7 +46,15 @@ if ($type == 'historical') {
$field = (isset($extradata->field) ? $extradata->field : 'count_usr');
}
if ($type == 'institution') {
$sort = (isset($extradata->sort) ? $extradata->sort : 'displayname');
$sortdesc = (isset($extradata->sortdesc) ? $extradata->sortdesc : false);
}
switch ($type) {
case 'institution':
$data = institution_comparison_stats_table($limit, $offset, $sort, $sortdesc);
break;
case 'historical':
$data = historical_stats_table($limit, $offset, $field);
break;
......
......@@ -35,7 +35,7 @@ require(get_config('libroot') . 'registration.php');
define('TITLE', get_string('sitestatistics', 'admin'));
$type = param_alpha('type', 'users');
$subpages = array('users', 'groups', 'views', 'registration', 'historical');
$subpages = array('users', 'groups', 'views', 'registration', 'historical', 'institution');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
......@@ -47,9 +47,17 @@ if ($type == 'historical') {
$field = param_alphanumext('field', 'count_usr');
}
if ($type == 'institution') {
$sort = param_alphanumext('sort', 'displayname');
$sortdesc = param_boolean('sortdesc');
}
$sitedata = site_statistics(true);
switch ($type) {
case 'institution':
$data = institution_comparison_statistics($limit, $offset, $sort, $sortdesc);
break;
case 'historical':
$data = historical_statistics($limit, $offset, $field);
break;
......
......@@ -278,6 +278,7 @@ $string['institutioncreated'] = 'Institution created';
// Registration statistics
$string['registrationstatstabletitle'] = 'Registration statistics for the current week';
$string['historicalstatstabletitle'] = 'Historical statistics for field \'%s\'';
$string['institutionstatstabletitle'] = 'Comparison of institution statistics';
// Site options
$string['adminsonly'] = 'Administrators only';
......
......@@ -1039,3 +1039,7 @@ $string['cleanurlallowedcharacters'] = 'Only lowercase letters from a-z, numbers
$string['registration'] = 'Registration';
$string['modified'] = 'Modified';
$string['historical'] = 'Historical';
$string['members'] = 'Members';
$string['blocks'] = 'Blocks';
$string['artefacts'] = 'Artefacts';
$string['posts'] = 'Posts';
......@@ -1980,6 +1980,184 @@ function institution_historical_stats_table($limit, $offset, $field, &$instituti
return $result;
}
function institution_comparison_statistics($limit, $offset, $sort, $sortdesc) {
$data = array();
$data['tableheadings'] = array(
array(
'name' => get_string('institution'),
'class' => 'search-results-sort-column' . ($sort == 'displayname' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=displayname&sortdesc=' . ($sort == 'displayname' ? !$sortdesc : false) . '&limit=' . $limit . '&offset=' . $offset
),
array(
'name' => get_string('members'),
'class' => 'search-results-sort-column' . ($sort == 'count_members' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=count_members&sortdesc=' . ($sort == 'count_members' ? !$sortdesc : true) . '&limit=' . $limit . '&offset=' . $offset
),
array(
'name' => get_string('views'),
'class' => 'search-results-sort-column' . ($sort == 'count_views' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=count_views&sortdesc=' . ($sort == 'count_views' ? !$sortdesc : true) . '&limit=' . $limit . '&offset=' . $offset
),
array(
'name' => get_string('blocks'),
'class' => 'search-results-sort-column' . ($sort == 'count_blocks' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=count_blocks&sortdesc=' . ($sort == 'count_blocks' ? !$sortdesc : true) . '&limit=' . $limit . '&offset=' . $offset
),
array(
'name' => get_string('artefacts'),
'class' => 'search-results-sort-column' . ($sort == 'count_artefacts' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=count_artefacts&sortdesc=' . ($sort == 'count_artefacts' ? !$sortdesc : true) . '&limit=' . $limit . '&offset=' . $offset
),
array(
'name' => get_string('posts'),
'class' => 'search-results-sort-column' . ($sort == 'count_interaction_forum_post' ? ' ' . ($sortdesc ? 'desc' : 'asc') : ''),
'link' => get_config('wwwroot') . 'admin/statistics.php?type=institution&sort=count_interaction_forum_post&sortdesc=' . ($sort == 'count_interaction_forum_post' ? !$sortdesc : true) . '&limit=' . $limit . '&offset=' . $offset
),
);
$data['table'] = institution_comparison_stats_table($limit, $offset, $sort, $sortdesc);
$data['tabletitle'] = get_string('institutionstatstabletitle', 'admin');
$data['summary'] = $data['table']['count'] == 0 ? get_string('nostats', 'admin') : 'FIXME: should something go here?';
return $data;
}
function institution_comparison_stats_table($limit, $offset, $sort, $sortdesc) {
global $USER;
$count = count_records_sql(
"SELECT COUNT(DISTINCT institution)
FROM {institution_registration}"
);
$pagination = build_pagination(array(
'id' => 'stats_pagination',
'url' => get_config('wwwroot') . 'admin/statistics.php?type=institution',
'jsonscript' => 'admin/statistics.json.php',
'datatable' => 'statistics_table',
'count' => $count,
'limit' => $limit,
'offset' => $offset,
'extradata' => array('sort' => $sort, 'sortdesc' => $sortdesc),
'setlimit' => true,
));
$result = array(
'count' => $count,
'tablerows' => '',
'pagination' => $pagination['html'],
'pagination_js' => $pagination['javascript'],
);
if ($count < 1) {
return $result;
}
if ($sort == 'displayname') {
if (is_postgres()) {
$institutions = get_records_sql_array(
"SELECT tmp.id, tmp.institution AS name, i.displayname
FROM (SELECT DISTINCT ON (institution)
id, institution
FROM {institution_registration}
ORDER BY institution, time DESC
) tmp
JOIN {institution} i ON (tmp.institution = i.name)
ORDER BY i.displayname " . ($sortdesc ? 'DESC' : 'ASC') . "
LIMIT ? OFFSET ?",
array($limit, $offset)
);
}
else {
$institutions = get_records_sql_array(
"SELECT tmp.id, tmp.institution AS name, i.displayname
FROM (SELECT ir.id, ir.institution
FROM {institution_registration} ir
JOIN (SELECT institution, MAX(time) AS time
FROM {institution_registration}
GROUP BY institution
) inn ON (inn.institution = ir.institution AND inn.time = ir.time)
) tmp
JOIN {institution} i ON (tmp.institution = i.name)
ORDER BY i.displayname " . ($sortdesc ? 'DESC' : 'ASC') . "
LIMIT ? OFFSET ?",
array($limit, $offset)
);
}
}
else {
if (is_postgres()) {
$institutions = get_records_sql_array(
"SELECT tmp.id, tmp.institution AS name, i.displayname
FROM (SELECT DISTINCT ON (ir.institution)
ir.id, ir.institution, ird.value
FROM {institution_registration} ir
JOIN {institution_registration_data} ird ON (ir.id = ird.registration_id)
WHERE ird.field = ?
ORDER BY ir.institution, ir.time DESC
) tmp
JOIN {institution} i ON (tmp.institution = i.name)
ORDER BY tmp.value::int " . ($sortdesc ? 'DESC' : 'ASC') . "
LIMIT ? OFFSET ?",
array($sort, $limit, $offset)
);
}
else {
$institutions = get_records_sql_array(
"SELECT tmp.id, tmp.institution AS name, i.displayname
FROM (SELECT ir.id, ir.institution, ird.value
FROM {institution_registration} ir
JOIN (SELECT institution, MAX(time) AS time
FROM {institution_registration}
GROUP BY institution
) inn ON (inn.institution = ir.institution AND inn.time = ir.time)
JOIN {institution_registration_data} ird ON (ir.id = ird.registration_id)
WHERE ird.field = ?
) tmp
JOIN {institution} i ON (tmp.institution = i.name)
ORDER BY (tmp.value + 0) " . ($sortdesc ? 'DESC' : 'ASC') . "
LIMIT ? OFFSET ?",
array($sort, $limit, $offset)
);
}
}
$registrationdata = array();
foreach ($institutions as $i) {
$d = new StdClass;
$d->name = $i->name;
$d->displayname = $i->displayname;
$current = get_records_select_array('institution_registration_data',
'registration_id = ? AND field IN (?, ?, ?, ? ,?)',
array($i->id, 'count_members', 'count_views', 'count_blocks', 'count_artefacts', 'count_interaction_forum_post'),
'', 'field, value'
);
foreach ($current as $data) {
$d->{$data->field} = $data->value;
}
$registrationdata[] = $d;
}
$csvfields = array('name', 'count_members', 'count_views', 'count_blocks', 'count_artefacts', 'count_interaction_forum_post');
$csv = join(',', $csvfields) . "\n";
foreach ($registrationdata as &$d) {
$data = array();
foreach ($csvfields as $f) {
$data[] = str_replace('"', '""', $d->$f);
}
$csv .= '"' . join('","', $data) . '"' . "\n";
}
$USER->set_download_file($csv, 'institutionstatistics.csv', 'text/csv');
$result['csv'] = true;
$smarty = smarty_core();
$smarty->assign('data', $registrationdata);
$smarty->assign('offset', $offset);
$result['tablerows'] = $smarty->fetch('admin/comparisonstats.tpl');
return $result;
}
function graph_site_data_weekly() {
......
{foreach from=$data item=item key=key}
<tr class="{cycle values='r0,r1'}">
<td><a href="users/statistics.php?institution={$item->name}&type=registration">{$item->displayname}</a></td>
<td><a href="users/statistics.php?institution={$item->name}&type=historical&field=count_members">{$item->count_members}</td>
<td><a href="users/statistics.php?institution={$item->name}&type=historical&field=count_views">{$item->count_views}</td>
<td><a href="users/statistics.php?institution={$item->name}&type=historical&field=count_blocks">{$item->count_blocks}</td>
<td><a href="users/statistics.php?institution={$item->name}&type=historical&field=count_artefacts">{$item->count_artefacts}</td>
<td><a href="users/statistics.php?institution={$item->name}&type=historical&field=count_interaction_forum_post">{$item->count_interaction_forum_post}</td>
</tr>
{/foreach}
......@@ -24,11 +24,11 @@
{/if}
<div id="statistics_table_container" class="statistics-subpage-right-column {if $subpagedata.table.count == 0} hidden{/if}">
<h3>{$subpagedata.tabletitle}</h3>
<table id="statistics_table" class="fullwidth">
<table id="statistics_table" class="tablerenderer fullwidth">
<thead>
<tr>
{foreach from=$subpagedata.tableheadings item=heading}
<th{if $heading.class} class="{$heading.class}"{/if}>{$heading.name}</th>
<th{if $heading.class} class="{$heading.class}"{/if}>{if $heading.link}<a href="{$heading.link}">{/if}{$heading.name}{if $heading.link}</a>{/if}</th>
{/foreach}
</tr>
</thead>
......
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