Welcome to the LimeSurvey Community Forum

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

Small solution to randomize responses

  • Marksom
  • Marksom's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
5 years 1 week ago - 5 years 1 week ago #183033 by Marksom
Small solution to randomize responses was created by Marksom
OfflineSurveys does not yet allow random responses.

But as I needed to use it, I created a function to resolve temporarily, and that worked well for me. So I'm sharing it to anyone who needs it.

Add the following function to the question source code:
Code:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
     randomItems({
            qid : '{QID}', //qid of question
            array : false, //True for array subquestions or False for radio, checkbox.
            excluir : ['other'] //exclude randomization option
     });
});
</script>


And add the following function to your template.js
Code:
function randomItems(data){
 
  var el = $('#question'+data.qid);
  var pr = data.array==true?'.answers-list':'.answer-item';
  var o = [];
 
  el.find(pr).each(function(i,v){
    var id = $(this).attr('id').split(data.qid).pop();
    if(data.excluir.indexOf(id)==-1){
      o.push(id);
    }
  });
 
  for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
 
  $.each(o,function(i,v){
    el.find(pr+'[id$="'+data.qid+''+v+'"]').insertBefore(el.find(pr+':eq('+i+')'));
  });
 
}

So that's it, I hope this code helps somebody.
Last edit: 5 years 1 week ago by Marksom.
The following user(s) said Thank You: aaskoura
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose