Welcome to the LimeSurvey Community Forum

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

Randomize questions in a group

More
13 years 3 weeks ago #84212 by dspaan
Hi,

I want to randomize the order of questions in a question group.

I used this javascript which i found in the wiki and pasted it in the code format field group desctiption:
Code:
<script type="text/javascript" charset="utf-8">
 
  $(document).ready(function() {
 
    function shuffleQuestions() {
 
      // Create an array to hold question IDs
      var qArray = new Array();
 
      // Find the group ID of the page
      var fieldNames = $( 'input#fieldnames' ).attr('value');
      var tmp = fieldNames.split('X');
      var sID = tmp[0];
      var gID = tmp[1];
 
      // A function to get the value of a cookie by name
      function getCookie ( cookieName ) {
          var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );
        if ( results ) {
          return ( unescape ( results[2] ) );
        }
        else {
          return null;
        }
      }
 
      // Get the session name
      var sessionName = getCookie ( 'PHPSESSID' );
 
      // Check to see if the question order has already been shuffled in this session
      var cookieArray = getCookie ( 'sArray' + gID + '_' + sessionName );
 
      // If already shuffled, use that question order
      if ( cookieArray ) {
        //qArray = $.trim(cookieArray);
        qArray = cookieArray.split(',');
      }
      // If not, go ahead and shuffle
      else {
        // Load the IDs of all questions on the page into the array 
        $( 'div[id^="question"]' ).each(function(i) {
          qArray.push($( this ).attr('id'));
        });
 
        // Shuffle the questions array
        function shuffle(o){
          for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
          return o;
        }      
        shuffle(qArray);
 
        // Set a cookie with the value of the shuffled array
        var today = new Date();
        today.setTime( today.getTime() );
        var expires = 2 * 1000 * 60 * 60 * 24; // Expire the cookie in 2 days
        var expiresDate = new Date( today.getTime() + (expires) );
        document.cookie = 'sArray' + gID + '_' + sessionName + '=' + qArray + ';expires=' + expiresDate.toGMTString();
      }
 
      // Wrap the questions in a div
      $( 'div[id^="question"]' ).wrapAll( document.createElement('div') );
      $( 'div[id^="question"]:eq(0)' ).parent().attr('id', 'qWrapper');
      $( 'div#qWrapper' ).css({
        'padding':0,
        'margin':0
      });  
 
      // Insert the questions into the wrapper in the shuffled order
      $.each(qArray, function(i, val){
        $( 'div#' + val + '' ).appendTo( $( '#qWrapper' ) );
      });
 
    }
 
    shuffleQuestions();
 
  });
 
</script>

In the global options i have set Filter HTML XSS to 'No'. But when i use the test button to test the survey the questions in that group are NOT randomized.

I'm using Limesurvey Version 1.92+ Build 120801.
What could be causing it not to work?

In this group there is one question that has a condition which is related to another question in the same group. I suppose i should remove that one?

There are also 3 other questions which should only be shown when a condition is met but the are related to answers on questions in previous groups. I suppose that should not be a problem?
The topic has been locked.
More
13 years 3 weeks ago #84214 by holch
I am not quite sure if I am getting you right, but Limesurvey has a feature to randomize questions within a group already.

Have a look at "randomization group" (not the same as a question group).

So you can give all those questions, that should be randomized among each other, the same "randomization group name" and then all questions with the same randomization group name will be randomized among each other.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
12 years 11 months ago #85572 by dspaan
Thanks Holch. You got me right.

I had to dig a bit in the forum and even the bugtracker to find out how it worked but it was quite simple. I simply had to got to the question settings and give all questions that needed to be randomized the same groupname in the Randomization Group name field.
The topic has been locked.
More
6 years 5 months ago #181670 by Mazi
Replied by Mazi on topic Randomize questions in a group
Maybe this is helpful to others: We have written a lengthy blog post about randomizing items at Limesurvey and have also outlined how to randomly show a set of questions, see survey-consulting.com/how-to-randomize-i...ow_questions_on_page

Best regards/Beste Grüße,
Dr. Marcel Minke
survey-consulting.com
offlinesurveys.com
Feel free to contact me by email for professional LimeSurvey support!
The topic has been locked.
More
6 years 5 months ago #181696 by jelo
Replied by jelo on topic Randomize questions in a group
Is SEO time?

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users
The topic has been locked.
More
6 years 5 months ago #181699 by Mazi
Replied by Mazi on topic Randomize questions in a group

