- Posts: 16
- Thank you received: 0
Welcome to the LimeSurvey Community Forum
Ask the community, share ideas, and connect with other LimeSurvey users!
Using a self-defined array to randomize questions
- lea_crtl
- Topic Author
- Offline
- New Member
Less
More
7 months 2 weeks ago - 7 months 2 weeks ago #250369
by lea_crtl
Using a self-defined array to randomize questions was created by lea_crtl
LimeSurvey version: 3.24.3
==================
Hi everyone,
I'm a PhD student taking her first steps with LimeSurvey, and am trying to do something that I thought would be very straightforward but after days of looking into it with no answer I am starting to have doubts. I am sorry if some similar question has already been asked on the forum, but I really could not find any satisfying explanation or solution.
My survey consists in successively submitting pairs of images to the user, for them to choose one. The pattern of randomization is complicated to explain (in brief: some images should not end up in the same pair), and is not the point of my question, but after careful consideration, it seems that defining an array at the beginning of the survey storing all possible pairs of pictures and shuffling it would solve the issue. This array would be 2-dimensional and look like this:
This array would allow me to retrieve valid pairs of images in the subsequent questions where images are submitted to the user.
I had anticipated to do this in the following way:
Unfortunately, it does not work: my main hypothesis is that arr was not properly defined.
Here are the steps I followed to define it:
1. I created a question of type Equation. I set it to "Mandatory" and set the option "Always hide this question" to ON. The code of the question is arr, and its source code is:
(I willingly defined a very basic array)
This JS code is pretty simple compared to many examples I saw on this forum which use jquery, but I don't think that in my case I need to do more.
2. In some following questions, I tried to import some image using arr. Here is again some example of source code:
Unfortunately no image is displayed.
Could you please help me? I am willing to investigate and understand what could be the issue but I'm lacking knowledge and experience I suppose. And I found nothing in the documentation that could help me solve this issue. Any help would be very much appreciated! And if you need any supplementary information please ask.
Thank you very much for reading and have a nice day/evening,
Best regards
Léa
==================
Hi everyone,
I'm a PhD student taking her first steps with LimeSurvey, and am trying to do something that I thought would be very straightforward but after days of looking into it with no answer I am starting to have doubts. I am sorry if some similar question has already been asked on the forum, but I really could not find any satisfying explanation or solution.
My survey consists in successively submitting pairs of images to the user, for them to choose one. The pattern of randomization is complicated to explain (in brief: some images should not end up in the same pair), and is not the point of my question, but after careful consideration, it seems that defining an array at the beginning of the survey storing all possible pairs of pictures and shuffling it would solve the issue. This array would be 2-dimensional and look like this:
Code:
arr = [[img1, img2], [img1, img3], ... , [img1, imgn], ..., [img(n-1), imgn]]
This array would allow me to retrieve valid pairs of images in the subsequent questions where images are submitted to the user.
I had anticipated to do this in the following way:
Code:
<img alt="" src="/upload/surveys/248689/images/{arr[1][1]}.jpg" />
Unfortunately, it does not work: my main hypothesis is that arr was not properly defined.
Here are the steps I followed to define it:
1. I created a question of type Equation. I set it to "Mandatory" and set the option "Always hide this question" to ON. The code of the question is arr, and its source code is:
Code:
<script type="text/javascript" charset="utf-8"> let arr = [[11, 12], [11, 13]]; </script>
(I willingly defined a very basic array)
This JS code is pretty simple compared to many examples I saw on this forum which use jquery, but I don't think that in my case I need to do more.
2. In some following questions, I tried to import some image using arr. Here is again some example of source code:
Code:
<img alt="" src="/upload/surveys/248689/images/{arr[1][1]}.jpg" />
Unfortunately no image is displayed.
Could you please help me? I am willing to investigate and understand what could be the issue but I'm lacking knowledge and experience I suppose. And I found nothing in the documentation that could help me solve this issue. Any help would be very much appreciated! And if you need any supplementary information please ask.
Thank you very much for reading and have a nice day/evening,
Best regards
Léa
Last edit: 7 months 2 weeks ago by lea_crtl. Reason: Bad code display
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11660
- Thank you received: 2742
7 months 2 weeks ago #250370
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Using a self-defined array to randomize questions
You can NOT use Javascript in questions that are hidden via the GUI function "Always hide this question" to ON.
Why? Javascript is executed in the browser, but with the setting "always hide this question" the question doesn't appear in the source code of the page created by Limesurvey. You can only use Expression Script/Manager as this is executed also on the server.
So the first step should be to hide your question via CSS by giving it the class ".hidden" in the GUI.
but I think you are doing this far too complicated. You need to create all the question pairs anyway and then pipe the images in randomly, right?
Why not create all the image pairs with their respective question(s) and then randomize the questions via "randomization group"?
Why? Javascript is executed in the browser, but with the setting "always hide this question" the question doesn't appear in the source code of the page created by Limesurvey. You can only use Expression Script/Manager as this is executed also on the server.
So the first step should be to hide your question via CSS by giving it the class ".hidden" in the GUI.
but I think you are doing this far too complicated. You need to create all the question pairs anyway and then pipe the images in randomly, right?
Why not create all the image pairs with their respective question(s) and then randomize the questions via "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 following user(s) said Thank You: lea_crtl
Please Log in to join the conversation.
- tpartner
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 10109
- Thank you received: 3595
7 months 2 weeks ago #250372
by tpartner
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Replied by tpartner on topic Using a self-defined array to randomize questions
You cannot write values to equation questions via JS.
If you need to write values to a question, use a short-text question, hide it with CSS (as Holch suggests) and write to it something like...
If you need to write values to a question, use a short-text question, hide it with CSS (as Holch suggests) and write to it something like...
Code:
$('#question{QID} :text').val('...my value...').trigger('keyup');
Cheers,
Tony Partner
Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: lea_crtl
Please Log in to join the conversation.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12940
- Thank you received: 3979
7 months 2 weeks ago #250373
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Using a self-defined array to randomize questions
And furthermore you can't access your javascript array with ExpressionScript.
You have to put the script into a text question that the final array appears there.
Then you can proceed with ExpressionScript.
And you need your shuffled array in your database. Otherwise you don't know which pair was displayed, if you taylor the display as you showed.
This should be rather easy.
But you should explain a bit more.
How many images?
Hiw many combinations do you want to display to the respondent.
10 images -> 45 pairs minus the forbidden ones.
Joffm
You have to put the script into a text question that the final array appears there.
Then you can proceed with ExpressionScript.
And you need your shuffled array in your database. Otherwise you don't know which pair was displayed, if you taylor the display as you showed.
This should be rather easy.
But you should explain a bit more.
How many images?
Hiw many combinations do you want to display to the respondent.
10 images -> 45 pairs minus the forbidden ones.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: lea_crtl
Please Log in to join the conversation.
- lea_crtl
- Topic Author
- Offline
- New Member
Less
More
- Posts: 16
- Thank you received: 0
7 months 2 weeks ago - 7 months 2 weeks ago #250374
by lea_crtl
Thank you very much for your swift reply.
My randomization problem is as follows: I have 9 x 7 images that I named ab\jpg where a goes from 1 to 9 and b from 1 to 7.
Only images with the same a and different bs can end up in the same pair. And the same pair should not be submitted two times to the user.
The user won't see all the possible pairs but only 20 of them among the 9 x 7 x 6 / 2 = 189 possible ones.
At first I thought about randomization groups but defining all pairs in an array and shuffling it seemed to me much more straighforward. Maybe it's more simple than I think? Would you have an idea how to do it?
Thank you again
Replied by lea_crtl on topic Using a self-defined array to randomize questions
Thank you very much for your swift reply.
My randomization problem is as follows: I have 9 x 7 images that I named ab\jpg where a goes from 1 to 9 and b from 1 to 7.
Only images with the same a and different bs can end up in the same pair. And the same pair should not be submitted two times to the user.
The user won't see all the possible pairs but only 20 of them among the 9 x 7 x 6 / 2 = 189 possible ones.
At first I thought about randomization groups but defining all pairs in an array and shuffling it seemed to me much more straighforward. Maybe it's more simple than I think? Would you have an idea how to do it?
Thank you again
Last edit: 7 months 2 weeks ago by lea_crtl.
Please Log in to join the conversation.
- lea_crtl
- Topic Author
- Offline
- New Member
Less
More
- Posts: 16
- Thank you received: 0
7 months 2 weeks ago #250375
by lea_crtl
Replied by lea_crtl on topic Using a self-defined array to randomize questions
Thank you for your reply. I will look into ExpressionScript tomorrow. For your information I have 9 x 7 images (see previous reply I made). The number of pairs that can be formed is thus 9 x 7 x 6 / 2 = 189
I divided by 2 since I consider the permutation of a pair as the same pair.
I divided by 2 since I consider the permutation of a pair as the same pair.
Please Log in to join the conversation.
- holch
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 11660
- Thank you received: 2742
7 months 2 weeks ago #250377
by holch
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
Replied by holch on topic Using a self-defined array to randomize questions
But how many pairs do you show to each respondent? I assume it won't be all 189 pairs each time, right?
Because I highly doubt that anyone wants to compare all these. But then if you reduce it too much, you will have to run an enormous sample, to make sure that you get n=X responses per pair. Because to analyze this, you will need a certain sample per pair, right?
Because I highly doubt that anyone wants to compare all these. But then if you reduce it too much, you will have to run an enormous sample, to make sure that you get n=X responses per pair. Because to analyze this, you will need a certain sample per pair, right?
I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.
The following user(s) said Thank You: lea_crtl
Please Log in to join the conversation.
- lea_crtl
- Topic Author
- Offline
- New Member
Less
More
- Posts: 16
- Thank you received: 0
7 months 2 weeks ago #250385
by lea_crtl
Replied by lea_crtl on topic Using a self-defined array to randomize questions
I am planning to show 20 pairs to each respondent. Indeed, we might have too many images: I still need to calculate precisely how many respondents I would need to get exploitable results with 189 pairs and if this number is too large, I might reduce it to 105 pairs. But the problem would be the same: I need to randomly select 20 valid pairs among all possible pairs to each respondent without showing the same twice.
Please Log in to join the conversation.
- Joffm
- Offline
- LimeSurvey Community Team
Less
More
- Posts: 12940
- Thank you received: 3979
7 months 1 week ago - 7 months 1 week ago #250396
by Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Replied by Joffm on topic Using a self-defined array to randomize questions
I hope I understood it right.
With this script
You will get something like this (of course this question will be hidden with the css class "hidden")
Now you have 20 pairs and you access each of them by the function substr (the first two characters refer to the first image, the last two to the second image of the pair.
1.<img src=".../{substr(Q1_SQ001,0,2)}.jpg"><img src=".../{substr(Q1_SQ001,2,2)}.jpg">
2.<img src=".../{substr(Q1_SQ002,0,2)}.jpg"><img src=".../{substr(Q1_SQ002,2,2)}.jpg">
...
By the way:
I tried these loops in Excel and got this nice result
Joffm
With this script
- you define an array with all valid combinations
- shuffle the array
- select the first 20 elements
- write them into a question of type "multiple short text" with 20 subquestions
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 < 10; i++) { for (var j = 1; j < 7; j++) { for (var k = j+1; k < 8; k++) { arr.push(i.toString() + j.toString()+ i.toString()+ k.toString()); } } } arr = shuffle(arr); arr = arr.slice(0,20); for (var x = 0; x < 20; x++) { $('#question{QID} input[type="text"]:eq('+x+')').val(arr[x]); } }); </script>
You will get something like this (of course this question will be hidden with the css class "hidden")
Now you have 20 pairs and you access each of them by the function substr (the first two characters refer to the first image, the last two to the second image of the pair.
1.<img src=".../{substr(Q1_SQ001,0,2)}.jpg"><img src=".../{substr(Q1_SQ001,2,2)}.jpg">
2.<img src=".../{substr(Q1_SQ002,0,2)}.jpg"><img src=".../{substr(Q1_SQ002,2,2)}.jpg">
...
By the way:
I tried these loops in Excel and got this nice result
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 7 months 1 week ago by Joffm.
The following user(s) said Thank You: lea_crtl
Please Log in to join the conversation.
- lea_crtl
- Topic Author
- Offline
- New Member
Less
More
- Posts: 16
- Thank you received: 0
7 months 1 week ago #250409
by lea_crtl
Replied by lea_crtl on topic Using a self-defined array to randomize questions
It works perfectly, thank you so much!! This is exactly what I needed. And I now have a clearer idea of what I can and can't do with JS within LimeSurvey. Thanks again for your time and help
Best regards
Best regards
Please Log in to join the conversation.