Welcome, Guest
Username: Password: Remember me

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

Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60660

  • cycas
  • cycas's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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: 3 years 5 months ago by cycas. Reason: got it wrong, doh :-(
The administrator has disabled public write access.

Re: Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60685

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
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.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.

Re: Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60742

  • cycas
  • cycas's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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!
The administrator has disabled public write access.

Re: Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60743

  • holch
  • holch's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2852
  • Thank you received: 359
  • Karma: 124
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 | Question Attributes | Workarounds

If you found this answer helpful and it saved you some time please consider a donation to the project to keep Limesurvey going!
The administrator has disabled public write access.

Re: Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60744

  • cycas
  • cycas's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 11
  • Karma: 0
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.
The administrator has disabled public write access.

Re: Assign each user that completes a survey to one of three groups? 3 years 5 months ago #60750

  • holch
  • holch's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 2852
  • Thank you received: 359
  • Karma: 124
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 | Question Attributes | Workarounds

If you found this answer helpful and it saved you some time please consider a donation to the project to keep Limesurvey going!
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.122 seconds
Donation Image