jelo wrote: Is SEO time?

Well, if I spent quite some time writing such a blog post for helping others, why not linking it at the appropriate topic so others could also find it?

Best regards/Beste Grüße,
Dr. Marcel Minke
survey-consulting.com
offlinesurveys.com
Feel free to contact me by email for professional LimeSurvey support!
The topic has been locked.
More
6 years 5 months ago #181726 by DenisChenu

Mazi wrote:

jelo wrote: Is SEO time?

Well, if I spent quite some time writing such a blog post for helping others, why not linking it at the appropriate topic so others could also find it?

Yes, but same post on 6 years and more older topics … :unsure: :(

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member. - Professional support - Plugins, theme and development .
I don't answer to private message.
The topic has been locked.
More
6 years 5 months ago #181729 by Mazi
Replied by Mazi on topic Randomize questions in a group
Yes, because those show up first when checking the forum for questions about randomization at Limesurvey.
Just because a topic is old it doesn't mean it is not relevant and since some of these old topics still deal with adding additional code for randomization it helps adding a hint that with the new version there are easier to use solutions.

Best regards/Beste Grüße,
Dr. Marcel Minke
survey-consulting.com
offlinesurveys.com
Feel free to contact me by email for professional LimeSurvey support!
The topic has been locked.
More
5 years 4 months ago #197050 by matthewpulis
Sorry for reviving this thread.

I have followed your suggestion to randomize the questions within a group. I added all the questions to 'rand' in the randomization fied, and in the group settings, in the field, randomization group, I added 'rand' but the questions are not being randomized. Anything I'm missing please?

Thanks!
The topic has been locked.
More
5 years 4 months ago - 5 years 4 months ago #197053 by tpartner
If you want randomized questions within a group, you do not need to put anything the the group "Randomization group" setting - that is to randomize groups.

All you need to do is place a common value in the question "Randomization group name" setting for all questions you want randomized.


Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 5 years 4 months ago by tpartner.
The topic has been locked.
More
5 years 4 months ago #197065 by holch
Just to add a little to Tpartners response. You can have 3 levels of 'randomization':

1. Randomization of answer options / subquestions
You'll find this as a setting within each question. This allows you to randomize the order of your answer options and subquestions within each question.

2. Randomization of questions
As Tpartner highlighted, you basically define which questions should be randomized with each other by giving them the same name in "Randomization group name". This even allows you to leave some questions always at the same position, if you don't include them into the same "Randomization group name", while other rotate around.

3. Randomization of question groups
Similar to randomizing questions, you can randomize question groups among them, by giving them the same name in randomization group.

Help us to help you!
  • Provide your LS version and where it is installed (own server, uni/employer, SaaS hosting, etc.).
  • Always provide a LSS file (not LSQ or LSG).
Note: I answer at this forum in my spare time, I'm not a LimeSurvey GmbH employee.
The topic has been locked.
More
5 years 4 months ago #197093 by Mazi
Replied by Mazi on topic Randomize questions in a group
Thanks for this helpful summary, holch!

We have created a blog post about the different randomization options at Limesurvey. Have a look at survey-consulting.com/how-to-randomize-items-at-limesurvey

Best regards/Beste Grüße,
Dr. Marcel Minke
survey-consulting.com
offlinesurveys.com
Feel free to contact me by email for professional LimeSurvey support!
The topic has been locked.
More
5 years 4 months ago #197101 by matthewpulis
Thanks both :)


That is what I did, however the questions in the question group remain in the order I gave them. The names of the questions are Qs1 - Qs34 .. could it be because they are sequential?


Thanks!
The topic has been locked.
More
5 years 4 months ago #197103 by tpartner
Please attach a small sample survey (.lss file) containing only that 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.
More
5 years 4 months ago #197105 by matthewpulis
Hi Tony

As I was exporting, and importing into a test survey, I tested again and realised that if you do Preview Survey the questions are randomized, but if you do Preview Question Group they are not.

So that was my issue - I didn't realise that randomization happens only on Survey preview not on Group preview

Thanks!
The topic has been locked.
Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose