Randomize answer options in array (not subquestions)

More
3 years 3 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

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

More
3 years 3 months ago - 3 years 3 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: 3 years 3 months ago by tpartner.

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

More
3 years 3 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

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

More
3 years 3 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

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

More
3 years 3 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

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

More
1 year 1 week ago #163879 by zserban
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
1 year 1 week 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

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

More
1 year 1 week ago #163891 by zserban
Sure, thanks for helping.
Attachments:

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

More
1 year 1 week 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

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

More
1 year 1 week ago #163897 by zserban
It sure does work.

Brilliant, thank you!

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

More
7 months 2 weeks 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:

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

More
7 months 2 weeks 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

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!