Welcome to the LimeSurvey Community Forum

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

Randomisation of array with textbox - all in same question group

  • Chrisou
  • Chrisou's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #197794 by Chrisou
After getting ample help in this thread on how to add a textbox to a basic array, I am now faced with the following problem:

I'd like to randomise my questions, but if I put everything in the same randomisation group, LS will sometimes put two "comment boxes" together to create something like you see attached. If I only randomise the actual questions, they will randomise, but the comments stay in the order I put them in originally.
Is there any way to couple the questions with the comment boxes so they randomise together while they're all in the same question group?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #197809 by tpartner
You will need to randomize the question order with JavaScript after inserting the text elements.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Chrisou
  • Chrisou's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #197814 by Chrisou
Okay, how do I do that?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago #197816 by tpartner
Can you provide a small sample survey (.lss file) containing only the relevant group.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Chrisou
  • Chrisou's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #197819 by Chrisou
Okay, I hope this works!
It should end up so that "Einstein Question" is paired with "Einstein Comment" and so on, but in random order.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 11 months ago - 3 years 11 months ago #197864 by tpartner
You will need to aggregate all of the scripts that insert the text elements into a single question, then you can run the randomization script after all questions have been manipulated.

Remove the scripts from all of your array questions and add this aggregated script to the source of the first array question in the group:

Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).on('ready pjax:scriptcomplete',function(){
 
    // Define a JS array for the question IDs
    var idsArr = [];
 
    // Loop through all of the array questions
    $('.array-flexible-row').each(function(i) {
 
      // Add this question ID to the JS array
      idsArr.push($(this).attr('id'));
 
      // Identify the questions
      var qArrayID = $(this).attr('id').replace(/question/, '');
      var qArray = $(this);
      var qMultiText = qArray.nextAll('.multiple-short-txt:eq(0)');
 
      // Add some classes
      qArray.addClass('array-with-uploads-question');
 
      //Hide the multi-short-text question
      $(qMultiText).hide();
 
      // Insert a new column
      $('table.questions-list col:not(.col-answers)', qArray).css('width', 'auto');
      $('table.questions-list thead tr', qArray).append('<th class="answer-text inserted-column-label" />');
      $('tr.answers-list', qArray).each(function(i) {
        $(this).append('<td class="answer-item text-item" />');
      });
 
      // Load the column label for the text inputs
      $('.inserted-column-label:eq(0)', qArray).text($('.ls-label-question', qMultiText).text());
      $('.text-item', qArray).append('<label class="ls-label-xs-visibility">'+$('.ls-label-question', qMultiText).text()+'</label>')
 
      // Loop through the multi-short-text sub-questions
      $('li.answer-item', qMultiText).each(function(i) {
        // Move the text inputs into the array
        $('input[type="text"]', this).appendTo($('tr.answers-list:eq('+i+') .text-item', qArray));
      });
    });
 
    // Shuffle the JS array of IDs
    shuffleArray(idsArr);
 
    // Insert the array quesions in the new order
    $.each(idsArr, function(i, value) {
      console.log('#'+value);
      $('.group-container').append($('#'+value));
    });
 
  });
 
  function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
      var j = Math.floor(Math.random() * (i + 1));
      var temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
    return array;
  }
</script>

Sample survey attached:

File Attachment:

File Name: limesurvey...9378.lss
File Size:43 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 3 years 11 months ago by tpartner.
The following user(s) said Thank You: Chrisou
The topic has been locked.
  • Chrisou
  • Chrisou's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
3 years 11 months ago #197887 by Chrisou
Thank you so much, it works perfectly!! :laugh:
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose