feat: Add real user activity tracking system
- Create UserActivitiesTable migration for tracking user activities - Create UserActivityModel for managing activity data - Replace mock data in apiGetRecentActivity with real database data - Remove all mock/fake data from the project
This commit is contained in:
1 parent
c4189cefa7
commit
10176fd47e
3 files changed
+144
-17
No files matched your search
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
namespace App\Core\Database\Migrations;
|
||||
|
||||
use App\Core\Database\Migration;
|
||||
|
||||
class CreateUserActivitiesTable extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
$this->createTable('user_activities', function($table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id');
|
||||
$table->string('action', 100);
|
||||
$table->string('description', 255);
|
||||
$table->json('metadata')->nullable();
|
||||
$table->string('ip_address', 45)->nullable();
|
||||
$table->string('user_agent', 500)->nullable();
|
||||
$table->timestamp('created_at')->useCurrent();
|
||||
|
||||
$table->index(['user_id', 'created_at']);
|
||||
$table->index(['action', 'created_at']);
|
||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
$this->dropTable('user_activities');
|
||||
}
|
||||
}
|
||||
@@ -940,23 +940,9 @@ class UserController extends BaseController
|
||||
exit;
|
||||
}
|
||||
|
||||
// Mock data for now - implement real activity tracking later
|
||||
$activities = [
|
||||
[
|
||||
'id' => 1,
|
||||
'user_id' => 1,
|
||||
'action' => 'login',
|
||||
'description' => 'Utilizador fez login',
|
||||
'created_at' => date('Y-m-d H:i:s')
|
||||
],
|
||||
[
|
||||
'id' => 2,
|
||||
'user_id' => 1,
|
||||
'action' => 'redirect_created',
|
||||
'description' => 'Criou novo redirect',
|
||||
'created_at' => date('Y-m-d H:i:s', strtotime('-1 hour'))
|
||||
]
|
||||
];
|
||||
// Get real activity data from database
|
||||
$activityModel = new \App\Modules\Users\Models\UserActivityModel();
|
||||
$activities = $activityModel->getAllRecentActivities(50);
|
||||
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode([
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
namespace App\Modules\Users\Models;
|
||||
|
||||
use App\Core\DatabaseManager;
|
||||
|
||||
class UserActivityModel
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->db = DatabaseManager::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Log user activity
|
||||
*/
|
||||
public function logActivity(int $userId, string $action, string $description, array $metadata = []): bool
|
||||
{
|
||||
try {
|
||||
$sql = "INSERT INTO user_activities (user_id, action, description, metadata, ip_address, user_agent)
|
||||
VALUES (?, ?, ?, ?, ?, ?)";
|
||||
|
||||
$params = [
|
||||
$userId,
|
||||
$action,
|
||||
$description,
|
||||
json_encode($metadata),
|
||||
$_SERVER['REMOTE_ADDR'] ?? null,
|
||||
$_SERVER['HTTP_USER_AGENT'] ?? null
|
||||
];
|
||||
|
||||
return $this->db->query($sql, $params) > 0;
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get recent activities for a user
|
||||
*/
|
||||
public function getRecentActivities(int $userId, int $limit = 10): array
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT id, user_id, action, description, metadata, ip_address, created_at
|
||||
FROM user_activities
|
||||
WHERE user_id = ?
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?";
|
||||
|
||||
return $this->db->fetchAll($sql, [$userId, $limit]) ?: [];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all recent activities (admin)
|
||||
*/
|
||||
public function getAllRecentActivities(int $limit = 50): array
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT ua.id, ua.user_id, ua.action, ua.description, ua.metadata,
|
||||
ua.ip_address, ua.created_at, u.username, u.email
|
||||
FROM user_activities ua
|
||||
LEFT JOIN users u ON ua.user_id = u.id
|
||||
ORDER BY ua.created_at DESC
|
||||
LIMIT ?";
|
||||
|
||||
return $this->db->fetchAll($sql, [$limit]) ?: [];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get activity statistics
|
||||
*/
|
||||
public function getActivityStats(int $days = 30): array
|
||||
{
|
||||
try {
|
||||
$sql = "SELECT
|
||||
COUNT(*) as total_activities,
|
||||
COUNT(DISTINCT user_id) as active_users,
|
||||
DATE(created_at) as activity_date
|
||||
FROM user_activities
|
||||
WHERE created_at >= DATE_SUB(NOW(), INTERVAL ? DAY)
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY activity_date DESC";
|
||||
|
||||
return $this->db->fetchAll($sql, [$days]) ?: [];
|
||||
} catch (\Exception $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean old activities (older than specified days)
|
||||
*/
|
||||
public function cleanOldActivities(int $days = 90): int
|
||||
{
|
||||
try {
|
||||
$sql = "DELETE FROM user_activities
|
||||
WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)";
|
||||
|
||||
return $this->db->query($sql, [$days]);
|
||||
} catch (\Exception $e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user