-
Notifications
You must be signed in to change notification settings - Fork 994
/
common_helper.php
6272 lines (5718 loc) · 226 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;
}
}
/**
* Translation helper function
* @param string $sToTranslate
* @param string $sEscapeMode Valid values are html (this is the default, js and unescaped
* @param string $sLanguage
*/
function gT($sToTranslate, $sEscapeMode = 'html', $sLanguage = NULL)
{
return quoteText(Yii::t('',$sToTranslate,array(),null,$sLanguage),$sEscapeMode);
}
/**
* Translation helper function which outputs right away.
* @param string $sToTranslate
* @param string $sEscapeMode
* @param string $sLanguage
*/
function eT($sToTranslate, $sEscapeMode = 'html', $sLanguage = NULL)
{
echo gT($sToTranslate,$sEscapeMode);
}
/**
* Translation helper function for plural forms
* @param string $sTextToTranslate
* @param integer $iCount
* @param string $sEscapeMode
*/
function ngT($sTextToTranslate, $iCount, $sEscapeMode = 'html')
{
return quoteText(Yii::t('',$sTextToTranslate,$iCount),$sEscapeMode);
}
/**
* Translation helper function for plural forms which outputs right away
* @param string $sToTranslate
* @param integer $iCount
* @param string $sEscapeMode
*/
function neT($sToTranslate, $iCount, $sEscapeMode = 'html')
{
echo ngT($sToTranslate,$iCount,$sEscapeMode);
}
/**
* Quotes a translation according to purpose
* if sEscapeMode is null, we use HTML method because probably we had to specify null as sEscapeMode upstream
*
* @param mixed $sText Text to quote
* @param string $sEscapeMode Optional - One of the values 'html','js' or 'unescaped' - defaults to 'html'
*/
function quoteText($sText, $sEscapeMode = 'html')
{
if ($sEscapeMode === null)
$sEscapeMode = 'html';
switch ($sEscapeMode)
{
case 'html':
return HTMLEscape($sText);
break;
case 'js':
return javascriptEscape($sText);
break;
case 'unescaped':
return $sText;
break;
default:
return "Unsupported EscapeMode in gT method";
break;
}
}
/**
* 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');
$qtypes = Question::typeList();
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 Template::isStandardTemplate($sTemplateName);
}
/**
* 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, $surveyid=false)
{
static $cached = null;
$bCheckIntegrity = false;
$timeadjust = getGlobalSetting('timeadjust');
App()->setLanguage((isset(Yii::app()->session['adminlang']) ? Yii::app()->session['adminlang'] : 'en'));
if(is_null($cached)) {
$surveyidresult = Survey::model()
->permission(Yii::app()->user->getId())
->with('defaultlanguage')
->findAll(array('order'=>'surveyls_title'));
$surveynames = array();
foreach ($surveyidresult as $result)
{
if(!empty($result->defaultlanguage))
{
$surveynames[] = array_merge($result->attributes, $result->defaultlanguage->attributes);
}
elseif(!($bCheckIntegrity))
{
$bCheckIntegrity=true;
Yii::app()->setFlashMessage(
CHtml::link(gT("One or more surveys seem to be broken - please use the data integrity check tool to fix this."),array("admin/checkintegrity"))
,
'error');
}
}
$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)>70)
{
$surveylstitle = htmlspecialchars(mb_strcut(html_entity_decode($surveylstitle,ENT_QUOTES,'UTF-8'), 0, 70, 'UTF-8'))."...";
}
if($sv['active']!='Y')
{
$inactivesurveys .= "<option ";
if(Yii::app()->user->getId() == $sv['owner_id'])
{
$inactivesurveys .= " class='mysurvey emphasis'";
}
if ($sv['sid'] == $surveyid)
{
$inactivesurveys .= " selected='selected'"; $svexist = 1;
}
$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 .= " class='mysurvey emphasis'";
}
if ($sv['sid'] == $surveyid)
{
$expiredsurveys .= " selected='selected'"; $svexist = 1;
}
$expiredsurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
} else
{
$activesurveys .= "<option ";
if(Yii::app()->user->getId() == $sv['owner_id'])
{
$activesurveys .= " class='mysurvey emphasis'";
}
if ($sv['sid'] == $surveyid)
{
$activesurveys .= " selected='selected'"; $svexist = 1;
}
$activesurveys .=" value='{$sv['sid']}'>{$surveylstitle}</option>\n";
}
} // End Foreach
}
//Only show each activesurvey group if there are some
if ($activesurveys!='')
{
$surveyselecter .= "<optgroup label='".gT("Active")."' class='activesurveyselect'>\n";
$surveyselecter .= $activesurveys . "</optgroup>";
}
if ($expiredsurveys!='')
{
$surveyselecter .= "<optgroup label='".gT("Expired")."' class='expiredsurveyselect'>\n";
$surveyselecter .= $expiredsurveys . "</optgroup>";
}
if ($inactivesurveys!='')
{
$surveyselecter .= "<optgroup label='".gT("Inactive")."' class='inactivesurveyselect'>\n";
$surveyselecter .= $inactivesurveys . "</optgroup>";
}
if (!isset($svexist))
{
$surveyselecter = "<option selected='selected' value=''>".gT("Please choose...")."</option>\n".$surveyselecter;
} else
{
$surveyselecter = "<option value=''>".gT("None")."</option>\n".$surveyselecter;
}
return $surveyselecter;
}
function getTemplateList()
{
return Template::getTemplateList();
}
function getTemplateListWithPreviews()
{
return Template::getTemplateListWithPreviews();
}
/**
* getQuestions() queries the database for an list of all questions matching the current survey and group id
*
* @return string string is returned containing <option></option> formatted list of questions in the current survey and group
*/
function getQuestions($surveyid,$gid,$selectedqid)
{
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Question::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/questions/sa/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'>".gT("Please choose...")."</option>\n".$sQuestionselecter;
}
else
{
$link = Yii::app()->getController()->createUrl("/admin/questiongroups/sa/view/surveyid/".$surveyid."/gid/".$gid);
$sQuestionselecter = "<option value='{$link}'>".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)
{
if (!$surveyid) {$surveyid=returnGlobal('sid',true);}
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qresult = QuestionGroup::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)
{
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Question::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)
{
if (!$surveyid) {$surveyid=returnGlobal('sid',true);}
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qresult = QuestionGroup::model()->findAllByAttributes(array('sid' => $surveyid, 'language' => $s_lang), array('order'=>'group_order'));
$GidNext="";
$i = 0;
$iNext = 0;
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)
{
$s_lang = Survey::model()->findByPk($surveyid)->language;
$qrows = Question::model()->findAllByAttributes(array('gid' => $gid, 'sid' => $surveyid, 'language' => $s_lang, 'parent_qid' => 0), array('order'=>'question_order'));
$i = 0;
$iNext = 0;
foreach ($qrows as $qrow)
{
if ($qid == $qrow->qid && $qid) {$iNext = $i + 1;}
$i += 1;
}
if ($iNext < count($qrows)) {$QidNext = $qrows[$iNext]->qid;}
else {$QidNext = "";}
return $QidNext;
}
function convertGETtoPOST($url)
{
// This function must be deprecated and replaced by $.post
$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','',$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;
}
/**
* @param string $directory
*/
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 = QuestionGroup::model()->countByAttributes($condn); //Checked)
return $sumresult3 ;
}
/**
* Queries the database for the maximum sortorder of a group and returns the next higher one.
*
* @param integer $surveyid
* @return int
*/
function getMaxGroupOrder($surveyid)
{
$queryResult = QuestionGroup::model()->find(array(
'condition' => 'sid = :sid',
'params' => array(':sid' => $surveyid),
'order' => 'group_order desc',
'limit' => '1'
));
$current_max = !is_null($queryResult) ? $queryResult->group_order : "";
if($current_max !== "")
{
$current_max += 1;
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 = QuestionGroup::model()->findByAttributes(array('sid' => $surveyid, 'gid' => $gid, 'language' => $s_lang)); //Checked
//var_dump($grporder_result); var_dump($surveyid); var_dump($gid); die();
$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)
{
Question::getQuestionClass($input);
};
/**
* 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.
*
* There are four options:
* 'css' using one of the various CSS only methods for
* rendering columns.
* (Check the CSS file for your chosen template to see
* how columns are defined.)
* 'ul' using multiple floated unordered lists. (DEFAULT)
* 'table' using conventional tables based layout.
* NULL blocks the use of columns
*
* 'ul' is the default because it's the best possible compromise
* between semantic markup and visual layout.
*/
function setupColumns($columns, $answer_count,$wrapperclass="",$itemclass="")
{
$column_style = Yii::app()->getConfig('column_style');
if ( !in_array($column_style,array('css','ul','table')) && !is_null($column_style) )
{
$column_style = 'ul';
};
if(!is_null($column_style) && $columns!=1) // Add a global class for all column.
{
$wrapperclass.= " colstyle-{$column_style}";
}
if($columns < 2)
{
$column_style = null;
$columns = 1;
}
if(($columns > $answer_count) && $answer_count>0)
{
$columns = $answer_count;
};
$class_first = ' class="'.$wrapperclass.'"';
if($columns > 1 && !is_null($column_style))
{
if($column_style == 'ul')
{
$ul = '-ul';
}
else
{
$ul = '';
}
$class_first = ' class="'.$wrapperclass.' cols-'.$columns . $ul.' first"';
$class = ' class="'.$wrapperclass.' cols-'.$columns . $ul.'"';
$class_last_ul = ' class="'.$wrapperclass.' cols-'.$columns . $ul.' last"';
$class_last_table = ' class="'.$wrapperclass.' cols-'.$columns.' last"';
}
else
{
$class = ' class="'.$wrapperclass.'"';
$class_last_ul = ' class="'.$wrapperclass.'"';
$class_last_table = ' class="'.$wrapperclass.'"';
};
$wrapper = array(
'whole-start' => "\n<ul$class_first>\n"
,'whole-end' => "</ul>\n"
,'col-devide' => ''
,'col-devide-last' => ''
,'item-start' => "\t<li class=\"{$itemclass}\">\n"
,'item-start-other' => "\t<li class=\"{$itemclass} other other-item\">\n"
,'item-start-noanswer' => "\t<li class=\"{$itemclass} noanswer-item\">\n"
,'item-end' => "\t</li>\n"
,'maxrows' => ceil($answer_count/$columns) //Always rounds up to nearest whole number
,'cols' => $columns
);
switch($column_style)
{
case 'ul': if($columns > 1)
{
$wrapper['col-devide'] = "\n</ul>\n\n<ul$class>\n";
$wrapper['col-devide-last'] = "\n</ul>\n\n<ul$class_last_ul>\n";
}
break;
case 'table': $table_cols = '';
for($cols = $columns ; $cols > 0 ; --$cols)
{
switch($cols)
{
case $columns: $table_cols .= "\t<col$class_first />\n";
break;
case 1: $table_cols .= "\t<col$class_last_table />\n";
break;
default: $table_cols .= "\t<col$class />\n";
};
};
if($columns > 1)
{
$wrapper['col-devide'] = "\t</ul>\n</td>\n\n<td>\n\t<ul>\n";
$wrapper['col-devide-last'] = "\t</ul>\n</td>\n\n<td class=\"last\">\n\t<ul>\n";
};
$wrapper['whole-start'] = "\n<table$class>\n$table_cols\n\t<tbody>\n<tr>\n<td>\n\t<ul>\n";
$wrapper['whole-end'] = "\t</ul>\n</td>\n</tr>\n\t</tbody>\n</table>\n";
$wrapper['item-start'] = "<li class=\"{$itemclass}\">\n";
$wrapper['item-end'] = "</li class=\"{$itemclass}\">\n";
};
return $wrapper;
};
function alternation($alternate = '' , $type = 'col')
{
/**
* alternation() Returns a class identifyer for alternating between
* two options. Used to style alternate elements differently. creates
* or alternates between the odd string and the even string used in
* as column and row classes for array type questions.
*
* @param string $alternate = '' (empty) (default) , 'array2' , 'array1' , 'odd' , 'even'
* @param string $type = 'col' (default) or 'row'
*
* @return string representing either the first alternation or the opposite alternation to the one supplied..
*/
/*
// The following allows type to be left blank for row in subsequent
// function calls.
// It has been left out because 'row' must be defined the first time
// alternation() is called. Since it is only ever written once for each
// while statement within a function, 'row' is always defined.
if(!empty($alternate) && $type != 'row')
{ if($alternate == ('array2' || 'array1'))
{
$type = 'row';
};
};
// It has been left in case it becomes useful but probably should be
// removed.
*/
if($type == 'row')
{
$odd = 'array2 well'; // should be row_odd
$even = 'array1 well'; // should be row_even
}
else
{
$odd = 'odd'; // should be col_odd
$even = 'even'; // should be col_even
};
if($alternate == $odd)
{
$alternate = $even;
}
else
{
$alternate = $odd;
};
return $alternate;
}
/**
* longestString() returns the length of the longest string past to it.
* @peram string $new_string
* @peram integer $longest_length length of the (previously) longest string passed to it.
* @param integer $longest_length
* @return integer representing the length of the longest string passed (updated if $new_string was longer than $longest_length)
*
* usage should look like this: $longest_length = longestString( $new_string , $longest_length );
*
*/
function longestString( $new_string , $longest_length )
{
if($longest_length < strlen(trim(strip_tags($new_string))))
{
$longest_length = strlen(trim(strip_tags($new_string)));
};
return $longest_length;
};
/**
* getNotificationList() returns different options for notifications
*
* @param string $notificationcode - the currently selected one
*
* @return string string is returned containing <option></option> formatted list of notification methods for current survey
*/
function getNotificationList($notificationcode)
{
$ntypes = array(
"0"=>gT("No email notification"),
"1"=>gT("Basic email notification"),
"2"=>gT("Detailed email notification with result codes")
);
if (!isset($ntypeselector)) {$ntypeselector="";}
foreach($ntypes as $ntcode=>$ntdescription)
{
$ntypeselector .= "<option value='$ntcode'";
if ($notificationcode == $ntcode) {$ntypeselector .= " selected='selected'";}
$ntypeselector .= ">$ntdescription</option>\n";
}
return $ntypeselector;
}
/**
* getGroupList() queries the database for a list of all groups matching the current survey sid
*
*
* @param string $gid - the currently selected gid/group
*
* @return string string is returned containing <option></option> formatted list of groups to current survey
*/
function getGroupList($gid,$surveyid)
{
$groupselecter="";
$gid=sanitize_int($gid);
$surveyid=sanitize_int($surveyid);
if (!$surveyid) {$surveyid=returnGlobal('sid',true);}
$s_lang = Survey::model()->findByPk($surveyid)->language;
$gidquery = "SELECT gid, group_name FROM {{groups}} WHERE sid='{$surveyid}' AND language='{$s_lang}' ORDER BY group_order";
$gidresult = Yii::app()->db->createCommand($gidquery)->query(); //Checked
foreach ($gidresult->readAll() as $gv)
{
$groupselecter .= "<option";
if ($gv['gid'] == $gid) {$groupselecter .= " selected='selected'"; $gvexist = 1;}
$groupselecter .= " value='".Yii::app()->getConfig('scriptname')."?sid=$surveyid&gid=".$gv['gid']."'>".htmlspecialchars($gv['group_name'])."</option>\n";
}
if ($groupselecter)
{
if (!isset($gvexist)) {$groupselecter = "<option selected='selected'>".gT("Please choose...")."</option>\n".$groupselecter;}
else {$groupselecter .= "<option value='".Yii::app()->getConfig('scriptname')."?sid=$surveyid&gid='>".gT("None")."</option>\n";}
}
return $groupselecter;
}
function getGroupList3($gid,$surveyid)
{
//
$gid=sanitize_int($gid);
$surveyid=sanitize_int($surveyid);
if (!$surveyid) {$surveyid=returnGlobal('sid',true);}
$groupselecter = "";
$s_lang = Survey::model()->findByPk($surveyid)->language;
//$gidquery = "SELECT gid, group_name FROM ".db_table_name('groups')." WHERE sid=$surveyid AND language='{$s_lang}' ORDER BY group_order";
$gidresult = QuestionGroup::model()->findAllByAttributes(array('sid' => $surveyid, 'language' => $s_lang), array('order'=>'group_order'));
foreach ($gidresult as $gv)
{
$gv = $gv->attributes;
$groupselecter .= "<option";
if ($gv['gid'] == $gid) {$groupselecter .= " selected='selected'"; }
$groupselecter .= " value='".$gv['gid']."'>".htmlspecialchars($gv['group_name'])." (ID:".$gv['gid'].")</option>\n";
}
return $groupselecter;
}
/**
* put your comment there...
*
* @param mixed $gid
* @param mixed $language
*/
function getGroupListLang($gid, $language, $surveyid)
{
$groupselecter="";
if (!$surveyid) {$surveyid=returnGlobal('sid',true);}
$gidresult = QuestionGroup::model()->findAll(array('condition'=>'sid=:surveyid AND language=:language',
'order'=>'group_order',
'params'=>array(':surveyid'=>$surveyid,':language'=>$language))); //Checked)
foreach ($gidresult as $gv)
{
$gv = $gv->attributes;
$groupselecter .= "<option";
if ($gv['gid'] == $gid) {$groupselecter .= " selected='selected'"; $gvexist = 1;}
$link = Yii::app()->getController()->createUrl("/admin/questiongroups/sa/view/surveyid/".$surveyid."/gid/".$gv['gid']);
$groupselecter .= " value='{$link}'>";
if (strip_tags($gv['group_name']))
{