summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/surveyData/class.SurveyDataCollect.php
blob: 6014ca09e18c6831ab13212309a86e94c76b5ad1 (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
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
<?php

/**
 *
 * Created on 17.12.2019
 * 
 * @author: Peter Hrvatin
 * 
 * Class za ustvarjanje datotek s podatki
 *
*/

set_time_limit(2400); # 30 minut

global $site_path;

class SurveyDataCollect{


    // Osnovni podatki (id ankete, path...)
	private $folder = ''; 			// Folder kamor shranjujemo vse podatke
	private $sid = null; 			// id ankete
	private $db_table = '';			// ali se uporablja aktivna tabela
	private $survey  = null;		// podatki ankete

    
    // Errorji
	private $noErrors = true;		        // ali smo naleteli na napako, prekinemo izvajanje
    private $errors = array();	            // beležimo napake
    
    // Zacsno za logiranje in testiranje
    private $log = array();
    

    // Ostale nastavitve
	private $header_file_name = null;		// Ime header datoteke
	private $data_file_name = null;			// Ime data datoteke
	private $data_file_time = null;			// datum zadnjega userja v data datoteki

    private $new_line_seperator = null;
    
    private $max_anketa_time = null;		// datum zadnje spremembe v anketi
	private $max_usr_time = null;			// datum zadnje spremembe v tabelu userjev

	private $collect_all_status = 1;		// ali so zbrani vsi statusi
	private $has_test_data = false;			// ali vnosi vsebujejo testne podatke
	private $is_valid_user_limit = '';		// sql ali zbiramo vse statuse
	private $force_show_hiden_system = false;	// ali prisilimo prikaz sistemskih vprašanj (email. ime, priimek)
	private $last_update = null; 			// kdaj je bila zadnja sprememba

	private $fileStatus = FILE_STATUS_NO_DATA;	// status datoteke z podatki 1 = OK, 0 = OLD .....	


	// Pointer do headerja
	private $_HEADER = null;


    // Kesiranje respondentov
    private $_qry_users = null;			    // cache query za vprašanja
	private $_str_users = null;			    // cache string za user_id ji
    private $_cnt_all_users = 0; 		    // cache za število userjev

    // Kesiranje strani
    private $_str_groups = null;		    // cache string za strani
    private $_array_groups = null;		    // cache array za strani
    private $_cnt_groups = 0;		        // cache število za strani
    
    // Kesiranje vprasanj
    private $AllQuestionsData = null;	    // cache array z podatki vseh vprašanj
    private $AllQuestionsOrder = null;	    // cache array z vrstnim redom vseh vprašanj, upoštevajoč loope
    private $_str_questions = null; 	    // cache string za id-je vprašanj
    private $_cnt_questions = 0; 		    // cache za število vprašanj
    private $_cnt_questions_types = null;	// cache za število vprasanj po posameznih tipih (da ne klicemo funkcij po nepotrebnem)

    // Kesiranje vrednosti v vprasanjih
    private $_array_vrednosti = null;	    // cache array z vrednostmi
       
    // Kesiranje gridov
	private $_array_gridi = null;		    // cache array z gridi

	// Prikaz vrstnega reda v randomiziranih vprasanjih / blokih
    private $_array_random = null;				// cache array za random bloke in vprasanja
    private $_str_blocks = null; 	            // cache string za id-je blokov
	
	// Kesiranje loopov
	private $_array_loop_on_spr = null;		    // cache array z loopi in na katero spremenljvko se nanaša
	private $_array_loop_parent = null;		    // cache array z spremenljivkami in pripadajočimiloop idji
	private $_array_vre_on_loop = null;		    // cache array z vrednostmi v posameznem loopu
	private $_array_spr_in_loop = null;		    // cache array z id-lopi za posamezno spremenljvko
	private $_array_loop_has_spr = null;	    // cache array katere spremenljvike vsebuje posamezen loop
	private $_array_vrednosti_in_loops = null;	// cache array vseh vrednosti ki se pojavljajo v loopih
    private $_cnt_loop = 0;				        // cache za število vseh loopov
    
    // Kesiranje ostalih dodatnih nastavitev
    private $SNVariablesForSpr = null;	        // cache array spremenljivk z variablami
    private $_array_users_from_CMS = null;      // cache za e-maile uporanikov iz CMS
	private $_array_SPSS = null;	            // cache array za spss izvoz za uporabnika
    private $_array_user_grupa = null;	        // cache array casov respondentov po straneh
    private $_user_spr_answer_count = array(); 	// stejemo variable na spremenljivko da spreminjamo -3 samo za variable katerih spremenljivka nima veljavnih vrednosti
    private $sysMissingMap = array(); 	        // za osnovni mapping sistemskih missingov, -1,-2,-3,-4,-5

    // Rekodiranje
    private $_array_recode = null;			// cache array vseh rekodiranih vrednosti
	private $_array_recoded = array();		// cache array vseh že rekodiranih vrednosti


	// POINTER - Array-i do podatkov
    private $_array_data_vrednost = null;   
    private $_array_data_vrednost_cond = null;    
    private $_array_data_text = null;    
    private $_array_data_grids = null;    
    private $_array_data_check_grids = null;   
    private $_array_data_rating = null; 
    private $_array_data_text_grid = null; 
    private $_array_data_text_upload = null;  
    private $_array_data_map = null; 
    private $_array_data_heatmap = null;
	private $_array_data_heatmap_regions = null;
	private $_array_data_random = null;



    function __construct($sid = null) {
        global $site_path, $lang, $mysql_database_name;
        
        // Zacasno povecamo ram samo za veliko nijz anketo
        if($sid == '123146' && $mysql_database_name == 'www1kasi'){
            ini_set('memory_limit', '2048M');
        }

		if (is_numeric($sid) && (int)$sid > 0) {

            // Nastavimo id ankete
            $this->sid = $sid;
            
            // Nastavimo globalni folder kjer se generirajo datoteke
			$this->folder = $site_path . EXPORT_FOLDER.'/';
            
            // Nastavimo spremenljivke, ki so potrebne pri generiranju datoteke (cas zadnjega vnosa, status datoteke...)
            $this->prepareVariables();

			// Za vsak slucaj resetiramo vse pointerje
			$this->cleanup();


            // Inicializiramo razrede za dodatne nastavitve
			SurveyStatusProfiles::Init($this->sid);	
			SurveySetting::getInstance()->Init($this->sid);
		} 
		else {
            
            $SL = new SurveyLog();
            $SL->addMessage(SurveyLog::DEBUG, 'SurveyDataCollect constructor - survey id == 0!');
            $SL->write();	
            
            echo 'Napaka! Manjka ID ankete!';

            /*return false;
            exit();*/

            die();
		}
	}


    // Nastavimo spremenljivke potrebne za generiranje datoteke
    private function prepareVariables(){

        // Dobimo instanco razreda SurveyDataFile
        $sdf = SurveyDataFile::get_instance();
   
        // Nastavimo spremenljivke, ki so ze nastavljene v razredu SurveyDataFile
		$this->header_file_name = $sdf->getHeaderFileName();
		$this->data_file_name = $sdf->getDataFileName();
        $this->data_file_time = $sdf->getDataFileTime();

        $this->last_update = $sdf->getFileUpdated();
        $this->fileStatus = $sdf->getStatus();
        $this->has_test_data = $sdf->getHasTestData();
        $this->max_usr_time = $sdf->getLastResponseTime();

        $this->collect_all_status = $sdf->getCollectAllStatus();

        if ((int)$this->collect_all_status == 0)
            $this->is_valid_user_limit = " AND u.last_status IN (5,6) ";
        else
            $this->is_valid_user_limit = '';


        // Dobimo podatke o anketi
        $qry_survey = sisplet_query("SELECT *, UNIX_TIMESTAMP(edit_time) AS srv_edit_time  FROM srv_anketa WHERE id='".$this->sid."'");
        $this->survey = mysqli_fetch_assoc($qry_survey);
        
        SurveyInfo::getInstance()->SurveyInit($this->sid);

        // Aktivne tabele za podatke v bazi
        $this->db_table = SurveyInfo::getInstance()->getSurveyArchiveDBString();
        
        // Zadnji cas editiranja ankete
        $this->max_anketa_time = (int)$this->survey['srv_edit_time'];

		// Ali prisilimo prikaz sistemskih spremenljivk
		if ($this->survey['show_email'] == 1 || SurveyInfo::getInstance()->checkSurveyModule('360_stopinj'))
			$this->force_show_hiden_system = true;
        else
			$this->force_show_hiden_system = false;
    }
   

    // Ustvarimo vse nove datoteke s podatki
    public function createFiles() {
		global $site_url, $lang, $site_path;

        // Najprej zakesiramo vse podatke
        $this->cache_data();

        // Zgeneriramo datoteko z naslovno vrstico
        $CH = (int)$this->createHeadFile();

        // Zgeneriramo datoteko s podatki
        $CD = (int)$this->createDataFile();
    }
    
    // Posodobimo datoteke ker so zastarele
    public function updateFilesIncremental() {
		global $site_url, $lang, $site_path;
        
		// Pobrišemo odvecne response
        $this->deleteUsers();
        
        // Zakesiramo vse podatke
        $this->cache_data();
        
        // Zgeneriramo datoteko z naslovno vrstico
        //$CH = (int)$this->createHeadFile();

        // Zgeneriramo datoteko s podatki
        $CD = (int)$this->createDataFile();
    }
    

    // Kreiramo datoteko z naslovno vrstico
    private function createHeadFile() {

        $result = false;

        // za vsak slučaj
        $this->_HEADER = null;     

        // Ce imamo query za vprasanja
        if ($this->_str_questions !== '') {

            // Izvedemo zbiranje podatkov v $this->_HEADER array
            $this->CollectHeaders();
                
            // Zapišemo header datoteko
            file_put_contents($this->header_file_name, serialize($this->_HEADER));
            unset($this->_HEADER);
            
            // Ce ni napak updejtamo zapis v bazi
            if ($this->noErrors) {
                
                // data time damo na 0 ker moramo datoteko s podatki zgenerirati čisto na novo.
                $updated = sisplet_query("INSERT INTO srv_data_files 
                                            (sid, head_file_time, data_file_time) VALUES ('".$this->sid."', FROM_UNIXTIME('".$this->max_anketa_time."'), '0000-00-00') 
                                            ON DUPLICATE KEY UPDATE head_file_time = FROM_UNIXTIME('".$this->max_anketa_time."'), data_file_time = '0000-00-00'");
                sisplet_query("COMMIT");
                
                if ($updated) {
                    $result = true;
                } 
                else {
                    $this->trigerError('updateHeader',  mysqli_error($GLOBALS['connect_db']));
                }
            }
        } 
        else {
            $this->trigerError('createHeaderFile',  'Not set this->survey[edit_header_time] ('.$this->sid.')');
        }	
        
		return $result;
	}

    // Kreiramo datoteko z responsi
	private function createDataFile() {
            
		$result = false;
						
        // Pobrišemo še morebitne prazne vrstice
        if (IS_WINDOWS) {
            $cmd1 = 'sed "/^$/d" '.$this->data_file_name.' > '.$this->data_file_name.'.empt && mv '.$this->data_file_name.'.empt '.$this->data_file_name;
        } 
        else {
            $cmd1 = 'sed \'/^$/d\' '.$this->data_file_name.' > '.$this->data_file_name.'.empt && mv '.$this->data_file_name.'.empt '.$this->data_file_name;
        }
        $out_command1 = shell_exec($cmd1);

        if (file_exists($this->data_file_name.'.empt')) {
            unlink($this->data_file_name.'.empt');
        }

        // preverimo ali datoteka obstaja in ali že imamo zapise da prilagodimo line seperator
        $this->new_line_seperator = null;
        
        $lines = $this->getLinesCnt();
        
        if ((int)$lines > 0) {
            $this->new_line_seperator = NEW_LINE;
        }

        // Datoteko pripravimo za dodajanje
        $file_handler = fopen($this->data_file_name, "a");

        if ($this->noErrors) {

            $_tmpCnt = 0;
        
            // v loopu dodamo podatke v data file
            if ($this->noErrors && is_countable($this->_str_users) && count($this->_str_users) > 0) {

                foreach ($this->_str_users AS $c => $string_user) {

                    // Zakesiramo podatke za vse odgovore respondenta
                    $this->cache_data_respondent($string_user);

                    // Ce ni errorjev izvedemo zbiranje podatkov 
                    if ($this->noErrors) {
                        $this->CollectData($c, $file_handler);
                    }

                    $_tmpCnt++;
                }
            }
            
            // Peverimo da res ni prišlo do napak
            if ($this->noErrors) {

                // Nastavimo cas zadnjega respondenta
                $sdf = SurveyDataFile::get_instance();
                $this->max_usr_time = $sdf->setLastResponseTime();

                $updated = sisplet_query("INSERT INTO srv_data_files 
                                            (sid, data_file_time, last_update ) VALUES ('".$this->sid."', FROM_UNIXTIME('".$this->max_usr_time."'), NOW())
                                            ON DUPLICATE KEY UPDATE data_file_time = FROM_UNIXTIME('".$this->max_usr_time."'), last_update = NOW()");
                                
                if ($updated) {
                    $result = true;
                } 
                else {
                    $this->trigerError('updateDataSql', mysqli_error($GLOBALS['connect_db']));
                }
            }
        }

        // Zapremo datoteko
        if ($file_handler !== null) {
            fclose($file_handler);

            // Se enkrat izvedemo generiranje ce imamo filter za uporabnost (ker ga racunamo na podlagi ze obstojece datoteke)
            if (SurveyStatusProfiles::usabilitySettings()) {
                $SUR = new SurveyUsableResp($this->sid, $generateDataFile=true);
                
                // Resetiramo status filter (da izracunamo za vse enote)
                $SUR->setStatusFilter($status_filter='');
                
                // Izracunamo uporabnost za vsako enoto
                $usability = $SUR->calculateData();
                
                $file_handler_old = fopen($this->data_file_name,"r");
                
                $data_file_name_new = $this->folder . 'export_data_'.$this->sid.'_2.dat';
                $file_handler_new = fopen($data_file_name_new,"a");
                
                // Beremo datoteko vrstico po vrstico
                $cnt = 1;
                while (($line = fgets($file_handler_old)) !== false) {
                    
                    // dodamo vrstici na koncu uporabnost
                    $usability_status = (isset($usability['data'][$cnt]['status'])) ? $usability['data'][$cnt]['status'] : 0;
                    $line = str_replace($this->new_line_seperator, '', $line);
                    $line .= STR_DLMT . $usability_status;
                    
                    fwrite($file_handler_new,  $line . $this->new_line_seperator);
                    
                    $cnt++;
                }			
                
                fclose($file_handler_old);
                fclose($file_handler_new);
                
                // Pobrisemo staro datoteko
                unlink($this->data_file_name);
                
                // Preimenujemo novo datoteko
                rename($data_file_name_new, $this->data_file_name);
            }
        }
		
		// Naredimo še cleanup pointerjev
        $this->cleanup();
        		
		return $result;
    }



    /***** ZBIRANJE PODATKOV ZA DATOTEKO *****/

    /**
	 * Vsako spremenljivko shranimo pod svojim ID-jem
	 * spremenljivka ima enega ali več gridov ( odviso od dimenzi: eno=>(radio, check, vsota...) multi=>(multigrid, multicheck, multibox)
	 * vsak grid pa lahko ima eno ali več variabel (odgovor, tekstovni odgovor... )
	 *
	 * vse skupaj shranjujemo v urejenem arrayu
	 * 	HEADERS[SPR_ID] => array( tip		=> tip variable
	 *                                variable	=> ime Variable
	 * 				  naslov	=> Naslov variable
	 *                                cnt_grids	=> koliko je gridov
	 * 				  grids[ID]	=> array(
	 * 					variables[ID] => array(variable	=> ime variable
	 * 						naslov		=> naslov variable
	 * 						other		=> ali je polje drugo
	 * 						text		=> ali je tekstovni odgovor
	 * 						spss		=> polje za spss
	 * 					)
	 * 				)
	 *			)
	 */
	public function CollectHeaders() {
		global $lang;
		global $admin_type;
        
		// v header dodomo userid
		$sequence = 1;	# vodimo zaporedno števiko polja v bazi
		$_data_sequence = 1;	# vodimo zaporedno števiko polja v bazi kjer se začnejo "resni" podatki


        // DODATNE NASTAVITVE 
        // Vsi status
		if ((int)$this->collect_all_status > 0) {
			$_HEADER['_settings']['collectAllStatus'] = '1';
        } 
        // Samo ustrezni statusi 6 in 5
        else {
			$_HEADER['_settings']['collectAllStatus'] = '0';
		}

        // Obstajajo testni podatki
		if ($this->has_test_data == true) {
			$_HEADER['_settings']['hasTestData'] = '1';
        } 
        else {
			$_HEADER['_settings']['hasTestData'] = '0';
        }
        
        // Force-amo zbiranje skritih sistemskih vprasanj
		if ($this->force_show_hiden_system == true) {
			$_HEADER['_settings']['force_show_hiden_system'] = '1';
        } 
        else {
			$_HEADER['_settings']['force_show_hiden_system'] = '0';
        }
        

		// Preštejemo normalne variable, ki niso sistemske kot je email, ime.... in se v podatkih prikazujejo normalno
        $_HEADER['_settings']['count_normal_data_variables'] = 0;
        
		// Preštejemo sistemske kot je email, ime.... in se v podatkih ne smejo prikazovat
		$_HEADER['_settings']['count_system_data_variables'] = 0;


		// user ID
		$_HEADER['uid']= array ('tip'=>'m', 'variable'=>'uid', 'naslov' =>'User ID',
								 'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'uid', 'naslov'=>'User ID','spss'=>'F11.0','sequence'=>$sequence))
		,'naslov'=>'uid','cnt_vars' => 1)));
		$_HEADER['uid']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;

        // status uporabnika
		$_HEADER['status']= array ('tip'=>'m', 'variable'=>'status', 'naslov' =>$lang['srv_data_status'],
        'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'status', 'naslov'=>$lang['srv_data_status'],'spss'=>'F3.0','sequence'=>$sequence))
                ,'naslov'=>$lang['srv_data_status'],'cnt_vars' => 1)));
                        $_HEADER['status']['sequences'] = $sequence;
        $sequence++;
        $_data_sequence++;

        // EMAIL VABILO - invitation
		$_HEADER['invitation']= array ('tip'=>'m', 'variable'=>'invitation', 'naslov' =>$lang['srv_data_invitation'],
                'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'invitation', 'naslov'=>$lang['srv_data_invitation'],'spss'=>'F3.0','sequence'=>$sequence))
                        ,'naslov'=>$lang['srv_data_invitation'],'cnt_vars' => 1)));
        $_HEADER['invitation']['sequences'] = $sequence;
        $sequence++;
        $_data_sequence++;
		
		// ustreznost  uporabnika
		$_HEADER['relevance']= array ('tip'=>'m', 'variable'=>'relevance', 'naslov' =>$lang['srv_data_relevance'],
				'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'relevance', 'naslov'=>$lang['srv_data_relevance'],'spss'=>'F3.0','sequence'=>$sequence))
						,'naslov'=>$lang['srv_data_relevance'],'cnt_vars' => 1)));
		$_HEADER['relevance']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;

		// ali je uporabnik lurker
		$_HEADER['lurker']= array ('tip'=>'m', 'variable'=>'lurker', 'naslov' =>$lang['srv_data_lurker'],
								 	'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'lurker', 'naslov'=>$lang['srv_data_lurker'],'spss'=>'F3.0','sequence'=>$sequence))
		,'naslov'=>$lang['srv_data_lurker'],'cnt_vars' => 1)));
		$_HEADER['lurker']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;

		// Cas vnosa
		$_HEADER['unx_ins_date']= array ('tip'=>'m', 'variable'=>'unx_ins_date', 'naslov' =>'unx_ins_date',
								 	'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'unx_ins_date', 'naslov'=>'unx_ins_date','spss'=>'F20.0','sequence'=>$sequence))
		,'naslov'=>'unx_ins_date','cnt_vars' => 1)));
		$_HEADER['unx_ins_date']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;

        // Recnum
		$_HEADER['recnum'] = array('tip'=>'m', 'variable'=>'recnum', 'naslov' =>$lang['srv_recnum'],
				'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'recnum','naslov'=>$lang['srv_recnum'],'spss'=>'F5.0','sortType'=>'number','sequence'=>$sequence)),
						'naslov'=>'recnum','cnt_vars' => 1)));
		$_HEADER['recnum']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;
        
		// Dodamo geslo - code
		if ($this->force_show_hiden_system == true) {
			$_HEADER['code'] = array('tip'=>'m', 'variable'=>'code', 'naslov' =>$lang['password'],
					'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'code','naslov'=>$lang['password'],'spss'=>'A6','sequence'=>$sequence)),
							'naslov'=>$lang['password'],'cnt_vars' => 1)));
			$_HEADER['code']['sequences'] = $sequence;
			$sequence++;
			$_data_sequence++;
		}

		// Ce vsebuje testne podatke dodamo tudi to polje
		if ($this->has_test_data) {
			$_HEADER['testdata']= array ('tip'=>'m', 'variable'=>'testdata', 'naslov' =>$lang['srv_data_test'],
								 'grids' => array(0 => Array('variables'=>array(0 => Array ('variable'=>'testdata', 'naslov'=>$lang['srv_data_test'],'spss'=>'F3.0','sequence'=>$sequence))
			,'naslov'=>$lang['srv_data_test'],'cnt_vars' => 1)));
			$_HEADER['testdata']['sequences'] = $sequence;
			$sequence++;
			$_data_sequence++;
		}

		// date insert (Meta)
		$_HEADER['itime']= array ('tip'=>'m', 'variable'=>'itime', 'naslov' =>$lang['srv_data_date'],
								 'grids' => array(0 => 
								 		Array('variables'=>array(0 => 
								 				Array ('variable'=>'itime', 'naslov'=>$lang['srv_data_date'],'spss'=>'DATETIMEw','sortType'=>'date','sequence'=>$sequence))
								 			,'naslov'=>$lang['srv_data_date'],'cnt_vars' => 1)));
		$_HEADER['itime']['sequences'] = $sequence;
		$sequence++;
		$_data_sequence++;
		
		
		
		// s katero sekvenco se začnejo podatki
		$_HEADER['_settings']['dataSequence'] = $_data_sequence;

		// naredimo pointerje na podatke če še ne obstajajo
        $_array_vrednosti = $this->get_vrednosti();     // za vrednosti ankete
		$_array_gridi = $this->get_gridi();			    // za vrednosti ankete

		
		// Stejemo loope za numeric vprasanje
		$num_loop_cnt = array();
		
		// dodamo vprašanja ki so v loopu
		if ($this->noErrors && count($this->AllQuestionsOrder) > 0) {
            
            foreach ($this->AllQuestionsOrder AS $_vprasanje_array) {
                
                // Ce vrpasanje ni nastavljeno preskocimo
                if(!isset($this->AllQuestionsData[$_vprasanje_array['id']]))
                    continue;

				# dodelimo vrednosti loopa
				$rowVprasanje = $this->AllQuestionsData[$_vprasanje_array['id']];
				
                // nastavimo vrstni red ce loopamo po numericu
                if(!isset($num_loop_cnt[$_vprasanje_array['id']]))
				    $num_loop_cnt[$_vprasanje_array['id']] = 1;
                else
                    $num_loop_cnt[$_vprasanje_array['id']]++;

				# spremenljivki dodamo loop_id da je konsistentno z podatki
				$rowVprasanje['spr_id'] = $rowVprasanje['spr_id'].'_'.$_vprasanje_array['loop_id'];
                $loop_vre = isset($this->_array_loop_on_spr[$rowVprasanje['if_id']]) ? $this->_array_loop_on_spr[$rowVprasanje['if_id']] : null;
				$_vrednosti = $this->get_vrednosti($loop_vre);
				$_loop_vrednost = isset($this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']]) ? $this->_array_loop_on_spr[$rowVprasanje['if_id']] : null;

				$spr_id = $rowVprasanje['spr_id'];
                $vrednostLoopSufix = '';

				# popravimo ime variable če smo v loopu
				if(isset($this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']])) {
					
					// Posebej obravnavamo loop po numeric vprasanju - samo nastavimo suffix _1, _2...
					if($this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']] == 'num_loop'){
						$rowVprasanje['variable'] = strip_tags($rowVprasanje['variable'])."_".$num_loop_cnt[$_vprasanje_array['id']];
					}
					else{
						# id spremenljivke po kateri loopamo
                        $_loop_on_spr_id = isset($this->_array_loop_on_spr[$rowVprasanje['if_id']]) ? $this->_array_loop_on_spr[$rowVprasanje['if_id']] : null;
                        
						#variabla spremenljivke po kateri loopamo
                        $_loop_on_spr_variable = $this->AllQuestionsData[$_loop_on_spr_id]['variable'];

                        if(isset($_vrednosti[$_loop_vrednost])){

                            # id variable po kateri loopamo (v okviru $_loop_on_spr_id)
                            $_loop_on_variable_id = $_vrednosti[$_loop_vrednost];

                            # naslov variable po keteri loopamo (v okviru $_loop_on_spr_id)
                            $_vrednost_naslov = $_array_vrednosti[$_loop_on_spr_id][$_loop_on_variable_id['id']]['naslov'];
                            $rowVprasanje['variable'] = strip_tags($rowVprasanje['variable'])."_".$_vrednosti[$_loop_vrednost]['variable'];
                            $vrednostLoopSufix = "_".$_vrednosti[$_loop_vrednost]['variable'];
                            
                            if ($_vrednost_naslov != '' && $_loop_on_spr_variable != '' && $rowVprasanje['naslov'] != '') {
                                # zamenjamo ime spremenljivke med #q1# (#q1# z naslovom trenutne variable)
                                $rowVprasanje['naslov'] = str_replace("#$_loop_on_spr_variable#", "$_vrednost_naslov", $rowVprasanje['naslov']);
                            }	
                        }
                        
						
					}
				}

				# pomožne variable
				$tip = 	$rowVprasanje['tip'];
				$_tmp_spr_id = explode('_',$rowVprasanje['spr_id']);
				$spr_id = $_tmp_spr_id[0];
				$spr_data_id = $rowVprasanje['spr_id'];

                // Pri kalkulaciji izpisemo labelo v imenu, ce jo imamo
                $spr_naslov = ($tip == '22' && $rowVprasanje['label'] != '') ? strip_tags($rowVprasanje['label']) : strip_tags($rowVprasanje['naslov']);

                $spr_naslov_graf = strip_tags($rowVprasanje['naslov_graf']);
				$spr_edit_graf = $rowVprasanje['edit_graf'];
				$spr_wide_graf = $rowVprasanje['wide_graf'];
				$spr_antonucci = $rowVprasanje['antonucci'];
				$spr_variable = strip_tags($rowVprasanje['variable']);
				$spr_size = $rowVprasanje['size'];
				$spr_cela = $rowVprasanje['cela'];
				$grid_subtitle1 = $rowVprasanje['grid_subtitle1'];
				$grid_subtitle2 = $rowVprasanje['grid_subtitle2'];
				$spr_decimalna = $rowVprasanje['decimalna'];
				$spr_skala = $rowVprasanje['skala'];
				$spr_sistem = $rowVprasanje['sistem'];
				$spr_upload = $rowVprasanje['upload'];
				$spr_signature = $rowVprasanje['signature'];
				$spr_random = $rowVprasanje['random'];

				#!!! po novem tako ali tako število znakov za spss lovimo iz datoteke s podatki
				 
				# TODO $spss_lngth mora biti enak za vse in sicer je enak največjemu možnemu številu znakov
				# TODO zato je potrebno zdužit vse  ($spss_lngth, $spss_lngth2, $spss_lngth3)
				$spss_lngth = isset($this->_array_SPSS[$spr_id]['text']) ? $this->_array_SPSS[$spr_id]['text'] : '';
				$spss_lngth2 = isset($this->_array_SPSS[$spr_id]['text2']) ? $this->_array_SPSS[$spr_id]['text2'] : '';
				$spss_lngth3 = isset($this->_array_SPSS[$spr_id]['vrednost']) && $this->_array_SPSS[$spr_id]['vrednost'] != '' ? $this->_array_SPSS[$spr_id]['vrednost'] : 0;

				$_HEADER[$spr_data_id] = array('spr_id'=>$spr_id, 'tip'=>$tip, 'variable'=>$spr_variable, 'naslov'=>$spr_naslov, 'sistem'=>$spr_sistem, 'skala'=>$spr_skala, 'naslov_graf'=>$spr_naslov_graf, 'edit_graf'=>$spr_edit_graf, 'wide_graf'=>$spr_wide_graf);
				# kontrola sistemskih skritih spremenljivk
				# kadar ne vsilimo prikaza sistemskih spremenljivk (lahko omogoči le admin)
				if ($this->force_show_hiden_system == false 
					&& $spr_sistem == '1' 
					&& in_array($spr_variable,unserialize (SYSTEM_VARIABLES)) # unserialize (SYSTEM_VARIABLES) -> definition.php = array('email','telefon','ime','priimek','naziv','drugo')
					# pri formi ali glasovanjzu prikazujemo tudi sistemske
					&& ( $this->survey['survey_type'] != 1 && $this->survey['survey_type'] != 0)) {

						$_HEADER[$spr_data_id]['hide_system'] = '1';

						# povečamo števec sistemskih variabel
						$_HEADER['_settings']['count_system_data_variables'] ++;
				} else {
					# povečamo števec normalnih
					$_HEADER['_settings']['count_normal_data_variables'] ++;
				}
				
				# če je sistemska variabla jo označimo
				if (in_array($spr_variable,unserialize (SYSTEM_VARIABLES))) {
					$_HEADER[$spr_data_id]['is_system'] = '1';
				}
				
				if ( $spr_upload == '1' || $spr_upload == '2') {
					$_HEADER[$spr_data_id]['upload'] = '1';
				}
				
				if ( $spr_signature == '1' ) {
					$_HEADER[$spr_data_id]['signature'] = '1';
				}

				if (isset($rowVprasanje['enota']) && $rowVprasanje['enota'] > 0) {
					$_HEADER[$spr_data_id]['enota'] = $rowVprasanje['enota'];
				}

				# dodoamo loop parent
				if (isset($rowVprasanje['loop_parent']) && $rowVprasanje['loop_parent'] > 0) {
					$_HEADER[$spr_data_id]['loop_parent'] = $rowVprasanje['loop_parent'];
					$_HEADER[$spr_data_id]['antonucci'] = $rowVprasanje['antonucci'];
				}

				# dodoamo parent loop id
				if (isset($_vprasanje_array['parent_loop_id']) && $_vprasanje_array['parent_loop_id'] > 0) {
					$_HEADER[$spr_data_id]['parent_loop_id'] = $_vprasanje_array['parent_loop_id'];
				}
				# Dodamo še loop id
				$_HEADER[$spr_data_id]['loop_id'] = $_vprasanje_array['loop_id'];

				$_GRIDS = array();
				$_seq_prefix = '';
				$_sequences = '';
                                
				// v odvisnosti od tipa vprašanja pohandlamo podatke
				switch ( $tip ) {

					# RADIO
					case 1:
					# DROPDOWN
					case 3:
						$cnt=0;
						# dodamo header variable
						$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$spr_naslov, 'variable'=>$spr_variable, 'other'=>false,'text'=>false,'spss'=>'F'.$spss_lngth3.'.0','sequence'=>$sequence);
						$_sequences .= $_seq_prefix.$sequence;
						$_seq_prefix = '_';
						$sequence++;
						$cnt++;
						$arrayVrednost = isset($_array_vrednosti[$spr_id]) ? $_array_vrednosti[$spr_id] : null;

						if (is_countable($arrayVrednost) && count($arrayVrednost)>0) {
							foreach ($arrayVrednost as $kid=> $vrednost) {
								# dodamo še eno polje za tekstovne odgovore drugo
								if ($vrednost['other'] == 1) {
									$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$vrednost['naslov'],
											'variable'=>$spr_variable.'_'.$vrednost['vrstni_red'].STR_OTHER_TEXT,  'other'=>true,'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
									$_sequences .= $_seq_prefix.$sequence;
									$_seq_prefix = '_';
									$sequence++;
									$cnt++;

                                    if(isset($_GRIDS[0]['cnt_other']))
									    $_GRIDS[0]['cnt_other'] += 1;
                                    else
									    $_GRIDS[0]['cnt_other'] = 1;
								} // end if
								#dodamo opcije (za spss)
								if ($vrednost['other'] == 0 || $vrednost['other'] == 1) {
									$_HEADER[$spr_data_id]['options'][$vrednost['variable']] = ($vrednost['naslov'] != null) ? $vrednost['naslov'] : $vrednost['variable'];
									$_HEADER[$spr_data_id]['options_graf'][$vrednost['variable']] = ($vrednost['naslov_graf'] != null) ? $vrednost['naslov_graf'] : $_HEADER[$spr_data_id]['options'][$vrednost['variable']];
								}
                                //dodamo missing values
                                else if($vrednost['naslov'] != null){
                                    $_HEADER[$spr_data_id]['missing_values'][$vrednost['other']] = $vrednost['naslov'];
                                }
							} // end foreach

						} // end if
						
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_HEADER[$spr_data_id]['cnt_all'] = $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
						
						if (isset($rowVprasanje['inline_edit'])) $_HEADER[$spr_data_id]['inline_edit'] = $rowVprasanje['inline_edit'];
					break;

					
					# CHECKBOX
					case 2:
						$arrayVrednost = $_array_vrednosti[$spr_id];
						$cnt=0;
						if (isset($arrayVrednost) && count($arrayVrednost)>0) {
							foreach ($arrayVrednost as $vrednost) {

								# dodamo header variable samo za ne -missing variable
								if ($vrednost['other'] == 0 || $vrednost['other'] == 1) {
									$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'], 'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$vrednostLoopSufix, 'other'=>false,'text'=>false,'spss'=>'F'.$spss_lngth3.'.0','sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
									$_sequences .= $_seq_prefix.$sequence;
									$_seq_prefix = '_';
									$sequence++;
									$cnt++;
									#dodamo še header za polja drugo
									if ($vrednost['other'] == 1) {
										// dodamo v array 'variables'
										$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'], 'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$vrednostLoopSufix.STR_OTHER_TEXT, 'other'=>true,'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
										$_sequences .= $_seq_prefix.$sequence;
										$_seq_prefix = '_';
										$sequence++;
										$cnt++;

                                        if(isset($_GRIDS[0]['cnt_other']))
                                            $_GRIDS[0]['cnt_other'] += 1;
                                        else
                                            $_GRIDS[0]['cnt_other'] = 1;
									} // end if
								}
                                //dodamo missing values
                                else if($vrednost['naslov'] != null){
                                    $_HEADER[$spr_data_id]['missing_values'][$vrednost['other']] = $vrednost['naslov'];
                                }
							} // end foreach
						} // end if
						
						#dodamo opcije (za spss)
						$_HEADER[$spr_data_id]['options']['0'] = '0';
						$_HEADER[$spr_data_id]['options']['1'] = '1';

						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_HEADER[$spr_data_id]['cnt_all'] = $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
						
					# TEXT
					case 4:
						$cnt=0;
						# dodamo header variable
						$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$spr_naslov, 'variable'=>$spr_variable, 'other'=>false, 'text'=>true, 'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
						$_sequences .= $_seq_prefix.$sequence;
						$_seq_prefix = '_';
						$sequence++;
						$cnt++;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_HEADER[$spr_data_id]['cnt_all'] = $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
					
					# MULTIRADIO
					case 6:				
						# ali je dvojni grid
						$double = (int)($rowVprasanje['enota'] == 3);
					
						$double_data = array();
						# Pri multigridu je logika obratna. variable predstavljajo podvprašanja, srv_grid pa odgovore
						$cntGrid = 0;
						$arrayVrednost = $_array_vrednosti[$spr_id];

						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {

							# če imamo dvojni grid gremo 2_skozi
							for ($i=1; $i<=$double+1;$i++) {
								// for $double
								$double_data[$i]['subtitle'] = $rowVprasanje['grid_subtitle'.$i];
								if ($double == 1) {
									$var_appendix = '_'.$i;
								} else {
									$var_appendix = '';
								}
								$part = $i;
								foreach ($arrayVrednost as $kid=> $vrednost) {
									
									// Za kombinirane tabele popravimo ime variable
									if($rowVprasanje['gru_id'] == -2){
										$arr = explode("_", $spr_variable, 2);
										$sprVar = $arr[0];
										$vrednost['variable'] = $sprVar.'_'.$vrednost['variable'];
									}
									
									# dodamo header samo za nemissing variable
									if ($vrednost['other'] == 0 || $vrednost['other'] == 1) {
                                        $cnt = 0;
										$_GRIDS[$cntGrid]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],
																	'naslov'=>$vrednost['naslov'],
																	'naslov2'=>$vrednost['naslov2'],
																	'variable'=>$vrednost['variable'].$var_appendix.$vrednostLoopSufix, 
																	'other'=>false,
																	'text'=>false,
																	'spss'=>'F'.$spss_lngth3.'.0','sequence'=>$sequence,
																	'naslov_graf'=>$vrednost['naslov_graf']);
										$_sequences .= $_seq_prefix.$sequence;
										$_seq_prefix = '_';
										$sequence++;
										$cnt++;
										if ($vrednost['other'] == 1) {
											$_GRIDS[$cntGrid]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$var_appendix.$vrednostLoopSufix.STR_OTHER_TEXT,  'other'=>true,'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
											$_sequences .= $_seq_prefix.$sequence;
											$_seq_prefix = '_';
											$sequence++;
											$cnt++;
											$_GRIDS[$cntGrid]['cnt_other'] = (isset($_GRIDS[$cntGrid]['cnt_other']) ? $_GRIDS[$cntGrid]['cnt_other'] += 1 : 1);
										} // end if
										$_GRIDS[$cntGrid]['cnt_vars'] = $cnt;
										$_GRIDS[$cntGrid]['naslov'] = $vrednost['variable'].$var_appendix.$vrednostLoopSufix;
										$_GRIDS[$cntGrid]['part'] = $i;
                                        if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
										$cntGrid++;
									}
								} // end foreach
			
							} // end for $double
						} // end if
						
						#dodamo opcije (za spss)
						$arrayGrids = $_array_gridi[$spr_id];
						if (is_countable($arrayGrids) && count($arrayGrids) > 0) {
							foreach ($arrayGrids AS $kid => $grid) {
								if ($grid['other'] == 0 || $grid['other'] == 1) {
									$_HEADER[$spr_data_id]['options'][$grid['variable']] = ($grid['naslov'] != null) ? $grid['naslov'] : $grid['variable'];
									// Opcije za dodaten naslov grida pri grafu
									$_HEADER[$spr_data_id]['options_graf'][$grid['variable']] = ($grid['naslov_graf'] != null) ? $grid['naslov_graf'] : $_HEADER[$spr_data_id]['options'][$grid['variable']];
								}
                                //dodamo missing values
                                else if($grid['naslov'] != null){
                                    $_HEADER[$spr_data_id]['missing_values'][$grid['other']] = $grid['naslov'];
                                }
							}
						}
						
						$_HEADER[$spr_data_id]['cnt_grids'] = $cntGrid;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
						
						if ($double == 1) {
							$_HEADER[$spr_data_id]['double'] = $double_data;
						}
					break;
					
						
					# NUMBER
					case 7:

						# pri number lahko imamo dve variabli grida, če je size 2
						$cnt_v = 0;
						$arrayVrednost = $_array_vrednosti[$spr_id];
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							$cnt=0;
							foreach ($arrayVrednost as $kid=> $vrednost) {
								if ($vrednost['other'] == 0) {
                                    
                                    if ($spr_size > 1) {
										# če imamo več variabel
										$_variable = $vrednost['variable'].$vrednostLoopSufix;
                                    } 
                                    else { #imamo samo eno variablo
										$_variable = $spr_variable.$vrednostLoopSufix;
                                    }
                                    
                                    // Ce nimamo enote je naslov enak naslovu vprasanja
                                    if(!isset($rowVprasanje['enota']) || $rowVprasanje['enota'] == 0)
                                        $vrednost['naslov'] = $spr_naslov;

									$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'], 'naslov'=>$vrednost['naslov'], 'variable'=>$_variable, 'other'=>false, 'text'=>false,'spss'=>'F'.($spr_cela+$spr_decimalna).($spr_decimalna > 0 ? ('.'.$spr_decimalna) : '.0'),'sortType'=>'number','sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
									$_sequences .= $_seq_prefix.$sequence;
									$_seq_prefix = '_';
									$sequence++;
									$cnt_v++;
									$cnt++;
								} // end if

							} // end foreach
							$_GRIDS[0]['cnt_vars'] = $cnt_v;
							$_GRIDS[0]['naslov'] = $spr_variable;

						} // end if
						$_GRIDS[0]['enota'] = $spr_variable;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
                        if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt_v;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt_v;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
						
					# DATUM
					case 8:
						$cnt=0;
		
						# dodamo header variable
						$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$spr_naslov, 'variable'=>$spr_variable, 'other'=>false, 'text'=>true,'spss'=>'A10','sortType'=>'date','sequence'=>$sequence);
						
						$_sequences .= $_seq_prefix.$sequence;
						$_seq_prefix = '_';
						$sequence++;
						$cnt++;
						
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
						
					# MULTICHECKBOX
					case 16:
					# MULTITEXT
					case 19:
					# MULTINUMBER
					case 20:
						// srv_variable predstavljajo podvprašanja, srv_grid pa možne pododkovore
						$_spss = ($tip == 16)
							? 'F'.$spss_lngth3.'.0'
							: (($tip == 19)
								? 'A'.$spss_lngth2
								: 'F'.($spr_cela+$spr_decimalna).($spr_decimalna > 0 ? ('.'.$spr_decimalna) : '.0'));
								
						$row = Cache::srv_spremenljivka($spr_id);
						$newParams = new enkaParameters($row['params']);
						$is_datum = $newParams->get('multigrid-datum');
						#'sortType'=>'date'
						$grid_id=0;
						
						$arrayGrids = $_array_gridi[$spr_id];
						$arrayVrednost = $_array_vrednosti[$spr_id];
                        
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							foreach ($arrayVrednost as $kid=> $vrednost) {
							
								// Za kombinirane tabele popravimo ime variable
								if($rowVprasanje['gru_id'] == -2){
									$arr = explode("_", $spr_variable, 2);
									$sprVar = $arr[0];
									$vrednost['variable'] = $sprVar.'_'.$vrednost['variable'];
								}
							
								$cnt=0;
								if (count($arrayGrids) > 0) {
									foreach ($arrayGrids AS $kid => $grid) {
										if ($grid['other'] == 0 || $grid['other'] == 1) {
											$_GRIDS[$grid_id]['variables'][$cnt] = array('vr_id'=>$vrednost['id'], 'gr_id'=>$grid['id'], 'naslov'=>$grid['naslov'].($grid['part'] == '2' ? '_'.$grid['part'] : ''), 'variable'=>$vrednost['variable'].'_'.$grid['variable'].($grid['part'] == '2' ? '_'.$grid['part'] : '').$vrednostLoopSufix, 'other'=>false, 'text'=>false,'spss'=>$_spss,'sequence'=>$sequence,'naslov_graf'=>$grid['naslov_graf']);

                                            if((int)$is_datum === 1) {
												$_GRIDS[$grid_id]['variables'][$cnt]['sortType'] ='date';
											}
											$_sequences .= $_seq_prefix.$sequence;
											$_seq_prefix = '_';
											$sequence++;
											$cnt++;
										}
                                        //dodamo missing values
                                        else if($grid['naslov'] != null){
                                            $_HEADER[$spr_data_id]['missing_values'][$grid['other']] = $grid['naslov'];
                                        }
									} // end foreach
									if ($vrednost['other'] == 1) {
										$_GRIDS[$grid_id]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$vrednostLoopSufix.STR_OTHER_TEXT,  'other'=>true,'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
										$_sequences .= $_seq_prefix.$sequence;
										$_seq_prefix = '_';
										$sequence++;
										$cnt++;
										$_GRIDS[$grid_id]['cnt_other'] += 1;
									} // end if

								} // end if
								
								$_GRIDS[$grid_id]['cnt_vars'] = $cnt;
								$_GRIDS[$grid_id]['naslov'] = $vrednost['naslov'];
								$_GRIDS[$grid_id]['variable'] = $vrednost['variable'];
								$_GRIDS[$grid_id]['naslov_graf'] = $vrednost['naslov_graf'];

                                if(!isset($_HEADER[$spr_data_id]['cnt_all'])) 
                                    $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                                else 
                                    $_HEADER[$spr_data_id]['cnt_all'] += $cnt;

								$grid_id++;
							} // end foreach
						} // end if
						
						if ($tip == 16) {
							#dodamo opcije (za spss)
							$_HEADER[$spr_data_id]['options']['0'] = '0';
							$_HEADER[$spr_data_id]['options']['1'] = '1';
						}

						$_HEADER[$spr_data_id]['cnt_grids'] = $grid_id;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;

					
					# RANKING
					case 17:
					# VSOTA
					case 18:
						$_spss = ($tip == 17)
						? 'F'.$spss_lngth3.'.0'
						: 'F'.($spr_cela+$spr_decimalna).($spr_decimalna > 0 ? ('.'.$spr_decimalna) : '.0');

						$cnt=0;
						$arrayVrednost = $_array_vrednosti[$spr_id];
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							foreach ($arrayVrednost as $kid=> $vrednost) {
								$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$vrednostLoopSufix, 'other'=>false, 'text'=>false,'spss'=>$_spss,'sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
								$_sequences .= $_seq_prefix.$sequence;
								$_seq_prefix = '_';
								$sequence++;
								$cnt++;
								if ($tip == 17) {
									$_HEADER[$spr_data_id]['options'][$vrednost['vrstni_red']] = $vrednost['vrstni_red'];
								}
							} // end foreach
						} // end if
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0][	'cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
                        if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
						
					# BESEDILO*
					case 21:
						$cnt=0;
						$arrayVrednost = $_array_vrednosti[$spr_id];
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							foreach ($arrayVrednost as $kid=> $vrednost) {
								if ($vrednost['other'] == 0) {

									$_variable = (count($arrayVrednost) == 1)
									? $spr_variable
									: $vrednost['variable'].$vrednostLoopSufix ;
									$_naslov = (trim($vrednost['naslov']) != '' && trim($vrednost['naslov']) != $lang['srv_new_text']) ? $vrednost['naslov'] : $spr_naslov;
                                                                        $_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$_naslov, 'variable'=>$_variable, 'other'=>false, 'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
									$_sequences .= $_seq_prefix.$sequence;
									$_seq_prefix = '_';
									$sequence++;
									$cnt++;
								} // end if
							} // end foreach
						} // end if
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
                        if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
						
						
					# KALKULACIJA
					case 22:
						$cnt=0;
						# dodamo header variable
						$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$spr_naslov, 'variable'=>$spr_variable, 'other'=>false, 'text'=>true,'spss'=>'F'.$spss_lngth.'.0','sequence'=>$sequence);
						$_sequences .= $_seq_prefix.$sequence;
						$_seq_prefix = '_';
						$sequence++;
						$cnt++;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
                        if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
					
					
					# KVOTA
					case 25:
						$cnt=0;
						# dodamo header variable
						$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$spr_naslov, 'variable'=>$spr_variable, 'other'=>false, 'text'=>true,'spss'=>'F'.$spss_lngth.'.0','sequence'=>$sequence);
						$_sequences .= $_seq_prefix.$sequence;
						$_seq_prefix = '_';
						$sequence++;
						$cnt++;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;

					
					# SN - IMENA
					case 9:
						$cnt=0;
                        $arrayVrednost = isset($this->SNVariablesForSpr[$_vprasanje_array['id']]) ? $this->SNVariablesForSpr[$_vprasanje_array['id']] : array();
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							foreach ($arrayVrednost as $kid=> $vrednost) {
								$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost,'naslov'=>($spr_variable.'_'.($cnt+1)), 'variable'=>($spr_variable.'_'.($cnt+1)), 'other'=>false, 'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
								$_sequences .= $_seq_prefix.$sequence;
								$_seq_prefix = '_';
								$sequence++;
								$cnt++;
							} // end foreach
						} // end if
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_GRIDS[0]['cnt_vars'] = $cnt;
						$_GRIDS[0]['naslov'] = $spr_variable;
						if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                        else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
					break;
                            
							
                                        # Lokacija
					case 26:					
						$row = Cache::srv_spremenljivka($spr_id);
						
						if($row['enota'] == 3){
							$cnt=0;
							$arrayVrednost = $_array_vrednosti[$spr_id];
							if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
								foreach ($arrayVrednost as $kid=> $vrednost) {
									if ($vrednost['other'] == 0) {

										$_variable = (count($arrayVrednost) == 1) ? $spr_variable
										: $vrednost['variable'].$vrednostLoopSufix ;
										$_naslov = (trim($vrednost['naslov']) != '' && trim($vrednost['naslov']) != $lang['srv_new_text']) ? $vrednost['naslov'] : $_variable;
										$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'],'naslov'=>$_naslov, 'variable'=>$_variable, 'other'=>false, 'text'=>true,'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
                                                                                $_sequences .= $_seq_prefix.$sequence;
										$_seq_prefix = '_';
										$sequence++;
										$cnt++;
									} // end if
								} // end foreach
							} // end if
							$_HEADER[$spr_data_id]['cnt_grids'] = 1;
							$_GRIDS[0]['cnt_vars'] = $cnt;
							$_GRIDS[0]['naslov'] = $spr_variable;
							if(!isset($_HEADER[$spr_data_id]['cnt_all'])) $_HEADER[$spr_data_id]['cnt_all'] = $cnt;
                            else $_HEADER[$spr_data_id]['cnt_all'] += $cnt;
							$_HEADER[$spr_data_id]['sequences'] = $_sequences;
						}
						else{
							$newParams = new enkaParameters($row['params']);
							$is_podvprasanje = $newParams->get('marker_podvprasanje') == 1;
							$multi_input_type = $newParams->get('multi_input_type');
							if($multi_input_type == 'marker'){
								$arrayNaslovov = ($is_podvprasanje) ? 
										array(array('naslov' => $lang['srv_data_column_naslov_map'], 'variable' => $spr_variable.'a'), 
                                                                                    array('naslov' => $lang['srv_data_column_vrednost_map'], 'variable' => $spr_variable.'b'),
                                                                                    array('naslov' => $lang['srv_data_column_koordinate_map'], 'variable' => $spr_variable.'c'))
										: array(array('naslov' => $lang['srv_data_column_naslov_map'], 'variable' => $spr_variable.'a'), 
                                                                                    array('naslov' => $lang['srv_data_column_koordinate_map'], 'variable' => $spr_variable.'b'));
							}
							else
								$arrayNaslovov = array(array('naslov' => $lang['srv_data_column_koordinate_map'], 'variable' => $spr_variable.'a'));

							//kot grid
							$cnt=0;
							# dodamo header variable
							foreach($arrayNaslovov as $naslo){
								$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$naslo['naslov'], 'variable'=>$naslo['variable'], 'other'=>false, 'text'=>true, 'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
                                                                $_sequences .= $_seq_prefix.$sequence;
								$_seq_prefix = '_';
								$sequence++;

								$cnt++;
							}
							//$cnt++;
							$_GRIDS[0]['cnt_vars'] = count($arrayNaslovov);
							$_GRIDS[0]['naslov'] = $spr_variable;
							$_HEADER[$spr_data_id]['cnt_grids'] = count($arrayNaslovov);
							$_HEADER[$spr_data_id]['cnt_all'] = count($arrayNaslovov);
							$_HEADER[$spr_data_id]['sequences'] = $_sequences;
						}
					break;
					
					
					# HEATMAP
					case 27:
						$arrayNaslovov = array($lang['srv_data_column_koordinate_map']);
						//kot grid
						$cnt=0;
						# dodamo header variable
						foreach($arrayNaslovov as $naslo){
							$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>null, 'naslov'=>$naslo, 'variable'=>$spr_variable, 'other'=>false, 'text'=>true, 'spss'=>'A'.$spss_lngth,'sequence'=>$sequence);
							$_sequences .= $_seq_prefix.$sequence;
							$_seq_prefix = '_';
							$sequence++;							
							$cnt++;
						}
						//checkbox vrednosti za imena obmocij
						$arrayVrednost = isset($_array_vrednosti[$spr_id]) ? $_array_vrednosti[$spr_id] : null;
						if (is_countable($arrayVrednost) && count($arrayVrednost) > 0) {
							foreach ($arrayVrednost as $vrednost) {

								# dodamo header variable samo za ne -missing variable
								if ($vrednost['other'] == 0 || $vrednost['other'] == 1) {
									$_GRIDS[0]['variables'][$cnt] = array('vr_id'=>$vrednost['id'], 'naslov'=>$vrednost['naslov'], 'variable'=>$vrednost['variable'].$vrednostLoopSufix, 'other'=>false,'text'=>false,'spss'=>'F'.$spss_lngth3.'.0','sequence'=>$sequence,'naslov_graf'=>$vrednost['naslov_graf']);
									$_sequences .= $_seq_prefix.$sequence;
									$_seq_prefix = '_';
									$sequence++;
									$cnt++;
									#dodamo še header za polja drugo
								}
							} // end foreach
						} // end if
						//checkbox vrednosti za imena obmocij - konec
												#dodamo opcije (za spss)
						$_HEADER[$spr_data_id]['options']['0'] = '0';
						$_HEADER[$spr_data_id]['options']['1'] = '1';
						
						
						$_GRIDS[0]['cnt_vars'] = $cnt;	//stevilo stolpcev pod enim vprasanjem
						$_GRIDS[0]['naslov'] = $spr_variable;
						$_HEADER[$spr_data_id]['cnt_grids'] = 1;
						$_HEADER[$spr_data_id]['cnt_all'] = $cnt;	//koliko stolpcev mora pokriti naslov tega vprasanja
						$_HEADER[$spr_data_id]['sequences'] = $_sequences;
						
					break;
				}

				$_HEADER[$spr_data_id]['grids'] = $_GRIDS;
			} // end foreach ($this->AllQuestionsOrder AS $_vprasanje_array) {
		} // end if (count($this->AllQuestionsOrder) > 0)


		// DODAMO SPECIAL META         
        // s katero sekvenco se začnejo meta podatki
        $_HEADER['_settings']['metaSequence'] = $sequence;
    
        $_HEADER['meta'] = array('tip'=>'sm', 'variable'=>'smeta', 'naslov' =>$lang['srv_displaydata_meta2'],'cnt_all'=>2);
        
        // Datum insert, datum edit, datume in čase za posamezno stran
        $_g_cnt = 0;
        $_tmp_seq = $sequence;

        // Na zacetku prikazemo randomizacijo ce je prisotna pri kaksnem vprasanju ali bloku
        foreach ($this->_array_random as $parent_id => $random_parent) {

            if($random_parent['type'] == 'spr'){
                $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'Random '.$random_parent['variable']);	
                $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'random_'.$random_parent['id'],'naslov'=>$random_parent['variable'],'sequence'=>$sequence);
                $_g_cnt++;
                $sequence++;
            }
            elseif($random_parent['type'] == 'blok_spr'){
                $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'Random B'.$random_parent['number']);
                $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'random_'.$random_parent['id'],'naslov'=>'B'.$random_parent['number'],'sequence'=>$sequence);
                $_g_cnt++;
                $sequence++;
            }
            elseif($random_parent['type'] == 'blok_blok'){
                $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'Random B'.$random_parent['number']);
                $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'random_'.$random_parent['id'],'naslov'=>'B'.$random_parent['number'],'sequence'=>$sequence);
                $_g_cnt++;
                $sequence++;
            }	
        }

        // če mamo da prepozna uporabnika iz sispleta
        if ((int)$this->survey['user_from_cms'] > 0) {
            $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'E-mail iz CMS');
            $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'usr_from_cms','naslov'=>'E-mail iz CMS','spss'=>'A256','sequence'=>$sequence);
            $sequence++;
            $_g_cnt ++;
        }

        // datum
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['date']);
        
        // time insert
        $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'t_insert','naslov'=>$lang['date_insert'],'spss'=>'DATETIMEw','sortType'=>'date','sequence'=>$sequence);
        $sequence++;

        // time header
        $_HEADER['meta']['grids'][$_g_cnt]['variables'][1] = Array ('variable'=>'t_edit','naslov'=>$lang['date_edit'],'spss'=>'DATETIMEw','sortType'=>'date','sequence'=>$sequence);
        $sequence++;

        // dodamo št. variabel na grupo
        $_HEADER['meta']['grids'][$_g_cnt]['cnt_vars'] = 2;
        $_g_cnt ++;

        # datumi in časi po posameznih straneh
        # zaloopamo skozi strani

        // zaloopamo skozi strani
        $page = 1;
        foreach ($this->_array_groups as $gid => $grupa) {

            // Uvod
            if($gid == '0'){
                $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['intro']);
            
                # date on page
                $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'date_0','naslov'=>'datum_0','spss'=>'DATETIMEw','sortType'=>'date','sequence'=>$sequence);
            }
            else{
                $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['page'].' '.$page);
            
                # date on page
                $_HEADER['meta']['grids'][$_g_cnt]['variables'][0] = Array ('variable'=>'date_'.$page,'naslov'=>'datum_'.$page,'spss'=>'DATETIMEw','sortType'=>'date','sequence'=>$sequence);
                
                $page++;
            }   

            $_HEADER['meta']['grids'][$_g_cnt]['cnt_vars'] = 1;
            $sequence++; $_g_cnt++;
        }
        
        // IP
        $ip = SurveySetting::getInstance()->getSurveyMiscSetting('survey_ip');
        $ip_show = SurveySetting::getInstance()->getSurveyMiscSetting('survey_show_ip');
        if($ip==0 && $ip_show==1 && ($admin_type == 0 || $admin_type == 1)){
            $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'IP',
                                                        'variables' => array( 0 => Array ('variable'=>'IP','naslov'=>'IP','spss'=>'A32','sequence'=>$sequence)));
            $sequence++; $_g_cnt++;
        }

        // Browser
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['browser'],
                                                    'variables' => array( 0 => Array ('variable'=>'Browser','naslov'=>$lang['browser'],'spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;
        
        // Browser version
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['browser_version'],
                                                    'variables' => array( 0 => Array ('variable'=>'BrowserVersion','naslov'=>$lang['browser_version'],'spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;
        
        // OS
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'OS',
                                                    'variables' => array( 0 => Array ('variable'=>'OS','naslov'=>$lang['srv_para_graph_os'],'spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;
        
        // Device
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['srv_para_graph_device'],
                                                    'variables' => array( 0 => Array ('variable'=>'Device','naslov'=>$lang['srv_para_graph_device'],'spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;

        // Referer
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'Referer',
                                                    'variables' => array( 0 => Array ('variable'=>'Referer','naslov'=>'Referer','spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;

        // unsubscribed
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>'Unsubscribed',
                                                    'variables' => array( 0 => Array ('variable'=>'Unsubscribed','naslov'=>'Unsubscribed','spss'=>'A2','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;

        // jezik - language
        $_HEADER['meta']['grids'][$_g_cnt] = Array('naslov'=>$lang['lang'],
                                                    'variables' => array( 0 => Array ('variable'=>'Language','naslov'=>$lang['lang'],'spss'=>'A256','sequence'=>$sequence)));
        $sequence++; $_g_cnt++;
        
        // dodamo št. variabel za celotno skupino
        $_HEADER['meta']['cnt_all'] = $sequence - $_tmp_seq;
        
        
		// USABILITY - ce vsebuje nastavitev da filtriramo po uporabnosti dodamo tudi to polje
		if (SurveyStatusProfiles::usabilitySettings()) {		
			$_HEADER['usability'] = Array('naslov'=>$lang['srv_usableResp_usability'],
											'variables' => array( 0 => Array ('variable'=>'Usability','naslov'=>$lang['srv_usableResp_usability'],'spss'=>'A2','sequence'=>$sequence)));
											
			$sequence++;
		}
		
		$this->_HEADER = $_HEADER;
	}

	// Zbiramo podatke
	public function CollectData($c, $file_handler) {
		global $site_path, $site_url, $lang, $admin_type;

		# osvežimo podatke
		if (mysqli_num_rows($this->_qry_users[$c]) > 0) {
			mysqli_data_seek($this->_qry_users[$c], 0);
        }
        
		$_tmpCnt = $c * MAX_USER_PER_LOOP;

		$_dataLine = '';

		// Dobimo vse jezike za katere obstaja jezikovna datoteka
        include_once($site_path.'lang/jeziki.php');
		$jeziki = $lang_all_global['ime'];
		// lang od 0 je privzet
		$jeziki[0] = $lang['language'];
        $jeziki[$lang['id']] = $lang['language'];

		if ($this->noErrors) {
            
			while ($rowUser = mysqli_fetch_assoc($this->_qry_users[$c])) {
                
                #sleep(1);
				$_tmpCnt++;
				
				$uid = $rowUser['usr_id'];
				# dodamo usr id k podatkom
				$_dataLine .= $rowUser['usr_id'];

                # dodamo status k podatkom
				$_dataLine .= STR_DLMT.$rowUser['status'];
				
				# dodamo email (invitation)k podatkom - če je bilo poslano z emailom ali je uporabnik ročno vnesel email
				$_dataLine .= STR_DLMT. ((int)$rowUser['inv_res_id'] > 0 || (int)$rowUser['inv_res_id'] == -1
						# uporabnik je bil dodan z email vabilom
						? ((int)$rowUser['status'] == 1 || (int)$rowUser['status'] >= 3
								# email je bil odposlan
								? '1'
								# email ni bil odposlan ali je bila napaka
								: '2')
				# uporabnik ni bil dodan z email vabilom
				: '0');

                # dodamo ustreznost k podatkom - relevance
				$_dataLine .= STR_DLMT. (($rowUser['status'] == 5 || $rowUser['status'] == 6) && $rowUser['lurker'] == 0 ? '1' : '0');
				
				# dodamo lurkerje
				$_dataLine .= STR_DLMT.$rowUser['lurker'];
					
				#dodamo unx_ins_date
				$_dataLine .= STR_DLMT.max($rowUser['unx_ins_date'],$rowUser['unx_edt_date']);
	
				#dodamo recnum
				$_dataLine .= STR_DLMT.$rowUser['recnum'];
	
				#dodamo geslo
				if ($this->force_show_hiden_system == true) {
					$_dataLine .= STR_DLMT.$rowUser['pass'];
				}
				
				# če vsebuje testne podatke dodamo tudi kolono z tem statusom
				if ($this->has_test_data) {
					$_dataLine .= STR_DLMT.(int)$rowUser['testdata'];
				}
	
				# dodamo insert time k podatkom
				$_dataLine .= STR_DLMT.date("d.m.Y",strtotime($rowUser['time_insert']));
				
				#dodamo uporabnikove podatke
				# zloopamo skozi vprašanja
				
				# dodamo vprašanja ki so v loopu
				$userAnswerLine = '';
				
				#resetiramo
				$this->_user_spr_answer_count = array('cnt'=>0, 'last'=>0, 'spremenljivke'=>array());
				$pages = array();
				if ($this->noErrors && count($this->AllQuestionsOrder) > 0) {
					foreach ($this->AllQuestionsOrder AS $_vprasanje_array) {
                        
                        // Ce vrpasanje ni nastavljeno preskocimo
                        if(!isset($this->AllQuestionsData[$_vprasanje_array['id']]))
                            continue;

                        $rowVprasanje = $this->AllQuestionsData[$_vprasanje_array['id']];
                        
                        # spremenljivki dodamo loop_id da je konsistentno z podatki
						$rowVprasanje['spr_id'] = $rowVprasanje['spr_id'].'_'.$_vprasanje_array['loop_id'];
						if (isset($this->_array_loop_on_spr[$rowVprasanje['if_id']])) {
							$_vrednosti = $this->get_vrednosti($this->_array_loop_on_spr[$rowVprasanje['if_id']]);
                        }
                        
						if(isset($this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']])) {
							$_loop_vrednost = $this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']];
                        }
                        
						# popravimo ime variable če smo v loopu
						if(isset($this->_array_vre_on_loop[$rowVprasanje['if_id']][$_vprasanje_array['loop_id']])) {
							$rowVprasanje['variable'] = strip_tags($rowVprasanje['variable'])."_".$_vrednosti[$_loop_vrednost]['variable'];
						}
	
						# pomožne variable
						$tip = 	$rowVprasanje['tip'];
						$_tmp_spr_id = explode('_',$rowVprasanje['spr_id']);
						$spr_id = $_tmp_spr_id[0];
						$spr_data_id = $rowVprasanje['spr_id'];
						$spr_naslov = strip_tags($rowVprasanje['naslov']);
						$spr_variable = strip_tags($rowVprasanje['variable']);
						$spr_size = $rowVprasanje['size'];
						$spr_cela = $rowVprasanje['cela'];
						$grid_subtitle1 = $rowVprasanje['grid_subtitle1'];
						$grid_subtitle2 = $rowVprasanje['grid_subtitle2'];
						$spr_decimalna = $rowVprasanje['decimalna'];
						$spr_skala = $rowVprasanje['skala'];
						$spr_sistem = $rowVprasanje['sistem'];
						$spr_page = $rowVprasanje['gru_id'];
						$spr_upload = $rowVprasanje['upload'];
						$spr_signature = $rowVprasanje['signature'];
						$spr_visible = $rowVprasanje['visible'];
						$spr_dostop = $rowVprasanje['dostop'];
						$pages[$spr_page] = true;
						
						# ponastavimo katere vrednosti se beležijo kot neodgovori na spremenljivko
						# ali je to -3 ali -1. Če uporabnik še ni bil na tej strani je -3 če ne je -1
						# ali pa če gre za email vabila dodamo status -3
						if ((($rowUser['status'] == 6 || $rowUser['status'] == 5  ) && $rowUser['lurker'] != 1) )  {			
							// Ce je skrito je -2 (-2d da ga pobrisemo z -3 ce je potrebno)
							if($spr_visible == 0 || !(($admin_type <= $spr_dostop && $admin_type>=0) || ($admin_type==-1 && $spr_dostop==4))){
								$VALUE_FOR_MISSING = '-2d';
							}
							else{							
								# če so veljavni odgovori damo -1 ali -3
								# na -3 popravimo na okncu
								#$VALUE_FOR_MISSING = isset($this->_array_user_grupa[$uid][$spr_page]) ? '-1' : '-1';
								$VALUE_FOR_MISSING = '-1';
							}
						} 
						else if ( $rowUser['status'] == 0 || $rowUser['status'] == 1 || $rowUser['status'] == 2 ) {
							# če je email vabilo
							#$VALUE_FOR_MISSING = '-3';
							$VALUE_FOR_MISSING = '-5';
						} 
						else {
							# to so lurkerji
							# če imamo neveljavne odgovore damo -5
							$VALUE_FOR_MISSING = '-5';
						}
						# array z vrednosmi rabimo za podatke in za polja drugo
						if (isset($this->_array_vrednosti[$spr_id])) {
							$spr_vrednosti = $this->_array_vrednosti[$spr_id];
						}
						#gridi
						if (isset($this->_array_gridi[$spr_id])) {
							$spr_gridi = $this->_array_gridi[$spr_id];
						}

						#ODGOVORI
						#tekstovno odgovori
						$spr_data_text = $this->get_array_data_text($uid,$spr_data_id);
						unset($this->_array_data_text[$uid][$spr_data_id]);
						# vrednostni odgovori
						$spr_data_vred = $this->get_array_data_vrednost($uid,$spr_data_id);
						unset($this->_array_data_vrednost[$uid][$spr_data_id]);
						# preslkočeni vrednostni odgovori
						$spr_data_vrednost_cond = $this->get_array_data_vrednost_cond($uid,$spr_data_id);
						unset($this->_array_data_vrednost_cond[$uid][$spr_data_id]);
						# grid odgovori
						$spr_data_grid = $this->get_array_data_grids($uid,$spr_data_id);
						unset($this->_array_data_grids[$uid][$spr_data_id]);
						# grid odgovori za checkbox
						$spr_data_grid_check = $this->get_array_data_check_grids($uid,$spr_data_id);
						unset($this->_array_data_check_grids[$uid][$spr_data_id]);
						# ranking odgovori
						$spr_data_ranking = $this->get_array_data_rating($uid,$spr_data_id);
						unset($this->_array_data_rating[$uid][$spr_data_id]);
						# textgrid odgovori
						$spr_data_grid_text = $this->get_array_data_text_grid($uid,$spr_data_id);
						unset($this->_array_data_text_grid[$uid][$spr_data_id]);
                        # lokacijski odgovori
						$spr_data_map = $this->get_array_data_map($uid,$spr_data_id);
						unset($this->_array_data_map[$uid][$spr_data_id]);
						# heatmap odgovori
						$spr_data_heatmap = $this->get_array_data_heatmap($uid,$spr_data_id);
						unset($this->_array_data_heatmap[$uid][$spr_data_id]);
						$spr_data_heatmap_regions = $this->get_array_data_heatmap_regions($uid,$spr_data_id);
						unset($this->_array_data_heatmap_regions[$uid][$spr_data_id]);

                        
                        // v odvisnosti od tipa vprašanja pohandlamo podatke
						switch ( $tip ) {
							
							# RADIO BUTTON in DROPDOWN - SELECT
							case 1:
							case 3:
	
                                $answer = null;
                                $_vred = null;

                                if (is_array($spr_data_vred)) {
                                    $key = key($spr_data_vred);
                                    
                                    if (isset($spr_data_vred[$key])) {
                                        $_vred = $spr_data_vred[$key];
                                    }
                                } 
                                else {
                                    $_vred = $VALUE_FOR_MISSING;
                                }

                                if ($_vred > 0) {
                                    $answer = $spr_vrednosti[$_vred]['variable'];
                                } 
                                else {
                                    $answer = $_vred;
                                }

                                if ( $answer == '' || $answer == null) {
                                    $answer = $VALUE_FOR_MISSING;
                                }
        
                                # dodamo podatek
                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                
                                // poiščemo polja drugo
                                if (isset($spr_vrednosti) && is_countable($spr_vrednosti) && count($spr_vrednosti) > 0){

                                    foreach ($spr_vrednosti AS $vid => $vrednost) {
                                        
                                        if ($vrednost['other'] == 1) {

                                            if ($answer == -1 || $answer == -2 || $answer == -3 || $answer == -4 || $answer == -96 || $answer == -97 || $answer == -98 || $answer == -99 ) {
                                                $answerOther = $answer;
                                            }
                                            else {

                                                if (isset($spr_data_text[$vid]['text'])) {
                                                    $answerOther = ($spr_data_text[$vid]['text'] == "" || $spr_data_text[$vid]['text'] == null) ? $VALUE_FOR_MISSING : $spr_data_text[$vid]['text'];
                                                }
                                                elseif($spr_vrednosti[$vid]['variable'] != $answer){
                                                    // Ce je bil -1 texta ni mogel vnesti in mora bit -2
                                                    if($VALUE_FOR_MISSING == -1)
                                                        $answerOther = '-2d';
                                                    else
                                                        $answerOther = $VALUE_FOR_MISSING;
                                                }
                                                else {
                                                    $answerOther = $VALUE_FOR_MISSING;
                                                }
                                            }

                                            // dodamo podatek
                                            $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);         
                                        }
                                    }
                                }

                                break;
    
                                
							# CHECKBOX
							case 2:
								$answer = null;
								$_tmp_answers = array();

								$_anything_set = false;
								$_has_missing = NULL;
								
								# najprej dodelimo odgovore posameznim vrednostim če obstatajo
								if (count($spr_vrednosti) > 0){
                                    foreach ($spr_vrednosti AS $vid => $vrednost) {
                                    
                                        if (isset($spr_data_vred[$vid]) && $_has_missing == NULL) {
                                            
                                            // če je spr_data_vrednost obstaja je checkbox obkljukan
                                            // preverimo ali imamo missing
                                            if ($vrednost['other'] != 0 && $vrednost['other'] != 1 ) {
                                                # odgovor je missing, vse vrednosti nastavimo na ta missing, zato pobrišemo morebitne že dodane odgovore
                                                unset($_tmp_answers);
                                                unset($spr_data_vred[$vid]);

                                                $_has_missing = $spr_vrednosti[$vid]['variable'];
                                            } 
                                            else {
                                                $_tmp_answers[$vid] = 1;
                                                unset($spr_data_vred[$vid]);
                                                $_anything_set = true;
                                            }
                                        } 
                                        else if (isset($spr_data_vrednost_cond[$vid])) {
                                            $_tmp_answers[$vid] = $spr_data_vrednost_cond[$vid];
                                        }
                                    }
								}
								
								// če ni nič nastavljeno, je lahko samo -1, -2, -3,-4
								$missing_answers = 0;
								# preverimo ali so ostale še kakšne vrednosti, potem so najbrž missingi
								#najprej preverimo missing na variabli
								if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
									unset($_tmp_answers);
									$_has_missing = end($spr_data_vred);
									$missing_answers = $_has_missing;
									$_anything_set = false;
								}
								
								if ($_anything_set == false || $_has_missing != null) {
									if ($_has_missing != null) {
										# immamo misssing value
										$missing_answers = $_has_missing;
									} else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
										$missing_answers = end($spr_data_vred); # lahko da je -2, -4
									} else {
										// lahko je -1, -3
										$missing_answers = $VALUE_FOR_MISSING;
									}
								}
		
								# še enkrat zloopamo skozi vrednosti in dodelimo odgovore, tudi za polja drugo
								if (count($spr_vrednosti) > 0) {
									if ($missing_answers == 0 ) {
										$missing_answers = $VALUE_FOR_MISSING;
									}
									foreach ($spr_vrednosti AS $vid => $vrednost) {
										# dodamo samo variable ki niso missing
										if ($vrednost['other'] == 0 || $vrednost['other'] == 1) {
											if (isset($_tmp_answers[$vid])) {
												$answer = $_tmp_answers[$vid];
											} else if ($_anything_set == true){
												#pri checkboxu je lahko tudi 0
												$answer = 0;
											} else {
												$answer = $missing_answers;
											}
											
											# dodamo odgovor
											$userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
											
											#dodamo še polje drugo
											if ($vrednost['other'] == 1) {
										
												#ce smo meli checkboc obkljukan
												if (isset($_tmp_answers[$vid])) {
													$answerOther = (!isset($spr_data_text[$vid]['text']) || $spr_data_text[$vid]['text'] == "" || $spr_data_text[$vid]['text'] == null) ? $VALUE_FOR_MISSING : $spr_data_text[$vid]['text'];
												} else if ($missing_answers !== null) {
													// Ce je bil 0 texta ni mogel vnesti in mora bit -2
													if($VALUE_FOR_MISSING == -1)
														$answerOther = '-2d';
													else
														$answerOther = $missing_answers;
												} else if ($answer == 0) {
													$answerOther = $VALUE_FOR_MISSING;
												} else {
													$answerOther = $answer;
												}
												# dodamo odgvor
												$userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);
												
											}
										}
									}
								}
		
							    break;
	
							# TEXT
							case 4:
                                $answer = null;
                                
                                # ce obstaja $spr_data_vred je po vsej vrjetnosti missing
                                if ( count($spr_data_vred) > 0 ) {
                                    $key = key($spr_data_vred);
                                    $answer = $spr_data_vred[$key];
                                    //unset ($spr_data_vred[$key]);
                                } 
                                else {
                                    $answer = $spr_data_text[0]['text'];
                                }

                                if ( $answer == '' || $answer == null) {
                                    $answer = $VALUE_FOR_MISSING;
                                }

                                // dodamo odgovor
                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                
                                break;
        
                                
							# MULTI RADIO BUTTON
							case 6:
								
                                # dvojni gridi so zapisani v $spr_data_grid_check
                                if ((int)$rowVprasanje['enota'] != 3) {

                                    # ni dvojni grid
                                    $answer = null;

                                    # zloopamo skozi podvprašanja (srv_vrednost
                                    if (count($spr_vrednosti) > 0){
                                        foreach ($spr_vrednosti AS $vid => $vrednost) {

                                            $missing_answer = false;

                                            if (is_array($spr_data_vred) && count($spr_data_vred) == 1 ) {
                                                $answer = end($spr_data_vred); # lahko da je -2, -4
                                                $missing_answer = true;
                                            } 
                                            else if (isset($spr_data_grid[$vid])) {
                                                if ($spr_data_grid[$vid] > 0) {
                                                    if ($spr_gridi[$spr_data_grid[$vid]]['other'] == 0) {
                                                        # preberemo grid vrednost
                                                        $answer = $spr_gridi[$spr_data_grid[$vid]]['variable']; // tukaj dodelimo ime variable, lahko bi tudi id ali vrstni red???
                                                        $missing_answer = false;
                                                    } else {
                                                        $answer = $spr_gridi[$spr_data_grid[$vid]]['other'];
                                                    }
                                                }else {
                                                    # je missing
                                                    $answer = $spr_data_grid[$vid];
                                                    $missing_answer = true;
                                                }
                                            } 
                                            else {
                                                $answer = $VALUE_FOR_MISSING;
                                                $missing_answer = true;
                                            }
                                            
                                            # dodamo odgovor
                                            $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                            
                
                                            if ($vrednost['other'] == 1) {
                                                # če answer ni missing
                                                if ($missing_answer == false) {
                                                    if (isset($spr_data_text[$vid]['text'])) {
                                                        $answerOther = ($spr_data_text[$vid]['text'] == "" || $spr_data_text[$vid]['text'] == null) ? $VALUE_FOR_MISSING : $spr_data_text[$vid]['text'];
                                                    }else {
                                                        $answerOther = $VALUE_FOR_MISSING;
                                                    }
                                                } 
                                                else {
                                                    $answerOther = $answer;
                                                }

                                                # dodamo odgvor
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);       
                                            }
                                        }
                                    }
                                } 
                                else {
                                    # DOUBLE GRID
                                    
                                    # če imamo dvojni grid gremo 2_skozi
                                    $double = (int)($rowVprasanje['enota'] == 3);
                                    $_tmp_answers = array();
                
                                    for ($i=1; $i<=$double+1;$i++) {
                                        // for $double
                
                                        # sfiltriramo odgovore za posamezen part
                                        $_tmp_part_answers = array();
                                        if (count($spr_data_grid_check) > 0) {
                                            foreach($spr_data_grid_check as $vre_id => $grids) {
                                                if (count($grids) > 0) {
                                                    foreach($grids AS $gid => $grid) {
                                                        if ($spr_gridi[$gid]['part'] == $i) {
                                                            if ($spr_gridi[$gid]['other'] == 0) {
                                                                $_tmp_part_answers[$vre_id] = $spr_gridi[$gid]['variable'];
                                                            } else {
                                                                $_tmp_part_answers[$vre_id] = $spr_gridi[$gid]['other'];
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        $answer = null;
                                        # zloopamo skozi podvprašanja (srv_vrednost
                                        if (count($spr_vrednosti) > 0) {
                                            foreach ($spr_vrednosti AS $vid => $vrednost) {
                                                $missing_answer=false;
                                                if (is_array($spr_data_vred) && count($spr_data_vred) == 1 ) {
                                                    $answer = end($spr_data_vred); # lahko da je -2, -4
                                                    $missing_answer = true;
                                                } else if (isset($_tmp_part_answers[$vid])) {
                                                    if ($_tmp_part_answers[$vid] > 0) {
                                                        # preberemo grid vrednost
                                                        $answer = $_tmp_part_answers[$vid]; // tukaj dodelimo ime variable, lahko bi tudi id ali vrstni red???
                                                        $missing_answer = false;
                                                    } else {
                                                        $answer = $_tmp_part_answers[$vid]; // tukaj dodelimo ime variable, lahko bi tudi id ali vrstni red???
                                                        $missing_answer = true;
                                                    }
                                                } else {
                                                    $answer = $VALUE_FOR_MISSING;
                                                    $missing_answer = true;
                                                }
                    
                                                # dodamo odgovor
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                                
                    
                                                if ($vrednost['other'] == 1) {
                                                    # če answer ni missing
                                                    if ($missing_answer == false) {
                                                    $answerOther = ($spr_data_text[$vid]['text'] == "" || $spr_data_text[$vid]['text'] == null) ? $VALUE_FOR_MISSING : $spr_data_text[$vid]['text'];
                                                } else {
                                                    $answerOther = $answer;
                                                }
                                                # dodamo odgvor
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);
                                                
                                                }
                                            }
                                        }
                                    } // for $double
                                }

                                break;


							# NUMBER in DATE
							case 7:
							case 8:
	
                                $_vred = null;
                                $answer0 = null;
            
                                # ce imamo odgovor
                                if (isset ($spr_data_text[0])) {
                                    $answer0 = ($spr_data_text[0]['text'] != '') ? $spr_data_text[0]['text'] : $VALUE_FOR_MISSING;
            
                                } 
                                else if (is_array($spr_data_vred)) {
                                    $key = key($spr_data_vred);
                                    if (isset($spr_data_vred[$key])) {
                                        $_vred = $spr_data_vred[$key];
                                    }
                                    #pogledamo če je odgovor missing:
                                    if (isset($spr_vrednosti[$_vred])) {
                                        $answer0 = $spr_vrednosti[$_vred]['variable'];
                                    } else {
                                        $answer0 = $_vred;
                                    }
            
                                }
        
                                $answer0 = ($answer0 == null ) ? $VALUE_FOR_MISSING : $answer0;
                                
                                # dodamo odgovor
                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer0,$uid);
                                
        
                                # ve imamo size = 2
                                if ($tip == 7 && $spr_size > 1) {
            
                                    $answer1 = null;
                                    # ce imamo odgovor
                                    if (isset ($spr_data_text[0])) {
                                        $answer1 = ($spr_data_text[0]['text2'] != '') ? $spr_data_text[0]['text2'] : $VALUE_FOR_MISSING;
                                    } else if (is_array($spr_data_vred)) {
                                        $key = key($spr_data_vred);
                                        if (isset($spr_data_vred[$key])) {
                                            $_vred = $spr_data_vred[$key];
                                        }
                                        #pogledamo če je odgovor missing:
                                        if (isset($spr_vrednosti[$_vred])) {
                                            $answer1 = $spr_vrednosti[$_vred]['variable'];
                                        } else {
                                            $answer1 = $_vred;
                                        }
                                    }
            
                                    $answer1 = ($answer1 == null ) ? $VALUE_FOR_MISSING : $answer1;
                                        
                                    # dodamo odgovor
                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer1,$uid);
                                    
                                }

							    break;
	
	
						    # MULTI CHECKBOX
							case 16:
                                # delamo na nivoju $spr_vrednosti kar predstavlja podvprašanje
                                if (count($spr_vrednosti) > 0) {
                                foreach ($spr_vrednosti AS $vid => $vrednost) {
                                    $_tmp_answers = array();
                                    $_anything_set = false;
                                    $_missing_answer = NULL;
                                    # zloopamo skozi podvprašanja in nastavimo izbrane odgovore
                                    if (count($spr_gridi) > 0) {
                                        foreach ($spr_gridi AS $gid => $grid) {
                                            if (isset($spr_data_grid_check[$vid][$gid]) && $_missing_answer == NULL) {
                                                # če je spr_data_vrednost obstaja je checkbox obkljukan
                                                #Najprej preverimo ali je odgovro missing...
                                                if ($spr_gridi[$gid]['other'] == 0 || $spr_gridi[$gid]['other'] == 1) {
                                                $_tmp_answers[$vid][$gid] = 1;
                                                $_anything_set = true;
                                            } else {
                                                $_missing_answer = $spr_gridi[$gid]['other'];
                                                # izrišemo morebitne  1 ke pri vrednostih ki imajo kakršnkoli missing
                                                # ker ne more bit hkrati -99 ne vem in veljavni odgovor
                                                unset($_tmp_answers);
                                                $_anything_set = false;
                                            }
                                            unset($spr_data_grid_check[$vid][$gid]);
                                            }
                                        }
                                    }
                                    # nastavimo ali missinge, ali neveljavne ali 0;
                                    if ($_anything_set == false) {
                                        if ($_missing_answer != NULL) {
                                            # missing je že nastavljen
                                        } else  if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                            $_missing_answer = end($spr_data_vred);
                                        } else {
                                            $_missing_answer = $VALUE_FOR_MISSING;
                                        }
                                    }
                                    # dodamo odgovore
                                    if (count($spr_gridi)>0) {
                                        foreach ($spr_gridi AS $gid => $grid) {
                                            # dodamo samo gride ki niso missingi
                                            if ($spr_gridi[$gid]['other'] == 0 || $spr_gridi[$gid]['other'] == 1) {
                                                $answer = '0';
                                                # če je nastavljen missing so vsi gridi na podvprašanje enaki missingu
                                                if ($_missing_answer != NULL ) {
                                                    $answer = $_missing_answer;
                                                } else if (isset($_tmp_answers[$vid][$gid])) {
                                                    $answer = $_tmp_answers[$vid][$gid];
                                                }
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id, $answer,$uid);
                                                
                                            }
                                        }
                                        # dodamo še odgovore other text
                                        if ($vrednost['other'] == 1) {
                                            if ($_missing_answer != null) {
                                                $answerOther = $_missing_answer;
                                            } else if (isset($spr_data_text[$vid]['text']) && $spr_data_text[$vid]['text'] !== '') {
                                                $answerOther = $spr_data_text[$vid]['text'];
                                            } else {
                                                if ($_anything_set == false) {
                                                    if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                                        $answerOther = end($spr_data_vred);
                                                    } else {
                                                        $answerOther = $VALUE_FOR_MISSING;
                                                    }
                                                } else {
                                                    $answerOther = $VALUE_FOR_MISSING;
                                                }
                                            }
                                            # dodamo odgvor
                                            $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);
                                            
                                        }
                                    }
                                } // end foreach spr_vrednost
                            } // end if count spr_vrednost
        
                            break;
    
                            
						# RANKING
                        case 17:
                            
							if (count($spr_vrednosti ) > 0){
                                foreach ($spr_vrednosti AS $vid => $vrednost) {
                                    $answer = null;
                                    if (isset($spr_data_ranking[$vid])) {
                                        $answer = $spr_data_ranking[$vid];
                                    } else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                        $answer = end($spr_data_vred);
                                    }
            
                                    if ($answer == "" || $answer == null) {
                                        $answer = $VALUE_FOR_MISSING;
                                    } // end if
            
                                    # dodamo odgvor
                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                    
                                } // end foreach
                            }

						    break;
	
	
						#VSOTA
                        case 18:
                            
							if (count($spr_vrednosti ) > 0){
                                foreach ($spr_vrednosti AS $vid => $vrednost) {
                                    $answer = null;
                                    
                                    if (isset($spr_data_text[$vid]['text'])) {
                                        $answer = $spr_data_text[$vid]['text'];
                                    } 
                                    else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                        $answer = end($spr_data_vred);
                                    }
                                        
                                    if ($answer == "" || $answer == null) {
                                        $answer = $VALUE_FOR_MISSING;
                                    } // end if
            
                                    # dodamo odgvor
                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                    
                                } // end foreach
                            }

                            break;
                            
	
						# MULTITEXT in MULTINUMBER
						case 19:
						case 20:
	
							$answer = null;
                            $_tmp_answers = array();
                            $_anything_set = array();
        
                            $missing_value_temp = $VALUE_FOR_MISSING;
                                                    
                            # preverimo al mamo missing (-2 ali -4, ce je bila naknadno dodana) nad celo spremenljivko
                            if (isset($spr_data_vred) && count($spr_data_vred) == 1) {
                                $missing_value_temp = (is_array($spr_data_vred) && count($spr_data_vred) == 1 ) ? end($spr_data_vred) : $VALUE_FOR_MISSING;
                            }
        
                            $is_grid_missing = array();

                            # zloopamo skozi podvprašanja in nastavimo izbrane odgovore
                            if (count($spr_vrednosti ) > 0) {

                                foreach ($spr_vrednosti AS $vid => $vrednost) {

                                    if (count($spr_gridi) > 0) {

                                        foreach ($spr_gridi AS $gid => $grid) {

                                            # dodajamo samo odgovore ki so veljavni
                                            if ($grid['other'] == 0) {

                                                # imamo veljaven odgovor
                                                if (isset($spr_data_grid_text[$vid][$gid])) {           
                                                    $_tmp_answers[$vid][$gid] = $spr_data_grid_text[$vid][$gid];
                                                    $_anything_set[$vid] = true;
                                                } 
                                                else if (isset($spr_data_grid[$vid])) {
                                                    $_tmp_answers[$vid][$gid] = $spr_gridi[$spr_data_grid[$vid]]['other'];
                                                } 
                                                else {
                                                    $_tmp_answers[$vid][$gid] = $missing_value_temp;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
        
                            # dodamo odgovore
                            if(count($spr_vrednosti) > 0)
                            foreach ($spr_vrednosti AS $vid => $vrednost) {
                                if(count($spr_gridi) > 0)
                                foreach ($spr_gridi AS $gid => $grid) {
                                    if ($grid['other'] == 0) {
                                        # dodamo samo veljavne gride
                                        $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$_tmp_answers[$vid][$gid],$uid);
                                        
        
                                    }
                                }
                                    
                                # dodamo še odgovore other text
                                if ($vrednost['other'] == 1) {
                                    # če mamo missing dodamo missing
                                    if (isset($is_grid_missing[$vid])) {
                                        $answerOther = $is_grid_missing[$vid];
                                    } else if ($_anything_set[$vid]) {
                                        $answerOther = ($spr_data_text[$vid]['text'] == "" || $spr_data_text[$vid]['text'] == null) ? $missing_value_temp : $spr_data_text[$vid]['text'];
                                    } else {
                                        $answerOther = $missing_value_temp;
                                    }
        
                                    # dodamo odgvor
                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerOther,$uid);         
                                }
                            }

                            break;
                            

						# BESEDILO *
						case 21:
                            
                            # zloopamo skozi podvprašanja in nastavimo izbrane odgovore
                            if(count($spr_vrednosti) > 0){

                                foreach ($spr_vrednosti AS $vid => $vrednost) {

                                    if ($vrednost['other'] == 0) {
                                        
                                        $answer = null;
                                        
                                        # imamo signature vprašanje
                                        if($spr_signature == 1){
                                        
                                            if(isset($spr_data_text[$vid]['text']))
                                                $answer = $spr_data_text[$vid]['text'] . ' ';
                                            
                                            if(@getimagesize($site_url.'main/survey/uploads/'.$rowUser['usr_id'].'_'.$spr_id.'_'.$this->sid.'.png'))
                                                $answer .= '('.$site_url.'main/survey/uploads/'.$rowUser['usr_id'].'_'.$spr_id.'_'.$this->sid.'.png)';
                                        } 
                                        else if (isset($spr_data_text[$vid]['text']) && $spr_signature != 1) {
                                        
                                            if ($spr_upload == 1 || $spr_upload == 2) {
                                                # imamo upload vprašanje
                                                # imena datotek
                                                $filename = substr($this->get_array_data_text_upload($spr_data_text[$vid]['text']),strlen($uid.'_'));
                                                $answer = ''.$site_url.'main/survey/download.php?anketa='.$this->sid.'&code='.$spr_data_text[$vid]['text'].'';
                                            } 
                                            else {
                                                # imamo normalno text vprašanje
                                                $answer = $spr_data_text[$vid]['text'];
                                            }
                                                
                                        } 
                                        else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                        
                                            #$answer = end($spr_data_vred);
                                                
                                            $key = key($spr_data_vred);
                                            if (isset($spr_data_vred[$key])) {
                                                $_vred = $spr_data_vred[$key];
                                            }
                                            #pogledamo če je odgovor missing:
                                            if (isset($spr_vrednosti[$_vred])) {
                                                $answer = $spr_vrednosti[$_vred]['variable'];
                                            } else {
                                                $answer = $_vred;
                                            }
                                        }
            
                                        if ($answer == "" || $answer == null) {
                                            $answer = $VALUE_FOR_MISSING;
                                        } // end if
                                        
                                        $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);		
                                    }
                                }
                            }

                            break;
                            
	
						# Kalkulacija
						case 22:
	
							$_vred = null;
							$answer0 = null;
		
							# ce imamo odgovor
							if (isset ($spr_data_text[0]) && $spr_data_text[0]['text'] != '') {
								$answer0 = $spr_data_text[0]['text'];
							} else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
								$answer0 = end($spr_data_vred);
							}
		
							$answer0 = ($answer0 == null ) ? $VALUE_FOR_MISSING : $answer0;
							# dodamo odgovor
							$userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer0,$uid);
							
                        break;
                        
						
						# Kvota
						case 25:
	
							$_vred = null;
							$answer0 = null;
		
							# ce imamo odgovor
							if (isset ($spr_data_text[0]) && $spr_data_text[0]['text'] != '') {
								$answer0 = $spr_data_text[0]['text'];
							} else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
								$answer0 = end($spr_data_vred);
							}
		
							$answer0 = ($answer0 == null ) ? $VALUE_FOR_MISSING : $answer0;
							# dodamo odgovor
							$userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer0,$uid);
									
						    break;
    
                            
						# SN - IMENA *
						case 9:
							
							# zloopamo skozi podvprašanja in nastavimo izbrane odgovore
							$arrayVrednost = isset($this->SNVariablesForSpr[$_vprasanje_array['id']]) ? $this->SNVariablesForSpr[$_vprasanje_array['id']] : array();

							if(count($spr_vrednosti) > 0){
                                foreach ($spr_vrednosti AS $vid => $vrednost) {
                                    if (in_array($vid,$arrayVrednost)) {
                                        $answer = null;
                                        if (isset($spr_data_text[$vid]['text'])) {
                                            # imamo normalno text vprašanje
                                            $answer = $spr_data_text[$vid]['text'];
            
                                        } else if (is_array($spr_data_vred) && count($spr_data_vred) > 0 ) {
                                            $answer = end($spr_data_vred);
                                        }
            
                                        if ($answer == "" || $answer == null) {
                                            $answer = $VALUE_FOR_MISSING;
                                        } // end if
                                        $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);
                                        
                                    }
                                }	
							}	
							
                            break;
                            
                                                
						# LOKACIJA
                        case 26:
                            
                            $row = Cache::srv_spremenljivka($spr_id);

                            if($row['enota'] == 3){

                                if(count($spr_vrednosti) > 0){
                                    # zloopamo skozi podvprašanja in nastavimo izbrane odgovore
                                    if (isset ($spr_data_map)) {
                                        if(empty($spr_data_map)){
                                            //missingi, lahko da je -4
                                            $missing_value_temp = (is_array($spr_data_vred) && count($spr_data_vred) == 1 ) ? end($spr_data_vred) : $VALUE_FOR_MISSING;
                                            for($i = 0; $i < count($spr_vrednosti); $i++){
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$missing_value_temp,$uid);
                                            }
                                        }
                                        else{
                                            $answerArr = (count($spr_data_map['izpis']) > 0) ? $spr_data_map['izpis'] : $VALUE_FOR_MISSING;

                                            if(count($spr_data_map) > 0)
                                                foreach ($spr_data_map['izpis'] AS $izpis) {
                                                    $answer = null;

                                                    if (isset($izpis['vrednost'])) {
                                                            # imamo normalno text vprašanje
                                                            $answer = $izpis['vrednost'];
                                                    }
                                                    if ($answer == "" || $answer == null) {
                                                            $answer = $VALUE_FOR_MISSING;
                                                    } // end if

                                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answer,$uid);		
                                                }
                                        }
                                    }
                                }   
                            }
                            else{
                                $_vred = null;
                                $answerArr = null;
                                $answerAdd = null;
                                $answerVre = null;
                                $answerKoo = null;

                                $newParams = new enkaParameters($row['params']);
                                $is_podvprasanje = $newParams->get('marker_podvprasanje') == 1;
                                //$podvprasanje_naslov = '';
                                $is_podvprasanje ? $podvprasanje_naslov = $newParams->get('naslov_podvprasanja_map') : 
                                $podvprasanje_naslov = '';

                                $multi_input_type = $newParams->get('multi_input_type');

                                # ce imamo odgovor
                                if (isset ($spr_data_map)) {
                                    $missing_value_temp = (is_array($spr_data_vred) && count($spr_data_vred) == 1 ) ? end($spr_data_vred) : $VALUE_FOR_MISSING;
                                    $answerArr = (isset($spr_data_map['izpis']) && is_countable($spr_data_map['izpis']) && count($spr_data_map['izpis']) > 0) ? $spr_data_map['izpis'] : $missing_value_temp;
                                }
                                else if (is_array($spr_data_map)) {
                                    $key = key($spr_data_map);
                                    
                                    if (isset($spr_data_map[$key])) {
                                                    $_vred = $spr_data_map[$key];
                                    }

                                    // pogledamo če je odgovor missing:
                                    if (isset($spr_vrednosti[$_vred])) {
                                                    $answerAdd = $spr_vrednosti[$_vred]['variable'];
                                                    $answerVre = $spr_vrednosti[$_vred]['variable'];
                                                    $answerKoo = $spr_vrednosti[$_vred]['variable'];
                                    } else {
                                                    $answerAdd = $_vred;
                                                    $answerVre = $_vred;
                                                    $answerKoo = $_vred;
                                    }
                                }

                                if ($answerArr == null || $answerArr == $missing_value_temp){
                                        $answerAdd = $answerVre = $answerKoo = $missing_value_temp;
                                }
                                elseif($answerArr[0]['address'] == '-2'){
                                        $answerAdd = $answerVre = $answerKoo = '-2';
                                }
                                else{
                                    foreach($answerArr as $varArr){
                                        if($answerKoo != null){
                                                $answerAdd .= '<br>'.$varArr['address'];
                                                $answerVre .= '<br>'.$varArr['vrednost'];
                                                $answerKoo .= '<br>'.$varArr['koordinate'];
                                        }
                                        else{
                                                $answerAdd = $varArr['address'];
                                                $answerVre = $varArr['vrednost'];
                                                $answerKoo = $varArr['koordinate'];
                                        }
                                    }
                                }

                                # dodamo odgovor
                                if($multi_input_type == 'marker')
                                    $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerAdd,$uid);
                                if($is_podvprasanje)
                                        $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerVre,$uid);
                                
                                        $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerKoo,$uid);
                            }

							break;
                           

						// HEATMAP
						case 27:
								$_vred = null;
								$answerArr = null;
								$answerAdd = null;
								$answerVre = null;
								$answerKoo = null;
								
								$row = Cache::srv_spremenljivka($spr_id);
								$newParams = new enkaParameters($row['params']);

                                // ce imamo odgovor
								if (isset ($spr_data_heatmap['izpis'])) {
									$answerArr = (is_countable($spr_data_heatmap['izpis']) && count($spr_data_heatmap['izpis']) > 0) ? $spr_data_heatmap['izpis'] : $VALUE_FOR_MISSING;
                                } 
                                else if (is_array($spr_data_heatmap)) {
										$key = key($spr_data_heatmap);
										if (isset($spr_data_heatmap[$key])) {
												$_vred = $spr_data_heatmap[$key];
												$_vred = $spr_data_heatmap[$key];
										}
										#pogledamo če je odgovor missing:
										if (isset($spr_vrednosti[$_vred])) {
												$answerAdd = $spr_vrednosti[$_vred]['variable'];
												$answerVre = $spr_vrednosti[$_vred]['variable'];
												$answerKoo = $spr_vrednosti[$_vred]['variable'];
										} else {
												$answerAdd = $_vred;
												$answerVre = $_vred;
												$answerKoo = $_vred;
										}
								}

								if ($answerArr == null || $answerArr == $VALUE_FOR_MISSING){
									$answerAdd = $answerVre = $answerKoo = $VALUE_FOR_MISSING;
								}
								elseif($answerArr[0]['address'] == '-2'){
									$answerAdd = $answerVre = $answerKoo = '-2';
								}
								else{
									foreach($answerArr as $varArr){
										$answerAdd .= '<br>'.$varArr['address'];
										$answerVre .= '<br>'.$varArr['vrednost'];
										$answerKoo .= '<br>'.$varArr['koordinate'];
									}
								}
								
								# dodamo odgovor za koordinate
								$userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerKoo,$uid);

								// za območja
								if (is_array($spr_data_heatmap_regions)) {

									//priprava spremenljivk za koordinate točk
									$pointx = $this->preparePointCoords($answerKoo, 1, 0);
									$pointy = $this->preparePointCoords($answerKoo, 0, 1);
									$numberOfPointsInside=array();
																			
									if (count($spr_vrednosti) > 0) {																		
										$i=0;		
                                        if(isset($this->_array_data_heatmap_regions[(int)$spr_id]))										
                                            foreach ($this->_array_data_heatmap_regions[(int)$spr_id] AS $regions){
                                                $answerReg = null;
                                                $numberOfPointsInside[$regions['region_name']] = 0;     // Belezi stevilo tock znotraj trenutnega obmocja
                                                
                                                // Pretvori polje s tockami obmocja v ustrezno obliko
                                                $poly = $this->convertPolyString($regions['region_coords']); 
                                                
                                                // preveri, ali je posamezna tocka znotraj trenutnega obmocja
                                                for ($z=0; $z<sizeof($pointx); $z++){
                                                    $inside = $this->insidePoly($poly, $pointx[$z]["x"], $pointy[$z]["y"]);
                                                    if ($inside == true){
                                                        $numberOfPointsInside[$regions['region_name']]++;
                                                    }
                                                }
                                                
                                                // Priprava odgovora za preglednico s podatki
                                                // Ce je missing
                                                if($answerKoo < 0){
                                                    $answerReg = $answerKoo;
                                                }
                                                else{
                                                    $answerReg = $numberOfPointsInside[$regions['region_name']];	
                                                }
                                                
                                                $i++;
                                                
                                                // dodamo odgovor s stevilom tock znotraj obmocja
                                                $userAnswerLine .= STR_DLMT.$this->recode_answer($spr_id,$answerReg,$uid);
                                                
                                            }
									}
								}
                                //za območja - konec
                                
							break;
						}
					} // end while
				} // end if ($this->noErrors)


				// popravimo -1, -4 in -2d (text za drugo) => -3, začnemo odzadaj in spreminjamo -1, -4 in -2d v -3 dokler obstajajo ampak samo če status ni 6
                $changed = false;
				if ((int)$rowUser['status'] !== 6 && !empty($userAnswerLine)) {
					
					// ugotovimo do katere spremenljivke sploh zamenjujemo vrednosti ker če je pri tabelah odgovor vsaj na 1 variablo, potem tam pustimo -1
					$valuesToChange = $this->calculateValuesToChange($uid);
					$userAnswerLineArray = explode('|',$userAnswerLine);

					$reversed = array_reverse($userAnswerLineArray);
					foreach ($reversed AS $key => $value) {
						
						if ($key+1 > $valuesToChange) {
							break;
						}

						if ($value == -1 || $value == -4 || $value == '-2d') {
							$reversed[$key] = -3;
							$changed = true;
                        } 
                        else {
                            $reversed[$key] = $value;
						}
                    } 
                    
					if ($changed) {
						$userAnswerLineArray = array_reverse($reversed);
						$userAnswerLine = (implode('|', $userAnswerLineArray));
					}
				}
			
				// Popravimo -2d (text za drugo, ce je naknadno nastavljen na -2, ker ni bil oznacen radio/checkbox za drugo)
				$userAnswerLine = str_replace('-2d','-2',$userAnswerLine);
				
				// Naknadno popravimo se -2, ce je lurker (-5), ker nekatere drugace ostanejo
				if ($VALUE_FOR_MISSING == -5) {
					$userAnswerLine = str_replace('-2','-5',$userAnswerLine);
				}
				
				// dodamo podatke k userju
				$_dataLine .= $userAnswerLine;
				

				// DODAMO META PODATKE
				if ($this->noErrors) {
					
					// Random vrstni redi
					
					foreach ($this->_array_random as $parent_id => $random_parent) {

						$type = ($random_parent['type'] == 'spr') ? 'spr' : 'block';
						$random_data = $this->get_array_data_random($uid, $random_parent['id'], $type);
						unset($this->_array_data_random[$uid][$type][$random_parent['id']]);

						$_dataLine .= STR_DLMT.$random_data;
					}

					# če mamo da prepozna uporabnika iz sispleta
					if ((int)$this->survey['user_from_cms'] > 0) {
						$_dataLine .= STR_DLMT;
						if ((int)$rowUser['user_id'] > 0) {
							$_dataLine .= $this->get_user_CMS_email((int)$rowUser['user_id']);
						}
					}
		
					# datum insert, datum edit
					$_dataLine .= STR_DLMT.datetime($rowUser['time_insert']);
					$_dataLine .= STR_DLMT.datetime($rowUser['time_edit']);
		
					# strani in časi
					foreach ($this->_array_groups AS $gid => $grupa) {
		
						if (isset($this->_array_user_grupa[$uid][$gid]) && $this->_array_user_grupa[$uid][$gid] != '') {
							$_dataLine .= STR_DLMT.datetime($this->_array_user_grupa[$uid][$gid]);
						} 
                        else {
							$_dataLine .= STR_DLMT.'';
						}
					}
				
					/* ZARADI VAROVANJA PODATKOV SMO ZAČASNO ONEMOGOČILI LOVLJENJE IP-jev */
					$ip = SurveySetting::getInstance()->getSurveyMiscSetting('survey_ip');
					$ip_show = SurveySetting::getInstance()->getSurveyMiscSetting('survey_show_ip');
					if($ip==0 && $ip_show==1 && ($admin_type == 0 || $admin_type == 1)){
						$_dataLine .= STR_DLMT.$rowUser['ip'];
					}
					
					$_dataLine .= STR_DLMT.$rowUser['useragent'];
					$_dataLine .= STR_DLMT.$rowUser['browser'];
					$_dataLine .= STR_DLMT.$rowUser['os'];
					$_dataLine .= STR_DLMT.$lang['srv_para_graph_device'.$rowUser['device']];
					$_dataLine .= STR_DLMT.$rowUser['referer'];
					$_dataLine .= STR_DLMT.$rowUser['unsubscribed'];
					$_dataLine .= STR_DLMT.$jeziki[$rowUser['language']];
				}
                
                
				// Pocistimo vrednosti, ker drugace v nekaterih primereih prihaja do zelo cudnih bugov
				unset($rowVprasanje);
				unset($spr_vrednosti);
				unset($spr_gridi);
                
                
				// Zapišemo vsako vrstico posebej
				if ($this->noErrors && !empty($_dataLine)) {

					// zapišemo vrstico z predhodnim vrivom nove vrstice
					$success = fwrite($file_handler, $this->new_line_seperator . $_dataLine);
					fflush($file_handler);
                    
                    // Imamo napako - vse skupaj prekinemo
                    if ((int)$success <= 0) {
                        $this->noErrors = false;                    
                    } 
                    
					unset($_dataLine);
					$_dataLine = '';
                } 
                else {
					# dodamo v log napako
					$SL = new SurveyLog();
					$SL->addMessage(SurveyLog::ERROR, " ERROR user ".$rowUser['usr_id']." for ank_id".$this->sid);
					$SL->write();			
                }
                
                $this->new_line_seperator = NEW_LINE;
                
			} // end while user loop
        } // end if ($this->noErrors)
    }

    /***** KONEC - ZBIRANJE PODATKOV ZA DATOTEKO *****/
    


    /***** KESIRANJE PODATKOV ANKETE (vprasanja, strani, gridi...) *****/

    // Glavna funkcija preko katere na zacetku izvedemo vso kesiranje
    private function cache_data(){

        // Zakesiramo query za respondente
        $this->create_qry_users();


        // Zakesiramo strani v anketi
        $this->get_groups();

        // Zakesiramo vsa vprasanja v anketi
        $this->get_vprasanja();   

        // Randomizacija v blokih in vprasanjih
        $this->get_random();
                    
        // Za vrednosti ankete
        $this->get_vrednosti();
        
        // Za gride ankete
        $this->get_gridi();		   

        // Skreira array z polji za spss 
        $this->get_SPSS();


        // Polovimo missing vrednosti ce so nastavljene drugače kot privzeto
        // TODO če uporabnik na anketi spremeni da naj bo -1 -6 se to ne upošteva.
        $this->setSurveyMissingValues();
    }

    // Naredimo query za respondente (srv_users)
	private function create_qry_users() {
			
		if ($this->_qry_users === null ) {
            
            if ((int)$this->data_file_time > 0 && file_exists($this->folder . 'export_data_'.$this->sid.'.dat')) {
				# delamo inkremental
				$inkremental_user_limit = " AND u.time_edit > FROM_UNIXTIME('".(int)$this->data_file_time."') ";
            } 
            else {
				# lovimo vse userje - datoteko generiramo na novo
				$inkremental_user_limit = '';
			}

			$_qry_cnt = sisplet_query("SELECT count(*) FROM srv_user AS u WHERE u.ank_id = '".$this->sid."' AND u.preview='0' AND u.deleted='0' ".$inkremental_user_limit .$this->is_valid_user_limit);
			$_allUsers_count = mysqli_fetch_row($_qry_cnt);

			$this->_cnt_all_users = 0;
			
            // naredimo array querijev za userje, limitirano po max vrednosti userjev na loop (MAX_USER_PER_LOOP)
            $c = 0;
			do {
                // Naredimo query
                $this->_qry_users[$c] = sisplet_query("SELECT u.id AS usr_id, u.cookie, u.recnum, u.last_status as status, u.pass, u.testdata, u.lurker, u.unsubscribed, UNIX_TIMESTAMP(u.time_insert) AS unx_ins_date, UNIX_TIMESTAMP(u.time_edit) AS unx_edt_date, u.user_id, u.inv_res_id, u.time_insert, u.time_edit, u.ip, REPLACE(u.useragent,'|',' ') as useragent, u.browser, u.os, u.device, REPLACE(u.referer,'|',' ') as referer, language 
                                                        FROM srv_user AS u 
                                                        WHERE u.ank_id = '".$this->sid."' AND u.preview='0' AND u.deleted='0' ".$inkremental_user_limit . $this->is_valid_user_limit." 
                                                        ORDER BY u.id ASC
                                                        LIMIT ".($c * MAX_USER_PER_LOOP).",".MAX_USER_PER_LOOP."");

				if (!$this->_qry_users[$c]) {
					$this->trigerError('create_qry_users', mysqli_error($GLOBALS['connect_db']));
				}

				// Naredimo še string z id userjev za večkratno uporabo
				$str = '';
				if (mysqli_num_rows($this->_qry_users[$c]) > 0) {
					$prefix ='';
					while ($row = mysqli_fetch_assoc($this->_qry_users[$c])) {
						$str .= $prefix.$row['usr_id'];
						$prefix = ',';
					}
					$this->_str_users[$c] = ' AND usr_id IN ('.$str.') ';
				}
				$this->_cnt_all_users += mysqli_num_rows($this->_qry_users[$c]);
	
				$c ++;
            } 
            while ($c * MAX_USER_PER_LOOP <= $_allUsers_count['0']);
		}
    }
    
    // Zakesiramo strani v anketi (srv_grupa)
	private function get_groups() {

		if ($this->_cnt_groups == 0) {
				
			$qry_groups = sisplet_query("SELECT id FROM srv_grupa WHERE ank_id='".$this->sid."' ORDER BY vrstni_red");
			if (!$qry_groups) {
				$this->trigerError('get_groups',  mysqli_error($GLOBALS['connect_db']));
			}

			// koliko strani imamo v anketi
			$this->_cnt_groups = mysqli_num_rows($qry_groups);

			// naredimo še string z id grupami za večkratno uporabo
			$str = '';
            $cnt = 0;
			if ($this->_cnt_groups > 0) {
				$this->_array_groups = array();

                // Uvod - ce je prikazan v anketi 
                if($this->survey['show_intro'] == '1'){
                    $this->_array_groups['0'] = '0';
                    $str .= '0,';
                }

				$prefix ='';
                
                while ($row = mysqli_fetch_assoc($qry_groups)) {
					$cnt++;
						
					$this->_array_groups[$row['id']] = $row['id'];
					$str .= $prefix.$row['id'];
					$prefix = ',';
                }
                
				$this->_str_groups = $str;
			}
        }
    }

    // Zakesiramo vsa vprasanja v anketi
    private function get_vprasanja(){

        // Delamo samo 1x
		if ( $this->AllQuestionsData !== null &&  $this->AllQuestionsOrder != null) {
			return array('AllQuestionsData'=>$this->AllQuestionsData, 'AllQuestionsOrder'=>$this->AllQuestionsOrder);
        }
        

        // Po novem lovimo tudi sistemske pa jih odstranimo pri prikazu in izvozih
        $_qry_questions = sisplet_query("SELECT s.id, s.tip, s.variable, REPLACE(REPLACE(REPLACE(s.naslov,'\n',' '),'\r','<br>'),'|','<br>') as naslov, s.label, s.gru_id, s.gru_id, s.random, s.size, s.cela, s.decimalna, s.skala, s.enota, s.sistem, s.upload, s.signature, s.grid_subtitle1, s.grid_subtitle2, s.inline_edit, REPLACE(REPLACE(REPLACE(s.naslov_graf,'\n',' '),'\r','<br>'),'|','<br>') as naslov_graf, s.edit_graf, s.wide_graf, antonucci, s.visible, s.dostop 
                                                FROM srv_spremenljivka AS s, srv_grupa AS g 
                                                WHERE s.gru_id = g.id AND g.ank_id = '".$this->sid."' AND s.tip != '5' 
                                                ORDER BY g.vrstni_red, s.vrstni_red");
        if (!$_qry_questions) {
            $this->trigerError('get_vprasanja',  mysqli_error($GLOBALS['connect_db']));
            return false;
        }

        // Prestejemo vprasanja v anketi
        $this->_cnt_questions = mysqli_num_rows($_qry_questions);

        // Ce nimamo vprasanj v anketi ne gremo naprej
        if ($this->_cnt_questions == 0) {
            $this->trigerError('get_vprasanja',  'Ni vprasanj v anketi');
            return false;
        }


        // Za SN - imena (modul social network)
		if(SurveyInfo::getInstance()->checkSurveyModule('social_network')){

            $_result = array();

            while ($rowVprasanje = mysqli_fetch_assoc($_qry_questions)) {
                if($rowVprasanje['tip'] == 9) {
                    $_result[] = $rowVprasanje['id'];
                }
            }

            if (count($_result) > 0) {
                foreach ($_result AS $spr_id) {
                    $qry = sisplet_query("SELECT vre_id FROM srv_data_text".$this->db_table." WHERE spr_id = '".$spr_id."' GROUP BY vre_id");
                    while ( $row = mysqli_fetch_assoc($qry)) {
                        if ( $row['vre_id'] > 0 ) {
                            $this->SNVariablesForSpr[$spr_id][$row['vre_id']] = $row['vre_id'];
                        }
                    }
                }
            }

            mysqli_data_seek($_qry_questions, 0);
        }


        // Polovimo spremenljivke za survey Grid Multiple - kombinirano tabelo
		$sgmMap = array();
		$sqlSgmMap = sisplet_query("SELECT sgm.parent AS sgm_parrent, s.id, s.tip, s.variable, REPLACE(REPLACE(REPLACE(s.naslov,'\n',' '),'\r','<br>'),'|','<br>') as naslov, s.gru_id, s.size, s.random, s.cela, s.decimalna, s.skala, s.enota, s.sistem, s.upload, s.signature, s.grid_subtitle1, s.grid_subtitle2, s.inline_edit, REPLACE(REPLACE(REPLACE(s.naslov_graf,'\n',' '),'\r','<br>'),'|','<br>') as naslov_graf, s.edit_graf, s.wide_graf, s.antonucci, s.visible, s.dostop 
                                        FROM srv_spremenljivka AS s JOIN srv_grid_multiple AS sgm ON (s.id = sgm.spr_id) 
                                        WHERE sgm.ank_id = '$this->sid' 
                                        ORDER BY sgm.vrstni_red");
		while ($sgmRow = mysqli_fetch_assoc($sqlSgmMap)) {
			$sgmMap[$sgmRow['sgm_parrent']][] = $sgmRow;
        }
        

		// Preverimo ali so bili skreirani loopi
		if ($this->_array_loop_on_spr == null || $this->_array_spr_in_loop == null) {
			$this->get_loops();
		}


        // Pripravimo array-e za kesiranje vprasanj
		$this->AllQuestionsData = array();
		$this->AllQuestionsOrder = array();
        $star_loop_id = null;	
        
        // Spremenljivka za kreacijo stringa z id spremenljivkami za večkratno uporabo
        $sprIds = array();
        $cnt = 0;

        // Loop po vseh vprasanjih v anketi
        while ($rowMainVprasanje = mysqli_fetch_assoc($_qry_questions)) {

            // Naredimo array z id spremenljivkami za večkratno uporabo - za kombinirano tabelo posebej dodamo se podtabele
            if ($rowMainVprasanje['tip'] != 24) {
                $sprIds[] = $rowMainVprasanje['id'];
            } 
            else {
                $sqlSub = sisplet_query("SELECT s.id FROM srv_spremenljivka AS s JOIN srv_grid_multiple AS sgm ON (s.id = sgm.spr_id) 
                                            WHERE sgm.parent = '".$rowMainVprasanje['id']."' 
                                            ORDER BY sgm.vrstni_red");
                while (list($subSprId) = mysqli_fetch_row($sqlSub)) {
                    $sprIds[] = $subSprId;
                }
            }


            $rowVprasanja = array();
            
            if (!isset($sgmMap[$rowMainVprasanje['id']]) || count($sgmMap[$rowMainVprasanje['id']]) == 0) {
                $rowVprasanja[] = $rowMainVprasanje;
            } 
            else {
                // preverimo parent tip. mora biti 24
                if ((int)$rowMainVprasanje['tip'] == 24){
                    
                    // imamo kombinirano vprašanje
                    foreach ($sgmMap[$rowMainVprasanje['id']] AS $sgmRow) {
                        $tmpVprasanje = $sgmRow;

                        // popravimo variablo in besedilo
                        $tmpVprasanje['sgm_parrent'] = $rowMainVprasanje['id'];
                        $tmpVprasanje['variable'] = $rowMainVprasanje['variable'].'_'.$sgmRow['variable'];
                        $tmpVprasanje['naslov'] = $rowMainVprasanje['naslov'];
                        $rowVprasanja[] = $tmpVprasanje;
                    }
                }
                // če ne ni kombinirana tabela
                else { 
                    $rowVprasanja[] = $rowMainVprasanje;
                }            
            }

            if (count($rowVprasanja) > 0) {

                foreach ($rowVprasanja AS $rowVprasanje) {

                    $spr_id = $rowVprasanje['id'];

                    $this->AllQuestionsData[$rowVprasanje['id']] = array(
                            'id' 		    => $rowVprasanje['id'],	
                            'spr_id'	    => $spr_id,	
                            'sgm_parrent'   => isset($rowVprasanje['sgm_parrent']) ? $rowVprasanje['sgm_parrent'] : '',
                            'tip'		    => $rowVprasanje['tip'],
                            'gru_id'	    => $rowVprasanje['gru_id'],
                            'variable'  	=> strip_tags($rowVprasanje['variable']),
                            'naslov' 	    => strip_tags($rowVprasanje['naslov']),
                            'label' 	    => (isset($rowVprasanje['label']) ? strip_tags($rowVprasanje['label']) : ''),
                            'size' 		    => $rowVprasanje['size'],
                            'cela' 		    => $rowVprasanje['cela'],
                            'grid_subtitle1' => $rowVprasanje['grid_subtitle1'],
                            'grid_subtitle2' => $rowVprasanje['grid_subtitle2'],
                            'decimalna'	    => $rowVprasanje['decimalna'],
                            'skala' 	    => $rowVprasanje['skala'],
                            'sistem'	    => $rowVprasanje['sistem'],
                            'enota'		    => $rowVprasanje['enota'],
                            'if_id'		    => (isset($this->_array_spr_in_loop[$spr_id]) ? $this->_array_spr_in_loop[$spr_id] : 0),
                            'upload'	    => $rowVprasanje['upload'],
                            'signature'	    => $rowVprasanje['signature'],
                            'naslov_graf'	=> $rowVprasanje['naslov_graf'],
                            'edit_graf'	    => $rowVprasanje['edit_graf'],
                            'wide_graf'     => $rowVprasanje['wide_graf'],
                            'antonucci'     => $rowVprasanje['antonucci'],
                            'visible'       => $rowVprasanje['visible'],
                            'dostop'        => $rowVprasanje['dostop'],
                            'random'        => $rowVprasanje['random']
                    );

                    if ($rowVprasanje['inline_edit'] != 0) { 
                        $this->AllQuestionsData[$rowVprasanje['id']]['inline_edit'] = $rowVprasanje['inline_edit'];
                    }
                    
                    // Zabelezimo stevilo vprasanj po tipu
                    $this->_cnt_questions_types[$rowVprasanje['tip']] = (isset($this->_cnt_questions_types[$rowVprasanje['tip']])) ? $this->_cnt_questions_types[$rowVprasanje['tip']]+1 : 1;
                    
                    // če je spremenljivka parent od loopa jo dodamo
                    if (isset($this->_array_loop_parent[$spr_id]) && $this->_array_loop_parent[$spr_id] > 0) {
                        $this->AllQuestionsData[$rowVprasanje['id']]['loop_parent'] = $this->_array_loop_parent[$spr_id];
                        $this->AllQuestionsData[$rowVprasanje['id']]['antonucci'] = $rowVprasanje['antonucci'];
                    }

                    // dodamo vprašanje v vrstni red, če je vprašanje v loopu dodamo loop
                    if (!isset($this->_array_spr_in_loop[$spr_id])) {

                        // vprašanje ni v loopu
                        $this->AllQuestionsOrder[] = array('id'=>$spr_id, 'loop_id'=>0);

	                    // nismo v loopu
                        $star_loop_id = null;
                    } 
                    else {
                        // vprašanje je v lopu
                        $trenutni_loop_id = $this->_array_spr_in_loop[$spr_id]; # id loopa v katerem se nahaja spremenljivk
                        
                        // na katero spremenljivko se dela loop
                        $loop_on_spr = $this->_array_loop_on_spr[$trenutni_loop_id];
    
                        // če ta loop in pripadajoča vprašanja še niso bila dodana v vrstni red, jih dodamoše ni bil
                        if ($trenutni_loop_id != $star_loop_id) {
    
                            // loop še ni bil sprocesiran;
                            $_vrednosti_v_loopu = $this->_array_vre_on_loop[$trenutni_loop_id];
    
                            // preverimo ali smo v navadnem loopu ali v SN loopu
                            if (isset($this->SNVariablesForSpr[$loop_on_spr]) == false) {

                                // smo v navadnem loopu
                                // kolikokrat moramo iti skozi loop
                                if (count($_vrednosti_v_loopu)) {

                                    foreach ($_vrednosti_v_loopu AS $li_id => $vrednost) {

                                        // dodamo vprašanja ki so v posameznemm loopu.
                                        if (count($this->_array_loop_has_spr[$trenutni_loop_id])) {
                                            foreach($this->_array_loop_has_spr[$trenutni_loop_id] AS $vprasanje) {
                                                $this->AllQuestionsOrder[] = array('id'=>$vprasanje,'loop_id'=>$li_id,'vre_id'=>$vrednost, 'parent_loop_id'=>$trenutni_loop_id);
                                            }
                                        }
                                    }
                                }
                            } 
                            else {
                                // smo v SN loopu - skozi loop gremo samo za vrednosti ki so dodane kot sn imena
                                if (count($_vrednosti_v_loopu)) {
                                    foreach ($_vrednosti_v_loopu AS $li_id => $vrednost) {

                                        if (in_array($vrednost,$this->SNVariablesForSpr[$loop_on_spr])) {

                                            // dodamo vprašanja ki so v posameznemm loopu.
                                            if (count($this->_array_loop_has_spr[$trenutni_loop_id])) {
                                                foreach($this->_array_loop_has_spr[$trenutni_loop_id] AS $vprasanje) {
                                                    $this->AllQuestionsOrder[] = array('id'=>$vprasanje,'loop_id'=>$li_id,'vre_id'=>$vrednost, 'parent_loop_id'=>$trenutni_loop_id);
                                                }
                                            }
                                        }
                                    }
                                }
    
                            }
    
                            # priredimo kateri loop smo že obdelali, da ne bomo 2x
                            $star_loop_id = $trenutni_loop_id;
                        }
                    }
                }
            }
        }

        // Naredimo string z id spremenljivkami za večkratno uporabo
        if (count($sprIds) > 0) {
            $this->_str_questions = implode(',', $sprIds);
        }

		return array('AllQuestionsData'=>$this->AllQuestionsData, 'AllQuestionsOrder'=>$this->AllQuestionsOrder);
    }

    // Polovimo podatke za loop ce je kaksen v anketi
    private function get_loops() {
			
        // na katero spremenljivko se veže loop
        $this->_array_loop_on_spr = array();
        $this->_array_loop_parent = array();
        
        $_cnt_loop = 0;

        $qryLoop_spr_select = sisplet_query("SELECT l.if_id, l.spr_id 
                                                FROM srv_branching AS b, srv_loop AS l 
                                                WHERE b.ank_id = '".$this->sid."' AND b.element_if = l.if_id");
        while (list($loop_id,$spr_id) = mysqli_fetch_row($qryLoop_spr_select)) {
            $this->_array_loop_parent[$spr_id] = $loop_id;
            $this->_array_loop_on_spr[$loop_id] = $spr_id;

            $_cnt_loop++;
        }
    
        // katere vrednosti uporablja posamezen loop ( kolikokrat se loop zavrti)
        $this->_array_vre_on_loop = array();
        $this->_array_vrednosti_in_loops = array();

        $qry_vre_on_loop_select = sisplet_query("SELECT ld.id, ld.if_id, ld.vre_id 
                                                    FROM srv_loop_data AS ld 
                                                    WHERE ld.if_id IN 
                                                        (SELECT l.if_id 
                                                            FROM srv_branching AS b, srv_loop AS l 
                                                            WHERE b.ank_id = '".$this->sid."' AND b.element_if = l.if_id) 
                                                    ORDER BY if_id, ld.vre_id, ld.id");
        while (list($id, $loop_id,$vre_id) = mysqli_fetch_row($qry_vre_on_loop_select)) {
            
            // če imamp SN -imena in je loop na SN - imena ne loopamo po vseh vrednostih, ampak samo po max vnosih
            $this->_array_vre_on_loop[$loop_id][$id] = $vre_id;
            $this->_array_vrednosti_in_loops[] = $vre_id;
            
            // Posebej nastavimo ce gre za loop po numeric vprasanju
            if($vre_id == null){
                $this->_array_vre_on_loop[$loop_id][$id] = 'num_loop';
                $this->_array_vrednosti_in_loops[] = 'num_loop';
            }
        }

        // preverimo katere spremenljvke so v katerem loopu
        $this->_array_spr_in_loop = array();
        $this->_array_loop_has_spr = array();
        $b = new Branching ($this->sid);
        $_loops = $b->spremenljivke_in_loop();
            
        if (count($_loops) > 0 ) {
            
            foreach ($_loops AS $lkey => $spr_ids) {
    
                if (count($spr_ids) > 0 ) {
                    $this->_array_loop_has_spr[$lkey] = array();
                    
                    foreach ($spr_ids AS $spr_id) {
                        $this->_array_spr_in_loop[$spr_id] = $lkey;
                        $this->_array_loop_has_spr[$lkey][] = $spr_id;

                        // Pri kombinirani tabeli dodamo v array se podtabele
                        $row = Cache::srv_spremenljivka($spr_id);
                        if($row['tip'] == 24){

                            $sqlMT =  sisplet_query("SELECT s.id
                                                        FROM srv_spremenljivka s, srv_grid_multiple mt
                                                        WHERE mt.ank_id='".$this->sid."' AND mt.parent='".$spr_id."' AND mt.spr_id=s.id
                                                        ORDER BY mt.vrstni_red ASC
                                                    ");                        
                            while ($rowMT = mysqli_fetch_array($sqlMT)) {
                                $this->_array_spr_in_loop[$rowMT['id']] = $lkey;
                                $this->_array_loop_has_spr[$lkey][] = $rowMT['id'];
                            }
                        }

                    }
                }
            }
        }
    }
    
    // Zakesiramo vrednosti vprasanj v anketi
	private function get_vrednosti($spid=null) {

        // Ce ze imamo podatek in iscemo za posamezno spremenljivko
        if ($spid != null && (int)$spid > 0 && isset($this->_array_vrednosti[$spid])) {		
		    return $this->_array_vrednosti[$spid];
        } 

        // Ce ze imamo pripravljen cel array
        if ($spid == null && $this->_array_vrednosti !== null) {
            return $this->_array_vrednosti;
        }
        

        if ($this->_str_questions != '') {
					
            $_qry_vrednosti = sisplet_query("SELECT id, spr_id, REPLACE(REPLACE(REPLACE(naslov,'\n',' '),'\r','<br>'),'|',' ') as naslov, REPLACE(REPLACE(REPLACE(naslov2,'\n',' '),'\r','<br>'),'|',' ') as naslov2, variable, vrstni_red, other, REPLACE(REPLACE(REPLACE(naslov_graf,'\n',' '),'\r','<br>'),'|',' ') as naslov_graf 
                                                FROM srv_vrednost 
                                                WHERE spr_id IN (".$this->_str_questions.") 
                                                ORDER BY spr_id, vrstni_red"); 
            if (!$_qry_vrednosti) {
                $this->trigerError('get_vrednosti',  mysqli_error($GLOBALS['connect_db']));
            }
            
            $_cnt_vrednosti = mysqli_num_rows($_qry_vrednosti);
        
            if ($_cnt_vrednosti > 0) {

                while (list($id, $spr_id, $naslov, $naslov2, $variable, $vrstni_red, $other, $naslov_graf) = mysqli_fetch_row($_qry_vrednosti)) {

                    $this->_array_vrednosti[(int)$spr_id][(int)$id] = array(
                        'id'            => (int)$id, 
                        'naslov'        => strip_tags($naslov), 
                        'naslov2'       => strip_tags($naslov2), 
                        'variable'      => $variable, 
                        'vrstni_red'    => (int)$vrstni_red, 
                        'other'         => (int)$other, 
                        'naslov_graf'   => strip_tags($naslov_graf) 
                    );
                }
            }
        }

        
		if ($spid != null && (int)$spid > 0) {
			return $this->_array_vrednosti[$spid];
        } 
        else {
			return $this->_array_vrednosti;
		}
    }
    
	// Zakesiramo gride tabel vprasanj v anketi
	private function get_gridi($grid=null) {

        // Ce ze imamo podatek in iscemo za posamezno spremenljivko
		if ($grid != null && (int)$grid > 0 && isset($this->_array_gridi[$grid])) {
		    return $this->_array_gridi[$grid];
        } 

        // Vrenmo celoten array
        if ($grid == null && $this->_array_gridi !== null) {
            return $this->_array_gridi;
		}


        if ($this->_str_questions != '') {

            $_qry_gridi = sisplet_query("SELECT id, spr_id, REPLACE(REPLACE(REPLACE(naslov,'\n',' '),'\r','<br>'),'|',' ') as naslov, variable, other, part, REPLACE(REPLACE(REPLACE(naslov_graf,'\n',' '),'\r','<br>'),'|',' ') as naslov_graf, vrstni_red 
                                            FROM srv_grid 
                                            WHERE spr_id IN (".$this->_str_questions.") 
                                            ORDER BY spr_id, vrstni_red");
            
            if (!$_qry_gridi) {
                $this->trigerError('get_gridi',  mysqli_error($GLOBALS['connect_db']));
            }

            $_cnt_gridi = mysqli_num_rows($_qry_gridi);

            if ($_cnt_gridi > 0) {

                while (list($id, $spr_id, $naslov, $variable, $other, $part, $naslov_graf) = mysqli_fetch_row($_qry_gridi)) {

                    $this->_array_gridi[(int)$spr_id][(int)$id] = array(
                        'id'            => (int)$id, 
                        'naslov'        => strip_tags($naslov), 
                        'variable'      => $variable, 
                        'other'         => (int)$other, 
                        'part'          => (int)$part, 
                        'naslov_graf'   => strip_tags($naslov_graf)
                    );
                }
            }
        }
     
        
        if ($grid != null && (int)$grid > 0 ) {
			return $this->_array_gridi[$grid];
        } 
        else {
			return $this->_array_gridi;
		}
    }
    
    // Zakesiramo podatke za SPSS
    private function get_SPSS() {
        
        if ($this->_array_SPSS === null && $this->_str_questions != '') {
            
            $this->_array_SPSS = array();
            
            $_qry_SPSS = sisplet_query("SELECT dt.spr_id, MAX(LENGTH(dt.text)) AS length, MAX(LENGTH(dt.text2)) AS length2 
                                            FROM srv_data_text".$this->db_table." dt, srv_grupa g, srv_spremenljivka s 
                                            WHERE dt.spr_id = s.id AND s.gru_id=g.id AND g.ank_id=".$this->sid." 
                                            GROUP BY dt.spr_id");
            while (list($spr_id, $text, $text2) = mysqli_fetch_row($_qry_SPSS)) {
                $this->_array_SPSS[$spr_id] = array('text'=>(int)$text, 'text2'=>(int)$text2);
            }
    
            $_qry_SPSS1 = sisplet_query("SELECT spr_id, max(LENGTH(text)) AS length 
                                            FROM srv_data_textgrid".$this->db_table." 
                                            WHERE spr_id IN (".$this->_str_questions.") 
                                            GROUP BY spr_id");
            while (list($spr_id, $text) = mysqli_fetch_row($_qry_SPSS1)) {
                $this->_array_SPSS[$spr_id]['text2'] = $text;
            }

            // polovimo še max vrednosti za variable
            $tmp_qry_SPSS = sisplet_query("SELECT spr_id, max(LENGTH(vrstni_red)) AS length 
                                            FROM srv_vrednost 
                                            WHERE spr_id IN (".$this->_str_questions.") 
                                            GROUP BY spr_id 
                                            ORDER BY spr_id, vrstni_red");
            while (list($spr_id, $vrstni_red) = mysqli_fetch_row($tmp_qry_SPSS)) {
                $this->_array_SPSS[$spr_id]['vrednost'] = $vrstni_red;
            }
        }
        
		return $this->_array_SPSS;
	}

    // Zakesiramo vse bloke in vprasanja, ki imajo vklopljeno randomizacijo
	private function get_random () {
		
		// ce smo ze zakesirali vrednosti
		if ($this->_array_random != null) {
			return $this->_array_random;
		}

		$this->_array_random = array();
        $i=0;
        
		// Preberemo vsa VPRASANJA ki imajo vklopljeno randomizacijo
		$_qry_random_spr = sisplet_query("SELECT s.id, s.variable FROM srv_spremenljivka s, srv_grupa g 
                                            WHERE g.ank_id='".$this->sid."' AND s.gru_id=g.id AND s.random='1' 
                                            ORDER BY g.vrstni_red ASC, s.vrstni_red ASC");
		if (!$_qry_random_spr) {
			$this->trigerError('get_random', mysqli_error($GLOBALS['connect_db']));
		}

		# koliko zapicov
		$cnt_rows = mysqli_num_rows($_qry_random_spr);

		// Napolnimo array z randomiziranimi vprasanji
		if ($cnt_rows > 0) {

            
			while ($row = mysqli_fetch_array($_qry_random_spr)) {
                $this->_array_random[$row['id']]['id'] = $row['id'];
				$this->_array_random[$row['id']]['variable'] = $row['variable'];
				$this->_array_random[$row['id']]['type'] = 'spr';

                $i++;
			}
		}


		// Preberemo vse BLOKE ki imajo vklopljeno randomizacijo
		$_qry_random_blok = sisplet_query("SELECT i.id, i.random, i.label, i.number 
                                            FROM srv_if i, srv_branching b 
                                            WHERE b.ank_id='".$this->sid."' AND b.element_if=i.id AND (i.random>=0 OR i.random=-2) AND i.tip='1'");
		if (!$_qry_random_blok) {
			$this->trigerError('get_random',  mysqli_error($GLOBALS['connect_db']));
		}

		# koliko zapicov
		$cnt_rows = mysqli_num_rows($_qry_random_blok);

		// Napolnimo array z randomiziranimi bloki
		if ($cnt_rows > 0) {
            
            $prefix = '';

			while ($row = mysqli_fetch_array($_qry_random_blok)) {
					
				$this->_array_random[$row['id']]['id'] = $row['id'];
				$this->_array_random[$row['id']]['variable'] = $row['label'];
                $this->_array_random[$row['id']]['number'] = $row['number'];
                
                // Napolnimo string z id-ji blokov
                $this->_str_blocks .= $prefix.$row['id'];
                $prefix = ',';
		
				// Blok ima randomizirane bloke
				if($row['random'] == -2){
					$this->_array_random[$row['id']]['type'] = 'blok_blok';
				}
				// Blok ima randomizirana vprasanja
				else{
					$this->_array_random[$row['id']]['type'] = 'blok_spr';
				}				
                
                $i++;
			}
		}
    }

    /***** KONEC - KESIRANJE PODATKOV ANKETE (vprasanja, strani, gridi...) *****/
    


    /***** KESIRANJE ODGOVOROV POSAMEZNEGA RESPONDENTA *****/

    // Zakesiramo odgovore respondenta
    private function cache_data_respondent($string_user){

        if ($this->_str_questions != '') {

            // resetiramo POINTER-je - Array-je do podatkov
            $this->_array_user_grupa = null;
            $this->_array_data_vrednost = null;
            $this->_array_data_text = null;
            $this->_array_data_grids = null;
            $this->_array_data_check_grids = null;
            $this->_array_data_text_grid = null;
            $this->_array_data_rating = null;
            $this->_array_data_text_upload = null;
            $this->_array_data_map = null;
            $this->_array_data_heatmap = null;
            $this->_array_data_heatmap_regions = null;
            $this->_array_data_vrednost_cond = null;
            $this->_array_data_random = null;


            // polovimo id-je strani do katerih je prišel posamezen uporabnik
            $this->get_user_grupa($string_user);


            // Popravimo string za omejevanje userjev
            $_string_user = str_replace(array(' AND usr_id IN (', ')'), '', $string_user);
            $_string_user = explode(',', $_string_user);
            $string_user = ' AND usr_id BETWEEN '.$_string_user[0].' AND '.end($_string_user);

            // Zakesiramo vse podatke - ce ni nobenega vprasanja tega tipa v anketi, ne rabimo tega izvajat
            $this->cache_data_respondent_vrednost($string_user);
            $this->cache_data_respondent_text($string_user);

            // Kesiranje podatkov za srv_data_grid
            if((isset($this->_cnt_questions_types['6']) && $this->_cnt_questions_types['6'] > 0) 
                || (isset($this->_cnt_questions_types['16']) && $this->_cnt_questions_types['16'] > 0)
                || (isset($this->_cnt_questions_types['19']) && $this->_cnt_questions_types['19'] > 0)
                || (isset($this->_cnt_questions_types['20']) && $this->_cnt_questions_types['20'] > 0)
                || (isset($this->_cnt_questions_types['24']) && $this->_cnt_questions_types['24'] > 0)
            )
                $this->cache_data_respondent_grid($string_user);

            // Kesiranje podatkov za srv_data_checkgrid
            if((isset($this->_cnt_questions_types['6']) && $this->_cnt_questions_types['6'] > 0) 
                || (isset($this->_cnt_questions_types['16']) && $this->_cnt_questions_types['16'] > 0)
                || (isset($this->_cnt_questions_types['24']) && $this->_cnt_questions_types['24'] > 0)
            )
                $this->cache_data_respondent_check_grid($string_user);

            // Kesiranje podatkov za srv_data_textgrid
            if((isset($this->_cnt_questions_types['19']) && $this->_cnt_questions_types['19'] > 0) 
                || (isset($this->_cnt_questions_types['20']) && $this->_cnt_questions_types['20'] > 0)
                || (isset($this->_cnt_questions_types['24']) && $this->_cnt_questions_types['24'] > 0)
            )
                $this->cache_data_respondent_text_grid($string_user);

            // Kesiranje podatkov za srv_data_rating
            if(isset($this->_cnt_questions_types['17']) && $this->_cnt_questions_types['17'] > 0)
                $this->cache_data_respondent_rating($string_user);

            // Kesiranje podatkov za srv_data_text_upload
            if(isset($this->_cnt_questions_types['21']) && $this->_cnt_questions_types['21'] > 0)
                $this->cache_data_respondent_text_upload($string_user);

            // Kesiranje podatkov za srv_data_map
            if(isset($this->_cnt_questions_types['26']) && $this->_cnt_questions_types['26'] > 0)          
                $this->cache_data_respondent_map($string_user);

            // Kesiranje podatkov za srv_data_heatmap in srv_data_heatmap_regions
            if(isset($this->_cnt_questions_types['27']) && $this->_cnt_questions_types['27'] > 0){
                $this->cache_data_respondent_heatmap($string_user);
                $this->cache_data_respondent_heatmap_regions($string_user);
            }
   
            // Kesiranje podatkov za srv_data_vrednost_cond
            if((isset($this->_cnt_questions_types['2']) && $this->_cnt_questions_types['2'] > 0) 
                || (isset($this->_cnt_questions_types['27']) && $this->_cnt_questions_types['27'] > 0)
            )
                $this->cache_data_respondent_vrednost_cond($string_user);

            // Kesiranje podatkov za randomizirane vrednosti oz. randomizirana vprasanja v blokih
            $this->cache_data_respondent_random($string_user);
        }
    }

    // Zakesiramo case odgovorov po straneh za respondenta
	private function get_user_grupa($string_user) {

		if ($this->_str_groups != '') {

			$qry = sisplet_query("SELECT gru_id, usr_id, time_edit, preskocena FROM srv_user_grupa".$this->db_table." WHERE gru_id IN (".$this->_str_groups.") ".$string_user."");
			if (!$qry) {
				$this->trigerError('get_user_grupa',  mysqli_error($GLOBALS['connect_db']));
			}

            $this->_array_user_grupa = array();
            
			while ($row = mysqli_fetch_assoc($qry)) {
				$this->_array_user_grupa[(int)$row['usr_id']][(int)$row['gru_id']] = $row['time_edit'];
			}
		}
			
    }

    // Zakesiramo podatke tabele srv_data_vrednost
    private function cache_data_respondent_vrednost($string_user){

        if ($this->_array_data_vrednost === null) {

            $_qry_data_vrednost = sisplet_query("SELECT spr_id, vre_id, usr_id, loop_id 
                                                    FROM srv_data_vrednost".$this->db_table." 
                                                    WHERE spr_id IN (".$this->_str_questions.")" . $string_user);
            if (!$_qry_data_vrednost) {
                $this->trigerError('cache_data_respondent_vrednost',  mysqli_error($GLOBALS['connect_db']));
            }	
     			
			if (mysqli_num_rows($_qry_data_vrednost) > 0) {

				while (list($spr_id, $vre_id, $usr_id,$loop_id) = mysqli_fetch_row($_qry_data_vrednost)) {

					$loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
					$this->_array_data_vrednost[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id] = (int)$vre_id;
				}
            }
        } 
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_vrednost
	private function get_array_data_vrednost ($uid, $spid) {
        
        if (isset($this->_array_data_vrednost[$uid][$spid]))
			return $this->_array_data_vrednost[$uid][$spid];
        else
			return array();                
	}

    // Zakesiramo podatke tabele srv_data_text
    private function cache_data_respondent_text($string_user){

        if ($this->_array_data_text === null) {

            $_qry_data_text = sisplet_query("SELECT spr_id, vre_id, usr_id, REPLACE(REPLACE(REPLACE(text,'\n',' '),'\r',' '),'|',' ') as text, REPLACE(REPLACE(REPLACE(text2,'\n',' '),'\r',' '),'|',' ') as text2, loop_id 
                                                FROM srv_data_text".$this->db_table." 
                                                WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_text) {
                $this->trigerError('cache_data_respondent_text',  mysqli_error($GLOBALS['connect_db']));
            }
            
            if (mysqli_num_rows($_qry_data_text) > 0) {

                while (list($spr_id, $vre_id, $usr_id, $text, $text2, $loop_id) = mysqli_fetch_row($_qry_data_text)) {
                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    
                    // Addslashes je potreben za analize
                    $this->_array_data_text[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id] = array(
                        'text'=>trim(addslashes(strip_tags($text))), 
                        'text2'=>trim(addslashes(strip_tags($text2)))
                    );
                }
            }
        }
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_text
	private function get_array_data_text ($uid, $spid) {
		
		if (isset($this->_array_data_text[$uid][$spid]))
			return $this->_array_data_text[$uid][$spid];
        else
			return array();
	}

    // Zakesiramo podatke tabele srv_data_grid
    private function cache_data_respondent_grid($string_user){
                
        if ($this->_array_data_grids === null) {

            $_qry_data_grids = sisplet_query("SELECT spr_id, vre_id, usr_id, grd_id, loop_id 
                                                FROM srv_data_grid".$this->db_table." 
                                                WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_grids) {
                $this->trigerError('cache_data_respondent_grid',  mysqli_error($GLOBALS['connect_db']));
            }
                                
            if (mysqli_num_rows($_qry_data_grids) > 0) {

                while (list($spr_id, $vre_id, $usr_id, $grd_id, $loop_id) = mysqli_fetch_row($_qry_data_grids)) {

                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    $this->_array_data_grids[(int)$usr_id][$spr_id.'_'.$loop_id ][(int)$vre_id] = (int)$grd_id;
                }
            }
		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_grids
	private function get_array_data_grids ($uid, $spid) {

		if (isset($this->_array_data_grids[$uid][$spid]))
			return $this->_array_data_grids[$uid][$spid];
		else
			return array();			
	}

    // Zakesiramo podatke tabele srv_data_checkgrid
    private function cache_data_respondent_check_grid($string_user){
            
        if ($this->_array_data_check_grids === null) {

            $_qry_data_check_grids = sisplet_query("SELECT spr_id, vre_id, usr_id, grd_id, loop_id 
                                                            FROM srv_data_checkgrid".$this->db_table." 
                                                            WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_check_grids) {
                $this->trigerError('cache_data_respondent_check_grid', mysqli_error($GLOBALS['connect_db']));
            }
                                
            if (mysqli_num_rows($_qry_data_check_grids) > 0) {

                while (list($spr_id, $vre_id, $usr_id, $grd_id, $loop_id) = mysqli_fetch_row($_qry_data_check_grids)) {

                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    $this->_array_data_check_grids[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id][(int)$grd_id] = (int)$grd_id;
                }
            }
		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_checkgrid
	private function get_array_data_check_grids ($uid, $spid) {
        
        if (isset($this->_array_data_check_grids[$uid][$spid]))
			return $this->_array_data_check_grids[$uid][$spid];
		else
			return array();
	}

    // Zakesiramo podatke tabele srv_data_textgrid
    private function cache_data_respondent_text_grid($string_user){
        
        if ($this->_array_data_text_grid === null) {

            $_qry_data_text_grid = sisplet_query("SELECT spr_id, vre_id, usr_id, grd_id, REPLACE(REPLACE(REPLACE(REPLACE(text, '\\\\', '/'),'\n',' '),'\r','<br>'),'|',' ') as text, loop_id 
                                                    FROM srv_data_textgrid".$this->db_table." 
                                                    WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_text_grid) {
                $this->trigerError('cache_data_respondent_text_grid',  mysqli_error($GLOBALS['connect_db']));
            }
            
            if (mysqli_num_rows($_qry_data_text_grid) > 0) {

                while ( list($spr_id, $vre_id, $usr_id, $grd_id, $text, $loop_id) = mysqli_fetch_row($_qry_data_text_grid)) {

                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    $this->_array_data_text_grid[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id][(int)$grd_id] = trim(strip_tags($text));
                }
            }
		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_textgrid
	private function get_array_data_text_grid ($uid, $spid) {
        
        if (isset($this->_array_data_text_grid[$uid][$spid]))
			return $this->_array_data_text_grid[$uid][$spid];
		else
			return array();
	}

    // Zakesiramo podatke tabele srv_data_rating
    private function cache_data_respondent_rating($string_user){
           
        if ($this->_array_data_rating === null) {

            $_qry_data_rating = sisplet_query("SELECT spr_id, vre_id, usr_id, vrstni_red, loop_id 
                                                FROM srv_data_rating 
                                                WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_rating) {
                $this->trigerError('cache_data_respondent_rating', mysqli_error($GLOBALS['connect_db']));
            }
                        
            if (mysqli_num_rows($_qry_data_rating) > 0) {
                
                while (list($spr_id, $vre_id, $usr_id, $vrstni_red, $loop_id) = mysqli_fetch_row($_qry_data_rating)) {

                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    $this->_array_data_rating[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id] = (int)$vrstni_red;
                }
            }
        }
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_rating
	private function get_array_data_rating ($uid, $spid) {
        
        if (isset($this->_array_data_rating[$uid][$spid]))
			return $this->_array_data_rating[$uid][$spid];
		else
			return array();
	}
    
    // Zakesiramo podatke tabele srv_data_upload
    private function cache_data_respondent_text_upload($string_user){

        if ($this->_array_data_text_upload === null) {

            $_qry_data_text_upload = sisplet_query("SELECT usr_id, code, filename 
                                                        FROM srv_data_upload 
                                                        WHERE ank_id='".$this->sid."' ".$string_user);
            if (!$_qry_data_text_upload) {
                $this->trigerError('cache_data_respondent_text_upload', mysqli_error($GLOBALS['connect_db']));
            }
            
            if (mysqli_num_rows($_qry_data_text_upload) > 0) {

                while (list($usr_id, $code, $filename) = mysqli_fetch_row($_qry_data_text_upload)) {
                    $this->_array_data_text_upload[$code] = strip_tags($filename);
                }
            }
		}
    }

    // Vrnemo vrednost za filename za posamezen code za tabelo srv_data_upload
	private function get_array_data_text_upload ($_code) {
        
        if (isset($this->_array_data_text_upload[$_code]))
			return $this->_array_data_text_upload[$_code];
		else
			return 'Download';
	}

    // Zakesiramo podatke tabele srv_data_map
    private function cache_data_respondent_map($string_user){
              
        if ($this->_array_data_map === null) {

            $_qry_data_map = sisplet_query("SELECT spr_id, usr_id, REPLACE(REPLACE(REPLACE(address,'\n',' '),'\r',' '),'|',' ') as address, REPLACE(REPLACE(REPLACE(text,'\n',' '),'\r',' '),'|',' ') as text, lat, lng, loop_id 
                                                FROM srv_data_map 
                                                WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_map) {
                $this->trigerError('cache_data_respondent_map', mysqli_error($GLOBALS['connect_db']));
            }

            $i = 0;
			if (mysqli_num_rows($_qry_data_map) > 0) {

				while (list($spr_id, $usr_id, $address, $text, $lat, $lng, $loop_id) = mysqli_fetch_row($_qry_data_map)) {
                   
                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    
                    // Ce respondent ni vnesel vrednosti v infowindow
                    if($text == '' || $text == null) $text = '-1';
                    
                    // Addslashes je potreben za analize
                    if(isset($this->_array_data_map[(int)$usr_id][$spr_id.'_'.$loop_id])){
                        $this->_array_data_map[(int)$usr_id][$spr_id.'_'.$loop_id]['izpis'][$i] = array(
                            'address'=>addslashes(strip_tags($address)), 
                            'vrednost'=>addslashes(strip_tags($text)), 
                            'koordinate'=> $lat . ', ' . $lng);
                    }
                    else{
                        $this->_array_data_map[(int)$usr_id][$spr_id.'_'.$loop_id] = array(
                            'izpis'=>array(
                                array(
                                    'address'=>addslashes(strip_tags($address)), 
                                    'vrednost'=>addslashes(strip_tags($text)),
                                    'koordinate'=>$lat . ', ' . $lng
                                )
                            ),
                            'spr_id' => $spr_id, 'usr_id' => $usr_id, 'loop_id' => $loop_id
                        );
                    }

                    $i++;
				}
			}
        } 
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_map
	private function get_array_data_map ($uid, $spid) {
		
		if (isset($this->_array_data_map[$uid][$spid]))
			return $this->_array_data_map[$uid][$spid];
        else
			return array();
	}

    // Zakesiramo podatke tabele srv_data_heatmap
    private function cache_data_respondent_heatmap($string_user){
        
        if ($this->_array_data_heatmap === null) {

            $_qry_data_heatmap = sisplet_query("SELECT spr_id, usr_id, REPLACE(REPLACE(REPLACE(address,'\n',' '),'\r',' '),'|',' ') AS address, REPLACE(REPLACE(REPLACE(text,'\n',' '),'\r',' '),'|',' ') as text, lat, lng, loop_id 
                                                    FROM srv_data_heatmap 
                                                    WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_heatmap) {
                $this->trigerError('cache_data_respondent_heatmap', mysqli_error($GLOBALS['connect_db']));
            }

            $i = 0;
			if (mysqli_num_rows($_qry_data_heatmap)) {

				while (list($spr_id, $usr_id, $address, $text, $lat, $lng, $loop_id) = mysqli_fetch_row($_qry_data_heatmap)) {

                    $loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    
                    // ce respondent ni vnesel vrednosti v infowindow
                    if($text == '' || $text == null) $text = '-1';
                    
                    // adslashes je potreben za analize
                    if(isset($this->_array_data_heatmap[(int)$usr_id][$spr_id.'_'.$loop_id])){
                        $this->_array_data_heatmap[(int)$usr_id][$spr_id.'_'.$loop_id]['izpis'][$i] = array(
                            'address'=>addslashes(strip_tags($address)), 
                            'vrednost'=>addslashes(strip_tags($text)), 
                            'koordinate'=> $lat . ', ' . $lng
                        );
                    }
                    else{
                        $this->_array_data_heatmap[(int)$usr_id][$spr_id.'_'.$loop_id] = array(
                            'izpis'=>array(
                                array(
                                    'address'=>addslashes(strip_tags($address)), 
                                    'vrednost'=>addslashes(strip_tags($text)),
                                    'koordinate'=>$lat . ', ' . $lng
                                )
                            ),
                            'spr_id' => $spr_id, 'usr_id' => $usr_id, 'loop_id' => $loop_id
                        );
                    }

                    $i++;
				}
			}
		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_heatmap
	private function get_array_data_heatmap ($uid, $spid) {
		
		if (isset($this->_array_data_heatmap[$uid][$spid]))
			return $this->_array_data_heatmap[$uid][$spid];
		else
			return array();
    }

    // Zakesiramo podatke tabele srv_data_heatmap_regions
    private function cache_data_respondent_heatmap_regions($string_user){
            
        if ($this->_array_data_heatmap_regions === null) {

            $spremenljivke_id = explode(",", $this->_str_questions);
            $_qry_data_heatmap_regions = array();
            
            foreach ($spremenljivke_id AS $val) {
                $str_query_regions = "SELECT id, vre_id, spr_id, region_name, region_coords FROM srv_hotspot_regions WHERE spr_id =".$val.' ';
                array_push($_qry_data_heatmap_regions, $str_query_regions);
            }

            foreach($_qry_data_heatmap_regions as $query){

                $regionsQuery = sisplet_query($query);			
                if ($regionsQuery === null) {
                    $this->trigerError('cache_data_respondent_heatmap_regions', mysqli_error($GLOBALS['connect_db']));
                }

                $i = 0;
                if (mysqli_num_rows($regionsQuery) > 0) {

                    while (list($region_id, $vre_id, $spr_id, $region_name, $region_coords) = mysqli_fetch_row($regionsQuery)) {   
                        
                        // adslashes je potreben za analize
                        $this->_array_data_heatmap_regions[(int)$spr_id][$i] = array(
                            'region_name'   => addslashes(strip_tags($region_name)), 
                            'region_coords' => $region_coords);

                        $i++;
                    }
                }			
            }
 		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_heatmap_regions
	private function get_array_data_heatmap_regions ($uid, $spid) {
					
        if (isset($this->_array_data_heatmap_regions[$uid][$spid]))
           return $this->_array_data_heatmap_regions[$uid][$spid];
        else
           return array();
    }

    // Zakesiramo podatke tabele srv_data_vrednost_cond
    private function cache_data_respondent_vrednost_cond($string_user){

        if ($this->_array_data_vrednost_cond === null) {

            $_qry_data_vrednost_cond = sisplet_query("SELECT spr_id, vre_id, usr_id, text, loop_id 
                                                        FROM srv_data_vrednost_cond 
                                                        WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
            if (!$_qry_data_vrednost_cond) {
                $this->trigerError('cache_data_respondent_vrednost_cond', mysqli_error($GLOBALS['connect_db']));
            }
       
			if (mysqli_num_rows($_qry_data_vrednost_cond) > 0) {

				while ( list($spr_id, $vre_id, $usr_id, $text, $loop_id) = mysqli_fetch_row($_qry_data_vrednost_cond)) {
					$loop_id = ($loop_id == null || $loop_id == '') ? 0 : $loop_id;
                    
                    // adslashes je potreben za analize
					$this->_array_data_vrednost_cond[(int)$usr_id][$spr_id.'_'.$loop_id][(int)$vre_id] = addslashes(strip_tags($text));
				}
			}
		}
    }

    // Vrnemo vrednost za uid in spr_id za tabelo srv_data_vrednost_cond
	private function get_array_data_vrednost_cond ($uid, $spid) {
        
        if (isset($this->_array_data_vrednost_cond[$uid][$spid]))
			return $this->_array_data_vrednost_cond[$uid][$spid];
        else
			return array();
	}

    // Zakesiramo podatke randomiziranih vprasanj
    private function cache_data_respondent_random($string_user){

        // Ce smo ze zakesirali ne rabimo naprej
        if ($this->_array_data_random === null){
                        
            // Najprej dobimo random podatke za randomizacijo znotraj vprasanj
            $_qry_data_random = sisplet_query("SELECT spr_id, usr_id, vrstni_red 
                                                FROM srv_data_random_spremenljivkaContent 
                                                WHERE spr_id IN (".$this->_str_questions.") ".$string_user);
                                                //WHERE ".$string_user);
            if (!$_qry_data_random) {
                $this->trigerError('cache_data_respondent_random', mysqli_error($GLOBALS['connect_db']));
            }

            if (mysqli_num_rows($_qry_data_random) > 0) {
                while($row1 = mysqli_fetch_array($_qry_data_random)){
                    $this->_array_data_random[$row1['usr_id']]['spr'][$row1['spr_id']] = $row1['vrstni_red'];
                }
            }


            // Dobimo se random podatke za randomizacijo znotraj blokov
            if($this->_str_blocks != ''){

                $_qry_data_random = sisplet_query("SELECT block_id, usr_id, vrstni_red 
                                                    FROM srv_data_random_blockContent 
                                                    WHERE block_id IN (".$this->_str_blocks.") ".$string_user);
                                                    //WHERE ".$string_user);
                if (!$_qry_data_random) {
                    $this->trigerError('cache_data_respondent_random', mysqli_error($GLOBALS['connect_db']));
                }

                if (mysqli_num_rows($_qry_data_random) > 0) {

                    while($row2 = mysqli_fetch_array($_qry_data_random)){
                        
                        // Zgradimo string z labelami vprasanj za prikaz v podatkih
                        $vrstni_red_string = '';

                        // Spremenljivke v blokih
                        if($this->_array_random[$row2['block_id']]['type'] == 'blok_spr'){

                            // Pridobimo imena vprasanj za izpis
                            $sqlLabele1 = sisplet_query("SELECT variable 
                                            FROM srv_spremenljivka
                                            WHERE id IN (".$row2['vrstni_red'].")
                                            ORDER BY FIND_IN_SET(id, '".$row2['vrstni_red']."')
                                        ");
                                        
                            while($rowLabele1 = mysqli_fetch_array($sqlLabele1)){
                                $vrstni_red_string .= $rowLabele1['variable'].',';
                            }
                        }
                        // Bloki v blokih
                        else{
                            // Pridobimo imena blokov za izpis
                            $sqlLabele2 = sisplet_query("SELECT number 
                                            FROM srv_if
                                            WHERE id IN (".$row2['vrstni_red'].")
                                            ORDER BY FIND_IN_SET(id, '".$row2['vrstni_red']."')
                                        ");

                            if($sqlLabele2){
                                while($rowLabele2 = mysqli_fetch_array($sqlLabele2)){
                                    $vrstni_red_string .= 'B'.$rowLabele2['number'].',';
                                }
                            }
                        }	

                        $vrstni_red_string = substr($vrstni_red_string, 0, -1);
                        $this->_array_data_random[$row2['usr_id']]['block'][$row2['block_id']] = $vrstni_red_string;
                    }
                }
            }
        }
    }

    // Vrnemo podatke vrstnega reda za userja in element (vprasanje ali blok)
	private function get_array_data_random ($uid, $el_id, $el_type) {

		if (isset($this->_array_data_random[$uid][$el_type][$el_id]))
			return $this->_array_data_random[$uid][$el_type][$el_id];
		else
			return '';
    }
    
    /***** KONEC - KESIRANJE ODGOVOROV POSAMEZNEGA RESPONDENTA *****/



    // Iz datoteke pobrisemo response, ki so zastareli / pobrisani
    private function deleteUsers() {

		// Datoteka s podatki
        $f1 = $this->data_file_name;
        
		// Datoteka kam zapišemo user id-je ki jih je potrebno pobrisat
        $f2 = $this->data_file_name.'.todel';
        
		// Datoteka kamor začasno shranimo original
		$f3 = $this->data_file_name.'_'.time().'.orig';

		if ((int)$this->sid > 0 && file_exists($f1)) {
            
            try {

                // Preberemo max time iz datoteke in uporabimo manjšega
                if (IS_WINDOWS) {
                    $command = 'awk -F"|" "BEGIN {max = 0} {if ('.TIME_FIELD.' > max && '.TIME_FIELD.' > 0) max='.TIME_FIELD.' } END {print max}" '.$f1;
                } 
                else {
                    $command = 'awk -F\'|\' \'BEGIN {max = 0} {if ('.TIME_FIELD.' > max && '.TIME_FIELD.' > 0) max='.TIME_FIELD.' } END {print max}\' '.$f1;
                }
                $user_time_from_file = shell_exec($command);
                
                $inkremental_user_limit = " AND u.time_edit > FROM_UNIXTIME('".max((int)$this->data_file_time,(int)$user_time_from_file)."') ";
                    
                // Za inkrementalno brisanje datotek
                $qry_users_deleted = sisplet_query("SELECT u.id AS usr_id  FROM srv_user AS u WHERE u.ank_id='".$this->sid."' ".$inkremental_user_limit);
                if (mysqli_num_rows($qry_users_deleted) > 0) {
                    
                    // Zapišemo v datoteko idje respondentov, ki jih brišemo
                    $file = fopen($f2, 'w');
                    
                    while (list($usr_id) = mysqli_fetch_row($qry_users_deleted)) {
                        fwrite($file, $usr_id . "\n");
                    }
                    
                    fclose($file);
                    
                    if (IS_WINDOWS) {
                        $cmd = 'cp '.$f1.' '.$f3.' && awk -F"|" "NR==FNR {++a[$0]; next} { f = $1; if (!a[$1]) print $0}" '.$f2.' '.$f3.' > '.$f1;
                    } 
                    else {
                        $cmd = 'cp '.$f1.' '.$f3.' && awk -F\'|\' \'NR==FNR {++a[$0]; next} { f = $1; if (!a[$1]) print $0}\' '.$f2.' '.$f3.' > '.$f1;
                    }

                    $out_command = shell_exec($cmd);
                }		

                // Ce je vse ok, pobrišemo datoteke
                if (file_exists($f1)) {
                   
                    // Pobrišemo datoteko z userji katere smo brisali
                    if (file_exists($f2)) {
                        unlink($f2);
                    }
                    
                    // Pobrišemo backup datoteko
                    if (file_exists($f3)) {
                        unlink($f3);
                    }
                }
            } 
            catch (Exception $e) {
            }
		}
    }

    // Resetiramo vse pointerje   
    private function cleanup() {
		
		$this->noErrors = true;
		
        $this->_HEADER = null;
        
		$this->_str_groups = null;		# cache string za strani
        $this->_cnt_groups = 0;			# cache število za strani
        
		$this->_str_questions = null; 	# cache string za vprašanja
        $this->_cnt_questions = 0; 		# cache za število vprašanj
        
		$this->_qry_users = null;		# cache query za vprašanja
        $this->_str_users = null;		# cache string za user_id ji
        $this->_cnt_all_users = 0; 		# cache za število userjev
        
		$this->_array_vrednosti = null;	# cache array z vrednostmi   
		$this->_array_gridi = null;		# cache array z gridi
        $this->_array_random = null;	# cache array z random vprasanji in bloki
        $this->_array_SPSS = null;			# zakeširamo array usergrup za uporabnika
		$this->_array_user_grupa = null;	# zakeširamo array usergrup za uporabnika
		$this->_array_users_from_CMS = null;
                
		$this->_array_data_vrednost = null;
		$this->_array_data_text = null;
		$this->_array_data_grids = null;
		$this->_array_data_check_grids = null;
		$this->_array_data_text_grid = null;
		$this->_array_data_rating = null;
        $this->_array_data_text_grid = null;
        $this->_array_data_text_upload = null;
        $this->_array_data_map = null;
        $this->_array_data_heatmap = null;
        $this->_array_data_heatmap_regions = null;
        $this->_array_data_vrednost_cond = null;
        $this->_array_data_random = null;
    }
    
    
    
    // Zakesiramo in vrnemo respondente, ki so tudi prijavljeni v 1ki (ce ima anketa to nastavitev vklopljeno)
    private function get_user_CMS_email($user_from_cms_id) {

		// če še niammo arraya z e-maili
		if ($this->_array_users_from_CMS == null) {
            
            // polovimo emaile uporrabnikov iz CMS
            $this->_array_users_from_CMS = array();

            $qry = sisplet_query("SELECT su.user_id, u.email 
                                    FROM srv_user AS su JOIN users AS u ON su.user_id = u.id 
                                    WHERE su.ank_id = '".$this->sid."' AND su.preview='0' AND su.deleted='0' AND su.user_id > 0");
            while ( $row = mysqli_fetch_assoc($qry)) {
                $this->_array_users_from_CMS[$row['user_id']] = $row['email'];
            }
        }

        // ko smo že polovili e-maile
        if (is_array($this->_array_users_from_CMS) && isset($this->_array_users_from_CMS[$user_from_cms_id])) {
            return $this->_array_users_from_CMS[$user_from_cms_id];
        } 
        else {
            return '';
        }
    }
    
    // Pripravimo missing vrednosti za anketo
	private function setSurveyMissingValues() {
        
        $smv = new SurveyMissingValues($this->sid);
		$smv -> Init();
		
        $_sys_missings = $smv->GetSurveyMissingValues();
        
		if (is_array($_sys_missings) && count($_sys_missings) > 0){
            
            foreach ($_sys_missings[1] AS $_sys_missing) {
                
                // nastavimo mapping za rekodiranje sistemskih vrednosti
                $this->sysMissingMap[$_sys_missing['defSysVal']] = $_sys_missing['value'];
            }
        }
    }

    // Prestejemo stevilo vrstic v datoteki
	private function getLinesCnt(){

		if (IS_WINDOWS) {
			$command = 'awk "NF != 0 {++count} END {print count}" '.$this->data_file_name;
        } 
        else {
			$command = 'awk \'NF != 0 {++count} END {print count}\' '.$this->data_file_name;
        }
        
        $lines = shell_exec($command);
        
		return (int)$lines;
    }
    
    // Ugotovimo do katere spremenljivke zamenjujemo vrednosti
	private function calculateValuesToChange($uid) {

		$result = 0;
		
		if (count($this->_user_spr_answer_count['spremenljivke']) == 0) {
			return $result;
        }
        
		if (isset($this->_user_spr_answer_count['last_seen']) && $this->_user_spr_answer_count['last_seen'] == $this->_user_spr_answer_count['cnt']) {
			return $result;
        }
        
		if (count($this->_user_spr_answer_count['spremenljivke']) == 0) {
			return $result;
        }
        
        if (isset($this->_user_spr_answer_count['last_seen']))
            foreach ($this->_user_spr_answer_count['spremenljivke'] AS $cnt) {
                if ($this->_user_spr_answer_count['last_seen'] > $result) {
                    $result += $cnt;
                }
            }
		 
		return $this->_user_spr_answer_count['cnt'] - $result;
    }
        

    // Rekodiranje odgovora
    private function recode_answer($spr_id, $answer, $uid) {

		$this->addUserSprAnswerCount($spr_id, $answer, $uid);
		
		// Najprej preverimo ali imamo mapping za sistemske missige
		$answer = isset($this->sysMissingMap[$answer]) && $this->sysMissingMap[$answer] != null 
			? $this->sysMissingMap[$answer]
            : $answer;
            
        $_recoded = $answer;
        
		if ( $this->_array_recode === null) {
			$this->_array_recode = array();

			// Polovimo vse spremenljivke za katere moramo rekodirat
			$sqlSelect = sisplet_query("SELECT spr_id, search, value, operator FROM srv_recode WHERE ank_id = '".$this->sid."' AND enabled='1' ORDER BY vrstni_red");
			if (!$sqlSelect) {
				$this->trigerError('recode_answer', mysqli_error($GLOBALS['connect_db']));
            }
            
			if (mysqli_num_rows($sqlSelect)) {
				while ($rowSelect = mysqli_fetch_assoc($sqlSelect)) {
					$this->_array_recode[$rowSelect['spr_id']][] = array('s'=>$rowSelect['search'],'v'=>$rowSelect['value'],'o'=>$rowSelect['operator']);
				}
			}
        }
        
		if (isset($this->_array_recode[$spr_id])) {

            $recoded = false;
            
			if (isset($this->_array_recoded[$spr_id][$answer])) {
				return $this->_array_recoded[$spr_id][$answer];
            } 
            else if (count($this->_array_recode[$spr_id]) > 0 && $recoded == false) {
                
                foreach ($this->_array_recode[$spr_id] AS $k => $do_recode) {
                    
                    if ( $answer == $do_recode['s'] || (int)$do_recode['o'] > 0 && $recoded == false) {
						$original = $_recoded;
						$_recoded = $this->check_recode($_recoded,$do_recode['s'],$do_recode['v'],$do_recode['o']);
                        
                        if ($original != $_recoded) {
							$recoded = true;
						}
					}
                }
                
				$this->_array_recoded[$spr_id][$answer] = $_recoded;
			}
		}

		// Vrnemo rezultat
		return $_recoded;
    }

    // Rekodiranje odgovora - stejemo zadnmi veljaven odgovor
    private function addUserSprAnswerCount($spr, $answer, $uid) {

        // štejemo zadnji veljaven odgovor
        $this->_user_spr_answer_count['cnt']++;
        
		if ($answer != -1 && $answer != -4 && $answer !== '-2d') {
            
            // sistemskih in skritih ne upoštevamo
            if ($this->AllQuestionsData[$spr]['sistem'] == 1 || $this->AllQuestionsData[$spr]['visible'] == 0) {
                // preskočimo
            } 
            else {
			    $this->_user_spr_answer_count['last'] = $this->_user_spr_answer_count['cnt']; 
			    $this->_user_spr_answer_count['last_seen'] = $this->_user_spr_answer_count['cnt']; 
            }
		}	
		
		// Pogledamo se ce je respondent videl vprasanje (tudi ce je -1, ker ga potem ne smemo spremenit v -3)					
		if ($answer == -1 || $answer === '-2d') {
            if (isset($this->_array_user_grupa[$uid]) && is_array($this->_array_user_grupa[$uid]) && array_key_exists($this->AllQuestionsData[$spr]['gru_id'], $this->_array_user_grupa[$uid])) {
				$this->_user_spr_answer_count['last_seen'] = $this->_user_spr_answer_count['cnt'] - 1; 
            }
		}	
		
		if (!isset($this->_user_spr_answer_count['spremenljivke'][$spr])) {
			$this->_user_spr_answer_count['spremenljivke'][$spr] = 0;
		}
		
		$this->_user_spr_answer_count['spremenljivke'][$spr]++;
    }

    // Rekodiranje odgovora - preverimo glede na operator
	private function check_recode ($answer, $search, $value, $operand) {

        $_recoded = $answer;
        
		switch ($operand) {
            
			case 0:	# ==
				$_recoded = $answer == $search ? $value : $answer;
                break;
                
			case 1:	# <>
				$_recoded = $answer != $search ? $value : $answer;
                break;
                
			case 2:	# <
				$_recoded = $answer < $search ? $value : $answer;
                break;
                
			case 3:	# >
				$_recoded = $answer > $search ? $value : $answer;
                break;
                
			case 4:	# <=
				$_recoded = $answer <= $search ? $value : $answer;
				break;
            
            case 5:	# >=
				$_recoded = $answer >= $search ? $value : $answer;
				break;
            
            case 6:	# >=
				$search = explode(',',$search);
				$s1 = (int)$search[0];
				$s2 = (int)$search[1];
				$_recoded = (int)$answer >= $s1 && (int)$answer <= $s2 ? $value : $answer;
				break;
        }
        
		return $_recoded;
    }


    // Heatmap - pretvori polje s tockami obmocja v ustrezno obliko
	private function convertPolyString($polypoints){

		$poly = array();
		$polyObjectArray = array();
		$j = 0;
		$poly = explode(',',$polypoints);

		for($i = 0; $i<sizeof($poly); $i++){
             
            if($i == 0 || $i%2 == 0){
				$tmpX = (int)$poly[$i];
            }
            else{
                $tmpY = (int)$poly[$i];
                
				$polyObjectArray[$j]["x"] = $tmpX;
                $polyObjectArray[$j]["y"] = $tmpY;
                
				$j++;
			}
        }
        
		return $polyObjectArray;
    }
    
    // Heatmap - preveri, ali je posamezna tocka znotraj trenutnega obmocja
 	private function insidePoly($poly, $pointx, $pointy){

        $inside = false;
        		
  		for ($i = 0, $j = sizeof($poly) - 1; $i < sizeof($poly); $j = $i++) {

            if( (($poly[$i]["y"] > $pointy) != ($poly[$j]["y"] > $pointy)) && ($pointx < ($poly[$j]["x"]-$poly[$i]["x"]) * ($pointy-$poly[$i]["y"]) / ($poly[$j]["y"]-$poly[$i]["y"]) + $poly[$i]["x"]) )
                $inside = !$inside;		
        }
        
		return $inside;
    }
    
    // Heatmap - priprava spremenljivk za koordinate točk
	private function preparePointCoords($answerKoo, $returnPointx, $returnPointy){

		$coordsWithComma = explode('<br>',$answerKoo);
		$pointx = array();
		$pointy = array();
        $coords = array();

		for($i = 1; $i<sizeof($coordsWithComma); $i++){

            // Vecdimenzionalno polje
			$coords[$i-1] = explode(',',$coordsWithComma[$i]);	
		}

		// Loop-anje skozi vecdimenzionalno polje
		$i = 0;
		$j = 0;
		foreach ($coords as $coord) {
            
            foreach ($coord as $coo) {			
                
                if($i == 0 || $i%2 == 0){
					$tmpX = (int)$coo;
                }
                else{
					$tmpY = (int)$coo;
					$pointx[$j]["x"] = $tmpX;
					$pointy[$j]["y"] = $tmpY;
					$j++;
                }
                
				$i++;
			}
		}
		
		if ($returnPointx == 1){
			return $pointx;
        }
        
		if ($returnPointy == 1){
			return $pointy;
		}
	}


    // Zapisemo error in oznacimo da imamo error
	private function trigerError($what, $error) {

		$this->noErrors = false;
		$this->errors[][$what] = $error;
		
		return;
	}

    // Vrnemo error
	private function getError() {
		return array('error'=>$this->errors);
    }

    // Shranimo checkpoint za merjenje performanca - zaenkrat onemogoceno (samo za testiranje)
    private function setLogCheckpoint($checkpoint_name){
        global $admin_type;

        if(true || $admin_type != 0)
            return;

        $this->log[$checkpoint_name]['time'] = microtime(true);
        $this->log[$checkpoint_name]['memory'] = memory_get_usage();
        $this->log[$checkpoint_name]['memory_peak'] = memory_get_peak_usage();        
    }

    // Zapisemo checkpointe v logfile - zaenkrat onemogoceno (samo za testiranje)
    private function saveLogCheckpoints(){

        if(true || $admin_type != 0)
            return;

        $SL = new SurveyLog();

        $cnt = 0;
        foreach($this->log as $checkpoint => $data){

            $time = ($cnt == 0) ? 'Start: '.$data['time'] : 'Elapsed: '.($data['time'] - $this->log['start']['time']);
            $memory = 'Memory: '.((int)$data['memory'] / 1024 / 1000).' MB';
            $memory_peak = 'Memory peak: '.((int)$data['memory_peak'] / 1024 / 1000).' MB';

            $message = ' TESTING data collection for survey '.$this->sid.' - checkpoint '.$checkpoint.' - '.$time.', '.$memory.', '.$memory_peak;

            $SL->addMessage(SurveyLog::DEBUG, $message);

            $cnt++;
        }

        $SL->write();	
    }
}
?>