Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
EventLogDatatable
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 5
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getPayload
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 doPostProcessing
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
12
 getFilters
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
210
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2017 (original work) Open Assessment Technologies SA ;
19 *
20 */
21
22namespace oat\taoEventLog\model\datatable;
23
24use DateTimeInterface;
25use oat\taoEventLog\model\eventLog\LoggerService;
26use oat\tao\model\datatable\implementation\DatatableRequest;
27use oat\tao\model\datatable\DatatablePayload;
28use oat\oatbox\service\ServiceManager;
29use Zend\ServiceManager\ServiceLocatorAwareInterface;
30use Zend\ServiceManager\ServiceLocatorAwareTrait;
31use DateTime;
32use DateTimeZone;
33
34/**
35 * Class DeliveriesActivityDatatable
36 * @package oat\taoEventLog\model\datatable
37 * @author Aleh Hutnikau, <hutnikau@1pt.com>
38 */
39class EventLogDatatable implements DatatablePayload, ServiceLocatorAwareInterface
40{
41    use ServiceLocatorAwareTrait;
42
43    /** @var DatatableRequest */
44    protected $request;
45
46    /** @var LoggerService */
47    protected $loggerService;
48
49    /**
50     * EventLogDatatable constructor.
51     */
52    public function __construct()
53    {
54        $this->setServiceLocator(ServiceManager::getServiceManager());
55        $request = DatatableRequest::fromGlobals();
56        $this->request = $request;
57        $this->loggerService =  $this->getServiceLocator()->get(LoggerService::SERVICE_ID);
58    }
59
60    /**
61     * @return array
62     */
63    public function getPayload()
64    {
65        $filters = $this->getFilters();
66        $results = [
67            'data' => $this->loggerService->searchInstances($filters, [
68                'limit' => $this->request->getRows(),
69                'offset' => ($this->request->getPage() - 1) * $this->request->getRows(),
70                'sort' => $this->request->getSortBy(),
71                'order' => $this->request->getSortOrder(),
72            ]),
73            'records' => $this->loggerService->count($filters),
74        ];
75
76        $result = $this->doPostProcessing($results);
77
78        return $result;
79    }
80
81    /**
82     * @param array $results
83     * @return array
84     */
85    protected function doPostProcessing(array $results)
86    {
87        // prettify data
88        array_walk($results['data'], function (&$row) {
89
90            $date = new DateTime($row['occurred'], new DateTimeZone('UTC'));
91            $row['occurred'] = \tao_helpers_Date::displayeDate($date->getTimestamp());
92
93            $row['raw'] = array_map(null, $row);
94
95            $row['id'] = 'identifier-' . $row['id'];
96
97            $eventNameChunks = explode('\\', $row['event_name']);
98            $row['event_name'] = array_pop($eventNameChunks);
99            $row['user_id'] = \tao_helpers_Uri::getUniqueId($row['user_id']) ?: $row['user_id'];
100
101            $roles = explode(',', $row['user_roles']);
102            foreach ($roles as &$role) {
103                $role =  \tao_helpers_Uri::getUniqueId($role);
104            }
105            $row['user_roles'] = join(', ', $roles);
106        });
107
108        $payload = [
109            'data' => $results['data'],
110            'page' => (int) $this->request->getPage(),
111            'records' => (int) count($results['data']),
112            'total' => ceil($results['records'] / $this->request->getRows()),
113        ];
114        return $payload;
115    }
116
117    /**
118     * @return array
119     */
120    protected function getFilters()
121    {
122        $params = \Context::getInstance()->getRequest()->getParameters();
123        $filters = [];
124
125        foreach ($params as $key => $val) {
126            if (empty($params[$key])) {
127                unset($params[$key]);
128            }
129        }
130
131        /** @var \common_session_Session $session */
132        $session = \common_session_SessionManager::getSession();
133
134        $timeZone = $session->getTimeZone();
135        $utc = new DateTimeZone('UTC');
136
137        if (isset($params['periodStart']) && !empty($params['periodStart'])) {
138            $params['periodStart'] = (
139                new DateTime(
140                    $params['periodStart'],
141                    new DateTimeZone($timeZone)
142                )
143            )->setTimezone($utc)->format(DateTimeInterface::ISO8601);
144        }
145
146        if (isset($params['periodEnd']) && !empty($params['periodEnd'])) {
147            $params['periodEnd'] = (
148                new DateTime(
149                    $params['periodEnd'],
150                    new DateTimeZone($timeZone)
151                )
152            )->setTimezone($utc)->format(DateTimeInterface::ISO8601);
153        }
154
155        if (isset($params['periodStart']) && isset($params['periodEnd'])) {
156            $filters[] = ['occurred', 'between', $params['periodStart'], $params['periodEnd']];
157        } elseif (isset($params['periodStart'])) {
158            $filters[] = ['occurred', '>', $params['periodStart']];
159        } elseif (isset($params['periodEnd'])) {
160            $filters[] = ['occurred', '<', $params['periodEnd']];
161        }
162
163        if (isset($params['filtercolumns']) && is_array($params['filtercolumns'])) {
164            foreach ($params['filtercolumns'] as $col => $val) {
165                $filters[] = [$col, 'like', '%' . $val . '%'];
166            }
167        }
168
169        return $filters;
170    }
171
172    /**
173     * @return array
174     */
175    public function jsonSerialize()
176    {
177        return $this->getPayload();
178    }
179}