summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_SPEEDINDEX/class.SurveySpeedIndex.php
blob: a15d70665d4e39e278474589dd2a02c0c03b9bc4 (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
<?php

define("TEMP_FOLDER", "admin/survey/modules/mod_SPEEDINDEX/temp");
define("SCRIPT_FOLDER", "admin/survey/modules/mod_SPEEDINDEX/R");
define("RESULTS_FOLDER", "admin/survey/modules/mod_SPEEDINDEX/results");

class SurveySpeedIndex{

	var $anketa;				# id ankete
	var $db_table = '';	

	
	function __construct($anketa){
		global $site_url;

		// Ce imamo anketo, smo v status->ul evealvacija
		if ((int)$anketa > 0){
			$this->anketa = $anketa;

			# polovimo vrsto tabel (aktivne / neaktivne)
			SurveyInfo :: getInstance()->SurveyInit($this->anketa);
			$this->db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();
		}
	}
	
	
	// Prikažemo stran
	public function displaySpeedIndex(){
		global $lang;
		
		// Izvedemo pripravo datoteke
		$this->executeExport();
		
		// Izrisemo tabelo
		$this->displaySpeedIndexTable();
	}
	
	// Prikazemo tabelo
	private function displaySpeedIndexTable(){
		global $site_path;
		global $lang;
		
		$result_folder = $site_path . RESULTS_FOLDER.'/';
			
		echo '<div id="speeder_table">';
		
		echo '<span class="bold">'.$lang['srv_speeder_index_text'].'</span>';
		
		// Legenda
		echo '<div class="speeder_leg">';
		echo '<span class="speeder_legend spaceLeft spaceRight" style="background-color:#ffffff;">'.$lang['srv_speeder_index_legend_0'].'</span>';
		echo '<span class="speeder_legend spaceLeft" style="background-color:#ffe8e8;">'.$lang['srv_speeder_index_legend_1'].'</span>';
		echo '</div>';

		echo '<table id="tbl_speeder">';
		
		if (($handle = fopen($result_folder."speederindex".$this->anketa.".csv", "r")) !== FALSE) {		
			// Loop po vrsticah
			$cnt=0;
			while (($row = fgetcsv($handle, 1000, ';')) !== FALSE) {
				
				$status = ($row[1] == 1 ? 'speeder' : 'no_speeder');
				
				echo '<tr class="'.$status.'">';
				
				// Prva vrstica
				if($cnt == 0){
					foreach($row as $val){
                        echo '<th>';
                        
                        // Prevedemo kar na roko:)
                        if($lang['id'] == '2'){
                            if($val == 'Index hitrosti')
                                echo $lang['srv_speeder_index'];
                            else
                                echo str_replace("Stran", $lang['page'], $val);
                        }
                        else
                            echo $val;
						
						echo '</th>';				
					}					
				}
				// Vrstice s podatki
				else{
					foreach($row as $val){
						echo '<td>';
						echo $val;
						echo '</td>';				
					}
				}
				
				echo '</tr>';
				
				$cnt++;
			}
			fclose($handle);
		}
			
		echo '</table></div>';
	}	
	
	
	// Zgeneriramo pdf analizo
	public function executeExport(){
		global $site_path;
		global $lang;	
		global $admin_type;

		// Zgeneriramo zacasne csv datoteke
		$this->prepareCSV();

		// Poklicemo R skripto in zgeneriramo pdf
		$script = $site_path . SCRIPT_FOLDER . '/speeder_index.R';
		$out = exec('Rscript '.$script.' '.$this->anketa.' 2>&1', $output, $return_var);
		
		// Testiranje - izpis errorjev
		/*if($admin_type == 0){
			echo '<div>';
			echo 'Rscript '.$script;
			//echo '<br />'.$out.'<br />';
			var_dump($output);
			echo '</div>';
		}*/
	
		// Na koncu pobrisemo zacasne datoteke
		$this->deleteTemp();
	}	
	
	// Pripravi csv s podatki o casih po straneh
	public function prepareCSV(){
		global $site_path;
		global $lang;	
		global $admin_type;
		
		$temp_folder = $site_path . TEMP_FOLDER.'/';
		
		$file_handler = fopen($temp_folder.'datum'.$this->anketa.'.csv',"w");
		
		
		// Prva vrstica
		$line_header = 'Id;Status;Lurker;Datum_0;';
		
		$grupe = array();
		$sql = sisplet_query("SELECT * FROM srv_grupa WHERE ank_id='".$this->anketa."' ORDER BY vrstni_red ASC");
		while ($row = mysqli_fetch_array($sql)) {
			$line_header .= 'Datum_'.$row['vrstni_red'].';';
			
			$grupe[$row['id']] = $row['vrstni_red'];
		}
		
		fwrite($file_handler, substr($line_header, 0, -1)."\r\n");		
		
		
		// Vrstice s podatki
		$sql = sisplet_query("SELECT id, recnum, last_status, lurker, time_insert FROM srv_user u
								WHERE ank_id='".$this->anketa."' AND preview='0' AND deleted='0'
								ORDER BY recnum ASC");
		while ($row = mysqli_fetch_array($sql)) {

			$line = $row['recnum'].';';
			$line .= $row['last_status'].';';
			$line .= $row['lurker'].';';
			$line .= $row['time_insert'].';';
			
			// Napolnimo case respondenta
			$user_grupe = array();
			$sqlG = sisplet_query("SELECT gru_id, time_edit FROM srv_user_grupa".$this->db_table."
								WHERE usr_id='".$row['id']."'");
			while ($rowG = mysqli_fetch_array($sqlG)) {
				$user_grupe[$rowG['gru_id']] = $rowG['time_edit'];
			}
			
			// Sprehodimo se po vseh straneh in zapisemo case v vrstico
			foreach($grupe as $gru_id => $vrstni_red){
				
				if(isset($user_grupe[$gru_id]))
					$line .= $user_grupe[$gru_id].';';
				else
					$line .= ';';
			}
			
			fwrite($file_handler, substr($line, 0, -1)."\r\n");	
		}
		
		
		fclose($file_handler);
	}	
	
	// Pobrisemo zacasne datoteke
	private function deleteTemp(){
		global $site_path;
		
		$temp_folder = $site_path . TEMP_FOLDER.'/';
		
		// Pobrisemo zacasno CSV datoteko s podatki
		if (file_exists($temp_folder.'/datum'.$this->anketa.'.csv')) {
			unlink($temp_folder.'/datum'.$this->anketa.'.csv');
		}
	}
	
}