aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/EventLog.php
blob: 7336415bd4c0ea402dc73783f99291bd1b018904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
 * event_log.php - event logging admin model
 *
 * @author     Elmar Ludwig <ludwig@uos.de>
 * @copyright  2009 Authors
 * @license    GPL2 or any later version
 */
class EventLog
{
    /**
     * clean up old log events
     */
    public function cleanup_log_events()
    {
        return LogEvent::deleteExpired();
    }

    /**
     * get object types available for query
     */
    public function get_object_types()
    {
        return [
            'course'    => _('Veranstaltung'),
            'institute' => _('Einrichtung'),
            'user'      => _('Nutzer/-in'),
            'resource'  => _('Ressource'),
            'other'     => _('Sonstige (von Aktion abhängig)')
        ];
    }

    /**
     * find objects matching the given string
     */
    public function find_objects($type, $string, $action_name = null)
    {
        switch ($type) {
            case 'course':
                return StudipLog::searchSeminar(addslashes($string));
            case 'institute':
                return StudipLog::searchInstitute(addslashes($string));
            case 'user':
                return StudipLog::searchUser(addslashes($string));
            case 'resource':
                return StudipLog::searchResource(addslashes($string));
            case 'other':
                return StudipLog::searchObjectByAction($string, $action_name);
        }

        return NULL;
    }

    /**
     * build SQL query filter for selected action and object
     */
    private function sql_event_filter($action_id, $object_id, &$parameters = [])
    {
        $filter = [];
        if (isset($action_id) && $action_id != 'all') {
            $filter[] = "action_id = :action_id";
            $parameters[':action_id'] = $action_id;
        }

        if (isset($object_id)) {
            $filter[] = "(:object_id IN (affected_range_id, coaffected_range_id, user_id))";
            $parameters[':object_id'] = $object_id;
        }

        return count($filter) > 0 ? implode(' AND ', $filter) : '';
    }

    /**
     * count number of log events for selected action
     */
    public function count_log_events($action_id, $object_id)
    {
        $filter = $this->sql_event_filter($action_id, $object_id, $parameters);
        return LogEvent::countBySql($filter ?: '1', $parameters);
    }

    /**
     * get log events (max. 50) for selected action, starting at offset
     */
    public function get_log_events($action_id, $object_id, $offset)
    {
        $offset = (int) $offset;

        $filter  = $this->sql_event_filter($action_id, $object_id, $parameters) ?: '1';
        $filter .= " ORDER BY mkdate DESC, event_id DESC LIMIT {$offset}, 50";
        $log_events = LogEvent::findBySQL($filter, $parameters);
        $events = [];

        foreach ($log_events as $log_event) {
            $events[] = [
                'time'   => $log_event->mkdate,
                'info'   => $log_event->formatEvent(),
                'detail' => $log_event->info,
                'debug'  => $log_event->dbg_info
            ];
        }

        return $events;
    }

    /**
     * get list of all available log actions
     */
    public function get_log_actions()
    {
        $log_count = LogEvent::countByActions();
        $actions = LogAction::findBySQL('1 ORDER BY name');
        $log_actions = [];
        foreach ($actions as $action) {
            $log_actions[$action->getId()] = $action->toArray();
            $log_actions[$action->getId()]['log_count']
                    = (int) ($log_count[$action->getId()] ?? 0);
        }

        return $log_actions;
    }
}