Randomize answer options in array (not subquestions)

More
2 years 7 months ago #128439 by elissa
elissa created the topic: Randomize answer options in array (not subquestions)
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

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago - 2 years 7 months ago #128442 by tpartner
tpartner replied the topic: Randomize answer options in array (not subquestions)
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: 2 years 7 months ago by tpartner.

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago #128443 by elissa
elissa replied the topic: Randomize answer options in array (not subquestions)
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

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago #128444 by tpartner
tpartner replied the topic: Randomize answer options in array (not subquestions)
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

Please Log in or Create an account to join the conversation.

More
2 years 7 months ago #128447 by elissa
elissa replied the topic: Randomize answer options in array (not subquestions)
Wow, now it works perfectly. Thank you very much! That's really great help for me.

Elzbieta Lesinska
LS voluntary Polish translator and supervisor

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #163879 by zserban
zserban replied the topic: Randomize answer options in array (not subquestions)
Hello tpartner,

Should this work with ver. 2.7?
I am trying it without much success.

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #163890 by tpartner
tpartner replied the topic: Randomize answer options in array (not subquestions)
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

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #163891 by zserban
zserban replied the topic: Randomize answer options in array (not subquestions)
Sure, thanks for helping.
Attachments:

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #163896 by tpartner
tpartner replied the topic: Randomize answer options in array (not subquestions)
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

Please Log in or Create an account to join the conversation.

More
4 months 1 week ago #163897 by zserban
zserban replied the topic: Randomize answer options in array (not subquestions)
It sure does work.

Brilliant, thank you!

Please Log in or Create an account to join the conversation.

Start now!

Just create your account and start using Limesurvey today.

Register now
Join our Newsletter!