Commit 29446b1e authored by Maik Messerschmidt's avatar Maik Messerschmidt
Browse files

Added running id to csv export (if wanted).

Closes #3
parent bd9d82c1
...@@ -121,9 +121,13 @@ class ResultController extends Controller ...@@ -121,9 +121,13 @@ class ResultController extends Controller
$all_rows = collect(); $all_rows = collect();
$id = config('experiment.csv-id-header-start');
$id_format = config('experiment.csv-id-header-format');
$id_header = config('experiment.csv-id-header');
foreach ($results as $result) { foreach ($results as $result) {
$rows = collect($result->data ?? []) $rows = collect($result->data ?? [])
->map(function($row) use ($header) { ->map(function($row) use ($header, $id_header, $id_format, $id) {
foreach ($header as $key) { foreach ($header as $key) {
// Fill empty columns with N/A // Fill empty columns with N/A
$row[$key] = $row[$key] ?? config('experiment.n-a-text'); $row[$key] = $row[$key] ?? config('experiment.n-a-text');
...@@ -133,10 +137,20 @@ class ResultController extends Controller ...@@ -133,10 +137,20 @@ class ResultController extends Controller
$row[$key] = json_encode($row[$key]); $row[$key] = json_encode($row[$key]);
} }
if ($id_header)
return array_merge(
[$id_header => sprintf($id_format, $id)], $row);
else
return $row; return $row;
}); });
$all_rows = $all_rows->concat($rows); $all_rows = $all_rows->concat($rows);
$id += 1;
}
if ($id_header) {
$header->prepend($id_header);
} }
return (object) [ return (object) [
......
...@@ -6,6 +6,13 @@ return [ ...@@ -6,6 +6,13 @@ return [
*/ */
'n-a-text' => 'N/A', 'n-a-text' => 'N/A',
/**
* header field of a running index added by the csv export.
*/
'csv-id-header' => env('EXPERIMENT_CSV_ID_HEADER', null),
'csv-id-header-start' => env('EXPERIMENT_CSV_ID_HEADER_START', 1),
'csv-id-header-format' => env('EXPERIMENT_CSV_ID_HEADER_FORMAT', '%d'),
/** /**
* The token for the next and submit route. * The token for the next and submit route.
* If the token is null, everyone can access these routes. * If the token is null, everyone can access these routes.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment