Commit ff846673 authored by ZeMKI's avatar ZeMKI
Browse files

Initial commit

parents
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Interview;
use App\Question;
use App\Answer;
use App\Study;
use App\User;
use Auth;
use DB;
use Storage;
class InterviewController extends Controller
{
public function showInterview(Request $request){
// check permissions for interview!
// if the user has access to this study's interview
$interview = Interview::where('id','=',$request->input('id'))->with('answers')->first();
$study = Study::where('id','=',$interview->study_id)->first();
$author = User::where('id','=',$interview->author)->first();
$data['interview'] = $interview;
$type = pathinfo($interview->sorting_screenshot, PATHINFO_EXTENSION);
$imagedata = file_get_contents($interview->sorting_screenshot);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($imagedata);
$data['interview']['sorting_screenshot'] = $base64;
$data['sortingtoken'] = $interview->tokens;
$data['study'] = $study;
$data['questions'] = $study->questions;
$data['sorting'] = $study->sortings[0];
$pieces = explode("||", $study->sortings[0]->pivot->details);
$circles = strstr($pieces[0], '|');
$circles = substr($circles, 1);
$data['circles'] = $circles;
$data['tokens'] = $study->available_tokens;
$data['author'] = $author;
return view('interview.view',$data);
}
public function showSorting(Request $request){
// check permissions for interview!
// if the user has access to this study's interview
$interview = Interview::where('id','=',$request->input('id'))->with('answers')->first();
$study = Study::where('id','=',$interview->study_id)->first();
$author = User::where('id','=',$interview->author)->first();
$data['interview'] = $interview;
$type = pathinfo($interview->sorting_screenshot, PATHINFO_EXTENSION);
$imagedata = file_get_contents($interview->sorting_screenshot);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($imagedata);
$data['sorting_screenshot'] = $base64;
$data['sortingtoken'] = $interview->tokens;
$data['study'] = $study;
$data['sorting'] = $study->sortings[0];
$pieces = explode("||", $study->sortings[0]->pivot->details);
$circles = strstr($pieces[0], '|');
$circles = substr($circles, 1);
$data['circles'] = $circles;
$data['tokens'] = $interview->tokens;
for ($i=0; $i < count($data['tokens']); $i++) {
$path = storage_path('app/'.$data['tokens'][$i]['image_path'] );
$data['tokens'][$i]['image_path'] = decrypt(file_get_contents($path));
}
$data['author'] = $author;
return view('sorting.view',$data);
}
public function newInterview(Request $request){
$query ='
select distinct qid, q, d,
GROUP_CONCAT(answer SEPARATOR ",") as answer,
GROUP_CONCAT(answers.id SEPARATOR ",") as answerids
from (
Select distinct questions.id as qid, questions.question as q, questions.detail as d
from questions
where `study_id` = '.$request->input('study').'
) as qtable
left join `answers` on qid = `answers`.`question_id`
group by qid,q,d';
$questions = DB::select(DB::raw($query));
$returnquestions['presort'] = array();
$returnquestions['postsort'] = array();
foreach ($questions as $q) {
$q->answer = json_decode("[".$q->answer."]");
$q->answer['ids'] = explode(',', $q->answerids);
if($q->d == 'presort') array_push($returnquestions['presort'],$q);
if($q->d == 'postsort') array_push($returnquestions['postsort'],$q);
}
$data['questions'] = $returnquestions;
$data['study'] = Study::where('id','=',$request->input('study'))->with('sortings')->first();
$data['interviewed'] = htmlspecialchars($request->input('interviewed') == "" ? "Name not provided" : $request->input('interviewed'));
$data['sorting'] = $data['study']->sorting[0];
$data['tokens'] = $data['study']->available_tokens;
for ($i=0; $i < count($data['tokens']); $i++) {
$path = storage_path('app/'.$data['tokens'][$i]['image_path'] );
$data['tokens'][$i]['image_path'] = decrypt(file_get_contents($path));
}
$data['gotos'] = $request->input('gotos') ? 'true' : 'false';
return view('interview.create',$data);
}
public function extension($uri){
$img = explode(',', $uri);
$ini =substr($img[0], 11);
$type = explode(';', $ini);
return $type[0];
}
public function saveinterview(Request $request){
$interview = new Interview;
$study = Study::where('id','=',$request->input('study'))->first();
$interview->study_id = $study->id;
$interview->author = Auth::user()->id;
$interview->interviewed = $request->input('interviewed');
// save screenshot
$image = $request->input('sortingscreenshot');
$name = "interview_".$request->input('time_start');
$arr = explode(",", $request->input('sortingscreenshot'), 2);
$base64firstpart = $arr[0];
$extension = $this->extension($request->input('sortingscreenshot'));
$path = storage_path('app/screenshots/'.$name.".".$extension);
// open file a image resource
\Image::make($image)->save($path);
$interview->sorting_screenshot = $path;
$interview->start = $request->input('time_start');
$interview->end = $request->input('time_end');
$interview->save();
// -- SAVE TOKENS
foreach ($request->input('tokens') as $t) {
$t['valutation']['position'] = $t['position'];
$t['valutation']['percentagePosition'] = $t['percentagePosition'];
$interview->tokens()->attach($t['id'],array('interview_id' => $interview->id,'sorting_id' => 1, 'valutation' => json_encode($t['valutation'])));
}
foreach ($request->input('results_questions') as $key => $value) {
if(is_numeric($key)){
$answer = Answer::where('id','=',$key)->first();
$question = Question::where('id','=',$answer->question_id)->first();
$answertype = $answer->answer->type;
if($answertype == "open" || $answertype == "scale"){
$interview->answers()->attach($key, array('result' => $value,'question_id' => $question->id));
}
}
if($key == "multi"){
$multi = [];
foreach ($value as $m) {
$answer = Answer::where('id','=',$m['id'])->first();
$question = Question::where('id','=',$answer->question_id)->first();
$interview->answers()->attach( $m['id'],['question_id'=>$question->id]);
}
}
if($key == "onechoice"){
$onechoice = [];
foreach ($value as $one) {
$answer = Answer::where('id','=',$one['id'])->first();
$question = Question::where('id','=',$answer->question_id)->first();
$interview->answers()->attach( $one['id'],['question_id'=>$question->id]);
}
}
}
return response()->json('Interview Saved!', 200);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Storage;
use File;
use App\Study;
use App\Sorting;
use App\Token;
use App\Answer;
use App\Question;
class StudyController extends Controller
{
public function extension($uri){
$img = explode(',', $uri);
$ini =substr($img[0], 11);
$type = explode(';', $ini);
return $type[0];
}
public function newStudy(){
$data['study'] = collect();
$data['isedit'] = 'false';
return view('study.create_edit',$data);
}
public function createStudy(Request $request){
if($request->name == "") return response()->json('Data are not valid', 422);
$newstudy = New Study;
$newstudy->description = $request->research_question;
$newstudy->name = $request->name;
$newstudy->author = $request->author;
$newstudy->user_id = Auth::user()->id;
$newstudy->save();
$newstudy->sortings()->detach();
$description = $request->input('sorting.description');
// insert in details something according to the sorting
$newstudy->sortings()->attach($request->get('sorting')['id'],array('details' => 'circles|'.$request->input('sorting.numberofcircles').'||'.'description|'.$description));
Auth::user()->studies()->attach($newstudy->id, array('permission_id' => 4));
foreach ($request->get('sorting')['tokens'] as $t) {
if($t['file'])
{
$image = $t['file'];
$extension = $this->extension($image);
$name = $t['name'];
$arr = explode(",", $t['file'], 2);
$base64firstpart = $arr[0];
// open file a image resource
\Image::make($image)->fit(100, 100)->save(storage_path('app/tokens/'.$name));
$path = storage_path('app/tokens/'.$name);
$ext = pathinfo($path, PATHINFO_EXTENSION);
$encryptedContent = encrypt($base64firstpart.",".base64_encode(file_get_contents($path)));
// Store the encrypted Content
Storage::put('tokens/'.$name.'.mtoken', $encryptedContent);
File::delete($path);
$token = new Token;
$token->name = $t['name'];
$token->image_path = 'tokens/'.$name.'.mtoken';
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($newstudy->id);
}else{
// BASE64 STRING FOR LOGO
$placeholder = config('utilities.base64logo');
$extension = $this->extension($placeholder);
$name = $t['name'];
$arr = explode(",", $placeholder, 2);
$base64firstpart = $arr[0];
// open file a image resource
\Image::make($placeholder)->fit(100, 100)->save(storage_path('app/tokens/'.$name));
$path = storage_path('app/tokens/'.$name);
$ext = pathinfo($path, PATHINFO_EXTENSION);
$encryptedContent = encrypt($base64firstpart.",".base64_encode(file_get_contents($path)));
// Store the encrypted Content
Storage::put('tokens/'.$name.'.mtoken', $encryptedContent);
File::delete($path);
$token = new Token;
$token->name = $t['name'];
$token->image_path = 'tokens/'.$name.'.mtoken';
$token->author = Auth::user()->id;
$token->save();
$token->studies()->sync($newstudy->id);
}
}
foreach ($request->presort["questions"] as $q) {
$question = new Question;
$question->question = $q['question'];
$question->detail = 'presort';
$question->study_id = $newstudy->id;
$question->save();
if($q['ismultiple']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "multi",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isonechoice']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "onechoice",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isopen']){
$answer = new Answer;
$ajson = json_encode(array('type' => "open",'answer' => ""));
$answer->question_id = $question->id;
$answer->answer = $ajson;
$answer->save();
}
if($q['isscale']){
$answer = new Answer;
$ajson = json_encode(array('type' => "scale",'answer' => array('min' => $q['scalemin'], 'max' => $q['scalemax'])));
$answer->question_id = $question->id;
$answer->answer = $ajson;
$answer->save();
}
}
foreach ($request->postsort["questions"] as $q) {
$question = new Question;
$question->question = $q['question'];
$question->detail = 'postsort';
$question->study_id = $newstudy->id;
$question->save();
if($q['ismultiple']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "multi",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isonechoice']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "onechoice",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isopen']){
$answer = new Answer;
$ajson = json_encode(array('type' => "open",'answer' => ""));
$answer->question_id = $question->id;
$answer->answer = $ajson;
$answer->save();
}
if($q['isscale']){
$answer = new Answer;
$ajson = json_encode(array('type' => "scale",'answer' => array('min' => $q['scalemin'], 'max' => $q['scalemax'])));
$answer->question_id = $question->id;
$answer->answer = $ajson;
$answer->save();
}
}
return response()->json(['message'=>'Study Saved!','studyid'=>$newstudy->id], 200);
}
/**
* Return the data necessary to edit the study
* @param Request $request [description]
* @return [type] [description]
*/
public function editstudy(Request $request){
// check permissions to edit study
// get the sorting and the tokens
$study = Study::where('id','=',$request->input('study'))->with('questions','sortings')->first();
if(!$study->iseditable() || !$study->allowedtointerview()) return back()->withInput();
$study->tokens = $study->available_tokens;
$qids = [];
$questions['presort'] = array();
$questions['postsort'] = array();
foreach ($study->questions as $q) {
if($q->detail == 'presort'){
$q['answers'] = Answer::where('question_id',$q->id)->get()->toArray();
array_push($questions['presort'],$q);
}elseif($q->detail == 'postsort'){
$q['answers'] = Answer::where('question_id',$q->id)->get()->toArray();
array_push($questions['postsort'],$q);
}
array_push($qids, $q->id);
}
$questions['presort']['count'] = count($questions['presort']);
$questions['postsort']['count'] = count($questions['postsort']);
$data['study'] = $study;
$data['study']['tokens'] = $study->available_tokens;
for ($i=0; $i < count($data['study']['tokens']); $i++) {
$path = storage_path('app/'.$data['study']['tokens'][$i]['image_path'] );
$data['study']['tokens'][$i]['image_path'] = decrypt(file_get_contents($path));
}
$data['study']['q'] = $questions;
$data['isedit'] = 'true';
return view('study.create_edit',$data);
}
public function updatestudy(Request $request){
if($request->name == "") return response()->json('Data are not valid', 422);
$study = Study::where('id','=',$request->input('id'))->with('questions')->first();
if(!$study->iseditable() || !$study->allowedtointerview()) return back()->withInput();
// sorting
$description = $request->input('sorting.description');
$study->sortings()->detach();
// insert in details something according to the sorting
$study->sortings()->attach($request->get('sorting')['id'],array('details' => 'circles|'.$request->input('sorting.numberofcircles').'||'.'description|'.$description));
Auth::user()->studies()->attach($study->id, array('permission_id' => 4));
$study->description = $request->research_question;
$study->name = $request->name;
$study->author = $request->author;
$study->user_id = Auth::user()->id;
$study->save();
$study->sortings()->sync($request->get('sorting')['id']);
$allstudies = Auth::user()->studies()->with('permissions')->get();
foreach ($request->get('sorting')['tokens'] as $t) {
/// HANDLE THE UPDATE TOKENS
$token = Token::where('id','=',$t['id'])->first();
$token->author = Auth::user()->id;
$token->name = $t['name'];
if(array_key_exists('file',$t)){
$token = Token::where('id','=',$t['id'])->first();
$token->author = Auth::user()->id;
//delete previous token
$path = storage_path('app/'.$token->image_path);
File::delete($path);
$image = $t['file'];
$name = $t['name'];
$arr = explode(",", $t['file'], 2);
$base64firstpart = $arr[0];
// open file a image resource
\Image::make($image)->fit(100, 100)->save(storage_path('app/'.$study->id.'/tokens/'.$name));
$path = storage_path('app/tokens/'.$name);
$encryptedContent = encrypt($base64firstpart.",".base64_encode(file_get_contents($path)));
// Store the encrypted Content
Storage::put($study->id.'/tokens/'.$name.'.mtoken', $encryptedContent);
File::delete($path);
$token->image_path = 'tokens/'.$name.'.mtoken';
}
$token->save();
}
foreach ($request->presort["questions"] as $q) {
if($q['id'] != "") {
Answer::where('question_id','=',$q['id'])->delete();
Question::where('id','=',$q['id'])->delete();
}
$question = new Question;
$question->question = $q['question'];
$question->detail = 'presort';
$question->study_id = $study->id;
$question->save();
if($q['ismultiple']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "multi",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isonechoice']){
foreach ($q['answers'] as $a) {
$answer = new Answer;
$answer->question_id = $question->id;
$ajson = json_encode(array('type' => "onechoice",'answer' => $a));
$answer->answer = $ajson;
$answer->save();
}
}
if($q['isopen']){
$answer = new Answer;