summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/tracking/TrackingClass.php
blob: fe31ddfc3f8128e1a76a34e2e0cac27b21b28c86 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php

/***************************************
 * Description:
 * Autor: Robert Šmalc
 * Created date: 02.02.2017
 *****************************************/
class TrackingClass
{
    private $sub;
    private $anketa;
    private $status = '';
	
	private $db_table = "";

    public function __construct($anketa = null)
    {
        if ((isset ($_REQUEST['anketa']) && $_REQUEST['anketa'] > 0) || (isset ($anketa) && $anketa > 0)) {
            $this->anketa = (isset ($_REQUEST['anketa']) && $_REQUEST['anketa'] > 0) ? $_REQUEST['anketa'] : $anketa;
        } else {
            return 'Anketa ID ne obstaja';
        }
		
		# poiščemo aktivno anketo
		SurveyInfo :: getInstance()->SurveyInit($this->anketa);
		$this->db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();

        if (isset($_GET['m']) && $_GET['m'] == 'tracking_data')
            $this->sub = 'data';
        elseif (isset($_GET['appendMerge']) && $_GET['appendMerge'] == '1')
            $this->sub = 'append';
        else
            $this->sub = 'survey';

        // Filter po statusu
        if (isset($_GET['status']) && in_array($_GET['status'], array('0', '1', '2', '3', '4', '5', '6')))
            $this->status = " AND status = '{$_GET[status]}' ";


        return $this;
    }

    private static $_instance;

    /**
     * V kolikor razred kličemo statično
     *
     * @return instance
     */

    public static function init()
    {
        if (!static::$_instance)
            static::$_instance = new TrackingClass();

        return static::$_instance;
    }

    /**
     * Filter po statusih
     * Filter omogoča, da se prvi parameter ne upošteva statusa, v koliko ni filtra upošteva vse statuse.
     * Drugi parameter naredi inverzno operacijo - išče samo po tem statusu
     *    0 => urejanje
     *    1 => uvoz podatkov
     *    2 => analiza
     *    3 => reporti
     *    4 => podatki
     *    5 => objava - vabila
     *    6 => hierarhija
     *    20 => hierarhija - splošno
     *    21 => hierarhija - gradnja strukture
     *    22 => hierarhija - uporabniki
     *
     * @param (int or array) $exclude_status
     * @param (boolean) $invert_status
     * @return $this
     */
    public function filter($exclude_status = null, $invert_status = false)
    {
        $opcija = '!';
        if ($invert_status)
            $opcija = '';


        if (!is_null($exclude_status) && is_int($exclude_status)) {
            $this->status = " AND status " . $opcija . "= '" . $exclude_status . "' ";
        } elseif (!is_null($exclude_status) && is_array($exclude_status)) {

            if (!empty($opcija)) {
                $this->status = " AND status NOT IN (" . implode(',', $exclude_status) . ")";
            } else {
                $this->status = " AND status IN (" . implode(',', $exclude_status) . ")";
            }


        }

        return $this;
    }


