C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\limesurvey\admin\quota.php


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
<?php
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id: quota.php 6621 2009-04-11 16:09:48Z jcleeland $
*/

include_once("login_check.php");  //Login Check dies also if the script is started directly

function getQuotaAnswers($qid,$surveyid,$quota_id)
{
    global 
$clang;
    
$baselang GetBaseLanguageFromSurveyID($surveyid);
    
$query "SELECT type, title FROM ".db_table_name('questions')." WHERE qid='{$qid}' AND language='{$baselang}'";
    
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());
    
$qtype $result->FetchRow();

    if (
$qtype['type'] == 'G')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";

        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$answerlist = array('M' => array('Title' => $qtype['title'], 'Display' => $clang->gT("Male"), 'code' => 'M'),
        
'F' => array('Title' => $qtype['title'],'Display' => $clang->gT("Female"), 'code' => 'F'));

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }
    
    if (
$qtype['type'] == 'M')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$query "SELECT code,answer FROM ".db_table_name('answers')." WHERE qid='{$qid}'";
        
$ansresult db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
        
$answerlist = array();
        
        while (
$dbanslist $ansresult->FetchRow())
        {
            
$tmparrayans = array('Title' => $qtype['title'], 'Display' => substr($dbanslist['answer'],0,40), 'code' => $dbanslist['code']);
            
$answerlist[$dbanslist['code']]    = $tmparrayans;
        }

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }
    
    if (
$qtype['type'] == 'L' || $qtype['type'] == 'O' || $qtype['type'] == '!')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
        
$query "SELECT code,answer FROM ".db_table_name('answers')." WHERE qid='{$qid}'";
        
$ansresult db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
        
$answerlist = array();
        
        while (
$dbanslist $ansresult->FetchRow())
        {
            
$answerlist[$dbanslist['code']] = array('Title'=>$qtype['title'],
                                                          
'Display'=>substr($dbanslist['answer'],0,40),
                                                          
'code'=>$dbanslist['code']);
        }

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }

    }

    if (
$qtype['type'] == 'A')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$query "SELECT code,answer FROM ".db_table_name('answers')." WHERE qid='{$qid}'";
        
$ansresult db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
        
$answerlist = array();
        
        while (
$dbanslist $ansresult->FetchRow())
        {
            for (
$x=1$x<6$x++)
            {
                
$tmparrayans = array('Title' => $qtype['title'], 'Display' => substr($dbanslist['answer'],0,40).' ['.$x.']''code' => $dbanslist['code']);
                
$answerlist[$dbanslist['code']."-".$x]    = $tmparrayans;
            }
        }

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }
    
    if (
$qtype['type'] == 'B')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$query "SELECT code,answer FROM ".db_table_name('answers')." WHERE qid='{$qid}'";
        
$ansresult db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
        
$answerlist = array();
        
        while (
$dbanslist $ansresult->FetchRow())
        {
            for (
$x=1$x<11$x++)
            {
                
$tmparrayans = array('Title' => $qtype['title'], 'Display' => substr($dbanslist['answer'],0,40).' ['.$x.']''code' => $dbanslist['code']);
                
$answerlist[$dbanslist['code']."-".$x]    = $tmparrayans;
            }
        }

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }
    
    if (
$qtype['type'] == 'Y')
    {
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";

        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$answerlist = array('Y' => array('Title' => $qtype['title'], 'Display' => $clang->gT("Yes"), 'code' => 'Y'),
        
'N' => array('Title' => $qtype['title'],'Display' => $clang->gT("No"), 'code' => 'N'));

        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }

    if (
$qtype['type'] == 'I')
    {
        
        
$slangs GetAdditionalLanguagesFromSurveyID($surveyid);
        
array_unshift($slangs,$baselang);
        
        
$query "SELECT * FROM ".db_table_name('quota_members')." WHERE sid='{$surveyid}' and qid='{$qid}' and quota_id='{$quota_id}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        while(list(
$key,$value) = each($slangs))
        {
            
$tmparrayans = array('Title' => $qtype['title'], 'Display' => getLanguageNameFromCode($value,false), $value);
            
$answerlist[$value]    = $tmparrayans;
        }
        
        if (
$result->RecordCount() > 0)
        {
            while (
$quotalist $result->FetchRow())
            {
                
$answerlist[$quotalist['code']]['rowexists'] = '1';
            }

        }
    }
    
    if (!isset(
$answerlist))
    {
        return array();
    }
    else
    {
        return 
$answerlist;
    }
}

