/
common_helper.php
7825 lines (7040 loc) · 283 KB
/
common_helper.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 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2011 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.
*/
Yii::import('application.helpers.sanitize_helper', true);
/**
* Simple function to sort the permissions by title
*
* @param mixed $aPermissionA Permission A to compare
* @param mixed $aPermissionB Permission B to compare
*/
function comparePermission($aPermissionA,$aPermissionB)
{
if($aPermissionA['title'] >$aPermissionB['title']) {
return 1;
}
else {
return -1;
}
}
/**
* getQuestionTypeList() Returns list of question types available in LimeSurvey. Edit this if you are adding a new
* question type
*
* @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 getQuestionTypeList($SelectedCode = "T", $ReturnType = "selector")
{
$publicurl = Yii::app()->getConfig('publicurl');
$clang = Yii::app()->lang;
$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/Time"),
'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'));
}
/**
* getSurveyList() Queries the database (survey table) for a list of existing surveys
*
* @param boolean $returnarray if set to true an array instead of an HTML option list is given back
* @return string This string is returned containing <option></option> formatted list of existing surveys
*
*/
function getSurveyList($returnarray=false, $returnwithouturl=false, $surveyid=false)
{
$cached = null;
$timeadjust = getGlobalSetting('timeadjust');
$clang = new Limesurvey_lang(Yii::app()->session['adminlang']);
if(is_null($cached)) {
if (!hasGlobalPermission('USER_RIGHT_SUPERADMIN'))
$surveyidresult = Survey::model()->permission(Yii::app()->user->getId())->with(array('languagesettings'=>array('condition'=>'surveyls_language=language')))->findAll();
else
$surveyidresult = Survey::model()->with(array('languagesettings'=>array('condition'=>'surveyls_language=language')))->findAll();
$surveynames = array();
foreach ($surveyidresult as $result)
{
$surveynames[] = array_merge($result->attributes, $result->languagesettings[0]->attributes);
}
$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(Yii::app()->user->getId() == $sv['owner_id'])
{
$inactivesurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$inactivesurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$inactivesurveys .=" value='".Yii::app()->getController()->createUrl("/admin/survey/view/surveyid/".$sv['sid'])."'>{$surveylstitle}</option>\n";
} else
{
$inactivesurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
} elseif($sv['expires']!='' && $sv['expires'] < dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i:s", $timeadjust))
{
$expiredsurveys .="<option ";
if (Yii::app()->user->getId() == $sv['owner_id'])
{
$expiredsurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$expiredsurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$expiredsurveys .=" value='".Yii::app()->getController()->createUrl("/admin/survey/view/surveyid/".$sv['sid'])."'>{$surveylstitle}</option>\n";
} else
{
$expiredsurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
} else
{
$activesurveys .= "<option ";
if(Yii::app()->user->getId() == $sv['owner_id'])
{
$activesurveys .= " style=\"font-weight: bold;\"";
}
if ($sv['sid'] == $surveyid)
{
$activesurveys .= " selected='selected'"; $svexist = 1;
}
if ($returnwithouturl===false)
{
$activesurveys .=" value='".Yii::app()->getController()->createUrl("/admin/survey/view/surveyid/".$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='".Yii::app()->getController()->createUrl("/admin")."'>".$clang->gT("None")."</option>\n".$surveyselecter;
} else
{
$surveyselecter = "<option value=''>".$clang->gT("None")."</option>\n".$surveyselecter;
}
}
return $surveyselecter;
}
/**
* Returns true if a user has permissions in the particular survey
*
* @param $iSID The survey ID
* @param $sPermission
* @param $sCRUD
* @param $iUID User ID - if not given the one of the current user is used
* @return bool
*/
function hasSurveyPermission($iSID, $sPermission, $sCRUD, $iUID=null)
{
if (!in_array($sCRUD,array('create','read','update','delete','import','export'))) return false;
$sCRUD=$sCRUD.'_p';
$iSID = (int)$iSID;
if ($iSID==0) return false;
$aSurveyPermissionCache = Yii::app()->getConfig("aSurveyPermissionCache");
if (is_null($iUID))
{
if (!Yii::app()->user->getIsGuest()) $iUID = Yii::app()->session['loginID'];
else return false;
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']==1) return true; //Superadmin has access to all
}
if (!isset($aSurveyPermissionCache[$iSID][$iUID][$sPermission][$sCRUD]))
{
//!!! Convert this model
$query = Survey_permissions::model()->findByAttributes(array("sid"=> $iSID,"uid"=> $iUID,"permission"=>$sPermission));
//$sSQL = "SELECT {$sCRUD} FROM " . db_table_name('survey_permissions') . "
// WHERE sid={$iSID} AND uid = {$iUID}
// and permission=".dbQuoteAll($sPermission); //Getting rights for this survey
$bPermission = is_null($query) ? array() : $query->attributes;
if (!isset($bPermission[$sCRUD]) || $bPermission[$sCRUD]==0)
{
$bPermission=false;
}
else
{
$bPermission=true;
}
$aSurveyPermissionCache[$iSID][$iUID][$sPermission][$sCRUD]=$bPermission;
}
Yii::app()->setConfig("aSurveyPermissionCache", $aSurveyPermissionCache);
return $aSurveyPermissionCache[$iSID][$iUID][$sPermission][$sCRUD];
}
/**
* Returns true if a user has global permission for a certain action. Available permissions are
*
* USER_RIGHT_CREATE_SURVEY
* USER_RIGHT_CONFIGURATOR
* USER_RIGHT_CREATE_USER
* USER_RIGHT_DELETE_USER
* USER_RIGHT_SUPERADMIN
* USER_RIGHT_MANAGE_TEMPLATE
* USER_RIGHT_MANAGE_LABEL
*
* @param $sPermission
* @return bool
*/
function hasGlobalPermission($sPermission)
{
if (!Yii::app()->user->getIsGuest()) $iUID = !Yii::app()->user->getId();
else return false;
if (Yii::app()->session['USER_RIGHT_SUPERADMIN']==1) return true; //Superadmin has access to all
if (Yii::app()->session[$sPermission]==1)
{
return true;
}
else
{
return false;
}
}
function getTemplateList()
{
$usertemplaterootdir=Yii::app()->getConfig("usertemplaterootdir");
$standardtemplaterootdir=Yii::app()->getConfig("standardtemplaterootdir");
if (!$usertemplaterootdir) {die("getTemplateList() no template directory");}
if ($handle = opendir($standardtemplaterootdir))
{
while (false !== ($file = readdir($handle)))
{
if (!is_file("$standardtemplaterootdir/$file") && $file != "." && $file != ".." && $file!=".svn" && isStandardTemplate($file))
{
$list_of_files[$file] = $standardtemplaterootdir.DIRECTORY_SEPARATOR.$file;
}
}
closedir($handle);
}
if ($handle = opendir($usertemplaterootdir))
{
while (false !== ($file = readdir($handle)))
{
if (!is_file("$usertemplaterootdir/$file") && $file != "." && $file != ".." && $file!=".svn")
{
$list_of_files[$file] = $usertemplaterootdir.DIRECTORY_SEPARATOR.$file;
}
}
closedir($handle);
}
ksort($list_of_files);
return $list_of_files;
}
function getAdminThemeList()
{
// $usertemplaterootdir=Yii::app()->getConfig("usertemplaterootdir");
$standardtemplaterootdir=Yii::app()->getConfig("styledir");
// if (!$usertemplaterootdir) {die("getTemplateList() no template directory");}
if ($handle = opendir($standardtemplaterootdir))
{
while (false !== ($file = readdir($handle)))
{
if (!is_file("$standardtemplaterootdir/$file") && $file != "." && $file != ".." && $file!=".svn")
{
$list_of_files[$file] = $standardtemplaterootdir.DIRECTORY_SEPARATOR.$file;
}
}
closedir($handle);
}
/* if ($handle = opendir($usertemplaterootdir))
{
while (false !== ($file = readdir($handle)))
{
if (!is_file("$usertemplaterootdir/$file") && $file != "." && $file != ".." && $file!=".svn")
{
$list_of_files[$file] = $usertemplaterootdir.DIRECTORY_SEPARATOR.$file;
}
}
closedir($handle);
} */
ksort($list_of_files);
return $list_of_files;
}
/**
* getQuestions() queries the database for an list of all questions matching the current survey and group id
*
* @return This string is returned containing <option></option> formatted list of questions in the current survey and group
*/
function getQuestions($surveyid,$gid,$selectedqid)
{
$clang = Yii::app()->lang;
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Questions::model()->findAllByAttributes(array('sid' => $surveyid, 'gid' => $gid, 'language' => $s_lang, 'parent_qid' => 0),array('order'=>'question_order'));
if (!isset($sQuestionselecter)) {$sQuestionselecter="";}
foreach ($qrows as $qrow)
{
$qrow = $qrow->attributes;
$qrow['title'] = strip_tags($qrow['title']);
$link = Yii::app()->getController()->createUrl("/admin/survey/view/surveyid/".$surveyid."/gid/".$gid."/qid/".$qrow['qid']);
$sQuestionselecter .= "<option value='{$link}'";
if ($selectedqid == $qrow['qid'])
{
$sQuestionselecter .= " selected='selected'";
$qexists=true;
}
$sQuestionselecter .=">{$qrow['title']}:";
$sQuestionselecter .= " ";
$question=flattenText($qrow['question']);
if (strlen($question)<35)
{
$sQuestionselecter .= $question;
}
else
{
$sQuestionselecter .= htmlspecialchars(mb_strcut(html_entity_decode($question,ENT_QUOTES,'UTF-8'), 0, 35, 'UTF-8'))."...";
}
$sQuestionselecter .= "</option>\n";
}
if (!isset($qexists))
{
$sQuestionselecter = "<option selected='selected'>".$clang->gT("Please choose...")."</option>\n".$sQuestionselecter;
}
else
{
$sQuestionselecter = "<option>".$clang->gT("None")."</option>\n".$sQuestionselecter;
}
return $sQuestionselecter;
}
/**
* 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)
{
$clang = Yii::app()->lang;
if (!$surveyid) {$surveyid=returnGlobal('sid');}
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qresult = Groups::model()->findAllByAttributes(array('sid' => $surveyid, 'language' => $s_lang), array('order'=>'group_order'));
$i = 0;
$iPrev = -1;
foreach ($qresult as $qrow)
{
$qrow = $qrow->attributes;
if ($gid == $qrow['gid']) {$iPrev = $i - 1;}
$i += 1;
}
if ($iPrev >= 0) {$GidPrev = $qresult[$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)
{
$clang = Yii::app()->lang;
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Questions::model()->findAllByAttributes(array('gid' => $gid, 'sid' => $surveyid, 'language' => $s_lang, 'parent_qid'=>0),array('order'=>'question_order'));
$i = 0;
$iPrev = -1;
if (count($qrows) > 0)
{
foreach ($qrows as $qrow)
{
$qrow = $qrow->attributes;
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)
{
$clang = Yii::app()->lang;
if (!$surveyid) {$surveyid=returnGlobal('sid');}
$s_lang = Survey::model()->findByPk($surveyid)->language;
//$gquery = "SELECT gid FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='{$s_lang}' ORDER BY group_order";
$qresult = Groups::model()->findAllByAttributes(array('sid' => $surveyid, 'language' => $s_lang), array('order'=>'group_order'));
$GidNext="";
$i = 0;
$iNext = 1;
foreach ($qresult as $qrow)
{
$qrow = $qrow->attributes;
if ($gid == $qrow['gid']) {$iNext = $i + 1;}
$i += 1;
}
if ($iNext < count($qresult)) {$GidNext = $qresult[$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)
{
$clang = Yii::app()->lang;
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Questions::model()->findAllByAttributes(array('gid' => $gid, 'sid' => $surveyid, 'language' => $s_lang, 'parent_qid' => 0), array('order'=>'question_order'));
$i = 0;
$iNext = 1;
if (count($qrows) > 0)
{
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;
}
function convertGETtoPOST($url)
{
$url = preg_replace('/&/i','&',$url);
$stack = explode('?',$url);
$calledscript = array_shift($stack);
$query = array_shift($stack);
$aqueryitems = explode('&',$query);
$arrayParam = Array();
$arrayVal = Array();
foreach ($aqueryitems as $queryitem)
{
$stack = explode ('=', $queryitem);
$paramname = array_shift($stack);
$value = array_shift($stack);
$arrayParam[] = "'".$paramname."'";
$arrayVal[] = substr($value, 0, 9) != "document." ? "'".$value."'" : $value;
}
// $Paramlist = "[" . implode(",",$arrayParam) . "]";
// $Valuelist = "[" . implode(",",$arrayVal) . "]";
$Paramlist = "new Array(" . implode(",",$arrayParam) . ")";
$Valuelist = "new Array(" . implode(",",$arrayVal) . ")";
$callscript = "sendPost('$calledscript','".Yii::app()->session['checksessionpost']."',$Paramlist,$Valuelist);";
return $callscript;
}
/**
* 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 calculateTotalFileUploadUsage(){
global $uploaddir;
$sQuery='select sid from {{surveys}}';
$oResult = dbExecuteAssoc($sQuery); //checked
$aRows = $oResult->readAll();
$iTotalSize=0.0;
foreach ($aRows as $aRow)
{
$sFilesPath=$uploaddir.'/surveys/'.$aRow['sid'].'/files';
if (file_exists($sFilesPath))
{
$iTotalSize+=(float)getDirectorySize($sFilesPath);
}
}
return (float)$iTotalSize/1024/1024;
}
function getDirectorySize($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)
{
//$condn = "WHERE sid=".$surveyid." AND language='".$lang."'"; //Getting a count of questions for this survey
$condn = array('sid'=>$surveyid,'language'=>$lang);
$sumresult3 = count(Groups::model()->findAllByAttributes($condn)); //Checked)
return $sumresult3 ;
}
/**
* getMaxGroupOrder($surveyid) queries the database for the maximum sortorder of a group and returns the next higher one.
*
* @param mixed $surveyid
*/
function getMaxGroupOrder($surveyid)
{
$s_lang = Survey::model()->findByPk($surveyid)->language;
//$max_sql = "SELECT max( group_order ) AS max FROM ".db_table_name('groups')." WHERE sid =$surveyid AND language='{$s_lang}'" ;
$query = Groups::model()->find(array('order' => 'group_order desc'));
$current_max = !is_null($query) ? $query->group_order : '';
if($current_max!="")
{
return ++$current_max ;
}
else return "0" ;
}
/**
* 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 = Survey::model()->findByPk($surveyid)->language;
//$grporder_sql = "SELECT group_order FROM ".db_table_name('groups')." WHERE sid =$surveyid AND language='{$s_lang}' AND gid=$gid" ;
$grporder_result = Groups::model()->findByAttributes(array('sid' => $surveyid, 'gid' => $gid, 'language' => $s_lang)); //Checked
$grporder_row = $grporder_result->attributes ;
$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.
*
*/
function getMaxQuestionOrder($gid,$surveyid)
{
$gid=sanitize_int($gid);
$s_lang = Survey::model()->findByPk($surveyid)->language;
$max_sql = "SELECT max( question_order ) AS max FROM {{questions}} WHERE gid='$gid' AND language='$s_lang'";
$max_result = Yii::app()->db->createCommand($max_sql)->query(); //Checked
$maxrow = $max_result->read() ;
$current_max = $maxrow['max'];
if($current_max=="")
{
return "0" ;
}
else return $current_max ;
}
/**
* getQuestionClass() 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 getQuestionClass($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
};
};
/**
* setupColumns() defines all the html tags to be wrapped around
* various list type answers.
*
* @param integer $columns - the number of columns, usually supplied by $dcols
* @param integer $answer_count - the number of answers to a question, usually supplied by $anscount
* @param string $wrapperclass - a global class for the wrapper
* @param string $itemclass - a class for the item
* @return array with all the various opening and closing tags to generate a set of columns.
*
* It returns an array with the following items:
* $wrapper['whole-start'] = Opening wrapper for the whole list
* $wrapper['whole-end'] = closing wrapper for the whole list
* $wrapper['col-devide'] = normal column devider
* $wrapper['col-devide-last'] = the last column devider (to allow
* for different styling of the last
* column
* $wrapper['item-start'] = opening wrapper tag for individual
* option
* $wrapper['item-start-other'] = opening wrapper tag for other
* option
* $wrapper['item-start-noanswer'] = opening wrapper tag for no answer
* option
* $wrapper['item-end'] = closing wrapper tag for individual
* option
* $wrapper['maxrows'] = maximum number of rows in each
* column
* $wrapper['cols'] = Number of columns to be inserted
* (and checked against)
*
*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Columns are a problem.
* Really there is no perfect solution to columns at the moment.
*
* - Using Tables is problematic semanticly.
* - Using inline or float to create columns, causes the answers
* flows horizontally, not vertically which is not ideal visually.
* - Using CSS3 columns is also a problem because of browser support
* and also because if you have answeres split across two or more
* lines, and those answeres happen to fall at the bottom of a
* column, the answer might be split across columns as well as
* lines.
* - Using nested unordered list with the first level of <LI>s
* floated is the same as using tables and so is bad semantically
* for the same reason tables are bad.
* - Breaking the unordered lists into consecutive floated unordered
* lists is not great semantically but probably not as bad as
* using tables.
*
* Because I haven't been able to decide which option is the least
* bad, I have handed over that responsibility to the admin who sets
* LimeSurvey up on their server.
*