Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ARC-Support
public projects
lou-server
Commits
94c6cad1
Commit
94c6cad1
authored
Nov 04, 2020
by
Maik Messerschmidt
Browse files
Added route, which let's you download all data as csv.
parent
8cf07da3
Changes
3
Hide whitespace changes
Inline
Side-by-side
app/Http/Controllers/ResultController.php
View file @
94c6cad1
...
...
@@ -6,6 +6,8 @@ use App\Experiment;
use
App\Result
;
use
App\Logic\IPAnonymizer
;
use
League\Csv\Writer
;
use
Illuminate\Http\Request
;
use
Str
;
use
Log
;
...
...
@@ -84,6 +86,58 @@ class ResultController extends Controller
]);
}
// Returns object obj with obj->header: array with header info
// obj->rows: arrays with data.
//
// @param $results Collection of Result
private
function
getResults
(
$results
)
{
$header
=
collect
(
$results
->
first
()
->
data
??
[])
->
map
(
function
(
$entry
)
{
return
array_keys
(
$entry
);
})
->
flatten
()
->
unique
();
$all_rows
=
collect
();
foreach
(
$results
as
$result
)
{
$rows
=
collect
(
$result
->
data
??
[])
->
map
(
function
(
$row
)
use
(
$header
)
{
foreach
(
$header
as
$key
)
{
// Fill empty columns with N/A
$row
[
$key
]
=
$row
[
$key
]
??
config
(
'experiment.n-a-text'
);
// Replace arrays with json
if
(
is_array
(
$row
[
$key
]))
$row
[
$key
]
=
json_encode
(
$row
[
$key
]);
}
return
$row
;
});
$all_rows
=
$all_rows
->
concat
(
$rows
);
}
return
(
object
)
[
'header'
=>
$header
->
toArray
(),
'rows'
=>
$all_rows
->
toArray
(),
];
}
public
function
showAllCSV
()
{
header
(
'Content-Type: text/csv; charset=utf-8'
);
header
(
'Content-Type: application/force-download'
);
header
(
'Content-Disposition: attachment; filename=results.csv'
);
$data
=
$this
->
getResults
(
Result
::
all
());
$csv
=
Writer
::
createFromString
(
''
);
$csv
->
insertOne
(
$data
->
header
);
$csv
->
insertAll
(
$data
->
rows
);
return
$csv
->
getContent
();
}
/**
* Display the result as json.
*/
...
...
resources/views/results/index.blade.php
View file @
94c6cad1
...
...
@@ -56,4 +56,6 @@
</
tbody
>
</
table
>
<
a
href
=
"{{ route('results.show-all-csv') }}"
>
Download
all
results
as
CSV
</
a
>
@
endsection
routes/web.php
View file @
94c6cad1
...
...
@@ -22,6 +22,9 @@ Route::middleware(['web', 'auth'])->group(function() {
Route
::
get
(
'/admin/results/show-json/{result}'
,
'ResultController@showJson'
)
->
name
(
'results.show-json'
);
Route
::
get
(
'/admin/results/show-all-csv'
,
'ResultController@showAllCSV'
)
->
name
(
'results.show-all-csv'
);
Route
::
get
(
'/admin/results/partial'
,
'ResultController@indexPartial'
)
->
name
(
'results.index.partial'
);
Route
::
resource
(
'/admin/results'
,
'ResultController'
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment