diff --git a/app/Http/Controllers/AdminDashboardController.php b/app/Http/Controllers/AdminDashboardController.php index 961c557..716af41 100644 --- a/app/Http/Controllers/AdminDashboardController.php +++ b/app/Http/Controllers/AdminDashboardController.php @@ -3,36 +3,71 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Models\Timetable; +use App\Models\User; use App\Models\Notification; +use App\Models\Timetable; +use App\Models\StudentPortalLog; +use Exception; class AdminDashboardController extends Controller { public function index() { - if (!session()->has('admin_id')) { - return redirect()->route('admin.login')->with('error', 'Please login first.'); + try { + // 1. Fetch Users + $users = User::all(); + + // 2. Fetch Notifications + $notifications = Notification::orderBy('created_at', 'desc')->get(); + $unreadNotifications = Notification::where('is_read', false)->count(); + + // 3. Fetch Timetable Slots Count + $totalTimetableSlots = class_exists(Timetable::class) ? Timetable::count() : 0; + + // 4. Fetch Portal Logs with user eager loaded + $portalLogs = class_exists(StudentPortalLog::class) + ? StudentPortalLog::with('user')->get() + : []; + + return view('admin.AdminDashboard', compact( + 'users', + 'notifications', + 'unreadNotifications', + 'totalTimetableSlots', + 'portalLogs' + )); + + } catch (Exception $e) { + \Log::error('Admin Dashboard Error: ' . $e->getMessage()); + + if (config('app.debug')) { + throw $e; + } + + return back()->with('error', 'Something went wrong: ' . $e->getMessage()); } - - - $totalTimetableSlots = Timetable::count(); - - $unreadNotifications = Notification::where('target_role', 'admin') - ->where('is_read', false) - ->count(); - - $notifications = Notification::where('target_role', 'admin') - ->orderBy('created_at', 'desc') - ->take(5) - ->get(); - - return view('admin.admindashboard', compact( - 'totalTimetableSlots', - 'unreadNotifications', - 'notifications' - )); } + // Notifications Store + public function store(Request $request) + { + $request->validate([ + 'title' => 'required|string|max:255', + 'message' => 'required|string', + 'type' => 'required|in:info,success,warning', + ]); + + Notification::create([ + 'title' => $request->title, + 'message' => $request->message, + 'type' => $request->type, + 'is_read' => false, + ]); + + return redirect()->back()->with('success', 'Notification created successfully!'); + } + + // Mark as Read public function markAsRead($id) { $notification = Notification::findOrFail($id); @@ -40,4 +75,57 @@ class AdminDashboardController extends Controller return redirect()->back()->with('success', 'Notification marked as read!'); } + + // Update Notification + public function update(Request $request, $id) + { + $request->validate([ + 'title' => 'required|string|max:255', + 'message' => 'required|string', + 'type' => 'required|in:info,success,warning', + ]); + + $notification = Notification::findOrFail($id); + $notification->update($request->only(['title', 'message', 'type'])); + + return redirect()->back()->with('success', 'Notification updated successfully!'); + } + + // Delete Notification + public function destroy($id) + { + $notification = Notification::findOrFail($id); + $notification->delete(); + + return redirect()->back()->with('success', 'Notification deleted successfully!'); + } + + // Send User to Portal Log + public function sendToPortalLog($userId) + { + try { + $user = User::findOrFail($userId); + + // Check if user is already logged + $exists = StudentPortalLog::where('user_id', $user->id)->exists(); + if ($exists) { + return redirect()->back()->with('error', 'This user is already in Portal Logs!'); + } + + // Create new portal log + StudentPortalLog::create([ + 'user_id' => $user->id, + 'first_name' => $user->first_name ?? '', + 'last_name' => $user->last_name ?? '', + 'email' => $user->email, + 'phone' => $user->phone ?? null, + ]); + + return redirect()->back()->with('success', 'User successfully added to Portal Logs!'); + } catch (Exception $e) { + \Log::error('Send to Portal Log Error: ' . $e->getMessage()); + + return redirect()->back()->with('error', 'Failed to add user to Portal Log.'); + } + } } \ No newline at end of file diff --git a/app/Http/Controllers/StudentPortalController.php b/app/Http/Controllers/StudentPortalController.php index 804ce60..ec31d16 100644 --- a/app/Http/Controllers/StudentPortalController.php +++ b/app/Http/Controllers/StudentPortalController.php @@ -5,10 +5,24 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Hash; +use App\Models\Notification; class StudentPortalController extends Controller -{ - +{public function index(Request $request) + { + + if (!session()->has('student_id') && !session()->has('student_log_id')) { + return redirect('/signin')->with('error', 'Please login first!'); + } + + + $notifications = Notification::latest()->take(10)->get(); + $unreadNotifications = Notification::where('is_read', 0)->count(); + + + return view('StudentPortal', compact('notifications', 'unreadNotifications')); + } + public function studentlogin(Request $request) { @@ -202,4 +216,22 @@ class StudentPortalController extends Controller return redirect()->back()->with('success', 'Profile updated successfully!'); } + + public function studentlogout(Request $request) + { + + $request->session()->forget(['student_id', 'student_log_id', 'student_name', 'student_email']); + $request->session()->invalidate(); + $request->session()->regenerateToken(); + + + if ($request->wantsJson() || $request->ajax()) { + return response()->json([ + 'success' => true, + 'message' => 'Logged out successfully!' + ], 200); + } + + return redirect('/')->with('success', 'Logged out successfully!'); + } } \ No newline at end of file diff --git a/app/Models/Notification.php b/app/Models/Notification.php index 213b241..859925a 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -17,4 +17,5 @@ class Notification extends Model 'is_read', 'target_role', ]; -} \ No newline at end of file +} + diff --git a/app/Models/StudentPortalLog.php b/app/Models/StudentPortalLog.php index 5676927..a51702d 100644 --- a/app/Models/StudentPortalLog.php +++ b/app/Models/StudentPortalLog.php @@ -2,24 +2,37 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class StudentPortalLog extends Model { - protected $table = 'student_portal_logs'; + use HasFactory; + + protected $table = 'student_portal_logs'; - protected $fillable = [ + 'user_id', + 'first_name', + 'last_name', 'email', + 'phone', 'studentid', 'password', 'pin', 'status', ]; - protected $hidden = [ 'password', 'pin', ]; -} + + /** + * Get the user associated with this portal log. + */ + public function user() + { + return $this->belongsTo(User::class, 'user_id'); + } +} \ No newline at end of file diff --git a/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php b/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php index 7007d8c..2d97a09 100644 --- a/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php +++ b/database/migrations/2026_07_15_101829_add_foreign_key_to_student_portal_logs_table.php @@ -11,12 +11,18 @@ return new class extends Migration */ public function up(): void { - Schema::table('student_portal_logs', function (Blueprint $table) { - $table->foreignId('user_id') - ->nullable() - ->after('email') - ->constrained('users') - ->onDelete('cascade'); + Schema::create('student_portal_logs', function (Blueprint $table) { + $table->id(); + $table->foreignId('user_id')->nullable()->constrained('users')->onDelete('cascade'); + $table->string('first_name')->nullable(); + $table->string('last_name')->nullable(); + $table->string('email'); + $table->string('phone')->nullable(); + $table->string('studentid')->nullable(); + $table->string('password')->nullable(); + $table->string('pin')->nullable(); + $table->string('status')->default('active'); + $table->timestamps(); }); } @@ -25,9 +31,6 @@ return new class extends Migration */ public function down(): void { - Schema::table('student_portal_logs', function (Blueprint $table) { - $table->dropForeign(['user_id']); - $table->dropColumn('user_id'); - }); + Schema::dropIfExists('student_portal_logs'); } -}; +}; \ No newline at end of file diff --git a/resources/views/StudentPortal.blade.php b/resources/views/StudentPortal.blade.php index f4d9243..4ae4226 100644 --- a/resources/views/StudentPortal.blade.php +++ b/resources/views/StudentPortal.blade.php @@ -321,6 +321,65 @@ + +
{{ $notification->message }}
+ + + + {{ $notification->created_at->diffForHumans() }} + +Automobile Engineering Academy System Overview
{{ $notification->message }}
- - {{ $notification->created_at->diffForHumans() }} + + + {{ $notification->created_at ? $notification->created_at->diffForHumans() : '' }}| ID | +Name | +Phone | +Portal PIN | +Created Date | +Action | +|
|---|---|---|---|---|---|---|
| #{{ $user->id }} | +{{ $user->first_name }} {{ $user->last_name }} | +{{ $user->email }} | +{{ $user->phone ?? 'N/A' }} | + +
+
+ @if(!empty($log->pin) && $log->pin !== 'N/A')
+
+
+ {{ $log->pin }}
+
+
+
+
+
+
+
+ |
+ {{ $log->created_at ? $log->created_at->format('Y-m-d H:i') : 'N/A' }} | +
+ |
+
| + + No registered users found. + | +||||||
| Log ID | +User ID | +Student Name | +Phone | +Portal PIN | +Created Date | +Actions | +|
|---|---|---|---|---|---|---|---|
| #LOG-{{ $log->id }} | +#{{ $log->user_id }} | +
+
+ @if(!empty($log->first_name) || !empty($log->last_name))
+ {{ trim($log->first_name . ' ' . $log->last_name) }}
+ @elseif($log->user)
+ {{ $log->user->name ?? trim($log->user->first_name . ' ' . $log->user->last_name) }}
+ @else
+ N/A
+ @endif
+
+ |
+ {{ $log->email }} | +{{ $log->phone ?? ($log->user->phone ?? 'N/A') }} | + + ++ + {{ $log->pin ?? 'N/A' }} + + | + +{{ $log->created_at ? $log->created_at->format('Y-m-d H:i') : 'N/A' }} | + +
+
+
+ Active
+
+
+
+
+
+
+
+
+ |
+
| + + No active student portal logs found. + | +|||||||