Skip to content

Commit

Permalink
refactor: move buildCSV() to UsageGraphs_UsageGraphs
Browse files Browse the repository at this point in the history
The buildCSV() method is shared amongst all usagegraphs classes and can
therefore be added to the abstract UsageGraphs_UsageGraphs class,
provided that it is amended to take in a string as a parameter with the
name of the section (eg. 'Admin')
  • Loading branch information
Chloe070196 committed Aug 30, 2024
1 parent 535281e commit 1bfc797
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion code/web/services/Admin/AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ public function copyMenuLinks() {
public function exportUsageData() {
require_once ROOT_DIR . '/services/Admin/UsageGraphs.php';
$aspenUsageGraph = new Admin_UsageGraphs();
$aspenUsageGraph->buildCSV();
$aspenUsageGraph->buildCSV('Admin');
// TODO: trigger page refresh
}
}
44 changes: 44 additions & 0 deletions code/web/services/Admin/AbstractUsageGraphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,48 @@ public function canView(): bool {
]);
}

public function buildCSV(string $section): void {
global $interface;

$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
$dataSeries = $interface->getVariable('dataSeries');

$filename = "{$section}UsageData_{$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');
$graphTitles = array_keys($dataSeries);
$numGraphTitles = count($dataSeries);

// builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph
for($i = 0; $i < $numGraphTitles; $i++) {
$dataSerie = $dataSeries[$graphTitles[$i]];
$numRows = count($dataSerie['data']);
$dates = array_keys($dataSerie['data']);
$header = ['Dates', $graphTitles[$i]];
fputcsv($fp, $header);

// builds each subsequent data row - aka the column value
if (empty($numRows)) {
fputcsv($fp, ['no data found']);
}
for($j = 0; $j < $numRows; $j++) {
$date = $dates[$j];
$value = $dataSerie['data'][$date];
$row = [$date, $value];
fputcsv($fp, $row);
}
}
exit();
}
}
42 changes: 0 additions & 42 deletions code/web/services/Admin/UsageGraphs.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,6 @@ function getActiveAdminSection(): string {
return 'system_reports';
}


public function buildCSV() {
global $interface;

$stat = $_REQUEST['stat'];
if (!empty($_REQUEST['instance'])) {
$instanceName = $_REQUEST['instance'];
} else {
$instanceName = '';
}
$this->getAndSetInterfaceDataSeries($stat, $instanceName);
$dataSeries = $interface->getVariable('dataSeries');

$filename = "AspenUsageData_{$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');
$graphTitles = array_keys($dataSeries);
$numGraphTitles = count($dataSeries);

// builds the header for each section of the table in the CSV - column headers: Dates, and the title of the graph
for($i = 0; $i < $numGraphTitles; $i++) {
$dataSerie = $dataSeries[$graphTitles[$i]];
$numRows = count($dataSerie['data']);
$dates = array_keys($dataSerie['data']);
$header = ['Dates', $graphTitles[$i]];
fputcsv($fp, $header);

// builds each subsequent data row - aka the column value
for($j = 0; $j < $numRows; $j++) {
$date = $dates[$j];
$value = $dataSerie['data'][$date];
$row = [$date, $value];
fputcsv($fp, $row);
}
}
exit();
}
private function getAndSetInterfaceDataSeries($stat, $instanceName) {
global $interface;
global $enabledModules;
Expand Down

0 comments on commit 1bfc797

Please sign in to comment.