if(
$sumrows5['edit_survey_property'] || $_SESSION['USER_RIGHT_SUPERADMIN'] == 1)
{
    if (isset(
$_POST['quotamax'])) $_POST['quotamax']=sanitize_int($_POST['quotamax']);
    if (!isset(
$action)) $action=returnglobal('action');
    if (!isset(
$subaction)) $subaction=returnglobal('subaction');
    if (!isset(
$quotasoutput)) $quotasoutput "";
    if(
$subaction == "insertquota")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "INSERT INTO ".db_table_name('quota')." (sid,name,qlimit,action) VALUES ('$surveyid','{$_POST['quota_name']}','{$_POST['quota_limit']}','{$_POST['quota_action']}')";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());
        
$viewquota "1";

    }

    if(
$subaction == "modifyquota")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "UPDATE ".db_table_name('quota')." SET name='{$_POST['quota_name']}', qlimit='{$_POST['quota_limit']}', action='{$_POST['quota_action']}' where id='{$_POST['quota_id']}' ";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());
        
$viewquota "1";

    }
    
    if(
$subaction == "insertquotaanswer")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "INSERT INTO ".db_table_name('quota_members')." (sid,qid,quota_id,code) VALUES ('$surveyid','{$_POST['quota_qid']}','{$_POST['quota_id']}','{$_POST['quota_anscode']}')";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());
        
$viewquota "1";

    }

    if(
$subaction == "quota_delans")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "DELETE FROM ".db_table_name('quota_members')." WHERE id = '{$_POST['quota_member_id']}' AND qid='{$_POST['quota_qid']}' and code='{$_POST['quota_anscode']}'";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());
        
$viewquota "1";

    }


    if(
$subaction == "quota_delquota")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "DELETE FROM ".db_table_name('quota')." WHERE id='{$_POST['quota_id']}'";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());

        
$query "DELETE FROM ".db_table_name('quota_members')." WHERE quota_id='{$_POST['quota_id']}'";
        
$connect->Execute($query) or safe_die($connect->ErrorMsg());
        
