function _getDownloadStats($pubObjectId)
{
$cache = CacheManager::getManager()->getCache('downloadStats', $pubObjectId, array($this, '_downloadStatsCacheMiss'));
if (time() - $cache->getCacheTime() > 60 * 60 * 24) {
// Cache is older than one day, erase it.
$cache->flush();
}
$statsReports = $cache->get($pubObjectId);
$currentYear = date("Y");
$months = range(1, 12);
$statsByFormat = $statsByMonth = $years = array();
$totalDownloads = 0;
foreach ($statsReports as $statsReport) {
$month = (int) substr($statsReport[STATISTICS_DIMENSION_MONTH], -2);
$year = (int) substr($statsReport[STATISTICS_DIMENSION_MONTH], 0, 4);
$metric = $statsReport[STATISTICS_METRIC];
// Keep track of the years, avoiding duplicates.
$years[$year] = null;
$representationId = $statsReport[STATISTICS_DIMENSION_REPRESENTATION_ID];
// Prepare the stats aggregating by Representation.
// Create entries for all months, so all representations will have the same entries count.
if (!array_key_exists($representationId, $statsByFormat)) {
$representationDao = Application::getRepresentationDAO();
$representation = $representationDao->getById($representationId);
if (empty($representation)) {
continue;
}
$statsByFormat[$representationId] = array('data' => array(), 'label' => $representation->getLocalizedName(), 'color' => $this->_getColor($representationId), 'total' => 0);
}
// Make sure we have entries for all years with stats.
if (!array_key_exists($year, $statsByFormat[$representationId]['data'])) {
$statsByFormat[$representationId]['data'][$year] = array_fill_keys($months, 0);
}
$statsByFormat[$representationId]['data'][$year][$month] = $metric;
$statsByFormat[$representationId]['total'] += $metric;
// Prepare the stats aggregating only by Month.
if (!array_key_exists($year, $statsByMonth)) {
$statsByMonth[$year] = array_fill_keys($months, 0);
}
$statsByMonth[$year][$month] += $metric;
$totalDownloads += $metric;
}
if ($statsByMonth) {
$datasetId = 'allDownloads';
// GraphJS works with datasets.
$statsByMonth = array($datasetId => array('data' => $statsByMonth, 'label' => __('common.allDownloads'), 'color' => $this->_getColor(REALLY_BIG_NUMBER), 'total' => $totalDownloads));
}
return array($statsByFormat, $statsByMonth, array_keys($years));
}