Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Using EM in the custom.js of the Theme

  • AritzP
  • AritzP's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
4 years 4 months ago #190398 by AritzP
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!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 4 months ago #190402 by tpartner
Replied by tpartner on topic Using EM in the custom.js of the Theme
1) If the questions are on different pages, I don't see a way to acquire the sub-question codes automatically.

2) I don't think you will easily get this to work from within custom.js as you cannot parse Expression Manager there so you would have to call a function from within the question source, providing many parameters.

A different approach may be to create a custom question theme and place the JS in the answer.twig file.

You could load the sub-question codes via custom question attributes in comma-separated lists. The JS could convert those lists to the arrays in your function.

Once you have the JS in your answer.twig file, you can access Expression Manager variables with something like:

Code:
var valorPreg4 = "{{ processString('{'+subquestionsArray[i]+'}') }}";

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 4 months ago #190406 by DenisChenu
Replied by DenisChenu on topic Using EM in the custom.js of the Theme

AritzP wrote: We are using a weighted formula to calculate the results of a survey, so we use Javascript instead of Expression Manager.

Why ?

Expression Manager can do weighted formula easily too …

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • gabrieljenik
  • gabrieljenik's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
4 years 4 months ago #190497 by gabrieljenik
Replied by gabrieljenik on topic Using EM in the custom.js of the Theme
I would create a plugin (maybe question theme as well) which:
- applies to short text questions
- adds question attribute where user can set source question.
- after completing the survey or as survey advances, the plugin would calculate the weighted value.

That would be easiee for the setup process.

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Checkout our Reporting Solutions and our plugin shop at www.encuesta.biz .

The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose