summaryrefslogtreecommitdiffstats
path: root/admin/survey/export/latexclasses/class.LatexSurvey.php
blob: 7f937495d278bddc15b191155a1417ceb4d4d87e (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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
<?php

/**
 *
 *	Class ki skrbi za izris vprasalnika v latex
 *
 *
 */
//namespace Export\Latexclasses;

include('../../vendor/autoload.php');

define("RADIO_BTN_SIZE", 0.13);
 
class LatexSurvey{
	
	var $anketa; // ID ankete
	var $tex;	//shrani tex kodo
	var $texNewLine = '\\\\ ';
	//var $texPageBreak = "\\pagebreak";
	var $texPageBreak = "\\newpage";
	var $export_format;
	var $export_data_show_recnum;
	var $exportDataPageBreak=0; //vsak respondent na svoji strani
	
	var $commentType = 1;	// tip izpisa komentarjev
	
	var $loop_id = null;	// id trenutnega loopa ce jih imamo	
	
	var $db_table = '';
	
	protected $showIntro = 0;
	protected $showGDPRIntro = 0;
	protected $GDPRIntro = '';
	protected $type = 0;			// tip izpisa - 0->navaden, 1->iz prve strani, 2->s komentarji
	
	protected $showIf = 0;		// izpis if-ov
	
	var $skipEmpty = 0; 	// izpusti vprasanja brez odgovora
	var $skipEmptySub = 0; 	// izpusti podvprasanja brez odgovora
	
	protected $recnum = 0;
	protected $usr_id = 0;
	protected $texBigSkip = '\bigskip';

	protected $admin_type;

	protected $path2UploadedImages;
	
	protected $language = -1;		// Katero verzijo prevoda izvazamo	
	
	//function __construct($anketa, $export_format){
	function __construct($anketa=null, $export_format='', $export_show_intro=null, $export_show_if=null, $export_data_skip_empty=null, $export_data_skip_empty_sub=null){
		global $site_path, $global_user_id, $admin_type, $lang;
	
		$this->anketa = $anketa;
		$this->export_format = $export_format;

		$this->admin_type = $admin_type;
		
		$this->usr_id = $_GET['usr_id'];
		
		$this->showIntro = $export_show_intro;
		$this->showIf = $export_show_if;
		
		$this->skipEmpty = $export_data_skip_empty;
		
		$this->skipEmptySub = $export_data_skip_empty_sub;

		$this->path2Images = $site_path.'admin/survey/export/latexclasses/textemp/images/';

					
		//Prikazi GDPR v uvodu			
		$gdpr = new GDPR();
		$this->showGDPRIntro = $gdpr->isGDPRSurveyTemplate($this->anketa);
		//echo "gdpr nastavitve: ".$this->showGDPRIntro."</br>";
		if($this->showGDPRIntro){
			$this->GDPRIntro = $gdpr->getSurveyIntro($this->anketa);
			//echo "gdpr te: ".$this->GDPRIntro."</br>";
		}
		
		if ($this->usr_id  != '') {
			$sqlR = sisplet_query("SELECT recnum FROM srv_user WHERE id = '$this->usr_id '");
			$rowR = mysqli_fetch_array($sqlR);
			$this->recnum = $rowR['recnum'];
		}
		
		//pridobitev nastavitev izvoza
		SurveySetting::getInstance()->Init($this->anketa);
		$this->export_data_show_recnum = SurveySetting::getInstance()->getSurveyMiscSetting('export_data_show_recnum');	//ali je potrebno pokazati recnum ob vsakem respondentu
		$this->exportDataPageBreak = (int)SurveySetting::getInstance()->getSurveyMiscSetting('export_data_PB');	//ali mora vsak izpis odgovorov respondenta zaceti na svoji strani

		SurveyStatusProfiles::Init($this->anketa);
		
		//if ( SurveyInfo::getInstance()->SurveyInit($this->anketa['id']) && $this->init())
		if ( SurveyInfo::getInstance()->SurveyInit($this->anketa) )
		{		
			if (SurveyInfo::getInstance()->getSurveyColumn('db_table') == 1){
				$this->db_table = '_active';
			}
		}
		else{
			return false;
		}		
	}
	
	#funkcija, ki skrbi za izpis praznega vprasalnika in vprasalnika z odgovori enega respondenta
	public function displaySurvey($export_subtype='', $export_data_type='', $language=null){
		global $lang, $site_url;
		//echo "Funkcija displaySurvey user: $this->usr_id</br>";
		$this->language = $language;
		if($this->language!=-1){
			// Naložimo jezikovno datoteko
			$file = '../../lang/'.$this->language.'.php';
			include($file);
			$_SESSION['langX'] = $site_url .'lang/'.$this->language.'.php';
		}		
		
		$surveyExpanded = SurveyInfo::getInstance()->getSurveyColumn('expanded');
		
		// filtriramo spremenljivke glede na profil	
		SurveyVariablesProfiles :: Init($this->anketa);
		
		$dvp = SurveyUserSetting :: getInstance()->getSettings('default_variable_profile');
		$_currentVariableProfile = SurveyVariablesProfiles :: checkDefaultProfile($dvp);
		
		$tmp_svp_pv = SurveyVariablesProfiles :: getProfileVariables($_currentVariableProfile);
		
		foreach ( $tmp_svp_pv as $vid => $variable) {
			$tmp_svp_pv[$vid] = substr($vid, 0, strpos($vid, '_'));			
		}

				
		if($export_subtype=='q_data'){	//ce je vprasalnik za enega respondenta
			//pridobitev podatkov trenutnega respondenta za izpis Recnum
			$izbranStatusProfile = SurveyStatusProfiles :: getStatusAsQueryString();
			$sqluString = "SELECT id, last_status, lurker, recnum FROM srv_user WHERE ank_id = '".$this->anketa."' ".$izbranStatusProfile." AND deleted='0' AND preview='0' AND id='".$this->usr_id."' ORDER BY recnum";
			//echo $sqluString;
			$sqlu = sisplet_query($sqluString);		
			$rowu = mysqli_fetch_array($sqlu);
			$recnum = $rowu['recnum'];
			if($recnum && $this->export_data_show_recnum){
				$recnumBesedilo = "(Recnum $recnum)";
			}else{
				$recnumBesedilo = "";
			}
			//pridobitev podatkov trenutnega respondenta za izpis Recnum - konec
			
			
			
			//$tex .= $lang['srv_respondent_answer'].": ".$this->recnum;			
			$tex .= '\MakeUppercase{\huge \textbf{'.$lang['srv_respondent_answer'].' '.$recnumBesedilo.'}}';	//izpisi "Odgovori respondenta"
			$tex .= $this->texNewLine;
			$tex .= $this->texNewLine;
		}
		
		//ce je potrebno izpisati GDPR besedilo v intro
		if($this->showGDPRIntro == 1){
			//$GDPRintro = "gdpr INTRO";
			$GDPRintro = strip_tags($this->GDPRIntro, '<a><img><ul><li><ol><br><p>');
			$GDPRintro = $this->encodeTextHere($GDPRintro);
			$tex .= ' \textbf{'.$GDPRintro.'} ';

			//radio buttona
			if($export_subtype=='q_empty'){	//ce je prazen vprasalnik
				$radioButtonTex = ("\\includegraphics[scale=".RADIO_BTN_SIZE."]{".$this->path2Images."radio}");
				$tex .= '\\\\'.$radioButtonTex.' '.$lang['srv_gdpr_intro_no'];
				$tex .= '\\\\'.$radioButtonTex.' '.$lang['srv_gdpr_intro_yes'];
			}else{
				//pridobitev ID grupe
				$sqlGrupeStringGDPR = "SELECT id FROM srv_grupa WHERE ank_id='".$this->anketa."' ORDER BY vrstni_red";		
				$sqlGrupeGDPR = sisplet_query($sqlGrupeStringGDPR);
				$rowGrupeGDPR = mysqli_fetch_array( $sqlGrupeGDPR );
				$grupa = $rowGrupeGDPR['id'];
				//pridobitev ID grupe - konec
				
				//pridobitev informacije obiska strani oz. ankete				
				$sqlStringStran = "SELECT time_edit FROM srv_user_grupa".$this->db_table." WHERE usr_id='".$this->usr_id."' AND gru_id='".$grupa."'";
				//echo $sqlStringStran;
				$sqlStran = sisplet_query($sqlStringStran);
				$rowStran = mysqli_fetch_array( $sqlStran );
				$stran = $rowStran['time_edit'];
				//echo $stran;
				//pridobitev informacije obiska strani oz. ankete - konec

				if($stran){
					$radioButtonTexNo = ("\\includegraphics[scale=".RADIO_BTN_SIZE."]{".$this->path2Images."radio}");
					$radioButtonTexYes = ("\\includegraphics[scale=".RADIO_BTN_SIZE."]{".$this->path2Images."radio2}");
				}else{
					$radioButtonTexNo = ("\\includegraphics[scale=".RADIO_BTN_SIZE."]{".$this->path2Images."radio2}");
					$radioButtonTexYes = ("\\includegraphics[scale=".RADIO_BTN_SIZE."]{".$this->path2Images."radio}");
				}

				$tex .= '\\\\'.$radioButtonTexNo.' '.$lang['srv_gdpr_intro_no'];
				$tex .= '\\\\'.$radioButtonTexYes.' '.$lang['srv_gdpr_intro_yes'];
				
			}
			
			
			//radio buttona - konec

			$tex .= $this->texNewLine;				
			$tex .= $this->texNewLine;
		}
		
		// ce obstaja intro izpisemo intro - pri izpisu vprasalnika brez odgovorov (ce smo na prvi strani moramo biti v razsirjenem nacinu)
		if( ($surveyExpanded != 0 || $this->type != 1) && $this->showIntro == 1 ){
			if ( SurveyInfo::getInstance()->getSurveyShowIntro() )
			{ 		
				//preverjanje, ali je prevod
				if(isset($_GET['language'])){
					$this->language = $_GET['language'];
					$prevod = 1;
				}else{
					$prevod = 0;
				}
				//preverjanje, ali je prevod - konec

				//if($this->language!=-1){ //ce je prevod ankete
				if($prevod){ //ce je prevod ankete
					$spr_id_uvod = -1;
					$sqll = sisplet_query("SELECT naslov, info FROM srv_language_spremenljivka WHERE ank_id='".$this->anketa."' AND spr_id='".$spr_id_uvod."' AND lang_id='".$this->language."'");
					$rowl = mysqli_fetch_array($sqll);	//pridobi prevod uvoda v ustreznem jeziku
					$intro = $rowl['naslov'];	//prevod uvoda v ustreznem jeziku
				}else{
					$intro = (SurveyInfo::getInstance()->getSurveyIntro() == '') ? $lang['srv_intro'] : SurveyInfo::getInstance()->getSurveyIntro();
				}					
				$tex .=  $this->encodeTextHere($intro);		
				if($intro){
					$tex .= $this->texNewLine;
					$tex .= $this->texNewLine;
				}
				
			}
		}
		
		$sqlGrupeString = "SELECT id FROM srv_grupa WHERE ank_id='".$this->anketa."' ORDER BY vrstni_red";		
		$sqlGrupe = sisplet_query($sqlGrupeString);

		//echo "__________________________________</br>";
		//echo "Funkcija displaySurvey user: $this->usr_id</br>";
		if($this->export_format == 'rtf'){
			$tex .= $this->texNewLine;
		}

		$question = new LatexSurveyElement($this->anketa, $this->export_format, 0, $this->usr_id, $export_subtype, $language);
		
		########
		while ( $rowGrupe = mysqli_fetch_assoc( $sqlGrupe ) )
		{ // sprehodmo se skozi grupe ankete
			$this->grupa = $rowGrupe['id'];

			// Pogledamo prvo spremenljivko v grupi ce je v loopu
			$sql = sisplet_query("SELECT id FROM srv_spremenljivka WHERE gru_id='".$this->grupa."' AND visible='1' ORDER BY vrstni_red ASC");			
			$row = mysqli_fetch_array($sql);

			// ce je ima loop za parenta
			$if_id = $this->find_parent_loop($row['id']);

			if ($if_id > 0 && ($export_subtype=='q_data'||$export_subtype=='q_data_all')){	//ce je loop in (je izpis odgovorov respondentov)
				$texLoop = '';
				//echo "Je v loopu $if_id </br>";
				$sql1 = sisplet_query("SELECT if_id FROM srv_loop WHERE if_id = '$if_id'");
				$row1 = mysqli_fetch_array($sql1);
				
				$this->loop_id = $this->findNextLoopId($row1['if_id']);
				//echo "Loop id: ".$this->loop_id."</br>";
				//echo " id: ".$row['id']."</br>";
				$if = Cache::srv_if($if_id);
				//echo "If label: ".$if['label']."</br>";
				//echo "If : ".print_r($if)."</br>";
				$loop_title = $if['label'];
				
				// gremo cez vse spremenljivke v trenutnem loopu
				while($this->loop_id != null){
					//echo "Loop id: ".$this->loop_id."</br>";
					
					// Izrisemo naslov loopa
					$tex .= $this->dataPiping($loop_title);
					
					//TODO: TUdi tu se celotna spremenljivka prenaša naprej
					$sqlSpremenljivke = sisplet_query("SELECT id, tip, visible, sistem, variable, naslov, info, orientation, dostop FROM srv_spremenljivka WHERE gru_id='".$this->grupa."' AND visible='1' ORDER BY vrstni_red ASC");
					//echo "SELECT id, tip, visible, sistem, variable, naslov, info, orientation FROM srv_spremenljivka WHERE gru_id='".$this->grupa."' AND visible='1' ORDER BY vrstni_red ASC "."</br>";
					
					while ($rowSpremenljivke = mysqli_fetch_assoc($sqlSpremenljivke))
					{ // sprehodimo se skozi spremenljivke grupe					

						$spremenljivka = $rowSpremenljivke['id'];
						
						if ($rowSpremenljivke['visible'] == 0 || !( ($this->admin_type <= $rowSpremenljivke['dostop'] && $this->admin_type>=0) || ($this->admin_type==-1 && $rowSpremenljivke['dostop']==4) ) ) {
							//echo "ni admin za sprem: ".$rowSpremenljivke['id']."</br>";
							continue;						
						}
						
						if($export_subtype=='q_data'||$export_subtype=='q_data_all'){	//ce je vprasalnik s podatki za enega ali vse respondente
							$preveriSpremenljivko = $this->checkSpremenljivkaData($spremenljivka, $this->loop_id);
						}else{	//ce je prazen vprasalnik ali vprasalnik s komentarji
							$preveriSpremenljivko = $this->checkSpremenljivka($spremenljivka);
						}
						
						//if ( $this->checkSpremenljivka ($spremenljivka) /*|| $this->showIf == 1*/ )
						if ($preveriSpremenljivko)	
						{ // lahko izrišemo spremenljivke

							// po potrebi obarvamo vprašanja
							//$this->pdf->SetTextColor(0);
							if ($rowSpremenljivke['visible'] == 0)
							{
								if ($rowSpremenljivke['sistem'] == 1)
								{ 		// če je oboje = vijolčno
									$this->pdf->SetTextColor(128,0,128);
								}
								else
								{		// Če je skrito = rdeče
									$this->pdf->SetTextColor(255,0,0);
								}
							}
							else if ($rowSpremenljivke['sistem'] == 1)
							  $this->pdf->SetTextColor(0,0,255);
							
										
							// Izpis vprasalnika z rezultati
							
							// Ce imamo kombinirano tabelo pogledamo ce prikazujemo katero od podtabel
							if($rowSpremenljivke['tip'] == 24){
								
								$subGrids = array();
								$showGridMultiple = false;
								
								// Loop po podskupinah gridov					
								$sqlSubGrid = sisplet_query("SELECT m.spr_id, s.tip, s.enota FROM srv_grid_multiple m, srv_spremenljivka s WHERE m.parent='".$spremenljivka."' AND m.spr_id=s.id");
								while($rowSubGrid = mysqli_fetch_array($sqlSubGrid)){					
									if(in_array($rowSubGrid['spr_id'],$tmp_svp_pv) || count($tmp_svp_pv) == 0){
										$showGridMultiple = true;
										break;
									}
								}
							}
							// ce je nastavljen profil s filtriranimi spremenljivkami
							if(in_array($spremenljivka,$tmp_svp_pv) || count($tmp_svp_pv) == 0 || ($rowSpremenljivke['tip'] == 24 && $showGridMultiple) || $rowSpremenljivke['tip'] == 5){
								
								$this->currentHeight = 0;
								
								//izpisi tekst vprasanja
								$tex .= $question->displayQuestionText($rowSpremenljivke, $stevilcenje, $export_subtype, $preveriSpremenljivko, $this->loop_id, $export_data_type);	//izpisi tekst vprasanja
								
								//izpisi odgovore								
								$tex  .= $question->displayAnswers($rowSpremenljivke, $export_subtype, $preveriSpremenljivko, $export_data_type, $this->loop_id);	//izpisi odgovore
							}											
						}
					}								
					$this->loop_id = $this->findNextLoopId();
					
					//ce je prazen vprasalnik, ustavi
					if($export_subtype=='q_empty'){
						break;
					}
				}
			}			
			// Navadne spremenljivke ki niso v loopu
			else{
				//echo "Ni v loop-u </br>";
				$loop_id = 'IS NULL';
				$zaporedna = 0;
				//TODO: Omenjen query je potrebno optimizirati in da se naprej samo prenaša ID, ki se potem v podfunkcijah kliče ustrezen query
                // trenutno se potrebuje:  id, tip, visible, sistem, variable, params
				$sqlSpremenljivke = sisplet_query("SELECT * FROM srv_spremenljivka WHERE gru_id='".$this->grupa."' AND visible='1' ORDER BY vrstni_red ASC");
				
				//$testniStavek = "SELECT * FROM srv_spremenljivka WHERE gru_id='".$this->grupa."' AND visible='1' ORDER BY vrstni_red ASC";
				//echo "sql stavek za testiranje: ".$testniStavek."</br>";
				while ($rowSpremenljivke = mysqli_fetch_assoc($sqlSpremenljivke)){ // sprehodimo se skozi spremenljivke grupe
					$spremenljivka = $rowSpremenljivke['id'];
					
					if ($rowSpremenljivke['visible'] == 0 || !( ($this->admin_type <= $rowSpremenljivke['dostop'] && $this->admin_type>=0) || ($this->admin_type==-1 && $rowSpremenljivke['dostop']==4) ) ) {
						//echo "ni admin za sprem: ".$rowSpremenljivke['id']."</br>";
						continue;						
					}

					//echo "Ni v loop-u:".$rowSpremenljivke['tip']."  </br>";
					if($export_subtype=='q_data'||$export_subtype=='q_data_all'){	//ce je vprasalnik s podatki za enega ali vse respondente
						$preveriSpremenljivko = $this->checkSpremenljivkaData($spremenljivka, $loop_id);
					}else{	//ce je prazen vprasalnik ali vprasalnik s komentarji
						$preveriSpremenljivko = $this->checkSpremenljivka($spremenljivka);
					}
					
					//if ( $this->checkSpremenljivka ($spremenljivka) /*|| $this->showIf == 1*/ )
					if ($preveriSpremenljivko)
					{ // lahko izrišemo spremenljivke

						// po potrebi obarvamo vprašanja
						//$this->pdf->SetTextColor(0);
						if ($rowSpremenljivke['visible'] == 0)
						{
							if ($rowSpremenljivke['sistem'] == 1)
							{ 		// če je oboje = vijolčno
								//$this->pdf->SetTextColor(128,0,128);
							}
							else
							{		// Če je skrito = rdeče
								//$this->pdf->SetTextColor(255,0,0);
							}
						}
						else if ($rowSpremenljivke['sistem'] == 1)
						  //$this->pdf->SetTextColor(0,0,255);


						// če imamo številčenje Type = 1 potem številčimo V1
						if (SurveyInfo::getInstance()->getSurveyCountType())
							$zaporedna++;
						$stevilcenje = ( SurveyInfo::getInstance()->getSurveyCountType() ) ?
						( ( SurveyInfo::getInstance()->getSurveyCountType() == 2 ) ? $rowSpremenljivke['variable'].") " : $zaporedna.") " ) : null;

						$this->currentHeight = 0;
						
						// izpis skrcenega vprasalnika (samo pri izpisu iz urejanja)
						if($surveyExpanded == 0 && $this->type == 1){
							//$this->outputVprasanjeCollapsed($rowSpremenljivke, $stevilcenje);
						}
						
						// izpis navadnega vprasalnika
						else{
							//echo "while stavek za".$rowSpremenljivke['id']." </br>";							
							$tex .= $question->displayQuestionText($rowSpremenljivke, $stevilcenje, $export_subtype, $preveriSpremenljivko, $this->loop_id, $export_data_type);	//izpisi tekst vprasanja							
							$tex .= $question->displayAnswers($rowSpremenljivke, $export_subtype, $preveriSpremenljivko, $export_data_type, $this->loop_id);	//izpisi odgovore
						}
						//$this->pdf->Ln(LINE_BREAK);
					}
				}
				//echo "Končni tex brez loopa: ".$tex."</br>";
			}
			//$tex .= $this->texBigSkip;
			if($this->export_format == 'rtf'){
				$tex .= $this->texBigSkip;
			}
			//$tex .= $texLoop;
			//echo "Končni tex: ".$tex."</br>";
		}
		//echo "Končni tex: ".$tex."</br>";
		// če izpisujemo grupo, ne izpisujemo zakljucka
		if ( !$this->getGrupa() ){
			if ( SurveyInfo::getInstance()->getSurveyShowConcl() && SurveyInfo::getInstance()->getSurveyConcl() )
			{		// ce obstaja footer izpisemo footer

			}
		}


        // Izpis grafa in tabele za NIJZ na koncu dokumenta
        global $site_domain;
        if( ($site_domain == 'test.1ka.si' && $this->anketa == '8892') || ($site_domain == 'anketa.nijz.si' && $this->anketa == '126738') ){	

            // Page break
            $tex .= '\cleardoublepage';

            // Naslov "Ocena bivalnega okolja"
            $tex .= '\begin{center} \textbf{Ocena bivalnega okolja} \end{center}';

            $nijz = new SurveyNIJZ($this->anketa, $this->usr_id);
            
            // Latex nijz slika grafa
            $tex .= $nijz->displayRadarLatex();

            // Latex nijz tabela
            $tex .= $nijz->displayTableLatex();
        }
		
		return $tex;
	}
	#funkcija, ki skrbi za izpis praznega vprasalnika in vprasalnika z odgovori enega respondenta - konec
	
	
	#funkcija, ki skrbi za izpis vseh odgovorov (vsi respondenti -> max 300)	//po outputAllResults() iz class.pdfIzvozResults.php
	public function displayAllSurveys($export_subtype='', $export_format='', $export_data_type=''){
		global $lang, $site_url;
		$tex = '';
		$izbranStatusProfile = SurveyStatusProfiles :: getStatusAsQueryString();
		
		$sqluString = "SELECT id, last_status, lurker, recnum FROM srv_user WHERE ank_id = '".$this->anketa."' ".$izbranStatusProfile." AND deleted='0' AND preview='0' ORDER BY recnum";
		//echo $sqluString;
		$sqlu = sisplet_query($sqluString);
		
		$numOfUsers = mysqli_num_rows($sqlu);	//stevilo respondentov
		
		if($numOfUsers > 300){
			$tex .= $lang['srv_export_all_respondent_data_error'].' ('.$numOfUsers.').';
		}		
		else{								
			//za vsak user oz respondent
			while($rowu = mysqli_fetch_array($sqlu)){								
				//belezenje statusa
				switch($rowu['last_status']){
					case '0':
						$status = $lang['srv_userstatus_0'];
						break;
					case '1':
						$status = $lang['srv_userstatus_1'];
						break;
					case '2':
						$status = $lang['srv_userstatus_2'];
						break;
					case '3':
						$status = $lang['srv_userstatus_3'];
						break;
					case '4':
						$status = $lang['srv_userstatus_4'];
						break;
					case '5':
						$status = $lang['srv_userstatus_5'];
						break;
					case '6':
						$status = $lang['srv_userstatus_6'];
						$status .= ($rowu['lurker'] == '1') ? ' - lurker' : '';
						break;
				}
				
				#prenos odgovorov vsakega respondenta na novo stran, če je to potrebno
				//if($this->exportDataPageBreak == 1){	//ce mora vsak respondent imeti svoje odgovore na svoji strani
				if($this->exportDataPageBreak == 1 && $export_format == 'pdf'){	//ce mora vsak respondent imeti svoje odgovore na svoji strani in je pdf izvoz, saj za rtf pagebreak ne deluje
					$tex .= $this->texPageBreak;
				}
				#prenos odgovorov vsakega respondenta na novo stran, če je to potrebno - konec
				
				//$this->export_data_show_recnum = 1;
				#izpis statusa respondenta in anketiranja
				if($this->export_data_show_recnum == 1){	//ce je potrebno pokazati stevilko respondenta					
					$tex .= '{\\Large Status '.$rowu['last_status'].' - '.$status.' (Recnum '.$rowu['recnum'].')} '.$this->texNewLine.' \\par ';					
				}else{
					$tex .= '{\\Large Status '.$rowu['last_status'].' - '.$status.$this->texNewLine.' \\par}';					
				}
				#izpis statusa respondenta in anketiranja - konec
				
				
				#izpis odgovorov posameznega userja
				$this->usr_id = $rowu['id'];
				
				#za pridobitev jezika respondenta
				if ($this->usr_id  != '') {					
					$sqlL = sisplet_query("SELECT language FROM srv_user WHERE id = '$this->usr_id ' ");
					$rowL = mysqli_fetch_array($sqlL);
					$language = $rowL['language'];					
				}
				#za pridobitev jezika respondenta - konec

				$tex .= $this->displaySurvey($export_subtype, $export_data_type, $language);
				#izpis odgovorov posameznega userja - konec
				
			}
		}
		return $tex;
	}
	#funkcija, ki skrbi za izpis vseh odgovorov (vsi respondenti -> max 300) - konec
	
	
	#funkcija, ki skrbi za izpis praznega vprasalnika s komentarji	//po outputCommentaries() iz class.pdfIzvoz.php
	public function displaySurveyCommentaries($export_subtype='', $export_data_type=''){	
		
		global $lang;
		global $site_url;
		global $admin_type;
		global $global_user_id;
		$texBigSkip = '\bigskip';
		$tex = '';
		
		$this->commentType = (isset($_GET['only_unresolved'])) ? $_GET['only_unresolved'] : 1;

		$f = new Forum;
		$c = 0;
		
		$b = new Branching($this->anketa);
		
		$rowi = SurveyInfo::getInstance()->getSurveyRow();

		SurveySetting::getInstance()->Init($this->anketa);
		$question_resp_comment_viewadminonly = SurveySetting::getInstance()->getSurveyMiscSetting('question_resp_comment_viewadminonly');
		$question_comment_viewadminonly = SurveySetting::getInstance()->getSurveyMiscSetting('question_comment_viewadminonly');
		$question_comment_viewauthor = SurveySetting::getInstance()->getSurveyMiscSetting('question_comment_viewauthor');
		$sortpostorder = SurveySetting::getInstance()->getSurveyMiscSetting('sortpostorder');
		$question_note_view = SurveySetting::getInstance()->getSurveyMiscSetting('question_note_view');
		$addfieldposition = SurveySetting::getInstance()->getSurveyMiscSetting('addfieldposition');
		$commentmarks = SurveySetting::getInstance()->getSurveyMiscSetting('commentmarks');
		$commentmarks_who = SurveySetting::getInstance()->getSurveyMiscSetting('commentmarks_who');
			
		$sqlString = "SELECT s.* FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='".$this->anketa."' ORDER BY g.vrstni_red ASC, s.vrstni_red ASC";
		$sql = sisplet_query($sqlString);

		$question = new LatexSurveyElement($this->anketa, $this->export_format, 0, 0, 0);


 		if ( mysqli_num_rows($sql) > 0 && ( (int)$question_resp_comment_viewadminonly + (int)$question_comment_viewadminonly ) > 0 ) {
			
			while ($row = mysqli_fetch_array($sql)) {
				
				$sql1 = sisplet_query("SELECT thread, note FROM srv_spremenljivka WHERE id = '$row[id]'");
				$row1 = mysqli_fetch_array($sql1);
				
 				$orderby = $sortpostorder == 1 ? 'DESC' : 'ASC' ;
				$tid = $row1['thread'];	
				
				$only_unresolved = " ";
				$only_unresolved2 = " ";
				
				if ($this->commentType == 1) $only_unresolved = " AND ocena <= 1 "; 
				if ($this->commentType == 1) $only_unresolved2 = " AND text2 <= 1 "; 
				
				if ($this->commentType == 2) $only_unresolved = " AND ocena = 0 "; 
				if ($this->commentType == 2) $only_unresolved2 = " AND text2 = 0 "; 
				
				if ($this->commentType == 3) $only_unresolved = " AND ocena = 1 "; 
				if ($this->commentType == 3) $only_unresolved2 = " AND text2 = 1 "; 
				
				if ($this->commentType == 4) $only_unresolved = " AND ocena = 2 "; 
				if ($this->commentType == 4) $only_unresolved2 = " AND text2 = 2 "; 
				
				if ($this->commentType == 5) $only_unresolved = " AND ocena = 3 "; 
				if ($this->commentType == 5) $only_unresolved2 = " AND text2 = 3 "; 
				
				
				$tema_vsebuje = substr($lang['srv_forum_intro'],0,10);		// da ne prikazujemo 1. default sporocila
				
				if ($admin_type <= $question_comment_viewadminonly) {	// vidi vse komentarje
					$sqlt = sisplet_query("SELECT uid, time, vsebina FROM post WHERE vsebina NOT LIKE '%{$tema_vsebuje}%' AND tid='$tid' $only_unresolved ORDER BY time $orderby, id $orderby");

				} elseif ($question_comment_viewauthor==1) {	// vidi samo svoje komentarje
					$sqlt = sisplet_query("SELECT uid, time, vsebina FROM post WHERE vsebina NOT LIKE '%{$tema_vsebuje}%' AND tid='$tid' $only_unresolved AND uid='$global_user_id' ORDER BY time $orderby, id $orderby");

				} else {												// ne vidi nobenih komentarjev
				    $sqlt = sisplet_query("SELECT uid, time, vsebina FROM post WHERE 1=0");
				}		
				
				$sql2 = sisplet_query("SELECT COUNT(*) AS count FROM srv_data_text".$this->db_table." WHERE spr_id='0' AND vre_id='$row[id]' $only_unresolved2");
				$row2 = mysqli_fetch_array($sql2);

				if ( mysqli_num_rows($sqlt) > 0 || $row2['count'] > 0 || $row1['note'] != '') {
					$c++;
					$preveriSpremenljivko = 0; //ni potrebno
					$tex .= $question->displayQuestionText($row, $stevilcenje, $export_subtype, $preveriSpremenljivko, null, $export_data_type);	//izpisi tekst vprasanja
					$tex .= $question->displayAnswers($row, $export_subtype, $preveriSpremenljivko, $export_data_type, null);	//izpisi odgovore
					
 					if ($admin_type <= $question_note_view || $question_note_view == '') {
						
						if ($row1['note'] != '') {

							//$tex .= $texNewLine;
							$tex .= $this->texNewLine;
							$tex .= $question->encodeText($lang['hour_comment']);

							//$tex .= $texNewLine;
							$tex .= $this->texNewLine;
							$tex .= $question->encodeText($row1['note']);
						}
					}
									
					// komentarji na vprasanje
					if ($row1['thread'] > 0) {
						
						if (mysqli_num_rows($sqlt) > 0) {							
							//$tex .= $texNewLine;
							$tex .= $this->texNewLine;
							//$tex .= '\textbf{'.$question->encodeText($lang['srv_admin_comment']).'}';	//izpis naslova komentarjev
							$tex .= '\textcolor{komentar}{\textbf{'.$question->encodeText($lang['srv_admin_comment']).'}}';	//izpis naslova komentarjev
							//$tex .= '\textcolor{komentar}{\textbf{'.$question->encodeText($lang['srv_admin_comment']).'}}';	//izpis naslova komentarjev
							$tex .= '\\\\';

							$i = 0;
							while ($rowt = mysqli_fetch_array($sqlt)) {

								$tex .= '\textbf{'.$question->encodeText($f->user($rowt['uid'])).'}';	//izpis imena trenutnega urednika
								
								$tex .= $question->encodeText(' ('.$f->datetime1($rowt['time']).'):');	//izpis datuma komentarja
								
								$tex .= '\\\\';

								// Popravimo vsebino ce imamo replike							
								$vsebina = $rowt['vsebina'];
								$odgovori = explode("<blockquote style=\"margin-left:20px\">", $vsebina);
								
								$tex .= $question->encodeText($odgovori[0]);	//izpis komentarja
								
								unset($odgovori[0]);
								foreach($odgovori as $odgovor){	//izpis replik
									$odgovor = explode('<br />', $odgovor);
									$avtor = explode('</b> ', $odgovor[0]);
									
									$tex .= '\\\\';
									$tex .= '\forceindent';	//da je indented
									$tex .= '\textbf{'.$question->encodeText($avtor[0]).'} ';	//avtor replike
									$tex .= $question->encodeText($avtor[1]); //izpis datuma replike
									
									$tex .= '\\\\';
									$tex .= '\forceindent ';	//da je indented
									$tex .= $question->encodeText($odgovor[1]); //izpis replike
								}
								
								// Crta
								$tex .= '\\\\';
								$tex .= '\noindent\rule{0.9\textwidth}{0.5pt} ';								
								$tex .= '\\\\';
							}			
						}
					}

					// komentarji respondentov
					if ($row2['count'] > 0) {
						
						if ($admin_type <= $question_resp_comment_viewadminonly) {

/* 							$this->pdf->Ln(3);
							$this->pdf->setFont('','B', 10);
							$this->pdf->Write  (0, $this->encodeText($lang['srv_repondent_comment']), '', 0, 'l', 1, 1); */
							//$tex .= $texNewLine;
							$tex .= $this->texNewLine;
							//$tex .= '\textbf{'.$question->encodeText($lang['srv_repondent_comment']).'}';	//izpisa naslova komentarjev
							$tex .= '\textcolor{komentar}{\textbf{'.$question->encodeText($lang['srv_repondent_comment']).'}}';	//izpisa naslova komentarjev
							$tex .= '\\\\';
							
							//$this->pdf->Ln(3);
							
							if ($this->commentType == 1) $only_unresolved = " AND d.text2 <= 1 "; else $only_unresolved = " ";
							
							$sqlt = sisplet_query("SELECT d.text AS text, u.time_edit AS time_edit FROM srv_data_text".$this->db_table." d, srv_user u WHERE d.spr_id='0' AND d.vre_id='$row[id]' AND u.id=d.usr_id $only_unresolved2 ORDER BY d.id ASC");
							if (!$sqlt) echo mysqli_error($GLOBALS['connect_db']);
							while ($rowt = mysqli_fetch_array($sqlt)) {
								
/* 								$this->pdf->setFont('','', 10);
								$this->pdf->Write(0, $this->encodeText($f->datetime1($rowt['time_edit']).':'), '', 0, 'l', 1, 1);*/
								
								$tex .= $question->encodeText($f->datetime1($rowt['time_edit']).':');	//izpis datuma komentarja
								
								$tex .= '\\\\';
								
								//$this->pdf->MultiCell(100, 0, $this->encodeText($rowt['text']),0,'L',0,1,0,0,true,0);
								$tex .= $question->encodeText($rowt['text']);	//izpis komentarja respondenta
								
								$tex .= '\\\\';
								
								// Crta
								$tex .= '\noindent\rule{0.9\textwidth}{0.5pt} ';								
								$tex .= '\\\\';
								

							}
						}
					}
					
					//$this->pdf->Ln(LINE_BREAK);
					$tex .= $texBigSkip;
					$tex .= $texBigSkip;
				}
			}
	
			
		}
		
	
		return $tex;	
	}
	
	function getGrupa() {return $this->grupa;}
	
	
	/**
    * @desc preveri ali je spremenljivka vidna (zaradi branchinga)
    */
    function checkSpremenljivka ($spremenljivka=null) {

        $row = sisplet_query("SELECT visible FROM srv_spremenljivka WHERE id = '".$spremenljivka."'", "obj");
        if ($row->visible == 0) {
            return false;
        }

        //TODO: Omenjen del kode je nepotreben, ker vedno vrne true
        //$sql1 = sisplet_query("SELECT parent FROM srv_branching WHERE element_spr = '".$spremenljivka."'");
        //if (!$sql1) echo mysqli_error($GLOBALS['connect_db']);
        //$row1 = mysqli_fetch_array($sql1);

        /*if (!$this->checkIf($row1['parent']))
            return false;*/

        return true;
    }
	
    /**
    * @desc preveri ali je spremenljivka vidna (zaradi branchinga), ko je q_data ali q_data_all
    */
    //function checkSpremenljivkaData ($spremenljivka, $gridMultiple=false) {
    function checkSpremenljivkaData ($spremenljivka=null, $loop_id_raw=null, $gridMultiple=false) {
		
		$loop_id = $loop_id_raw == 'IS NULL' ? " IS NULL" : " = '".$loop_id_raw."'";
		
		//$row = sisplet_query("SELECT id, visible, dostop, tip, enota FROM srv_spremenljivka WHERE id = '".$spremenljivka."'", "array");
		$sql = sisplet_query("SELECT id, visible, dostop, tip, enota FROM srv_spremenljivka WHERE id = '".$spremenljivka."'");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
		$row = mysqli_fetch_array($sql);
		
		// Ce vprasanje ni vidno ali ce uporabnik nima dostopa do vprasanja
        if ($row['visible'] == 0 || !( ($this->admin_type <= $row['dostop'] && $this->admin_type>=0) || ($this->admin_type==-1 && $row['dostop']==4) ) ) {
            return false;
        }
			
		// Kalklulacije in kvote ne prikazujemo
       	if($row['tip'] == 22 || $row['tip'] == 25) {
            return false;
        }
			
		// Preverjamo ce je vprasanje prazno in ce preskakujemo prazne
		if($this->skipEmpty==1 && !$gridMultiple){
			
			$isEmpty = true;
			//echo "isEmpty: ".$isEmpty."</br>";
			switch ( $row['tip'] ){
				case 1: //radio
				case 2: //check
				case 3: //select -> radio
					//$sqlUserAnswer = sisplet_query("SELECT * FROM srv_data_vrednost".$this->db_table." WHERE spr_id='$row[id]' AND usr_id='".$this->usr_id."' AND vre_id!='-2'");
					$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_vrednost".$this->db_table." WHERE spr_id='$row[id]' AND usr_id='".$this->usr_id."' AND vre_id>'0' AND loop_id $loop_id ";
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					if($sqlUserAnswer->st > 0){
						$isEmpty = false;
					}						
				break;

				case 6: //multigrid
				case 16:// multicheckbox
				case 19:// multitext
				case 20:// multinumber											
					if($row['tip'] == 6 && $row['enota'] != 3){
						$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_grid".$this->db_table." WHERE spr_id = '".$row['id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
					}
					elseif($row['tip'] == 16 || ($row['tip'] == 6 && $row['enota'] == 3)){
						//$sqlUserAnswer = sisplet_query("SELECT * FROM srv_data_checkgrid".$this->db_table." WHERE spr_id = '".$row['id']."' AND usr_id = '".$this->usr_id."'");
						$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_checkgrid".$this->db_table." WHERE spr_id = '".$row['id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
					}
					else{
						//$sqlUserAnswer = sisplet_query("SELECT * FROM srv_data_textgrid".$this->db_table." WHERE spr_id = '".$row['id']."' AND usr_id = '".$this->usr_id."'");
						$sqlUserAnswerString ="SELECT COUNT(*) AS st FROM srv_data_textgrid".$this->db_table." WHERE spr_id = '".$row['id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
					}
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					//echo "stevilo podatkov: ".mysqli_num_rows($sqlUserAnswer)."</br>";
					if($sqlUserAnswer->st > 0){
						$isEmpty = false;	
					}
				break;
				
				case 7: //number
				case 8: //datum	
				case 18: //vsota
				case 21: //besedilo*
					$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_text".$this->db_table." WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' AND loop_id $loop_id";
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					if($sqlUserAnswer->st > 0)
						$isEmpty = false;				
				break;

				case 4: //besedilo*
					$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_text".$this->db_table." WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' AND loop_id $loop_id";
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					if($sqlUserAnswer->st > 0)
						$isEmpty = false;				
				break;
				
				case 17: //ranking					
					$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_rating WHERE spr_id=".$row['id']." AND usr_id='".$this->usr_id."' AND loop_id $loop_id";
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					if($sqlUserAnswer->st > 0)
						$isEmpty = false;
				break;
				
				case 24: //mesan multigrid	
					// loop po podskupinah gridov
					$sqlSubGrid = sisplet_query("SELECT m.spr_id AS spr_id, s.tip AS tip, s.enota FROM srv_grid_multiple AS m LEFT JOIN srv_spremenljivka AS s ON m.spr_id=s.id WHERE m.parent='".$spremenljivka."'");
					while($rowSubGrid = mysqli_fetch_array($sqlSubGrid)){
						if($rowSubGrid['tip'] == 6){
							$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_grid".$this->db_table." WHERE spr_id = '".$rowSubGrid['spr_id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
						}
						elseif($rowSubGrid['tip'] == 16){
							$sqlUserAnswerString ="SELECT COUNT(*) AS st FROM srv_data_checkgrid".$this->db_table." WHERE spr_id = '".$rowSubGrid['spr_id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
						}
						else{
							$sqlUserAnswerString = "SELECT COUNT(*) AS st FROM srv_data_textgrid".$this->db_table." WHERE spr_id = '".$rowSubGrid['spr_id']."' AND usr_id = '".$this->usr_id."' AND loop_id $loop_id";
						}

						$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
						if($sqlUserAnswer->st > 0){
							$isEmpty = false;	
							break;
						}
					}	
				break;
				
				case 5: //nagovor	
					// Ce je nagovor v loopu, ga prikazemo
					if($this->loop_id != null)
						$isEmpty = false;
				break;
				
				case 26: //lokacija
                                        //za podtip izberi lokacijo je treba preverjat posebej, ker se vrstica pri userju vedno kreira ampak brez odgovora
                                        if($row['enota'] == 3){
                                            $qu = "SELECT count(*) as cnt FROM srv_data_map WHERE spr_id='".$row['id']."' AND usr_id='$this->usr_id' AND loop_id $loop_id AND (text NOT REGEXP '^[+\-]?[0-9]+$' OR text>=0);";
                                            $sql = sisplet_query($qu, 'obj');
                                            if($sql->cnt > 0)
						$isEmpty = false;
                                        }
                                        else{
                                            //$sqlUserAnswerString ="SELECT lat, lng, address, text FROM srv_data_map WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' ";
                                            $sqlUserAnswerString ="SELECT COUNT(*) AS st FROM srv_data_map WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' AND loop_id $loop_id";
                                            $sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
                                            if($sqlUserAnswer->st > 0)
                                                    $isEmpty = false;
                                        }
				break;
				
				case 27: //heatmap
					//$sqlUserAnswerString ="SELECT lat, lng, address, text FROM srv_data_heatmap WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' ";
					$sqlUserAnswerString ="SELECT COUNT(*) AS st FROM srv_data_heatmap WHERE spr_id='".$row['id']."' AND usr_id='".$this->usr_id."' AND loop_id $loop_id";
					$sqlUserAnswer = sisplet_query($sqlUserAnswerString, "obj");
					if($sqlUserAnswer->st > 0)
						$isEmpty = false;
				break;
				
				default:
					$isEmpty = false;
					//$isEmpty = true;
				break;
			}
			//echo "isEmpty na koncu: ".$isEmpty."</br>";
			if($isEmpty == true){
				return false;
			}
		}
        return true;
    }
	
	
	function displayIf($if=null){
		global $lang;
		//echo "</br> displayIf funkcija </br> ";
		$sql_if_string = "SELECT tip, number FROM srv_if WHERE id = '$if'";
		//echo "sql_if_string: ".$sql_if_string." </br>";
    	//$sql_if = sisplet_query("SELECT * FROM srv_if WHERE id = '$if'");
    	$sql_if = sisplet_query($sql_if_string);
    	$row_if = mysqli_fetch_array($sql_if);
		//echo "tip: ".$row_if['tip']." </br>";

        // Blok
		if($row_if['tip'] == 1)
			$output = strtoupper($lang['srv_block']).' ';
		// Loop
		elseif($row_if['tip'] == 2)
			$output = strtoupper($lang['srv_loop']).' ';
		// IF
		else
			$output = 'IF ';

		$output .= '('.$row_if['number'].') ';

        $sql = Cache::srv_condition($if);
        
        $bracket = 0;
        $i = 0;
         while ($row = mysqli_fetch_array($sql)) {

            if ($i++ != 0)
                if ($row['conjunction'] == 0)
                    $output .= ' and ';
                else
                    $output .= ' or ';

            if ($row['negation'] == 1)
                $output .= ' NOT ';

            for ($i=1; $i<=$row['left_bracket']; $i++)
				$output .=  ' ( ';

            // obicajne spremenljivke
            if ($row['spr_id'] > 0) {

				$row2 = Cache::srv_spremenljivka($row['spr_id']);
				
                // obicne spremenljivke
                if ($row['vre_id'] == 0) {
                    $row1 = Cache::srv_spremenljivka($row['spr_id']);
                // multigrid
                } elseif ($row['vre_id'] > 0) {
                    $sql1 = sisplet_query("SELECT variable FROM srv_vrednost WHERE id = '$row[vre_id]'");
                    $row1 = mysqli_fetch_array($sql1);
                } else
                    $row1 = null;

                $output .= $row1['variable'];

                // radio, checkbox, dropdown in multigrid
                if (($row2['tip'] <= 3 || $row2['tip'] == 6) && ($row['spr_id'] || $row['vre_id'])) {

                    if ($row['operator'] == 0)
                        $output .= ' = ';
                    else
                        $output .= ' != ';

                    $output .= '[';

                    // obicne spremenljivke
                    if ($row['vre_id'] == 0) {
                        $sql2 = sisplet_query("SELECT v.variable AS variable FROM srv_condition_vre c, srv_vrednost v WHERE cond_id='$row[id]' AND c.vre_id=v.id");

                        $j = 0;
                        while ($row2 = mysqli_fetch_array($sql2)) {
                            if ($j++ != 0) $output .= ', ';
                            $output .= $row2['variable'];
                        }
                    // multigrid
                    } elseif ($row['vre_id'] > 0) {
                        $sql2 = sisplet_query("SELECT g.variable AS variable FROM srv_condition_grid c, srv_grid g WHERE c.cond_id='$row[id]' AND c.grd_id=g.id AND g.spr_id='$row[spr_id]'");

                        $j = 0;
                        while ($row2 = mysqli_fetch_array($sql2)) {
                            if ($j++ != 0) $output .= ', ';
                            $output .= $row2['variable'];
                        }
                    }

                    $output .= ']';

                // textbox in nubmer mata drugacne pogoje in opcije
                } elseif ($row2['tip'] == 4 ||$row2['tip'] == 21 || $row2['tip'] == 7 || $row2['tip'] == 22) {

                    if ($row['operator'] == 0)
                        $output .= ' = ';
                    elseif ($row['operator'] == 1)
                        $output .= ' <> ';
                    elseif ($row['operator'] == 2)
                        $output .= ' < ';
                    elseif ($row['operator'] == 3)
                        $output .= ' <= ';
                    elseif ($row['operator'] == 4)
                        $output .= ' > ';
                    elseif ($row['operator'] == 5)
                        $output .= ' >= ';

                    $output .= '\''.$row['text'].'\'';

                }

            // recnum
            } elseif ($row['spr_id'] == -1) {

                $output .= 'mod(recnum, '.$row['modul'].') = '.$row['ostanek'];

			} 

            for ($i=1; $i<=$row['right_bracket']; $i++)
				$output .= ' ) ';
        }
        
        if ($row_if['label'] != '') {
	        $output .= ' (';
	        $output .= ' '.$row_if['label'].' ';
	        $output .= ') ';      		
        }

		return $output;
	}

	/* poisce, ce ima podani element parenta, ki je loop
	* 
	*/
	function find_parent_loop ($element_spr=null, $element_if=0) {
		
		//$sql = sisplet_query("SELECT parent FROM srv_branching WHERE element_spr = '$element_spr' AND element_if = '$element_if' AND ank_id='".$this->anketa['id']."'");
		$sql = sisplet_query("SELECT parent FROM srv_branching WHERE element_spr = '$element_spr' AND element_if = '$element_if' AND ank_id='".$this->anketa."'");
		if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
		$row = mysqli_fetch_array($sql);
		
		if ($row['parent'] == 0) return 0;
		
		$count = sisplet_query("SELECT COUNT(*) AS st FROM srv_if WHERE id = '$row[parent]' AND tip = '2'", "obj");
		if ($count->st > 0) {
            return $row['parent'];
        }else {
            return $this->find_parent_loop(0, $row['parent']);
        }
	}	
	
		 /**
    * poisce naslednjo vre_id v loopu
    * 
    */
    function findNextLoopId ($if_id=0) {
		if ($if_id == 0) {
			$sql = sisplet_query("SELECT if_id FROM srv_loop_data WHERE id='$this->loop_id'");
			$row = mysqli_fetch_array($sql);
			$if_id = $row['if_id'];
			$loop_id = $this->loop_id;
		} else{
			$loop_id = 0;
		}

		$sql = sisplet_query("SELECT spr_id, max FROM srv_loop WHERE if_id = '$if_id'");
		$row = mysqli_fetch_array($sql);
		$spr_id = $row['spr_id'];
		$max = $row['max'];
		
		$spr = Cache::srv_spremenljivka($spr_id);
		//echo "spr tip: ".$spr['tip']."</br>";


		if ($spr['tip'] == 2 || $spr['tip'] == 3 || $spr['tip'] == 9) {
			$data_vrednost = array();
			if($spr['tip'] == 9){
				if($this->usr_id){
					$sql1String = "SELECT vre_id FROM srv_data_text".$this->db_table." WHERE spr_id='$spr_id' AND usr_id='".$this->usr_id."' ";
				}else{
					$sql1String = "SELECT vre_id FROM srv_data_text".$this->db_table." WHERE spr_id='$spr_id' ";
				}
			}
			else{
 				if($this->usr_id){
					$sql1String = "SELECT vre_id FROM srv_data_vrednost".$this->db_table." WHERE spr_id='$spr_id' AND usr_id='".$this->usr_id."'";
				}else{
					$sql1String = "SELECT vre_id FROM srv_data_vrednost".$this->db_table." WHERE spr_id='$spr_id' ";
				}				
			}
			//echo "sql1String: ".$sql1String."</br>";
			$sql1 = sisplet_query($sql1String);
			while ($row1 = mysqli_fetch_array($sql1)) {
				$data_vrednost[$row1['vre_id']] = 1;
			}
			
			$vre_id = '';
			$i = 1;
			//$sql = sisplet_query("SELECT * FROM srv_loop_vre WHERE if_id='$if_id'");

			$sql = sisplet_query("SELECT lv.vre_id AS vre_id, lv.tip AS tip FROM srv_loop_vre lv, srv_vrednost v WHERE lv.if_id='$if_id' AND lv.vre_id=v.id ORDER BY v.vrstni_red ASC");
			while ($row = mysqli_fetch_array($sql)) {
				
				if ($row['tip'] == 0) {			// izbran
					if ( isset($data_vrednost[$row['vre_id']]) ) {
						$vre_id .= ', '.$row['vre_id'];
						$i++;
					}
				} elseif ($row['tip'] == 1) {	// ni izbran
					if ( !isset($data_vrednost[$row['vre_id']]) ) {
						$vre_id .= ', '.$row['vre_id'];
						$i++;
					}
				} elseif ($row['tip'] == 2) {	// vedno
					$vre_id .= ', '.$row['vre_id'];
					$i++;
				}								// nikoli nimamo sploh v bazi, zato ni potrebno nic, ker se nikoli ne prikaze
				
				if ($i > $max && $max>0) break;
			}
			
			$vre_id = substr($vre_id, 2);
			
			if ($vre_id == '') return null;

			//Stari query srv_vrednost se ni nikjer uporabljal
			//$sql = sisplet_query("SELECT l.* FROM srv_loop_data l, srv_vrednost v WHERE l.if_id='$if_id' AND l.id > '$loop_id' AND l.vre_id IN ($vre_id) AND l.vre_id=v.id ORDER BY l.id ASC");
			//Problematicni sql, ker ocitno tale zadeva z obj ne dela najbolje
			//$sql = sisplet_query("SELECT id FROM srv_loop_data WHERE if_id='$if_id' AND id > '$loop_id' AND vre_id IN ($vre_id) ORDER BY id ASC", "obj");	
						
			$sqlTestString = "SELECT id FROM srv_loop_data WHERE if_id='$if_id' AND id > '$loop_id' AND vre_id IN ($vre_id) ORDER BY id ASC";
			//echo "sqlString: ".$sqlTestString."</br>";
			$sqlTest = sisplet_query($sqlTestString);
			$rowTest = mysqli_fetch_array($sqlTest);
			
			if (count($sql) > 0)
				return $rowTest['id'];
				//return $sql->id;
			else
				return null;
				
		// number
		} elseif ($spr['tip'] == 7) {
			
			//$sql1 = sisplet_query("SELECT text FROM srv_data_text".$this->db_table." WHERE spr_id='$spr_id' AND usr_id='".$this->getUserId()."'");
			$sql1String = "SELECT text FROM srv_data_text".$this->db_table." WHERE spr_id='$spr_id' AND usr_id='".$this->usr_id."'";
			//echo "sql1String: ".$sql1String."</br>";
			$sql1 = sisplet_query($sql1String);
			$row1 = mysqli_fetch_array($sql1);
			
			$num = (int)$row1['text'];

			//$countLoopManjsi = sisplet_query("SELECT COUNT(*) AS st FROM srv_loop_data WHERE if_id='$if_id' AND id <= '$loop_id'", "obj");
			$countLoopManjsiString = "SELECT COUNT(*) AS st FROM srv_loop_data WHERE if_id='$if_id' AND id <= '$loop_id'";
			$countLoopManjsi = sisplet_query($countLoopManjsiString);
			$rowcountLoopManjsi = mysqli_fetch_array($countLoopManjsi);
			//echo "countLoopManjsiString: ".$countLoopManjsiString."</br>";
			//if ($countLoopManjsi->st >= $num || ($countLoopManjsi->st >= $max && $max>0)) {
			if ($rowcountLoopManjsi['st'] >= $num || ($rowcountLoopManjsi['st'] >= $max && $max>0)) {
                return null;
            }

			//$loopVecji = sisplet_query("SELECT id FROM srv_loop_data WHERE if_id='$if_id' AND id > '$loop_id'", "obj");
			$loopVecjiString = "SELECT id FROM srv_loop_data WHERE if_id='$if_id' AND id > '$loop_id'";
			//echo "loopVecjiString: ".$loopVecjiString."</br>";
			$loopVecji = sisplet_query($loopVecjiString);
			$rowcountLoopVecji = mysqli_fetch_array($loopVecji);
			//if (count($loopVecji) > 0) {
			if (mysqli_num_rows($loopVecji) > 0) {
				//return $loopVecji->id;				
				return $rowcountLoopVecji['id'];
            }else {
                return null;
            }
			
		}
    }
	
		/**
    * @desc V podanem stringu poisce spremenljivke in jih spajpa z vrednostmi
    */
    function dataPiping ($text='') {
    	Common::getInstance()->Init($this->anketa);
        return Common::getInstance()->dataPiping($text, $this->usr_id, $this->loop_id);
	}
	
	#funkcija ki skrbi za encode dolocenih spornih delov besedila v latex-u prijazno
	function encodeTextHere($text='', $vre_id=0){
		global $site_path, $lang;
		
		//echo "Encoding ".$text."</br>";
		//echo "vre_id: ".$vre_id."</br>";
		
		$this->path2UploadedImages = $site_path.'uploadi/editor/';
		if($text == ''){	//ce ni teksta, vrni se
			return;			
		}
		$textOrig = $text;
		$findme = '<br />';
		$findmeLength = strlen($findme);
		$findImg = '<img';		
		$findImgLength = strlen($findImg);
		$findUl = '<ul';
		$findUlLength = strlen($findUl);
		$findLi = '<li';
		
		$findPar = '<p>';
		
		$pos = strpos($text, $findme);
		$posImg = strpos($text, $findImg);
		$posUl = strpos($text, $findUl);
		$posLi = strpos($text, $findLi);
		$posPar = strpos($text, $findPar);

        
		//ureditev posebnih karakterjev za Latex	http://www.cespedes.org/blog/85/how-to-escape-latex-special-characters, https://en.wikibooks.org/wiki/LaTeX/Special_Characters#Other_symbols
		$text = str_replace('\\','\textbackslash{} ',$text);
		//$text = str_replace('{','\{',$text);		
		//$text = str_replace('}','\}',$text);	
		$text = str_replace('$','\$ ',$text);
		$text = str_replace('#','\# ',$text);
		$text = str_replace('%','\% ',$text);		
		$text = str_replace('€','\euro',$text);		
		$text = str_replace('^','\textasciicircum{} ',$text);		
		$text = str_replace('_','\_ ',$text);	
		$text = str_replace('~','\textasciitilde{} ',$text);		
		$text = str_replace('&amp;','\&',$text);
		//$text = str_replace('&lt;','\textless ',$text);
		$text = str_replace('&lt;','\textless',$text);
		//$text = str_replace('&gt;','\textgreater ',$text);
		$text = str_replace('&gt;','\textgreater',$text);
		//ureditev posebnih karakterjev za Latex - konec
		
		//ureditev preureditve html kode ul in li v latex itemize
 		if($posUl !== false){			
			//echo "text prej: ".$text."</br>";
			$numOfUl = substr_count($text, $findUl);	//stevilo '<ul' v tekstu
			//echo "numOfUl ".$numOfUl."</br>";			
			######################
			//if($numOfUl!=0){
			if($numOfUl!=0 && $posLi !== false){	//ce imamo ul in li				
				$text = str_replace('<ul>','\begin{itemize} ', $text);
				$text = str_replace('<li>','\item ', $text);
				$text = str_replace('</ul>','\end{itemize} ', $text);					
			}
			//echo "prazno v html: ".strpos($text, '\r')."</br>";
			//echo "text potem: ".$text."</br>";
			######################
		}
		//ureditev preureditve html kode ul in li v latex itemize - konec
				
		if($posPar !== false){	//ce je kaksen html tag <p>, dodaj prazno vrstico oz. break			
			if($numOfUl!=0 && $posLi !== false){	//ce imamo ul in li	
				$divider = ' ';
			}else{
				$divider = ' \\ \\\\ ';
			}
			//$text = str_replace('<p>',' ', $text);
			//$text = str_replace('<p>',' \break ', $text);
			//$text = str_replace('<p>',' \\\\ ', $text);

			$text = str_replace('<p>', $divider, $text);
		}

 		if($pos === false && $posImg === false) {	//v tekstu ni br in img 		
			
			$text = preg_replace("/(\R){2,}/", "$1", $text);
			return strip_tags($text);
		}else {	//v tekstu sta prisotna br ali img		
			$text2Return = '';	//tekst ki bo vrnjen
			
			//ureditev preureditev html kode za novo vrstico v latex, ureditev prenosa v novo vrstico
			if($pos !== false){
				$pos = strpos($text, $findme);
				$numOfBr = substr_count($text, $findme);	//stevilo '<br />' v tekstu
				for($i=0; $i<$numOfBr; $i++){
					if($i == 0){	//ce je prvi najdeni '<br />'
						$textPrej = substr($text, 0, $pos);
						$textPotem = substr($text, $pos+$findmeLength);
						if($i == $numOfBr-1){
							//$text2Return .= $textPrej.' \break '.$textPotem;
							$text2Return .= $textPrej.' \\\\ '.$textPotem;
						}else{
							//$text2Return .= $textPrej.' \break ';
							$text2Return .= $textPrej.' \\\\ ';
						}
					}else{	//drugace
						$pos = strpos($textPotem, $findme);
						$textPrej = substr($textPotem, 0, $pos);
						$textPotem = substr($textPotem, $pos+$findmeLength);
						if($i == $numOfBr-1){
							//$text2Return .= $textPrej.' \break '.$textPotem;
							$text2Return .= $textPrej.' \\\\ '.$textPotem;
						}else{
							//$text2Return .= $textPrej.' \break ';
							$text2Return .= $textPrej.' \\\\ ';
						}
					}
				}
				$text = $text2Return;
			}			
			//ureditev preureditev html kode za novo vrstico v latex, ureditev prenosa v novo vrstico - konec
			//echo "text potem: ".$text."</br>";
			$text = preg_replace("/(\R){2,}/", "$1", $text);
			return strip_tags($text);	//vrni tekst brez html tag-ov
		}
	}
	#funkcija ki skrbi za encode dolocenih spornih delov besedila v latex-u prijazno - konec
	
}