summaryrefslogtreecommitdiffstats
path: root/admin/survey/modules/mod_advanced_timestamps/class.SurveyAdvancedTimestamps.php
blob: a6cc56ca8af8ef16f8c9f8fec32bcc678d0aa284 (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
<?php

/*
 *  Modul za pregledovanje in urejanje nastavitev naprednih časov po straneh
 *
 */


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


class SurveyAdvancedTimestamps {

	var $anketa;				# id ankete

	
	function __construct($anketa){

		// Ce imamo anketo
		if ((int)$anketa > 0){
			$this->anketa = $anketa;
		}
	}
	
	
	public function displaySettings(){
		global $lang;
               
		
		echo '<fieldset><legend>'.$lang['srv_results'].'</legend>';

		$href_csv = 'izvoz.php?m=advanced_timestamps_xls&anketa='.$this->anketa;
		echo ' <span><a href="'.$href_csv.'">Excel izvoz parapodatkov</a></span>';

		echo '</fieldset>';	
    }
    
	// Izvozimo tabelo s parapodatki v xls
	public function exportTable(){
		global $site_path;
    
        ini_set('memory_limit', '4048M');


		// Pridobimo podatke za vse tabele
		$timestamp_data = $this->getTimestampData();
		$page_data = $this->getPageData();
		$question_data = $this->getQuestionData();
		$variable_data = $this->getVariableData();


		$spreadsheet = new Spreadsheet();
		

		// Zavihek strani
		$sheet = $spreadsheet->getActiveSheet();
	
		$sheet->setTitle("Strani");
		$sheet->fromArray(
			$page_data,  // The data to set
			NULL,        // Array values with this value will not be set
			'A1'         // Top left coordinate of the worksheet range where
		);

		$spreadsheet->createSheet();


		// Zavihek vprasanja
		$sheet = $spreadsheet->getSheet(1);

		$sheet->setTitle("Vprašanja");
		$sheet->fromArray(
			$question_data,  // The data to set
			NULL,        // Array values with this value will not be set
			'A1'         // Top left coordinate of the worksheet range where
		);

		$spreadsheet->createSheet();


		// Zavihek itemi
		$sheet = $spreadsheet->getSheet(2);

		$sheet->setTitle("Itemi");
		$sheet->fromArray(
			$variable_data,  // The data to set
			NULL,        // Array values with this value will not be set
			'A1'         // Top left coordinate of the worksheet range where
		);

		$spreadsheet->createSheet();


		// Zavihek casi po straneh
		$sheet = $spreadsheet->getSheet(3);

		$sheet->setTitle("Časi po straneh");
		$sheet->fromArray(
			$timestamp_data,  // The data to set
			NULL,        // Array values with this value will not be set
			'A1'         // Top left coordinate of the worksheet range where
		);


		$writer = new Xlsx($spreadsheet);

		// ob_clean();
		// ob_start();
		$filename = 'advanced_timestamps_'.$this->anketa.'.xlsx';

		header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		header("Content-Disposition: attachment;filename=".$filename);
		header("Cache-Control: max-age=0");
		header("Last-Modified: ".date("D, d M Y H:i:s"));
		header("Cache-Control: cache, must-revalidate");
		header("Pragma: public");

		$writer->save("php://output");
		// ob_end_flush();

		die();
	}



	// Pridobimo parapodatke casov po straneh
	private function getTimestampData(){

		$data = array(0 => array('Stran ID', 'Respondent ID', 'Čas'));

		// Najprej dobimo case na uvodih (gru_id=0)
		$sql = sisplet_query("SELECT at.gru_id, at.usr_id, at.time_edit 
								FROM srv_advanced_timestamps at, srv_user u
                                WHERE u.ank_id='".$this->anketa."' AND at.usr_id=u.id AND gru_id<1
                                ORDER BY at.usr_id ASC, at.id ASC
							");
		while($row = mysqli_fetch_assoc($sql)){
			$data[] = $row;
		}

		// Dobimo se ostale case
		$sql = sisplet_query("SELECT at.gru_id, at.usr_id, at.time_edit 
								FROM srv_advanced_timestamps at, srv_grupa g
                                WHERE g.ank_id='".$this->anketa."' AND at.gru_id=g.id
                                ORDER BY at.usr_id ASC, at.id ASC
							");
		while($row = mysqli_fetch_assoc($sql)){
			$data[] = $row;
		}
		
		return $data;
	}

	// Pridobimo podatke strani v anketi
	private function getPageData(){

		$data = array(0 => array('Stran ID', 'Naslov', 'Vrstni red'));
        
		$sql = sisplet_query("SELECT id, naslov, vrstni_red 
								FROM srv_grupa 
                                WHERE ank_id='".$this->anketa."' 
                                ORDER BY vrstni_red ASC
							");
		while($row = mysqli_fetch_assoc($sql)){
			$data[] = $row;
		}
		
		return $data;
	}

	// Pridobimo podatke vprasanj v anketi
	private function getQuestionData(){

		$question_types = array(
			'1' => 'radio',
			'2' => 'checkbox',
			'3' => 'select',
			'4' => 'text', 
			'21' => 'besedilo*',
			'5' => 'label',
			'6' => 'multigrid',
			'16' => 'multicheckbox',
			'19' => 'multitext',
			'20' => 'multinumber',
			'7' => 'number',
			'22' => 'compute ',
			'25' => 'quota',
			'8' => 'datum ',
			'17' => 'ranking  ',
			'18' => 'vsota',
			'24' => 'grid - multiple',
			'23' => 'iz knjiznice',
			'9' => 'SN-imena',
			'26' => 'Lokacija',
			'27' => 'HeatMap ', 
		);

		$data = array(0 => array('Vprašanje ID', 'Stran ID', 'Naslov', 'Variabla', 'Tip'));
        
		$sql = sisplet_query("SELECT s.id, s.gru_id, s.naslov, s.variable, s.tip 
								FROM srv_spremenljivka s, srv_grupa g
                                WHERE g.ank_id='".$this->anketa."' AND s.gru_id=g.id
                                ORDER BY g.vrstni_red ASC, s.id ASC
							");
		while($row = mysqli_fetch_assoc($sql)){

			$row['tip'] = $question_types[$row['tip']];
			$row['naslov'] = strip_tags($row['naslov']);

			$data[] = $row;
		}
		
		return $data;
	}

	// Pridobimo podatke posameznih itemov v vprasanjih
	private function getVariableData(){

		$data = array(0 => array('Vrednost ID', 'Vprašanje ID, ', 'Naslov', 'Variabla', 'Vrstni red'));
        
		$sql = sisplet_query("SELECT v.id, v.spr_id, v.naslov, v.variable, v.vrstni_red
								FROM srv_vrednost v, srv_spremenljivka s, srv_grupa g
                                WHERE g.ank_id='".$this->anketa."' AND s.gru_id=g.id AND v.spr_id=s.id
                                ORDER BY g.vrstni_red ASC, s.id ASC, v.id ASC
							");
		while($row = mysqli_fetch_assoc($sql)){

			$row['naslov'] = strip_tags($row['naslov']);

			$data[] = $row;
		}
		
		return $data;
	}

}