summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_MAZA/class.MAZAExport.php
blob: cfb686361b62c824459310cd783d0a948aec5048 (plain) (blame)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php

/**
 * Created on 23.1.2018
 *
 * @author: Uroš Podkrižnik
 *
 * @desc: for exports of module MAZA - mobile app for respondents
 *
 *
 */
class MAZAExport {

    var $_ank_id; // id ankete

    function __construct($ank_id = 0) {
        $this->_ank_id = $ank_id;
    }

    /*private function getDataInactiveIdentifiers() {
        $Query = "SELECT au.datetime_inserted, au.identifier FROM maza_app_users AS au " .
                "LEFT JOIN (SELECT ank_id, maza_user_id FROM maza_user_srv_access) AS sa " .
                "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id' AND au.datetime_last_active IS NULL;";

        $podatki = array(array('inserted', 'identifier'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($sql_row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($sql_row['datetime_inserted'], $sql_row['identifier']);
            }
        }

        return $podatki;
    }
    
    private function getDataActiveIdentifiers() {
        $Query = "SELECT au.datetime_inserted, au.identifier, au.datetime_last_active, au.deviceInfo, au.tracking_log FROM maza_app_users AS au " .
                "LEFT JOIN (SELECT ank_id, maza_user_id FROM maza_user_srv_access) AS sa " .
                "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id' AND au.datetime_last_active IS NOT NULL;";

        $podatki = array(array('inserted', 'identifier', 'last_active', 'device_info', 'tracking_log'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($sql_row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($sql_row['datetime_inserted'], $sql_row['identifier'], $sql_row['datetime_last_active'], $sql_row['deviceInfo'], $sql_row['tracking_log']);
            }
        }

        return $podatki;
    }*/
    
    private function getDataIdentifiers() {
        $act = isset($_POST['maza_active_ident']) && $_POST['maza_active_ident'] == 1;
        $inact = isset($_POST['maza_inactive_ident']) && $_POST['maza_inactive_ident'] == 1;
        $deact = isset($_POST['maza_deactive_ident']) && $_POST['maza_deactive_ident'] == 1;
        
        $where = '';
        if($act && !$inact)
            $where .= ' AND au.datetime_last_active IS NOT NULL';
        else if(!$act && $inact && $deact)
            $where .= ' AND (au.datetime_last_active IS NULL OR sa.datetime_unsubscribed IS NOT NULL)';
        else if(!$act && $inact)
            $where .= ' AND au.datetime_last_active IS NULL';
        if($deact && !$act && !$inact)
            $where .= ' AND sa.datetime_unsubscribed IS NOT NULL';
        else if(!$deact)
            $where .= ' AND sa.datetime_unsubscribed IS NULL';
        
        $Query = "SELECT au.datetime_inserted, au.identifier, au.datetime_last_active, au.deviceInfo, au.tracking_log, sa.datetime_unsubscribed FROM maza_app_users AS au " .
                "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_unsubscribed FROM maza_user_srv_access) AS sa " .
                "ON au.id = sa.maza_user_id WHERE sa.ank_id='$this->_ank_id'$where;";
        
        $podatki = array(array('inserted', 'identifier', 'last_active', 'device_info', 'tracking_log', 'unsubscribed'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($sql_row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($sql_row['datetime_inserted'], $sql_row['identifier'], $sql_row['datetime_last_active'], $sql_row['deviceInfo'], $sql_row['tracking_log'], $sql_row['datetime_unsubscribed']);
            }
        }

        return $podatki;
    }
    
    private function getDataTrackingLocations() {
        $Query = "SELECT au.identifier, ml.maza_user_id, ml.lat, ml.lng, ml.provider, ml.timestamp, ml.accuracy, ml.altitude, "
                . "ml.bearing, ml.speed, ml.vertical_acc, ml.speed_acc, ml.bearing_acc, ml.extras, ml.is_mock FROM maza_user_locations AS ml "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa "
                . "ON ml.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = ml.maza_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL AND sa.datetime_started <= ml.timestamp "
                . "ORDER BY sa.datetime_started, au.identifier, ml.timestamp DESC;";

        $podatki = array(array('Identifier', 'timestamp', 'latitude', 'longitude', 'provider', 'accuracy', 
            'altitude', 'bearing', 'speed', 'vertical_acc', 'speed_acc', 'bearing_acc', 'extras', 'is_mock'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['timestamp'], $row['lat'], $row['lng'], $row['provider'], 
                    $row['accuracy'], $row['altitude'], $row['bearing'], $row['speed'], $row['vertical_acc'], $row['speed_acc'], 
                    $row['bearing_acc'], $row['extras'], $row['is_mock']);
            }
        }
        return $podatki;
    }
    
