|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Modules\HR\Http\Controllers; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Modules\HR\Models\EmployeeSurvey; |
| 7 | +use App\Modules\HR\Models\SurveyResponse; |
| 8 | +use Illuminate\Http\RedirectResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Inertia\Inertia; |
| 11 | +use Inertia\Response; |
| 12 | + |
| 13 | +class EmployeeSurveyController extends Controller |
| 14 | +{ |
| 15 | + public function index(): Response |
| 16 | + { |
| 17 | + $this->authorize('viewAny', EmployeeSurvey::class); |
| 18 | + $surveys = EmployeeSurvey::where('tenant_id', app('tenant')->id) |
| 19 | + ->latest() |
| 20 | + ->paginate(20); |
| 21 | + return Inertia::render('HR/Surveys/Index', compact('surveys')); |
| 22 | + } |
| 23 | + |
| 24 | + public function store(Request $request): RedirectResponse |
| 25 | + { |
| 26 | + $this->authorize('create', EmployeeSurvey::class); |
| 27 | + $validated = $request->validate([ |
| 28 | + 'title' => 'required|string|max:255', |
| 29 | + 'description' => 'nullable|string', |
| 30 | + 'start_date' => 'nullable|date', |
| 31 | + 'end_date' => 'nullable|date|after_or_equal:start_date', |
| 32 | + 'is_anonymous' => 'nullable|boolean', |
| 33 | + ]); |
| 34 | + $validated['tenant_id'] = app('tenant')->id; |
| 35 | + $validated['created_by'] = auth()->id(); |
| 36 | + EmployeeSurvey::create($validated); |
| 37 | + return back()->with('success', 'Survey created.'); |
| 38 | + } |
| 39 | + |
| 40 | + public function show(EmployeeSurvey $survey): Response |
| 41 | + { |
| 42 | + $this->authorize('view', $survey); |
| 43 | + return Inertia::render('HR/Surveys/Show', compact('survey')); |
| 44 | + } |
| 45 | + |
| 46 | + public function publish(EmployeeSurvey $survey): RedirectResponse |
| 47 | + { |
| 48 | + $this->authorize('update', $survey); |
| 49 | + $survey->publish(); |
| 50 | + return back()->with('success', 'Survey published.'); |
| 51 | + } |
| 52 | + |
| 53 | + public function close(EmployeeSurvey $survey): RedirectResponse |
| 54 | + { |
| 55 | + $this->authorize('update', $survey); |
| 56 | + $survey->close(); |
| 57 | + return back()->with('success', 'Survey closed.'); |
| 58 | + } |
| 59 | + |
| 60 | + public function respond(Request $request, EmployeeSurvey $survey): RedirectResponse |
| 61 | + { |
| 62 | + $validated = $request->validate([ |
| 63 | + 'answers' => 'required|array', |
| 64 | + 'employee_id' => 'nullable|exists:employees,id', |
| 65 | + ]); |
| 66 | + SurveyResponse::create([ |
| 67 | + 'tenant_id' => app('tenant')->id, |
| 68 | + 'employee_survey_id' => $survey->id, |
| 69 | + 'employee_id' => $validated['employee_id'] ?? null, |
| 70 | + 'answers' => $validated['answers'], |
| 71 | + 'submitted_at' => now(), |
| 72 | + ]); |
| 73 | + return back()->with('success', 'Response submitted.'); |
| 74 | + } |
| 75 | + |
| 76 | + public function destroy(EmployeeSurvey $survey): RedirectResponse |
| 77 | + { |
| 78 | + $this->authorize('delete', $survey); |
| 79 | + $survey->delete(); |
| 80 | + return back()->with('success', 'Survey deleted.'); |
| 81 | + } |
| 82 | +} |
0 commit comments