diff --git a/app/Http/Controllers/FormController.php b/app/Http/Controllers/FormController.php index 13743e3..7c5028d 100644 --- a/app/Http/Controllers/FormController.php +++ b/app/Http/Controllers/FormController.php @@ -10,14 +10,23 @@ use App\Models\Response; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Session; +use Illuminate\Validation\Rules\Unique; class FormController extends Controller { public function index() { - // Get forms belonging to the authenticated user + $totalForms = Form::count(); + $publishedForms = Form::where('is_published', true)->count(); + $totalResponses = Response::count(); + $forms = Form::where('user_id', Auth::id())->get(); - return view('forms.index', compact('forms')); + return view('forms.index', [ + 'forms' => $forms, + 'totalForms' => $totalForms, + 'publishedForms' => $publishedForms, + 'totalResponses' => $totalResponses, + ]); } public function create() @@ -36,13 +45,11 @@ class FormController extends Controller public function edit(Form $form) { - // Questions are already fetched with their options cast to array due to the casts property $questions = $form->questions; foreach ($questions as $question) { $question->options = json_decode($question->options, true); } - // Pass the questions to the view return view('forms.edit', compact('form', 'questions')); } @@ -61,6 +68,7 @@ class FormController extends Controller 'questions.*.required' => 'nullable|boolean', ]); + $form = new Form(); $form->title = $validatedData['title']; $form->description = $validatedData['description']; @@ -78,6 +86,8 @@ class FormController extends Controller $question->save(); } + + return response()->json(['success' => true, 'form_id' => $form->id]); } catch (\Exception $e) { Log::error('Error saving form: ' . $e->getMessage(), ['exception' => $e]); @@ -145,7 +155,6 @@ class FormController extends Controller $existingQuestionIds[] = $question->id; } - // Delete questions that were removed $form->questions()->whereNotIn('id', $existingQuestionIds)->delete(); Log::info('Remaining questions: ', $form->questions()->get()->toArray()); diff --git a/app/Http/Controllers/ResponseController.php b/app/Http/Controllers/ResponseController.php index c70c197..94c5fe2 100644 --- a/app/Http/Controllers/ResponseController.php +++ b/app/Http/Controllers/ResponseController.php @@ -85,7 +85,7 @@ public function viewResponses(Form $form) } } - return view('responses.viewResponses', compact('form', 'responses', 'statistics')); + return view('responses.viewResponses', compact('form', 'responses', 'statistics', 'questions')); } diff --git a/package-lock.json b/package-lock.json index d436571..e4b6383 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,8 @@ "packages": { "": { "dependencies": { - "sweetalert": "^2.1.2" + "sweetalert": "^2.1.2", + "toastr": "^2.1.4" }, "devDependencies": { "@popperjs/core": "^2.11.6", @@ -889,6 +890,11 @@ "node": ">=0.12.0" } }, + "node_modules/jquery": { + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.7.1.tgz", + "integrity": "sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg==" + }, "node_modules/laravel-vite-plugin": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/laravel-vite-plugin/-/laravel-vite-plugin-1.0.5.tgz", @@ -1107,6 +1113,14 @@ "node": ">=8.0" } }, + "node_modules/toastr": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/toastr/-/toastr-2.1.4.tgz", + "integrity": "sha512-LIy77F5n+sz4tefMmFOntcJ6HL0Fv3k1TDnNmFZ0bU/GcvIIfy6eG2v7zQmMiYgaalAiUv75ttFrPn5s0gyqlA==", + "dependencies": { + "jquery": ">=1.12.0" + } + }, "node_modules/vite": { "version": "5.3.3", "resolved": "https://registry.npmjs.org/vite/-/vite-5.3.3.tgz", diff --git a/package.json b/package.json index 8e672e9..0ff6706 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "vite": "^5.0.0" }, "dependencies": { - "sweetalert": "^2.1.2" + "sweetalert": "^2.1.2", + "toastr": "^2.1.4" } } diff --git a/public/js/script.js b/public/js/script.js index 4c6026a..57b8ce2 100644 --- a/public/js/script.js +++ b/public/js/script.js @@ -47,7 +47,7 @@ document.addEventListener("DOMContentLoaded", function () { function addNewQuestion() { const newQuestionDiv = document.createElement("div"); - newQuestionDiv.className = "question"; + // newQuestionDiv.className = "question"; newQuestionDiv.innerHTML = `
- +
- -
- - -
-
-
-
- - -
-
-
-
-
- - -
-
- - -
-
- - -
- -
-
- -
- - - -   -   -   - - - -
- - - - --}} @@ -122,7 +22,7 @@ -