Search code examples
javascriptphphtmlhttp-redirectevenly

Evenly redirect users to 18 different pages AND confirm submission


Have a design problem and would appreciate some advice.

I am expecting 540 participants to complete 18 surveys. Each survey should have exactly 30 participants (to COMPLETE/SUBMIT the survey - not to just VISIT the survey). The surveys are highly similar (structurally the same; only differ in some wording/images. Can be derived from the same html).

The way I used to do it is to host only one html, and have a random number generator in the html/JavaScript. When the page is requested, a random number between 1 to 18 will be generated, and according to that number, one of the 18 surveys is generated and returned to the participant. This is really convenient in the sense that I only need to maintain one html page, and worry about generating different surveys according to a given number. However I have noticed that the participants are not perfectly evenly distributed - I may have 25 participants in one survey, and 35 participants in another. This is possibly due to the randomness coming from the random number generator (and I assume there is nothing much I can do about it - correct me if I am wrong).

I am searching for a better solution. I thought about having a counter on the server side, and redirect participants according to the count (e.g. every 1st participants go to the 1st survey, every 2nd participants go to the 2nd survey... and every 18th participants go to the 18th survey). However this can only guarantee the participants evenly VISIT the surveys, not COMPLETE/SUBMIT the surveys - a participant can totally increase the count but do not finish the survey. If he doesn't submit the survey the count shouldn't be increased. But if the count doesn't increase he (and successive participants) cannot be assigned a survey. Putting a lock on the counter is unrealistic because there will be so many participants waiting to get a number and hence survey... ah I need some help @.@

Any solutions/suggestions?

Thanks in advance for any reply!


Solution

  • How about this:

    Create individual counters for each survey, and store them in a database. Every time a user requests a survey, fetch the counters from the database, iterate through them, and return the first survey whose count is < 30. Redirect the user to said survey, and increment that surveys counter.

    To ensure the surveys are actually submitted, you can do as follows: Create another table in the database. Every time you serve a user a survey, insert a new record into the table containing: unique_id, survey_id, timestamp. Pass the unique_id along with the survey to the user, and upon submission, remove the associated entry from the database. Now prior to selecting a survey (as described above), you fetch this data, and for any entry whose timestamp is > 10 minutes ago, you can assume it was not submitted, and decrement that surveys counter.