Compare commits
1 Commits
2ce4691231
...
d391440e2a
Author | SHA1 | Date |
---|---|---|
yash | d391440e2a |
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
use Illuminate\Support\Facades\DB;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use App\Models\Form;
|
use App\Models\Form;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
@ -104,6 +104,7 @@ Contact us at (123) 456-7890 or no_reply@example.com
|
||||||
|
|
||||||
public function store(Request $request)
|
public function store(Request $request)
|
||||||
{
|
{
|
||||||
|
try {
|
||||||
$validatedData = $request->validate([
|
$validatedData = $request->validate([
|
||||||
'title' => 'required|string|max:255',
|
'title' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string',
|
'description' => 'nullable|string',
|
||||||
|
@ -111,35 +112,33 @@ Contact us at (123) 456-7890 or no_reply@example.com
|
||||||
'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text',
|
'questions.*.type' => 'required|string|in:multiple_choice,checkbox,dropdown,text',
|
||||||
'questions.*.text' => 'required|string',
|
'questions.*.text' => 'required|string',
|
||||||
'questions.*.options' => 'nullable|array',
|
'questions.*.options' => 'nullable|array',
|
||||||
'questions.*.required' => 'boolean',
|
'questions.*.required' => 'nullable|boolean',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
DB::beginTransaction();
|
|
||||||
|
|
||||||
try {
|
$form = new Form();
|
||||||
$form = Form::create([
|
$form->title = $validatedData['title'];
|
||||||
'title' => $validatedData['title'],
|
$form->description = $validatedData['description'];
|
||||||
'description' => $validatedData['description'],
|
$form->is_published = $request->input('is_published', false);
|
||||||
'user_id' => Auth::id(),
|
$form->user_id = Auth::id();
|
||||||
]);
|
$form->save();
|
||||||
|
|
||||||
foreach ($validatedData['questions'] as $questionData) {
|
foreach ($validatedData['questions'] as $questionData) {
|
||||||
$question = new Question([
|
$question = new Question();
|
||||||
'form_id' => $form->id,
|
$question->form_id = $form->id;
|
||||||
'type' => $questionData['type'],
|
$question->type = $questionData['type'];
|
||||||
'question_text' => $questionData['text'],
|
$question->question_text = $questionData['text'];
|
||||||
'options' => json_encode($questionData['options'] ?? []),
|
$question->options = isset($questionData['options']) ? json_encode($questionData['options']) : null;
|
||||||
'required' => $questionData['required'],
|
$question->required = isset($questionData['required']) ? $questionData['required'] : false;
|
||||||
]);
|
|
||||||
$question->save();
|
$question->save();
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::commit();
|
|
||||||
return response()->json(['success' => true, 'message' => 'Form saved successfully.']);
|
|
||||||
|
return response()->json(['success' => true, 'form_id' => $form->id]);
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
DB::rollBack();
|
Log::error('Error saving form: ' . $e->getMessage(), ['exception' => $e]);
|
||||||
Log::error('Error saving form: ' . $e->getMessage());
|
return response()->json(['success' => false, 'message' => 'Error saving form'], 500);
|
||||||
return response()->json(['success' => false, 'message' => 'Failed to save form.']);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,21 +157,14 @@ Contact us at (123) 456-7890 or no_reply@example.com
|
||||||
|
|
||||||
public function update(Request $request, Form $form)
|
public function update(Request $request, Form $form)
|
||||||
{
|
{
|
||||||
try {
|
if ($request->has('publish')) {
|
||||||
// Normalize the 'required' field to boolean
|
$form->is_published = !$form->is_published;
|
||||||
if ($request->has('questions')) {
|
$form->save();
|
||||||
$questions = $request->input('questions');
|
|
||||||
foreach ($questions as $index => $question) {
|
return redirect()->route('forms.show', $form);
|
||||||
if (isset($question['required']) && $question['required'] === 'on') {
|
}
|
||||||
$questions[$index]['required'] = true;
|
Log::info('Incoming request data: ', $request->all());
|
||||||
} else {
|
|
||||||
$questions[$index]['required'] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$request->merge(['questions' => $questions]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the request
|
|
||||||
$validatedData = $request->validate([
|
$validatedData = $request->validate([
|
||||||
'title' => 'required|string|max:255',
|
'title' => 'required|string|max:255',
|
||||||
'description' => 'nullable|string|max:255',
|
'description' => 'nullable|string|max:255',
|
||||||
|
@ -182,41 +174,53 @@ Contact us at (123) 456-7890 or no_reply@example.com
|
||||||
'questions.*.text' => 'required|string|max:255',
|
'questions.*.text' => 'required|string|max:255',
|
||||||
'questions.*.options' => 'nullable|array',
|
'questions.*.options' => 'nullable|array',
|
||||||
'questions.*.options.*' => 'nullable|string|max:255',
|
'questions.*.options.*' => 'nullable|string|max:255',
|
||||||
'questions.*.required' => 'boolean',
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Update form
|
Log::info('Validated data: ', $validatedData);
|
||||||
|
|
||||||
$form->update([
|
$form->update([
|
||||||
'title' => $validatedData['title'],
|
'title' => $validatedData['title'],
|
||||||
'description' => $validatedData['description'],
|
'description' => $validatedData['description'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Clear existing questions
|
$existingQuestionIds = [];
|
||||||
$form->questions()->delete();
|
|
||||||
|
|
||||||
// Create or update questions
|
|
||||||
foreach ($validatedData['questions'] as $questionData) {
|
foreach ($validatedData['questions'] as $questionData) {
|
||||||
$question = new Question([
|
if (isset($questionData['id'])) {
|
||||||
'form_id' => $form->id,
|
$question = Question::find($questionData['id']);
|
||||||
'type' => $questionData['type'],
|
} else {
|
||||||
'question_text' => $questionData['text'],
|
$question = new Question();
|
||||||
'options' => json_encode($questionData['options'] ?? []),
|
$question->form_id = $form->id;
|
||||||
'required' => $questionData['required'],
|
|
||||||
]);
|
|
||||||
$question->save();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DB::commit();
|
$question->type = $questionData['type'];
|
||||||
|
$question->question_text = $questionData['text'];
|
||||||
|
$question->options = isset($questionData['options']) ? json_encode($questionData['options']) : json_encode([]);
|
||||||
|
$question->save();
|
||||||
|
|
||||||
|
Log::info('Saved question: ', $question->toArray());
|
||||||
|
|
||||||
|
$existingQuestionIds[] = $question->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
$form->questions()->whereNotIn('id', $existingQuestionIds)->delete();
|
||||||
|
|
||||||
|
Log::info('Remaining questions: ', $form->questions()->get()->toArray());
|
||||||
|
|
||||||
return redirect()->route('forms.show', $form)->with('success', 'Form updated successfully.');
|
return redirect()->route('forms.show', $form)->with('success', 'Form updated successfully.');
|
||||||
} catch (\Exception $e) {
|
|
||||||
DB::rollBack();
|
|
||||||
Log::error('Error updating form: ' . $e->getMessage());
|
|
||||||
return back()->withErrors(['error' => 'An error occurred while updating the form. Please try again.'])->withInput();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function destroy(Form $form)
|
public function destroy(Form $form)
|
||||||
{
|
{
|
||||||
// This will also delete all related questions and responses due to foreign key constraints
|
// This will also delete all related questions and responses due to foreign key constraints
|
||||||
|
|
|
@ -31,27 +31,33 @@ class ResponseController extends Controller
|
||||||
|
|
||||||
public function viewResponse(Form $form, $responseId)
|
public function viewResponse(Form $form, $responseId)
|
||||||
{
|
{
|
||||||
|
|
||||||
$responses = Response::where('response_id', $responseId)
|
$responses = Response::where('response_id', $responseId)
|
||||||
->where('form_id', $form->id)
|
->where('form_id', $form->id)
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
if ($responses->isEmpty()) {
|
|
||||||
abort(404, 'Response not found');
|
$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),
|
||||||
|
'responses' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($responses as $response) {
|
||||||
|
$decodedAnswers = json_decode($response->answers, true);
|
||||||
|
if (isset($decodedAnswers[$question->id])) {
|
||||||
|
$statistics[$question->id]['responses'][] = $decodedAnswers[$question->id];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$formSnapshot = json_decode($responses->first()->form_snapshot, true);
|
return view('responses.viewResponse', compact('form', 'responses', 'questions', 'statistics'));
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
|
|
||||||
$questions = collect($formSnapshot['questions'])->keyBy('id');
|
|
||||||
|
|
||||||
return view('responses.viewResponse', compact('form', 'responses', 'questions'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -102,39 +108,34 @@ class ResponseController extends Controller
|
||||||
|
|
||||||
public function submitForm(Request $request, Form $form)
|
public function submitForm(Request $request, Form $form)
|
||||||
{
|
{
|
||||||
Log::info('Form submission started', $request->all());
|
Log::info($request->all());
|
||||||
|
|
||||||
|
|
||||||
$questions = $form->questions;
|
$questions = $form->questions;
|
||||||
|
|
||||||
|
|
||||||
$requiredQuestionIds = $questions->where('required', true)->pluck('id')->toArray();
|
$requiredQuestionIds = $questions->where('required', true)->pluck('id')->toArray();
|
||||||
|
|
||||||
|
|
||||||
$validatedData = $request->validate([
|
$validatedData = $request->validate([
|
||||||
'answers' => 'array',
|
'answers' => 'required|array',
|
||||||
'answers.*' => '',
|
'answers.*' => 'required',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
||||||
foreach ($requiredQuestionIds as $requiredQuestionId) {
|
foreach ($requiredQuestionIds as $requiredQuestionId) {
|
||||||
if (!isset($validatedData['answers'][$requiredQuestionId]) || empty($validatedData['answers'][$requiredQuestionId])) {
|
if (!isset($validatedData['answers'][$requiredQuestionId]) || empty($validatedData['answers'][$requiredQuestionId])) {
|
||||||
return response()->json(['success' => false, 'message' => 'Please answer all required questions.']);
|
return redirect()->back()
|
||||||
|
->withErrors(['errors' => 'Please answer all required questions.'])
|
||||||
|
->withInput();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::info('Validation passed', $validatedData);
|
Log::info($validatedData);
|
||||||
|
|
||||||
|
|
||||||
$responseId = Uuid::uuid4()->toString();
|
$responseId = Uuid::uuid4()->toString();
|
||||||
|
|
||||||
$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(),
|
|
||||||
];
|
|
||||||
|
|
||||||
foreach ($validatedData['answers'] as $questionId => $answer) {
|
foreach ($validatedData['answers'] as $questionId => $answer) {
|
||||||
$response = new Response();
|
$response = new Response();
|
||||||
|
@ -144,12 +145,10 @@ class ResponseController extends Controller
|
||||||
$response->user_id = auth()->id();
|
$response->user_id = auth()->id();
|
||||||
$response->answers = json_encode($answer);
|
$response->answers = json_encode($answer);
|
||||||
$response->submitted_at = now();
|
$response->submitted_at = now();
|
||||||
$response->form_snapshot = json_encode($formSnapshot);
|
|
||||||
$response->save();
|
$response->save();
|
||||||
|
|
||||||
Log::info('Response saved', $response->toArray());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(['success' => true, 'message' => 'Response submitted successfully.']);
|
return redirect()->route('responses.showForm', $form)
|
||||||
|
->with('success', 'Response submitted successfully.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,25 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
class Question extends Model
|
class Question extends Model
|
||||||
{
|
{
|
||||||
use SoftDeletes;
|
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
protected $fillable = ['form_id', 'type', 'question_text', 'options', 'required'];
|
protected $fillable = ['form_id', 'user_id', 'submitted_at', 'answers'];
|
||||||
|
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
|
'answers' => 'array',
|
||||||
'options' => 'array',
|
'options' => 'array',
|
||||||
'required' => 'boolean',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
public function getOptionsAttribute($value)
|
||||||
|
{
|
||||||
|
return json_decode($value, true);
|
||||||
|
}
|
||||||
|
|
||||||
public function form()
|
public function form()
|
||||||
{
|
{
|
||||||
return $this->belongsTo(Form::class);
|
return $this->belongsTo(Form::class);
|
||||||
|
|
|
@ -8,7 +8,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||||
class Response extends Model
|
class Response extends Model
|
||||||
{
|
{
|
||||||
use HasFactory;
|
use HasFactory;
|
||||||
protected $fillable = ['form_id', 'user_id', 'response_id', 'question_id', 'answers', 'submitted_at', 'form_snapshot'];
|
protected $fillable = ['form_id', 'user_id', 'answers'];
|
||||||
|
|
||||||
// Define relationships
|
// Define relationships
|
||||||
public function form()
|
public function form()
|
||||||
|
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
0
database/migrations/2014_10_12_100000_create_password_reset_tokens_table.php
Normal file → Executable file
0
database/migrations/2014_10_12_100000_create_password_resets_table.php
Normal file → Executable file
0
database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php
Normal file → Executable file
|
@ -1,22 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
public function up()
|
|
||||||
{
|
|
||||||
Schema::table('questions', function (Blueprint $table) {
|
|
||||||
$table->softDeletes();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public function down()
|
|
||||||
{
|
|
||||||
Schema::table('questions', function (Blueprint $table) {
|
|
||||||
$table->dropSoftDeletes();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -1,30 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
Schema::table('responses', function (Blueprint $table) {
|
|
||||||
$table->json('form_snapshot')->nullable();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::table('responses', function (Blueprint $table) {
|
|
||||||
if (Schema::hasColumn('responses', 'form_snapshot')) {
|
|
||||||
$table->dropColumn('form_snapshot');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -255,7 +255,3 @@ input:focus, textarea:focus, select:focus{
|
||||||
color: black;
|
color: black;
|
||||||
height: 20px;
|
height: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.active-question {
|
|
||||||
border-left: 4px solid blue;
|
|
||||||
}
|
|
||||||
|
|
Before Width: | Height: | Size: 959 B After Width: | Height: | Size: 959 B |
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 1.1 KiB |
Before Width: | Height: | Size: 5.9 KiB After Width: | Height: | Size: 5.9 KiB |
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 6.7 KiB After Width: | Height: | Size: 6.7 KiB |
Before Width: | Height: | Size: 6.3 KiB After Width: | Height: | Size: 6.3 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 623 B After Width: | Height: | Size: 623 B |
Before Width: | Height: | Size: 8.5 KiB After Width: | Height: | Size: 8.5 KiB |
Before Width: | Height: | Size: 19 KiB After Width: | Height: | Size: 19 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
@ -6,7 +6,7 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
const optionDiv = document.createElement("div");
|
const optionDiv = document.createElement("div");
|
||||||
optionDiv.className = "option";
|
optionDiv.className = "option";
|
||||||
optionDiv.innerHTML = `
|
optionDiv.innerHTML = `
|
||||||
<input type="text" class="form-control option-input mb-1" placeholder="New Option" />
|
<input type="text" class="form-control option-input" placeholder="New Option" />
|
||||||
<span class="delete-option" onclick="deleteOption(this)">✕</span>
|
<span class="delete-option" onclick="deleteOption(this)">✕</span>
|
||||||
`;
|
`;
|
||||||
optionContainer.appendChild(optionDiv);
|
optionContainer.appendChild(optionDiv);
|
||||||
|
|