Randomize answer options in array (not subquestions)

More
6 years 2 months ago #128439 by elissa
Hi,
I would like to randomize answer options in an array question - not subquestions but answers options. Is there a setting or a workaround for that? I cannot find.
Regards
elisa

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
More
6 years 2 months ago - 6 years 2 months ago #128442 by tpartner
Hi Elisa. I think this should do the trick...

1) Add this function to the end of template.js:
function randomArrayCols(qID){
 
	var thisQuestion = $('#question'+qID);
 
	// Index the array columns
	$('table.subquestions-list tr', thisQuestion).each(function(i) {
		$('> *', this).each(function(i) {
			$(this).attr('data-index', i);
		});
	});
	if($('.noanswer-item', thisQuestion).length > 0) {
		$('table.subquestions-list tr > :last-child', thisQuestion).addClass('col-no-answer');
	}
 
	// Create an array of the columns
	var columns = [];
	$('table.subquestions-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) {
		columns.push($(this).attr('data-index'));
	});	
 
	// A function to shuffle elements or contents of arrays
	(function($){
		$.fn.shuffle = function() {
			return this.each(function(){
				var items = $(this).children();
				return (items.length)
					? $(this).html($.shuffle(items))
					: this;
			});
		}
 
		$.shuffle = function(arr) {
			for(
				var j, x, i = arr.length; i;
				j = parseInt(Math.random() * i),
				x = arr[--i], arr[i] = arr[j], arr[j] = x
			);
			return arr;
 
		}
	})(jQuery);
 
	// Shuffle the columns array
	columns = $.shuffle(columns);
 
	// Reposition the row elements
	$('table.subquestions-list tr', thisQuestion).each(function(i) {
		var thisRow = $(this);
		$(columns).each(function(i, val) {
			thisRow.append($('[data-index="'+val+'"]', thisRow));
			thisRow.append($('.col-no-answer', thisRow));
		});
	});
}

2) Add this script to the source of each array you want randomized:
<script type="text/javascript" charset="utf-8">		
	$(document).ready(function(){
		randomArrayCols({QID});
	});
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 6 years 2 months ago by tpartner.
The topic has been locked.
More
6 years 2 months ago #128443 by elissa
Hi Tony,
Thank you for your help but I'm testing it and cannot make it working. Columns stay in original order every time.
Regards
elisa

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
More
6 years 2 months ago #128444 by tpartner
0ops, sorry, I have a typo in the script for the question source. It should be this:

<script type="text/javascript" charset="utf-8">		
	$(document).ready(function(){
		randomArrayCols({QID});
	});
</script>

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: elissa
The topic has been locked.
More
6 years 2 months ago #128447 by elissa
Wow, now it works perfectly. Thank you very much! That's really great help for me.

Elzbieta Lesinska
LS voluntary Polish translator and supervisor
The topic has been locked.
More
3 years 11 months ago #163879 by zserban
Hello tpartner,

Should this work with ver. 2.7?
I am trying it without much success.
The topic has been locked.
More
3 years 11 months ago #163890 by tpartner
Probably not as it is - there have been many HTML changes. Can you attach a small sample survey containing only that question?

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: zserban
The topic has been locked.
More
3 years 11 months ago #163891 by zserban
Sure, thanks for helping.
Attachments:
The topic has been locked.
More
3 years 11 months ago #163896 by tpartner
This function will work for 2.73.0:

function randomArrayCols(qID){
 
	var thisQuestion = $('#question'+qID);
 
	// Index the array columns
	$('table.subquestion-list tr', thisQuestion).each(function(i) {
		$('> *', this).each(function(i) {
			$(this).attr('data-index', i);
		});
	});
	if($('.noanswer-item', thisQuestion).length > 0) {
		$('table.subquestion-list tr > :last-child', thisQuestion).addClass('col-no-answer');
	}
 
	// Create an array of the columns
	var columns = [];
	$('table.subquestion-list tr.answers-list:eq(0) td.answer-item', thisQuestion).each(function(i) {
		columns.push($(this).attr('data-index'));
	});	
 
	// A function to shuffle elements or contents of arrays
	(function($){
		$.fn.shuffle = function() {
			return this.each(function(){
				var items = $(this).children();
				return (items.length)
					? $(this).html($.shuffle(items))
					: this;
			});
		}
 
		$.shuffle = function(arr) {
			for(
				var j, x, i = arr.length; i;
				j = parseInt(Math.random() * i),
				x = arr[--i], arr[i] = arr[j], arr[j] = x
			);
			return arr;
 
		}
	})(jQuery);
 
	// Shuffle the columns array
	columns = $.shuffle(columns);
 
	// Reposition the row elements
	$('table.subquestion-list tr', thisQuestion).each(function(i) {
		var thisRow = $(this);
		$(columns).each(function(i, val) {
			thisRow.append($('[data-index="'+val+'"]', thisRow));
			thisRow.append($('.col-no-answer', thisRow));
		});
	});
}

Here is your test survey back with that function and call in the source of the question:

File Attachment:

File Name: limesurvey...5941.lss
File Size:18 KB

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Attachments:
The following user(s) said Thank You: zserban
The topic has been locked.
More
3 years 11 months ago #163897 by zserban
It sure does work.

Brilliant, thank you!
The topic has been locked.
More
3 years 6 months ago #170782 by zserban
Hi tpartner,


Do you have any idea if/how to do this in Limesurvey v3.13?


I uploaded an example.
Attachments:
The topic has been locked.
More
3 years 6 months ago #170783 by zserban
Nevermind, your code still works!

All one needs to do is to add the theme code in the custom.js, rather them template.js
The topic has been locked.
More
1 year 7 months ago #200790 by Gustavo_Kremer
Hi, I'm trying to use this solution in version 3.22.17, but it's not working. I'm attaching an example of the kind of question in which I need to randomize the order of columns.
I tried pasting the function in my theme's "custom.js", and also in the source of the very question (just as you can see in the attached example), but in neither case the columns are randomized.
Any help will be much appreciated!
Attachments:
The topic has been locked.
More
1 year 7 months ago #200795 by Joffm
Elissa asked for randomization of answer options

I would like to randomize answer options in an array question - not subquestions but answers options

Only in arrays the colums are called answer options,
in array(text) or array(number) both scales (x-scale and y-scale) are called subquestions.

So I think this script works is an array, not an array(numbers).

Therefore I changed your question to type "array".


Joffm


Volunteers are not paid.
Not because they are worthless, but because they are priceless
Attachments:
The topic has been locked.
More
1 year 7 months ago #200796 by Gustavo_Kremer
Many thanks Joffm. Unfortunately, switching from Array (Numbers) to Array is not an option in this case, because Array won't let me choose more than one answer per row or column (basically, what I'm trying to do is to join a series of multiple choice questions in a single grid/array).
The topic has been locked.

Start now!

Just create your account and start using Limesurvey today.

Register now