Check out the LimeSurvey source code on GitHub!

Assign each user that completes a survey to one of three groups?

More
5 years 8 months ago - 5 years 8 months ago #60660 by cycas
I'd like to have a hidden question that automatically assigns users into one of three neat groups - so,
user 1 : A
user 2 : B
user 3 : C
user 4 : A
user 5 : B
user 6 : C

I know I should be able to do this with Javascript, but I am awful at it :( . Can anyone point me the right way?
I can do a hidden question, and I can get the user ID, but I can't work out how to rotate the letters! :huh:
Last Edit: 5 years 8 months ago by cycas. Reason: got it wrong, doh :-(

Please Log in to join the conversation.

More
5 years 8 months ago #60685 by tpartner
You can use the JavaScript Modulus (division remainder) operator to divide the user ID into groups.

Something like this, where "id" is the user ID, "11111" is the survey ID, "22" the group ID and "33" the hidden question ID.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Find the remainder of the ID divided by 3
		var mod = id % 3;		
 
		// Set the "group" var
		var group = '';
		switch(mod) {
			case 0 :
				group = 'A';
				break;				
			case 1 :
				group = 'B';
				break;				
			case 2 :
				group = 'C';
				break;
		}
 
		// Load the "group" var into a text question
		$('#answer11111X22X33').val(group);
 
	});
 
</script>

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

Please Log in to join the conversation.

More
5 years 8 months ago #60742 by cycas
Ooh, you are a star, thanks! I'd got as far as realising I needed to use modulus, but I can't express how crap my maths is, so this was so helpful!

Please Log in to join the conversation.

More
5 years 8 months ago #60743 by holch
How do you invite your participants?

Do you have a list? Because if you invite them via token, then you can just create a custom attribute with the answer group and you can use that custom attribute to show/hide the respective questions via conditions.

Have a look at the manual! It is a really valuable source for information. Here some helpful links:
Manual (EN) | Question Types | Workarounds

Please Log in to join the conversation.

More
5 years 8 months ago #60744 by cycas
That was the problem, I don't want to use tokens for this particular survey as it is a pre-screening survey which doesn't have a pre-existing list, and I wanted to avoid any potential obstacles with spam filters etc that might get in the way of filling it in.

The JS solution has done the job nicely.

Please Log in to join the conversation.

More
5 years 8 months ago #60750 by holch
Well, in that case the token solution makes no sense of course.

Have a look at the manual! It is a really valuable source for information. Here some helpful links:
Manual (EN) | Question Types | Workarounds

Please Log in to join the conversation.

Imprint                   Privacy policy         General Terms & Conditions         Revocation information and revocation form