« Back to History
user_activity_report.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('user_activity_report'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; require_once __DIR__ . '/partials/header.php'; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } function h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } /* ============================================================ FILTERS ============================================================ */ $from_date = trim($_GET['from_date'] ?? date('Y-m-d')); $to_date = trim($_GET['to_date'] ?? date('Y-m-d')); $user_id = (int)($_GET['user_id'] ?? 0); $module = trim($_GET['module'] ?? ''); $action = trim($_GET['action'] ?? ''); /* ============================================================ USERS ============================================================ */ $users = []; $stmtUsers = $pdo->prepare(" SELECT id, name FROM users WHERE company_id = ? AND is_active = 1 ORDER BY name ASC "); $stmtUsers->execute([$company_id]); $users = $stmtUsers->fetchAll(PDO::FETCH_ASSOC); /* ============================================================ QUERY ============================================================ */ $where = []; $params = []; $where[] = "l.company_id = ?"; $params[] = $company_id; $where[] = "DATE(l.created_at) >= ?"; $params[] = $from_date; $where[] = "DATE(l.created_at) <= ?"; $params[] = $to_date; if ($user_id > 0) { $where[] = "l.user_id = ?"; $params[] = $user_id; } if ($module !== '') { $where[] = "l.module_name = ?"; $params[] = $module; } if ($action !== '') { $where[] = "l.activity_type = ?"; $params[] = $action; } $where_sql = implode(' AND ', $where); /* ============================================================ FETCH LOGS ============================================================ */ $sql = " SELECT l.*, u.name AS user_name, fr.role_name FROM user_activity_logs l LEFT JOIN users u ON u.id = l.user_id LEFT JOIN functional_roles fr ON fr.id = l.functional_role_id WHERE {$where_sql} ORDER BY l.id DESC LIMIT 500 "; $stmt = $pdo->prepare($sql); $stmt->execute($params); $logs = $stmt->fetchAll(PDO::FETCH_ASSOC); /* ============================================================ SUMMARY ============================================================ */ $total_logs = count($logs); $create_count = 0; $update_count = 0; $delete_count = 0; $export_count = 0; $print_count = 0; foreach ($logs as $r) { switch (strtolower($r['activity_type'])) { case 'create': $create_count++; break; case 'update': $update_count++; break; case 'delete': $delete_count++; break; case 'export': $export_count++; break; case 'print': $print_count++; break; } } ?> <div class="container-fluid py-4"> <div class="card shadow-sm border-0 rounded-4"> <div class="card-header bg-dark text-white"> <h4 class="mb-0"> User Activity Report </h4> </div> <div class="card-body"> <!-- ========================================================= FILTERS ========================================================== --> <form method="get" class="row g-3 mb-4"> <div class="col-md-2"> <label class="form-label"> From Date </label> <input type="date" name="from_date" class="form-control" value="<?= h($from_date) ?>" > </div> <div class="col-md-2"> <label class="form-label"> To Date </label> <input type="date" name="to_date" class="form-control" value="<?= h($to_date) ?>" > </div> <div class="col-md-2"> <label class="form-label"> User </label> <select name="user_id" class="form-select" > <option value=""> All Users </option> <?php foreach ($users as $usr): ?> <option value="<?= (int)$usr['id'] ?>" <?= $user_id == $usr['id'] ? 'selected' : '' ?> > <?= h($usr['name']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <label class="form-label"> Module </label> <input type="text" name="module" class="form-control" value="<?= h($module) ?>" placeholder="payment" > </div> <div class="col-md-2"> <label class="form-label"> Action </label> <select name="action" class="form-select" > <option value=""> All </option> <?php $actions = [ 'create', 'update', 'delete', 'export', 'print', 'approve', 'status_change', ]; ?> <?php foreach ($actions as $ac): ?> <option value="<?= h($ac) ?>" <?= $action === $ac ? 'selected' : '' ?> > <?= ucfirst($ac) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-2 d-flex align-items-end"> <button type="submit" class="btn btn-primary w-100" > Search </button> </div> </form> <!-- ========================================================= SUMMARY ========================================================== --> <div class="row mb-4"> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Total Logs</h6> <h3> <?= number_format($total_logs) ?> </h3> </div> </div> </div> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Create</h6> <h3> <?= number_format($create_count) ?> </h3> </div> </div> </div> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Update</h6> <h3> <?= number_format($update_count) ?> </h3> </div> </div> </div> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Delete</h6> <h3> <?= number_format($delete_count) ?> </h3> </div> </div> </div> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Export</h6> <h3> <?= number_format($export_count) ?> </h3> </div> </div> </div> <div class="col-md-2"> <div class="card border-0 shadow-sm"> <div class="card-body text-center"> <h6>Print</h6> <h3> <?= number_format($print_count) ?> </h3> </div> </div> </div> </div> <!-- ========================================================= TABLE ========================================================== --> <div class="table-responsive"> <table class="table table-bordered table-hover align-middle"> <thead class="table-dark"> <tr> <th width="140"> Date Time </th> <th> User </th> <th> Role </th> <th> Module </th> <th> Action </th> <th> Page </th> <th> Description </th> <th width="80"> Ref ID </th> <th width="130"> IP </th> </tr> </thead> <tbody> <?php if ($logs): ?> <?php foreach ($logs as $row): ?> <tr> <td> <?= h($row['created_at']) ?> </td> <td> <?= h($row['user_name']) ?> </td> <td> <?= h($row['role_name']) ?> </td> <td> <?= h($row['module_name']) ?> </td> <td> <?php $badge = 'secondary'; switch ($row['activity_type']) { case 'create': $badge = 'success'; break; case 'update': $badge = 'warning'; break; case 'delete': $badge = 'danger'; break; case 'export': $badge = 'info'; break; case 'print': $badge = 'primary'; break; } ?> <span class="badge bg-<?= h($badge) ?>"> <?= h(strtoupper($row['activity_type'])) ?> </span> </td> <td> <?= h($row['page_slug']) ?> </td> <td> <?= h($row['activity_note']) ?> </td> <td> <?= (int)$row['reference_id'] ?> </td> <td> <?= h($row['ip_address']) ?> </td> </tr> <?php endforeach; ?> <?php else: ?> <tr> <td colspan="9" class="text-center py-4" > No activity logs found. </td> </tr> <?php endif; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>