Commit d25951a3 authored by Hugh Davenport's avatar Hugh Davenport
Browse files

Allow exporting statistics as CSV file



Export the site and institution statistics as CSV file.
Only exports the results that are shown in the table.

Change-Id: I7e9a5730a31d310f2b3a2bd8c5a5e556421e5d42
Signed-off-by: default avatarHugh Davenport <hugh@catalyst.net.nz>
parent a5ccb01f
......@@ -267,6 +267,7 @@ $string['viewsbytype'] = 'Pages by type';
$string['userstatstabletitle'] = 'Daily user statistics';
$string['groupstatstabletitle'] = 'Biggest groups';
$string['viewstatstabletitle'] = 'Most popular pages';
$string['exportstatsascsv'] = 'Export statistics in CSV format';
// Institutional statistics
$string['statistics'] = 'Statistics';
......
......@@ -608,6 +608,8 @@ function user_statistics($limit, $offset, &$sitedata) {
}
function user_stats_table($limit, $offset) {
global $USER;
$count = count_records('site_data', 'type', 'user-count-daily');
$pagination = build_pagination(array(
......@@ -692,6 +694,18 @@ function user_stats_table($limit, $offset) {
}
}
$csvfields = array('date', 'loggedin', 'created', 'total');
$csv = join(',', $csvfields) . "\n";
foreach ($data as $row) {
$u = array();
foreach ($csvfields as $f) {
$u[] = str_replace('"', '""', (isset($row[$f]) ? $row[$f] : 0));
}
$csv .= '"' . join('","', $u) . '"' . "\n";
}
$USER->set_download_file($csv, 'userstatistics.csv', 'text/csv');
$result['csv'] = true;
$smarty = smarty_core();
$smarty->assign('data', $data);
$result['tablerows'] = $smarty->fetch('admin/userstats.tpl');
......@@ -828,6 +842,8 @@ function institution_user_statistics($limit, $offset, &$institutiondata) {
}
function institution_user_stats_table($limit, $offset, &$institutiondata) {
global $USER;
$count = count_records('institution_data', 'type', 'user-count-daily', 'institution', $institutiondata['name']);
$pagination = build_pagination(array(
......@@ -914,6 +930,18 @@ function institution_user_stats_table($limit, $offset, &$institutiondata) {
}
}
$csvfields = array('date', 'loggedin', 'created', 'total');
$csv = join(',', $csvfields) . "\n";
foreach ($data as $row) {
$u = array();
foreach ($csvfields as $f) {
$u[] = str_replace('"', '""', (isset($row[$f]) ? $row[$f] : 0));
}
$csv .= '"' . join('","', $u) . '"' . "\n";
}
$USER->set_download_file($csv, 'userstatistics.csv', 'text/csv');
$result['csv'] = true;
$smarty = smarty_core();
$smarty->assign('data', $data);
$result['tablerows'] = $smarty->fetch('admin/userstats.tpl');
......@@ -1019,6 +1047,8 @@ function group_statistics($limit, $offset) {
}
function group_stats_table($limit, $offset) {
global $USER;
$count = count_records('group', 'deleted', 0);
$pagination = build_pagination(array(
......@@ -1081,6 +1111,18 @@ function group_stats_table($limit, $offset) {
$limit
);
$csvfields = array('id', 'name', 'members', 'views', 'forums', 'posts');
$csv = join(',', $csvfields) . "\n";
foreach ($groupdata as &$g) {
$group = array();
foreach ($csvfields as $f) {
$group[] = str_replace('"', '""', $g->$f);
}
$csv .= '"' . join('","', $group) . '"' . "\n";
}
$USER->set_download_file($csv, 'groupstatistics.csv', 'text/csv');
$result['csv'] = true;
require_once('group.php');
$smarty = smarty_core();
......@@ -1198,6 +1240,8 @@ function view_statistics($limit, $offset) {
}
function view_stats_table($limit, $offset) {
global $USER;
$count = count_records_select('view', '(owner != 0 OR owner IS NULL) AND type != ?', array('dashboard'));
$pagination = build_pagination(array(
......@@ -1258,6 +1302,18 @@ function view_stats_table($limit, $offset) {
$v->comments = isset($comments[$v->id]) ? (int) $comments[$v->id]->comments : 0;
}
$csvfields = array('title', 'displaytitle', 'fullurl', 'owner', 'group', 'institution', 'ownername', 'ownerurl', 'visits', 'type', 'comments');
$csv = join(',', $csvfields) . "\n";
foreach ($viewdata as &$v) {
$view = array();
foreach ($csvfields as $f) {
$view[] = str_replace('"', '""', $v->$f);
}
$csv .= '"' . join('","', $view) . '"' . "\n";
}
$USER->set_download_file($csv, 'viewstatistics.csv', 'text/csv');
$result['csv'] = true;
$smarty = smarty_core();
$smarty->assign('data', $viewdata);
$smarty->assign('offset', $offset);
......@@ -1369,6 +1425,8 @@ function institution_view_statistics($limit, $offset, &$institutiondata) {
}
function institution_view_stats_table($limit, $offset, &$institutiondata) {
global $USER;
if ($institutiondata['views'] != 0) {
$count = count_records_select('view', 'id IN (' . join(',', array_fill(0, $institutiondata['views'], '?')) . ') AND type != ?',
array_merge($institutiondata['viewids'], array('dashboard')));
......@@ -1436,6 +1494,18 @@ function institution_view_stats_table($limit, $offset, &$institutiondata) {
$v->comments = isset($comments[$v->id]) ? (int) $comments[$v->id]->comments : 0;
}
$csvfields = array('title', 'displaytitle', 'fullurl', 'owner', 'group', 'institution', 'ownername', 'ownerurl', 'visits', 'type', 'comments');
$csv = join(',', $csvfields) . "\n";
foreach ($viewdata as &$v) {
$view = array();
foreach ($csvfields as $f) {
$view[] = str_replace('"', '""', $v->$f);
}
$csv .= '"' . join('","', $view) . '"' . "\n";
}
$USER->set_download_file($csv, 'viewstatistics.csv', 'text/csv');
$result['csv'] = true;
$smarty = smarty_core();
$smarty->assign('data', $viewdata);
$smarty->assign('offset', $offset);
......
......@@ -16,6 +16,12 @@
<div class="statistics-subpage-left-column">
{$subpagedata.summary|safe}
</div>
{if $subpagedata.table.csv}
<div class="fr">
<span class="bulkaction-title">{str tag=exportstatsascsv section=admin}:</span>
<a href="{$WWWROOT}download.php" target="_blank">{str tag=Download section=admin}</a>
</div>
{/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">
......
......@@ -17,6 +17,12 @@
<div class="statistics-subpage-left-column">
{$subpagedata.summary|safe}
</div>
{if $subpagedata.table.csv}
<div class="fr">
<span class="bulkaction-title">{str tag=exportstatsascsv section=admin}:</span>
<a href="{$WWWROOT}download.php" target="_blank">{str tag=Download section=admin}</a>
</div>
{/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">
......
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