This commit is contained in:
fpicone
2025-12-06 18:42:49 +01:00
parent 30e1f9b36b
commit 61a177475a
11 changed files with 911 additions and 2 deletions

View File

@@ -110,3 +110,110 @@ function formatDateTime($datetime) {
if (empty($datetime)) return '-';
return date('d/m/Y H:i', strtotime($datetime));
}
/**
* Registra un'attività nel log
*
* @param string $action_type Tipo di azione (es: 'login', 'create', 'update', 'delete', 'assign', 'return')
* @param string $action_description Descrizione dell'azione
* @param string $entity_type Tipo di entità (es: 'territory', 'assignment', 'user', 'config')
* @param int $entity_id ID dell'entità coinvolta
*/
function logActivity($action_type, $action_description, $entity_type = null, $entity_id = null) {
if (!isLoggedIn()) {
return false;
}
$db = getDB();
// Ottieni informazioni sull'utente e sulla richiesta
$user_id = $_SESSION['user_id'];
$username = $_SESSION['username'];
$ip_address = $_SERVER['REMOTE_ADDR'] ?? null;
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? null;
try {
$db->execute(
"INSERT INTO activity_logs
(user_id, username, action_type, action_description, entity_type, entity_id, ip_address, user_agent)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
[$user_id, $username, $action_type, $action_description, $entity_type, $entity_id, $ip_address, $user_agent]
);
return true;
} catch (Exception $e) {
error_log("Errore nel log dell'attività: " . $e->getMessage());
return false;
}
}
/**
* Ottieni i log delle attività con filtri e paginazione
*
* @param array $filters Array di filtri (user_id, action_type, entity_type, date_from, date_to)
* @param int $page Numero pagina
* @param int $per_page Elementi per pagina
* @return array Array con 'logs' e 'total'
*/
function getActivityLogs($filters = [], $page = 1, $per_page = 50) {
$db = getDB();
$where = [];
$params = [];
if (!empty($filters['user_id'])) {
$where[] = "user_id = ?";
$params[] = $filters['user_id'];
}
if (!empty($filters['action_type'])) {
$where[] = "action_type = ?";
$params[] = $filters['action_type'];
}
if (!empty($filters['entity_type'])) {
$where[] = "entity_type = ?";
$params[] = $filters['entity_type'];
}
if (!empty($filters['date_from'])) {
$where[] = "DATE(created_at) >= ?";
$params[] = $filters['date_from'];
}
if (!empty($filters['date_to'])) {
$where[] = "DATE(created_at) <= ?";
$params[] = $filters['date_to'];
}
if (!empty($filters['search'])) {
$where[] = "(action_description LIKE ? OR username LIKE ?)";
$search_term = '%' . $filters['search'] . '%';
$params[] = $search_term;
$params[] = $search_term;
}
$where_clause = !empty($where) ? 'WHERE ' . implode(' AND ', $where) : '';
// Conta totale
$total = $db->fetchOne(
"SELECT COUNT(*) as count FROM activity_logs $where_clause",
$params
)['count'];
// Ottieni log con paginazione
$offset = ($page - 1) * $per_page;
$logs = $db->fetchAll(
"SELECT * FROM activity_logs
$where_clause
ORDER BY created_at DESC
LIMIT ? OFFSET ?",
array_merge($params, [$per_page, $offset])
);
return [
'logs' => $logs,
'total' => $total,
'pages' => ceil($total / $per_page),
'current_page' => $page
];
}