academy/app/Http/Controllers/AdminDashboardController.php

188 lines
6.1 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!');
}
// Store Student Portal Log
public function storePortalLog(Request $request)
{
$request->validate([
'user_id' => 'required|exists:users,id',
]);
return $this->sendToPortalLog($request->user_id);
}
// Send User to Portal Log
public function sendToPortalLog($userId)
{
try
{
$user = User::findOrFail($userId);
$exists = StudentPortalLog::where('user_id', $user->id)->exists();
if ($exists) {
return redirect()->back()->with('error', 'This user is already in Portal Logs!');
}
StudentPortalLog::create([
'user_id' => $user->id,
'first_name' => $user->first_name ?? '',
'last_name' => $user->last_name ?? '',
'email' => $user->email,
'phone' => $user->phone ?? null,
'pin' => 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: ' . $e->getMessage());
}
}
// Update or Delete User Portal PIN
public function updatePin(Request $request, $id)
{
$request->validate([
'pin' => 'nullable|string|max:20',
]);
try {
$portalLog = StudentPortalLog::findOrFail($id);
$portalLog->pin = $request->filled('pin') ? $request->pin : null;
$portalLog->save();
$message = $request->filled('pin')
? 'Portal PIN updated successfully!'
: 'Portal PIN deleted successfully!';
return redirect()->back()->with('success', $message);
} catch (Exception $e) {
\Log::error('Update PIN Error: ' . $e->getMessage());
return redirect()->back()->with('error', 'Failed to update PIN: ' . $e->getMessage());
}
}
// =========================================================================
// STUDENT PORTAL LOG DELETION ONLY
// =========================================================================
public function destroyPortalLog($id)
{
try {
$portalLog = StudentPortalLog::findOrFail($id);
$portalLog->delete();
return redirect()->back()->with('success', 'Student Portal access record deleted successfully!');
} catch (Exception $e) {
\Log::error('Delete Portal Log Error: ' . $e->getMessage());
return redirect()->back()->with('error', 'Failed to remove from Student Portal: ' . $e->getMessage());
}
}
}