Welcome to the LimeSurvey Community Forum

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

Random Groups with Random Items

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 weeks 22 hours ago - 2 weeks 21 hours ago #263171 by davebostockgmail
Random Groups with Random Items was created by davebostockgmail
Please help us help you and fill where relevant:
Your LimeSurvey version: 5.6.61+240430
Own server or LimeSurvey hosting: Own Server
Survey theme/template: Modified Fruity (I think)
==================
What I would like to do is to have, and I am not sure if this is possible, is the randomisation of subgroups and then the randomisation of the answer within each subgroup in a multiple choice question.

For example if we have a question with the following answer options
Subgroup Heading 1
Answer 1
Answer 2
Answer 3
Subgroup Heading 2
Answer 4
Answer 5
Answer 6
Subgroup Heading 3
Answer 7
Answer 8
Answer 9

... then I would like it randomise the subgroups so for example it could be subgroup 2 then 1 then 3, and within each group randomise the answers so answer 8 then 7 then 9

I have attached a .lss file with this structure and the code to do the sub headers for the groups but I need to know if the randomisation pattern is possible.

Thanks for any help in advance
Dave

 

File Attachment:

File Name: RandomWith...ders.lss
File Size:31 KB


 
Last edit: 2 weeks 21 hours ago by davebostockgmail. Reason: Edited to clarify the request

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 21 hours ago #263183 by Joffm
Replied by Joffm on topic Random Groups with Random Items
You want to shuffle the groups. Enter the same randomization group name on group level.
You want to shuffle the questions withing a group
In each group enter the same randomization group name on question level, but different from the name in the other groups.

Group 1: RG1
Item 1: RQ1
Item 2: RQ1
Item 3: RQ1
Group 2: RG1
Item 4: RQ2
Item 5: RQ2
Item 6: RQ2
Group 3: RG1
Item 7: RQ3
Item 8: RQ3
Item 9: RQ3

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 weeks 21 hours ago - 2 weeks 21 hours ago #263185 by davebostockgmail
Replied by davebostockgmail on topic Random Groups with Random Items
Hi Joffm and thanks for the response.
Maybe I wasn't clear in my description, I know how to randomise groups etc. This was to randomise answers in a multiple choice question, keeping items within subheaders etc. as per the lss file. I have updated the original post for clarity

thanks
Dave
Last edit: 2 weeks 21 hours ago by davebostockgmail. Reason: Edited to clarify the request

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 18 hours ago - 2 weeks 18 hours ago #263205 by Joffm
Replied by Joffm on topic Random Groups with Random Items
War noch ein Fehler drin.

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 2 weeks 18 hours ago by Joffm.

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 18 hours ago #263207 by Joffm
Replied by Joffm on topic Random Groups with Random Items
Hi, please, do not confuse us.
You are a long time user to know that "group" has a special meaning in the LS structure.

Well, my idea is:
In a question of type "short text" (QOrder) create the order.
  • Three arrays with the question codes without the code of the header
  • Shuffle the arrays
  • Put the header codes at the beginning of each array
  • Create a random number (1-6)
  • Depending on this number "concat" the arrays
  • Put the "other" code at the and of the final array
Code:
<script type="text/javascript">
  $(document).on('ready pjax:scriptcomplete',function(){
 
function shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
        var j = Math.floor(Math.random() * (i + 1));
        var temp = array;
        array = array[j];
        array[j] = temp;
    }
    return array;
}
       
    //Identify this question
    var thisQuestion = $('#question{QID}');
    var thisAnswerList = $('tr.answers-list:eq(0)', thisQuestion).parent();
    
    // Create the arrays with "normal" codes
    var array1=[2,3,4];
    var array2=[6,7,8,9];
    var array3=[11,12];
    var arrayG=;
    
    // Shuffle the arrays
    shuffleArray(array1);
    shuffleArray(array2);
    shuffleArray(array3);
 
    // Add the Header code
    array1.unshift(1);
    array2.unshift(5);
    array3.unshift(10);
 
    // Create a random number (1-6)
    var rn=Math.floor(Math.random() * 6 + 1);
    
    if(rn==1) {  arrayG=array1.concat(array2,array3); }
    if(rn==2) {  arrayG=array1.concat(array3,array2); }
    if(rn==3) {  arrayG=array2.concat(array1,array3); }
    if(rn==4) {  arrayG=array2.concat(array3,array1); }
    if(rn==5) {  arrayG=array3.concat(array1,array2); }
    if(rn==6) {  arrayG=array3.concat(array2,array1); }
 
    // Add the "other"code
   arrayG.push('other');
   $('input:text', thisQuestion).val(arrayG);    
  });
