Welcome to the LimeSurvey Community Forum

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

Randomize answer options in array (not subquestions)

  • elissa
  • elissa's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 4 months ago #128439 by elissa
Hi,
I would like to randomize answer options in an array question - not subquestions but answers options. Is there a setting or a workaround for that? I cannot find.
Regards
elisa

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 4 months ago - 8 years 4 months ago #128442 by tpartner
Hi Elisa. I think this should do the trick...

1) Add this function to the end of template.js:
Code:
function randomArrayCols(qID){
 
  var thisQuestion = $('#question'+qID);
 
  // Index the array columns
  $('table.subquestions-list tr', thisQuestion).each(function(i) {
    $('> *', this).each(function(i) {
      $(this).attr('data-index', i);
    });
  });
  if($('.noanswer-item', thisQuestion).length > 0) {
    $('table.subquestions-list tr > :last-child', thisQuestion).addClass('col-no-answer');
  }
 
  // Create an array of the columns
  var columns = [];
  $('table.subquestions-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) {
    columns.push($(this).attr('data-index'));
  });  
 
  // A function to shuffle elements or contents of arrays
  (function($){
    $.fn.shuffle = function() {
      return this.each(function(){
        var items = $(this).children();
        return (items.length)
          ? $(this).html($.shuffle(items))
          : this;
      });
    }
 
    $.shuffle = function(arr) {
      for(
        var j, x, i = arr.length; i;
        j = parseInt(Math.random() * i),
        x = arr[--i], arr[i] = arr[j], arr[j] = x
      );
      return arr;
 
    }
  })(jQuery);
 
  // Shuffle the columns array
  columns = $.shuffle(columns);
 
  // Reposition the row elements
  $('table.subquestions-list tr', thisQuestion).each(function(i) {
    var thisRow = $(this);
    $(columns).each(function(i, val) {
      thisRow.append($('[data-index="'+val+'"]', thisRow));
      thisRow.append($('.col-no-answer', thisRow));
    });
  });
}

2) Add this script to the source of each array you want randomized:
Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){
    randomArrayCols({QID});
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 8 years 4 months ago by tpartner.
The topic has been locked.
  • elissa
  • elissa's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 4 months ago #128443 by elissa
Hi Tony,
Thank you for your help but I'm testing it and cannot make it working. Columns stay in original order every time.
Regards
elisa

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 4 months ago #128444 by tpartner
0ops, sorry, I have a typo in the script for the question source. It should be this:

Code:
<script type="text/javascript" charset="utf-8">    
  $(document).ready(function(){
    randomArrayCols({QID});
  });
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: elissa
The topic has been locked.
  • elissa
  • elissa's Avatar Topic Author
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
8 years 4 months ago #128447 by elissa
Wow, now it works perfectly. Thank you very much! That's really great help for me.

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
More
6 years 1 month ago #163879 by zserban
Hello tpartner,

Should this work with ver. 2.7?
I am trying it without much success.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 1 month ago #163890 by tpartner
Probably not as it is - there have been many HTML changes. Can you attach a small sample survey containing only that question?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: zserban
The topic has been locked.
More
6 years 1 month ago #163891 by zserban
Sure, thanks for helping.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
6 years 1 month ago #163896 by tpartner
This function will work for 2.73.0:

Code:
function randomArrayCols(qID){
 
  var thisQuestion = $('#question'+qID);
 
  // Index the array columns
  $('table.subquestion-list tr', thisQuestion).each(function(i) {
    $('> *', this).each(function(i) {
      $(this).attr('data-index', i);
    });
  });
  if($('.noanswer-item', thisQuestion).length > 0) {
    $('table.subquestion-list tr > :last-child', thisQuestion).addClass('col-no-answer');
  }
 
  // Create an array of the columns
  var columns = [];
  $('table.subquestion-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) {
    columns.push($(this).attr('data-index'));
  });  
 
  // A function to shuffle elements or contents of arrays
  (function($){
    $.fn.shuffle = function() {
      return this.each(function(){
        var items = $(this).children();
        return (items.length)
          ? $(this).html($.shuffle(items))
          : this;
      });
    }
 
    $.shuffle = function(arr) {
      for(
        var j, x, i = arr.length; i;
        j = parseInt(Math.random() * i),
        x = arr[--i], arr[i] = arr[j], arr[j] = x
      );
      return arr;
 
    }
  })(jQuery);
 
  // Shuffle the columns array
  columns = $.shuffle(columns);
 
  // Reposition the row elements
  $('table.subquestion-list tr', thisQuestion).each(function(i) {
    var thisRow = $(this);
    $(columns).each(function(i, val) {
      thisRow.append($('[data-index="'+val+'"]', thisRow));
      thisRow.append($('.col-no-answer', thisRow));
    });
  });
}

Here is your test survey back with that function and call in the source of the question:

File Attachment:

File Name: limesurvey...5941.lss
File Size:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: zserban
The topic has been locked.
More
6 years 1 month ago #163897 by zserban
It sure does work.

Brilliant, thank you!
The topic has been locked.
More
5 years 8 months ago #170782 by zserban
Hi tpartner,


Do you have any idea if/how to do this in Limesurvey v3.13?


I uploaded an example.
The topic has been locked.
More
5 years 8 months ago #170783 by zserban
Nevermind, your code still works!

All one needs to do is to add the theme code in the custom.js, rather them template.js
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose