summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveyPostProcess.php
blob: 2af0cbf413139c935640981f4347dfec5bb54264 (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
<?php

/**
* 
* V tem classu so funkcije, ki se ticejo postprocessinga podatkov
* ala compute, recode, code ...
* 
*/

class SurveyPostProcess {
	
	private $anketa;
	private $spremenljivka;
	
	private $db_table = '';
	
	private $SDF = null;
	
	/**
	* post procesiranje podatkov ankete
	* 
	* @param int $anketa ID ankete
	*/
	function __construct ($anketa) {
		
		$this->anketa = $anketa;
		
		SurveyInfo::getInstance()->SurveyInit($this->anketa);
		
		$this->db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();
		
		#inicializiramo class za datoteke
		$this->SDF = SurveyDataFile::get_instance();
		$this->SDF->init($this->anketa);
	}
	
	/**
	* izris taba za kalkulacijo
	* 
	*/
	function displayTab () {
		global $lang;
		
		echo '<fieldset id="calculations"><legend>'.$lang['srv_compute'].'</legend>';
		echo '<script>__vnosi=1;</script>';
		
		$b = new Branching($this->anketa);
		
		$sql = sisplet_query("SELECT s.id, s.naslov, s.variable FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='$this->anketa' AND s.tip='22'");
		$count = 0;
		if (mysqli_num_rows($sql) > 0) {
			
			echo '<p class="bottom16">'.$lang['srv_compute_list'].'</p>';
			
			while ($row = mysqli_fetch_array($sql)) {
				$count +=1;
				echo '<div class="icon_holder">';
				echo '<p>'.$row['naslov'].' '.$count.': '.$b->calculations_display(-$row['id'], 1).'</p>';
				echo '<span class="faicon edit aslink" onclick="calculation_editing(\'-'.$row['id'].'\'); return false;"></span>';
				echo '<span class="faicon trash empty aslink" onclick="brisi_spremenljivko(\''.$row['id'].'\'); return false;"></span>';
				echo '</div>';
			}
		}
		
		echo '<a class="noline" href="#" onClick="spremenljivka_new(0, 0, 1, 0, 22); return false;"><span class="faicon plus_circle link-right"></span>'.$lang['srv_add_compute'].'</a>';
		
		if (mysqli_num_rows($sql) > 0) {
			echo '<div class="button_holder">';
			echo '<button class="medium blue" onclick="postprocess_start_calculation(); return false;">'.$lang['srv_compute_start'].'</button>';				
			echo '</div>';
		}
		
		echo '</legend>';
	}
	
	/**
	* prikaze link za dodat novo kalkulacijo za post process
	* dodamo kar obicno kalkulacijo, pri zapiranju (detektiramo v JS) pa se poklice ajax_calculation_postprocess_save, ki gre cez celo bazo
	* 
	*/
	function displayLink () {
		global $lang;
		
		echo '<div style="float:right; margin-right:10px">';
		echo '<a href="" onclick="spremenljivka_new(0, 0, 1, 0, 22); return false;">'.$lang['srv_add_compute'].'</a>';
		echo '</div>';
		
	}
	
	/**
	* zavihek za avtomatsko rekodiranje
	* 
	*/
	function displayCodingAuto () {
		global $lang;
		
		echo '<fieldset><legend>'.$lang['srv_auto_coding'].'</legend>';
		
		SurveyAnalysis::Init($this->anketa);
		$freq = SurveyAnalysis::getFrequencys();
		
		$ok = false;

		echo '<div id="ok">';

		echo '<div class="setting_holder">';
		echo '<label for="mass_coding_spr_id">'.$lang['srv_mass_coding'].'</label>';

		echo '<select class="dropdown large w400" name="spr_id" id="mass_coding_spr_id">';
		
		foreach (SurveyAnalysis::$_HEADERS AS $key => $h) {
			
			$spr = explode('_', $key);
			
			if ( is_numeric( $spr['0'] ) ) {
				
				foreach ($h['grids'] AS $grids_k => $grids_v) {
					
					foreach ($grids_v['variables'] AS $variables_k => $variables_v) {
						
						if ($variables_v['text'] == '1'/* || $h['tip'] == 19*/) {
							$ok = true;
							echo '<option value="'.$variables_v['sequence'].'-'.$spr[0].'">('.$h['variable'].') '.$h['naslov'].($variables_v['naslov']!=''?' - '.$variables_v['naslov']:'').'</option>';
						}						
					}					
				}
			}
		}
		
		echo '</select>';
                                    
		echo '</div>';

		echo '<div class="setting_holder">';
		
		echo '<label for="coding_type">'.$lang['srv_mass_coding_count'].'</label>';
		echo '<select class="dropdown large w400" name="coding_type" id="coding_type">';
		echo '<option value="1">'.$lang['srv_coding_one'].'</option>';
		echo '<option value="2">'.$lang['srv_coding_multi'].'</option>';
		echo '</select> ';

		echo '</div>';

		echo '<div class="button_holder intext">';
		echo '<button class="medium blue" type="submit" onclick="$(this).prop(\'disabled\', true); mass_coding($(\'#mass_coding_spr_id\').val(), $(\'#coding_type\').val()); return false;">'.$lang['srv_mass_coding_run'].'</button>';              
		echo '</div>';
		echo '</div>';
		
		if (!$ok) {
			?><script> $('#ok').addClass('displayNone'); </script><?
			echo '<p class="bold">'.$lang['srv_mass_coding_no_vars'].'</p>';
		}
		else {
			?><script> $('#ok').removeClass('displayNone'); </script><?
		}
		
		echo '<p class="top16">'.$lang['srv_mass_coding_txt1'].'</p>';
		echo '<p class="top16">'.$lang['srv_mass_coding_txt2'].'</p>';
		echo '<p class="top16">'.$lang['srv_mass_coding_txt3'].'</p>';
		echo '<p class="top16">'.$lang['srv_mass_coding_txt4'].'</p>';
		
		echo '</fieldset>';
	}
	
	/**
	* zavihek za rocno rekodiranje
	* 
	*/
	function displayCoding () {
		global $lang;
		global $global_user_id;
		
		//$this->updateTracking($this->anketa, 4);
		echo '<fieldset><legend>'.$lang['srv_hand_coding'].'</legend>';
		echo '<div id="analiza_data">';
		Timer::StartTimer($lang['srv_collectData']);
		SurveyDataDisplay::Init($this->anketa);
		
		echo '<div id="div_analiza_filtri_right" class="analiza" style="left:auto"><a href="#" onclick="$(\'#filters\').slideToggle(); $(this).find(\'span\').toggleClass(\'plus\').toggleClass(\'minus\'); return false"><span class="faicon plus"></span> '.$lang['srv_advanced'].'</a></div>';
		
		echo '<div id="filters" style="display:none; height:80px; margin-top:10px">';
		SurveyDataDisplay::displayFilters();
		echo '</div>';
		
		SurveyAnalysis::Init($this->anketa);
		$freq = SurveyAnalysis::getFrequencys();
		
		SurveyVariablesProfiles::Init($this->anketa, $global_user_id);
		$variables = SurveyVariablesProfiles::getProfileVariables(-1);
		if (SurveyVariablesProfiles::getCurentProfileId() != -1)
			$variables = array();
		
		echo '<p class="bottom16">'.$lang['srv_hand_coding_text'] . '</p>';

		echo '<div class="setting_holder">';

		echo '<div class="setting_item">';
		echo '<input type="radio" name="filter" '.(count($variables)==0?'checked':'').'>';
		echo '<label id="link_variable_profile_remove" onclick="removeVariableProfile();"> '.$lang['srv_coding_spr_1'].'</label>';
		echo '</div>';
		echo '<div class="setting_item">';
		echo '<input type="radio" name="filter" '.(count($variables)>0?'checked':'').' onclick="return false;">';
		echo '<label onclick="coding_filter($(\'#mass_coding_spr_id\').val());"> '.$lang['srv_coding_spr'].':</label>';
		echo '<select name="spr_id" id="mass_coding_spr_id" class="dropdown medium '.(count($variables)>0?'':'disabled').'" onchange="coding_filter($(\'#mass_coding_spr_id\').val()); return false;">';

		SurveyAnalysis::Init($this->anketa);
		$freq = SurveyAnalysis::getFrequencys();
		
		foreach (SurveyAnalysis::$_HEADERS AS $key => $h) {
			
			$spr = explode('_', $key);
			
			if ( is_numeric( $spr['0'] ) ) {
				
				foreach ($h['grids'] AS $grids_k => $grids_v) {
					
					foreach ($grids_v['variables'] AS $variables_k => $variables_v) {
						
						if ($variables_v['text'] == '1'/* || $h['tip'] == 19*/) {
							
							echo '<option value="'.$variables_v['sequence'].'-'.$spr[0].'" '.(in_array($spr[0].'_0', $variables)?'selected':'').'>('.$h['variable'].') '.$h['naslov'].($variables_v['naslov']!=''?' - '.$variables_v['naslov']:'').'</option>';
						}	
					}			
				}
			}
		}
		
		echo '</select> ';
		echo '</div>';
					
		echo '<div class="setting_item coding-refresh"><p class="top4 bottom4"><a href="index.php?anketa='.$this->anketa.'&a=data&m=coding">'.$lang['src_coding_refresh'].'</a></p></div>';
		
		echo '</div>';
		
		
		SurveyDataDisplay::displayVnosiHTML();
		echo '</div>'; // div_analiza_data
		Timer::GetTimer($lang['srv_collectData']);

		
		// div za popup editiranje
		echo '<div id="coding"></div>';
		echo '</fieldset>';
	}
	
	function ajax() {
		
		$this->anketa = $_REQUEST['anketa'];
		$this->spremenljivka = isset($_REQUEST['spremenljivka']) ? $_REQUEST['spremenljivka'] : null;
		
		if ($_GET['a'] == 'postprocess_start_calculation') {
			$this->ajax_postprocess_start_calculation();
		
		} elseif ($_GET['a'] == 'edit_data_question') {
			$this->ajax_edit_data_question();
			
		} elseif ($_GET['a'] == 'edit_data_question_save') {
			$this->ajax_edit_data_question_save(0);
			
		} elseif ($_GET['a'] == 'edit_data_question_upload_delete') {
			$this->ajax_edit_data_question_upload_delete();
			
		} elseif ($_GET['a'] == 'get_inline_edit') {
			$this->ajax_get_inline_edit();
			
		} elseif ($_GET['a'] == 'get_inline_edit_all') {
			$this->ajax_get_inline_edit_all();
			
		} elseif ($_GET['a'] == 'coding') {
			$this->ajax_coding();
			
		} elseif ($_GET['a'] == 'coding_save') {
			$this->ajax_coding_save();
			
		} elseif ($_GET['a'] == 'vrednost_new') {
			$this->ajax_vrednost_new();
			
		} elseif ($_GET['a'] == 'spremenljivka_new') {
			$this->ajax_spremenljivka_new();
			
		} elseif ($_GET['a'] == 'tip') {
			$this->ajax_tip();
			
		} elseif ($_GET['a'] == 'mass_coding') {
			$this->ajax_mass_coding();
			
		} elseif ($_GET['a'] == 'coding_merge') {
			$this->ajax_coding_merge();
			
		} elseif ($_GET['a'] == 'coding_filter') {
			$this->ajax_coding_filter();	
		}	
	}
	
	/**
	* pozene racunanje kalkulacij na celotni anketi
	* 
	*/
	function ajax_postprocess_start_calculation () {
		Common::updateEditStamp();
		
		$sql = sisplet_query("SELECT s.id FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='$this->anketa' AND s.tip='22'");
		if (!$sql) echo 'err34234'.mysqli_error($GLOBALS['connect_db']);
		
		$spremenljivk = mysqli_num_rows($sql);
		
		$i = 0;
		while ($row = mysqli_fetch_array($sql)) {
			$this->calculate_spremenljivka($row['id']);
		}
		
		self::forceRefreshData($this->anketa);
				
		// redirect
		echo 'index.php?anketa='.$this->anketa.'&a=data';
	}
	
	/**
	* za spremenljivko (tipa 22 - kalkulacija) gre cez celo bazo (beremo iz tekstovne datoteke) in preracuna vrednosti
	* 
	*/
	function calculate_spremenljivka ($spremenljivka = 0) {
		
		require('definition.php');
		
		if ($spremenljivka != 0)
			$this->spremenljivka = $spremenljivka;
		
		$row = Cache::srv_spremenljivka($this->spremenljivka);
		if ($row['tip'] != 22) return;
				
		$_fileStatus = $this->SDF->getStatus() ;

		# če imamo data datoteko (novo=1 in staro=0)
		if ($_fileStatus == FILE_STATUS_OK || $_fileStatus == FILE_STATUS_OLD) {
			$_dataFileName = $this->SDF->getDataFileName();
			
			$missing = $this->generateCalculationMissing(-$this->spremenljivka);
			$calculation = $this->generateCalculationAWK(-$this->spremenljivka);			
			
			// for(i=1;i<=NF;i++) if ($i < 0) $i=0;	// nastavi vse <0 na 0
			
			if (IS_WINDOWS) {
				$cmd = 'awk -F"|" "{{OFS=\"\"} {ORS=\"\"}} { if ( '.$missing.' ) calc=-1; else calc = '.$calculation.' ; print \"('.$this->spremenljivka.',\",calc,\",\",$1,\"),\"}" '.$_dataFileName;
			} else {
				$cmd = 'awk -F"|" \'{{OFS=""} {ORS=""}} { if ( '.$missing.' ) calc=-1; else calc = '.$calculation.' ; print "('.$this->spremenljivka.',",calc,",",$1,"),"}\' '.$_dataFileName;
			}
			
			$out = shell_exec($cmd);
			
			// rezultat lahko vrne nan - spremenimo na -1
			$out = str_replace('-nan', '-1', $out);
			$out = str_replace('nan', '-1', $out);
			
			// pobrisemo zadnjo vejico
			$values = substr($out, 0, strrpos($out, ",") );
			
			// Zaokrozimo na doloceno stevilo decimalk
			$decimals = $row['decimalna'];
			$calc_val_array = explode(',', $values);
			for($i=1; $i<count($calc_val_array); $i+=3){			
				$calc_val_array[$i] = round(floatval($calc_val_array[$i]), $decimals);
			}
			$values = implode(',', $calc_val_array);
			
			$sql = sisplet_query("DELETE FROM srv_data_text".$this->db_table." WHERE spr_id='".$this->spremenljivka."'");
			if (!$sql) echo 'err3324'.mysqli_error($GLOBALS['connect_db']);
			
			if ($values != '') {
			
				$sql_query = "INSERT INTO srv_data_text".$this->db_table." (spr_id, text, usr_id) VALUES " . $values . "";
	
				$sql = sisplet_query($sql_query);
				if (!$sql) echo 'err766567'.' '.$sql_query.' '.mysqli_error($GLOBALS['connect_db']);
			
			}
			
			$sql = sisplet_query("UPDATE srv_anketa SET edit_time = NOW() WHERE id = '$this->anketa'");
			if (!$sql) echo 'err6563'.sisplet_query($sql);
			
		} else {
			//echo 'filestatus' . $_fileStatus;
		}
				
	}
	
	/**
	* vrne vrstni red stolpca v datoteki s podatki za podano spremenljivko
	* 
	* @param mixed $spr_id
	* @param mixed $vre_id
	* @return mixed
	*/
	function getSequence ($spr_id, $vre_id = null, $grd_id = null) {
		
		$header = $this->SDF->getHeaderVariable($spr_id.'_0');

	 	switch ($header['tip']) {
			case 1 :		// radio
			case 3 :		// dropdown
			case 4 :		// text -??
			case 21:		// beseilo?
			case 22:		// compute
			case 25:		// kvota
			    return $header['grids']['0']['variables']['0']['sequence'];
			break;
			case 7:			// number
				return $header['grids']['0']['variables'][$grd_id]['sequence'];
			case 16:		// multicheckbox
			case 20:		// multinumber
				foreach ($header['grids'] AS $grids) {
					foreach ($grids['variables'] AS $variables) {
						if ($variables['vr_id'] == $vre_id && $variables['gr_id'] == $grd_id)
							return ($variables['sequence']);
						}
				}
			case 17:		// razvrscanje
	        case 18:		// vsota
			default :		// multigrid, checkbox
				foreach ($header['grids'] AS $grids) {
					foreach ($grids['variables'] AS $variables) {
						if ($variables['vr_id'] == $vre_id)
							return ($variables['sequence']);
						}
				}
			    return ;
			break;
	 	}
	 }
	
	/**
    * @desc zgenerira kalkulacijo za vstavitev v awk
    */
    function generateCalculationAWK ($condition) {
		
        $sql = sisplet_query("SELECT * FROM srv_calculation WHERE cnd_id = '$condition' ORDER BY vrstni_red ASC");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
                
        $i = 0;
        $expression = '';
        while ($row = mysqli_fetch_array($sql)) {

        	$row1 = Cache::srv_spremenljivka($row['spr_id']);
        	
            if ($i++ != 0)
                if ($row['operator'] == 0)
                    $expression .= ' + ';
                elseif ($row['operator'] == 1)
                    $expression .= ' - ';
                elseif ($row['operator'] == 2)
                    $expression .= ' * ';
                elseif ($row['operator'] == 3)
                    $expression .= ' / ';

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

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

                $seq = $this->getSequence($row['spr_id'], $row['vre_id'], $row['grd_id']);
                if ($seq > 0)
                	$expression .= ' $'.$seq.' ';
                else
                	$expression .= ' 0 ';

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

                $expression .= $row['number'];

            }

            for ($i=1; $i<=$row['right_bracket']; $i++)
                $expression .= ' ) ';

        }

        return '('.$expression.')';
    }
	
	/**
    * @desc zgenerira pogoj, ki preveri, ce je kateri od odgovorov missing - potem je tudi kalkulacija missing
    */
    function generateCalculationMissing ($condition) {

        $sql = sisplet_query("SELECT * FROM srv_calculation WHERE cnd_id = '$condition' ORDER BY vrstni_red ASC");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
                
        $i = 0;
        $expression = '';
        while ($row = mysqli_fetch_array($sql)) {

        	$row1 = Cache::srv_spremenljivka($row['spr_id']);

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

				$seq = $this->getSequence($row['spr_id'], $row['vre_id'], $row['grd_id']);
                if ($seq > 0) {
                	if ($expression != '') $expression .= ' || ';
                	$expression .= ' $'.$seq.' < 0 ';
				}
            
            }

        }

        if ($expression == '') $expression = 'false';
        return $expression;
    }
	
	function ajax_edit_data_question() {
		global $lang;
		
		/*?>
		  <link rel="stylesheet" href="http://localhost/fdv/cms2/main/survey/skins/Modern.css" type="text/css" media="screen" />
		<?*/
		
		$spr_id = $_POST['spr_id'];
		$usr_id = $_POST['usr_id'];
		
		// preverimo, ce gre za multiple tabelo
		$sql = sisplet_query("SELECT parent FROM srv_grid_multiple WHERE spr_id='$spr_id' AND ank_id='$this->anketa'");
		$row = mysqli_fetch_array($sql);
		if (mysqli_num_rows($sql) > 0)
			$spr_id = $row['parent'];
		
		echo '<div id="preview_spremenljivka">';
		echo '<div id="spremenljivka_preview">';
        
        echo '<div class="popup_close"><a href="#" onClick="edit_data_close(); return false;">✕</a></div>';

		echo '<form name="edit_data" method="post" action="ajax.php?t=postprocess&a=edit_data_question_save&anketa='.$this->anketa.'" onsubmit="edit_data_question_save(); return false;">';
		echo '<input type="submit" id="submit" style="display:none" />'; // workaround, ker se drugace ne poklice onsubmit
		echo '<input type="hidden" name="spr_id" value="'.$spr_id.'" />';
		echo '<input type="hidden" name="usr_id" value="'.$usr_id.'" />';

		include_once('../../main/survey/app/global_function.php');
		new \App\Controllers\SurveyController(true);
		save('anketa', $this->anketa);
		save('forceShowSpremenljivka', true);
		save('question_edit', true);
		\App\Controllers\Vprasanja\VprasanjaController::getInstance()->displaySpremenljivka($spr_id);
		
		echo '<div class="buttons_holder">';

		echo '<button class="medium white-blue" onclick="javascript:edit_data_close();">'.$lang['srv_cancel'].'</button>';
		echo '<button class="medium blue" onClick="javascript:$(\'#submit\').click(); return false;">'.$lang['srv_potrdi'].'</button>';
		
		echo '</div>';
		
		echo '</form>';
		
		/*echo '<div class="clr"></div>';*/
		
		echo '</div>';
		echo '</div>';
		
	}
	
	function ajax_edit_data_question_save ($refresh = 1) {
		Common::updateEditStamp();
		
		// Preverimo ce gre za prvo popravljanje podatkov in avtomatskoustvarimo arhiv podatkov ce je potrebno
		$sas = new SurveyAdminSettings();
		$sas->checkFirstDataChange();
		
		$spr_id = $_POST['spr_id'];
		$usr_id = $_POST['usr_id'];

		include_once('../../main/survey/app/global_function.php');
		new \App\Controllers\SurveyController(true);
		save('anketa', $this->anketa);
		save('forceShowSpremenljivka', true);
		save('usr_id', $usr_id);
		\App\Models\SaveSurvey::getInstance()->posted(0, $spr_id);
		
		/*if ($refresh == 1)
			header("Location: index.php?anketa=".$this->anketa."&a=data&m=edit");*/
	}

    // Pobrisemo upload datoteke v urejanju podatkov
    function ajax_edit_data_question_upload_delete () {
		Common::updateEditStamp();
				
		$spr_id = $_POST['spr_id'];
		$usr_id = $_POST['usr_id'];
		$code = $_POST['code'];

        $s = sisplet_query("DELETE FROM srv_data_upload WHERE ank_id='".$this->anketa."' AND usr_id='".$usr_id."' AND code='".$code."'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);

        $s2 = sisplet_query("DELETE FROM srv_data_text".$this->db_table." WHERE spr_id='".$spr_id."' AND usr_id='".$usr_id."'");
		if (!$s2) echo mysqli_error($GLOBALS['connect_db']);

        $this->ajax_edit_data_question();

        self::forceRefreshData($this->anketa);
	}
	
	function ajax_get_inline_edit () {
		
		$spr_id = $_POST['spr_id'];
		
		echo '<form action="ajax.php?t=postprocess&a=edit_data_question_save&anketa='.$this->anketa.'">';
		echo '<input type="hidden" name="spr_id" value="'.$spr_id.'" />';
		echo '<input type="hidden" name="visible_'.$spr_id.'" value="1" />';

		echo '<select class="fullwidth dropdown" name="vrednost_'.$spr_id.'" onchange="edit_data_inline_edit_save(this.parentNode.getAttribute(\'name\'));">';
		$sql = sisplet_query("SELECT id, naslov, variable FROM srv_vrednost WHERE spr_id='$spr_id' ORDER BY vrstni_red ASC");
		while ($row = mysqli_fetch_array($sql)) {
			
			echo '<option value="'.$row['id'].'" variable="'.$row['variable'].'" class="'.$row['variable'].'">'.$row['naslov'].'</option>';
		}
		
		echo '</select>';
		echo '</form>';	
	}
	
	function ajax_get_inline_edit_all () {
		
		$spr = isset($_POST['spr']) ? $_POST['spr'] : array();
		
		$response = array();
		
		foreach ($spr AS $spr_id) {
			
			$output = array();
			
			$output['spr'] = $spr_id;
			$output['html'] = '';
			
			$output['html'] .= '<form action="ajax.php?t=postprocess&a=edit_data_question_save&anketa='.$this->anketa.'">';
			$output['html'] .= '<input type="hidden" name="spr_id" value="'.$spr_id.'" />';
			$output['html'] .= '<input type="hidden" name="visible_'.$spr_id.'" value="1" />';
			
			$output['html'] .= '<select class="fullwidth dropdown" name="vrednost_'.$spr_id.'" onchange="edit_data_inline_edit_save(this.parentNode.getAttribute(\'name\'));">';
						
			$sql = sisplet_query("SELECT id, naslov, variable FROM srv_vrednost WHERE spr_id='$spr_id' ORDER BY naslov ASC");
			while ($row = mysqli_fetch_array($sql)) {
				
				$output['html'] .= '<option value="'.$row['id'].'" variable="'.$row['variable'].'" class="'.$row['variable'].'">'.$row['naslov'].'</option>';	
			}
			
			$output['html'] .= '</select>';
			$output['html'] .= '</form>';
		
			$response[] = $output;
		}
		
		echo json_encode($response);
	}
	
	function ajax_coding ($editing = false) {
		global $lang;
		
		$usr_id = $_POST['usr_id'];
		$spr_id = $_POST['spr_id'];
		
		$row = Cache::srv_spremenljivka($spr_id);
		if ($row['coding'] > 0) {
			$coding_id = $row['coding']; 
		} else {
			$coding_id = $spr_id;
		}
		echo '<div class="popup_close"><a href="#" onClick="toggle_coding_div(); return false;">✕</a></div>';
		echo '<h2><span class="faicon fa-code-branch blue"></span>'.$lang['srv_hand_coding'].'</h2>';

		echo '<form id="coding_'.$usr_id.'">';
		
		echo '<input type="hidden" value="'.$usr_id.'" name="usr_id">';
		echo '<input type="hidden" value="'.$this->anketa.'" name="anketa">';
		
		$sql = sisplet_query("SELECT s.* FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='$this->anketa' AND s.coding='$coding_id' ORDER BY g.vrstni_red, s.vrstni_red");
		if (mysqli_num_rows($sql) > 0) {
			
			while ($row = mysqli_fetch_array($sql)) {
				
				echo '<fieldset style="position: relative">';

				//Zgornji sivi del
				echo '<div class="variable_name">';
				
				echo '<div class="osnovno">';
				echo '<p class=semi-bold>'.skrajsaj(strip_tags($row['naslov']), 20).'</p>';
				echo '<span class="faicon edit pointer blue" onclick="$(\'.edit_'.$row['id'].'\').toggle(); return false;" title="'.$lang['edit2'].'"></span>';
				echo '<span class="faicon delete empty blue pointer" onclick="brisi_spremenljivko(\''.$row['id'].'\'); return false;" title="'.$lang['srv_brisispremenljivko'].'"></span>';
				echo '</div>';

				echo '<div class="urejanje edit_'.$row['id'].'" style="display:none;">'.$lang['srv_tip'].': ';
				echo '<select name="tip" class="dropdown small" onchange="if (confirm(\''.$lang['srv_change_q_tip'].'\')) {coding_tip(\''.$row['id'].'\', \''.$usr_id.'\', $(this).val());} return false;"><option value="1" '.($row['tip']==1?'selected':'').'>'.$lang['srv_vprasanje_tip_1'].'</option><option value="2" '.($row['tip']==2?'selected':'').'>'.$lang['srv_vprasanje_tip_2'].'</option><option value="3" '.($row['tip']==3?'selected':'').'>'.$lang['srv_vprasanje_tip_3'].'</option></select>';
				echo '<a class="noline" href="index.php?anketa='.$this->anketa.'&spr_id='.$row['id'].'#branching_'.$row['id'].'">'.$lang['srv_napredno_urejanje'].'</a></div>';

				echo '</div>';

				//Spodnji del
				echo '<div class="variable_content">';
				
				echo '<input id="visible_'.$row['id'].'" type="hidden" value="1" name="visible_'.$row['id'].'">';
				
				if ($row['tip'] == 3) {
					echo '<select name="vrednost_'.$row['id'].'">';
				}
				$sql1 = sisplet_query("SELECT id, naslov FROM srv_vrednost WHERE spr_id='$row[id]' ORDER BY vrstni_red ASC");
				while ($row1 = mysqli_fetch_array($sql1)) {

					echo '<div class="setting_holder horizontal">';
					
					$sql2 = sisplet_query("SELECT * FROM srv_data_vrednost".$this->db_table." WHERE spr_id='$row[id]' AND vre_id='$row1[id]' AND usr_id='$usr_id'");
					if (mysqli_num_rows($sql2) > 0)
						$checked = 'checked="checked"'; else $checked = '';
					
					if ($row['tip'] == 1) {
						echo '<input type="radio" name="vrednost_'.$row['id'].'" id="spremenljivka_'.$row['id'].'_vrednost_'.$row1['id'].'" value="'.$row1['id'].'" '.$checked.' /><label for="spremenljivka_'.$row['id'].'_vrednost_'.$row1['id'].'"> '.$row1['naslov'].'</label>';
					} elseif ($row['tip'] == 2) {
						echo '<input type="checkbox" name="vrednost_'.$row['id'].'[]" id="spremenljivka_'.$row['id'].'_vrednost_'.$row1['id'].'" value="'.$row1['id'].'" '.$checked.' /><label for="spremenljivka_'.$row['id'].'_vrednost_'.$row1['id'].'"> '.$row1['naslov'].'</label>';
					} elseif ($row['tip'] == 3) {
						echo '<option value="'.$row1['id'].'" '.($checked!=''?'selected':'').'>'.$row1['naslov'].'</option>';
					}
					
					if ($row['tip'] == 1 || $row['tip'] == 2) {
						echo '<span style="display:none;" class="edit_'.$row['id'].'"><select class="dropdown small" onchange="coding_merge(\''.$row['id'].'\', \''.$row1['id'].'\', \''.$usr_id.'\', this.value)" style="width:150px">';
						echo '<option value="0">'.$lang['srv_coding_merge'].':</option>';
						
						$sql2 = sisplet_query("SELECT id, naslov FROM srv_vrednost WHERE spr_id='$row[id]' AND id != '$row1[id]' ORDER BY vrstni_red ASC");
						while ($row2 = mysqli_fetch_array($sql2))
							echo '<option value="'.$row2['id'].'">'.$row2['naslov'].'</option>';
						
						echo '</select>';
					}

					echo '</div>';
					
				}
				if ($row['tip'] == 3) {
					echo '</select>';
				}
				echo '<input type="text" class="text small" name="vrednost_new_'.$row['id'].'" value="" placeholder="'.$lang['srv_novavrednost'].'"/> <a href="#" onclick="coding_vrednost_new(\''.$row['id'].'\', \''.$usr_id.'\', $(\'input[name=vrednost_new_'.$row['id'].']\').val()); return false;"><span class="faicon add blue pointer" title="'.$lang['add'].'"></span></a>';

				echo '</div>'; #div.variable_content
				echo '</fieldset>';
			}
		} else {
			echo '<p class="italic bottom16">'.$lang['srv_coding_no_spr'].'</p>';
		}
		

		echo '<div class="button_holder left" id="dodaj_spr">';
		echo '<button class="medium blue" onclick="'.(true?' $(\'#coding_spr_new\').toggle(); $(\'#dodaj_spr\').toggle();':'').' return false;">'.$lang['srv_coding_new'].'</button>';
		echo '</div>';

		echo '<div class="setting_holder horizontal" id="coding_spr_new" '.(true?' style="display:none"':'').'>';
		echo '<p>'.$lang['name'].':</p>';
		echo '<div class="button_holder inline">';
		echo '<input type="text" class="text large" name="spremenljivka_new" value=""/>';
		echo '<button class="medium blue" onclick="coding_spremenljivka_new(\''.$coding_id.'\', \''.$usr_id.'\', $(\'input[name=spremenljivka_new]\').val()); return false;">'.$lang['add'].'</button>';
		echo '<button class="medium white-blue" onclick="$(\'#coding_spr_new\').toggle(); $(\'#dodaj_spr\').toggle(); return false;">'.$lang['edit1338'].'</button>';
		echo '</div>';
		echo '</div>';

		echo '<div class="button_holder left">';
		echo '<button class="medium white-blue" onclick="coding_save(\''.$usr_id.'\'); return false;">'.$lang['save'].'</button>';
		echo '</div>';
		
		echo '</form>';
		
		if ($editing !== false) {
			echo '<script> $(\'.edit_'.$editing.'\').show(); </script>';
		}
		
	}
	
	function ajax_coding_save () {
		Common::updateEditStamp();
		
		$usr_id = $_POST['usr_id'];
		
		foreach ($_POST AS $key => $val) {
			
			if ( substr($key, 0, 8) == 'visible_' ) {
				
				$spr_id = substr($key, 8);

				include_once('../../main/survey/app/global_function.php');
				new \App\Controllers\SurveyController(true);
				save('anketa', $this->anketa);
				save('forceShowSpremenljivka', true);
				save('usr_id', $usr_id);
				\App\Models\SaveSurvey::getInstance()->posted(0, $spr_id);
			
			}
		}
		
		self::forceRefreshData($this->anketa);
		
	}
	
	function ajax_vrednost_new () {
		Common::updateEditStamp();
		
		$usr_id = $_POST['usr_id'];
		
		$v = new Vprasanje($this->anketa);
		$v->spremenljivka = $_POST['spr_id'];
		
		$v->vrednost_new($_POST['naslov']);
		
		$this->ajax_coding();
		
		self::forceRefreshData($this->anketa);
		
	}
	
	function ajax_spremenljivka_new () {
		Common::updateEditStamp();
		global $global_user_id;
		
		$usr_id = $_POST['usr_id'];
		$spr_id = $_POST['spr_id'];
		
		ob_start();
		$ba = new BranchingAjax($this->anketa);
		$ba->ajax_spremenljivka_new($spr_id, 0, 0, 0, 2, 0);
		$spr_new = $ba->spremenljivka;
		ob_clean();
		
		SurveyVariablesProfiles::Init($this->anketa, $global_user_id);
		$variables = SurveyVariablesProfiles::getProfileVariables(0);
		if ( isset($variables['variables']) && count($variables['variables']) > 0 ) {
			SurveyVariablesProfiles::setProfileVariables(0, implode(',', $variables['variables']).','.$spr_new.'_0');
			SurveyVariablesProfiles::setDefaultProfile(0);
		}
		
		// spremenimo ime, coding
		$s = sisplet_query("UPDATE srv_spremenljivka SET variable_custom='1' WHERE id='$spr_id'");
					
		$var = Cache::get_spremenljivka($spr_id, 'variable');
		$i = 1;
		do {
			$variable = $var.'_coding_'.$i++;
			$sql = sisplet_query("SELECT s.id FROM srv_spremenljivka s, srv_grupa g WHERE variable='$variable' AND s.gru_id=g.id AND g.ank_id='".$this->anketa."'");
		} while (mysqli_num_rows($sql) > 0);
		
		if ($_POST['naslov'] != '')
			$naslov = $_POST['naslov'];
		else
			$naslov = $variable;
		
		$s = sisplet_query("UPDATE srv_spremenljivka SET coding='$spr_id', visible='0', naslov='".$naslov."', variable='".$variable."', variable_custom='1', size='0' WHERE id='$spr_new'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		$s = sisplet_query("DELETE FROM srv_vrednost WHERE spr_id='$spr_new'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		
		$s = sisplet_query("DELETE FROM srv_vrednost WHERE spr_id = '$spr_new'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		
		$this->ajax_coding();
		
		self::forceRefreshData($this->anketa);
		
	}
	
	
	function ajax_tip () {
		Common::updateEditStamp();
		
		$spr_id = $_POST['spr_id'];
		$tip = $_POST['tip'];
		
		$v = new Vprasanje($this->anketa);
		$v->change_tip($spr_id, $tip);
		
		Common::prestevilci($spr_id);
		
		$this->ajax_coding();
		
		self::forceRefreshData($this->anketa);
		
	}
	
	function ajax_coding_merge () {
		Common::updateEditStamp();
		
		$spr_id = $_POST['spr_id'];
		$vre_id = $_POST['vre_id'];
		$merge = $_POST['merge'];
		
		$s = sisplet_query("UPDATE srv_data_vrednost$this->db_table SET vre_id='$merge' WHERE vre_id='$vre_id' AND spr_id='$spr_id'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		
		$s = sisplet_query("DELETE FROM srv_vrednost WHERE id = '$vre_id'");
		if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		
		$this->ajax_coding($spr_id);
		
		self::forceRefreshData($this->anketa);
		
	}
	
	function ajax_mass_coding () {
		global $lang;
				
		$spr_new = $this->mass_coding_auto();
		
		$link = '<a href="index.php?anketa='.$this->anketa.'&a=data&m=coding&highlight_spr='.$spr_new.'&relevance=0">'.$lang['srv_hand_coding'].'</a>';
		
		// enajblamo labele
		session_start();
		$_SESSION['sid_'.$this->anketa]['dataIcons_labels'] = true;
		
		echo '<h2><span class="faicon blue fa-check-circle"></span>'.$lang['srv_auto_coding_end_title'].'</h2>';
		echo '<div class="popup_close"><a href="#" onclick="popupClose();">✕</a></div>';

		echo '<p>';
		
		printf($lang['srv_auto_coding_end'], $this->mass_coding_auto_vars, $link);
		
		echo '</p>';
        
		echo '<div class="button_holder">';
		echo '<button class="medium white-blue" onclick="popupClose(); window.location.reload(); return false;">'.$lang['srv_zapri'].'</button>';
		echo '</div>';
	}
	
	private $mass_coding_auto_vars = 0;
	
	function mass_coding_auto () {
		global $global_user_id;
		Common::updateEditStamp();
		
		global $lang;
		
		$coding_type = $_POST['coding_type'];
		
		$seq = $_POST['seq'];
		$seq = explode('-', $seq);
		
		$spr_id = (int)$seq[1];
		$sequence = (int)$seq[0];

		// frekvence besed poberemo iz analize
		SurveyAnalysis::Init($this->anketa);
		SurveyAnalysis::setUpFilter(1);
		$freq = SurveyAnalysis::getFrequencys();
		
		// imamo sekvenco polja (pozicijo v text fajlu)
		if ($sequence != false) {
			
			$besede = isset($freq[$sequence]['valid']) ? $freq[$sequence]['valid'] : array();
			$koreni = array();
			
			// sestavimo array korenov
			foreach ($besede AS $beseda => $count) {
				
				if (strlen( iconv("UTF-8", "ISO-8859-2", $beseda) ) >= 3) {	// manjsih od  3 znakov ne upostevamo
					
					if ($coding_type == 1) {
					
						$k = iconv("ISO-8859-2", "UTF-8", substr( iconv("UTF-8", "ISO-8859-2", $beseda), 0, round(strlen( iconv("UTF-8", "ISO-8859-2", $beseda) )*0.75) ) );
					
						if ( array_key_exists($k, $koreni) ) {
							$koreni[$k] += $count;
						} else {
							$koreni[$k] = $count;
						}
						
					} elseif ($coding_type == 2) {
						
						$razbita = explode(",", $beseda);
						foreach ($razbita AS $part) {
							$k = iconv("ISO-8859-2", "UTF-8", substr( iconv("UTF-8", "ISO-8859-2", $part), 0, round(strlen( iconv("UTF-8", "ISO-8859-2", $part) )*0.75) ) );
					
							if ( array_key_exists($k, $koreni) ) {
								$koreni[$k] += $count;
							} else {
								$koreni[$k] = $count;
							}
						}
						
					}
				}
			}
			
			// array obrnemo, da imamo vrstni_red => koren
			$i = 1;
			$koren = array();
			foreach ($koreni AS $key => $val) {
				$koren[$i++] = $key;
			}
			
			if (count($koren) > 0) {
				
				// naredimo novo spremenljivko
				ob_start();
				$ba = new BranchingAjax($this->anketa);
				$ba->ajax_spremenljivka_new($spr_id, 0, 0, 0, $coding_type, 0);
				$spr_new = $ba->spremenljivka;
				ob_clean();
				
				if ($spr_new > 0) {
					
					// spremenimo ime, coding
					$s = sisplet_query("UPDATE srv_spremenljivka SET variable_custom='1' WHERE id='$spr_id'");
					
					$var = Cache::get_spremenljivka($spr_id, 'variable');
					$i = 1;
					do {
						$variable = $var.'_coding_'.$i++;
						$sql = sisplet_query("SELECT s.id FROM srv_spremenljivka s, srv_grupa g WHERE s.variable='$variable' AND s.gru_id=g.id AND g.ank_id='".$this->anketa."'");
					} while (mysqli_num_rows($sql) > 0);
					
					$s = sisplet_query("UPDATE srv_spremenljivka SET coding='$spr_id', visible='0', naslov='".$variable."', variable='".$variable."', variable_custom='1', size='".count($koren)."' WHERE id='$spr_new'");
					if (!$s) echo mysqli_error($GLOBALS['connect_db']);
					$s = sisplet_query("DELETE FROM srv_vrednost WHERE spr_id='$spr_new'");
					if (!$s) echo mysqli_error($GLOBALS['connect_db']);
					
					// napolnimo vrednosti spremenljivke s koreni
					$koren_new = array();
					foreach ($koren AS $key => $val) {
						$val = ucfirst(strtolower( $val ));
						$s = sisplet_query("INSERT INTO srv_vrednost (id, spr_id, naslov, variable, vrstni_red) VALUES ('', '$spr_new', '$val', '$key', '$key')");
						if (!$s) echo mysqli_error($GLOBALS['connect_db']);
						
						$koren_new[strtolower($val)] = mysqli_insert_id($GLOBALS['connect_db']);
					}
					
					// gremo cez tekstovne podatke in vnasamo vrednosti
					$sql = sisplet_query("SELECT usr_id, text FROM srv_data_text".$this->db_table." WHERE spr_id='$spr_id'");
					while ($row = mysqli_fetch_array($sql)) {
						
						if ($coding_type == 1) {
							
							$k = iconv("ISO-8859-2", "UTF-8", substr( iconv("UTF-8", "ISO-8859-2", $row['text']), 0, round(strlen( iconv("UTF-8", "ISO-8859-2", $row['text']) )*0.75) ) );
							$k = strtolower($k);
							
							if ( array_key_exists($k, $koren_new) ) {
							
								$s = sisplet_query("DELETE FROM srv_data_vrednost$this->db_table WHERE spr_id='$spr_new' AND usr_id='$row[usr_id]'");	// ker se zafilajo z -4
								$s = sisplet_query("INSERT INTO srv_data_vrednost$this->db_table (spr_id, vre_id, usr_id, loop_id) VALUES('$spr_new', '$koren_new[$k]', '$row[usr_id]', NULL)");
								if (!$s) echo mysqli_error($GLOBALS['connect_db']);
								
								$this->mass_coding_auto_vars++;
							}
						
						} elseif ($coding_type == 2) {
							
							$s = sisplet_query("DELETE FROM srv_data_vrednost$this->db_table WHERE spr_id='$spr_new' AND usr_id='$row[usr_id]'");	// ker se zafilajo z -4
							
							foreach ($koren_new AS $k => $v) {
								if ( strpos($row['text'], $k) !== false ) {
									
									$s = sisplet_query("INSERT INTO srv_data_vrednost$this->db_table (spr_id, vre_id, usr_id, loop_id) VALUES('$spr_new', '$koren_new[$k]', '$row[usr_id]', NULL)");
									if (!$s) echo mysqli_error($GLOBALS['connect_db']);
									
									$this->mass_coding_auto_vars++;
									
								}
									
							}
							
						}
						
					}
					
					$variables = $spr_id.'_0,'.$spr_new.'_0';
					SurveyVariablesProfiles::Init($this->anketa, $global_user_id);
					SurveyVariablesProfiles::setProfileVariables(0, $variables);
					SurveyVariablesProfiles::setDefaultProfile(0);
					
					self::forceRefreshData($this->anketa);
					
					return $spr_new;
					
				}
				
			}
			
		}
		
		return 0;
		
	}
	
	function ajax_coding_filter () {
		global $global_user_id;
		global $lang;
		
		$seq = $_POST['seq'];
		$seq = explode('-', $seq);
		
		$spr_id = (int)$seq[1];
		$sequence = (int)$seq[0];
		$variables = array();
		$variables[] = $spr_id.'_0';
		
		$sql = sisplet_query("SELECT id FROM srv_spremenljivka WHERE coding = '$spr_id'");
		while ($row = mysqli_fetch_array($sql)) {
			$variables[] = $row['id'].'_0';
		}
		
		$variables = serialize($variables);
		
		SurveyVariablesProfiles::Init($this->anketa, $global_user_id);
		SurveyVariablesProfiles::setProfileVariables(-1, $variables);
		SurveyVariablesProfiles::setDefaultProfile(-1);
	}
	
	static function forceRefreshData( $anketa ) {
		
		if ( file_exists( dirname(__FILE__) . '/../surveyData/export_dashboard_'.$anketa.'.html' ) )
			unlink( dirname(__FILE__) . '/../surveyData/export_dashboard_'.$anketa.'.html' );
		
		if ( file_exists( dirname(__FILE__) . '/../surveyData/export_data_'.$anketa.'.dat' ) )
			unlink( dirname(__FILE__) . '/../surveyData/export_data_'.$anketa.'.dat' );
		
		if ( file_exists( dirname(__FILE__) . '/../surveyData/export_header_'.$anketa.'.dat' ) )
			unlink( dirname(__FILE__) . '/../surveyData/export_header_'.$anketa.'.dat' );
		
	}
	
}
 
?>