diff --git a/code/web/services/SideLoads/AJAX.php b/code/web/services/SideLoads/AJAX.php index 5f638f58d6..b04f9ae905 100644 --- a/code/web/services/SideLoads/AJAX.php +++ b/code/web/services/SideLoads/AJAX.php @@ -49,6 +49,6 @@ public function deleteMarc() { public function exportUsageData() { require_once ROOT_DIR . '/services/SideLoads/UsageGraphs.php'; $aspenUsageGraph = new SideLoads_UsageGraphs(); - $aspenUsageGraph->buildCSV(); + $aspenUsageGraph->buildCSV('SideLoads'); } } \ No newline at end of file diff --git a/code/web/services/SideLoads/UsageGraphs.php b/code/web/services/SideLoads/UsageGraphs.php index 4d64f41707..7d0701e5bb 100644 --- a/code/web/services/SideLoads/UsageGraphs.php +++ b/code/web/services/SideLoads/UsageGraphs.php @@ -1,35 +1,14 @@ assign('graphTitle', $title); - $this->assignGraphSpecificTitle($stat); - - $profileName= $_REQUEST['profileName']; - $sideloadId = $this->getSideloadIdBySideLoadName($profileName); - $this->getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId); - $interface->assign('profileName', $profileName); - $interface->assign('stat', $stat); - $interface->assign('propName', 'exportToCSV'); - $interface->assign('showCSVExportButton', true); - $interface->assign('section', 'SideLoads'); - - $title = $interface->getVariable('graphTitle'); - $this->display('../Admin/usage-graph.tpl', $title); +class SideLoads_UsageGraphs extends Admin_AbstractUsageGraphs { + function launch(): void { + $this->launchGraph('SideLoads'); } function getBreadcrumbs(): array { @@ -52,60 +31,13 @@ function canView(): bool { ]); } - // note that this will only handle tables with one stat (as is needed for Summon usage data) - // to see a version that handle multpile stats, see the Admin/UsageGraphs.php implementation - public function buildCSV() { - global $interface; - $stat = $_REQUEST['stat']; - if (!empty($_REQUEST['instance'])) { - $instanceName = $_REQUEST['instance']; - } else { - $instanceName = ''; - } - - $profileName= $_REQUEST['profileName']; - $sideloadId = $this->getSideloadIdBySideLoadName($profileName); - $this->getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId); - $dataSeries = $interface->getVariable('dataSeries'); - - $filename = "SideLoadsUsageData_{$stat}.csv"; - header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); - header("Cache-Control: no-store, no-cache, must-revalidate"); - header("Cache-Control: post-check=0, pre-check=0", false); - header("Pragma: no-cache"); - header('Content-Type: text/csv; charset=utf-8'); - header("Content-Disposition: attachment;filename={$filename}"); - $fp = fopen('php://output', 'w'); - - // builds the first row of the table in the CSV - column headers: Dates, and the title of the graph - fputcsv($fp, ['Dates', $stat]); - - // builds each subsequent data row - aka the column value - foreach ($dataSeries as $dataSerie) { - $data = $dataSerie['data']; - $numRows = count($data); - $dates = array_keys($data); - - if( empty($numRows)) { - fputcsv($fp, ['no data found!']); - } - for($i = 0; $i < $numRows; $i++) { - $date = $dates[$i]; - $value = $data[$date]; - $row = [$date, $value]; - fputcsv($fp, $row); - } - } - exit(); - } - /* The only unique identifier available to determine for which sideload to fetch data is the sideload's name as $profileName. It is used here to find the sideloads' id as only this exists on the sideload usage tables */ - private function getSideloadIdBySideLoadName($name) { + private function getSideloadIdBySideLoadName($name): int { $sideload = new SideLoad(); $sideload->whereAdd('name = "' . $name .'"'); $sideload->selectAdd(); @@ -113,13 +45,16 @@ private function getSideloadIdBySideLoadName($name) { return $sideload->fetch()->id; } - private function getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId) { + protected function getAndSetInterfaceDataSeries($stat, $instanceName): void { global $interface; $dataSeries = []; $columnLabels = []; $usage = []; + $profileName= $_REQUEST['subSection']; + $sideloadId = $this->getSideloadIdBySideLoadName($profileName); + // for the graph displaying data retrieved from the user_sideload_usage table if ($stat == 'activeUsers') { $usage = new UserSideLoadUsage(); @@ -133,11 +68,7 @@ private function getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId) $usage->selectAdd('month'); $usage->orderBy('year, month'); - $dataSeries['Active Users'] = [ - 'borderColor' => 'rgba(255, 99, 132, 1)', - 'backgroundColor' => 'rgba(255, 99, 132, 0.2)', - 'data' => [], - ]; + $dataSeries['Active Users'] = GraphingUtils::getDataSeriesArray(count($dataSeries)); $usage->selectAdd('COUNT(id) as numUsers'); } @@ -155,11 +86,7 @@ private function getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId) $usage->selectAdd('month'); $usage->orderBy('year, month'); - $dataSeries['Records Accessed Online'] = [ - 'borderColor' => 'rgba(255, 99, 132, 1)', - 'backgroundColor' => 'rgba(255, 99, 132, 0.2)', - 'data' => [], - ]; + $dataSeries['Records Accessed Online'] = GraphingUtils::getDataSeriesArray(count($dataSeries)); $usage->selectAdd('SUM(IF(timesUsed>0,1,0)) as numRecordsUsed'); } @@ -184,7 +111,7 @@ private function getAndSetInterfaceDataSeries($stat, $instanceName, $sideloadId) $interface->assign('translateColumnLabels', false); } - private function assignGraphSpecificTitle($stat) { + protected function assignGraphSpecificTitle($stat): void { global $interface; $title = $interface->getVariable('graphTitle'); if ($stat == 'activeUsers') {