summaryrefslogtreecommitdiffstats
path: root/admin/exportclases/class.xlsIzvozAnalizaMean.php
blob: c007754fdc541b569cf7f3584a40a540c48cb767 (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
<?php

	global $site_path;
	
	include_once('../../function.php');
	include_once('../survey/definition.php');
	include_once('../exportclases/class.xls.php');	
	
	
/** Class za generacijo xls-ja
 *
 */
class XlsIzvozAnalizaMean {

	var $anketa;									// trenutna anketa

	private $headFileName = null;					// pot do header fajla
	private $dataFileName = null;					// pot do data fajla
	private $dataFileStatus = null;					// status data datoteke
	
	public $meansClass = null;		//means class
	
	var $meanData1;
	var $meanData2;
	
	var $sessionData;			// podatki ki so bili prej v sessionu - za nastavitve, ki se prenasajo v izvoze...
		

	/**
    * @desc konstruktor
    */
	function __construct ($anketa = null, $podstran = 'mean'){
		global $site_path;
		global $global_user_id;
		global $output;

		// preverimo ali imamo stevilko ankete
		if ( is_numeric($anketa) ){

			$this->anketa['id'] = $anketa;
			
			SurveyAnalysis::Init($this->anketa['id']);
			SurveyAnalysis::$setUpJSAnaliza = false;
			
			// create new XLS document
			$this->xls = new xls();
			
            // Poskrbimo za datoteko s podatki
            $SDF = SurveyDataFile::get_instance();
            $SDF->init($this->anketa['id']);           
            $SDF->prepareFiles();  

            $this->headFileName = $SDF->getHeaderFileName();
            $this->dataFileName = $SDF->getDataFileName();
            $this->dataFileStatus = $SDF->getStatus();

            $_GET['a'] = A_ANALYSIS;
		
            // preberemo nastavitve iz baze (prej v sessionu) 
            SurveyUserSession::Init($this->anketa['id']);
            $this->sessionData = SurveyUserSession::getData('means');
            
            // ustvarimo means objekt
            $this->meansClass = new SurveyMeans($anketa);
                    
            if ( SurveyInfo::getInstance()->SurveyInit($this->anketa['id']) && $this->init()){
                $this->anketa['uid'] = $global_user_id;
                SurveyUserSetting::getInstance()->Init($this->anketa['id'], $this->anketa['uid']);
            }
            else
                return false;

            return true;
		}
		else{
			return false;
		}
	}

	// SETTERS && GETTERS

	function getFile($fileName)
	{
		//Close and output rtf document
		$output = $this->createXls();
		$this->xls->display($fileName, $output);
	}


	function init()
	{
		return true;
	}
	
	function encodeText($text)
	{ // popravimo sumnike ce je potrebno
		$text = html_entity_decode($text, ENT_NOQUOTES, 'UTF-8');
		$text = str_replace(array("&scaron;","&#353;","&#269;"),array("š","š","č"),$text);
		return strip_tags($text);
	}

	function createXls()
	{
		global $site_path;
		global $lang;
		global $output;
					
		$convertTypes = array('charSet'	=> "windows-1250",
						 'delimit'	=> ";",
						 'newLine'	=> "\n",
						 'BOMchar'	=> "\xEF\xBB\xBF");
		
		$output = $convertTypes['BOMchar'];

		$output .= '<table border="0"><tr><td colspan="10"><font size="3"><b>'.$lang['export_analisys_means'].'</b></font></td></tr></table>';
		
		
		$means = array();
		# če ne uporabljamo privzetega časovnega profila izpišemo opozorilo
		//$doNewLine = SurveyTimeProfiles :: printIsDefaultProfile(false);

		# če imamo filter ifov ga izpišemo
		//$doNewLine = SurveyConditionProfiles:: getConditionString($doNewLine );

		# če imamo filter spremenljivk ga izpišemo
		//$doNewLine = SurveyVariablesProfiles:: getProfileString($doNewLine , true) || $doNewLine;
		
		
		$this->meanData1 = $this->sessionData['means_variables']['variabla1'];
		$this->meanData2 = $this->sessionData['means_variables']['variabla2'];
			
		if ($this->meanData1 !== null && $this->meanData2 !== null) {
			$variables1 = $this->meanData2;
			$variables2 = $this->meanData1;
			$c1=0;
			$c2=0;
			if (is_array($variables2) && count($variables2) > 0) {
				foreach ($variables2 AS $v_second) {
					if (is_array($variables1) && count($variables1) > 0) {
						foreach ($variables1 AS $v_first) {
							$_means = $this->meansClass->createMeans($v_first, $v_second);
							if ($_means != null) {
								$means[$c1][$c2] = $_means;
							}
							$c2++;
						}
					}
					$c1++;
					$c2=0;
				}
			}
			
			if (is_array($means) && count($means) > 0) {
			
				foreach ($means AS $mean_sub_grup) {
					
					$output .= '<table border="0"><tr><td></td></tr></table>';
					
					$this->displayMeansTable($mean_sub_grup);
				}
			}
		}		
		
		return $output;
	}	

	public function displayMeansTable($_means) {
		global $lang;
		global $output;
		
		#število vratic in število kolon
		$cols = count($_means);
		# preberemo kr iz prvega loopa
		$rows = count($_means[0]['options']);

		# ali prikazujemo vrednosti variable pri spremenljivkah
		$show_variables_values = $this->meansClass->doValues;
		# izrišemo tabelo
		$output .= '<table border="1">';

		$output .= '<tr>';

		$label2 = $this->meansClass->getSpremenljivkaTitle($_means[0]['v2']);
		$label2 = $this->encodeText($label2);
			
		$output .= '<td align="center" rowspan="2">';
		$output .= '<b>'.$label2.'</b>';
		$output .= '</td>';
		
		for ($i = 0; $i < $cols; $i++) {
			$output .= '<td align="center" colspan="2" >';
			$label1 = $this->meansClass->getSpremenljivkaTitle($_means[$i]['v1']);
			$label1 = $this->encodeText($label1);
			$output .= '<b>'.$label1.'</b>';
			$output .= '</td>';
		}
		$output .= '</tr>';
		$output .= '<tr>';
		
		for ($i = 0; $i < $cols; $i++) {
			#Povprečje
			$output .= '<td align="center">';
			$output .= $lang['srv_means_label'];
			$output .= '</td>';
			#odstotki						
			$output .= '<td align="center">'.$lang['srv_means_label4'].'</td>';
		}
		$output .= '</tr>';

		if (count($_means[0]['options']) > 0) {
			foreach ($_means[0]['options'] as $ckey2 =>$crossVariabla2) {
				$output .= '<tr>';
				
				
				$output .= '<td align="center">';
				$output .= $crossVariabla2['naslov'];
				# če ni tekstovni odgovor dodamo key
				if ($crossVariabla2['type'] !== 't' ) {
					if ($show_variables_values == true) {
						if ($crossVariabla2['vr_id'] == null) {
							$output .= '&nbsp;( '.$ckey2.' )';
						} else {
							$output .= '&nbsp;( '.$crossVariabla2['vr_id'].' )';
						}
					}
				}
				$output .= '</td>';
				# celice z vsebino
				for ($i = 0; $i < $cols; $i++) {
					$output .= '<td align="center" k1="'.$ckey1.'" k2="'.$ckey2.'" n1="'.$crossVariabla1['naslov'].'" n2="'.$crossVariabla2['naslov'].'" v1="'.$crossVariabla1['vr_id'].'" v2="'.$crossVariabla2['vr_id'].'">';
					$output .= $this->meansClass->formatNumber($_means[$i]['result'][$ckey2], SurveyDataSettingProfiles :: getSetting('NUM_DIGIT_RESIDUAL'));
					$output .= '</td>';
					$output .= '<td align="center">';
					$output .= (int)$_means[$i]['sumaVrstica'][$ckey2];
					$output .= '</td>';
				}
				$output .= '</tr>';
			}
		}
		$output .= '<tr>';
		$output .= '<td align="center">'.$lang['srv_means_label3'].'</td>';
		for ($i = 0; $i < $cols; $i++) {
			$output .= '<td align="center">';
			
			$output .= $this->meansClass->formatNumber($_means[$i]['sumaMeans'], SurveyDataSettingProfiles :: getSetting('NUM_DIGIT_RESIDUAL'));
			$output .= '</td>';
			$output .= '<td align="center">';
			$output .= (int)$_means[$i]['sumaSkupna'];
			$output .= '</td>';
		}
		$output .= '</tr>';
		$output .= '</table>';		
	}
		
	
	/*Skrajsa tekst in doda '...' na koncu*/
	function snippet($text,$length=64,$tail="...")
	{
		$text = trim($text);
		$txtl = strlen($text);
		if($txtl > $length)
		{
			for($i=1;$text[$length-$i]!=" ";$i++)
			{
				if($i == $length)
				{
					return substr($text,0,$length) . $tail;
				}
			}
		$text = substr($text,0,$length-$i+1) . $tail;
		}
		return $text;
	}

	function setUserId($usrId) {$this->anketa['uid'] = $usrId;}
	function getUserId() {return ($this->anketa['uid'])?$this->anketa['uid']:false;}

	function formatNumber($value,$digit=0,$sufix="")
	{
		if ( $value <> 0 && $value != null )
			$result = round($value,$digit);
		else
			$result = "0";
		//$result = number_format($result, $digit, ',', '.').$sufix;
		$result = number_format($result, $digit, ',', '') . $sufix;
	
		// Preprecimo da bi se stevilo z decimalko pretvorilo v datum
		//$result = '="'. $result.'"';
	
		return $result;
	}
}

?>