Skip to content

Instantly share code, notes, and snippets.

@PetengDedet PetengDedet/snip
Created Nov 24, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.