|
- <?php
-
- declare(strict_types=1);
-
- namespace App\Repositories;
-
- use Core\Database;
-
- class ActivityRepository
- {
- public function __construct(protected ?Database $database = null)
- {
- $this->database ??= database();
- }
-
- public function record(
- string $eventType,
- string $headline,
- string $detail = '',
- ?int $projectId = null,
- ?int $taskId = null
- ): void {
- $this->database->execute(
- 'INSERT INTO activity_log (project_id, task_id, event_type, headline, detail)
- VALUES (:project_id, :task_id, :event_type, :headline, :detail)',
- [
- 'project_id' => $projectId,
- 'task_id' => $taskId,
- 'event_type' => $eventType,
- 'headline' => $headline,
- 'detail' => $detail,
- ]
- );
- }
-
- public function recent(int $limit = 10): array
- {
- $limit = max(1, min(50, $limit));
-
- return $this->database->query(
- 'SELECT a.*, p.name AS project_name, p.code AS project_code, t.title AS task_title
- FROM activity_log a
- LEFT JOIN projects p ON p.id = a.project_id
- LEFT JOIN tasks t ON t.id = a.task_id
- ORDER BY a.created_at DESC, a.id DESC
- LIMIT ' . $limit
- );
- }
-
- public function forProject(int $projectId, int $limit = 12): array
- {
- $limit = max(1, min(50, $limit));
-
- return $this->database->query(
- 'SELECT a.*, t.title AS task_title
- FROM activity_log a
- LEFT JOIN tasks t ON t.id = a.task_id
- WHERE a.project_id = :project_id
- ORDER BY a.created_at DESC, a.id DESC
- LIMIT ' . $limit,
- ['project_id' => $projectId]
- );
- }
- }
|