aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/shared/log_event.php
blob: 923a462f09b9d1607d4846e5c4ea3771685f91af (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
 * log_event.php - Shared_LogEventController
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author      Peter Thienel <thienel@data-quest.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 * @since       3.5
 */
class Shared_LogEventController extends MVVController
{

    public function show_action($object_type, $object_id)
    {
        $this->object_type = $object_type;
        $this->object_id = $object_id;

        // check permissions
        $this->have_log_perm($this->object_type, $this->object_id);

        $event_log = new EventLog();

        $this->start = (int) Request::int('start');
        $this->format = Request::option('format');
        $this->num_entries = 0;
        $this->log_events = [];

        $this->num_entries += $event_log->count_log_events('all', $this->object_id);
        if ($this->num_entries) {
            $this->log_events = $event_log->get_log_events('all', $this->object_id, 0);
        }

        $this->object2_type = Request::option('object2_type');
        $this->object2_id = Request::option('object2_id');
        if ($this->object2_type) {
            // check permission
            $this->have_log_perm($this->object2_type, $this->object2_id);
            $this->num_entries += $event_log->count_log_events('all', $this->object2_id);
            $log_events = $event_log->get_log_events('all', $this->object2_id, 0);
            if ($log_events) {
                if (empty($this->log_events)){
                    $this->log_events = $log_events;
                } else {
                    $this->log_events = array_merge($this->log_events, $log_events);
                }
            }
        }

        //Merging the events in correct order
        $this->log_events = $this->order_events_by_date($this->log_events);
    }

    private function have_log_perm($object_type, $object_id)
    {
        if (is_subclass_of($object_type, 'ModuleManagementModel')) {
            $object = $object_type::find($object_id);
            if ($object && MvvPerm::get($object)->havePerm(MvvPerm::PERM_READ)) {
                return true;
            }
        }
        throw new AccessDeniedException();
    }

    private function order_events_by_date($event_array)
    {
        $ordered_events = [];
        $log_events = [];
        foreach ($event_array as $levent) {
            $ordered_events[$levent['time']][] = $levent;
        }
        foreach ($ordered_events as $ts => $events) {
            $log_events = array_merge($log_events, $events);
        }
        return $log_events;

    }

    public function get_log_autor_action()
    {
        if (Request::isAjax()) {
            $mvv_id = Request::get('mvv_id',null);
            $mvv_coid = Request::get('mvv_coid',null);
            $mvv_field = Request::get('mvv_field',null);
            $log_action = Request::get('log_action',null);
            $mvv_debug = Request::get('mvv_debug',null);

            $parts = explode('.', $mvv_field);
            if($parts[0] == 'mvv_modul_deskriptor'){
                if ($modul = Modul::find($mvv_id)) {
                    $mvv_id = $modul->getDeskriptor()->getId();
                }
            }
            if($parts[0] == 'mvv_modulteil_deskriptor'){
                if ($modulteil = Modulteil::find($mvv_id)){
                    $mvv_id = $modulteil->getDeskriptor()->getId();
                }
            }

            if ($mvv_id && $mvv_field || $mvv_debug && $mvv_field) {
                $search_action = "";

                if ($mvv_id){
                    $search_action .= " AND `affected_range_id` LIKE " . DBManager::get()->quote($mvv_id) ;
                }

                if ($mvv_coid){
                    $search_action .= " AND `coaffected_range_id` LIKE " . DBManager::get()->quote($mvv_coid) ;
                }

                if ($mvv_debug){
                    $search_action .= " AND `dbg_info` LIKE " . DBManager::get()->quote($mvv_debug) ;
                }

                if ($log_action == 'new') {
                    $search_action .= " AND ( `log_actions`.`name` LIKE '%_new' OR `log_actions`.`name` LIKE '%_update' )";
                } else {
                    $search_action .= " AND `log_actions`.`name` LIKE CONCAT('%_'," . DBManager::get()->quote($log_action) . ")";
                }

                $statement = DBManager::get()->prepare("SELECT *
                        FROM `log_events`
                        LEFT JOIN `log_actions` ON `log_events`.`action_id` =  `log_actions`.`action_id`
                        WHERE `info` = ?
                        " . $search_action . "
                        ORDER BY `log_events`.`event_id` DESC");
                $statement->execute([$mvv_field]);
                $res = $statement->fetchOne();
                if ($res) {
                    //$user = get_fullname($res['user_id'], "full", true);
                    $user = get_username($res['user_id']);
                    $fields = explode('.',$mvv_field);
                    $field = count($fields) > 1 ? $fields[1] : $mvv_field;
                    echo json_encode(["user"=> $user ,"time"=> date('d.m.Y - H:i:s', $res['mkdate']), "field" => $field]);
                }
            }
            die();
        } else {
            $this->render_nothing();
        }
    }

}