requireRole('admin')) { return $r; } $activityModel = new ActivityLogModel(); // Get filter parameters $search = $this->request->getGet('search') ?? ''; $action = $this->request->getGet('action') ?? ''; $userType = $this->request->getGet('user_type') ?? ''; $dateFrom = $this->request->getGet('date_from') ?? ''; $dateTo = $this->request->getGet('date_to') ?? ''; $ip = $this->request->getGet('ip') ?? ''; // Build filters array $filters = []; if (!empty($search)) $filters['search'] = $search; if (!empty($action)) $filters['action'] = $action; if (!empty($userType)) $filters['user_type'] = $userType; if (!empty($dateFrom)) $filters['date_from'] = $dateFrom; if (!empty($dateTo)) $filters['date_to'] = $dateTo; if (!empty($ip)) $filters['ip'] = $ip; // Get filtered logs $logs = $activityModel->getFilteredLogs($filters, 200); // Debug: Check database connection and count $db = \Config\Database::connect(); $totalInDb = $db->table('activity_logs')->countAll(); log_message('debug', 'Activity logs in database: ' . $totalInDb . ', retrieved: ' . count($logs)); return view('admin/activity_log', [ 'logs' => $logs, 'totalLogs' => count($logs), 'totalInDb' => $totalInDb, 'actionSummary' => $activityModel->getActionSummary(), 'roleSummary' => $activityModel->getRoleSummary(), 'filters' => [ 'search' => $search, 'action' => $action, 'user_type' => $userType, 'date_from' => $dateFrom, 'date_to' => $dateTo, 'ip' => $ip, ], 'availableActions' => $activityModel->getAvailableActions(), 'actionIcons' => [ 'login' => 'box-arrow-in-right', 'logout' => 'box-arrow-right', 'register_patient' => 'person-plus', 'register_doctor' => 'person-badge-plus', 'add_doctor' => 'person-badge-plus', 'update_doctor' => 'person-badge', 'delete_doctor' => 'person-badge-x', 'add_patient' => 'person-plus', 'update_patient' => 'person', 'delete_patient' => 'person-x', 'book_appointment' => 'calendar-plus', 'update_appointment' => 'calendar-check', 'cancel_appointment' => 'calendar-x', 'view_appointment' => 'calendar-event', ], ]); } public function clear() { if ($r = $this->requireRole('admin')) { return $r; } if ($this->request->getMethod() === 'post') { $activityModel = new ActivityLogModel(); if ($activityModel->clearAll()) { return redirect()->to(base_url('admin/activity-log'))->with('success', 'All activity logs have been cleared.'); } else { return redirect()->to(base_url('admin/activity-log'))->with('error', 'Failed to clear activity logs.'); } } return redirect()->to(base_url('admin/activity-log')); } public function analytics() { if ($r = $this->requireRole('admin')) { return $r; } $period = $this->request->getGet('period') ?? '7_days'; if (! in_array($period, ['7_days', '30_days'], true)) { $period = '7_days'; } $activityModel = new ActivityLogModel(); $summary = $activityModel->getSummary($period); return view('admin/activity_analytics', [ 'period' => $period, 'summary' => $summary, 'actionLabels' => json_encode(array_keys($summary['by_action'])), 'actionCounts' => json_encode(array_values($summary['by_action'])), 'typeLabels' => json_encode(array_keys($summary['by_role'])), 'typeCounts' => json_encode(array_values($summary['by_role'])), 'userLabels' => json_encode(array_column($summary['most_active_users'], 'actor')), 'userCounts' => json_encode(array_column($summary['most_active_users'], 'count')), 'uniqueIPs' => $summary['unique_ips'], 'criticalActions' => $activityModel->getCriticalActions(50), ]); } }