summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_advanced_paradata/class.SurveyAdvancedParadataExport.php
blob: 7055b0777078c725862eda89a61ae57db0d071b4 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php

/*
 *  Modul za pripravo izvoza naprednih parapodatkov
 *
 */


class SurveyAdvancedParadataExport {

    var $anketa;			// ID ankete
    
    var $limit = 100;       // Max vrstic pri izpisu

	
	function __construct($anketa){
		global $site_url;

		// Ce imamo anketo
		if ((int)$anketa > 0){
			$this->anketa = $anketa;
		}
	}
	
	
	// Izvozimo ustrezno tabelo v csv
	public function exportTable($table_name='srv_advanced_paradata_page'){
		global $site_path;
    
        ini_set('memory_limit', '4048M');

		// Dobimo naslove stolpcev
		$header = $this->getHeader($table_name);
		
		
		// Pripravimo datoteko za izvoz
		$file = $site_path.'admin/survey/modules/mod_advanced_paradata/temp/'.$table_name.'_'.$this->anketa.'.csv';
		$fd = fopen($file, "w");

		$convertTypes = array('charSet'	=> 'windows-1250',
							'delimit'	=> ',',
							'newLine'	=> "\n",
							'BOMchar'	=> "\xEF\xBB\xBF");
		# dodamo boomchar za utf-8
		fwrite($fd, $convertTypes['BOMchar']);
		
		// Zapisemo header row
		$header_line = '';
		foreach($header as $col){
			$header_line .= $col.',';
		}
		$header_line = substr($header_line, 0, -1);
		fwrite($fd, $header_line."\r\n");

        // Zapisemo vsako vrstico posebej
        // Dobimo vrstice s podatki
		switch($table_name){

			case 'srv_advanced_paradata_question':
				$data = $this->writeQuestionParadata($fd, $header);
				break;
			
			case 'srv_advanced_paradata_vrednost':
				$data = $this->writeVrednostParadata($fd, $header);
				break;

			case 'srv_advanced_paradata_other':
				$data = $this->writeOtherParadata($fd, $header);
                break;
                
            case 'srv_advanced_paradata_movement':
				$data = $this->writeMovementParadata($fd, $header);
                break;
                			
			case 'srv_advanced_paradata_alert':
				$data = $this->writeAlertParadata($fd, $header);
				break;

			default:
				$data = $this->writePageParadata($fd, $header);
				break;
		}
					
		fclose($fd);


		// Pripravimo file za download
		if(file_exists($file)){
			
			header('Content-Description: File Transfer');
			//header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename='.basename(''.$table_name.'_'.$this->anketa.'.csv'.''));
			
			header("Content-type: text/x-csv; charset=utf-8");
			//header("Content-type: text/csv");
			
			header('Content-Transfer-Encoding: binary');
			header('Expires: 0');
			header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
			header('Pragma: public');
			header('Content-Length: ' . filesize($file));
			
			ob_clean();
			flush();
			
			readfile($file);
		}
		
		// Na koncu pobrisemo zacasne datoteke
		if (file_exists($file)) {
			unlink($file);
		}
		
		// Ugasnemo skripto:)
		die();
	}


