Hi,
We are using a weighted formula to calculate the results of a survey, so we use Javascript instead of Expression Manager.
The problem is that we have to copy and paste the same code (adding or deleting subquestions, and changing their codes) in all the questions where we calculate.
The code:
Code:
$(function(){
//Estos son los contadores que tendremos para saber cuantas subpreguntas son del tipo básico, intermedio o avanzado.
var contB = 0;
var contI = 0;
var contA = 0;
//Esta variable recoge el ID de la pregunta actual
var thisQuestion = $('#question{QID}');
//Esta variable nos dice cuantas subpreguntas tiene la pregunta.
var total = '{count(that.INFO012)}';
//Aquí tenemos los códigos de las subpreguntas.
var subPregunta1 = "INFO012_41B";
var subPregunta2 = "INFO012_42B";
var subPregunta3 = "INFO012_43B";
var subPregunta4 = "INFO012_44B";
var subPregunta5 = "INFO012_45I";
var subPregunta6 = "INFO012_46A";
var subPregunta7 = "INFO012_47I";
var subPregunta8 = "INFO012_48A";
var subPreguntasArray = [subPregunta1, subPregunta2, subPregunta3, subPregunta4, subPregunta5, subPregunta6, subPregunta7, subPregunta8];
//En este bucle identificamos el nivel de las subpreguntas.
for(var i = 0; i < total ;i++){
if (subPreguntasArray[i].slice(-1) == "B")
{
contB++;
}
else if (subPreguntasArray[i].slice(-1) == "I")
{
contI++;
}
else if (subPreguntasArray[i].slice(-1) == "A")
{
contA++;
}
}
//Aquí determinamos la prioridad de cada uno y obtenemos los porcentajes.
contB = contB * 1.25;
contI = contI * 1.5;
contA = contA * 1.75;
var contTotal = contB + contI + contA;
var porcentajeB = 1.25 / contTotal;
var porcentajeI = 1.5 / contTotal;
var porcentajeA = 1.75 / contTotal;
var valorPreg1 = '{that.INFO012.sq_1}';
var valorPreg2 = '{that.INFO012.sq_2}';
var valorPreg3 = '{that.INFO012.sq_3}';
var valorPreg4 = '{INFO012_44B}';
var valorPreg5 = '{that.INFO012.sq_5}';
var valorPreg6 = '{that.INFO012.sq_6}';
var valorPreg7 = '{that.INFO012.sq_7}';
var valorPreg8 = '{that.INFO012.sq_8}';
var valorPregArray = [valorPreg1, valorPreg2, valorPreg3, valorPreg4, valorPreg5, valorPreg6, valorPreg7, valorPreg8];
for(var i = 0; i < total ;i++){
//Aquí ponderamos los resultados
if (subPreguntasArray[i].slice(-1) == "B")
{
valorPregArray[i] = valorPregArray[i] * porcentajeB;
}
else if (subPreguntasArray[i].slice(-1) == "I")
{
valorPregArray[i] = valorPregArray[i] * porcentajeI;
}
else if (subPreguntasArray[i].slice(-1) == "A")
{
valorPregArray[i] = valorPregArray[i] * porcentajeA;
}
}
var valorTotal = 0;
//Finalmente sumamos todas las subpreguntas ponderadas para lograr el resultado
for(var i = 0; i < total; i++){
valorTotal = valorTotal + valorPregArray[i];
}
//Si la pregunta es de tipo Si-No, descomenta la siguiente línea
//valorTotal = valorTotal * 10;
//Redondeamos el valor.
var redondear = Math.round(valorTotal);
//Aquí es donde asignamos el valor total.
$('input:text', thisQuestion).val(redondear).trigger('keyup');
});
This is the section where we calculate the results:
Each question has the same js code but we only add or remove subquestions variables depending of the amount of subquestions of the question (this graphic example could be the next 2 pictures).
So we need two things:
1- Take the subquestion code automatically. I have tried taking them automatically, but it seems that Expression Manager is unidirectional (Currently, we give it manually)
2- And the most important one, a way to use this js code in the custom.js of the theme instead of copying and pasting this code in all the question that appears on the first picture.
We are using Limesurvey 3.17.7
Thanks for your time!