Commit 57b46549 authored by Robert Lyon's avatar Robert Lyon
Browse files

Bug 1691910: Consolidating the admin and institution statistics pages



Making the admin/statistics.php obsolete and getting admin/users/statistics.php
to do the work.

The institution 'all' shows the site statistics

Sponsored by University of Sussex and Dublin City University

behatnotneeded

Change-Id: Ic2fb9d4ef355fdc5e5527c1ce776463d821c0c08
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 75f4fd25
......@@ -17,7 +17,7 @@ define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'index');
require(dirname(dirname(__FILE__)).'/init.php');
require(get_config('libroot') . 'registration.php');
require(get_config('libroot') . 'statistics.php');
if (get_config('installed')) {
define('TITLE', get_string('administration', 'admin'));
validate_theme(get_config('theme'));
......@@ -26,7 +26,6 @@ else {
define('TITLE', get_string('installation', 'admin'));
}
require(get_config('libroot') . 'upgrade.php');
require_once(get_config('libroot') . 'registration.php');
$upgrades = check_upgrades();
......
<?php
/**
*
* @package mahara
* @subpackage core
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('STAFF', 1);
define('JSON', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
require(get_config('libroot') . 'registration.php');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$extradata = json_decode(param_variable('extradata'));
$type = param_alpha('type', 'users');
$subpages = array('users', 'groups', 'views', 'content', 'historical', 'institutions', 'logins');
if (!in_array($type, $subpages)) {
$type = 'users';
}
if ($type == 'historical') {
$field = (isset($extradata->field) ? $extradata->field : 'count_usr');
}
if ($type == 'institutions' || $type == 'logins') {
$sort = (isset($extradata->sort) ? $extradata->sort : 'displayname');
$sortdesc = (isset($extradata->sortdesc) ? $extradata->sortdesc : false);
$start = param_alphanumext('start', null);
$end = param_alphanumext('end', null);
}
switch ($type) {
case 'logins':
$data = institution_logins_statistics($limit, $offset, $sort, $sortdesc, $start, $end);
$data = $data['table'];
break;
case 'institutions':
$data = institution_comparison_stats_table($limit, $offset, $sort, $sortdesc);
break;
case 'historical':
$data = historical_stats_table($limit, $offset, $field);
break;
case 'content':
$data = content_stats_table($limit, $offset);
break;
case 'groups':
$data = group_stats_table($limit, $offset);
break;
case 'views':
$data = view_stats_table($limit, $offset);
break;
case 'users':
default:
$data = user_stats_table($limit, $offset);
}
json_reply(false, (object) array('message' => false, 'data' => $data));
<?php
/**
*
* @package mahara
* @subpackage admin
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
define('STAFF', 1);
define('MENUITEM', 'adminhome/statistics');
require(dirname(dirname(__FILE__)).'/init.php');
require(get_config('libroot') . 'registration.php');
define('TITLE', get_string('sitestatistics', 'admin'));
$type = param_alpha('type', 'users');
$subpages = array('users', 'groups', 'views', 'content', 'historical', 'institutions', 'logins');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
if (!in_array($type, $subpages)) {
$type = 'users';
}
if ($type == 'historical') {
$field = param_alphanumext('field', 'count_usr');
}
if ($type == 'institutions' || $type == 'logins') {
$sort = param_alphanumext('sort', 'displayname');
$sortdesc = param_boolean('sortdesc');
$start = param_alphanumext('start', null);
$end = param_alphanumext('end', null);
}
$sitedata = site_statistics(true);
switch ($type) {
case 'logins':
$data = institution_logins_statistics($limit, $offset, $sort, $sortdesc, $start, $end);
break;
case 'institutions':
$data = institution_comparison_statistics($limit, $offset, $sort, $sortdesc);
break;
case 'historical':
$data = historical_statistics($limit, $offset, $field);
break;
case 'content':
$data = content_statistics($limit, $offset);
break;
case 'groups':
$data = group_statistics($limit, $offset);
break;
case 'views':
$data = view_statistics($limit, $offset);
break;
case 'users':
default:
$data = user_statistics($limit, $offset, $sitedata);
}
$js = <<< EOF
jQuery(function() {
{$data['table']['pagination_js']}
});
EOF;
$smarty = smarty(array('paginator','js/chartjs/Chart.min.js'));
setpageicon($smarty, 'icon-area-chart');
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('sitedata', $sitedata);
$smarty->assign('type', $type);
$smarty->assign('subpages', $subpages);
$smarty->assign('subpagedata', $data);
$smarty->display('admin/statistics.tpl');
......@@ -14,38 +14,33 @@ define('INSTITUTIONALSTAFF', 1);
define('JSON', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require(get_config('libroot') . 'registration.php');
require(get_config('libroot') . 'statistics.php');
$limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$extradata = json_decode(param_variable('extradata'));
$institution = (isset($extradata->institution) ? $extradata->institution : 'mahara');
$type = param_alpha('type', 'users');
$subpages = array('users', 'views', 'content', 'historical');
if (!in_array($type, $subpages)) {
$type = 'users';
}
if ($type == 'historical') {
$field = (isset($extradata->field) ? $extradata->field : 'count_members');
$institution = param_alphanum('institution', null);
if (empty($institution)) {
if (isset($extradata->institution)) {
$institution = $extradata->institution;
}
else if ($USER->get('admin') || $USER->get('staff')) {
$institution = 'all';
}
else {
$institution = 'mahara';
}
}
$institutiondata = institution_statistics($institution, true);
switch ($type) {
case 'historical':
$data = institution_historical_stats_table($limit, $offset, $field, $institutiondata);
break;
case 'content':
$data = institution_content_stats_table($limit, $offset, $institutiondata);
break;
case 'views':
$data = institution_view_stats_table($limit, $offset, $institutiondata);
break;
case 'users':
default:
$data = institution_user_stats_table($limit, $offset, $institutiondata);
}
json_reply(false, (object) array('message' => false, 'data' => $data));
$type = param_alpha('type', 'users');
$extraparams = new stdClass();
$extraparams->type = $type;
$extraparams->offset = param_integer('offset', 0);
$extraparams->limit = param_integer('limit', 10);
$extraparams->sort = isset($extradata->sort) ? $extradata->sort : 'displayname';
$extraparams->sortdesc = isset($extradata->sortdesc) ? true : false;
$extraparams->start = param_alphanumext('start', null);
$extraparams->end = param_alphanumext('end', null);
$extraparams->field = isset($extradata->field) ? $extradata->field : (($institution == 'all') ? 'count_usr' : 'count_members');
list($subpages, $institutiondata, $subpagedata) = display_statistics($institution, $type, $extraparams);
json_reply(false, (object) array('message' => false, 'data' => $subpagedata['table']));
......@@ -14,7 +14,7 @@ define('INSTITUTIONALSTAFF', 1);
define('MENUITEM', 'manageinstitutions/statistics');
require(dirname(dirname(dirname(__FILE__))).'/init.php');
require(get_config('libroot') . 'registration.php');
require(get_config('libroot') . 'statistics.php');
require_once('institution.php');
if (!is_logged_in()) {
......@@ -31,7 +31,7 @@ if (!empty($institution)) {
}
if (!$notallowed) {
$institutionelement = get_institution_selector(true, false, true, get_config('staffstats'));
$institutionelement = get_institution_selector(true, false, true, get_config('staffstats'), ($USER->get('admin') || $USER->get('staff')));
}
if (empty($institutionelement) || $notallowed) {
......@@ -47,6 +47,7 @@ if (!$institution || !$USER->can_edit_institution($institution, true)) {
else if (!empty($institution)) {
$institutionelement['defaultvalue'] = $institution;
}
$institutionselector = pieform(array(
'name' => 'usertypeselect',
'class' => 'form-inline',
......@@ -55,42 +56,32 @@ $institutionselector = pieform(array(
)
));
define('TITLE', get_string('institutionstatisticsfor', 'admin', get_field('institution', 'displayname', 'name', $institution)));
$type = param_alpha('type', 'users');
$subpages = array('users', 'views', 'content', 'historical');
$offset = param_integer('offset', 0);
$limit = param_integer('limit', 10);
if (!in_array($type, $subpages)) {
$type = 'users';
$showall = ($institution == 'all') ? true : false;
if ($showall) {
define('TITLE', get_string('statisticsforallinstitutions', 'admin'));
$icon = 'icon-area-chart';
}
if ($type == 'historical') {
$field = param_alphanumext('field', 'count_members');
else {
define('TITLE', get_string('institutionstatisticsfor', 'admin', get_field('institution', 'displayname', 'name', $institution)));
$icon = 'icon-university';
}
$institutiondata = institution_statistics($institution, true);
switch ($type) {
case 'historical':
$data = institution_historical_statistics($limit, $offset, $field, $institutiondata);
break;
case 'content':
$data = institution_content_statistics($limit, $offset, $institutiondata);
break;
case 'views':
$data = institution_view_statistics($limit, $offset, $institutiondata);
break;
case 'users':
default:
$data = institution_user_statistics($limit, $offset, $institutiondata);
}
$type = param_alpha('type', 'users');
$extraparams = new stdClass();
$extraparams->type = $type;
$extraparams->offset = param_integer('offset', 0);
$extraparams->limit = param_integer('limit', 10);
$extraparams->sort = param_alphanumext('sort', 'displayname');
$extraparams->sortdesc = param_boolean('sortdesc');
$extraparams->start = param_alphanumext('start', null);
$extraparams->end = param_alphanumext('end', null);
list($subpages, $institutiondata, $subpagedata) = display_statistics($institution, $type, $extraparams);
$wwwroot = get_config('wwwroot');
$js = <<< EOF
jQuery(function ($) {
{$data['table']['pagination_js']}
{$subpagedata['table']['pagination_js']}
function reloadStats() {
window.location.href = '{$wwwroot}admin/users/statistics.php?institution='+$('#usertypeselect_institution').val() +'&type={$type}';
......@@ -101,15 +92,14 @@ jQuery(function ($) {
EOF;
$smarty = smarty(array('paginator','js/chartjs/Chart.min.js'));
setpageicon($smarty, 'icon-university');
setpageicon($smarty, $icon);
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('institutiondata', $institutiondata);
$smarty->assign('type', $type);
$smarty->assign('subpages', $subpages);
$smarty->assign('subpagedata', $data);
$smarty->assign('showall', ($showall ? '_all' : ''));
$smarty->assign('subpagedata', $subpagedata);
$smarty->assign('institutionselector', $institutionselector);
$smarty->display('admin/users/statistics.tpl');
......@@ -20,8 +20,9 @@ if (!defined('CRON')) {
json_headers();
$validtypes = array('Line', 'Bar', 'Radar', 'PolarArea', 'Pie', 'Doughnut');
$type = ucfirst(param_alphanum('type', false));
if (!in_array($type, $validtypes)) {
$search_array = array_combine(array_map('strtolower', $validtypes), $validtypes);
$type = strtolower(param_alphanum('type', false));
if (empty($search_array[$type])) {
json_reply('missingparameter', '\'' . $type . '\' is not a valid graph type');
}
$graph = param_alphanumext('graph', null);
......@@ -31,13 +32,13 @@ if (!defined('CRON')) {
$extradata = json_decode($extradata);
require_once(get_config('libroot') . 'graph.php');
require_once(get_config('libroot') . 'registration.php');
require_once(get_config('libroot') . 'statistics.php');
if (!function_exists($graph) || !in_array($graph, allowed_graph_functions())) {
json_reply('invalidparameter', 'Cannot call graph function \'' . $graph . '\'');
}
else {
$data = ($extradata) ? $graph($type, $extradata) : $graph($type);
$data = ($extradata) ? $graph($search_array[$type], $extradata) : $graph($search_array[$type]);
if (empty($data)) {
$data['empty'] = true;
json_reply(false, array('data' => $data));
......
......@@ -266,8 +266,7 @@ $string['reopensitedetail'] = 'Your site is closed. Site administrators may stay
// Statistics
$string['siteinformation'] = 'Site information';
$string['viewfullsitestatistics'] = 'View full site statistics';
$string['sitestatistics'] = 'Site statistics';
$string['siteinstalled'] = 'Site installed';
$string['institutioncreated_all'] = 'Site installed';
$string['databasesize'] = 'Database size';
$string['diskusage'] = 'Disk usage';
$string['maharaversion'] = 'Mahara version';
......@@ -1307,3 +1306,5 @@ $string['cli_lang_branch'] = 'Mahara series version to fetch langpacks for serie
$string['withselectedcontentexport'] = 'Re-queue items into the export queue';
$string['withselectedcontentdelete'] = 'Delete selected items from the export queue';
$string['statisticsforallinstitutions'] = 'Statistics for all institutions';
\ No newline at end of file
......@@ -3,5 +3,5 @@
<h3>Login statistics</h3>
<p>By default you see the logins for the previous calendar month. If you view this page in April, you will see the results for March.</p>
<p>To alter the results time period you can add a <strong>'start'</strong> and an <strong>'end'</strong> option to the URL, e.g.</p>
<p>'admin/statistics.php?type=logins&start=2015-01-01&end=2015-04-30'</p>
<p>'admin/users/statistics.php?type=logins&start=2015-01-01&end=2015-04-30'</p>
<p>This will return the results for the 4-month period of 1 January to 30 April 2015.</p>
......@@ -5000,5 +5000,10 @@ function xmldb_core_upgrade($oldversion=0) {
}
}
if ($oldversion < 2017052900) {
log_debug('Clear menu cache for removal of menu items');
clear_menu_cache();
}
return $status;
}
......@@ -840,7 +840,18 @@ class Institution {
}
}
function get_institution_selector($includedefault = true, $assumesiteadmin=false, $includesitestaff=false, $includeinstitutionstaff=false) {
/**
* Returns an institution dropdown selector
*
* @param bool $includedefault To include the 'mahara' institution in list
* @param bool $assumesiteadmin To call this function like you had site admin privileges
* @param bool $includesitestaff To allow site staff to see dropdown like the site admin would
* @param bool $includeinstitutionstaff To allow institution staff to see dropdown like institution admin would
* @param bool $allselector To add an 'all' option to the dropdown where it makes sense, eg in institution statistics page
*
* @return null or array suitable for pieform element
*/
function get_institution_selector($includedefault = true, $assumesiteadmin=false, $includesitestaff=false, $includeinstitutionstaff=false, $allselector=false) {
global $USER;
if (($assumesiteadmin || $USER->get('admin')) || ($includesitestaff && $USER->get('staff'))) {
......@@ -883,6 +894,9 @@ function get_institution_selector($includedefault = true, $assumesiteadmin=false
}
$options = array();
if ($allselector) {
$options['all'] = get_string('Allinstitutions', 'mahara');
}
foreach ($institutions as $i) {
$options[$i->name] = $i->displayname;
}
......
......@@ -4085,7 +4085,7 @@ function cron_institution_registration_data() {
* Cronjob to save weekly site data locally
*/
function cron_site_data_weekly() {
require_once(get_config('libroot') . 'registration.php');
require_once(get_config('libroot') . 'statistics.php');
$current = site_data_current();
$time = db_format_timestamp(time());
......@@ -4107,7 +4107,7 @@ function cron_site_data_weekly() {
}
function cron_site_data_daily() {
require_once(get_config('libroot') . 'registration.php');
require_once(get_config('libroot') . 'statistics.php');
$current = site_data_current();
$time = db_format_timestamp(time());
......@@ -4191,7 +4191,7 @@ function cron_site_data_daily() {
}
function cron_institution_data_weekly() {
require_once(get_config('libroot') . 'registration.php');
require_once(get_config('libroot') . 'statistics.php');
foreach (get_column('institution', 'name') as $institution) {
$current = institution_data_current($institution);
$time = db_format_timestamp(time());
......@@ -4214,7 +4214,7 @@ function cron_institution_data_weekly() {
}
function cron_institution_data_daily() {
require_once(get_config('libroot') . 'registration.php');
require_once(get_config('libroot') . 'statistics.php');
foreach (get_column('institution', 'name') as $institution) {
$current = institution_data_current($institution);
$time = db_format_timestamp(time());
......
......@@ -367,1926 +367,3 @@ function institution_registration_data() {
}
return $data_to_store;
}
function site_data_current() {
return array(
'users' => count_records_select('usr', 'id > 0 AND deleted = 0'),
'groups' => count_records('group', 'deleted', 0),
'views' => count_records_select('view', 'owner != 0 OR owner IS NULL'),
);
}
function site_statistics($full=false) {
$data = array();
if ($full) {
$data = site_data_current();
$data['weekly'] = true;
if (is_postgres()) {
$weekago = "CURRENT_DATE - INTERVAL '1 week'";
$thisweeksql = "(lastaccess > $weekago)::int";
$todaysql = '(lastaccess > CURRENT_DATE)::int';
$eversql = "(NOT lastaccess IS NULL)::int";
}
else {
$weekago = 'CURRENT_DATE - INTERVAL 1 WEEK';
$thisweeksql = "lastaccess > $weekago";
$todaysql = 'lastaccess > CURRENT_DATE';
$eversql = "NOT lastaccess IS NULL";
}
$sql = "SELECT SUM($todaysql) AS today, SUM($thisweeksql) AS thisweek, $weekago AS weekago, SUM($eversql) AS ever FROM {usr}";
$active = get_record_sql($sql);
$data['usersloggedin'] = get_string('loggedinsince', 'admin', $active->today, $active->thisweek, format_date(strtotime($active->weekago), 'strftimedateshort'), $active->ever);
$memberships = count_records_sql("
SELECT COUNT(*)
FROM {group_member} m JOIN {group} g ON g.id = m.group
WHERE g.deleted = 0
");
$data['groupmemberaverage'] = round($memberships/$data['users'], 1);
$data['strgroupmemberaverage'] = get_string('groupmemberaverage', 'admin', $data['groupmemberaverage']);
$data['viewsperuser'] = get_field_sql("
SELECT (0.0 + COUNT(id)) / NULLIF(COUNT(DISTINCT \"owner\"), 0)
FROM {view}
WHERE NOT \"owner\" IS NULL AND \"owner\" > 0
");
$data['viewsperuser'] = round($data['viewsperuser'], 1);
$data['strviewsperuser'] = get_string('viewsperuser', 'admin', $data['viewsperuser']);
}
$data['name'] = get_config('sitename');
$data['release'] = get_config('release');
$data['version'] = get_config('version');
$data['installdate'] = format_date(strtotime(get_config('installation_time')), 'strftimedate');
$data['dbsize'] = db_total_size();
$data['diskusage'] = get_field('site_data', 'value', 'type', 'disk-usage');
$data['cronrunning'] = !record_exists_select('cron', 'nextrun IS NULL OR nextrun < CURRENT_DATE');
$data['siteclosedbyadmin'] = get_config('siteclosedbyadmin');
if ($latestversion = get_config('latest_version')) {
$data['latest_version'] = $latestversion;
if ($data['release'] == $latestversion) {
$data['strlatestversion'] = get_string('uptodate', 'admin');
}
else {
$download_page = 'https://launchpad.net/mahara/+download';
$data['strlatestversion'] = get_string('latestversionis', 'admin', $download_page, $latestversion);
}
}
return($data);
}
function institution_data_current($institution) {
$data = array();
if ($institution == 'mahara') {
$membersquery = 'SELECT id FROM {usr}
WHERE deleted = 0 AND id > 0 AND id NOT IN
(SELECT usr FROM {usr_institution})';
$membersqueryparams = array();
}
else {
$membersquery = 'SELECT usr FROM {usr_institution} ui
JOIN {usr} u ON (u.id = ui.usr)
WHERE u.deleted = 0 AND ui.institution = ?';
$membersqueryparams = array($institution);
}
$data['memberssql'] = $membersquery;
$data['memberssqlparams'] = $membersqueryparams;
$data['users'] = get_field_sql('SELECT COUNT(*) FROM (' . $membersquery . ') AS members', $membersqueryparams);
if (!$data['users']) {
$data['views'] = 0;
}
else {
$data['viewssql'] = '
SELECT id FROM {view}
WHERE owner IS NOT NULL AND owner IN (' . $membersquery . ')
UNION
SELECT id FROM {view}
WHERE institution IS NOT NULL AND institution = ?';
$data['viewssqlparam'] = array_merge($membersqueryparams, array($institution));
$data['views'] = count_records_sql('SELECT COUNT(*) FROM (' . $data['viewssql'] . ') AS members', $data['viewssqlparam']);
}
return $data;
}
function institution_statistics($institution, $full=false) {
$data = array();
if ($full) {
$data = institution_data_current($institution);
$data['weekly'] = true;
$data['institution'] = $institution;
if (is_postgres()) {
$weekago = "CURRENT_DATE - INTERVAL '1 week'";
$thisweeksql = "COALESCE((lastaccess > $weekago)::int, 0)::int";
$todaysql = 'COALESCE((lastaccess > CURRENT_DATE)::int, 0)::int';
$eversql = "(NOT lastaccess IS NULL)::int";
}
else {