@@ -120,6 +120,8 @@ public function RenderCurrentFilters()
120120 continue ;
121121 } else if ($ qid == 'lang ' ) {
122122 $ output .= 'Language, ' ;
123+ } else if ($ qid == 'nps ' ) {
124+ $ output .= 'NPS, ' ;
123125 }
124126 $ q = $ template ->getQuestionById ($ qid );
125127 if (is_null ($ q )) {
@@ -249,6 +251,22 @@ private function generateFilters($survey_table_prefix = 'I')
249251 )
250252SQL ;
251253
254+ $ nps_query = <<<SQL
255+ AND EXISTS
256+ (
257+ SELECT * FROM SurveyAnswer A2
258+ INNER JOIN SurveyStep S2 ON S2.ID = A2.StepID
259+ INNER JOIN Survey I2 ON I2.ID = S2.SurveyID
260+ INNER JOIN SurveyTemplate SSTPL2 ON SSTPL2.ID = I2.TemplateID
261+ INNER JOIN SurveyQuestionValueTemplate SQVT2 ON SQVT2.ID = A2.`Value`
262+ WHERE
263+ I2.ClassName = ' {$ class_name }' AND I2.IsTest = 0
264+ AND SSTPL2.ID = %s AND A2.QuestionID = %s
265+ AND SQVT2.`Value` >= %s AND SQVT2.`Value` < %s
266+ AND I2.ID = {$ survey_table_prefix }.ID
267+ )
268+ SQL ;
269+
252270 $ filters_where = '' ;
253271
254272 if (!empty ($ from ) && !empty ($ to )) {
@@ -260,10 +278,24 @@ private function generateFilters($survey_table_prefix = 'I')
260278 $ filters = explode (', ' , $ filters );
261279 foreach ($ filters as $ t ) {
262280 $ t = explode (': ' , $ t );
263- $ qid = intval ($ t [0 ]);
281+ $ qid = is_int ( $ t [ 0 ]) ? intval ($ t [0 ]) : $ t [ 0 ] ;
264282 $ vid = is_int ($ t [1 ]) ? intval ($ t [1 ]) : $ t [1 ];
265283 if ($ qid == 'lang ' ) {
266284 $ filters_where .= sprintf ($ lang_query , $ template ->ID , $ vid );
285+ } else if ($ qid == 'nps ' ) {
286+ $ qid = $ t [1 ];
287+ $ vid = $ t [2 ];
288+ if ($ vid == 'D ' ) {
289+ $ lower = 0 ;
290+ $ upper = 7 ;
291+ } else if ($ vid == 'N ' ) {
292+ $ lower = 7 ;
293+ $ upper = 9 ;
294+ } else { //vid = P
295+ $ lower = 9 ;
296+ $ upper = 11 ;
297+ }
298+ $ filters_where .= sprintf ($ nps_query , $ template ->ID , $ qid , $ lower , $ upper );
267299 } else {
268300 if (count ($ t ) === 3 )
269301 $ vid = sprintf ('%s:%s ' , $ t [1 ], $ t [2 ]);
@@ -995,5 +1027,63 @@ public function SurveyBuilderCountLang($lang)
9951027 return DB ::query ($ query )->value ();
9961028 }
9971029
1030+ public function SurveyBuilderSurveyNPS ($ question_id )
1031+ {
1032+ $ template = $ this ->getCurrentSelectedSurveyTemplate ();
1033+ if (is_null ($ template )) return 0 ;
1034+
1035+ $ question = $ template ->getQuestionById ($ question_id );
1036+ if (is_null ($ question )) return 0 ;
1037+
1038+ $ class_name = $ this ->getCurrentSelectedSurveyClassName ();
1039+
1040+ $ filters_where = $ this ->generateFilters ();
1041+
1042+ $ query = <<<SQL
1043+ SELECT SUM(IF(V.`Value` < 7, 1, 0)) AS D, SUM(IF(V.`Value` > 6 AND V.`Value` < 9, 1, 0)) AS N, SUM(IF(V.`Value` > 8, 1, 0)) AS P
1044+ FROM SurveyAnswer A
1045+ INNER JOIN SurveyQuestionValueTemplate V ON V.ID = A.`Value`
1046+ LEFT JOIN SurveyStep S ON S.ID = A.StepID
1047+ LEFT JOIN Survey I ON I.ID = S.SurveyID
1048+ WHERE A.QuestionID = $ question_id AND A.`Value` IS NOT NULL AND
1049+ I.TemplateID = $ template ->ID AND I.ClassName = ' {$ class_name }' AND I.IsTest = 0
1050+ AND EXISTS
1051+ (
1052+ SELECT COUNT(A.ID) AS AnsweredMandatoryQuestionCount
1053+ FROM SurveyAnswer A
1054+ INNER JOIN SurveyStep STP ON STP.ID = A.StepID
1055+ INNER JOIN Survey S ON S.ID = STP.SurveyID
1056+ WHERE
1057+ S.ID = I.ID AND S.IsTest = 0 AND
1058+ A.QuestionID IN
1059+ (
1060+ SELECT Q.ID FROM SurveyQuestionTemplate Q
1061+ INNER JOIN SurveyStepTemplate STP ON STP.ID = Q.StepID AND STP.SurveyTemplateID = $ template ->ID
1062+ WHERE Q.Mandatory = 1 AND NOT EXISTS ( SELECT ID FROM SurveyQuestionTemplate_DependsOn DP WHERE SurveyQuestionTemplateID = Q.ID )
1063+ )
1064+ GROUP BY S.ID
1065+ )
1066+ {$ filters_where };
1067+ SQL ;
1068+
1069+ $ results = new ArrayList ();
1070+ $ total_count = $ this ->SurveyBuilderSurveyCountByQuestion ($ question_id );
1071+
1072+ if ($ total_count ) {
1073+ foreach (DB ::query ($ query ) as $ row ) {
1074+ $ d = round ($ row ['D ' ]/$ total_count ,2 )*100 ;
1075+ $ n = round ($ row ['N ' ]/$ total_count ,2 )*100 ;
1076+ $ p = round ($ row ['P ' ]/$ total_count ,2 )*100 ;
1077+
1078+ $ results ->push (new ArrayData (['Label ' => 'D ' , 'Value ' => $ d ]));
1079+ $ results ->push (new ArrayData (['Label ' => 'N ' , 'Value ' => $ n ]));
1080+ $ results ->push (new ArrayData (['Label ' => 'P ' , 'Value ' => $ p ]));
1081+ $ results ->push (new ArrayData (['Label ' => 'NPS ' , 'Value ' => ($ p - $ d )]));
1082+
1083+ }
1084+ }
1085+
1086+ return $ results ;
1087+ }
9981088
9991089}
0 commit comments