Welcome to the LimeSurvey Community Forum

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

Randomize questions in a group

  • dspaan
  • dspaan's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 years 8 months ago #84212 by dspaan
Randomize questions in a group was created 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.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 years 8 months ago #84214 by holch
Replied by holch on topic Randomize questions in a group
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.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • dspaan
  • dspaan's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 years 7 months ago #85572 by dspaan
Replied by dspaan on topic Randomize questions in a group
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.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 1 month 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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
5 years 1 month 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.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 1 month 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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
5 years 1 month ago #181726 by DenisChenu
Replied by DenisChenu on topic Randomize questions in a group

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 service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
5 years 1 month 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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.
More
4 years 5 days ago #197050 by matthewpulis
Replied by matthewpulis on topic Randomize questions in a group
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.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 5 days ago - 4 years 5 days ago #197053 by tpartner
Replied by tpartner on topic Randomize questions in a group
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: 4 years 5 days ago by tpartner.
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 4 days ago #197065 by holch
Replied by holch on topic Randomize questions in a group
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.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • Mazi
  • Mazi's Avatar
  • Offline
  • Official LimeSurvey Partner
  • Official LimeSurvey Partner
More
4 years 4 days 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
Need Help? We offer professional Limesurvey support: survey-consulting.com
Contact: marcel.minke(at)survey-consulting.com
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose