Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
1.25% covered (danger)
1.25%
1 / 80
0.00% covered (danger)
0.00%
0 / 4
CRAP
n/a
0 / 0
_get_login_place
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
register_login_event
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
_get_guest_place
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
90
register_guest_event
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
110
1<?php
2    include_once __DIR__."/registry.php";
3
4    /**
5     * Resolve place for user login. Uses central DB (PLACE).
6     *
7     * @param \ConxHelper $conx Connection helper (global used).
8     * @param array|null $place Output: place row.
9     * @param array $event Login event with 'place' key.
10     * @return int ApplicationError code.
11     */
12    function _get_login_place(\ConxHelper $conx, &$place, &$event){
13        $retval = ApplicationError::Success;
14
15        if (success($retval)){
16            $results = [];
17            $res = $conx->global->query('PLACE', [ 'objid' => $event['place'], 'status' => 'AC' ], $results);
18            if ($res){
19                if (count($results) > 0){
20                    $place = $results[0];
21                }
22
23                if (!$place){
24                    addlog(__FILE__, LogLevel::Error, "No active place found for user login: '".$event['place']."'");
25                    $retval = ApplicationError::NotFound;
26                }
27            }
28            else {
29                $retval = ApplicationError::Database;
30            }
31        }
32
33        return $retval;
34    }
35
36    /**
37     * Register login event (user login). Uses central DB.
38     *
39     * @param \ConxHelper $conx Connection helper.
40     * @param string $session Session id.
41     * @param array $event Event with 'place', 'login', 'created'.
42     * @return int ApplicationError code.
43     */
44    function register_login_event(\ConxHelper $conx, $session, &$event){
45        $retval = ApplicationError::Success;
46
47        // obtain the place related information
48        if (success($retval)){
49            $retval = _get_login_place($conx, $place, $event);
50        }
51
52        if (success($retval)){
53            $dstpath = AppConstants::RegLogPath();
54            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
55                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
56                $error = error_get_last();
57                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
58            }
59
60            if (is_dir($dstpath)){
61                $dstfile = sprintf('%s/%05d_ticket_activity.csv', $dstpath, $place['objid']);
62
63                $lineinfo = [
64                    'session' => $session
65                ];
66
67                if ($event['login'] == '1'){
68                    $chainResult = append_logline($dstfile, $event['created'], $place['objid'], LogEvent::UsrLogin, $lineinfo);
69                    if (!success($chainResult)){
70                        addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place['objid']."' (usr login) — write failure details are in an earlier log line.");
71                    }
72                }
73                else {
74                    $chainResult = append_logline($dstfile, $event['created'], $place['objid'], LogEvent::UsrLogout, $lineinfo);
75                    if (!success($chainResult)){
76                        addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place['objid']."' (usr logout) — write failure details are in an earlier log line.");
77                    }
78                }
79            }
80        }
81
82        return $retval;
83    }
84
85    /**
86     * Resolve place for guest login. Uses central DB (QRSCAN, PLACE).
87     *
88     * @param \ConxHelper $conx Connection helper (global used).
89     * @param array|null $place Output: place row.
90     * @param array $event Login event with 'qrcode' key (QRSCAN.objid from scan).
91     * @return int ApplicationError code.
92     */
93    function _get_guest_place(\ConxHelper $conx, &$place, &$event){
94        $retval = ApplicationError::Success;
95
96        if (success($retval)){
97            $qrcode = null;
98            $results = [];
99            $res = $conx->global->query('QRSCAN', [ 'objid' => $event['qrcode'], 'status' => 'AC' ], $results);
100            if ($res){
101                if (count($results) > 0){
102                    $qrcode = $results[0];
103                }
104
105                if (!$qrcode){
106                    addlog(__FILE__, LogLevel::Error, "No active table found for guest login: '".$event['qrcode']."'");
107                    $retval = ApplicationError::NotFound;
108                }
109            }
110            else {
111                $retval = ApplicationError::Database;
112            }
113        }
114
115        if (success($retval)){
116            $results = [];
117            $res = $conx->global->query('PLACE', [ 'objid' => $qrcode['place'], 'status' => 'AC' ], $results);
118            if ($res){
119                if (count($results) > 0){
120                    $place = $results[0];
121                }
122
123                if (!$place){
124                    addlog(__FILE__, LogLevel::Error, "No active place found for guest login on table: '".$event['qrcode']."'");
125                    $retval = ApplicationError::NotFound;
126                }
127            }
128            else {
129                $retval = ApplicationError::Database;
130            }
131        }
132
133        return $retval;
134    }
135
136    /**
137     * Register guest login event. Uses central DB.
138     *
139     * @param \ConxHelper $conx Connection helper.
140     * @param string $session Session id.
141     * @param array $event Event with 'qrcode', 'login', 'created'.
142     * @return int ApplicationError code.
143     */
144    function register_guest_event(\ConxHelper $conx, $session, &$event){
145        $retval = ApplicationError::Success;
146
147        // obtain the qrcode related information
148        if (success($retval)){
149            $retval = _get_guest_place($conx, $place, $event);
150        }
151
152        if (success($retval)){
153            $dstpath = AppConstants::RegLogPath();
154            if (!file_exists($dstpath) && !mkdir($dstpath, 0775)){
155                addlog(__FILE__, LogLevel::Error, "Could not create local registry log folder in '".$dstpath."'");
156                $error = error_get_last();
157                if ($error) addlog(__FILE__, LogLevel::Debug, $error['message']);
158            }
159
160            if (is_dir($dstpath)){
161                $dstfile = sprintf('%s/%05d_ticket_activity.csv', $dstpath, $place['objid']);
162
163                $lineinfo = [
164                    'session' => $session
165                ];
166
167                if ($event['login'] == '1'){
168                    $chainResult = append_logline($dstfile, $event['created'], $place['objid'], LogEvent::GstLogin, $lineinfo);
169                    if (!success($chainResult)){
170                        addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place['objid']."' (gst login) — write failure details are in an earlier log line.");
171                    }
172                }
173                else {
174                    $chainResult = append_logline($dstfile, $event['created'], $place['objid'], LogEvent::GstLogout, $lineinfo);
175                    if (!success($chainResult)){
176                        addlog(__FILE__, LogLevel::Critical, "Fiscal activity link not written for place '".$place['objid']."' (gst logout) — write failure details are in an earlier log line.");
177                    }
178                }
179            }
180        }
181
182        return $retval;
183    }
184
185?>