- Posts: 4
- Thank you received: 1
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Picking random X questions out of Y using extra function.
- Templar
- Topic Author
- Offline
- New Member
Less
More
4 months 2 days ago #253847
by Templar
Picking random X questions out of Y using extra function. was created by Templar
Please help us help you and fill where relevant:
Your LimeSurvey version: 6.4.0+231218
Own server or LimeSurvey hosting: Self-hosted
==================
I needed a possibility to randomly pick X questions out of Y, and existing solutions I didn't like - they were to complex to implement. So, I try to solve it another way. The Idea is not new, and theoretically should work: randomly collecting X questions' IDs in service question and checking inclusion as relevance condition.
I wrote a function getRandomlyPickedQuestionsIDs($surveyId, $groupId, $quantity), and put it in a plugin, extending ExpressionScript. I used it in equation question like {if(!is_empty(self), self, "--,"+getRandomlyPickedQuestionsIDs(SID, 24, 3)+",")}, and put strpos(Group1, ","+self.qid+",") as Condition.
But the problem is it works quite instabil: sometimes it shows questions, sometimes not. If I show them question by question, sometimes the question disappears right before my eyes. Please help.
The plugin and example I attached here, you just need to readjust groups' IDs after import in the ruling group.
Your LimeSurvey version: 6.4.0+231218
Own server or LimeSurvey hosting: Self-hosted
==================
I needed a possibility to randomly pick X questions out of Y, and existing solutions I didn't like - they were to complex to implement. So, I try to solve it another way. The Idea is not new, and theoretically should work: randomly collecting X questions' IDs in service question and checking inclusion as relevance condition.
I wrote a function getRandomlyPickedQuestionsIDs($surveyId, $groupId, $quantity), and put it in a plugin, extending ExpressionScript. I used it in equation question like {if(!is_empty(self), self, "--,"+getRandomlyPickedQuestionsIDs(SID, 24, 3)+",")}, and put strpos(Group1, ","+self.qid+",") as Condition.
But the problem is it works quite instabil: sometimes it shows questions, sometimes not. If I show them question by question, sometimes the question disappears right before my eyes. Please help.
The plugin and example I attached here, you just need to readjust groups' IDs after import in the ruling group.
Attachments:
Please Log in to join the conversation.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12941
- Thank you received: 3979
4 months 1 day ago #253929
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Picking random X questions out of Y using extra function.
Hi,
1. Split the questions into two groups
a. group 1 (visible) with x questions
b. group 2 (hidden) with the rest, y-x questions.
All questions get the same randomization group name.
Group 1:
2. A small javascript snippet to select randomly x out of y elements
You may use randomization groups or not.
And display the questions with the condition(s) (if I call the question with the javascript "QPool"):
strpos(QPool,"001")>0
strpos(QPool,"002")>0
strpos(QPool,"003")>0
This script can handle up to 999 questions arr.push(('00'+i).slice(-3));
You may hide this script either by javascript $('#question{QID}').hide(); or by the css clss "d-none"
.
Joffm
pardon, but what is too complex to implement one of these two "standared" solutions.they were to complex to implement.
1. Split the questions into two groups
a. group 1 (visible) with x questions
b. group 2 (hidden) with the rest, y-x questions.
All questions get the same randomization group name.
Group 1:
- Question 1
- Question 2
- Question 3
- Question 4
- Question 5
- Question 6
- Question 7
- Question 8
- Question 9
- Question 10
2. A small javascript snippet to select randomly x out of y elements
You may use randomization groups or not.
And display the questions with the condition(s) (if I call the question with the javascript "QPool"):
strpos(QPool,"001")>0
strpos(QPool,"002")>0
strpos(QPool,"003")>0
Code:
function shuffle(array) { var currentIndex = array.length, temporaryValue, randomIndex; while (0 !== currentIndex) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; } $(document).on('ready pjax:scriptcomplete',function(){ // Fill the array let countAllQuestions = 10; let countShown = 4; var arr = []; for (var i = 1; i <= countAllQuestions; i++) { arr.push(('00'+i).slice(-3)); } arr = shuffle(arr).slice(0,countShown).join(','); $('#question{QID} input[type="text"]').val('#'+arr); $('#question{QID}').hide(); }); </script>
This script can handle up to 999 questions arr.push(('00'+i).slice(-3));
You may hide this script either by javascript $('#question{QID}').hide(); or by the css clss "d-none"
.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu
Please Log in to join the conversation.
- Templar
- Topic Author
- Offline
- New Member
Less
More
- Posts: 4
- Thank you received: 1
4 months 1 day ago #253943
by Templar
Replied by Templar on topic Picking random X questions out of Y using extra function.
Hi,
and thanks! The first method should work in my case.
I was referring to this sample: manual.limesurvey.org/ExpressionScript_s..._of_the_questions.29 . I searched through forum, saw many variants, but yours I see for the first time. And I liked it :)
and thanks! The first method should work in my case.
I was referring to this sample: manual.limesurvey.org/ExpressionScript_s..._of_the_questions.29 . I searched through forum, saw many variants, but yours I see for the first time. And I liked it :)
Please Log in to join the conversation.