Skip to content

Commit 831c7ed

Browse files
committed
[spalenque] - #13206 * add nps formula for survey stats report in sangria
1 parent 3526d27 commit 831c7ed

3 files changed

Lines changed: 140 additions & 2 deletions

File tree

sangria/code/SangriaPageSurveyBuilderStatisticsExtension.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
)
250252
SQL;
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
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<div class="col-md-4 "style="height:450px">
2+
<h3>$Label</h3>
3+
<p>
4+
N = $Top.ParentPage.SurveyBuilderSurveyCountByQuestion($Top.QuestionID)
5+
&nbsp; / &nbsp;
6+
<% loop $Top.ParentPage.SurveyBuilderSurveyNPS($Top.QuestionID) %>
7+
<% if $Label == NPS %>
8+
$Label: $Value
9+
<% else %>
10+
<% if $Top.ParentPage.IsQuestionOnFiltering(nps) %>
11+
$Label: $Value%
12+
<% else %>
13+
<a href="$Top.ParentPage.Link($Top.ParentPage.Action)?qid=nps&vid=$Top.QuestionID:$Label$Top.ParentPage.SurveyBuilderDateFilterQueryString">
14+
$Label: $Value%
15+
</a>
16+
<% end_if %>
17+
&nbsp;|&nbsp;
18+
<% end_if %>
19+
<% end_loop %>
20+
</p>
21+
22+
<div class="row" style="text-align:center">
23+
<div class="col-md-4">
24+
<% loop getFormattedValues %>
25+
<% if $Label == 7 || $Label == 9 %>
26+
</div>
27+
<div class="col-md-4" style="border-left: 1px solid #ddd">
28+
<% end_if %>
29+
<div class="row" style="border-top: 1px solid #ddd;padding: 8px;">
30+
<div class="col-md-6">
31+
<% if $Top.ParentPage.IsQuestionOnFiltering($Up.ID) %>
32+
$Label
33+
<% else %>
34+
<a href="$Top.ParentPage.Link($Top.ParentPage.Action)?qid=$Up.ID&vid=$ID$Top.ParentPage.SurveyBuilderDateFilterQueryString">$Label</a>
35+
<% end_if %>
36+
</div>
37+
<div class="col-md-6">
38+
$Top.ParentPage.SurveyBuilderCountAnswers($Up.ID, $ID)
39+
</div>
40+
</div>
41+
<% end_loop %>
42+
</div>
43+
</div>
44+
45+
46+
</div>

sangria/templates/Layout/SangriaPage_ViewStatisticsSurveyBuilder.ss

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
<% include SangriaPage_StatisticsLanguage ParentPage=$Top, QuestionID=$ID %>
4646

4747
<% loop SurveyQuestions2Show %>
48-
<% if $ClassName == 'SurveyRadioButtonMatrixTemplateQuestion' %>
48+
<% if $Name == 'NetPromoter' %>
49+
<% include SangriaPage_StatisticsNetPromoter ParentPage=$Top, QuestionID=$ID %>
50+
<% else_if $ClassName == 'SurveyRadioButtonMatrixTemplateQuestion' %>
4951
<% include SangriaPage_StatisticsSurveyRadioButtonMatrixTemplateQuestion ParentPage=$Top, QuestionID=$ID %>
5052
<% else %>
5153
<% include SangriaPage_StatisticsMultiValueQuestionTemplate ParentPage=$Top, QuestionID=$ID %>

0 commit comments

Comments
 (0)