$viewquota "1";

    }

    if (
$subaction == "quota_editquota")
    {
        
$_POST  array_map('db_quote'$_POST);
        
$query "SELECT * FROM ".db_table_name('quota')." where id='{$_POST['quota_id']}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        
$quotainfo $result->FetchRow();
        
        
$quotasoutput .='<form action="'.$scriptname.'" method="post">
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F8F8FF">
                          <tr>
                            <td valign="top">
                                <table width="100%" border="0">
                                    <tbody>
                                          <tr> 
                                            <td colspan="2" class="header">'
.$clang->gT("Modify Quota").'</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Name").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"> <input name="quota_name" type="text" size="30" maxlength="255" value="'
.$quotainfo['name'].'"></td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Limit").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"><input name="quota_limit" type="text" size="12" maxlength="8" value="'
.$quotainfo['qlimit'].'"></td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Action").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"> <select name="quota_action">
                                                <option value ="1" '
;
                                                if(
$quotainfo['action'] == 1$quotasoutput .= "selected";
                                                
$quotasoutput .='>'.$clang->gT("Terminate Survey") .'</option> 
                                                <option value ="2" '
;
                                                if(
$quotainfo['action'] == 2$quotasoutput .= "selected";
                                                
$quotasoutput .= '>'.$clang->gT("Terminate Survey With Warning") .'</option>
                                                </select></td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td>&nbsp;</td>
                                            <td><table width="30%"><tr><td align="left"><input name="submit" type="submit" value="'
.$clang->gT("Update Quota").'" />
                                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                                            <input type="hidden" name="action" value="quotas" />
                                            <input type="hidden" name="subaction" value="modifyquota" />
                                            <input type="hidden" name="quota_id" value="'
.$quotainfo['id'].'" />
                                            </form></td><td>
                                            <form action="'
.$scriptname.'" method="post">
                                            <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Cancel").'">
                                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                                            <input type="hidden" name="action" value="quotas" />
                                            </form></td></tr></table></td>
                                          </tr>
                                   </tbody>
                              </table>
                        </td>
                      </tr>
                </table>'
;
    }
    
    
$totalquotas=0;
    
$totalcompleted=0;
    
$csvoutput=array();
    if ((
$action == "quotas" && !isset($subaction)) || isset($viewquota))
    {

        
$query "SELECT * FROM ".db_table_name('quota')." where sid='".$surveyid."'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());

        
$quotasoutput .='<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F8F8FF">
                          <tr>
                        <td valign="top">
                        <table width="100%" border="0">
                           <tbody>
                          <tr> 
                        <td colspan="6" class="header">'
.$clang->gT("Survey Quotas").'</td>
                          </tr>
                          <tr> 
                            <th width="20%">'
.$clang->gT("Quota Name").'</th>
                            <th width="20%">'
.$clang->gT("Status").'</th>
                            <th width="30%">'
.$clang->gT("Quota Action").'</th>
                            <th width="5%">'
.$clang->gT("Limit").'</th>
                            <th width="5%">'
.$clang->gT("Completed").'</th>
                            <th width="20%">'
.$clang->gT("Action").'</th>
                          </tr>'
;

        if (
$result->RecordCount() > 0)
        {

            while (
$quotalisting $result->FetchRow())
            {
                
$quotasoutput .='<tr>
                    <td align="center"><a name="quota_'
.$quotalisting['id'].'">'.$quotalisting['name'].'</a></td>
                    <td align="center">'
;
                if (
$quotalisting['active'] == 1)
                {
                    
$quotasoutput .= '<font color="#48B150">'.$clang->gT("Active").'</font>';
                } else {
                    
$quotasoutput .= '<font color="#B73838">'.$clang->gT("Not Active").'</font>';
                }
                
$quotasoutput .='</td>
                    <td align="center">'
;
                if (
$quotalisting['action'] == 1)
                {
                    
$quotasoutput .= $clang->gT("Terminate Survey");
                } elseif (
$quotalisting['action'] == 2) {
                    
$quotasoutput .= $clang->gT("Terminate Survey With Warning");
                }
                
$totalquotas+=$quotalisting['qlimit'];
                
$completed=get_quotaCompletedCount($surveyid$quotalisting['id']);
                
$highlight=($completed >= $quotalisting['qlimit']) ? "" "style='color: red'"//Incomplete quotas displayed in red
                
$totalcompleted=$totalcompleted+$completed;
                
$csvoutput[]=$quotalisting['name'].",".$quotalisting['qlimit'].",".$completed.",".($quotalisting['qlimit']-$completed)."\r\n";

                
$quotasoutput .='</td>
                    <td align="center">'
.$quotalisting['qlimit'].'</td>
                    <td align="center" '
.$highlight.'>'.$completed.'</td>
                    <td align="center" style="padding: 3px;">
                    <table width="100%"><tr><td align="center">
                    <form action="'
.$scriptname.'" method="post">
                        <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Modify").'">
                        <input type="hidden" name="sid" value="'
.$surveyid.'" />
                        <input type="hidden" name="action" value="quotas" />
                        <input type="hidden" name="quota_id" value="'
.$quotalisting['id'].'" />
                        <input type="hidden" name="subaction" value="quota_editquota" />
                    </form></td><td>
                    <form action="'
.$scriptname.'" method="post">
                        <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Remove").'">
                        <input type="hidden" name="sid" value="'
.$surveyid.'" />
                        <input type="hidden" name="action" value="quotas" />
                        <input type="hidden" name="quota_id" value="'
.$quotalisting['id'].'" />
                        <input type="hidden" name="subaction" value="quota_delquota" />
                    </form></td></tr></table>
                    </td>
                  </tr>
                  <tr class="evenrow"> 
                       <td align="center">&nbsp;</td>
                    <td align="center"><strong>'
.$clang->gT("Questions").'</strong></td>
                    <td align="center"><strong>'
.$clang->gT("Answers").'</strong></td>
                    <td align="center">&nbsp;</td>
                    <td align="center">&nbsp;</td>
                    <td style="padding: 3px;" align="center"><form action="'
.$scriptname.'" method="post">
                            <input name="submit" type="submit" id="quota_new" value="'
.$clang->gT("Add Answer").'">
                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                            <input type="hidden" name="action" value="quotas" />
                            <input type="hidden" name="quota_id" value="'
.$quotalisting['id'].'" />
                            <input type="hidden" name="subaction" value="new_answer" /></form></td>
                  </tr>'
;

                
$query "SELECT id,code,qid FROM ".db_table_name('quota_members')." where quota_id='".$quotalisting['id']."'";
                
$result2 db_execute_assoc($query) or safe_die($connect->ErrorMsg());

                if (
$result2->RecordCount() > 0)
                {
                    while (
$quota_questions $result2->FetchRow())
                    {
                        
$question_answers getQuotaAnswers($quota_questions['qid'],$surveyid,$quotalisting['id']);
                        
$quotasoutput .='<tr class="evenrow">
                        <td align="center">&nbsp;</td>
                        <td align="center">'
.$question_answers[$quota_questions['code']]['Title'].'</td>
                        <td align="center">'
.$question_answers[$quota_questions['code']]['Display'].'</td>
                        <td align="center">&nbsp;</td>
                        <td align="center">&nbsp;</td>
                        <td style="padding: 3px;" align="center">
                        <form action="'
.$scriptname.'" method="post">
                            <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Remove").'">
                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                            <input type="hidden" name="action" value="quotas" />
                            <input type="hidden" name="quota_member_id" value="'
.$quota_questions['id'].'" />
                            <input type="hidden" name="quota_qid" value="'
.$quota_questions['qid'].'" />
                            <input type="hidden" name="quota_anscode" value="'
.$quota_questions['code'].'" />
                            <input type="hidden" name="subaction" value="quota_delans" />
                        </form>
                        </td>
                          </tr>'
;
                    }
                }

            }




        } else
        {
            
$quotasoutput .='<tr>
               <td colspan="6" align="center">'
.$clang->gT("No quotas have been set for this survey").'.</td>
             </tr>'
;
        }

        
$quotasoutput .='<tr>
                            <td align="center"><form action="'
.$scriptname.'" method="post">
                            <input name="submit" type="submit" id="quota_new" value="'
.$clang->gT("Add New Quota").'">
                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                            <input type="hidden" name="action" value="quotas" />
                            <input type="hidden" name="subaction" value="new_quota" /></form></td>
                            <td align="center"><a name="quota_end">&nbsp;</a></td>
                            <td align="center">&nbsp;</td>
                            <td align="center">'
.$totalquotas.'</td>
                            <td align="center">'
.$totalcompleted.'</td>
                            <td align="center" style="padding: 3px;"<input type="button" value="'
.$clang->gT("Quick CSV Report").'" onClick="window.open(\'admin.php?action=quotas&amp;sid='.$surveyid.'&amp;quickreport=y\', \'_top\')"></td>
                              </tr>
                            </tbody>
                              </table>
                            </td>
                             </tr>
                            </table>'
;
    }

    if(isset(
$_GET['quickreport']) && $_GET['quickreport'])
    {
        
header("Content-Disposition: attachment; filename=results-survey".$surveyid.".csv");
        
header("Content-type: text/comma-separated-values; charset=UTF-8");
        echo 
$clang->gT("Quota Name").",".$clang->gT("Limit").",".$clang->gT("Completed").",".$clang->gT("Remaining")."\r\n";
        foreach(
$csvoutput as $line)
        {
          echo 
$line;
        }
    die;
    }
    if(
$subaction == "new_answer" || ($subaction == "new_answer_two" && !isset($_POST['quota_qid'])))
    {
        if (
$subaction == "new_answer_two"$_POST['quota_id'] = $_POST['quota_id'];

        
$allowed_types "(type ='G' or type ='M' or type ='Y' or type ='A' or type ='B' or type ='I' or type = 'L' or type='O' or type='!')";
        
$query "SELECT qid, title, question FROM ".db_table_name('questions')." WHERE $allowed_types AND sid='$surveyid' AND language='{$baselang}'";
        
$result db_execute_assoc($query) or safe_die($connect->ErrorMsg());
        if (
$result->RecordCount() == 0)
        {
            
$quotasoutput .='<table width="100%" border="0">
                                        <tbody>
                                          <tr> 
                                               <td class="header">'
.$clang->gT("Add Answer").': '.$clang->gT("Question Selection").'</td>
                                          </tr>
                                          <tr>
                                          <td align="center"><br />'
.$clang->gT("Sorry there are no supported question types in this survey.").'<br /><br /></td>
                                          </tr>
                                        </tbody>
                                      </table>'
;
        } else
        {
            
$quotasoutput .='<form action="'.$scriptname.'" method="post">
                            <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F8F8FF">
                              <tr>
                                <td valign="top">
                                    <table width="100%" border="0">
                                        <tbody>
                                          <tr> 
                                               <td colspan="2" class="header">'
.$clang->gT("Survey Quota").': '.$clang->gT("Add Answer").'</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="center">&nbsp;</td>
                                            <td align="center">&nbsp;</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td width="21%" align="center" valign="top"><strong>'
.$clang->gT("Select Question").':</strong></td>
                                            <td align="left">
                                            <select name="quota_qid" size="15">'
;

            while (
$questionlisting $result->FetchRow())
            {
                
$quotasoutput .='<option value="'.$questionlisting['qid'].'">'.$questionlisting['title'].': '.strip_tags(substr($questionlisting['question'],0,40)).'</option>';
            }


            
$quotasoutput .='                    </select>
                                             </td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td colspan="2">&nbsp;</td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td>&nbsp;</td>
                                            <td>
                                                <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Next").'">
                                                <input type="hidden" name="sid" value="'
.$surveyid.'" />
                                                <input type="hidden" name="action" value="quotas" />
                                                <input type="hidden" name="subaction" value="new_answer_two" />
                                                <input type="hidden" name="quota_id" value="'
.$_POST['quota_id'].'" />
                                            </td>
                                          </tr>
                                        </tbody>
                                      </table>
                                </td>
                              </tr>
                        </table>'
;
        }
    }

    if(
$subaction == "new_answer_two" && isset($_POST['quota_qid']))
    {
        
$_POST  array_map('db_quote'$_POST);

        
$question_answers getQuotaAnswers($_POST['quota_qid'],$surveyid,$_POST['quota_id']);
        
$x=0;

        foreach (
$question_answers as $qacheck)
        {
            if (isset(
$qacheck['rowexists'])) $x++;
        }

        
reset($question_answers);

        if (
count($question_answers) == $x)
        {
            
$quotasoutput .='<table width="100%" border="0">
                                        <tbody>
                                          <tr> 
                                               <td class="header">'
.$clang->gT("Add Answer").': '.$clang->gT("Question Selection").'</td>
                                          </tr>
                                          <tr>
                                          <td align="center"><br />'
.$clang->gT("All answers are already selected in this quota.").'<br /><br /></td>
                                          </tr>
                                        </tbody>
                                      </table>'
;
        } else
        {
            
$quotasoutput .='<form action="'.$scriptname.'#quota_'.$_POST['quota_id'].'" method="post">
                             <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F8F8FF">
                              <tr>
                                <td valign="top">
                                    <table width="100%" border="0">
                                        <tbody>
                                          <tr> 
                                               <td colspan="2" class="header">'
.$clang->gT("Survey Quota").': '.$clang->gT("Add Answer").'</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="center">&nbsp;</td>
                                            <td align="center">&nbsp;</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td width="21%" align="center" valign="top"><strong>'
.$clang->gT("Select Answer").':</strong></td>
                                            <td align="left">
                                            <select name="quota_anscode" size="15">'
;

            while (list(
$key,$value) = each($question_answers))
            {
                if (!isset(
$value['rowexists'])) $quotasoutput .='<option value="'.$key.'">'.strip_tags(substr($value['Display'],0,40)).'</option>';
            }


            
$quotasoutput .='                    </select>
                                             </td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td colspan="2">&nbsp;</td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td>&nbsp;</td>
                                            <td>
                                                <input name="submit" type="submit" id="submit" value="'
.$clang->gT("Next").'">
                                                <input type="hidden" name="sid" value="'
.$surveyid.'" />
                                                <input type="hidden" name="action" value="quotas" />
                                                <input type="hidden" name="subaction" value="insertquotaanswer" />
                                                <input type="hidden" name="quota_qid" value="'
.$_POST['quota_qid'].'" />
                                                <input type="hidden" name="quota_id" value="'
.$_POST['quota_id'].'" />
                                            </td>
                                          </tr>
                                        </tbody>
                                      </table>
                                </td>
                              </tr>
                        </table>'
;
        }
    }

    if (
$subaction == "new_quota")
    {
        
$quotasoutput .='<form action="'.$scriptname.'#quota_end" method="post">
                    <table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#F8F8FF">
                          <tr>
                            <td valign="top">
                                <table width="100%" border="0">
                                    <tbody>
                                          <tr> 
                                            <td colspan="2" class="header">'
.$clang->gT("New Quota").'</td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Name").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"> <input name="quota_name" type="text" size="30" maxlength="255"></td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Limit").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"><input name="quota_limit" type="text" size="12" maxlength="8"></td>
                                          </tr>
                                          <tr class="evenrow"> 
                                            <td align="right"><blockquote> 
                                                <p><strong>'
.$clang->gT("Quota Action").':</strong></p>
                                                  </blockquote></td>
                                            <td align="left"> <select name="quota_action">
                                                <option value ="1">'
.$clang->gT("Terminate Survey") .'</option> 
                                                <option value ="2">'
.$clang->gT("Terminate Survey With Warning") .'</option>
                                                </select></td>
                                          </tr>
                                          <tr align="left" class="evenrow"> 
                                            <td>&nbsp;</td>
                                            <td><input name="submit" type="submit" value="'
.$clang->gT("Add New Quota").'" />
                                            <input type="hidden" name="sid" value="'
.$surveyid.'" />
                                            <input type="hidden" name="action" value="quotas" />
                                            <input type="hidden" name="subaction" value="insertquota" />
                                            </td>
                                            
                                            </form>
                                          </tr>
                                   </tbody>
                              </table>
                        </td>
                      </tr>
                </table>'
;
    }
}

?>