Commit b400ab4b authored by ZeMKI's avatar ZeMKI
Browse files

Export all the interviews by study + permissions

* now you can export all the interviews for a study, xlsx divided by name of interviewee
* bugfix permissions in various actions
parent 75222671
<?php
namespace App\Exports;
use App\InterviewTokens;
use App\Study;
use Image;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Storage;
class AllInterviewTokenExport implements FromCollection, WithMapping, WithHeadings
{
use Exportable;
public function __construct($id)
{
$this->id = $id;
}
public function headings(): array
{
return [
'#',
'token name',
'circle',
'distance from center (pixels)',
'sorting number',
'position (pixels)',
'% percentage position',
'classifiers'
];
}
/**
* @return array
* @var Interview $interview
*/
public function map($study): array
{
$printArray = [];
$indexForName = 0;
$count = 0;
foreach ($study->interviews as $emptyInterview) {
$allinterviews = InterviewTokens::where('interview_id', $emptyInterview->id)->get();
array_push($printArray,[$emptyInterview->interviewed]);
foreach ($allinterviews as $interview) {
if ($this->invalidData($interview)) continue;
if (isset($interview->valutation->percentagePosition)) {
$percentagePosition = "x: " . $interview->valutation->percentagePosition->x . " y:" . $interview->valutation->percentagePosition->y;
} else {
$percentagePosition = '';
}
if (is_object($interview->valutation->position)) {
$position = "x: " . $interview->valutation->position->x . " y:" . $interview->valutation->position->y;
} else {
$position = $interview->valutation->position;
}
if (is_array($interview->valutation->classifiers)) {
$classifiers = "";
foreach ($interview->valutation->classifiers as $classifier) {
$classifiers .= $classifier->basename . " - ";
}
}
$toprint = [
$interview->token_id,
\App\Token::where('id', $interview->token_id)->first()->name,
$interview->valutation->circle,
$interview->valutation->distance,
$interview->valutation->sorting,
$position,
$percentagePosition,
$classifiers
];
array_push($printArray, $toprint);
}
//array_unshift($printArray,[$emptyInterview->interviewed]);
}
return $printArray;
}
public function collection()
{
return Study::where('id', $this->id)->get();
}
/**
* @param $interview
* @return bool
*/
private function invalidData($interview): bool
{
return !is_object($interview->valutation->position) && $interview->valutation->position == 0 && $interview->valutation->distance == 0 && $interview->valutation->circle == 0;
}
}
......@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Answer;
use App\Exports\AllInterviewTokenExport;
use App\Exports\InterviewTokenExport;
use App\Files;
use App\Interview;
......@@ -163,9 +164,8 @@ class InterviewController extends Controller
*/
public function destroy(Interview $interview, Request $request)
{
if (!auth()->user()->hasRole(['superadministrator', 'supervisor'])) {
return response()->json('You cannot delete this interview', 403);
}
if (!auth()->user()->can('delete-interviews',$interview->study->id)) return response('You are not authorized to delete this study.', 403);
$id = $interview->id;
$interview->files()->delete();
$interview->answers()->detach();
......@@ -182,7 +182,11 @@ class InterviewController extends Controller
public function export(Interview $interview)
{
return (new InterviewTokenExport($interview->id))->download('tokens.xlsx');
if (!auth()->user()->can('read-studies', $interview->study_id)) abort(403, "You are not authorized to download these data.");
return (new InterviewTokenExport($interview->id))->download($interview->interviewed . ' tokens.xlsx');
}
}
......@@ -219,6 +219,8 @@ class StudyController extends Controller
abort(403, 'You are not allowed to edit this study');
}
if (!auth()->user()->can('delete-studies',$study->id)) return response('You are not authorized to delete this study.', 403);
foreach ($study->interviews as $interview) {
File::delete($interview->sorting_screenshot);
$interview->answers()->detach();
......
......@@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Exports\AllInterviewTokenExport;
use App\User;
use App\Study;
use App\Interview;
......@@ -20,4 +21,11 @@ class StudyInterviewController extends Controller
return view('interview.index', $data);
}
public function exportall(Study $study)
{
if (!auth()->user()->can('read-studies', $study->id)) abort(403, "You are not authorized to download these data.");
return (new AllInterviewTokenExport($study->id))->download('Interviews from study' . $study->name . '.xlsx');
}
}
......@@ -100,4 +100,12 @@ class InterviewPolicy
public function forceDelete(User $user, Interview $interview)
{
}
public function exportall(User $user, $study){
if ( $user->can('read-studies', Study::where('id', '=', $study)->first()->name)) {
abort(403, 'You are not allowed to read this study');
}
return true;
}
}
......@@ -336,9 +336,10 @@ window.app = new Vue({
}, 500);
}).catch(function (error) {
console.log(error);
self.loading = false;
self.$buefy.snackbar.open("There it was an error during the request - refresh page and try again");
self.$buefy.snackbar.open(error.response.data);
});
}
,
......
......@@ -6,6 +6,15 @@
<div class="columns">
<div class="column">
<h1 class="text-center">Study {{$study->name}} - Interview list</h1>
<div class="py-4 text-center">
<a href="{{url('export/'.$study->id."/study")}}"
class="bg-red-300 hover:bg-red-400 text-black-800 font-bold py-2 px-4 rounded inline-flex items-center">
<b-icon
class="fill-current w-4 h-4 mr-2"
icon="export"
>
</b-icon> Export all the interviews</a>
</div>
<interview-list :interviews="{{$interviews}}"></interview-list>
</div>
......
......@@ -61,6 +61,7 @@ Route::group(['middleware' => ['auth','verified']], static function () {
Route::get('/interview/{interview}/show', 'InterviewController@show');
Route::delete('/interview/{interview}', 'InterviewController@destroy');
Route::get('/export/{interview}/interview/', 'InterviewController@Export');
Route::get('/export/{study}/study/', 'StudyInterviewController@exportall');
Route::get('/interview/{interview}/sorting/{sorting}/show', 'SortingController@show');
......
Supports Markdown
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