summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_advanced_paradata/class.SurveyAdvancedParadataLog.php
blob: acaef1f9d7d16f5fd547b504756bc4960c7b9f8e (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
<?php
	
/*
 *  Modul za beleženje naprednih parapodatkov med izpolnjevanjem ankete
 *
 */
 	
class SurveyAdvancedParadataLog {
	
	
	private static $instance = false;
	
	private $collectParadata = false;
	
	private $anketa = 0;
	private $session_id = 0;
	
	
	// Privatni construct, ki ga 1x poklice getInstance
	private function __construct () {
		
        if((isset($_GET['m']) && $_GET['m'] == 'quick_edit') || (isset($_GET['t']) && $_GET['t'] == 'postprocess'))
            return false;
            
		$anketa_hash = $_REQUEST['anketa'];		
		$this->anketa = getSurveyIdFromHash($anketa_hash);	
		
		if($this->anketa > 0){
			SurveyInfo::getInstance()->SurveyInit($this->anketa);
			$this->collectParadata = (SurveyInfo::getInstance()->checkSurveyModule('advanced_paradata')) ? true : false;
		}
	}
	
	// Vrne instanco classa - da mamo singleton
	public static function getInstance () {
		
		if (!self::$instance)
			self::$instance = new SurveyAdvancedParadataLog();
							
		return self::$instance;
	}
	
	// Vrne ce zbiramo napredne parapodatke
	public function paradataEnabled(){

        if((isset($_GET['m']) && $_GET['m'] == 'quick_edit') || (isset($_GET['t']) && $_GET['t'] == 'postprocess'))
            return false;

		return $this->collectParadata;
    }
    
    // Vrne ce zbiramo post time
	public function collectPostTime(){

        $collectPostTime = true;

        $sql = sisplet_query("SELECT collect_post_time FROM srv_advanced_paradata_settings WHERE ank_id='".$this->anketa."'");
		if(mysqli_num_rows($sql) > 0){	

            $row = mysqli_fetch_array($sql);
            
            if($row['collect_post_time'] == '0')
                $collectPostTime = false;
        }
        
        return $collectPostTime;
    }
	
	
	// Ustvarimo polje v bazi za session (vezan na load posamezne strani) in nastavimo session_id za js
	public function prepareLogging () {
				
		$user_agent = $_SERVER['HTTP_USER_AGENT'];
		
		// Vstavimo v bazo novo polje za session na strani
		$sql = sisplet_query("INSERT INTO srv_advanced_paradata_page (ank_id, load_time, user_agent) VALUES ('".$this->anketa."', NOW(3), '".$user_agent."')");
		
		if (!$sql){
			echo mysqli_error($GLOBALS['connect_db']);
		}
		else{
			// Nastavimo session_id
			$this->session_id = mysqli_insert_id($GLOBALS['connect_db']);
			
			// Nastavimo session_id se za JS
			echo '<script> var _session_id = '.$this->session_id.'; </script>';
		}
	}
	
	// Zapiše log v bazo
	public function logData ($event_type, $event, $data) {
		
		switch ($event_type) {
			
			case 'page':	
				$this->logDataPage($event, $data);		
				break;
			
			case 'question':	
				$this->logDataQuestion($event, $data);		
				break;
				
			case 'vrednost':	
				$this->logDataVrednost($event, $data);		
				break;
				
			case 'other':	
				$this->logDataOther($event, $data);		
                break;
                
            case 'movement':	
				$this->logDataMovement($event, $data);		
				break;
				
			case 'alert':	
				$this->logDataAlert($event, $data);		
				break;
		}
	}
	
	// Zabelezimo dogodek na nivoju strani
	private function logDataPage($event, $data){

		$update = '';
		
		// Nastavimo katere parametre updatamo
		switch($event){
			case 'load_page':
				$update = " gru_id = '".$data['page']."', 
							usr_id = '".$data['usr_id']."', 
							recnum = '".$data['recnum']."', 
							language = '".$data['language']."', 
							load_time = '".$data['timestamp']."', 
							devicePixelRatio = '".$data['data']['devicePixelRatio']."', 
							width = '".$data['data']['width']."', 
							height = '".$data['data']['height']."', 
							availWidth = '".$data['data']['availWidth']."', 
							availHeight = '".$data['data']['availHeight']."', 
							jquery_windowW = '".$data['data']['jquery_windowW']."', 
							jquery_windowH = '".$data['data']['jquery_windowH']."', 
							jquery_documentW = '".$data['data']['jquery_documentW']."', 
							jquery_documentH = '".$data['data']['jquery_documentH']."'";
				break;
				
			case 'unload_page':
				$update = " post_time='".$data['timestamp']."' ";
				break;
		}	
		
		$sql = sisplet_query("UPDATE srv_advanced_paradata_page SET ".$update." WHERE id='$this->session_id'");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
		
		/*var_dump($data);
		echo "UPDATE srv_advanced_paradata_page SET ".$update." WHERE id='$this->session_id'";*/
	}
	
	// Zabelezimo dogodek na nivoju vprasanja
	private function logDataQuestion($event, $data){
		
		// Preverimo, ce gre ya vprasanje v ifu - potem se preveri da se zapise samo 1x
		$sqlU = sisplet_query("SELECT p.id
								FROM srv_advanced_paradata_page p, srv_advanced_paradata_question q
								WHERE p.usr_id='".$data['usr_id']."' AND p.id=q.page_id AND q.spr_id='".$data['data']['spr_id']."'
							");

		// Ce se nimamo vnosa za vprasanje in userja zapisemo
		if(mysqli_num_rows($sqlU) == 0){
		
			$sql = sisplet_query("INSERT INTO srv_advanced_paradata_question 
								(page_id, spr_id, vre_order) 
								VALUES 
								('".$this->session_id."', '".$data['data']['spr_id']."', '".$data['data']['vre_order']."')");
			if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
		}
	}
	
	// Zabelezimo dogodek na nivoju vrednosti vprasanja
	private function logDataVrednost($event, $data){

		$value = isset($data['data']['value']) ? $data['data']['value'] : '';
			
		$sql = sisplet_query("INSERT INTO srv_advanced_paradata_vrednost 
								(page_id, spr_id, vre_id, time, event, value) 
								VALUES 
								('".$this->session_id."', '".$data['data']['spr_id']."', '".$data['data']['vre_id']."', '".$data['timestamp']."', '".$event."', '".$value."')");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);	
	}
	
	// Zabelezimo ostale dogodke
	private function logDataOther($event, $data){
		
		$value = isset($data['data']['value']) ? $data['data']['value'] : '';
		$pos_x = isset($data['data']['pos_x']) ? $data['data']['pos_x'] : '';
		$pos_y = isset($data['data']['pos_y']) ? $data['data']['pos_y'] : '';
		$div_type = isset($data['data']['div_type']) ? $data['data']['div_type'] : '';
		$div_id = isset($data['data']['div_id']) ? $data['data']['div_id'] : '';
		$div_class = isset($data['data']['div_class']) ? $data['data']['div_class'] : '';
		
		$sql = sisplet_query("INSERT INTO srv_advanced_paradata_other 
								(page_id, time, event, value, pos_x, pos_y, div_type, div_id, div_class) 
								VALUES 
								('".$this->session_id."', '".$data['timestamp']."', '".$event."', '".$value."', '".$pos_x."', '".$pos_y."', '".$div_type."', '".$div_id."', '".$div_class."')");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
	}
    
    // Zabelezimo premike miske
	private function logDataMovement($event, $data){
        
        $time_start_raw = mysqli_real_escape_string($GLOBALS['connect_db'], $data['data']['time_start']);
        $time_start = date("Y-m-d H:i:s", $time_start_raw/1000).'.'.substr($time_start_raw, -3);	

        $time_end_raw = mysqli_real_escape_string($GLOBALS['connect_db'], $data['data']['time_end']);
        $time_end = date("Y-m-d H:i:s", $time_end_raw/1000).'.'.substr($time_end_raw, -3);	
        
		$pos_x_start = isset($data['data']['pos_x_start']) ? $data['data']['pos_x_start'] : '';
		$pos_y_start = isset($data['data']['pos_y_start']) ? $data['data']['pos_y_start'] : '';
		$pos_x_end = isset($data['data']['pos_x_end']) ? $data['data']['pos_x_end'] : '';
        $pos_y_end = isset($data['data']['pos_y_end']) ? $data['data']['pos_y_end'] : '';
        $distance = isset($data['data']['distance']) ? $data['data']['distance'] : '';
		
		$sql = sisplet_query("INSERT INTO srv_advanced_paradata_movement
								(page_id, time_start, time_end, pos_x_start, pos_y_start, pos_x_end, pos_y_end, distance) 
								VALUES 
								('".$this->session_id."', '".$time_start."', '".$time_end."', '".$pos_x_start."', '".$pos_y_start."', '".$pos_x_end."', '".$pos_y_end."', '".$distance."')");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
    }
    
	// Zabelezimo alerte
	private function logDataAlert($event, $data){
		
		$type = isset($data['data']['type']) ? $data['data']['type'] : '';
		$trigger_id = isset($data['data']['trigger_id']) ? $data['data']['trigger_id'] : 0;
		$trigger_type = isset($data['data']['trigger_type']) ? $data['data']['trigger_type'] : '';
		$ignorable = isset($data['data']['ignorable']) ? $data['data']['ignorable'] : 0;
		$text = isset($data['data']['text']) ? $data['data']['text'] : '';
		$action = isset($data['data']['action']) ? $data['data']['action'] : '';
		
		$timestamp_display_raw = isset($data['data']['time_display']) ? $data['data']['time_display'] : '';
		$timestamp_display = date("Y-m-d H:i:s", $timestamp_display_raw/1000).'.'.substr($timestamp_display_raw, -3);	

		$sql = sisplet_query("INSERT INTO srv_advanced_paradata_alert 
								(page_id, time_display, time_close, type, trigger_id, trigger_type, ignorable, text, action) 
								VALUES 
								('".$this->session_id."', '".$timestamp_display."', '".$data['timestamp']."', '".$type."', '".$trigger_id."', '".$trigger_type."', '".$ignorable."', '".$text."', '".$action."')");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
	}
	
	
	// Izpise link na javascript datoteko v header htmlja
	public function linkJavaScript() {
		global $site_url;
        
		// Osnovni js za belezenje parapodatkov
        echo '  <script src="'.$site_url.'admin/survey/modules/mod_advanced_paradata/js/advanced_paradata.js"></script>'."\n";
        
        // Belezenje post tima (upocasni prehode cez strani)
        if($this->collectPostTime())
		    echo '  <script src="'.$site_url.'admin/survey/modules/mod_advanced_paradata/js/advanced_paradata_postTime.js"></script>'."\n";
		
		// JS za belezenje alertov
		echo '  <script src="'.$site_url.'admin/survey/modules/mod_advanced_paradata/js/sledenjeOpozoril.js"></script>'."\n";
	}
	
	// Izpise trenutno grupo v JS
	public function displayGrupa ($grupa) {
			
		echo '<script> var srv_meta_grupa_id = '.$grupa.'; </script>';
	}
	
	
	// Ajax klici
	public function ajax() {

		if ($_GET['a'] == 'logData') {
			$this->ajax_logData();
		}
	}
	
	// Logiranje eventa
	private function ajax_logData () {

		$this->session_id = $_POST['session_id'];
		
		$event_type = mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['event_type']);
		$event = mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['event']);
		
		$timestamp_raw = mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['timestamp']);
		$timestamp = date("Y-m-d H:i:s", $timestamp_raw/1000).'.'.substr($timestamp_raw, -3);	
		
		$data_array = array(
			'page' 	=> mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['page']),
			'usr_id' 	=> mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['usr_id']),
			'recnum' 	=> mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['recnum']),
			'language' 	=> mysqli_real_escape_string($GLOBALS['connect_db'], $_POST['language']),
			'timestamp' => $timestamp,
			'data' 		=> $_POST['data']
		);

		var_dump($_POST);

		$this->logData($event_type, $event, $data_array);
	}
}
	
?>