Complex scoring of a form and sharing it with the user - example | XM Community

Complex scoring of a form and sharing it with the user - example

  • 20 November 2018
  • 1 reply
  • 21 views

Badge +2
I wanted to score the SF-36, a popular general physical and mental health questionnaire and share the results with the client.

I needed to solve two main issues: How to do the scoring and how to do the workflow so that there would be the least amount of disruption and a user would receive a pdf with all 10 scores listed (8 primary and 2 summary scores).

The scoring was done by javascript as I couldn"t see how Math alone could do the job. The scoring feature didn"t work as it only shares one score in the end, even though you can calculate at least the 8 primary scales (I didn"t check whether the 2 summary scores could be done).

The latter was achieved by using a timing question, in a block of its own at the end, which runs the javascript on load and moves to the next question after 1 second with the text "Calculating scores ...". In the next block was a text paragraph giving the scores and by clicking on "next", the user received a pdf with the scores at the end.

Here is the javascript Part 1. I expect to put the second part (message too long otherwise) as an answer:

```
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/

/*Place your JavaScript here to run when the page is unloaded*/
// PF
var v1 = parseInt("${q://QID13/SelectedAnswerRecode/1}",10);
var v2 = parseInt("${q://QID13/SelectedAnswerRecode/2}",10);
var v3 = parseInt("${q://QID13/SelectedAnswerRecode/3}",10);
var v4 = parseInt("${q://QID13/SelectedAnswerRecode/4}",10);
var v5 = parseInt("${q://QID13/SelectedAnswerRecode/5}",10);
var v6 = parseInt("${q://QID13/SelectedAnswerRecode/6}",10);
var v7 = parseInt("${q://QID13/SelectedAnswerRecode/7}",10);
var v8 = parseInt("${q://QID13/SelectedAnswerRecode/8}",10);
var v9 = parseInt("${q://QID13/SelectedAnswerRecode/9}",10);
var v10 = parseInt("${q://QID13/SelectedAnswerRecode/10}",10);
PF = ((v1+v2+v3+v4+v5+v6+v7+v8+v9+v10-10) * 100) / (30-10);
Qualtrics.SurveyEngine.setEmbeddedData("PF",PF);

//RP
var v1 = parseInt("${q://QID14/SelectedAnswerRecode/1}",10);
var v2 = parseInt("${q://QID14/SelectedAnswerRecode/2}",10);
var v3 = parseInt("${q://QID14/SelectedAnswerRecode/3}",10);
var v4 = parseInt("${q://QID14/SelectedAnswerRecode/4}",10);
RP = ((v1+v2+v3+v4-4) * 100) / (8-4);
Qualtrics.SurveyEngine.setEmbeddedData("RP",RP);

var q28Val = "${q://QID17/SelectedChoicesRecode}";
var q29Val = "${q://QID18/SelectedChoicesRecode}";

var q28RecodeMap = {"1" : "6", "2" : "5.4", "3" : "4.2", "4" : "3.2", "5" : "2.2", "6" : "1"};
var q28Val_new ="";
q28Val_new = q28RecodeMap[q28Val];

var q29RecodeMap_1 = {"1" : "6", "2" : "4", "3" : "3", "4" : "2", "5" : "1"};
var q29RecodeMap_2 = {"1" : "5", "2" : "4", "3" : "3", "4" : "2", "5" : "1"};
var q29Val_new = "";
if(q28Val && q28Val == "1") {
q29Val_new = q29RecodeMap_1[q29Val];
} else {
q29Val_new = q29RecodeMap_2[q29Val];
}
BP = ((parseFloat(q28Val_new) + parseInt(q29Val_new) - 2) * 100) / 10;
BP = Math.round( BP * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("BP",BP);


// GH
var q2Val = "${q://QID11/SelectedChoicesRecode}";
var q12Val_1 = "${q://QID21/SelectedAnswerRecode/1}";
var q12Val_2 = "${q://QID21/SelectedAnswerRecode/2}";
var q12Val_3 = "${q://QID21/SelectedAnswerRecode/3}";
var q12Val_4 = "${q://QID21/SelectedAnswerRecode/4}";

var q2RecodeMap = {"1" : "5", "2" : "4.4", "3" : "3.4", "4" : "2", "5" : "1"};
var q2Val_new ="";
q2Val_new = q2RecodeMap[q2Val];

var q12RecodeMap = {"1" : "5", "2" : "4", "3" : "3", "4" : "2", "5" : "1"};
var q12Val_new_2 = "";
q12Val_new_2 = q12RecodeMap[q12Val_2];
var q12Val_new_4 = "";
q12Val_new_4 = q12RecodeMap[q12Val_4];

GH = ((parseFloat(q2Val_new) + parseInt(q12Val_1) + parseInt(q12Val_new_2) + parseInt(q12Val_3) + parseInt(q12Val_new_4) - 5) * 100) / (25-5);
GH = Math.round( GH * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("GH",GH);

// VT
var q10Val_1 = "${q://QID19/SelectedAnswerRecode/1}";
var q10Val_5 = "${q://QID19/SelectedAnswerRecode/5}";
var q10Val_7 = "${q://QID19/SelectedAnswerRecode/7}";
var q10Val_9 = "${q://QID19/SelectedAnswerRecode/9}";

var q10RecodeMap = {"1" : "6", "2" : "5", "3" : "4", "4" : "3", "5" : "2", "6" : "1"};
var q10Val_1_new ="";
q10Val_1_new = q10RecodeMap[q10Val_1];
var q10Val_5_new ="";
q10Val_5_new = q10RecodeMap[q10Val_5];

VT = ((parseInt(q10Val_1_new) + parseInt(q10Val_5_new) + parseInt(q10Val_7) + parseInt(q10Val_9 ) - 4) * 100) / 20;
//alert("VT: " + VT + " 1: " + parseInt(q10Val_1_new) + " 5: " + parseInt(q10Val_5_new) + " 7: " + parseInt(q10Val_7) + " 9: " + parseInt(q10Val_9));
Qualtrics.SurveyEngine.setEmbeddedData("VT",VT);


// SF
var q7Val = "${q://QID16/SelectedChoicesRecode}";
var q11Val = "${q://QID20/SelectedChoicesRecode}";

var q7Val_new ="";
q7Val_new = q12RecodeMap[q7Val];
// alert("q7Val: " + q7Val + " q11Val: " + q11Val + " q7Val_new: " + q7Val_new);
SF = ((parseInt(q7Val_new) + parseInt(q11Val) - 2) * 100) / 8;
// alert("SF: " + SF + " 1: " + parseInt(q17Val_new) + " 2: " + parseInt(q11Val_new));
SF = Math.round( SF * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("SF",SF);


// RE
var v1 = parseInt("${q://QID15/SelectedAnswerRecode/1}",10);
var v2 = parseInt("${q://QID15/SelectedAnswerRecode/2}",10);
var v3 = parseInt("${q://QID15/SelectedAnswerRecode/3}",10);
RE = ((v1+v2+v3-3) * 100) / (6-3);
RE = Math.round(RE * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("RE",RE);

//alert("RE: " + RE + " 1: " + parseInt(v1) + " 2: " + parseInt(v2) + " 3: " + parseInt(v3));


// MH
var q10Val_2 = "${q://QID19/SelectedAnswerRecode/2}";
var q10Val_3 = "${q://QID19/SelectedAnswerRecode/3}";
var q10Val_4 = "${q://QID19/SelectedAnswerRecode/4}";
var q10Val_6 = "${q://QID19/SelectedAnswerRecode/6}";
var q10Val_8 = "${q://QID19/SelectedAnswerRecode/8}";

var q10Val_4_new ="";
q10Val_4_new = q10RecodeMap[q10Val_4];
var q10Val_8_new ="";
q10Val_8_new = q10RecodeMap[q10Val_8];

MH = ((parseInt(q10Val_2) + parseInt(q10Val_3) + parseInt(q10Val_4_new) + parseInt(q10Val_6) + parseInt(q10Val_8_new) - 5) * 100) / 25;
//alert("MH: " + MH + " 1: " + parseInt(q10Val_2) + " 3: " + parseInt(q10Val_3) + " 4: " + parseInt(q10Val_4_new) + " 6: " + parseInt(q10Val_6) + " 8: " + parseInt(q10Val_8_new));
Qualtrics.SurveyEngine.setEmbeddedData("MH",MH);

var PF_ZC = "";
PF_ZC = ((parseFloat(PF) - 83.46290)/23.22864);
var RP_ZC = "";
RP_ZC = ((parseFloat(RP) - 80.28166)/34.83783);
var BP_ZC = "";
BP_ZC = ((parseFloat(BP) - 76.94163)/24.83714);
var GH_ZC = "";
GH_ZC = ((parseFloat(GH) - 71.81575)/20.35165);
var VT_ZC = "";
VT_ZC = ((parseFloat(VT) - 64.47694)/19.77187);
var SF_ZC = "";
SF_ZC = ((parseFloat(SF) - 85.05929)/22.29047);
var RE_ZC = "";
RE_ZC = ((parseFloat(RE) - 83.19165)/32.15215);
var MH_ZC = "";
MH_ZC = ((parseFloat(MH) - 75.97772)/16.96210);

// alert("PF_ZC: " + PF_ZC + " PF: " + PF);
// alert("MH_ZC: " + MH_ZC + " MH: " + MH);

```

1 reply

Badge +2
Part 2 of the javascript:
```

var GPH = "";
GPH = (((parseFloat(PF_ZC) * 0.47268) + (parseFloat(RP_ZC) * 0.38210) + (parseFloat(BP_ZC) * 0.36750) + (parseFloat(GH_ZC) * 0.18993) + (parseFloat(VT_ZC) * -0.01883) + (parseFloat(SF_ZC) * -0.01324) + (parseFloat(RE_ZC) * -0.14971) + (parseFloat(MH_ZC) * -0.27145)) * 10 + 50);
var GMH = "";
GMH = (((parseFloat(PF_ZC) * -0.24358) + (parseFloat(RP_ZC) * -0.13410) + (parseFloat(BP_ZC) * -0.12414) + (parseFloat(GH_ZC) * 0.05271) + (parseFloat(VT_ZC) * 0.27100) + (parseFloat(SF_ZC) * 0.26460) + (parseFloat(RE_ZC) * 0.35922) + (parseFloat(MH_ZC) * 0.48753)) * 10 + 50);
var GPH_ZC = (GPH - 50) / 10;
var GMH_ZC = (GMH - 50) / 10;

GPH = Math.round( GPH * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("GPH",GPH);
GMH = Math.round( GMH * 10 ) / 10;
Qualtrics.SurveyEngine.setEmbeddedData("GMH",GMH);
// alert("PF: " + PF + " RP: " + RP + " BP: " + BP + " GH: " + GH + " VT: " + VT + " SF: " + SF + " RE: " + RE + " MH: " + MH + " GPH: " + GPH + " GMH: " + GMH);

function GetZPercent(z)
{
//z == number of standard deviations from the mean

//if z is greater than 6.5 standard deviations from the mean
//the number of significant digits will be outside of a reasonable
//range
if ( z < -6.5)
return 0.0;
if( z > 6.5)
return 1.0;

var factK = 1;
var sum = 0;
var term = 1;
var k = 0;
var loopStop = Math.exp(-23);
while(Math.abs(term) > loopStop)
{
term = .3989422804 * Math.pow(-1,k) * Math.pow(z,k) / (2 * k + 1) / Math.pow(2,k) * Math.pow(z,k+1) / factK;
sum += term;
k++;
factK *= k;

}
sum += 0.5;
sum = sum * 100;
sum = Math.round( sum * 10 ) / 10;

return sum;
}

var PFPercent = GetZPercent(PF_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("PFPercent",PFPercent);
var RPPercent = GetZPercent(RP_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("RPPercent",RPPercent);
var BPPercent = GetZPercent(BP_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("BPPercent",BPPercent);
var GHPercent = GetZPercent(GH_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("GHPercent",GHPercent);
var VTPercent = GetZPercent(VT_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("VTPercent",VTPercent);
var SFPercent = GetZPercent(SF_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("SFPercent",SFPercent);
var REPercent = GetZPercent(RE_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("REPercent",REPercent);
var MHPercent = GetZPercent(MH_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("MHPercent",MHPercent);
var GPHPercent = GetZPercent(GPH_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("GPHPercent",GPHPercent);
var GMHPercent = GetZPercent(GMH_ZC);
Qualtrics.SurveyEngine.setEmbeddedData("GMHPercent",GMHPercent);


});

```

Leave a Reply