-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleastFilledStatFunctions.php
81 lines (69 loc) · 2.32 KB
/
leastFilledStatFunctions.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
<?php
/**
* This file extends the statFunctions plugin
* @version 0.0.1
*/
namespace leastFilledFunctions;
use Yii;
use CHtml;
use LimeExpressionManager;
use Survey;
use Question;
use SurveyDynamic;
use CDbCriteria;
use Permission;
use LimeSurvey\PluginManager\LimesurveyApi as LimesurveyApi;
use statFunctions;
class leastFilledStatFunctions
{
/**
* Return the least filled response on current ExpressionScript Engine survey question
* @param string $qCode : code of question, currently must be existing sgqa. Sample Q01.sgqa.
* @param boolean $submitted (or not) response
* @param boolean $self include (or not) current response
* @return integer|string
*/
public static function statLF($qCode, $submitted = true, $self = true)
{
$api = new LimesurveyApi();
$surveyId = $api->getCurrentSurveyid(true);
if (!$surveyId) {
return 0;
}
$qtmp = explode("X",$qCode);
$qid = null;
if (isset($qtmp[2])) {
$qid = $qtmp[2];
} else {
return null;
}
$oQuestion = Question::model()->find(
"qid = :qid and sid = :sid",
array(":qid" => $qid, ":sid" => $surveyId)
);
if ($oQuestion && $oQuestion->parent_qid) {
$oQuestion = Question::model()->find(
"qid = :qid and sid = :sid",
array(":qid" => $oQuestion->parent_qid, ":sid" => $surveyId)
);
}
if (empty($oQuestion)) {
if (Permission::model()->hasSurveyPermission($surveyId, 'surveycontent')) { // update ???
return sprintf(gT("Invalid question code or ID “%s”"), CHtml::encode($qCode));
}
return null;
}
// get all answer codes then getcount on all of them
$aAnswersFilled=array();
$countFunctions = new \statFunctions\countFunctions();
foreach($oQuestion->answers as $key => $value) {
$aAnswersFilled[$value['code']]=$countFunctions->statCountIf($qCode,$value['code'],$submitted,$self);
}
if(!empty($aAnswersFilled)) {
//return least filled code
asort($aAnswersFilled);
return key($aAnswersFilled);
}
return "";
}
}