summaryrefslogtreecommitdiffstats
path: root/admin/survey/classes/class.SurveyQuotas.php
blob: 4059521f9e6b7768ece9a4dd3b26a2569f7ca2cd (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
<?php
/**
* @author 	Peter Hrvatin
* @date		December 2015
*
*/

	
class SurveyQuotas {
	
	private $anketa;									# id ankete	
	private $spremenljivka;							# id spremenljivke	

	
	/**
	* 	Konstruktor
	*/
	public function __construct( $anketa = null ) {
		global $global_user_id, $site_path;
	
		// če je podan anketa ID		
		if ((int)$anketa > 0) { 		

			$this->anketa = $anketa;
		}
		else {
			die("Napaka!");
		}
	}
	
	
	public function quota_display($condition, $long_alert=0) {
        global $lang;

        $echo = '';
        $echo .= '<span class="quota_display">';

        if ($condition < 0) {
	        $rowC = Cache::srv_spremenljivka($condition < 0 ? -$condition : $condition);
	        $echo .= $rowC['variable'].' = ';
		}

        $sql = sisplet_query("SELECT * FROM srv_quota WHERE cnd_id = '$condition' ORDER BY vrstni_red ASC");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);

        $bracket = 0;
        $i = 0;
        while ($row = mysqli_fetch_array($sql)) {

            if ($i++ != 0)
                if ($row['operator'] == 0)
                    $echo .= ' <span class="conjunction">+</span> ';
                elseif ($row['operator'] == 1)
                    $echo .= ' <span class="conjunction">-</span> ';
                elseif ($row['operator'] == 2)
                    $echo .= ' <span class="conjunction">*</span> ';
                elseif ($row['operator'] == 3)
                    $echo .= ' <span class="conjunction">/</span> ';

            for ($i=1; $i<=$row['left_bracket']; $i++)
                if ($long_alert == 1)
                    $echo .= ' <span class="bracket'.(($bracket++)%12).'">(</span> ';
                else
                    $echo .= ' ( ';

			// Pri kvotah imamo vedno count
			$echo .= $lang['srv_quota'].' (';
					
            // spremenljivke
            if ($row['spr_id'] > 0) {

                // obicne spremenljivke
                if ($row['vre_id'] == 0) {
                    $row1 = Cache::srv_spremenljivka($row['spr_id']);
                    if ($row1['tip'] != 7) {
                    	$variable = $row1['variable'];					
					}
					// number
					else {
						$variable = $row1['variable'].'['.($row['grd_id']+1).']';
					}
                } 
				// multigrid, checkbox, radio
				elseif ($row['grd_id'] == 0) {
                    $sql1 = sisplet_query("SELECT variable FROM srv_vrednost WHERE id = '$row[vre_id]'");
                    if (!$sql1) echo mysqli_error($GLOBALS['connect_db']);
                    $row1 = mysqli_fetch_array($sql1);
                    
					$rowS = Cache::srv_spremenljivka($row['spr_id']);
					if ($rowS['tip'] != 1) {
						$variable = $row1['variable'];
					}
					else{
						$variable = $rowS['variable'].'_'.$row1['variable'];
					}
                } 
				// multichecckbox, multinumber
				elseif ($row['grd_id'] > 0) {
					$sql1 = sisplet_query("SELECT variable FROM srv_vrednost WHERE id = '$row[vre_id]'");
                    if (!$sql1) echo mysqli_error($GLOBALS['connect_db']);
                    $row1 = mysqli_fetch_array($sql1);
                    $sql1g = sisplet_query("SELECT variable FROM srv_grid WHERE id = '$row[grd_id]'");
                    if (!$sql1g) echo mysqli_error($GLOBALS['connect_db']);
                    $row1g = mysqli_fetch_array($sql1g);
                    $variable = $row1['variable'].'['.$row1g['variable'].']';
                }

                if ($long_alert) $echo .= '<strong>';
                $echo .= $variable;
				if ($long_alert) $echo .= '</strong>';

            // konstante
            } elseif ($row['spr_id'] < 0) {

                $echo .= $lang['srv_quota_status_'.(-1*$row['spr_id'])];

            }
			
			// Pri kvotah imamo vedno count
			$echo .= ')';	

            for ($i=1; $i<=$row['right_bracket']; $i++)
                if ($long_alert == 1)
                    $echo .= ' <span class="bracket'.((--$bracket)%12).'">)</span> ';
                else
                    $echo .= ' ) ';

        }

        $echo .= '</span>';

        if ($long_alert) {
            $quota_check = $this->quota_check($condition);

            if ( $quota_check != 0) {

                if ($quota_check == 1)
                	$echo .= '<br /><span class="faicon warning icon-orange"></span> <span class="red">'.$lang['srv_error_spremenljivka'].'</span>';
                elseif ($quota_check == 2)
                	$echo .= '<br /><span class="faicon warning icon-orange"></span> <span class="red">'.$lang['srv_error_spremenljivka'].'</span>';
                elseif ($quota_check == 3)
                	$echo .= '<br /><span class="faicon warning icon-orange"></span> <span class="red">'.$lang['srv_error_oklepaji'].'</span>';

			}
        }

        return $echo;
    }


	// Izpise urejanje kvote
    private function quota_editing ($condition, $vrednost=0) {
        global $lang;

        echo '<div class="popup_close"><a href="#" onClick="quota_editing_close(\''.$condition.'\', \''.$vrednost.'\'); return false;">✕</a></div>';

        echo '<div id="quota_editing_inner" class="podatki">';
        $this->quota_editing_inner($condition, $vrednost);
        echo '</div>';

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

		$row = Cache::srv_spremenljivka(-$condition);
        if ($condition < 0) {	
            echo '<div class="setting_horizontal_wrapper">';	

            echo '<div class="setting_holder">';	
			echo '  <label>'.$lang['srv_variable'].':</label>';
            echo '  <input type="text" id="variable_'.(-$condition).'" class="large" value="'.$row['variable'].'" onkeyup="quota_edit_variable(\''.-$condition.'\');" />';
            echo '</div>';

            echo '</div>';
        }
		
        echo '</div>';


        echo '<div id="condition_editing_close" class="podatki">';
		echo '  <div class="button_holder">';

		// kalkulacija kot spremenljivka (lahko jo zbrisemo)
        if ($condition < 0) 
			echo '      <button class="medium white-blue" onclick="brisi_spremenljivko(\''.(-$condition).'\'); return false;">'.$lang['srv_anketadelete_txt'].'</button>';				

		echo '      <button class="medium white-blue" onclick="quota_editing_close(\''.$condition.'\', \''.$vrednost.'\'); return false;">'.$lang['srv_zapri'].'</button>';				
		echo '      <button class="medium blue" onclick="quota_editing_close(\''.$condition.'\', \''.$vrednost.'\'); return false;">'.$lang['srv_potrdi'].'</button>';
		
        echo '  </div>';
        echo '</div>';


        ?><script>
		$('#quota_editing_inner').sortable({items: 'form', handle: '.move', stop: function () {
			quota_sort(<?=$condition?>);
		} });
		</script><?
    }

    private function quota_editing_inner ($condition, $vrednost=0) {
		global $lang;

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

        echo '<h2><span class="blue faicon edit"></span>'.$lang['srv_quota'].'</h2>';
        echo '<div id="quota_editing_calculations">';
        echo $this->quota_display($condition, 1);
        echo '</div>';

		echo '</div>';


        echo '<div class="quota_editing_body">';
        
        $sql = sisplet_query("SELECT id FROM srv_quota WHERE cnd_id = '$condition' ORDER BY vrstni_red");
        if (mysqli_num_rows($sql) == 0) {
            sisplet_query("INSERT INTO srv_quota (id, cnd_id, vrstni_red) VALUES ('', '$condition', '1')");
            $sql = sisplet_query("SELECT id FROM srv_quota WHERE cnd_id = '$condition' ORDER BY vrstni_red");
        }
        while ($row = mysqli_fetch_array($sql)) {
			$this->quota_edit($row['id'], $vrednost);
        }

        echo '<p id="quota_editing_operators" class="div_quota_bottom">'.$lang['srv_add_cond'].':
            <a href="#" onclick="quota_add(\''.$condition.'\', \'0\', \''.$vrednost.'\'); return false;" class="noline"><strong style="font-size:18px">&nbsp;+&nbsp;</strong></a>,
            <a href="#" onclick="quota_add(\''.$condition.'\', \'1\', \''.$vrednost.'\'); return false;" class="noline"><strong style="font-size:18px">&nbsp;-&nbsp;</strong></a>,
            <a href="#" onclick="quota_add(\''.$condition.'\', \'2\', \''.$vrednost.'\'); return false;" class="noline"><strong style="font-size:18px">&nbsp;*&nbsp;</strong></a>,
            <a href="#" onclick="quota_add(\''.$condition.'\', \'3\', \''.$vrednost.'\'); return false;" class="noline"><strong style="font-size:18px">&nbsp;/&nbsp;</strong></a>
            </p>';

        echo '</div>';
    }

    /**
    * @desc vrstica v urejanju kvot
    */
    private function quota_edit ($quota, $vrednost=0) {
        global $lang;

		$b = new Branching($this->anketa);
		
        $sql = sisplet_query("SELECT * FROM srv_quota WHERE id = '$quota'");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
        $row = mysqli_fetch_array($sql);

        $sql1 = sisplet_query("SELECT if_id FROM srv_condition WHERE id = '{$row['cnd_id']}'");
        $row1 = mysqli_fetch_array($sql1);

        if ($row['cnd_id'] > 0) {	// kalkulacija znotraj pogoja
        	$vrstni_red = $b->vrstni_red($b->find_before_if($row1['if_id']));
		} else {		// kalkulacija kot spremenljivka
			$vrstni_red = $b->vrstni_red(-$row['cnd_id']) - 1;	// -1 je da ne prikaze se trenutne kalkulacije
		}

        $sql_count = sisplet_query("SELECT COUNT(*) AS count FROM srv_quota WHERE cnd_id='$row[cnd_id]'");
        if (!$sql_count) die();
        $row_count = mysqli_fetch_array($sql_count);


        // operator
        if ($row['vrstni_red'] > 1) {
            echo '<div class="condition_editing_row operator">';

            if ($row['operator']==0)
                echo '<a href="#" onclick="quota_operator_edit(\''.$quota.'\', \'1\'); return false;" style="font-weight:bold; font-size:18px" title="'.$lang['srv_edit_condition_conjunction'].'">&nbsp;+&nbsp;</a>';
            if ($row['operator']==1)
                echo '<a href="#" onclick="quota_operator_edit(\''.$quota.'\', \'2\'); return false;" style="font-weight:bold; font-size:18px" title="'.$lang['srv_edit_condition_conjunction'].'">&nbsp;-&nbsp;</a>';
            if ($row['operator']==2)
                echo '<a href="#" onclick="quota_operator_edit(\''.$quota.'\', \'3\'); return false;" style="font-weight:bold; font-size:18px" title="'.$lang['srv_edit_condition_conjunction'].'">&nbsp;*&nbsp;</a>';
            if ($row['operator']==3)
                echo '<a href="#" onclick="quota_operator_edit(\''.$quota.'\', \'0\'); return false;" style="font-weight:bold; font-size:18px" title="'.$lang['srv_edit_condition_conjunction'].'">&nbsp;/&nbsp;</a>';
                
            echo '</div>';
        }


        // form
        echo '<form name="quota_'.$quota.'" id="quota_'.$quota.'" action="" method="post" onsubmit="quota_edit(\''.$quota.'\'); return false;">'."\n\r";

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

        // move
        echo '<div class="move_holder">';
        if ($row_count['count'] != 1 )
            echo '<span class="faicon move_updown move" title="'.$lang['srv_move'].'"></span>';
        echo '</div>';
        

        // Srednji bel del
        echo '<div class="white_holder">';

        // left_bracket
        echo '<div class="column bracket left">';

		if ($row_count['count'] != 1 || $row['left_bracket']>0 || $row['right_bracket']>0) {

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

			echo '<a href="#" onclick="javascript:quota_bracket_edit_new(\''.$quota.'\', \''.$vrednost.'\', \'left\', \'plus\' ); return false;" title="'.$lang['srv_oklepaj_add'].'"><span class="faicon add"></span></a>';
			
            if ($row['left_bracket'] > 0)
				echo '<a href="#" onclick="javascript:quota_bracket_edit_new(\''.$quota.'\', \''.$vrednost.'\', \'left\', \'minus\'); return false;" title="'.$lang['srv_oklepaj_rem'].'"><span class="faicon delete_circle"></span></a>';
			else
				echo '<span class="faicon delete_circle icon-grey_normal"></span>';

            echo '</div>';    

            echo '<div class="bracket_text">'.$lang['srv_oklepaji'].'</div>';
		} 

		echo '</div>';

        // left_bracket
        echo '<div class="column display_bracket left">';

        for ($i=$row['left_bracket']; $i>0; $i--) {
            echo ' ( ';        
        }

        echo '</div>';

        // spremenljivka
        echo '<div class="column variable">';

        if ($row['spr_id']==0) echo '<span class="red">'.$lang['srv_select_spr'].'!</span>';

        echo '<select name="quota_spremenljivka_'.$quota.'" id="quota_spremenljivka_'.$quota.'" class="dropdown medium" size="1" style="width:150px" onchange="javascript:quota_edit(\''.$quota.'\', \''.$vrednost.'\');">'."\n\r";

        echo '<option value="0"></option>';
		
		// Kvote po statusu
        echo '<option value="-1"'. ($row['spr_id']==-1 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_1'].'</option>';
        echo '<option value="-2"'. ($row['spr_id']==-2 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_2'].'</option>';
        echo '<option value="-3"'. ($row['spr_id']==-3 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_3'].'</option>';
        echo '<option value="-4"'. ($row['spr_id']==-4 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_4'].'</option>';
        echo '<option value="-5"'. ($row['spr_id']==-5 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_5'].'</option>';
        echo '<option value="-6"'. ($row['spr_id']==-6 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_6'].'</option>';
		// Kvota po ustreznih odgovorih
        echo '<option value="-7"'. ($row['spr_id']==-7 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_7'].'</option>';
		// Kvota po vseh odgovorih
        echo '<option value="-8"'. ($row['spr_id']==-8 ?' selected="selected"':'').' style="color: blue">&nbsp;&nbsp;&nbsp; '.$lang['srv_quota_status_8'].'</option>';

        $sql1 = sisplet_query("SELECT s.id, s.gru_id, s.naslov, s.variable, s.tip, g.naslov AS grupa_naslov
                            FROM srv_spremenljivka s, srv_grupa g
                            WHERE g.ank_id='$this->anketa' AND s.gru_id=g.id AND s.tip IN (1, 2, 3, 6, 7, 22, 16, 20, 17, 18)
                            ORDER BY g.vrstni_red ASC, s.vrstni_red ASC");

        if(!$sql1) echo mysqli_error($GLOBALS['connect_db']);

        $prev_grupa = 0;

        while ($row1 = mysqli_fetch_array($sql1)) {

            if ($b->vrstni_red($row1['id']) <= $vrstni_red) {

            	if ($row1['gru_id'] != $prev_grupa) {
					echo '<option value="0" disabled style="font-style: italic;">'.$row1['grupa_naslov'].'</option>';
					$prev_grupa = $row1['gru_id'];
            	}


				// radio, select, checkbox
                if ($row1['tip'] == 1 || $row1['tip'] == 2 || $row1['tip'] == 3) {

                	echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp; ('.$row1['variable'].') '.$row1['naslov'].'</option>';

                    $sql2 = sisplet_query("SELECT id, naslov, variable FROM srv_vrednost WHERE spr_id='$row1[id]' ORDER BY vrstni_red ASC");
                    while ($row2 = mysqli_fetch_array($sql2)) {

                        if ($row2['id'] == $row['vre_id'] && $row['grd_id'] == 0)
                            $selected = ' selected="selected"';
                        else
                            $selected = '';

                        echo '<option value="vre_'.$row2['id'].'"'.$selected.'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - ('.($row1['tip'] == 1 ? $row1['variable'].'_' : '').''.$row2['variable'].') '.$row2['naslov'].'</option>'."\n\r";
                    }
				} 
				// multigrid, multichecbox
				elseif ($row1['tip'] == 6 || $row1['tip'] == 16) {

					echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp; ('.$row1['variable'].') '.$row1['naslov'].'</option>';

                    $sql2 = sisplet_query("SELECT id, naslov, variable FROM srv_vrednost WHERE spr_id='$row1[id]' ORDER BY vrstni_red ASC");
                    while ($row2 = mysqli_fetch_array($sql2)) {

                        echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - ('.$row2['variable'].') '.$row2['naslov'].'</option>'."\n\r";

                        $sql3 = sisplet_query("SELECT id, naslov, variable FROM srv_grid WHERE spr_id='$row1[id]' ORDER BY vrstni_red ASC");
                        while ($row3 = mysqli_fetch_array($sql3)) {

							if ($row1['id']==$row['spr_id'] && $row2['id']==$row['vre_id'] && $row3['id']==$row['grd_id'])
		                        $selected = ' selected="selected"';
		                    else
		                        $selected = '';

		                    echo '<option value="mlti_'.$row1['id'].'_'.$row2['id'].'_'.$row3['id'].'"'.$selected.'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ('.$row3['variable'].') '.$row3['naslov'].'</option>'."\n\r";
                        }
                    }
                } 
				// multinumber
				elseif ($row1['tip'] == 20) {

					/*echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp; ('.$row1['variable'].') '.$row1['naslov'].'</option>';

                    $sql2 = sisplet_query("SELECT * FROM srv_vrednost WHERE spr_id='$row1[id]' ORDER BY vrstni_red ASC");
                    while ($row2 = mysqli_fetch_array($sql2)) {

                        echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - ('.$row2['variable'].') '.$row2['naslov'].'</option>'."\n\r";

                        $sql3 = sisplet_query("SELECT * FROM srv_grid WHERE spr_id='$row1[id]' ORDER BY vrstni_red ASC");
                        while ($row3 = mysqli_fetch_array($sql3)) {

							if ($row1['id']==$row['spr_id'] && $row2['id']==$row['vre_id'] && $row3['id']==$row['grd_id'])
		                        $selected = ' selected="selected"';
		                    else
		                        $selected = '';

		                    echo '<option value="mlti_'.$row1['id'].'_'.$row2['id'].'_'.$row3['id'].'"'.$selected.'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ('.$row3['variable'].') '.$row3['naslov'].'</option>'."\n\r";
                        }
                    }*/
                } 
				// number
				elseif ($row1['tip'] == 7) {

					/*echo '<option value="0" disabled>&nbsp;&nbsp;&nbsp; ('.$row1['variable'].') '.$row1['naslov'].'</option>'."\n\r";

					// number ima lahko dva polja
					for ($i=0; $i<$row1['size']; $i++) {

						if ($row1['id'] == $row['spr_id'] && $i == $row['grd_id'])
	                        $selected = ' selected="selected"';
	                    else
	                        $selected = '';

	                    echo '<option value="num_'.$row1['id'].'_'.$i.'"'.$selected.'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - ('.($i+1).') '.($i+1).'. '.$lang['srv_field'].'</option>'."\n\r";
					}*/				
				} 
				// vsi ostali (numericni)
				else {
                    /*if ($row1['id'] == $row['spr_id'])
                        $selected = ' selected="selected"';
                    else
                        $selected = '';

                    echo '<option value="'.$row1['id'].'"'.$selected.'>&nbsp;&nbsp;&nbsp; ('.$row1['variable'].') '.$row1['naslov'].'</option>'."\n\r";*/
                }
            }
        }

        echo '</select>';

        echo '</div>';


        // right_bracket
        echo '<div class="column display_bracket right">';

        for ($i=$row['right_bracket']; $i>0; $i--) {
            echo ' ) ';
        }

        echo '</div>';


        // right_bracket buttons
        echo '<div class="column bracket right">';

		if ($row_count['count'] != 1 || $row['right_bracket']>0 || $row['left_bracket']>0) {

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

			if ($row['right_bracket'] > 0)
				echo '<a href="#" onclick="javascript:quota_bracket_edit_new(\''.$quota.'\', \''.$vrednost.'\', \'right\', \'minus\'); return false;" title="'.$lang['srv_zaklepaj_rem'].'"><span class="faicon delete_circle"></span></a>';
			else
				echo '<span class="faicon delete_circle icon-grey_normal"></span>';

			echo '<a href="#" onclick="javascript:quota_bracket_edit_new(\''.$quota.'\', \''.$vrednost.'\', \'right\', \'plus\' ); return false;" title="'.$lang['srv_zaklepaj_add'].'"><span class="faicon add"></span></a>';

            echo '</div>';

            echo '<div class="bracket_text">'.$lang['srv_zaklepaji'].'</div>';
		} 

		echo '</div>';

		echo '</div>';


        // remove
        echo '<div class="delete_holder">';

        $sql3 = sisplet_query("SELECT id FROM srv_quota WHERE cnd_id='$row[cnd_id]'");
        if (mysqli_num_rows($sql3) != 1 )
            echo ' <a href="#" onclick="quota_remove(\''.$row['cnd_id'].'\', \''.$quota.'\', \''.$vrednost.'\'); return false;" title="'.$lang['srv_if_rem'].'"><span class="faicon delete icon-grey_dark_link delte-if-block"></span></a>'."\n\r";
        
        echo '</div>';


        echo '</div>';

        echo '</form>'."\n\r";
    }

	 /**
    * @desc preveri ali so oklepaji pravilno postavljeni v kalkulacijah (presteje predoklepaje in zaklepaje)
    * in pa ce je izbrana spremenljivka
    */
    private function quota_check ($condition) {

        $sql = sisplet_query("SELECT spr_id, left_bracket, right_bracket FROM srv_quota WHERE cnd_id='$condition' ORDER BY vrstni_red");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);

		if (mysqli_num_rows($sql) == 0) return 1;	// ni sploh se izbrana spremenljivka (ker ni quota sploh se inicializiran z default vrstico)

        $bracket = 0;
        while ($row = mysqli_fetch_array($sql)) {
        	if ($row['spr_id'] == 0)	// ce ni izbrana spremenljivka v eni od vrstic
        		return 2;

            $bracket = $bracket + $row['left_bracket'] - $row['right_bracket'];
            if ($bracket < 0)
                return 3;		// oklepaj
        }

        if ($bracket == 0)
            return 0;			// vse ok
        else
            return 3;			// zaklepaj
    }


	
	public function ajax() {
		global $global_user_id;
		global $lang;
		global $site_path;
			
		if (isset($_POST['anketa'])) $this->anketa = $_POST['anketa'];
        if (isset($_POST['spremenljivka'])) $this->spremenljivka = $_POST['spremenljivka'];
		
		$ajax = 'ajax_' . $_GET['a'];
		
		if ( method_exists('SurveyQuotas', $ajax) )
			$this->$ajax();
		else
			echo 'method '.$ajax.' does not exist';
	}
	
	private function ajax_quota_editing () {
    	if ($_POST['noupdate'] != 1) {
        	Common::updateEditStamp();
		}
		
        $condition = $_POST['condition'];
        $vrednost = $_POST['vrednost'];
        
        $sql = sisplet_query("SELECT vre_id FROM srv_condition WHERE id = '$condition'");
        $row = mysqli_fetch_array($sql);
        
        $quota = $this->quota_editing($condition, $vrednost);
        
        if ($row['vre_id'] == 0) {
            $s = sisplet_query("UPDATE srv_condition SET vre_id='$quota' WHERE id='$condition'");
            if (!$s) echo mysqli_error($GLOBALS['connect_db']);
        }
    }
    
    private function ajax_quota_editing_close () {
        
        $condition = $_POST['condition'];
        $vrednost = $_POST['vrednost'];
        
        $sql = sisplet_query("SELECT if_id FROM srv_condition WHERE id = '$condition'");
        $row = mysqli_fetch_array($sql);
        
        $b = new Branching($this->anketa);
        if ($condition >= 0)
        	$b->condition_editing($row['if_id'], $vrednost);
        else {
        	$row = SurveyInfo::getInstance()->getSurveyRow();
        	if ($row['expanded'] == 1) {
				$b->vprasanje(-$condition);
			} else {
        		$b->spremenljivka_name(-$condition);
			}
		}
    }
    
    private function ajax_quota_save () {
    	if ($_POST['noupdate'] != 1) {
    		Common::updateEditStamp();
		}
		
        $quota = $_POST['quota'];
        $expression = $_POST['expression'];
        
        sisplet_query("UPDATE srv_quota SET expression='$expression' WHERE id = '$quota'");
    }
    
    private function ajax_quota_add () {
    	if ($_POST['noupdate'] != 1) {
    		Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
        $condition = $_POST['condition'];
        $operator = $_POST['operator'];
        $vrednost = $_POST['vrednost'];
        
        $sql = sisplet_query("SELECT MAX(vrstni_red) AS max FROM srv_quota WHERE cnd_id = '$condition'");
        $row = mysqli_fetch_array($sql);
        $vrstni_red = $row['max'] + 1;

        $s = sisplet_query("INSERT INTO srv_quota (id, cnd_id, operator, vrstni_red) VALUES ('', '$condition', '$operator', '$vrstni_red')");
        if (!$s) echo mysqli_error($GLOBALS['connect_db']);
        
        $this->quota_editing_inner($condition, $vrednost);
    }
    
    private function ajax_quota_operator_edit () {
		if ($_POST['noupdate'] != 1) {
			Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
		$quota = $_POST['quota'];
		$operator = $_POST['operator'];
		
        sisplet_query("UPDATE srv_quota SET operator='$operator' WHERE id = '$quota'");

        $sql = sisplet_query("SELECT cnd_id FROM srv_quota WHERE id = '$quota'");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
        $row = mysqli_fetch_array($sql);

        $this->quota_editing_inner($row['cnd_id'], $_POST['vrednost']);
    }
    
    private function ajax_quota_edit () {
    	if ($_POST['noupdate'] != 1) {
    		Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
        $spremenljivka = $_POST['spremenljivka'];
        $quota = $_POST['quota'];
        $value = $_POST['value'];
        $vrednost = $_POST['vrednost'];
        $grd_id = 0;
        
        // obicna spremenljivka
        if (is_numeric($spremenljivka)) {
            $spr_id = $spremenljivka;
            $vre_id = 0;
            
        // checkbox, multigrid
        } elseif ( strpos($spremenljivka, 'vre_') !== false ) {
            $e = explode('_', $spremenljivka);
			list( , $vre_id, $grd_id) = $e;
            $sql2 = sisplet_query("SELECT spr_id FROM srv_vrednost WHERE id = '$vre_id'");
            $row2 = mysqli_fetch_array($sql2);
            $spr_id = $row2['spr_id'];
        
        // number
		} elseif ( strpos($spremenljivka, 'num_') !== false ) {
        	
        	$e = explode('_', $spremenljivka);
			list( , $spr_id, $grd_id) = $e;
        	$vre_id = 0;
        	
        // multicheckbox, multinumber
        } else {
			$e = explode('_', $spremenljivka);
			list( , $spr_id, $vre_id, $grd_id) = $e;
        }
        
        if (!is_numeric($value)) $value = 0;
        
        $s = sisplet_query("UPDATE srv_quota SET spr_id='$spr_id', vre_id='$vre_id', grd_id='$grd_id', value='$value' WHERE id = '$quota'");
        if (!$s) echo mysqli_error($GLOBALS['connect_db']);
        
        $sql = sisplet_query("SELECT cnd_id FROM srv_quota WHERE id = '$quota'");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
        $row = mysqli_fetch_array($sql);

        $this->quota_editing_inner($row['cnd_id'], $vrednost);
    }

	private function ajax_quota_value_edit (){
		
		if ($_POST['noupdate'] != 1) {
    		Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
		if(isset($_POST['spremenljivka']) && $_POST['spremenljivka'] > 0){
			$spremenljivka = $_POST['spremenljivka'];
			$value = $_POST['value'];
			
			$s = sisplet_query("UPDATE srv_spremenljivka SET vsota_limit='$value' WHERE id = '$spremenljivka'");
			if (!$s) echo mysqli_error($GLOBALS['connect_db']);
		}
	}
    
    private function ajax_quota_remove () {
    	if ($_POST['noupdate'] != 1) {
    		Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
        $quota = $_POST['quota'];
        $vrednost = $_POST['vrednost'];

        sisplet_query("DELETE FROM srv_quota WHERE id='$quota'");

        $b = new Branching($this->anketa);
        $b->repare_calculation($_POST['condition']);

        $this->quota_editing_inner($_POST['condition'], $vrednost);
    }
    
    private function ajax_quota_bracket_edit_new () {
    	if ($_POST['noupdate'] != 1) {
    		Common::getInstance()->Init($this->anketa);
    		Common::getInstance()->updateEditStamp();
		}
		
        $who = $_POST['who'];
        $what = $_POST['what'];
        $left_bracket = $_POST['left_bracket'];
        $quota = $_POST['quota'];

        if ($who == 'left')
        	if ($what == 'plus')
        		$bracket = 'left_bracket=left_bracket+1';
        	else
        		$bracket = 'left_bracket=left_bracket-1';
        else
        	if ($what == 'plus')
        		$bracket = 'right_bracket=right_bracket+1';
        	else
        		$bracket = 'right_bracket=right_bracket-1';
        		
        
        sisplet_query("UPDATE srv_quota SET $bracket WHERE id = '$quota'");

        $sql = sisplet_query("SELECT cnd_id FROM srv_quota WHERE id = '$quota'");
        if (!$sql) echo mysqli_error($GLOBALS['connect_db']);
        $row = mysqli_fetch_array($sql);

        $this->quota_editing_inner($row['cnd_id'], $_POST['vrednost']);
    }
    	
	private function ajax_quota_sort() {
		Common::getInstance()->Init($this->anketa);
    	Common::getInstance()->updateEditStamp();
    	
    	$condition = $_POST['condition'];
    	$sortable = $_POST['sortable'];
    	$sortable = explode('&', $sortable);
    	
    	$i=1;
    	foreach ($sortable AS $calc) {
			$quota = explode('=', $calc);
			$quota = $quota[1];
			
			$s = sisplet_query("UPDATE srv_quota SET vrstni_red='{$i}' WHERE id='{$quota}'");
			if (!$s) echo mysqli_error($GLOBALS['connect_db']);

			$i++;
    	}
    	
		$b = new Branching($this->anketa);
		$b->repare_condition($if);
		$this->quota_editing_inner($condition);
	}
	
	private function ajax_quota_edit_variable () {
		Common::updateEditStamp();
		
		if(isset($_POST['spremenljivka']) && $_POST['spremenljivka'] > 0){
			$spremenljivka = $_POST['spremenljivka'];
			$variable = $_POST['variable'];
			
			// preverimo, da ni se kje drugje v anekti tako ime spremenljivke
			$sqlv = sisplet_query("SELECT s.variable FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='$this->anketa'");
			$ok = true;
			while ($rowv = mysqli_fetch_array($sqlv)) {
				if ($rowv['variable'] == $variable) 
					$ok = false;
			}

			// Ce se ime ze pojavi v anketi mu dodamo stevilko
			if(!$ok){
				$ok = false;
				$i = 2;
				while(!$ok){				
					$ok = true;
					$variable = $_POST['variable'].'_'.$i;
					
					$sqlv = sisplet_query("SELECT s.variable, s.id as id FROM srv_spremenljivka s, srv_grupa g WHERE s.gru_id=g.id AND g.ank_id='$this->anketa'");
					while ($rowv = mysqli_fetch_array($sqlv)) {
						if ($rowv['variable'] == $variable && $spremenljivka != $rowv['id']){
							$ok = false;
							$i++;
						}
					}
				}
			}
			
			sisplet_query("UPDATE srv_spremenljivka SET variable='$variable', variable_custom='1' WHERE id = '$spremenljivka'");
		}
	}
	
}
?>