forked from tacman/LimeSurveyOfficial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_functions.php
7287 lines (6585 loc) · 265 KB
/
common_functions.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
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
<?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: common_functions.php 12418 2012-02-09 11:54:10Z mennodekker $
* Files Purpose: lots of common functions
*/
if (version_compare(PHP_VERSION,'5.1.2')<0)
{
die('Your PHP version is outdated. LimeSurvey needs PHP 5.2 or later.');
}
require_once('replacements.php');
/**
* This function gives back an array that defines which survey permissions and what part of the CRUD+Import+Export subpermissions is available.
* - for example it would not make sense to have a 'create' permissions for survey locale settings as they exist with every survey
* so the editor for survey permission should not show a checkbox here, therfore the create element of that permission is set to 'false'
* If you want to generally add a new permission just add it here.
*
*/
function aGetBaseSurveyPermissions()
{
global $clang;
$aPermissions=array(
'assessments'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>false,'export'=>false,'title'=>$clang->gT("Assessments"),'description'=>$clang->gT("Permission to create/view/update/delete assessments rules for a survey"),'img'=>'assessments'), // Checked
'quotas'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>false,'export'=>false,'title'=>$clang->gT("Quotas"),'description'=>$clang->gT("Permission to create/view/update/delete quota rules for a survey"),'img'=>'quota'), // Checked
'responses'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>true,'export'=>true,'title'=>$clang->gT("Responses"),'description'=>$clang->gT("Permission to create(data entry)/view/update/delete/import/export responses"),'img'=>'browse'),
'statistics'=>array('create'=>false,'read'=>true,'update'=>false,'delete'=>false,'import'=>false,'export'=>false,'title'=>$clang->gT("Statistics"),'description'=>$clang->gT("Permission to view statistics"),'img'=>'statistics'), //Checked
'survey'=>array('create'=>false,'read'=>true,'update'=>false,'delete'=>true,'import'=>false,'export'=>false,'title'=>$clang->gT("Survey deletion"),'description'=>$clang->gT("Permission to delete a survey"),'img'=>'delete'), //Checked
'surveyactivation'=>array('create'=>false,'read'=>false,'update'=>true,'delete'=>false,'import'=>false,'export'=>false,'title'=>$clang->gT("Survey activation"),'description'=>$clang->gT("Permission to activate/deactivate a survey"),'img'=>'activate_deactivate'), //Checked
'surveycontent'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>true,'export'=>true,'title'=>$clang->gT("Survey content"),'description'=>$clang->gT("Permission to create/view/update/delete/import/export the questions, groups, answers & conditions of a survey"),'img'=>'add'),
'surveylocale'=>array('create'=>false,'read'=>true,'update'=>true,'delete'=>false,'import'=>false,'export'=>false,'title'=>$clang->gT("Survey locale settings"),'description'=>$clang->gT("Permission to view/update the survey locale settings"),'img'=>'edit'),
'surveysecurity'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>false,'export'=>false,'title'=>$clang->gT("Survey security"),'description'=>$clang->gT("Permission to modify survey security settings"),'img'=>'survey_security'),
'surveysettings'=>array('create'=>false,'read'=>true,'update'=>true,'delete'=>false,'import'=>false,'export'=>false,'title'=>$clang->gT("Survey settings"),'description'=>$clang->gT("Permission to view/update the survey settings including token table creation"),'img'=>'survey_settings'),
'tokens'=>array('create'=>true,'read'=>true,'update'=>true,'delete'=>true,'import'=>true,'export'=>true,'title'=>$clang->gT("Tokens"),'description'=>$clang->gT("Permission to create/update/delete/import/export token entries"),'img'=>'tokens'),
'translations'=>array('create'=>false,'read'=>true,'update'=>true,'delete'=>false,'import'=>false,'export'=>false,'title'=>$clang->gT("Quick translation"),'description'=>$clang->gT("Permission to view & update the translations using the quick-translation feature"),'img'=>'translate')
);
uasort($aPermissions,"aComparePermission");
return $aPermissions;
}
/**
* Simple function to sort the permissions by title
*
* @param mixed $aPermissionA Permission A to compare
* @param mixed $aPermissionB Permission B to compare
*/
function aComparePermission($aPermissionA,$aPermissionB)
{
if($aPermissionA['title'] >$aPermissionB['title']) {
return 1;
}
else {
return -1;
}
}
/**
* getqtypelist() Returns list of question types available in LimeSurvey. Edit this if you are adding a new
* question type
*
* @global string $publicurl
* @global string $sourcefrom
*
* @param string $SelectedCode Value of the Question Type (defaults to "T")
* @param string $ReturnType Type of output from this function (defaults to selector)
*
* @return depending on $ReturnType param, returns a straight "array" of question types, or an <option></option> list
*
* Explanation of questiontype array:
*
* description : Question description
* subquestions : 0= Does not support subquestions x=Number of subquestion scales
* answerscales : 0= Does not need answers x=Number of answer scales (usually 1, but e.g. for dual scale question set to 2)
* assessable : 0=Does not support assessment values when editing answerd 1=Support assessment values
*/
function getqtypelist($SelectedCode = "T", $ReturnType = "selector")
{
global $publicurl;
global $sourcefrom, $clang;
if (!isset($clang))
{
$clang = new limesurvey_lang("en");
}
$group['Arrays'] = $clang->gT('Arrays');
$group['MaskQuestions'] = $clang->gT("Mask questions");
$group['SinChoiceQues'] = $clang->gT("Single choice questions");
$group['MulChoiceQues'] = $clang->gT("Multiple choice questions");
$group['TextQuestions'] = $clang->gT("Text questions");
$qtypes = array(
"1"=>array('description'=>$clang->gT("Array dual scale"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'assessable'=>1,
'hasdefaultvalues'=>0,
'answerscales'=>2),
"5"=>array('description'=>$clang->gT("5 Point Choice"),
'group'=>$group['SinChoiceQues'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"A"=>array('description'=>$clang->gT("Array (5 Point Choice)"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>0),
"B"=>array('description'=>$clang->gT("Array (10 Point Choice)"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>0),
"C"=>array('description'=>$clang->gT("Array (Yes/No/Uncertain)"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>0),
"D"=>array('description'=>$clang->gT("Date"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"E"=>array('description'=>$clang->gT("Array (Increase/Same/Decrease)"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>0),
"F"=>array('description'=>$clang->gT("Array"),
'group'=>$group['Arrays'],
'subquestions'=>1,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>1),
"G"=>array('description'=>$clang->gT("Gender"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"H"=>array('description'=>$clang->gT("Array by column"),
'group'=>$group['Arrays'],
'hasdefaultvalues'=>0,
'subquestions'=>1,
'assessable'=>1,
'answerscales'=>1),
"I"=>array('description'=>$clang->gT("Language Switch"),
'group'=>$group['MaskQuestions'],
'hasdefaultvalues'=>0,
'subquestions'=>0,
'assessable'=>0,
'answerscales'=>0),
"K"=>array('description'=>$clang->gT("Multiple Numerical Input"),
'group'=>$group['MaskQuestions'],
'hasdefaultvalues'=>1,
'subquestions'=>1,
'assessable'=>1,
'answerscales'=>0),
"L"=>array('description'=>$clang->gT("List (Radio)"),
'group'=>$group['SinChoiceQues'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>1,
'answerscales'=>1),
"M"=>array('description'=>$clang->gT("Multiple choice"),
'group'=>$group['MulChoiceQues'],
'subquestions'=>1,
'hasdefaultvalues'=>1,
'assessable'=>1,
'answerscales'=>0),
"N"=>array('description'=>$clang->gT("Numerical Input"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"O"=>array('description'=>$clang->gT("List with comment"),
'group'=>$group['SinChoiceQues'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>1,
'answerscales'=>1),
"P"=>array('description'=>$clang->gT("Multiple choice with comments"),
'group'=>$group['MulChoiceQues'],
'subquestions'=>1,
'hasdefaultvalues'=>1,
'assessable'=>1,
'answerscales'=>0),
"Q"=>array('description'=>$clang->gT("Multiple Short Text"),
'group'=>$group['TextQuestions'],
'subquestions'=>1,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"R"=>array('description'=>$clang->gT("Ranking"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>1),
"S"=>array('description'=>$clang->gT("Short Free Text"),
'group'=>$group['TextQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"T"=>array('description'=>$clang->gT("Long Free Text"),
'group'=>$group['TextQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"U"=>array('description'=>$clang->gT("Huge Free Text"),
'group'=>$group['TextQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>0,
'answerscales'=>0),
"X"=>array('description'=>$clang->gT("Text display"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"Y"=>array('description'=>$clang->gT("Yes/No"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"!"=>array('description'=>$clang->gT("List (Dropdown)"),
'group'=>$group['SinChoiceQues'],
'subquestions'=>0,
'hasdefaultvalues'=>1,
'assessable'=>1,
'answerscales'=>1),
":"=>array('description'=>$clang->gT("Array (Numbers)"),
'group'=>$group['Arrays'],
'subquestions'=>2,
'hasdefaultvalues'=>0,
'assessable'=>1,
'answerscales'=>0),
";"=>array('description'=>$clang->gT("Array (Texts)"),
'group'=>$group['Arrays'],
'subquestions'=>2,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"|"=>array('description'=>$clang->gT("File upload"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
"*"=>array('description'=>$clang->gT("Equation"),
'group'=>$group['MaskQuestions'],
'subquestions'=>0,
'hasdefaultvalues'=>0,
'assessable'=>0,
'answerscales'=>0),
);
asort($qtypes);
if ($ReturnType == "array") {return $qtypes;}
if ($ReturnType == "group"){
foreach($qtypes as $qkey=>$qtype){
$newqType[$qtype['group']][$qkey] = $qtype;
}
$qtypeselecter = "";
foreach($newqType as $group=>$members)
{
$qtypeselecter .= '<optgroup label="'.$group.'">';
foreach($members as $TypeCode=>$TypeProperties){
$qtypeselecter .= "<option value='$TypeCode'";
if ($SelectedCode == $TypeCode) {$qtypeselecter .= " selected='selected'";}
$qtypeselecter .= ">{$TypeProperties['description']}</option>\n";
}
$qtypeselecter .= '</optgroup>';
}
return $qtypeselecter;
};
$qtypeselecter = "";
foreach($qtypes as $TypeCode=>$TypeProperties)
{
$qtypeselecter .= "<option value='$TypeCode'";
if ($SelectedCode == $TypeCode) {$qtypeselecter .= " selected='selected'";}
$qtypeselecter .= ">{$TypeProperties['description']}</option>\n";
}
return $qtypeselecter;
}
/**
* isStandardTemplate returns true if a template is a standard template
* This function does not check if a template actually exists
*
* @param mixed $sTemplateName template name to look for
* @return bool True if standard template, otherwise false
*/
function isStandardTemplate($sTemplateName)
{
return in_array($sTemplateName,array('basic',
'bluengrey',
'business_grey',
'citronade',
'clear_logo',
'default',
'eirenicon',
'limespired',
'mint_idea',
'sherpa',
'vallendar'));
}
function &db_execute_num($sql,$inputarr=false)
{
global $connect;
$connect->SetFetchMode(ADODB_FETCH_NUM);
$dataset=$connect->Execute($sql,$inputarr); //Checked
return $dataset;
}
function &db_select_limit_num($sql,$numrows=-1,$offset=-1,$inputarr=false)
{
global $connect;
$connect->SetFetchMode(ADODB_FETCH_NUM);
$dataset=$connect->SelectLimit($sql,$numrows,$offset,$inputarr=false) or safe_die($sql);
return $dataset;
}
function &db_execute_assoc($sql,$inputarr=false,$silent=false)
{
global $connect;
$connect->SetFetchMode(ADODB_FETCH_ASSOC);
$dataset=$connect->Execute($sql,$inputarr); //Checked
if (!$silent && !$dataset) {safe_die($connect->ErrorMsg().':'.$sql);}
return $dataset;
}
function &db_select_limit_assoc($sql,$numrows=-1,$offset=-1,$inputarr=false,$dieonerror=true)
{
global $connect;
$connect->SetFetchMode(ADODB_FETCH_ASSOC);
$dataset=$connect->SelectLimit($sql,$numrows,$offset,$inputarr=false);
if (!$dataset && $dieonerror) {safe_die($connect->ErrorMsg().':'.$sql);}
return $dataset;
}
/**
* Returns the first row of values of the $sql query result
* as a 1-dimensional array
*
* @param mixed $sql
*/
function &db_select_column($sql)
{
global $connect;
$connect->SetFetchMode(ADODB_FETCH_NUM);
$dataset=$connect->Execute($sql);
$resultarray=array();
while ($row = $dataset->fetchRow()) {
$resultarray[]=$row[0];
}
return $resultarray;
}
/**
* This functions quotes fieldnames accordingly
*
* @param mixed $id Fieldname to be quoted
*/
function db_quote_id($id)
{
global $databasetype;
// WE DONT HAVE nor USE other thing that alphanumeric characters in the field names
// $quote = $connect->nameQuote;
// return $quote.str_replace($quote,$quote.$quote,$id).$quote;
switch ($databasetype)
{
case "mysqli" :
case "mysql" :
return "`".$id."`";
break;
case "mssql_n" :
case "mssql" :
case "mssqlnative" :
case "odbc_mssql" :
return "[".$id."]";
break;
case "postgres":
return "\"".$id."\"";
break;
default:
return "`".$id."`";
}
}
function db_random()
{
global $connect,$databasetype;
if ($databasetype=='odbc_mssql' || $databasetype=='mssql_n' || $databasetype=='odbtp') {$srandom='NEWID()';}
else {$srandom=$connect->random;}
return $srandom;
}
function db_quote($str,$ispostvar=false)
// This functions escapes the string only inside
{
global $connect;
if ($ispostvar) { return $connect->escape($str, get_magic_quotes_gpc());}
else {return $connect->escape($str);}
}
function db_quoteall($str,$ispostvar=false)
// This functions escapes the string inside and puts quotes around the string according to the used db type
// IF you are quoting a variable from a POST/GET then set $ispostvar to true so it doesnt get quoted twice.
{
global $connect;
if ($ispostvar) { return $connect->qstr($str, get_magic_quotes_gpc());}
else {return $connect->qstr($str);}
}
function db_table_name($name)
{
global $dbprefix;
return db_quote_id($dbprefix.$name);
}
/**
* returns the table name without quotes
*
* @param mixed $name
*/
function db_table_name_nq($name)
{
global $dbprefix;
return $dbprefix.$name;
}
/**
* Return a sql statement for finding LIKE named tables
* Be aware that you have to escape underscor chars by using a backslash
* otherwise you might get table names returned you don't want
*
* @param mixed $table
*/
function db_select_tables_like($table)
{
global $databasetype;
switch ($databasetype) {
case 'mysqli':
case 'mysql' :
return "SHOW TABLES LIKE '$table'";
case 'odbtp' :
case 'mssql_n' :
case 'mssqlnative':
case 'odbc_mssql' :
return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE' and TABLE_NAME LIKE '$table'";
case 'postgres' :
$table=str_replace('\\','\\\\',$table);
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' and table_name like '$table'";
default: safe_die ("Couldn't create 'select tables like' query for connection type 'databaseType'");
}
}
/**
* Return a boolean stating if the table(s) exist(s)
* Accepts '%' in names since it uses the 'like' statement
*
* @param mixed $table
*/
function db_tables_exist($table)
{
global $connect;
$surveyHasTokensTblQ = db_select_tables_like("$table");
$surveyHasTokensTblResult = db_execute_num($surveyHasTokensTblQ); //Checked
if ($surveyHasTokensTblResult->RecordCount() >= 1)
{
return TRUE;
}
else
{
return FALSE;
}
}
/**
* getsurveylist() Queries the database (survey table) for a list of existing surveys
*
* @param mixed $returnarray boolean - if set to true an array instead of an HTML option list is given back
*
* @global string $surveyid
* @global string $dbprefix
* @global string $scriptname
* @global string $connect
* @global string $clang
*
* @return string This string is returned containing <option></option> formatted list of existing surveys
*
*/
function getsurveylist($returnarray=false,$returnwithouturl=false)
{
global $surveyid, $dbprefix, $scriptname, $connect, $clang, $timeadjust;
static $cached = null;
if(is_null($cached)) {
$surveyidquery = " SELECT a.*, surveyls_title, surveyls_description, surveyls_welcometext, surveyls_url "
." FROM ".db_table_name('surveys')." AS a "
. "INNER JOIN ".db_table_name('surveys_languagesettings')." on (surveyls_survey_id=a.sid and surveyls_language=a.language) ";
if (!bHasGlobalPermission('USER_RIGHT_SUPERADMIN'))
{
$surveyidquery .= "WHERE a.sid in (select sid from ".db_table_name('survey_permissions')." where uid={$_SESSION['loginID']} and permission='survey' and read_p=1) ";
}
$surveyidquery .= " order by active DESC, surveyls_title";
$surveyidresult = db_execute_assoc($surveyidquery); //Checked
if (!$surveyidresult) {return "Database Error";}
$surveynames = $surveyidresult->GetRows();
$cached=$surveynames;
} else {
$surveynames = $cached;
}
$surveyselecter = "";
if ($returnarray===true) return $surveynames;
$activesurveys='';
$inactivesurveys='';
$expiredsurveys='';
if ($surveynames)
{
foreach($surveynames as $sv)
{
$surveylstitle=FlattenText($sv['surveyls_title']);
if (strlen($surveylstitle)>45)
{
$surveylstitle = htmlspecialchars(mb_strcut(html_entity_decode($surveylstitle,ENT_QUOTES,'UTF-8'), 0, 45, 'UTF-8'))."...";
}
if($sv['active']!='Y')
{
$inactivesurveys .= "<option ";
if($_SESSION['loginID'] == $sv['owner_id'])
{
$inactivesurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$inactivesurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$inactivesurveys .=" value='$scriptname?sid={$sv['sid']}'>{$surveylstitle}</option>\n";
} else
{
$inactivesurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
} elseif($sv['expires']!='' && $sv['expires'] < date_shift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust))
{
$expiredsurveys .="<option ";
if ($_SESSION['loginID'] == $sv['owner_id'])
{
$expiredsurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$expiredsurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$expiredsurveys .=" value='$scriptname?sid={$sv['sid']}'>{$surveylstitle}</option>\n";
} else
{
$expiredsurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
} else
{
$activesurveys .= "<option ";
if($_SESSION['loginID'] == $sv['owner_id'])
{
$activesurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$activesurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$activesurveys .=" value='$scriptname?sid={$sv['sid']}'>{$surveylstitle}</option>\n";
} else
{
$activesurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
}
} // End Foreach
}
//Only show each activesurvey group if there are some
if ($activesurveys!='')
{
$surveyselecter .= "<optgroup label='".$clang->gT("Active")."' class='activesurveyselect'>\n";
$surveyselecter .= $activesurveys . "</optgroup>";
}
if ($expiredsurveys!='')
{
$surveyselecter .= "<optgroup label='".$clang->gT("Expired")."' class='expiredsurveyselect'>\n";
$surveyselecter .= $expiredsurveys . "</optgroup>";
}
if ($inactivesurveys!='')
{
$surveyselecter .= "<optgroup label='".$clang->gT("Inactive")."' class='inactivesurveyselect'>\n";
$surveyselecter .= $inactivesurveys . "</optgroup>";
}
if (!isset($svexist))
{
$surveyselecter = "<option selected='selected' value=''>".$clang->gT("Please choose...")."</option>\n".$surveyselecter;
} else
{
if ($returnwithouturl===false)
{
$surveyselecter = "<option value='$scriptname?sid='>".$clang->gT("None")."</option>\n".$surveyselecter;
} else
{
$surveyselecter = "<option value=''>".$clang->gT("None")."</option>\n".$surveyselecter;
}
}
return $surveyselecter;
}
/**
* getQuestions() queries the database for an list of all questions matching the current survey and group id
*
* @global string $surveyid
* @global string $gid
* @global string $selectedqid
*
* @return This string is returned containing <option></option> formatted list of questions in the current survey and group
*/
function getQuestions($surveyid,$gid,$selectedqid)
{
global $scriptname, $clang;
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$qquery = 'SELECT * FROM '.db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND language='{$s_lang}' and parent_qid=0 order by question_order";
$qresult = db_execute_assoc($qquery); //checked
$qrows = $qresult->GetRows();
if (!isset($questionselecter)) {$questionselecter="";}
foreach ($qrows as $qrow)
{
$qrow['title'] = strip_tags($qrow['title']);
$questionselecter .= "<option value='$scriptname?sid=$surveyid&gid=$gid&qid={$qrow['qid']}'";
if ($selectedqid == $qrow['qid']) {$questionselecter .= " selected='selected'"; $qexists="Y";}
$questionselecter .=">{$qrow['title']}:";
$questionselecter .= " ";
$question=FlattenText($qrow['question']);
if (strlen($question)<35)
{
$questionselecter .= $question;
}
else
{
$questionselecter .= htmlspecialchars(mb_strcut(html_entity_decode($question,ENT_QUOTES,'UTF-8'), 0, 35, 'UTF-8'))."...";
}
$questionselecter .= "</option>\n";
}
if (!isset($qexists))
{
$questionselecter = "<option selected='selected'>".$clang->gT("Please choose...")."</option>\n".$questionselecter;
}
return $questionselecter;
}
/**
* getGidPrevious() returns the Gid of the group prior to the current active group
*
* @param string $surveyid
* @param string $gid
*
* @return The Gid of the previous group
*/
function getGidPrevious($surveyid, $gid)
{
global $scriptname, $clang;
if (!$surveyid) {$surveyid=returnglobal('sid');}
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$gquery = "SELECT gid FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='{$s_lang}' ORDER BY group_order";
$qresult = db_execute_assoc($gquery); //checked
$qrows = $qresult->GetRows();
$i = 0;
$iPrev = -1;
foreach ($qrows as $qrow)
{
if ($gid == $qrow['gid']) {$iPrev = $i - 1;}
$i += 1;
}
if ($iPrev >= 0) {$GidPrev = $qrows[$iPrev]['gid'];}
else {$GidPrev = "";}
return $GidPrev;
}
/**
* getQidPrevious() returns the Qid of the question prior to the current active question
*
* @param string $surveyid
* @param string $gid
* @param string $qid
*
* @return This Qid of the previous question
*/
function getQidPrevious($surveyid, $gid, $qid)
{
global $scriptname, $clang;
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$qquery = 'SELECT * FROM '.db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND language='{$s_lang}' and parent_qid=0 order by question_order";
$qresult = db_execute_assoc($qquery); //checked
$qrows = $qresult->GetRows();
$i = 0;
$iPrev = -1;
foreach ($qrows as $qrow)
{
if ($qid == $qrow['qid']) {$iPrev = $i - 1;}
$i += 1;
}
if ($iPrev >= 0) {$QidPrev = $qrows[$iPrev]['qid'];}
else {$QidPrev = "";}
return $QidPrev;
}
/**
* getGidNext() returns the Gid of the group next to the current active group
*
* @param string $surveyid
* @param string $gid
*
* @return The Gid of the next group
*/
function getGidNext($surveyid, $gid)
{
global $scriptname, $clang;
if (!$surveyid) {$surveyid=returnglobal('sid');}
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$gquery = "SELECT gid FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='{$s_lang}' ORDER BY group_order";
$qresult = db_execute_assoc($gquery); //checked
$qrows = $qresult->GetRows();
$GidNext="";
$i = 0;
$iNext = 1;
foreach ($qrows as $qrow)
{
if ($gid == $qrow['gid']) {$iNext = $i + 1;}
$i += 1;
}
if ($iNext < count($qrows)) {$GidNext = $qrows[$iNext]['gid'];}
else {$GidNext = "";}
return $GidNext;
}
/**
* getQidNext() returns the Qid of the question prior to the current active question
*
* @param string $surveyid
* @param string $gid
* @param string $qid
*
* @return This Qid of the previous question
*/
function getQidNext($surveyid, $gid, $qid)
{
global $scriptname, $clang;
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$qquery = 'SELECT qid FROM '.db_table_name('questions')." WHERE sid=$surveyid AND gid=$gid AND language='{$s_lang}' and parent_qid=0 order by question_order";
$qresult = db_execute_assoc($qquery); //checked
$qrows = $qresult->GetRows();
$i = 0;
$iNext = 1;
foreach ($qrows as $qrow)
{
if ($qid == $qrow['qid']) {$iNext = $i + 1;}
$i += 1;
}
if ($iNext < count($qrows)) {$QidNext = $qrows[$iNext]['qid'];}
else {$QidNext = "";}
return $QidNext;
}
/**
* This function calculates how much space is actually used by all files uploaded
* using the File Upload question type
*
* @returns integer Actual space used in MB
*/
function fCalculateTotalFileUploadUsage(){
global $uploaddir;
$sQuery="select sid from ".db_table_name('surveys');
$oResult = db_execute_assoc($sQuery); //checked
$aRows = $oResult->GetRows();
$iTotalSize=0.0;
foreach ($aRows as $aRow)
{
$sFilesPath=$uploaddir.'/surveys/'.$aRow['sid'].'/files';
if (file_exists($sFilesPath))
{
$iTotalSize+=(float)iGetDirectorySize($sFilesPath);
}
}
return (float)$iTotalSize/1024/1024;
}
function iGetDirectorySize($directory) {
$size = 0;
foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
$size+=$file->getSize();
}
return $size;
}
/**
* Gets number of groups inside a particular survey
*
* @param string $surveyid
* @param mixed $lang
*/
function getGroupSum($surveyid, $lang)
{
global $surveyid,$dbprefix ;
$sumquery3 = "SELECT * FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='".$lang."'"; //Getting a count of questions for this survey
$sumresult3 = db_execute_assoc($sumquery3); //Checked
$groupscount = $sumresult3->RecordCount();
return $groupscount ;
}
/**
* Gets number of questions inside a particular group
*
* @param string $surveyid
* @param mixed $groupid
*/
function getQuestionSum($surveyid, $groupid)
{
global $surveyid,$dbprefix ;
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$sumquery3 = "SELECT * FROM ".db_table_name('questions')." WHERE gid=$groupid and sid=$surveyid AND language='{$s_lang}'"; //Getting a count of questions for this survey
$sumresult3 = db_execute_assoc($sumquery3); //Checked
$questionscount = $sumresult3->RecordCount();
return $questionscount ;
}
/**
* getMaxgrouporder($surveyid) queries the database for the maximum sortorder of a group and returns the next higher one.
*
* @param mixed $surveyid
* @global string $surveyid
*/
function getMaxgrouporder($surveyid)
{
global $surveyid, $connect ;
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$max_sql = "SELECT max( group_order ) AS max FROM ".db_table_name('groups')." WHERE sid =$surveyid AND language='{$s_lang}'" ;
$current_max = $connect->GetOne($max_sql) ;
if(is_null($current_max))
{
return "0" ;
}
else return ++$current_max ;
}
/**
* getGroupOrder($surveyid,$gid) queries the database for the sortorder of a group.
*
* @param mixed $surveyid
* @param mixed $gid
* @return mixed
*/
function getGroupOrder($surveyid,$gid)
{
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$grporder_sql = "SELECT group_order FROM ".db_table_name('groups')." WHERE sid =$surveyid AND language='{$s_lang}' AND gid=$gid" ;
$grporder_result =db_execute_assoc($grporder_sql); //Checked
$grporder_row = $grporder_result->FetchRow() ;
$group_order = $grporder_row['group_order'];
if($group_order=="")
{
return "0" ;
}
else return $group_order ;
}
/**
* getMaxquestionorder($gid) queries the database for the maximum sortorder of a question.
*
* @global string $surveyid
*/
function getMaxquestionorder($gid)
{
global $surveyid ;
$gid=sanitize_int($gid);
$s_lang = GetBaseLanguageFromSurveyID($surveyid);
$max_sql = "SELECT max( question_order ) AS max FROM ".db_table_name('questions')." WHERE gid='$gid' AND language='$s_lang'";
$max_result =db_execute_assoc($max_sql) ; //Checked
$maxrow = $max_result->FetchRow() ;
$current_max = $maxrow['max'];
if($current_max=="")
{
return "0" ;
}
else return $current_max ;
}
/**
* question_class() returns a class name for a given question type to allow custom styling for each question type.
*
* @param string $input containing unique character representing each question type.
* @return string containing the class name for a given question type.
*/
function question_class($input)
{
switch($input)
{ // I think this is a bad solution to adding classes to question
// DIVs but I can't think of a better solution. (eric_t_cruiser)
case 'X': return 'boilerplate'; // BOILERPLATE QUESTION
case '5': return 'choice-5-pt-radio'; // 5 POINT CHOICE radio-buttons
case 'D': return 'date'; // DATE
case 'Z': return 'list-radio-flexible'; // LIST Flexible radio-button
case 'L': return 'list-radio'; // LIST radio-button
case 'W': return 'list-dropdown-flexible'; // LIST drop-down (flexible label)
case '!': return 'list-dropdown'; // List - dropdown
case 'O': return 'list-with-comment'; // LIST radio-button + textarea
case 'R': return 'ranking'; // RANKING STYLE
case 'M': return 'multiple-opt'; // Multiple choice checkbox
case 'I': return 'language'; // Language Question
case 'P': return 'multiple-opt-comments'; // Multiple choice with comments checkbox + text
case 'Q': return 'multiple-short-txt'; // TEXT
case 'K': return 'numeric-multi'; // MULTIPLE NUMERICAL QUESTION
case 'N': return 'numeric'; // NUMERICAL QUESTION TYPE
case 'S': return 'text-short'; // SHORT FREE TEXT
case 'T': return 'text-long'; // LONG FREE TEXT
case 'U': return 'text-huge'; // HUGE FREE TEXT
case 'Y': return 'yes-no'; // YES/NO radio-buttons
case 'G': return 'gender'; // GENDER drop-down list
case 'A': return 'array-5-pt'; // ARRAY (5 POINT CHOICE) radio-buttons
case 'B': return 'array-10-pt'; // ARRAY (10 POINT CHOICE) radio-buttons
case 'C': return 'array-yes-uncertain-no'; // ARRAY (YES/UNCERTAIN/NO) radio-buttons
case 'E': return 'array-increase-same-decrease'; // ARRAY (Increase/Same/Decrease) radio-buttons
case 'F': return 'array-flexible-row'; // ARRAY (Flexible) - Row Format
case 'H': return 'array-flexible-column'; // ARRAY (Flexible) - Column Format
// case '^': return 'slider'; // SLIDER CONTROL
case ':': return 'array-multi-flexi'; // ARRAY (Multi Flexi) 1 to 10
case ";": return 'array-multi-flexi-text';
case "1": return 'array-flexible-duel-scale'; // Array dual scale
case "*": return 'equation'; // Equation
default: return 'generic_question'; // Should have a default fallback
};
};