academy/app/Http/Controllers/AdminDashboardController.php

131 lines
4.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
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()
{
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());
}
}
// 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);
$notification->update(['is_read' => true]);
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.');
}
}
}