Files
Piwigo/admin/stats.php
Zacharie b32daddafe Issue #1189 : Statistic page redesign
* Statistic page totally redesign, replace by a single graphic which display for last hours, days, months and years
 * Add Chart.js plugin for graphic representation
 * Add Moment.js plugin for time operations
2020-06-12 15:33:35 +02:00

206 lines
5.1 KiB
PHP

<?php
// +-----------------------------------------------------------------------+
// | This file is part of Piwigo. |
// | |
// | For copyright and license information, please view the COPYING.txt |
// | file that was distributed with this source code. |
// +-----------------------------------------------------------------------+
if (!defined('PHPWG_ROOT_PATH'))
{
die ('Hacking attempt!');
}
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
include_once(PHPWG_ROOT_PATH.'admin/include/functions_history.inc.php');
// +-----------------------------------------------------------------------+
// | Functions |
// +-----------------------------------------------------------------------+
function get_last($last_number=60, $type='year')
{
$query = '
SELECT
year,
month,
day,
hour,
nb_pages
FROM '.HISTORY_SUMMARY_TABLE;
if ($type === 'hour')
{
$query.= '
WHERE year IS NOT NULL
AND month IS NOT NULL
AND day IS NOT NULL
AND hour IS NOT NULL
ORDER BY
year DESC,
month DESC,
day DESC,
hour DESC
LIMIT '.$last_number.'
;';
}
elseif ($type === 'day')
{
$query.= '
WHERE year IS NOT NULL
AND month IS NOT NULL
AND day IS NOT NULL
AND hour IS NULL
ORDER BY
year DESC,
month DESC,
day DESC
LIMIT '.$last_number.'
;';
}
elseif ($type === 'month')
{
$query.= '
WHERE year IS NOT NULL
AND month IS NOT NULL
AND day IS NULL
ORDER BY
year DESC,
month DESC
LIMIT '.$last_number.'
;';
}
else
{
$query.= '
WHERE year IS NOT NULL
AND month IS NULL
ORDER BY
year DESC
LIMIT '.$last_number.'
;';
}
$result = pwg_query($query);
$output = array();
while ($row = pwg_db_fetch_assoc($result))
{
$output[] = $row;
}
return $output;
}
// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
// +-----------------------------------------------------------------------+
check_status(ACCESS_ADMINISTRATOR);
// +-----------------------------------------------------------------------+
// | Display statistics header |
// +-----------------------------------------------------------------------+
$template->set_filename('stats', 'stats.tpl');
// TabSheet initialization
history_tabsheet();
$base_url = get_root_url().'admin.php?page=history';
$template->assign(
array(
'U_HELP' => get_root_url().'admin/popuphelp.php?page=history',
'F_ACTION' => $base_url,
)
);
// +-----------------------------------------------------------------------+
// | Set missing unit to 0 |
// +-----------------------------------------------------------------------+
function set_missing_value($unit, $data)
{
$limit = count($data);
$result = array();
$date = get_date_object($data[count($data) - 1]);
if ($unit == 'year')
{
$date_format = 'Y';
$date_add = 'P1Y';
}
else if ($unit == 'month')
{
$date_format = 'Y-m';
$date_add = 'P1M';
}
else if ($unit == 'day')
{
$date_format = 'Y-m-d';
$date_add = 'P1D';
}
else if ($unit == 'hour')
{
$date_format = 'Y-m-d\TH:00';
$date_add = 'PT1H';
}
for ($i=0; $i < $limit; $i++)
{
$result[$date->format($date_format)] = 0;
$date->add(new DateInterval($date_add));
}
foreach ($data as $value)
{
$str = get_date_object($value)->format($date_format);
if (isset($result[$str]))
{
$result[$str] += $value['nb_pages'];
}
}
return $result;
}
function get_date_object($row) {
$date_string = $row['year'];
if ($row['month'] != null)
{
$date_string = $date_string.'-'.$row['month'] ;
if ($row['day'] != null)
{
$date_string = $date_string.'-'.$row['day'];
if ($row['hour'] != null)
{
$date_string = $date_string.' '.$row['hour'].':00';
}
}
}
else
{
$date_string .= '-1';
}
return new DateTime($date_string);
}
// +-----------------------------------------------------------------------+
// | Display statistic rows |
// +-----------------------------------------------------------------------+
$template->append('lastHours', set_missing_value('hour',get_last(72, 'hour')));
$template->append('lastDays', set_missing_value('day',get_last(90, 'day')));
$template->append('lastMonths', set_missing_value('month',get_last(24, 'month')));
$template->append('lastYears', set_missing_value('year',get_last(60, 'year')));
// +-----------------------------------------------------------------------+
// | Sending html code |
// +-----------------------------------------------------------------------+
$template->assign_var_from_handle('ADMIN_CONTENT', 'stats');
?>