    private function getDataTrackingAR() {
        $Query = "SELECT au.identifier, ml.maza_user_id, ml.timestamp, ml.in_vehicle, ml.on_bicycle, ml.on_foot, ml.walking, "
                . "ml.running, ml.still, ml.tilting, ml.unknown FROM maza_user_activity_recognition AS ml "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa "
                . "ON ml.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = ml.maza_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL AND sa.datetime_started <= ml.timestamp "
                . "ORDER BY sa.datetime_started, au.identifier, ml.timestamp DESC;";

        $podatki = array(array('identifier', 'timestamp', 'in_vehicle', 'on_bicycle', 'on_foot', 'walking', 'running', 
            'still', 'tilting', 'unknown'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['timestamp'], $row['in_vehicle'], $row['on_bicycle'], $row['on_foot'], 
                    $row['walking'], $row['running'], $row['still'], $row['tilting'], $row['unknown']);
            }
        }

        return $podatki;
    }
    
    private function getDataAlarmRespondents() {
        $Query = "SELECT au.identifier, su.maza_user_id, su.srv_user_id, su.mode, su.srv_version_datetime, res.id, res.recnum, re.ank_id FROM maza_srv_users AS su "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa ON su.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT ank_id FROM maza_srv_repeaters) AS re ON re.ank_id = sa.ank_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = su.maza_user_id "
                . "LEFT JOIN (SELECT id, recnum FROM srv_user) AS res ON res.id = su.srv_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND re.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL AND (mode='repeater' OR (su.geof_id IS NULL AND su.activity_id IS NULL)) "//todo zadnji OR je samo zacasno, pobrisi enkrat po APP verziji 25
                . "ORDER BY sa.datetime_started, au.identifier, su.srv_version_datetime DESC;";

        $podatki = array(array('identifier', 'recnum', 'time_version'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['recnum'], $row['srv_version_datetime']);
            }
        }
        return $podatki;
    }
        
    private function getDataTriggeredGeofences() {
        $Query = "SELECT tg.id, tg.maza_user_id, tg.geof_id, tg.triggered_timestamp, tg.enter_timestamp, tg.dwell_timestamp, au.identifier, ge.name, su.maza_user_id, su.srv_user_id, su.srv_version_datetime, su.mode, su.tgeof_id, res.id, res.recnum "
                . ", ml.maza_user_id, ml.lat, ml.lng, ml.provider, ml.timestamp, ml.accuracy, ml.altitude, ml.bearing, ml.speed, ml.vertical_acc, ml.speed_acc, ml.bearing_acc, ml.extras, ml.is_mock"
                . " FROM maza_srv_triggered_geofences AS tg "
                . "LEFT JOIN (SELECT tgeof_id, maza_user_id, lat, lng, provider, timestamp, accuracy, altitude, bearing, speed, vertical_acc, speed_acc, bearing_acc, extras, is_mock FROM maza_user_locations) AS ml ON tg.id = ml.tgeof_id "
                . "LEFT JOIN (SELECT ank_id, id, name FROM maza_srv_geofences) AS ge ON ge.id = tg.geof_id "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa ON tg.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = tg.maza_user_id "
                . "LEFT JOIN (SELECT maza_user_id, srv_user_id, srv_version_datetime, tgeof_id, mode FROM maza_srv_users) AS su ON su.maza_user_id = tg.maza_user_id AND su.tgeof_id = tg.id "
                . "LEFT JOIN (SELECT id, recnum FROM srv_user) AS res ON res.id = su.srv_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND ge.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL "
                . "ORDER BY sa.datetime_started, au.identifier, tg.triggered_timestamp DESC;";
        
        $podatki = array(array('identifier', 'recnum', 'geofence_id', 'geofence_name', 'triggered_timestamp', 'enter_timestamp', 'dwell_timestamp', 'location_timestamp', 'latitude', 'longitude', 'provider', 'accuracy', 
            'altitude', 'bearing', 'speed', 'vertical_acc', 'speed_acc', 'bearing_acc', 'extras', 'is_mock'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['recnum'], $row['geof_id'], $row['name'], $row['triggered_timestamp'], $row['enter_timestamp'], $row['dwell_timestamp'], 
                    $row['timestamp'], $row['lat'], $row['lng'], $row['provider'], $row['accuracy'], $row['altitude'], $row['bearing'], $row['speed'], $row['vertical_acc'], $row['speed_acc'], 
                    $row['bearing_acc'], $row['extras'], $row['is_mock']);
            }
        }
        return $podatki;
    }
    
        private function getDataTriggeredGeofencesAnswers() {
        $Query = "SELECT tg.id, tg.maza_user_id, tg.geof_id, tg.triggered_timestamp, au.identifier, ge.name, su.maza_user_id, su.srv_user_id, su.srv_version_datetime, su.mode, su.tgeof_id, res.id, res.recnum "
                . ", ml.maza_user_id, ml.lat, ml.lng, ml.provider, ml.timestamp, ml.accuracy, ml.altitude, ml.bearing, ml.speed, ml.vertical_acc, ml.speed_acc, ml.bearing_acc, ml.extras, ml.is_mock"
                . " FROM maza_srv_triggered_geofences AS tg "
                . "LEFT JOIN (SELECT tgeof_id, maza_user_id, lat, lng, provider, timestamp, accuracy, altitude, bearing, speed, vertical_acc, speed_acc, bearing_acc, extras, is_mock FROM maza_user_locations) AS ml ON tg.id = ml.tgeof_id "
                . "LEFT JOIN (SELECT ank_id, id, name FROM maza_srv_geofences) AS ge ON ge.id = tg.geof_id "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa ON tg.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = tg.maza_user_id "
                . "LEFT JOIN (SELECT maza_user_id, srv_user_id, srv_version_datetime, tgeof_id, mode FROM maza_srv_users) AS su ON su.maza_user_id = tg.maza_user_id AND su.tgeof_id = tg.id "
                . "LEFT JOIN (SELECT id, recnum FROM srv_user) AS res ON res.id = su.srv_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND ge.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL AND su.mode='geofence' "
                . "ORDER BY res.recnum DESC;";
        
        $podatki = array(array('identifier', 'recnum', 'geofence_id', 'geofence_name', 'triggered_timestamp', 'location_timestamp', 'latitude', 'longitude', 'provider', 'accuracy', 
            'altitude', 'bearing', 'speed', 'vertical_acc', 'speed_acc', 'bearing_acc', 'extras', 'is_mock'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['recnum'], $row['geof_id'], $row['name'], $row['triggered_timestamp'], $row['timestamp'], $row['lat'], $row['lng'], $row['provider'], 
                    $row['accuracy'], $row['altitude'], $row['bearing'], $row['speed'], $row['vertical_acc'], $row['speed_acc'], 
                    $row['bearing_acc'], $row['extras'], $row['is_mock']);
            }
        }
        return $podatki;
    }
    
    private function getDataEntryLocations() {
        $Query = "SELECT au.identifier, su.maza_user_id, su.srv_user_id, su.srv_version_datetime, su.mode, res.id, res.recnum, su.loc_id, ml.id "
                . ", ml.maza_user_id, ml.lat, ml.lng, ml.provider, ml.timestamp, ml.accuracy, ml.altitude, ml.bearing, ml.speed, ml.vertical_acc, ml.speed_acc, ml.bearing_acc, ml.extras, ml.is_mock"
                . " FROM maza_srv_users AS su "
                . "LEFT JOIN (SELECT ank_id, maza_user_id, datetime_started FROM maza_user_srv_access) AS sa ON su.maza_user_id = sa.maza_user_id "
                . "LEFT JOIN (SELECT id, identifier FROM maza_app_users) AS au ON au.id = su.maza_user_id "
                . "LEFT JOIN (SELECT * FROM maza_user_locations) AS ml ON su.maza_user_id = ml.maza_user_id AND su.loc_id = ml.id "
                . "LEFT JOIN (SELECT id, recnum FROM srv_user) AS res ON res.id = su.srv_user_id "
                . "WHERE sa.ank_id='$this->_ank_id' AND sa.datetime_started IS NOT NULL AND su.mode='entry' "
                . "ORDER BY res.recnum DESC;";

        $podatki = array(array('identifier', 'recnum', 'entry_timestamp', 'location_timestamp', 'latitude', 'longitude', 'provider', 'accuracy', 
            'altitude', 'bearing', 'speed', 'vertical_acc', 'speed_acc', 'bearing_acc', 'extras', 'is_mock'));
        
        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['identifier'], $row['recnum'], $row['srv_version_datetime'], $row['timestamp'], $row['lat'], $row['lng'], $row['provider'], 
                    $row['accuracy'], $row['altitude'], $row['bearing'], $row['speed'], $row['vertical_acc'], $row['speed_acc'], 
                    $row['bearing_acc'], $row['extras'], $row['is_mock']);
            }
        }
        return $podatki;
    }
            
    private function getDataGeofences() {
        $Query = "SELECT * FROM maza_srv_geofences WHERE ank_id='$this->_ank_id' ORDER BY id;";

        $podatki = array(array('id', 'survey_id', 'geofence_on', 'center_latitude', 'center_longitude', 'radius', 'center_address', 'name', 
            'notification_title', 'notification_message', 'notification_sound', 'on_transition', 'after_seconds', 'location_after_triggered'));

        $sqlQuery = sisplet_query($Query);

        if (mysqli_num_rows($sqlQuery)) {
            while ($row = mysqli_fetch_assoc($sqlQuery)) {
                $podatki[] = array($row['id'], $row['ank_id'], $row['geofence_on'], $row['lat'], $row['lng'], $row['radius'], $row['address'], $row['name'], $row['notif_title'], 
                    $row['notif_message'], $row['notif_sound'], $row['on_transition'], $row['after_seconds'], $row['location_triggered']);
            }
        }

        return $podatki;
    }

    private function exportCSVTable($data, $name) {
        // Izvozimo CSV
        $fp = fopen('php://output', 'w');

        header('Content-Type: application/csv charset=UTF-8');
        header('Content-Disposition: attachement; filename="'.$name.'_' . time() .'_'. $this->_ank_id . '.csv";');
        echo "\xEF\xBB\xBF"; // UTF-8 BOM

        foreach ($data as $row) {
            fputcsv($fp, $row, ',');
        }

        fclose($fp);
    }
    
    public function exportCSVIdentifiers(){
        $data = $this->getDataIdentifiers();
        $this->exportCSVTable($data, 'maza_identifiers');
    }
    
    /*public function exportCSVInactiveIdentifiers(){
        $data = $this->getDataInactiveIdentifiers();
        $this->exportCSVTable($data, 'maza_inactive');
    }
    
    public function exportCSVActiveIdentifiers(){
        $data = $this->getDataActiveIdentifiers();
        $this->exportCSVTable($data, 'maza_active');
    }*/
    
    public function exportCSVTrackingLocations(){
        $data = $this->getDataTrackingLocations();
        $this->exportCSVTable($data, 'maza_tracking_locations');
    }
    
    public function exportCSVTrackingAR(){
        $data = $this->getDataTrackingAR();
        $this->exportCSVTable($data, 'maza_tracking_ar');
    }
    
    public function exportCSVEntryLocations(){
        $data = $this->getDataEntryLocations();
        $this->exportCSVTable($data, 'maza_entry_locations');
    }
        
    public function exportCSVTriggeredGeofences(){
        $data = $this->getDataTriggeredGeofences();
        $this->exportCSVTable($data, 'maza_triggered_geofences');
    }
    
    public function exportCSVTriggeredGeofencesAnswered(){
        $data = $this->getDataTriggeredGeofencesAnswers();
        $this->exportCSVTable($data, 'maza_triggered_geofences_answers');
    }
    
    public function exportCSVGeofences(){
        $data = $this->getDataGeofences();
        $this->exportCSVTable($data, 'maza_geofences');
    }
    
    public function exportCSVAlarmRespondets(){
        $data = $this->getDataAlarmRespondents();
        $this->exportCSVTable($data, 'maza_alarm_respondets');
    }
}

?>