	// Izpisemo tabelo parapodatkov vezanih na strani
	public function displayPageTable(){
		global $lang;
		
		$data = $this->getPageParadata();
		
		echo '<table class="advanced_paradata_table page_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>ID</th>';
		echo '<th>User ID</th>';
		echo '<th>Page ID</th>';
		echo '<th>Recnum</th>';
		echo '<th>Load time</th>';
		echo '<th>Post time</th>';
		echo '<th>User Agent</th>';
		echo '<th>Device pixel ratio</th>';
		echo '<th>width x height</th>';
		echo '<th>availWidth x availHeight</th>';
		echo '<th>jQuery windowWidth x windowHeight</th>';
		echo '<th>jQuery documentWidth x documentHeight</th>';
		echo '<th>Language</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['id'].'</td>';
			echo '<td>'.$row['usr_id'].'</td>';
			echo '<td>'.$row['gru_id'].'</td>';
			echo '<td>'.$row['recnum'].'</td>';
			echo '<td>'.$row['load_time'].'</td>';
			echo '<td>'.$row['post_time'].'</td>';
			echo '<td>'.$row['user_agent'].'</td>';
			echo '<td>'.$row['devicePixelRatio'].'</td>';
			echo '<td>'.$row['width'].'px X '.$row['height'].'px</td>';
			echo '<td>'.$row['availWidth'].'px X '.$row['availHeight'].'px</td>';
			echo '<td>'.$row['jquery_windowW'].'px X '.$row['jquery_windowH'].'px</td>';
			echo '<td>'.$row['jquery_documentW'].'px X '.$row['jquery_documentH'].'px</td>';
			echo '<td>'.$row['language'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
	}
	// Izpisemo tabelo parapodatkov vezanih na vprasanja
	public function displayQuestionTable(){
		
		$data = $this->getQuestionParadata();
		
		echo '<table class="advanced_paradata_table question_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>Page session ID</th>';
		echo '<th>Question ID</th>';
		echo '<th>Order</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['page_id'].'</td>';
			echo '<td>'.$row['spr_id'].'</td>';
			echo '<td>'.$row['vre_order'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
	}
	
	// Izpisemo tabelo parapodatkov vezanih na vredosti
	public function displayVrednostTable(){
		
		$data = $this->getVrednostParadata();
		
		echo '<table class="advanced_paradata_table vrednost_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>Page session ID</th>';
		echo '<th>Question ID</th>';
		echo '<th>Vrednost ID</th>';
		echo '<th>Time</th>';
		echo '<th>Event</th>';
		echo '<th>Value</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['page_id'].'</td>';
			echo '<td>'.$row['spr_id'].'</td>';
			echo '<td>'.$row['vre_id'].'</td>';
			echo '<td>'.$row['time'].'</td>';
			echo '<td>'.$row['event'].'</td>';
			echo '<td>'.$row['value'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
	}
	
	// Izpisemo tabelo ostalih parapodatkov
	public function displayOtherTable(){
		
		$data = $this->getOtherParadata();
		
		echo '<table class="advanced_paradata_table other_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>Page session ID</th>';
		echo '<th>Time</th>';
		echo '<th>Event</th>';
		echo '<th>Value</th>';
		echo '<th>Position</th>';
		echo '<th>Element type</th>';
		echo '<th>Element id</th>';
		echo '<th>Element class</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['page_id'].'</td>';
			echo '<td>'.$row['time'].'</td>';
			echo '<td>'.$row['event'].'</td>';
			echo '<td>'.$row['value'].'</td>';
			echo '<td>X: '.$row['pos_x'].', Y: '.$row['pos_y'].'</td>';
			echo '<td>'.$row['div_type'].'</td>';
			echo '<td>'.$row['div_id'].'</td>';
			echo '<td>'.$row['div_class'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
    }
    
    // Izpisemo tabelo premikov miske
	public function displayMovementTable(){
		
		$data = $this->getMovementParadata();
		
		echo '<table class="advanced_paradata_table movement_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>Page session ID</th>';
		echo '<th>Time start</th>';
		echo '<th>Time end</th>';
		echo '<th>Position start</th>';
		echo '<th>Position end</th>';
		echo '<th>Distance traveled</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['page_id'].'</td>';
			echo '<td>'.$row['time_start'].'</td>';
			echo '<td>'.$row['time_end'].'</td>';
			echo '<td>X: '.$row['pos_x_start'].', Y: '.$row['pos_y_start'].'</td>';
			echo '<td>X: '.$row['pos_x_end'].', Y: '.$row['pos_y_end'].'</td>';
			echo '<td>'.$row['distance'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
	}
	
	// Izpisemo tabelo ostalih parapodatkov
	public function displayAlertTable(){
		
		$data = $this->getAlertParadata();
		
		echo '<table class="advanced_paradata_table alert_paradata styled_table">';
			
		echo '<tr>';
		echo '<th>Page session ID</th>';
		echo '<th>Display time</th>';
		echo '<th>Close time</th>';
		echo '<th>Type</th>';
		echo '<th>Trigger ID</th>';
		echo '<th>Trigger type</th>';
		echo '<th>Ignorable</th>';
		echo '<th>Alert text</th>';
		echo '<th>User action</th>';
		echo '</tr>';
		
		foreach($data as $row){
			
			echo '<tr>';
			
			echo '<td>'.$row['page_id'].'</td>';
			echo '<td>'.$row['time_display'].'</td>';
			echo '<td>'.$row['time_close'].'</td>';
			echo '<td>'.$row['type'].'</td>';
			echo '<td>'.$row['trigger_id'].'</td>';
			echo '<td>'.$row['trigger_type'].'</td>';
			echo '<td>'.$row['ignorable'].'</td>';
			echo '<td>'.$row['text'].'</td>';
			echo '<td>'.$row['action'].'</td>';
			
			echo '</tr>';
		}
		
		echo '</table>';
	}
	
	
	// Pridobimo naslove parapodatkov vezane na strani
	private function getHeader($table_name='srv_advanced_paradata_page'){

		$header = array();
		
		$sql = sisplet_query("SHOW columns FROM ".$table_name."");
		while($row = mysqli_fetch_array($sql)){
			$header[] = $row['Field'];
		}

		return $header;
	}

	// Pridobimo parapodatke vezane na strani
	private function getPageParadata($all=false){

		$data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;

		$sql = sisplet_query("SELECT * FROM srv_advanced_paradata_page 
                                WHERE ank_id='".$this->anketa."' 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
	}

	// Pridobimo parapodatke vezane na vprasanja
	private function getQuestionParadata($all=false){
		
        $data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;
		
		$sql = sisplet_query("SELECT q.* FROM srv_advanced_paradata_question q, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND q.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
	}
	
	// Pridobimo parapodatke vezane na vrednosti v vprasanju
	private function getVrednostParadata($all=false){
		
        $data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;
		
		$sql = sisplet_query("SELECT v.* FROM srv_advanced_paradata_vrednost v, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND v.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
	}
	
	// Pridobimo ostale parapodatke
	private function getOtherParadata($all=false){
		
		$data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;

		$sql = sisplet_query("SELECT o.* FROM srv_advanced_paradata_other o, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND o.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
    }
    
    // Pridobimo parapodatke premikov miske
	private function getMovementParadata($all=false){
		
		$data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;

		$sql = sisplet_query("SELECT m.* FROM srv_advanced_paradata_movement m, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND m.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
	}
	
	// Pridobimo parapodatke alertov
	private function getAlertParadata($all=false){
		
		$data = array();
        
        $limit = $all ? '' : ' LIMIT '.$this->limit;

		$sql = sisplet_query("SELECT a.* FROM srv_advanced_paradata_alert a, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND a.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
			$data[] = $row;
		}
		
		return $data;
    }
    

    // Zapisemo v datoteko parapodatke vezane na strani
	private function writePageParadata($fd, $header){

		$sql = sisplet_query("SELECT * FROM srv_advanced_paradata_page 
                                WHERE ank_id='".$this->anketa."' 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
  
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
	}

	// Zapisemo v datoteko parapodatke vezane na vprasanja
	private function writeQuestionParadata($fd, $header){

		$sql = sisplet_query("SELECT q.* FROM srv_advanced_paradata_question q, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND q.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
            
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
	}
	
	// Zapisemo v datoteko parapodatke vezane na vrednosti v vprasanju
	private function writeVrednostParadata($fd, $header){
		
		$sql = sisplet_query("SELECT v.* FROM srv_advanced_paradata_vrednost v, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND v.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
            
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
	}
	
	// Zapisemo v datoteko ostale parapodatke
	private function writeOtherParadata($fd, $header){
		
		$sql = sisplet_query("SELECT o.* FROM srv_advanced_paradata_other o, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND o.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
            
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
    }
    
    // Zapisemo v datoteko parapodatke premikov miske
	private function writeMovementParadata($fd, $header){
		
		$sql = sisplet_query("SELECT m.* FROM srv_advanced_paradata_movement m, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND m.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
            
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
	}
	
	// Zapisemo v datoteko parapodatke alertov
	private function writeAlertParadata($fd, $header){
		
		$sql = sisplet_query("SELECT a.* FROM srv_advanced_paradata_alert a, srv_advanced_paradata_page p 
								WHERE p.ank_id='".$this->anketa."' AND a.page_id=p.id 
                                ORDER BY id DESC ".$limit."");
		while($row = mysqli_fetch_array($sql)){
            
            $data_line = '';
            foreach($header as $col){
                $data_line .= '\''.$row[$col].'\',';
            }
            $data_line = substr($data_line, 0, -1);
            
            fwrite($fd, $data_line."\r\n");
		}
	}
}