256 lines
8.0 KiB
PHP
Executable File
256 lines
8.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Admins;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Master\ManagementMasters;
|
|
use App\Models\QuestionAnswers;
|
|
use App\Models\QuestionDetails;
|
|
use App\Models\QuestionSurveys;
|
|
use App\Models\Surveys;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
use Illuminate\Support\Facades\Session;
|
|
|
|
class SurveyController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
// call data
|
|
$surveys = Surveys::paginate(10);
|
|
|
|
// return view
|
|
return view('pages.admins.surveys.index', compact('surveys'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
$survey = Surveys::create([
|
|
'name' => 'Survey Tanpa Nama',
|
|
'status' => 'Published'
|
|
]);
|
|
|
|
QuestionSurveys::create([
|
|
'survey_id' => $survey->id,
|
|
'name' => 'Pertanyaan anda?',
|
|
'type' => 'Iya atau Tidak',
|
|
]);
|
|
|
|
// redirect
|
|
return Redirect::to('admin/surveys/' . $survey->id . '/edit');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
return [
|
|
'semua' => $request->all(),
|
|
'test' => $request->type[1] . " " . $request->valuefromtype[1]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function preview(string $id)
|
|
{
|
|
// find data
|
|
$survey = Surveys::where('status', '!=', 'Deleted')
|
|
->with(['questionSurveys' => fn ($query) => $query->where('status', '!=', 'Deleted')])
|
|
->findOrFail($id);
|
|
// return ($survey);
|
|
|
|
// return view
|
|
return view('pages.admins.surveys.view', compact('survey'));
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(string $id)
|
|
{
|
|
// find data
|
|
$survey = Surveys::where('status', '!=', 'Deleted')->with('questionSurveys')->findOrFail($id);
|
|
// return $survey->questionSurveys;
|
|
$questionAnswers = QuestionAnswers::where('survey_id', $id)->with('questionSurveys')->get();
|
|
// return $questionSurveys = QuestionSurveys::where('survey_id', $id)->with('questionAnswers')->get();
|
|
// return [$survey->questionSurveys->first()->questionAnswers->first()->users->name];
|
|
|
|
// return view
|
|
return view('pages.admins.surveys.detail', compact('survey', 'questionAnswers'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
// find data
|
|
$survey = Surveys::where('status', '!=', 'Deleted')->findOrFail($id);
|
|
$management_master = ManagementMasters::get();
|
|
|
|
// return view
|
|
return view('pages.admins.surveys.edit', compact('survey', 'management_master'));
|
|
}
|
|
|
|
// SaveSurvey (Autosaved)
|
|
public function saveSurvey(Request $request, string $id)
|
|
{
|
|
$survey = Surveys::findOrFail($id);
|
|
$survey->update($request->all());
|
|
return $survey;
|
|
}
|
|
|
|
// Add Data Question (Autosaved)
|
|
public function addQuestion(string $id)
|
|
{
|
|
$questionSurvey = QuestionSurveys::create([
|
|
'survey_id' => $id,
|
|
'name' => 'Pertanyaan anda?',
|
|
'type' => 'Iya atau Tidak',
|
|
]);
|
|
|
|
QuestionDetails::create([
|
|
'question_id' => $questionSurvey->id,
|
|
'question_label' => '["Iya", "Tidak"]',
|
|
'question_value' => '[1, 0]',
|
|
]);
|
|
|
|
// return data
|
|
return $questionSurvey;
|
|
}
|
|
|
|
// Save Question (Autosaved)
|
|
public function saveQuestion(Request $request, string $id)
|
|
{
|
|
$questionSurveys = QuestionSurveys::findOrFail($id);
|
|
foreach ($request->questionSurvey as $key => $value) {
|
|
if ($value['type'] == null) $value['type'] = 'Iya atau Tidak';
|
|
$questionSurveys->update([
|
|
'name' => $value['question'],
|
|
'type' => $value['type']
|
|
]);
|
|
|
|
if ($value['type'] == 'Skala') {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["' . $value['labelfrom'] . '","' . $value['labelto'] . '"]',
|
|
'question_value' => '["' . $value['valuefrom'] . '","' . $value['valueto'] . '"]',
|
|
]);
|
|
} else if ($value['type'] == 'Jawaban Angka') {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["Jawaban Angka"]',
|
|
'question_value' => '["Angka"]',
|
|
]);
|
|
} else if ($value['type'] == 'Jawaban Singkat') {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["Jawaban Singkat"]',
|
|
'question_value' => '["Teks Jawaban Singkat"]',
|
|
]);
|
|
} else if ($value['type'] == 'Esai') {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["Esai"]',
|
|
'question_value' => '["Teks"]',
|
|
]);
|
|
} else if ($value['type'] == 'Master') {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["Master"]',
|
|
'question_value' => '["' . $value['master'] . '"]',
|
|
]);
|
|
} else {
|
|
QuestionDetails::updateOrCreate([
|
|
'question_id' => $questionSurveys->id,
|
|
], [
|
|
'question_label' => '["Iya", "Tidak"]',
|
|
'question_value' => '[1, 0]',
|
|
]);
|
|
}
|
|
}
|
|
return $questionSurveys;
|
|
}
|
|
|
|
// Delete Question
|
|
public function deleteQuestion(string $id)
|
|
{
|
|
// Find Question
|
|
$question = QuestionSurveys::findOrFail($id);
|
|
|
|
if ($question->questionAnswers) {
|
|
$question->update([
|
|
'status' => 'Deleted'
|
|
]);
|
|
|
|
return [
|
|
'msg_status' => "Success Deleted (s)",
|
|
'question' => $question,
|
|
];
|
|
} else {
|
|
$question = $question->delete();
|
|
return [
|
|
'msg_status' => "Success Deleted",
|
|
'question' => $question,
|
|
];
|
|
}
|
|
}
|
|
|
|
// Add Data Answers
|
|
public function addAnswers(Request $request)
|
|
{
|
|
// create unique answer_id
|
|
$unique_answer_id = QuestionAnswers::select('answer_id')->groupby('answer_id')->latest()->first();
|
|
$unique_answer_id = empty($unique_answer_id) ? 1 : $unique_answer_id['answer_id'] + 1;
|
|
|
|
// validation
|
|
$request->validate([
|
|
'question.*.answer' => 'required',
|
|
'survey_id' => 'required',
|
|
]);
|
|
|
|
// return $request;
|
|
foreach ($request->question as $key => $value) {
|
|
$answer_type = QuestionSurveys::select('type')->find($key);
|
|
QuestionAnswers::create([
|
|
'user_id' => Auth::user()->id,
|
|
'question_id' => $key,
|
|
'answer_id' => $unique_answer_id,
|
|
'answer_type' => $answer_type ? $answer_type['type'] : null,
|
|
'answer' => $value['answer'],
|
|
'survey_id' => $request->survey_id,
|
|
]);
|
|
}
|
|
|
|
// Create Session message
|
|
Session::flash('surveys-message', [
|
|
'type' => 'success',
|
|
'msg' => 'Anda berhasil mengirimkan jawaban survey!'
|
|
]);
|
|
return Redirect::to('admin/surveys');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|