</script>

In your multiple question insert this script
Code:
<script type="text/javascript" charset="utf-8">
  $(document).on('ready pjax:scriptcomplete',function(){
 
    //Identify this question
    var thisQuestion = $('#question{QID}');
    var thisAnswerList = $('li.answer-item:eq(0)', thisQuestion).parent();
 
    // Retrieve the answer codes from the "randomOrder" question
    var answerCodes = '{QOrder}'.split(',');
 
    // Loop through the answer codes
    $.each(answerCodes, function(i, val) {
      // Move the answer item
      $(thisAnswerList).append($('li.answer-item[id$="X{QID}'+val+'"]', thisQuestion));
    });
  });
</script>
 
 
The remaining issue is:
You removed the checkboxes of the headers fix.
You should remove the headers if the codes are equal a header code.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 17 hours ago #263209 by Joffm
Replied by Joffm on topic Random Groups with Random Items
I changed the script to remove the checkboxes like this
Not the best, but it works

These are the only possible positions of the different headers
Code:
<script charset="utf-8" type="text/javascript">
 
        $(document).ready(function() {
          var answerCodes = '{QOrder}'.split(',');
          var x1 = answerCodes.indexOf("1");
          var x5 = answerCodes.indexOf("5");
          var x10 = answerCodes.indexOf("10");
 
          $( '#question{QID} .question-item:eq(0)').addClass('hide-pseudo-elements').find('input.checkbox').remove();
          if(x1==3) { $( '#question{QID} .question-item:eq(3)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x1==5) { $( '#question{QID} .question-item:eq(5)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x1==8) { $( '#question{QID} .question-item:eq(8)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x5==3) { $( '#question{QID} .question-item:eq(3)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x5==4) { $( '#question{QID} .question-item:eq(4)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x5==7) { $( '#question{QID} .question-item:eq(7)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x10==4) { $( '#question{QID} .question-item:eq(4)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x10==5) { $( '#question{QID} .question-item:eq(5)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
          if(x10==9) { $( '#question{QID} .question-item:eq(9)').addClass('hide-pseudo-elements').find('input.checkbox').remove(); }
     });
</script>

And the css
Code:
<style type="text/css">.hide-pseudo-elements label::before,
  .hide-pseudo-elements label::after
  {
    display: none;
  }
  .hide-pseudo-elements .label-text
  {
    margin-left: -40px;
  }
</style>

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: davebostockgmail

Please Log in to join the conversation.

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 weeks 1 hour ago #263236 by davebostockgmail
Replied by davebostockgmail on topic Random Groups with Random Items
Amazing and thank you as always this is perfect for my needs.

Apologies again for not being clear in my first post,

Thanks and have a great weekend
Dave

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 weeks 54 minutes ago #263238 by Joffm
Replied by Joffm on topic Random Groups with Random Items
Hi,
you can shorten the second script to
Code:
<script charset="utf-8" type="text/javascript">
 
        $(document).ready(function() {
          var answerCodes = '{QOrder}'.split(',');
          var x1 = answerCodes.indexOf("1");
          var x5 = answerCodes.indexOf("5");
          var x10 = answerCodes.indexOf("10");
          $( '#question{QID} .question-item:eq('+x1+')').addClass('hide-pseudo-elements').find('input.checkbox').remove();
          $( '#question{QID} .question-item:eq('+x5+')').addClass('hide-pseudo-elements').find('input.checkbox').remove();
          $( '#question{QID} .question-item:eq('+x10+')').addClass('hide-pseudo-elements').find('input.checkbox').remove();
     });
</script>

In my previous solution I missed the quotes. Therefore the "brute force" solution

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: davebostockgmail

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose