View Issue Details

IDProjectCategoryView StatusLast Update
07589Feature requests[All Projects] Conditionspublic2018-08-21 19:30
ReporterfsandoAssigned To 
Status acknowledgedResolutionopen 
Product Version 
Target VersionFixed in Version 
Summary07589: Randomly select x out y questions (or x out of y groups)

The idea is that I have a pool of eg. 20 questions, I want each respondent to answer 10 questions randomly selected from the pool of 20. I may have several such pools, where the respondent should only answer a random selection.

I have a working implementation. It touches the core code but I have tried to make it as self-contained as possible. It is contained in its own class and only a 7 lines goes into to core code.

The idea is the following:

First the basic idea:

1) Add one or more attribute fields to token table
2) Fill this with a string of 1/0. The total number of digits is the "y", the number of 1s is the "x".
Say I want to pick 3 out of 6, the string would be: "100101"

3) As relevance equation for each question in the pool I then add a regular expression to match the relevant field in the token table like this:
The first question: "^1" (matches only when the first character is 1)
The second question: "^.1" (matches only when the second character is 1)
The third question: "^..1" (matches only when the third character is 1)

Each such attribute column in the token table could in principle be filled by hand for each respondent, and in this way you could create custom routes through the questionnaire for each respondent.

In the implementation (described below) the fields are filled by random patterns. Thus creating random routes that are specific to each respondent. It is possible to create several individually independent random selection pools, by creating more fields and associating each to a different group of questions, indeed there is no limitation to how one could construct one's selection pool. Basically this just creates the ability to randomly selct a specific number of items or options out of a larger set.

I also achieves to make the presentation of each single question indepenent of the presentation other questions in the same randomization group.

The way this work in practice is that one adds a field to the token table and makes sure its description follows a specific format. Then every time a new token is added a random pattern is also added, this pattern is then used to either show or hide individual questions.

Additional Information

A possible solution: Random selection of x out of y questions (or x out of y groups)
This is a proof of concept, not necessarily a good solution.
Implemented in the Yii branch.

The implementation:

1) Add one or more extra attribute fields to the token table
Their description must have a very specific format (the format can, of course, be changed):


Where "RANDOM_PATTERN" identifies it as a randomizer field and "3,6" are parameters for the "x" and "y".

2) Have a function automatically fill those columns with random 0/1 strings of length y containing x 1s and (y-x) 0s.

This will effectively create one or more random "background" variables for each respondent that can be used as a selection condition both at question and group level.

I have created a class that does all the validation, computations and updating of the relevant randomization fields, if any, in the token table.

The class is currently placed in application/controllers/ in order to be autoloaded
I have added a function to Token_dynamic


This function is called in two places:

1) when dummy tokens are added and when createTokens is called, that is, it is called when new token-values are computed.
However, it is always called even when no tokens are actually calculated (if eg. no empty tokens).

Every time the updateRandomFields() is called all randomizer fields are checked for validity and if found to valid randomizer fields but in a an invalid state they are recalculated. If the field

How can they valid but in an invalid state?

The are valid if
description has the above mentioned format
x is non-negative and y is greater than zero
y is greater than x

They may be in an invalid state if the parameters were changed since last calculation: if the length of the current value differs from y or if the number of 1s differs from x.

The way it works in practice is that one adds a field to the token table and makes sure its description follows the format. Then every time a new token is computed the validity of the random pattern is checked and if necessary (re)calculated.

I do not fully understand how Yii works but I believe there are some filters or hooks that can run code before and after events. I have a feeling that this would probably be a more appropriate way to trigger the update than putting an extra function into Token_dynamic.

If the implementation is found to be robust and useful the idea could easily be extended to other types of fields by simply adding other description formats and associated methods.

TagsNo tags attached.


There are no notes attached to this issue.

Issue History

Date Modified Username Field Change