diff --git a/app/Http/Controllers/StudentPortalController.php b/app/Http/Controllers/StudentPortalController.php index 34efbe4..804ce60 100644 --- a/app/Http/Controllers/StudentPortalController.php +++ b/app/Http/Controllers/StudentPortalController.php @@ -80,14 +80,126 @@ class StudentPortalController extends Controller return view('StudentProfile', compact('student')); } - public function studentlogout(Request $request) - { - $request->session()->forget(['student_id', 'student_log_id', 'student_name']); - $request->session()->invalidate(); - $request->session()->regenerateToken(); + public function updateProfile(Request $request) + { + $log_id = $request->session()->get('student_log_id'); + $student_code = $request->session()->get('student_id'); - return response()->json(['success' => true]); + if (!$log_id && !$student_code) { + return redirect('/')->with('error', 'Please login first!'); } - + $request->validate([ + 'full_name' => 'required|string|max:255', + 'nic_passport' => 'nullable|string|max:100', + 'date_of_birth' => 'nullable|date', + 'gender' => 'nullable|string|max:20', + 'email' => 'required|email|max:255', + 'phone' => 'nullable|string|max:50', + 'qualification' => 'nullable|string|max:255', + 'institute' => 'nullable|string|max:255', + 'year_completed' => 'nullable|string|max:50', + 'profile_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:5120', + ]); + + $student = DB::table('student_details') + ->where(function ($query) use ($log_id, $student_code) { + if ($log_id) { + $query->where('student_portal_log_id', $log_id); + } + if ($student_code) { + if ($log_id) { + $query->orWhere('student_id', $student_code); + } else { + $query->where('student_id', $student_code); + } + } + }) + ->first(); + + if (!$student) { + return redirect()->back()->with('error', 'Student profile record not found!'); + } + + $updateData = [ + 'full_name' => $request->full_name, + 'nic_passport' => $request->nic_passport, + 'date_of_birth' => $request->date_of_birth, + 'gender' => $request->gender, + 'email' => $request->email, + 'phone' => $request->phone, + 'qualification' => $request->qualification, + 'institute' => $request->institute, + 'year_completed' => $request->year_completed, + 'updated_at' => now(), + ]; + + // 1. Check if user requested to REMOVE image + if ($request->input('remove_profile_image') == '1') { + if (!empty($student->image) && file_exists(public_path($student->image))) { + @unlink(public_path($student->image)); + } + $updateData['image'] = null; + } + // 2. Check if user provided CROPPED Base64 image + elseif (!empty($request->input('cropped_image_data'))) { + $base64Image = $request->input('cropped_image_data'); + if (preg_match('/^data:image\/(\w+);base64,/', $base64Image, $type)) { + $data = substr($base64Image, strpos($base64Image, ',') + 1); + $type = strtolower($type[1]); + if (in_array($type, ['jpg', 'jpeg', 'gif', 'png'])) { + $data = base64_decode($data); + if ($data !== false) { + $filename = time() . '_' . uniqid() . '.png'; + $destinationPath = public_path('uploads/student_images'); + if (!file_exists($destinationPath)) { + mkdir($destinationPath, 0777, true); + } + file_put_contents($destinationPath . '/' . $filename, $data); + + // Delete old image if existed + if (!empty($student->image) && file_exists(public_path($student->image))) { + @unlink(public_path($student->image)); + } + + $updateData['image'] = 'uploads/student_images/' . $filename; + } + } + } + } + // 3. Fallback to normal direct file upload + elseif ($request->hasFile('profile_image')) { + $file = $request->file('profile_image'); + $filename = time() . '_' . uniqid() . '.' . $file->getClientOriginalExtension(); + $destinationPath = public_path('uploads/student_images'); + if (!file_exists($destinationPath)) { + mkdir($destinationPath, 0777, true); + } + $file->move($destinationPath, $filename); + + if (!empty($student->image) && file_exists(public_path($student->image))) { + @unlink(public_path($student->image)); + } + + $updateData['image'] = 'uploads/student_images/' . $filename; + } + + DB::table('student_details') + ->where('id', $student->id) + ->update($updateData); + + if (!empty($student->student_portal_log_id)) { + DB::table('student_portal_logs') + ->where('id', $student->student_portal_log_id) + ->update([ + 'email' => $request->email, + 'updated_at' => now(), + ]); + } + + $request->session()->put('student_name', $request->full_name); + $request->session()->put('student_email', $request->email); + + return redirect()->back()->with('success', 'Profile updated successfully!'); + } } \ No newline at end of file diff --git a/public/uploads/student_images/1786340066_6a7962e2b508b.png b/public/uploads/student_images/1786340066_6a7962e2b508b.png new file mode 100644 index 0000000..0eb0dde Binary files /dev/null and b/public/uploads/student_images/1786340066_6a7962e2b508b.png differ diff --git a/resources/views/StudentPortal.blade.php b/resources/views/StudentPortal.blade.php index 2a4f236..f4d9243 100644 --- a/resources/views/StudentPortal.blade.php +++ b/resources/views/StudentPortal.blade.php @@ -1,344 +1,493 @@ @extends('layouts.studentportalnav') -@section('title', 'Dashboard') +@section('title', 'Student Dashboard') @section('content') -
Manage your academic activities and course updates from your dashboard.
-