Hello, I am trying to shuffle the rows of an array type question which I have recoded to perform as a conjoint analysis/ discrete choice question using a table.
I am trying to make it so the order of the rows in the table is randomised. I have found a few similar queries on the forum (e.g.
forums.limesurvey.org/forum/can-i-do-thi...s-in-array-by-column
) but have no luck applying them to my own survey. The built-in randomise function does not work because of how I have had to code the question.
Is there a way to randomise the rows of a table? I have attached the example question (EXAMPLE 1). If this is not possible, I will rework the question and use a standard table with randomised rows instead (EXAMPLE 2), I am assuming this would use the shuffle array function but any on advice on how to do this would be really appreciated.
OK, so if the built in way doesn't work because of the way you have had to code it, then it would be important to understand what you did. Because how should we know how you had to code the question without the code?
You are talking about examples, but there are none attached.
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.
1. LSQ and LSG files are language sensitive. This means we need to guess the base language correctly.
2. We need to create a survey, we need to create groups, etc. Make it easy for those that help in the forum: Provide a LSS file.
The LSS file has also the advantage, that we can see some general settings of your survey (e.g. if it is all in one, group by group, question by question, etc.). And no, we do not want to see your whole survey, just the relevant parts for your issue. I know it might be a little bit more of work for you, but it makes it a lot easier for the helpers: copy your survey, delete the irrelevant parts, export it as LSS and send it here.
Of course, you can always just create a little sample survey as well.
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.
Hi,
you don't want to shuffle neither rows nor columns in an array.
Your array consists of only one row.
What you want to do is to change the question text.
You see this text is hard coded as HTML table.
Depending on your model there could be a way to insert variable parts into the questin text by Expression Manager.
But therefore we need a bit more information, a better example of your model.
Joffm
Volunteers are not paid.
Not because they are worthless, but because they are priceless
Thank you, I see what you are saying. I am thinking it will be easier to rearrange the question to a table with a radio for the answers. This means I would like to change question 'example2' in the LSS file to do the following things:
Randomise the order rows excluding the top header
Randomise the order of columns 2 and 3 with column 1 fixed.
I have attached a picture to try and demonstrate what I mean. If this is overly complicated randomising the rows only would be good enough.
The second question is a list-radio without answer options. Can you provide a sample survey with all answer options and indicate in your mock-up where the answer options are to be placed?
Cheers,
Tony Partner Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
This version has the full radio list of answers. These would remain under the table as a list as they appear by default in the example lss file attached.
In the meantime I got a solution according to your first example in "examplequestions.lss"
Well, here it is.
First you create a random string of the numbers of your cards in a question of type "short text" (let's call it H0)
insert this javascript snippet.
Code:
<script type="text/javascript" charset="utf-8">functionshuffle(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;}returnarray;}
$(document).on('ready pjax:scriptcomplete',function(){// Fill the arrayvar arr =[];// create an array with the number of your cards (here 20)for(i =1; i <21; i++){
w=("000"+ i).slice(-3);
arr.push(w);}
arr =shuffle(arr);// Limit the array to the first (here 20) elements
anumbers = arr.slice(0,20).join('');
$('#question{QID} input[type="text"]').val(anumbers);
$('#question{QID}').hide();
$('#ls-button-submit').trigger('click');});</script>
You get something like this.
You see this string contains all numbers from 1 to 20, left padded like "001", "002, ...
In the later display we take the first and the second, the third and the fourth, ... number to select the displayed cards.
Next.
In a question of type "long text" you enter your cards as default answer. (let's call it H1)
Like
...
Each attribute of same length (here 20 characters). and a total length per row of 80 characters.
Now it is easy to select the row according to the number and each of the four attributes
So for the first number in the string:
1st attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),0,20))}
2nd attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),20,20))}
3rd attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),40,20))}
4th attribute: {trim(substr(substr(H1, (substr(H0, 0, 3) - 1) * 82, 80),60,20))}
So for the second number in the string:
1st attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),0,20))}
2nd attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),20,20))}
3rd attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),40,20))}
4th attribute: {trim(substr(substr(H1, (substr(H0, 3, 3) - 1) * 82, 80),60,20))}
You see the logic?
And the "82"? This is because you have to consider the added carriage return and linefeed character.
You will get questions like:
And because you created 20 numbers you will be able to display 10 questions with different options.
To adapt it is up to you.
Thank you this works great for what I need and seems like a lot more effort than I naively anticipated when I posted the question so really appreciate your time spent helping me!