2024-07-15 06:24:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
2024-07-24 05:41:55 +00:00
|
|
|
|
2024-07-15 18:01:10 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2024-07-15 06:24:05 +00:00
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use App\Models\Form;
|
|
|
|
use App\Models\Response;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use App\Models\Question;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
|
|
class ResponseController extends Controller
|
|
|
|
{
|
|
|
|
public function index(Form $form)
|
|
|
|
{
|
|
|
|
$responses = Response::where('form_id', $form->id)
|
|
|
|
->with('user')
|
|
|
|
->orderBy('submitted_at', 'desc')
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return view('responses.index', compact('form', 'responses'));
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:59:23 +00:00
|
|
|
public function showSuccess(Form $form)
|
2024-07-24 05:41:55 +00:00
|
|
|
{
|
|
|
|
return view('responses.success', compact('form'));
|
|
|
|
}
|
|
|
|
|
2024-07-23 19:59:23 +00:00
|
|
|
|
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
public function viewResponse(Form $form, $responseId)
|
2024-07-24 05:41:55 +00:00
|
|
|
{
|
|
|
|
$responses = Response::where('response_id', $responseId)
|
|
|
|
->where('form_id', $form->id)
|
|
|
|
->get();
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
if ($responses->isEmpty()) {
|
|
|
|
abort(404, 'Response not found');
|
|
|
|
}
|
2024-07-18 11:43:20 +00:00
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
$formSnapshot = json_decode($responses->first()->form_snapshot, true);
|
2024-07-24 05:41:55 +00:00
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
if (is_null($formSnapshot) || !isset($formSnapshot['questions'])) {
|
|
|
|
Log::error('Form snapshot is null or does not contain questions', [
|
|
|
|
'response_id' => $responseId,
|
|
|
|
'form_snapshot' => $responses->first()->form_snapshot
|
|
|
|
]);
|
|
|
|
abort(500, 'Form snapshot is invalid');
|
2024-07-18 11:43:20 +00:00
|
|
|
}
|
2024-07-24 05:41:55 +00:00
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
$questions = collect($formSnapshot['questions'])->keyBy('id');
|
|
|
|
|
|
|
|
return view('responses.viewResponse', compact('form', 'responses', 'questions'));
|
2024-07-18 11:43:20 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
|
|
|
|
public function viewResponses(Form $form)
|
|
|
|
{
|
|
|
|
|
|
|
|
$responses = Response::where('form_id', $form->id)
|
|
|
|
->orderBy('submitted_at', 'desc')
|
|
|
|
->get()
|
|
|
|
->groupBy('response_id');
|
|
|
|
|
|
|
|
|
|
|
|
$questions = Question::where('form_id', $form->id)->get()->keyBy('id');
|
|
|
|
|
|
|
|
|
|
|
|
$statistics = [];
|
|
|
|
foreach ($questions as $question) {
|
|
|
|
$statistics[$question->id] = [
|
|
|
|
'question_text' => $question->question_text,
|
|
|
|
'type' => $question->type,
|
|
|
|
'options' => json_decode($question->options, true),
|
|
|
|
'responses' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($responses as $responseGroup) {
|
|
|
|
foreach ($responseGroup as $response) {
|
|
|
|
$decodedAnswers = json_decode($response->answers, true);
|
|
|
|
if (isset($decodedAnswers[$question->id])) {
|
|
|
|
$statistics[$question->id]['responses'][] = $decodedAnswers[$question->id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('responses.viewResponses', compact('form', 'responses', 'statistics', 'questions'));
|
|
|
|
}
|
2024-07-15 18:01:10 +00:00
|
|
|
|
2024-07-18 11:43:20 +00:00
|
|
|
|
2024-07-15 06:24:05 +00:00
|
|
|
public function showForm(Form $form)
|
|
|
|
{
|
|
|
|
$questions = $form->questions;
|
2024-07-16 11:44:26 +00:00
|
|
|
if (!$form->is_published) {
|
|
|
|
return redirect('/forms')->with('delete', 'This form is not published.');
|
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
|
|
|
|
return view('responses.showForm', compact('form', 'questions'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function submitForm(Request $request, Form $form)
|
2024-07-24 05:41:55 +00:00
|
|
|
{
|
2024-07-24 19:41:19 +00:00
|
|
|
Log::info('Form submission started', $request->all());
|
2024-07-15 06:24:05 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
$questions = $form->questions;
|
|
|
|
|
|
|
|
$requiredQuestionIds = $questions->where('required', true)->pluck('id')->toArray();
|
2024-07-18 21:12:07 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
$validatedData = $request->validate([
|
2024-07-24 11:54:47 +00:00
|
|
|
'answers' => 'array',
|
|
|
|
'answers.*' => '',
|
2024-07-24 05:41:55 +00:00
|
|
|
]);
|
2024-07-18 21:12:07 +00:00
|
|
|
|
2024-07-24 05:41:55 +00:00
|
|
|
foreach ($requiredQuestionIds as $requiredQuestionId) {
|
2024-07-24 11:54:47 +00:00
|
|
|
if (!isset($validatedData['answers'][$requiredQuestionId]) || empty($validatedData['answers'][$requiredQuestionId])) {
|
2024-07-24 19:41:19 +00:00
|
|
|
return response()->json(['success' => false, 'message' => 'Please answer all required questions.']);
|
2024-07-24 05:41:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
Log::info('Validation passed', $validatedData);
|
2024-07-24 05:41:55 +00:00
|
|
|
|
|
|
|
$responseId = Uuid::uuid4()->toString();
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
$formSnapshot = [
|
|
|
|
'title' => $form->title,
|
|
|
|
'description' => $form->description,
|
|
|
|
'questions' => $questions->map(function ($question) {
|
|
|
|
return [
|
|
|
|
'id' => $question->id,
|
|
|
|
'question_text' => $question->question_text,
|
|
|
|
'type' => $question->type,
|
|
|
|
'options' => $question->options,
|
|
|
|
];
|
|
|
|
})->toArray(),
|
|
|
|
];
|
2024-07-24 05:41:55 +00:00
|
|
|
|
|
|
|
foreach ($validatedData['answers'] as $questionId => $answer) {
|
|
|
|
$response = new Response();
|
|
|
|
$response->response_id = $responseId;
|
|
|
|
$response->question_id = $questionId;
|
|
|
|
$response->form_id = $form->id;
|
|
|
|
$response->user_id = auth()->id();
|
|
|
|
$response->answers = json_encode($answer);
|
|
|
|
$response->submitted_at = now();
|
2024-07-24 19:41:19 +00:00
|
|
|
$response->form_snapshot = json_encode($formSnapshot);
|
2024-07-24 05:41:55 +00:00
|
|
|
$response->save();
|
2024-07-24 19:41:19 +00:00
|
|
|
|
|
|
|
Log::info('Response saved', $response->toArray());
|
2024-07-24 05:41:55 +00:00
|
|
|
}
|
|
|
|
|
2024-07-24 19:41:19 +00:00
|
|
|
return response()->json(['success' => true, 'message' => 'Response submitted successfully.']);
|
2024-07-24 05:41:55 +00:00
|
|
|
}
|
2024-07-15 06:24:05 +00:00
|
|
|
}
|