summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveyParaGraph.php
blob: 891af1b64a88a33e64c7f00987254e5578d8f40b (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
<?php

class SurveyParaGraph{

	private $anketa;									# id ankete
	private $paraGraph_filter = array();				# Filtriranje parapodatkov (po napravi ali po statusu)
	
	function __construct($anketa){
		global $global_user_id;
		
		if((int)$anketa > 0){			
			$this->anketa = $anketa;
			
			SurveyStatusProfiles :: Init($this->anketa);		
		} 
		else{
			echo 'Invalid Survey ID!';
			exit();
		}
	}
        
    function setParaGraphFilter($pgf) { 

        $this->paraGraph_filter = $pgf; 
    }
	
	function DisplayParaGraph(){
		global $lang;
		global $site_path;
		global $admin_type;

		// Nastavimo filter
		$this->paraGraphSetFilter();
		
		// Popravimo stare ankete -> v bazo vnesemo browser, os, device, popravljen js
		$this->paraGraphFixOld();
		
		// Zberemo podatke vseh userjev
		$paraData = $this->collectParaGraphDataNew();
					
		
		echo '<p>'.$lang['srv_para_graph_text'].'</p>';
		
		
		// PC, tablica, mobi
		echo '<fieldset><legend>'.$lang['srv_para_graph_device'].'</legend>';		

		// Filter po napravi
		echo '<div style="margin:5px 0 15px 5px;">';
		echo '<label>'.$lang['srv_analiza_filter'].': </label>';
		echo '<label for="paraGraph_filter_pc"><input type="checkbox" id="paraGraph_filter_pc" '.($this->paraGraph_filter['pc']==1 ? ' checked="checked"' : '').' onClick="changeParaGraphFilter();">'.$lang['srv_para_graph_device0'].'</label>';
		echo ' <label for="paraGraph_filter_mobi"><input type="checkbox" id="paraGraph_filter_mobi" '.($this->paraGraph_filter['mobi']==1 ? ' checked="checked"' : '').' onClick="changeParaGraphFilter();">'.$lang['srv_para_graph_device1'].'</label>';
		echo ' <label for="paraGraph_filter_tablet"><input type="checkbox" id="paraGraph_filter_tablet" '.($this->paraGraph_filter['tablet']==1 ? ' checked="checked"' : '').' onClick="changeParaGraphFilter();">'.$lang['srv_para_graph_device2'].'</label>';
		echo ' <label for="paraGraph_filter_robot"><input type="checkbox" id="paraGraph_filter_robot" '.($this->paraGraph_filter['robot']==1 ? ' checked="checked"' : '').' onClick="changeParaGraphFilter();">'.$lang['srv_para_graph_device3'].'</label>';
		echo '&nbsp;&nbsp;&nbsp;<label>('.$lang['srv_para_graph_filteredCnt'].' '.$paraData['allCount'].')</label>';
		echo '</div>';
	
		
		echo '<table style="width:100%">';
		
		echo '<tr>';
		echo '<th nowrap>'.$lang['srv_para_graph_device0'].'</th>';
		echo '<td style="width:100%">';
		echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['pcCount']/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
		echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['pcCount'].'</span></span>';
		echo '</td>';
		echo '</tr>';
		
		echo '<tr>';
		echo '<th nowrap>'.$lang['srv_para_graph_device1'].'</th>';
		echo '<td style="width:100%">';
		echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['mobiCount']/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
		echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['mobiCount'].'</span></span>';
		echo '</td>';		
		echo '</tr>';
		
		echo '<tr>';
		echo '<th nowrap>'.$lang['srv_para_graph_device2'].'</th>';
		echo '<td style="width:100%">';
		echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['tabletCount']/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
		echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['tabletCount'].'</span></span>';
		echo '</td>';
		echo '</tr>';
		
		// Zaenkrat nimamo detekcije robotov, ker se uporablja browscap lite
		/*echo '<tr>';
		echo '<th nowrap>'.$lang['srv_para_graph_device3'].'</th>';
		echo '<td style="width:100%">';
		echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['robotCount']/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
		echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['robotCount'].'</span></span>';
		echo '</td>';		
		echo '</tr>';

		//echo '<tr><td colspan="3" style="border-bottom:1px solid #E4E4F9"></td></tr>';
		//echo '<tr><td></td><th style="text-align:left; padding-right: 20px" nowrap>'.$lang['srv_anl_suma1'].': '.$paraData['allCount'].'</th></tr>';*/
		
		echo '</table>';
		
        echo '</fieldset>';
        
		
		// Browser		
		echo '<fieldset><legend>'.$lang['srv_para_graph_browser'].'</legend>';		

		echo '<table style="width:100%">';

		if(count($paraData['browser']) > 0){
			if(count($paraData['browser']) > 1)
				ksort($paraData['browser'], SORT_REGULAR);
			
			foreach($paraData['browser'] as $key => $browserCnt){
							
				if($key != $lang['srv_para_graph_other_slo'] && $key != $lang['srv_para_graph_other_ang']){
					echo '<tr>';
					echo '<th nowrap>'.$key.'</th>';
					echo '<td style="width:100%">';
					echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $browserCnt/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
					echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$browserCnt.'</span></span>';
					echo '</td>';
					echo '</tr>';	
				}
			}
		
			if(isset($paraData['browser'][$lang['srv_para_graph_other_slo']]) && $paraData['browser'][$lang['srv_para_graph_other_slo']] > 0){
				echo '<tr>';
				echo '<th nowrap>'.$lang['srv_para_graph_other'].'</th>';
				echo '<td style="width:100%">';
				echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['browser'][$lang['srv_para_graph_other_slo']]/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
				echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['browser'][$lang['srv_para_graph_other_slo']].'</span></span>';
				echo '</td>';
				echo '</tr>';
			}
			
			if(isset($paraData['browser'][$lang['srv_para_graph_other_ang']]) && $paraData['browser'][$lang['srv_para_graph_other_ang']] > 0){
				echo '<tr>';
				echo '<th nowrap>'.$lang['srv_para_graph_other'].'</th>';
				echo '<td style="width:100%">';
				echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['browser'][$lang['srv_para_graph_other_ang']]/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
				echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['browser'][$lang['srv_para_graph_other_ang']].'</span></span>';
				echo '</td>';
				echo '</tr>';
			}
			
			//echo '<tr><td colspan="3" style="border-bottom:1px solid #E4E4F9"></td></tr>';
			//echo '<tr><td></td><th style="text-align:left; padding-right: 20px" nowrap>'.$lang['srv_anl_suma1'].': '.$paraData['allCount'].'</th></tr>';
		}
		
		echo '</table>';
		
		echo '</fieldset>';
		
		
		// Operacijski sistem		
		echo '<fieldset><legend>'.$lang['srv_para_graph_os'].'</legend>';		

		echo '<table style="width:100%">';

		if(count($paraData['os']) > 0){		
			if(count($paraData['os']) > 1)
				ksort($paraData['os'], SORT_REGULAR);
			
			foreach($paraData['os'] as $key => $osCnt){
				
				if($key != $lang['srv_para_graph_other_slo'] && $key != $lang['srv_para_graph_other_ang']){
					echo '<tr>';
					echo '<th nowrap>'.$key.'</th>';
					echo '<td style="width:100%">';
					echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $osCnt/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
					echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$osCnt.'</span></span>';
					echo '</td>';
					echo '</tr>';
				}
			}
			
			if(isset($paraData['os'][$lang['srv_para_graph_other_slo']]) && $paraData['os'][$lang['srv_para_graph_other_slo']] > 0){
				echo '<tr>';
				echo '<th nowrap>'.$lang['srv_para_graph_other'].'</th>';
				echo '<td style="width:100%">';
				echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['os'][$lang['srv_para_graph_other_slo']]/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
				echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['os'][$lang['srv_para_graph_other_slo']].'</span></span>';
				echo '</td>';
				echo '</tr>';
			}
			
			if(isset($paraData['os'][$lang['srv_para_graph_other_ang']]) && $paraData['os'][$lang['srv_para_graph_other_ang']] > 0){
				echo '<tr>';
				echo '<th nowrap>'.$lang['srv_para_graph_other'].'</th>';
				echo '<td style="width:100%">';
				echo '  <div class="graph_lb" style="text-align: right; float: left; width: '.($paraData['allCount']>0 ? $paraData['os'][$lang['srv_para_graph_other_ang']]/$paraData['allCount']*85 : '0').'%">&nbsp;</div>';
				echo '  <span style="display: block; margin: auto auto auto 5px; float: left">'.$paraData['os'][$lang['srv_para_graph_other_ang']].'</span></span>';
				echo '</td>';
				echo '</tr>';
			}
			
			//echo '<tr><td colspan="3" style="border-bottom:1px solid #E4E4F9"></td></tr>';
			//echo '<tr><td></td><th style="text-align:left; padding-right: 20px" nowrap>'.$lang['srv_anl_suma1'].': '.$paraData['allCount'].'</th></tr>';
		}
		
		echo '</table>';
		
		echo '</fieldset>';
	}

	
	function collectParaGraphData(){
		global $lang;
		
		SurveySetting::getInstance()->Init($this->anketa);
		
		// Preberemo tabelo s podatki za izbrane filtre (ce ze obstaja)
		$filterString = implode('_', $this->paraGraph_filter);
		$paraData = unserialize(SurveySetting::getInstance()->getSurveyMiscSetting('para_graph_data_'.$filterString));
		
		// Pogledamo kdaj je bila kreirana datoteka (ce imamo nove podatke)
		$sqlTime = sisplet_query("SELECT UNIX_TIMESTAMP(last_update) AS last_update FROM srv_data_files WHERE sid='".$this->anketa."'");
		$rowTime = mysqli_fetch_array($sqlTime);
		$time = $rowTime['last_update'];

		$status = ($this->paraGraph_filter['status'] == 1) ? ' AND last_status>\'4\'' : ' AND last_status>\'2\'';
		$sqlu = sisplet_query("SELECT useragent FROM srv_user WHERE ank_id='".$this->anketa."' ".$status." AND preview='0' AND deleted='0'");
		
		// Ce se nimamo shranjenih izracunov (timestamp datoteke se ne ujema), racunamo na novo
		if(!isset($paraData['allCount']) || $paraData['timestamp'] != $time || $_GET['refresh'] == 1){

			$sqlu2 = sisplet_query("SELECT id FROM srv_user WHERE ank_id='".$this->anketa."' AND last_status>'2' AND u.preview='0' AND u.deleted='0'");
			
			$paraData = array(
				'timestamp'			=> $time,
				'unfilteredCount'	=> mysqli_num_rows($sqlu2),
				'allCount' 			=> 0, 
				'pcCount' 			=> 0, 
				'mobiCount' 		=> 0, 
				'tabletCount' 		=> 0, 
				'robotCount' 		=> 0, 
				'browser' 			=> array(),
				'os' 				=> array()
			);
								
			$detect = New Mobile_Detect();

			// Loop cez vse ustrezne respondente
			while($rowu = mysqli_fetch_array($sqlu)){
				
				// Detect mobilnikov in tablic
				$detect->setUserAgent($rowu['useragent']);
					
				// Detect z browscap	
				$browserDetect = get_browser($rowu['useragent'], true);
				
				// Filtriranje po napravi
				if(  ($this->paraGraph_filter['pc'] != 0 || $detect->isMobile() || $browserDetect['crawler'] == 1)
						&& ($this->paraGraph_filter['tablet'] != 0 || !$detect->isTablet())
						&& ($this->paraGraph_filter['mobi'] != 0 || !$detect->isMobile() || $detect->isTablet())
						&& ($this->paraGraph_filter['robot'] != 0 || $browserDetect['crawler'] != 1) ){
						
					// Naprava
					if($detect->isMobile()) {			
						if($detect->isTablet())
							$paraData['tabletCount']++;
						else
							$paraData['mobiCount']++;
					}
					elseif($browserDetect['crawler'] == 1)
						$paraData['robotCount']++;
					else
						$paraData['pcCount']++;
										
					// Browser
					if($browserDetect['browser'] == 'Default Browser')
						$browser = $lang['srv_para_graph_other'];
					else
						$browser = $browserDetect['browser'].' '.$browserDetect['version'];
					$paraData['browser'][$browser]++;
					
					// OS
					if($browserDetect['platform'] == 'unknown')
						$os = $lang['srv_para_graph_other'];
					else
						$os = $browserDetect['platform'];
					
					$paraData['os'][$os]++;
					
			
					$paraData['allCount']++;
				}
			}
			
			// Na koncu shranimo nove izracune v bazo	
			SurveySetting::getInstance()->setSurveyMiscSetting('para_graph_data_'.$filterString, serialize($paraData));
		}
		
		return $paraData;
	}
	
	function collectParaGraphDataNew(){
		global $lang;
				
		$paraData = array(
			/*'timestamp'			=> $time,*/
			'unfilteredCount'	=> 0,
			'allCount' 			=> 0, 
			'pcCount' 			=> 0, 
			'mobiCount' 		=> 0, 
			'tabletCount' 		=> 0, 
			'robotCount' 		=> 0, 
			'browser' 			=> array(),
			'os' 				=> array()
		);
			

		// Filter za status
		$status_filter = ($this->paraGraph_filter['status'] == 1) ? ' AND last_status>\'4\' AND lurker=\'0\'' : ' AND last_status>\'2\'';
		
		// Filter za napravo
		$device_filter = ' AND (';	
		$device_filter .= ($this->paraGraph_filter['pc'] != 0) ? 'device=\'0\' OR ' : '';
		$device_filter .= ($this->paraGraph_filter['mobi'] != 0) ? 'device=\'1\' OR ' : '';
		$device_filter .= ($this->paraGraph_filter['tablet'] != 0) ? 'device=\'2\' OR ' : '';
		$device_filter .= ($this->paraGraph_filter['robot'] != 0) ? 'device=\'3\' OR ' : '';
		$device_filter = substr($device_filter, 0, -4) . ')';

	
		// Prestejemo vse
		$sql = sisplet_query("SELECT id FROM srv_user WHERE ank_id='".$this->anketa."' AND last_status>'2' AND useragent!='' AND preview='0' AND deleted='0'");
		$paraData['unfilteredCount'] = mysqli_num_rows($sql);
		
		// Prestejemo vse filtrirane
		$sql = sisplet_query("SELECT id FROM srv_user WHERE ank_id='".$this->anketa."' ".$status_filter." ".$device_filter." AND useragent!='' AND preview='0' AND deleted='0'");
		$paraData['allCount'] = mysqli_num_rows($sql);
					
		// Prestejemo naprave
		$sql = sisplet_query("SELECT device, count(*) FROM srv_user WHERE ank_id='".$this->anketa."' ".$status_filter." ".$device_filter." AND useragent!='' AND preview='0' AND deleted='0'
							GROUP BY device");
		while($row = mysqli_fetch_array($sql)){			
			if($row['device'] == 0)
				$paraData['pcCount'] = $row['count(*)'];
			elseif($row['device'] == 1)
				$paraData['mobiCount'] = $row['count(*)'];
			elseif($row['device'] == 2)
				$paraData['tabletCount'] = $row['count(*)'];
			elseif($row['device'] == 3)
				$paraData['robotCount'] = $row['count(*)'];
		}		
				
		// Prestejemo browserje
		$sql = sisplet_query("SELECT browser, count(*) FROM srv_user WHERE ank_id='".$this->anketa."' ".$status_filter." ".$device_filter." AND useragent!='' AND preview='0' AND deleted='0'
							AND browser!='' GROUP BY browser");
		while($row = mysqli_fetch_array($sql)){
			$paraData['browser'][$row['browser']] = $row['count(*)'];
		}
			
		// Prestejemo os
		$sql = sisplet_query("SELECT os, count(*) FROM srv_user WHERE ank_id='".$this->anketa."' ".$status_filter." ".$device_filter." AND useragent!='' AND preview='0' AND deleted='0'
							AND os!='' GROUP BY os");
		while($row = mysqli_fetch_array($sql)){
			$paraData['os'][$row['os']] = $row['count(*)'];
		}	

		
		return $paraData;
	}
	
	
	function paraGraphSetFilter(){
		
		// Nastavimo filter po statusu (vsi ali ustrezni)
		$this->paraGraph_filter['status'] = (SurveyStatusProfiles::getDefaultProfile() == 2) ? 1 : 0; 
		/*if(isset($_GET['status']))
			$this->paraGraph_filter['status'] = $_GET['status'];
		else
			$this->paraGraph_filter['status'] = 0;*/
			
		// Nastavimo filter po napravi (pc, mobi, tablica, crawler)	
		$this->paraGraph_filter['pc'] = (isset($_GET['pc'])) ? $_GET['pc'] : 1;
		$this->paraGraph_filter['tablet'] = (isset($_GET['tablet'])) ? $_GET['tablet'] : 1;
		$this->paraGraph_filter['mobi'] = (isset($_GET['mobi'])) ? $_GET['mobi'] : 1;
		$this->paraGraph_filter['robot'] = (isset($_GET['robot'])) ? $_GET['robot'] : 1;
	}
	
	
	// Se pozene za stare ankete ki imajo v bazi samo useragent (samo prvic ko gledamo staro anketo) oz. ce hocemo na novo zracunat vse ($_GET['refresh'] == 1)
	function paraGraphFixOld(){
		global $lang;
		
		if(isset($_GET['refresh']) && $_GET['refresh'] == 1)
			$sqlu = sisplet_query("SELECT id, useragent FROM srv_user WHERE ank_id='".$this->anketa."' AND last_status>'2' AND useragent!='' AND preview='0' AND deleted='0'");
		else
			$sqlu = sisplet_query("SELECT id, useragent FROM srv_user WHERE ank_id='".$this->anketa."' AND last_status>'2' AND useragent!='' AND browser='' AND preview='0' AND deleted='0'");
		if(mysqli_num_rows($sqlu) > 0){
	
			$detect = New Mobile_Detect();
			
			$cnt = 0;
			while($rowu = mysqli_fetch_array($sqlu)){
				
				// Detect mobilnikov in tablic
				$detect->setUserAgent($rowu['useragent']);
					
				// Detect z browscap	
				$browserDetect = get_browser($rowu['useragent'], true);
											
				// Naprava
				if($detect->isMobile()) {			
					if($detect->isTablet())
						$device = 2;
					else
						$device = 1;
				}
				elseif($browserDetect['crawler'] == 1)
					$device = 3;
				else
					$device = 0;
								
				// Browser
				if($browserDetect['browser'] == 'Default Browser')
					$browser = $lang['srv_para_graph_other'];
				else
					$browser = $browserDetect['browser'].' '.$browserDetect['version'];
				
				// OS
				if($browserDetect['platform'] == 'unknown')
					$os = $lang['srv_para_graph_other'];
				else
					$os = $browserDetect['platform'];
				
				$sqlu2 = sisplet_query("UPDATE srv_user SET device='$device', browser='$browser', os='$os' WHERE ank_id='".$this->anketa."' AND id='$rowu[id]'");
				if (!$sqlu2)
					echo mysqli_error($GLOBALS['connect_db']);
					
				$cnt++;
			}
			
			echo '<div style="float:right; margin:-10px 20px 0 0;">Posodobljenih je bilo '.$cnt.' starih vnosov.</div>';
		}
	}
	
}