Skip to content

Commit

Permalink
Merge pull request #683 from TheRestartProject/RES-1932_event_stats_e…
Browse files Browse the repository at this point in the history
…xport

RES-1932 event stats export
  • Loading branch information
edwh authored Nov 27, 2023
2 parents 11650d5 + 31da845 commit 58eaa0a
Show file tree
Hide file tree
Showing 23 changed files with 164 additions and 943 deletions.
133 changes: 0 additions & 133 deletions app/Helpers/SearchHelper.php

This file was deleted.

163 changes: 85 additions & 78 deletions app/Http/Controllers/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\GrouptagsGroups;
use App\Helpers\Fixometer;
use App\Helpers\SearchHelper;
use App\Network;
use App\Party;
use App\Search;
use App\User;
Expand Down Expand Up @@ -75,20 +76,21 @@ public function devices(Request $request, $idevents = NULL, $idgroups = NULL)

$me = auth()->user();

// We can't put accented characters into a CSV file, so flatten them.
$columns = [
'Item Type',
'Product Category',
'Brand',
'Model',
'Comments',
'Repair Status',
'Spare parts (needed/used)',
'Event',
'Group',
'Date',
'Waste Prevented',
'CO2 Prevented',
'Powered'
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.item_type_short')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.category')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.brand')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.model')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.title_assessment')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.repair_status')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('devices.spare_parts')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('events.event')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.group')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('events.event_date')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('events.stat-7')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('events.stat-6')),
iconv('UTF-8', 'ASCII//TRANSLIT', ucfirst(__('devices.title_powered')))
];

fputcsv($file, $columns);
Expand Down Expand Up @@ -145,78 +147,83 @@ public function devices(Request $request, $idevents = NULL, $idgroups = NULL)
/**
* @return \Illuminate\Http\Response
*/
public function parties(Request $request)
public function groupEvents(Request $request, $idgroups)
{
if ($request->has('fltr') && ! empty($request->input('fltr'))) {
$dropdowns = SearchHelper::getUserGroupsAndParties();
$filters = SearchHelper::getSearchFilters($request);

$Search = new Search;
$PartyList = $Search->parties(
$filters['searched_parties'],
$filters['searched_groups'],
$filters['from_date'],
$filters['to_date'],
$filters['group_tags'],
$dropdowns['allowed_parties']
);

if (count($PartyList) > 0) {

// prepare the column headers
$statsKeys = array_keys(\App\Party::getEventStatsArrayKeys());
array_walk($statsKeys, function (&$k) {
$key = explode('_', $k);
array_walk($key, function (&$v) {
$v = str_replace('Waste', 'Weight', str_replace('Co2', 'CO2', ucfirst($v)));
});
$k = implode(' ', $key);
});
$headers = array_merge(['Date', 'Venue', 'Group', 'Approved'], $statsKeys);

// Send these to getEventStats() to speed things up a bit.
$eEmissionRatio = \App\Helpers\LcaStats::getEmissionRatioPowered();
$uEmissionratio = \App\Helpers\LcaStats::getEmissionRatioUnpowered();

// prepare the column values
$PartyArray = [];
foreach ($PartyList as $i => $party) {
$stats = $party->getEventStats($eEmissionRatio, $uEmissionratio);
array_walk($stats, function (&$v) {
$v = round($v);
});

$PartyArray[$i] = [
$party->getFormattedLocalStart(),
$party->getEventName(),
$party->theGroup && $party->theGroup->name ? $party->theGroup->name : '?',
$party->approved ? 'true' : 'false',
];
$PartyArray[$i] += $stats;
}
$group = Group::findOrFail($idgroups);
$parties = $group->parties()->undeleted()->get();
return $this->exportEvents($parties);
}

// write content to file
$filename = 'parties.csv';
public function networkEvents(Request $request, $id)
{
$network = Network::findOrFail($id);
$parties = collect([]);

$file = fopen($filename, 'w+');
fputcsv($file, $headers);
foreach ($network->groups as $group) {
$parties = $parties->merge($group->parties()->undeleted()->get());
}

foreach ($PartyArray as $d) {
fputcsv($file, $d);
}
fclose($file);
return $this->exportEvents($parties);
}

$headers = [
'Content-Type' => 'text/csv',
];
private function exportEvents($parties) {
// We can't put accented characters into a CSV file, so flatten them.
$headers = [
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.date')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.event')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.volunteers')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.participants')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_total')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_fixed')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_repairable')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_end_of_life')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_kg_waste_prevented')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.items_kg_co2_prevent')),
iconv('UTF-8', 'ASCII//TRANSLIT', __('groups.export.events.group'))
];

return Response::download($filename, $filename, $headers);
}
// }
// Send these to getEventStats() to speed things up a bit.
$eEmissionRatio = \App\Helpers\LcaStats::getEmissionRatioPowered();
$uEmissionratio = \App\Helpers\LcaStats::getEmissionRatioUnpowered();

// prepare the column values
$PartyArray = [];
foreach ($parties as $party) {
$stats = $party->getEventStats($eEmissionRatio, $uEmissionratio);
array_walk($stats, function (&$v) {
$v = round($v);
});

$PartyArray[] = [
$party->getFormattedLocalStart(),
$party->getEventName(),
$party->volunteers,
$party->participants ? $party->participants : 0,
$stats['fixed_devices'] + $stats ['repairable_devices'] + $stats['dead_devices'],
$stats['fixed_devices'],
$stats['repairable_devices'],
$stats['dead_devices'],
$stats['waste_powered'] + $stats['waste_unpowered'],
$stats['co2_powered'] + $stats['co2_unpowered'],
iconv('UTF-8', 'ASCII//TRANSLIT', $party->theGroup && $party->theGroup->name ? $party->theGroup->name : '?'),
];
}

// write content to file
$filename = 'events.csv';

$file = fopen($filename, 'w+');
fputcsv($file, $headers);

foreach ($PartyArray as $d) {
fputcsv($file, $d);
}
fclose($file);

$headers = [
'Content-Type' => 'text/csv',
];

return view('export.parties', [
'data' => ['No data to return'],
]);
return Response::download($filename, $filename, $headers);
}
}
Loading

0 comments on commit 58eaa0a

Please sign in to comment.