    /**
     * @desc prikaze tracking sprememb
     * status:
     *   -1 => unknown
     *    0 => urejanje
     *    1 => uvoz podatkov
     *    2 => analiza
     *    3 => reporti
     *    4 => podatki
     *    5 => objava - vabila
     *    20 => hierarhija - splošno
     *    21 => hierarhija - gradnja strukture
     *    22 => hierarhija - uporabniki
     */
    public function trackingDisplay()
    {
        global $lang;

        $surveyName = SurveyInfo::getInstance()->getSurveyTitle();

        echo '<fieldset>';
        echo '<legend>' . $lang['srv_survey_archives_tracking_' . $this->sub] . '</legend>';

        // Tabela s podatki o spremembah podatkov
        if ($this->sub == 'data') {

            // Filter po podatkih
            $data = ' AND (`get` LIKE \'%edit_data%\' 
							OR (`get` LIKE \'%a: "data", m: "quick_edit"%\' AND `get` LIKE \'%post: "1"%\')
							OR (`get` LIKE \'%a: "dataCopyRow"%\')
							OR (`get` LIKE \'%a: "dataDeleteMultipleRow"%\')
							OR (`get` LIKE \'%a: "dataDeleteRow"%\')
							OR (`get` LIKE \'%urejanje: "1"%\' AND status=\'4\')
						)';

            
            // Vrstice s podatki
            $sql = sisplet_query("SELECT * FROM srv_tracking".$this->db_table." WHERE ank_id = '$this->anketa' " . $data . " ORDER BY datetime DESC");
            
            if(!empty(mysqli_fetch_array($sql))) {


                echo '<p class="bottom16">'.$lang['srv_survey_archives_tracking_survey_text1'].' '.$lang['srv_survey_archives_tracking_survey_text1b'].' <a href="index.php?anketa=' . $this->anketa . '&a=data">\'<span class="caps">'.$lang['navigation_NAVI_RESULTS'].'</span>\' - \''.$lang['navigation_NAVI_DATA'].'\'</a> '.$lang['srv_survey_archives_tracking_survey_text1c'].' \''.$surveyName.'\'. '.$lang['srv_survey_archives_tracking_survey_text2'].' <a href="index.php?anketa=' . $this->anketa . '&a=' . $_GET['a'] . '&d=download">'.$lang['srv_survey_archives_tracking_survey_text3'].'</a>.</p>';

                // Izvoz v Excel
                echo '<div class="izvoz_nad_tabelo">';
                echo '<a class="noline" href="index.php?anketa=' . $this->anketa . '&a=' . $_GET['a'] . '&d=download"><span class="faicon xls green link-right"></span>'.$lang['srv_export_excel'].'</a>';
                echo '</div>';

                echo '<table id="tracking" class="fullwidth">';
                // Prva vrstica
                echo '<tr>';
                echo '	<th>' . $lang['date'] . '</th>';
                echo '	<th>' . $lang['user'] . '</th>';
                echo '	<th>IP</th>';
                echo '	<th>Respondent</th>';
                echo '	<th>GET</th>';
                echo '	<th>POST</th>';
                echo '</tr>';

                $count = 0;
                while ($row = mysqli_fetch_array($sql)) {
                    echo '<tr '.($count > 24 ? 'class="displayNone"' : '').($count == 24 ? ' class="show_as_last_row"' : '').'>';

                    $sqlu = sisplet_query("SELECT name, surname FROM users WHERE id = '$row[user]'");
                    $rowu = mysqli_fetch_array($sqlu);

                    // Pri podatkih dobimo posebej podatke o editiranem respondentu
                    $usr_id = '';
                    $cookie = '';

                    // Preverimo ce imamo usr_id v GET-u
                    $get_array_temp = explode(', ', $row['get']);
                    foreach ($get_array_temp AS $get_val) {
                        $param = explode(': ', $get_val);
                        $get_array[$param[0]] = $param[1];
                    }

                    if (isset($get_array['usr_id']) && $get_array['usr_id'] != '') {
                        $usr_id = trim($get_array['usr_id'], '"');
                    } // Preverimo ce iammo slucajno cookie
                    elseif (isset($get_array['survey-' . $this->anketa]) && $get_array['survey-' . $this->anketa] != '') {
                        $cookie = trim($get_array['survey-' . $this->anketa], '"');
                    } else {
                        // Preverimo ce imamo usr_id v POST-u
                        $post_array_temp = explode(', ', $row['post']);
                        foreach ($post_array_temp AS $post_val) {
                            $param = explode(': ', $post_val);
                            $post_array[$param[0]] = $param[1];
                        }

                        if (isset($post_array['usr_id']) && $post_array['usr_id'] != '')
                            $usr_id = trim($post_array['usr_id'], '"');
                    }

                    if ($usr_id != '') {
                        $sqlR = sisplet_query("SELECT recnum FROM srv_user WHERE id = '$usr_id'");
                        $rowR = mysqli_fetch_array($sqlR);

                        $recnum = $rowR['recnum'];
                    } elseif ($cookie != '') {
                        $sqlR = sisplet_query("SELECT recnum FROM srv_user WHERE cookie = '$cookie'");
                        $rowR = mysqli_fetch_array($sqlR);

                        $recnum = $rowR['recnum'];
                    } else
                        $recnum = 0;

                    echo '	<td><div class="no_wrap">' . datetime($row['datetime']) . '</div></td>';
                    echo '	<td><div class="no_wrap">' . $rowu['name'] . ' ' . $rowu['surname'] . '</div></td>';
                    echo '	<td>' . $row['ip'] . '</td>';
                    echo '	<td>' . $recnum . '</td>';
                    echo '	<td><div class="postget_wrap">' . $row['get'] . '</div></td>';
                    echo '	<td><div class="postget_wrap">' . (($row['post'] != "") ? $row['post'] : '/') . '</div></td>';

                    echo '</tr>';

                    $count++;
                }
            }
            else {
                echo '<p>'.ucfirst($lang['srv_survey_archives_tracking_survey_text1b']).' <a href="index.php?anketa=' . $this->anketa . '&a=data">\'<span class="caps">'.$lang['navigation_NAVI_RESULTS'].'</span>\' - \''.$lang['navigation_NAVI_DATA'].'\'</a> '.$lang['srv_survey_archives_tracking_survey_text1d'].'.</p>';
            }
            
            
        } // Tabela s podatki o spremembah - vse oz. merge/append
        else {

            // Legenda statusov
            $statuses = array(
                -1 => $lang['srv_unknown'],
                0 => $lang['srv_urejanje'],
                1 => $lang['import_data'],
                2 => $lang['export_analisys'],
                3 => $lang['srv_reporti'],
                4 => $lang['srv_podatki'],
                5 => $lang['srv_inv_nav_email'],
                20 => $lang['srv_hierarchy'], // Splošni podatki o hierarhiji
                21 => $lang['srv_hierarchy_structure'], // Grajenje hierarhije
                22 => $lang['srv_hierarchy_users'], // Urejanje uporabnikov
            );


            // Filter za uvoze
            if ($this->sub == 'append')
                $appendMerge = " AND (`get` LIKE '%appendMerge%' OR status='1') ";
            else
                $appendMerge = "";


            // Vrstice s podatki
            $sql = sisplet_query("SELECT * FROM srv_tracking".$this->db_table." WHERE ank_id = '$this->anketa' " . $this->status . " " . $appendMerge . " ORDER BY datetime DESC");
            
            if(!empty(mysqli_fetch_array($sql))) {

                if($appendMerge == "")
                    echo '<p class="bottom16">'.$lang['srv_survey_archives_tracking_survey_text1'].' '.$lang['srv_survey_archives_tracking_survey_text1a'].' \''. $surveyName .'\'. '.$lang['srv_survey_archives_tracking_survey_text2'].' <a href="index.php?anketa=' . $this->anketa . '&a=' . $_GET['a'] . '&d=download">'.$lang['srv_survey_archives_tracking_survey_text3'].'</a>.</p>';
                else
                    echo '<p class="bottom16">'.$lang['srv_survey_archives_tracking_survey_text1'].', '.$lang['srv_survey_archives_tracking_survey_text_appendmerge'].' '.$lang['srv_survey_archives_tracking_survey_text1a'].' \''. $surveyName .'\'. '.$lang['srv_survey_archives_tracking_survey_text2'].' <a href="index.php?anketa=' . $this->anketa . '&a=' . $_GET['a'] . '&d=download">'.$lang['srv_survey_archives_tracking_survey_text3'].'</a>.</p>';

                // Izvoz v Excel
                echo '<div class="izvoz_nad_tabelo">';
                echo '<a class="noline" href="index.php?anketa=' . $this->anketa . '&a=' . $_GET['a'] . '&d=download"><span class="faicon xls green link-right"></span>'.$lang['srv_export_excel'].'</a>';
                echo '</div>';
                
                echo '<table id="tracking" class="fullwidth">';
                // Prva vrstica
                echo '<tr>';
                echo '	<th>' . $lang['date'] . '</th>';
                echo '	<th>' . $lang['user'] . '</th>';
                echo '	<th>IP</th>';
                echo '	<th>Podstran</th>';
                echo '	<th>GET</th>';
                echo '	<th>POST</th>';
                echo '</tr>';
            
                $count = 0;
                while ($row = mysqli_fetch_array($sql)) {
                    echo '<tr '.($count > 24 ? 'class="displayNone"' : '').($count == 24 ? ' class="show_as_last_row"' : '').'>';

                    $sqlu = sisplet_query("SELECT name, surname FROM users WHERE id = '$row[user]'");
                    $rowu = mysqli_fetch_array($sqlu);

                    echo '	<td><div class="no_wrap">' . datetime($row['datetime']) . '</div></td>';
                    echo '	<td><div class="no_wrap">' . $rowu['name'] . ' ' . $rowu['surname'] . '</div></td>';
                    echo '	<td>' . $row['ip'] . '</td>';
                    echo '	<td>' . $statuses[$row['status']] . '</td>';
                    echo '	<td><div class="postget_wrap">' . $row['get'] . '</div></td>';
                    echo '	<td><div class="postget_wrap">' .  (($row['post'] != "") ? $row['post'] : '/') . '</div></td>';

                    echo '</tr>';
                    $count++;
                }
            }
            else{
                echo $lang['srv_survey_archives_tracking_append_no_data'];
            }
        }

        echo '</table>';

        echo '</fieldset>';
    }

    public function csvExport(){
        
        define('delimiter', ';');

        $podatki = 'datetime' . delimiter;
        $podatki .= 'user_id' . delimiter;
        $podatki .= 'user_name' . delimiter;
        $podatki .= 'ip' . delimiter;
        $podatki .= 'status' . delimiter;
        $podatki .= 'parameter' . delimiter;
        $podatki .= 'value' . delimiter;
        $podatki .= 'parameter' . delimiter;
        $podatki .= 'value' . delimiter;

        $podatki .= "\n";

        $sql = sisplet_query("SELECT t.*, u.name, u.surname 
                                FROM srv_tracking".$this->db_table." t, users u
                                WHERE t.ank_id = '" . $this->anketa."' ".str_replace('status', 't.status', $this->status)."
                                    AND u.id=t.user
                                ORDER BY t.datetime DESC
                            ");
        while ($row = mysqli_fetch_array($sql)) {

            $podatki .= '' . datetime($row['datetime']) . delimiter;
            $podatki .= '' . $row['user'] . delimiter;
            $podatki .= '' . $row['name'].' '.$row['surname'] . delimiter;
            $podatki .= '' . $row['ip'] . delimiter;
            $podatki .= '' . $row['status'] . delimiter;

            foreach (explode(',', $row['get']) AS $value) {
                $value = explode(':', $value);
                $podatki .= trim($value[0]) . delimiter;
                $podatki .= trim($value[1]) . delimiter;
            }

            $podatki .= "\n";
        }

        $ime = str_replace('-', '_', $_GET['a']);

        return Export::init()->csv('Spremembe_' . $ime, $podatki);
    }

    /**
     * Update srv_tracking table
     * @desc vnese spremembo v srv_tracking za sledenje sprememb
     *
     * @param (int) $anketa
     * @param (int) $status
     */
    private static $time_start;

    static function update($anketa, $status = 0)
    {
        global $global_user_id;

		# poiščemo aktivno anketo
		SurveyInfo :: getInstance()->SurveyInit($anketa);
		$db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();
		
        $get = '';
        foreach ($_GET AS $key => $val) {
            if ($get != '')
                $get .= ', ';
            $get .= $key . ': "' . $val . '"';
        }

        $post = '';
        foreach ($_POST AS $key => $val) {
            if ($post != '')
                $post .= ', ';

            if (is_array($val))
                $val = self::arrayToString($val);

            $post .= $key . ': "' . $val . '"';
        }

        // izracunamo trajanje skripte v sekundah
        if (self::$time_start != null)
            $time_seconds = microtime(true) - self::$time_start;
        else
            $time_seconds = 0;

        // IP uporabnika
        $ip = GetIP();

        $s = sisplet_query("INSERT INTO srv_tracking".$db_table." (ank_id, datetime, ip, user, `get`, post, status, time_seconds) VALUES ('$anketa', NOW(), '".$ip."', '$global_user_id', '$get', '$post','$status', '$time_seconds')");
        if (!$s) echo mysqli_error($GLOBALS['connect_db']);
    }
    
    static function update_user($status = 0)
    {
        global $global_user_id;
		
        $get = '';
        foreach ($_GET AS $key => $val) {
            if ($get != '')
                $get .= ', ';
            $get .= $key . ': "' . $val . '"';
        }

        $post = '';
        foreach ($_POST AS $key => $val) {
            if ($post != '')
                $post .= ', ';

            if (is_array($val))
                $val = self::arrayToString($val);

            $post .= $key . ': "' . $val . '"';
        }

        // izracunamo trajanje skripte v sekundah
        if (self::$time_start != null)
            $time_seconds = microtime(true) - self::$time_start;
        else
            $time_seconds = 0;

        // IP uporabnika
        $ip = GetIP();

        $s = sisplet_query("INSERT INTO user_tracking (datetime, ip, user, `get`, post, status, time_seconds) VALUES (NOW(), '".$ip."', '$global_user_id', '$get', '$post','$status', '$time_seconds')");
        if (!$s) echo mysqli_error($GLOBALS['connect_db']);
    }

    private static function arrayToString($array)
    {
        $string = "";
        if (is_array($array)) {
            foreach ($array as $key => $value) {

                if (is_array($value)) {
                    $string .= ', '.$key .': [' . self::arrayToString($value) . '] ';
                } else {
                    if ($key == 0)
                        $string .= $key . ': ' . $value . ' ';
                }


            }
        }

        return $string;
    }


}