Commit 0374dab2 authored by ZeMKI's avatar ZeMKI
Browse files

delete bugfix

* inserted studyname in delete action
* new actions when creating a study and conducting interview
* started user profile
parent 66efc03d
......@@ -35,18 +35,14 @@ class InterviewController extends Controller
$data['interview'] = $interview;
$data['screenshots'] = [];
$data['sortingtoken'] = $interview->tokens;
$hasExtremeQuestion = $interview->study->questions->filter(function ($item)
{
$hasExtremeQuestion = $interview->study->questions->filter(function ($item) {
return $item->detail == "extremeQuestion";
})->first();
if ($interview->study->sortings[0]->id === 3 && $hasExtremeQuestion !== null)
{
if ($interview->study->sortings[0]->id === 3 && $hasExtremeQuestion !== null) {
$data['extremequestion'] = $hasExtremeQuestion;
foreach ($interview->answers as $answer)
{
foreach ($interview->answers as $answer) {
$token_answer = json_decode($answer->getOriginal('pivot_result'));
$tokenToChange = $data['sortingtoken']->filter(function ($item) use ($token_answer)
{
$tokenToChange = $data['sortingtoken']->filter(function ($item) use ($token_answer) {
return $item->id == $token_answer->token_id;
})->first();
$tokenToChange['answer'] = $token_answer->text;
......@@ -74,7 +70,9 @@ class InterviewController extends Controller
public function create(Request $request)
{
$study = Study::where('id', '=', $request->input('study'));
if (auth()->check()) $this->authorize([Interview::class, $study->first()]);
if (auth()->check()) {
$this->authorize([Interview::class, $study->first()]);
}
/** Extract method FORMATQUESTIONSANSWERS */
$this->FormatQuestionsAndAnswers($request, $returnQuestions);
$data['questions'] = $returnQuestions;
......@@ -110,20 +108,16 @@ class InterviewController extends Controller
group by qid,q,type';
$questions = DB::select(DB::raw($query));
$returnQuestions['presort'] = $returnQuestions['postsort'] = $returnQuestions['extremeQuestion'] = [];
foreach ($questions as $question)
{
foreach ($questions as $question) {
$question->answer = json_decode('[' . $question->answer . ']');
$question->answer['ids'] = explode(',', $question->answerids);
if ($question->type === 'presort')
{
if ($question->type === 'presort') {
array_push($returnQuestions['presort'], $question);
}
if (Str::contains($question->type, 'postsort'))
{
if (Str::contains($question->type, 'postsort')) {
array_push($returnQuestions['postsort'], $question);
}
if ($question->type === 'extremeQuestion')
{
if ($question->type === 'extremeQuestion') {
array_push($returnQuestions['extremeQuestion'], $question);
}
}
......@@ -135,14 +129,11 @@ class InterviewController extends Controller
private function GetTokensForInterview(&$data): void
{
$TokensCount = count($data['tokens']);
for ($i = 0; $i < $TokensCount; $i++)
{
if (strpos($data['tokens'][$i]['image_path'], 'presets') !== false)
{
for ($i = 0; $i < $TokensCount; $i++) {
if (strpos($data['tokens'][$i]['image_path'], 'presets') !== false) {
$path = $data['tokens'][$i]['image_path'];
$data['tokens'][$i]['image_path'] = mb_convert_encoding($path, 'HTML-ENTITIES', 'UTF-8');
} else
{
} else {
$path = storage_path('app/' . $data['tokens'][$i]['image_path']);
$data['tokens'][$i]['image_path'] = decrypt(file_get_contents($path));
}
......@@ -163,6 +154,8 @@ class InterviewController extends Controller
Files::storeSortingScreenshot($request, $study, $interview->id, $name);
$this->SaveTokenValues($request, $interview);
Answer::saveResultQuestions($request, $interview);
auth()->user()->addAction('Interview Conducted', $request->url(), 'user conducted interview for study' . $study->name);
// Study::removeUpdatePermission($study);
return response()->json('Interview Saved!', 200);
}
......@@ -173,17 +166,13 @@ class InterviewController extends Controller
*/
private function SaveTokenValues(Request $request, Interview $interview): void
{
foreach ($request->input('sorting') as $sorting)
{
foreach ($sorting['tokens'] as $token)
{
foreach ($request->input('sorting') as $sorting) {
foreach ($sorting['tokens'] as $token) {
$token['valutation']['position'] = $token['position'];
if (isset($token['percentagePosition']))
{
if (isset($token['percentagePosition'])) {
$token['valutation']['percentagePosition'] = $token['percentagePosition'];
}
foreach ($token['valutation']['classifiers'] as $key => $classifier)
{
foreach ($token['valutation']['classifiers'] as $key => $classifier) {
unset($token['valutation']['classifiers'][$key]['base64']);
}
$interview->tokens()->attach($token['id'], ['interview_id' => $interview->id, 'sorting_id' => 1, 'valutation' => json_encode($token['valutation'])]);
......@@ -199,8 +188,7 @@ class InterviewController extends Controller
*/
public function destroy(Interview $interview, Request $request)
{
if (auth()->user()->notOwnerNorInvited($interview->study))
{
if (auth()->user()->notOwnerNorInvited($interview->study)) {
return response()->json(__('You are not authorized to delete this interview.'), 403);
}
$id = $interview->id;
......@@ -222,25 +210,26 @@ class InterviewController extends Controller
*/
public function export(Interview $interview)
{
if (auth()->user()->notOwnerNorInvited($interview->study))
{
if (auth()->user()->notOwnerNorInvited($interview->study)) {
abort(403, "You are not authorized to download these data.");
}
$headings = $this->getHeadings($interview->study);
if ($interview->study->sortings[0]->id === 3) return (new InterviewQsortExport($interview->id, $headings))->download($interview->interviewed . ' tokens.xlsx');
else return (new InterviewTokenExport($interview->id, $headings, $interview->study->sortings[0]->id))->download($interview->interviewed . ' tokens.xlsx');
if ($interview->study->sortings[0]->id === 3) {
return (new InterviewQsortExport($interview->id, $headings))->download($interview->interviewed . ' tokens.xlsx');
} else {
return (new InterviewTokenExport($interview->id, $headings, $interview->study->sortings[0]->id))->download($interview->interviewed . ' tokens.xlsx');
}
}
public function getHeadings($study)
{
if ($study->sortings[0]->id === 2)
{
if ($study->sortings[0]->id === 2) {
return ["section number", "section name"];
} elseif($study->sortings[0]->id === 3){
} elseif ($study->sortings[0]->id === 3) {
return ['Reason for Placement'];
}else return [];
} else {
return [];
}
}
/**
......
......@@ -35,7 +35,9 @@ class StudyController extends Controller
public function show(Study $study)
{
if (auth()->user()->notOwnerNorInvited($study)) abort(403, "You are not authorized to see this content.");
if (auth()->user()->notOwnerNorInvited($study)) {
abort(403, "You are not authorized to see this content.");
}
$data['interviews'] = $study->interviews()->get();
$data['invites'] = $study->invited;
......@@ -53,26 +55,21 @@ class StudyController extends Controller
on art_urls.url like CONCAT(\'%\',study_interview_public_url.id,\'%\')
where study_id = :study', ['study' => $study->id]));
foreach ($data['publicInterviews'] as $publicInterview)
{
$publicInterview->shorturl = url('','short')."=".$publicInterview->code;
$publicInterview->url = (\App\Helpers\Helper::get_string_between($publicInterview->url,"&interviewed=","&t") == "") ? "Name not provided" : Helper::get_string_between($publicInterview->url,"&interviewed=","&t");
foreach ($data['publicInterviews'] as $publicInterview) {
$publicInterview->shorturl = url('', 'short')."=".$publicInterview->code;
$publicInterview->url = (\App\Helpers\Helper::get_string_between($publicInterview->url, "&interviewed=", "&t") == "") ? "Name not provided" : Helper::get_string_between($publicInterview->url, "&interviewed=", "&t");
}
$data['study'] = $study;
foreach ($data['interviews'] as $interview)
{
foreach ($data['interviews'] as $interview) {
$interview['author'] = User::where('id', $interview['author'])->first()->email ?? $interview['author'];
}
return view('study.show', $data);
}
public function create(Request $request)
{
if (session('hasReachMaxNumberOfStudies'))
{
if (session('hasReachMaxNumberOfStudies')) {
auth()->user()->addAction('Trying to create a study', $request->url(), 'Max numbers of studies reached for user ' . auth()->user()->email);
abort(403, __('You reached the max number of studies'));
}
......@@ -90,18 +87,14 @@ class StudyController extends Controller
$copy = $study->replicate();
$copy->save();
$study->load('questions', 'sortings', 'tokens');
foreach ($study->getRelations() as $relationName => $values)
{
if ($relationName === "sortings")
{
foreach ($study->getRelations() as $relationName => $values) {
if ($relationName === "sortings") {
$request = new Request();
$request->replace(['details' => $study->sortings[0]->pivot->details, 'sortingid' => $study->sortings[0]->id]);
Sorting::store($request, $copy);
} else if ($relationName === "questions")
{
} elseif ($relationName === "questions") {
$this->CopyQuestionsRemoveInterviews($study, $copy);
} else if ($relationName === "tokens")
{
} elseif ($relationName === "tokens") {
$this->RemoveTokensCreatedDuringInterview($values);
$copy->{$relationName}()->attach($values);
}
......@@ -117,19 +110,15 @@ class StudyController extends Controller
*/
private function CopyQuestionsRemoveInterviews(Study $study, Study $copy): void
{
foreach ($study->questions as $question)
{
foreach ($study->questions as $question) {
$copiedQuestion = $copy->questions()->create($question->toArray());
$answers = Answer::where('question_id', $question->id)->get();
foreach ($answers as $answer)
{
foreach ($answers as $answer) {
$answer->question_id = $copiedQuestion->id;
$copiedQuestion->answers()->create($answer->toArray());
}
// remove interviews
foreach ($copiedQuestion->answers as $answer)
{
foreach ($copiedQuestion->answers as $answer) {
$answer->interviews()->detach();
$answer->interviews()->delete();
}
......@@ -141,11 +130,9 @@ class StudyController extends Controller
*/
private function RemoveTokensCreatedDuringInterview(&$values): void
{
// remove tokens created during interview
foreach ($values as $index => $token)
{
if ($token->author == 0)
{
// remove tokens created during interview
foreach ($values as $index => $token) {
if ($token->author == 0) {
unset($values[$index]);
}
}
......@@ -153,13 +140,13 @@ class StudyController extends Controller
public function store(Request $request, $dummy = false)
{
if ($dummy != false)
{
if ($dummy != false) {
$userForStudy = User::where('id', $dummy)->first();
$request = new Request(config('utilities.dummyStudy1'));
} else $userForStudy = Auth::user();
if (!$request->has('name') || !$request->has('description'))
{
} else {
$userForStudy = Auth::user();
}
if (!$request->has('name') || !$request->has('description')) {
return response()->json('Data are not valid', 422);
}
$newStudy = new Study();
......@@ -178,11 +165,12 @@ class StudyController extends Controller
$this->saveQuestionsAnswers($presortQuestions, $newStudy);
$this->saveQuestionsAnswers($postSortQuestions, $newStudy);
if ($request->input('sorting.qsortaskextremes'))
{
if ($request->input('sorting.qsortaskextremes')) {
// create question extreme and 1 answer associated.
Question::storeExtremeQuestion($request->input('sorting.qsortextremequestion'),$newStudy);
Question::storeExtremeQuestion($request->input('sorting.qsortextremequestion'), $newStudy);
}
auth()->user()->addAction('Study Created', $request->url(), 'user created study ' . $newStudy->name);
return response()->json(['message' => 'Study Saved!', 'studyid' => $newStudy->id], 200);
}
......@@ -194,11 +182,8 @@ class StudyController extends Controller
*/
public function saveTokens(Study $study, Request $request, &$token): void
{
if ($request->get('sorting')['tokens'])
{
foreach ($request->get('sorting')['tokens'] as $tokenToSave)
{
if ($request->get('sorting')['tokens']) {
foreach ($request->get('sorting')['tokens'] as $tokenToSave) {
Token::store($tokenToSave, $study);
}
}
......@@ -212,28 +197,25 @@ class StudyController extends Controller
*/
public function saveQuestionsAnswers($questions, $study, $edit = false)
{
if ($edit)
{
foreach ($study->questions as $questionToReset)
{
if ($edit) {
foreach ($study->questions as $questionToReset) {
Answer::where('question_id', '=', $questionToReset['id'])->delete();
Question::destroy($questionToReset['id']);
}
}
$atLeastOneQuestion = array_key_exists('question', $questions[0]);
if ($atLeastOneQuestion)
{
foreach ($questions as $questionToSave)
{
if ($atLeastOneQuestion) {
foreach ($questions as $questionToSave) {
$question = new Question();
$question->question = $questionToSave['question'];
$question->detail = $questions[0]['type'];
if(array_key_exists('canShowSorting',$questionToSave) && $questionToSave['canShowSorting']) $question->detail .= '|showsorting';
if (array_key_exists('canShowSorting', $questionToSave) && $questionToSave['canShowSorting']) {
$question->detail .= '|showsorting';
}
$question->study_id = $study->id;
$question->save();
$this->saveAnswers($questionToSave, $question);
}
}
}
......@@ -244,10 +226,8 @@ class StudyController extends Controller
*/
private function saveAnswers($questionToSave, Question $question): void
{
if ($questionToSave['ismultiple'])
{
foreach ($questionToSave['answers'] as $answerToSave)
{
if ($questionToSave['ismultiple']) {
foreach ($questionToSave['answers'] as $answerToSave) {
$answer = new Answer();
$answer->question_id = $question->id;
$answerJson = ['type' => 'multi', 'answer' => $answerToSave];
......@@ -255,10 +235,8 @@ class StudyController extends Controller
$answer->save();
}
}
if ($questionToSave['isonechoice'])
{
foreach ($questionToSave['answers'] as $answerToSave)
{
if ($questionToSave['isonechoice']) {
foreach ($questionToSave['answers'] as $answerToSave) {
$answer = new Answer();
$answer->question_id = $question->id;
$answerJson = ['type' => 'onechoice', 'answer' => $answerToSave];
......@@ -266,16 +244,14 @@ class StudyController extends Controller
$answer->save();
}
}
if ($questionToSave['isopen'])
{
if ($questionToSave['isopen']) {
$answer = new Answer();
$answerJson = ['type' => 'open', 'answer' => ''];
$answer->question_id = $question->id;
$answer->answer = $answerJson;
$answer->save();
}
if ($questionToSave['isscale'])
{
if ($questionToSave['isscale']) {
$answer = new Answer();
$answerJson = ['type' => 'scale', 'answer' => ['min' => $questionToSave['scalemin'], 'max' => $questionToSave['scalemax'], 'minlabel' => $questionToSave['minlabel'], 'maxlabel' => $questionToSave['maxlabel']]];
$answer->question_id = $question->id;
......@@ -314,13 +290,11 @@ class StudyController extends Controller
public function update(Study $study, Request $request)
{
$this->authorize($study);
if (!$request->has('name'))
{
if (!$request->has('name')) {
return response()->json('Data are not valid', 422);
}
$study = Study::where('id', '=', $request->input('id'))->with('questions')->first();
if (!$study->isEditable())
{
if (!$study->isEditable()) {
return back()->withInput();
}
// sorting
......@@ -340,10 +314,9 @@ class StudyController extends Controller
$postsortQuestions[0]['type'] = 'postsort';
$this->saveQuestionsAnswers($presortQuestions, $study, true);
$this->saveQuestionsAnswers($postsortQuestions, $study, true);
if ($request->input('sorting.qsortaskextremes'))
{
if ($request->input('sorting.qsortaskextremes')) {
// create question extreme and 1 answer associated.
Question::storeExtremeQuestion($request->input('sorting.qsortextremequestion'),$study);
Question::storeExtremeQuestion($request->input('sorting.qsortextremequestion'), $study);
}
return response()->json(['message' => 'Study Updated!', 'studyid' => $study->id], 200);
}
......@@ -355,8 +328,7 @@ class StudyController extends Controller
{
// @todo: delete files as well as tokens.
$available_tokens = $study->available_tokens()->pluck('tokens.id')->toArray();
foreach ($available_tokens as $tokensToDelete)
{
foreach ($available_tokens as $tokensToDelete) {
$to = Token::where('id', '=', $tokensToDelete)->first();
$study->tokens()->detach($to->id);
$to->delete();
......@@ -371,12 +343,10 @@ class StudyController extends Controller
*/
public function destroy(Study $study, Request $request)
{
if (auth()->user()->isNot($study->creator()))
{
if (auth()->user()->isNot($study->creator())) {
abort(403, 'You are not allowed to edit this study');
}
foreach ($study->interviews as $interview)
{
foreach ($study->interviews as $interview) {
File::delete($interview->sorting_screenshot);
$interview->answers()->detach();
$interview->answers()->delete();
......@@ -410,8 +380,7 @@ class StudyController extends Controller
public function deleteallbyuser(User $user, Request $request)
{
$studies = Study::where('user_id', $user->id)->get();
foreach ($studies as $study)
{
foreach ($studies as $study) {
$study->delete();
}
auth()->user()->addAction('delete all studies by user', $request->url(), 'user deleted studies for the user ' . $user->email);
......@@ -424,11 +393,9 @@ class StudyController extends Controller
*/
public function inviteUser(Request $request)
{
$study = Study::where('id', $request->input('study'))->first();
$user = User::where('email', '=', $request->email)->first();
if (!$user)
{
if (!$user) {
$user = new User();
$user->email = $request->email;
$user->password = Helper::random_str(60);
......@@ -446,9 +413,10 @@ class StudyController extends Controller
->useSecret('Jj3dDY2KzSFDS2kxZ/SvbmjdswXTASAXxC2GfgfTpFooK5Eo4kFBGPyDRrtsWmgED3')
->dispatch();
}
}
if($user->is($study->creator())) return response()->json(['message' => __('You can\'t invite who created the study.!')], 200);
if ($user->is($study->creator())) {
return response()->json(['message' => __('You can\'t invite who created the study.!')], 200);
}
$study->invited()->syncWithoutDetaching($user->id);
return response()->json(['user' => $user, 'message' => __('User was invited!')], 200);
......@@ -456,18 +424,16 @@ class StudyController extends Controller
public function removeFromStudy(Request $request)
{
$user_is_not_invited = !in_array(Study::find($request->input('study'))->id, auth()->user()->invites()->pluck('study_id')->toArray());
if($user_is_not_invited && !auth()->user()->is(Study::find($request->input('study'))->creator()) ) return response()->json(['message' => __('Not authorized!')], 403);
if ($user_is_not_invited && !auth()->user()->is(Study::find($request->input('study'))->creator())) {
return response()->json(['message' => __('Not authorized!')], 403);
}
$user = User::where('email', '=', $request->email)->first();
if ($user)
{
if ($user) {
$user->invites()->detach($request->input('study'));
return response()->json(['message' => __('User was removed from the Study!')], 200);
} else
{
} else {
return response()->json(['message' => __("The user doesn't exist!")], 403);
}
}
......
......@@ -23,6 +23,7 @@ use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;
use App\Action;
class UserController extends Controller
{
......@@ -50,7 +51,9 @@ class UserController extends Controller
*/
public function show()
{
return view('user.profile');
$data['actions'] = Action::join('users', 'users.id', 'actions.user_id')->where('users.id', '=', auth()->user()->id)->orderBy('actions.id', 'desc')->paginate('10');
ray($data);
return view('user.profile', $data);
}
......
......@@ -283,7 +283,7 @@
v-if="study.authiscreator"
type="button"
href="#"
@click="confirmdelete(study.id, 'study.name')"
@click="confirmdelete(study.id, study.name)"
class="inline-flex items-center justify-center px-4 py-2 mt-3 text-sm font-medium text-white bg-red-500 border border-gray-300 rounded-md shadow-sm hover:bg-red-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 sm:mt-0 sm:ml-3 xl:ml-0 xl:mt-3 xl:w-full"
>{{ trans("Delete Study") }}</a
>
......
......@@ -239,9 +239,9 @@
<div class="px-4 py-5 bg-white shadow sm:rounded-lg sm:px-6">
<h2 id="timeline-title" class="text-lg font-medium text-gray-900">Timeline</h2>
<!-- Activity Feed -->
<div class="flow-root mt-6">
<ul role="list" class="-mb-8">
@foreach($actions as $action)
<li>
<div class="relative pb-8">
<span class="absolute top-4 left-4 -ml-px h-full w-0.5 bg-gray-200" aria-hidden="true"></span>
......@@ -258,126 +258,18 @@
</div>
<div class="min-w-0 flex-1 pt-1.5 flex justify-between space-x-4">
<div>
<p class="text-sm text-gray-500">Applied to <a href="#" class="font-medium text-gray-900">Front
End Developer</a></p>
<p class="text-sm text-gray-500">{{$action->name}}</p>
<p class="text-sm text-gray-500">{{$action->description}}</p>
</div>
<div class="text-sm text-right text-gray-500 whitespace-nowrap">
<time datetime="2020-09-20">Sep 20</time>
<time datetime="{{$action->time}}" {{$action->time}}></time>
</div>
</div>
</div>
</div>
</li>
@endforeach
<li>
<div class="relative pb-8">
<span class="absolute top-4 left-4 -ml-px h-full w-0.5 bg-gray-200" aria-hidden="true"></span>
<div class="relative flex space-x-3">
<div>
<span class="flex items-center justify-center w-8 h-8 bg-blue-500 rounded-full ring-8 ring-white">
<!-- Heroicon name: solid/thumb-up -->
<svg class="w-5 h-5 text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"
fill="currentColor" aria-hidden="true">
<path
d="M2 10.5a1.5 1.5 0 113 0v6a1.5 1.5 0 01-3 0v-6zM6 10.333v5.43a2 2 0 001.106 1.79l.05.025A4 4 0 008.943 18h5.416a2 2 0 001.962-1.608l1.2-6A2 2 0 0015.56 8H12V4a2 2 0 00-2-2 1 1 0 00-1 1v.667a4 4 0 01-.8 2.4L6.8 7.933a4 4 0 00-.8 2.4z" />
</svg>
</span>
</div>
<div class="min-w-0 flex-1 pt-1.5 flex justify-between space-x-4">
<div>
<p class="text-sm text-gray-500">Advanced to phone screening by <a href="#"
class="font-medium text-gray-900">Bethany Blake</a></p>
</div>
<div class="text-sm text-right text-gray-500 whitespace-nowrap">
<time datetime="2020-09-22">Sep 22</time>
</div>
</div>
</div>
</div>
</li>
<li>
<div class="relative pb-8">
<span class="absolute top-4 left-4 -ml-px h-full w-0.5 bg-gray-200" aria-hidden="true"></span>
<div class="relative flex space-x-3">
<div>
<span class="flex items-center justify-center w-8 h-8 bg-green-500 rounded-full ring-8 ring-white">
<!-- Heroicon name: solid/check -->
<svg class="w-5 h-5 text-white" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"
fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd"