Welcome to the LimeSurvey Community Forum

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

Select 5 answers at Random from a Multiple Choice Question

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 years 2 weeks ago #227046 by davebostockgmail
Please help us help you and fill where relevant:
Your LimeSurvey version: 5.3.8
Own server or LimeSurvey hosting: Own
Survey theme/template: Fruity
==================
I have been looking for a solution to the following but the closest I have seen is in this old post that Joffm had the answer to my previous problem ....

[url] forums.limesurvey.org/forum/can-i-do-thi...iple-choice-question [/url]

What I need to do now is select 5 Answers from a Multiple Choice question to be used as a mask in future question ... I do not have an LSS for this as I am yet to start the survey so as an example it would be 

Multiple Choice Question with 20 answer options Hidden and all set with the answers to be true (Q1Possible)
Second Multiple Choice question that has all the answers but none selected (Q1Chosen)
Equation Question that selects 5 from the 20 answered options and sets that into Q1Chosen (EQ1)
Array Question that displays the 5 Selected options as the subquestions using the Array Filter from Q1Chosen (Arr1)

I am not sure if this is the most elegant way to do this or if I can just skip the first part and populate 5 answers at Q1Chosen with EQ1 rather than having to refer to a list holder question but this is how the older workaround did it ...

Thanks in advance for any help or advice.





 

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks ago #227047 by Joffm
So, you only want to display 5 out of 20 options randomly without user input?
And it is only the array question where this selection is displayed?

In this case you may do it by
inserting this script in a question of type "short text" (Q1)
Code:
<script type="text/javascript" charset="utf-8">
 
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
 
  // While there remain elements to shuffle...
  while (0 !== currentIndex) {
 
    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
 
    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
   return array;
}
  
    $(document).on('ready pjax:scriptcomplete',function(){
// Fill the array
      var arr = [];
      for (var i = 1; i < 21; i++) 
      { 
        w=("00" + i).slice(-2); 
        arr.push(w);
      }
 
      arr = shuffle(arr);
      anumbers = arr.slice(0,5).join(',');
      $('#question{QID} input[type="text"]').val("#"+anumbers);
//      $('#question{QID}').hide();
   });
</script>

You will get this
 

And the rows of the array you display with subquestion relevance using the function "strpos(haystack,needle)"
1st row: strpos(Q1,"01")>0
2nd row: strpos(Q1,"02")>0
...

Joffm

P.S. the hashtag is to avoid an index of "0".
Without it the result of strpos(Q1,"03") would be "0" (strpos starts counting at 0). But this also means "not found".
 

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 years 2 weeks ago #227049 by davebostockgmail
Replied by davebostockgmail on topic Select 5 answers at Random from a Multiple Choice Question
Thanks Joffm

Amazing as always

Dave

Please Log in to join the conversation.

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 years 2 weeks ago #227207 by davebostockgmail
Replied by davebostockgmail on topic Select 5 answers at Random from a Multiple Choice Question
Hi Joffm

I think I have spoken too soon .... this works well when the short text question is being displayed but when it is hidden the random selection fails to work.

I have attached an LSS file showing this behaviour ... The first time the short text is shown and the selection works as expected, the second time it is hidden and the random selection fails.

this is on version : Version 5.3.9+220411 and it is shown in a Question by Question format.

Any ideas on why this is the case?

Thanks
Dave
 

Please Log in to join the conversation.

More
2 years 2 weeks ago #227209 by jelo
Depends on how you hide the question. I wasn't able to import your LSS (seems to be a bug inside the LimeSurvey demo installation), but try to hide the question via a CSS class. You can see it here (my thread was about the different kinds of hiding thing)
forums.limesurvey.org/forum/future-featu...to-hide-the-question

The meaning of the word "stable" for users
www.limesurvey.org/forum/development/117...ord-stable-for-users

Please Log in to join the conversation.

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 years 2 weeks ago #227211 by davebostockgmail
Replied by davebostockgmail on topic Select 5 answers at Random from a Multiple Choice Question
Hi Jelo

It is hidden via the "Hide this question option" not via CSS

This is, as I understand it, a way to hide a question but still have the data accessible via the EM manager is that not the case?

Please Log in to join the conversation.

  • davebostockgmail
  • davebostockgmail's Avatar Topic Author
  • Offline
  • Elite Member
  • Elite Member
More
2 years 2 weeks ago #227214 by davebostockgmail
Replied by davebostockgmail on topic Select 5 answers at Random from a Multiple Choice Question
I have made it work by turning a display text question into a short free text question and hiding the answer box via CSS.

I know that previous versions used to allow you to always hide the question but retain the data ... not sure if this is now a bug or a feature?

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Away
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
2 years 2 weeks ago #227215 by Joffm
In the script of these questions there is the line
//      $('#question{QID}').hide();
After you have tested and everything is fine you just remove slashes to hide this question.
Or you hide it with css class "hidden".

But you can't hide it by "Always hide this question" In this case the question is not in the DOM, it is there.
But it has to be because it runs in the machine of the participant.

And now the obstacles.
If this question has to be there, only hidden,  you can't display your survey "question by question".
In this case you will see a blank screen with the navigation buttons.

Of course you can work around this by hiding the question with css and adding
$('#ls-button-submit').trigger('click');

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