-
-
Save PetengDedet/f0c179ed207f6814ebfd17c51fbb9c6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function surveyPlayStore($id, Request $request) | |
{ | |
$userdata = Cektoken::cek($request->token); | |
$data = []; | |
$survey = \App\Models\Survey::find($id); | |
if (!$survey OR $survey->is_active == 'n') { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Survey tidak valid'], 404); | |
} | |
//Check if the member already take this survey | |
if (\App\Models\SurveyResult::where('survey_id', $survey->id)->where('user_id', $userdata['data']['id'])->count() > 0) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Anda sudah pernah berpartisipasi dalam survey ini. Terimakasih.'], 404); | |
} | |
if(!is_array($request->question_id)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400); | |
} | |
foreach ($request->question_id as $k => $v) { | |
$question = \App\Models\SurveyQuestion::find($k); | |
if (!$question){ | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, pertanyaan dan jawaban yang anda kirim tidak valid. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400); | |
} | |
$answer = $v; | |
switch ($question->type) { | |
case 'pg': | |
$validAnswer = [$question->pil_a, $question->pil_b, $question->pil_c, $question->pil_d, $question->pil_e, $question->pil_f]; | |
if (! in_array($v, $validAnswer)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400); | |
} | |
$answer = $v; | |
$result = new \App\Models\SurveyResult(); | |
$result->survey_id = $survey->id; | |
$result->question_id = $question->id; | |
$result->user_id = $userdata['data']['id']; | |
$result->answer = $answer; | |
$result->save(); | |
break; | |
case 'multi': | |
if (!is_array($v)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. INI PERTANYAAN MULTI. Harap kirim response dalam bentuk array MULTIDIMENSIONAL untuk masing-masing question. Ctoh: question_id[id][0] => "Jawaban ke 0" '], 400); | |
} | |
foreach ($v as $key => $value) { | |
$validAnswer = [$question->pil_a, $question->pil_b, $question->pil_c, $question->pil_d, $question->pil_e, $question->pil_f]; | |
if (! in_array($value, $validAnswer)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'INI PERTANYAAN MULTI. Harap kirim response dalam bentuk array MULTIDIMENSIONAL untuk masing-masing question. Ctoh: question_id[id][0] => "Jawaban ke 0"'], 400); | |
} | |
$result = new \App\Models\SurveyResult(); | |
$result->survey_id = $survey->id; | |
$result->question_id = $question->id; | |
$result->user_id = $userdata['data']['id']; | |
$result->answer = $value; | |
$result->save(); | |
} | |
// $answer = $v; | |
break; | |
case 'scale': | |
$validAnswer = ['1', '2', '3', '4', '5']; | |
if (! in_array($v, $validAnswer)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, Anda mengirimkan jawaban yang tidak valid untuk pertanyaan ' . $k . '. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400); | |
} | |
$answer = $v; | |
$result = new \App\Models\SurveyResult(); | |
$result->survey_id = $survey->id; | |
$result->question_id = $question->id; | |
$result->user_id = $userdata['data']['id']; | |
$result->answer = $answer; | |
$result->save(); | |
break; | |
case 'essay': | |
$answer = $v; | |
$result = new \App\Models\SurveyResult(); | |
$result->survey_id = $survey->id; | |
$result->question_id = $question->id; | |
$result->user_id = $userdata['data']['id']; | |
$result->answer = $answer; | |
$result->save(); | |
break; | |
default: | |
if (! in_array($v, $validAnswer)) { | |
return response()->json(['status' => false, 'data' => $data, 'message' => 'Ups, pertanyaan dan jawaban yang anda kirim tidak valid. Harap kirim response dalam bentuk array untuk masing-masing question [\'question_id\' => \'answer\']'], 400); | |
} | |
break; | |
} | |
} | |
$data['survey']['id'] = $survey->id; | |
$data['survey']['title'] = $survey->title; | |
$data['survey']['description'] = $survey->description; | |
$data['survey']['timestamp'] = $survey->created_at; | |
$surveyQuestion = \App\Models\SurveyQuestion::where('survey_id', $survey->id)->get(); | |
$data['survey']['question_count'] = $surveyQuestion->count(); | |
return response()->json(['status' => true, 'data' => $data, 'message' => 'Berhasil menyimpan survey. Terimakasih.'], 200); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment