Randomise or rotate a code list with groups & headers
Summary | Often it's required to show a code list with several group headers and responses within these groups. In most cases it's necessary to randomise the responses within the groups and/or the groups themselves. This article shows how this can be done using the 'Change response order' routing action providing and example QEX and syntax. This functionality is often referred to as 'block randomisation'. |
to | AskiaDesign |
Written for | Survey programmers, Scripters |
Keywords | Block randomisation, Group randomisation, Headers, Groups, Code list, Block rotation, Group rotation, Change response order, Askia Script 2.0, Shuffle, Code in control, Module, Export, Import, Function, .asx, Askia script extension, Resources, Screens. |
Download the full example QEX file here.
You might start with something like this:
The key point to note is that all the group headers must contain a character that the responses within the groups do not contain. In this case you can use a colon ':'
The script for the 'Change response order' routing can be written as follows:
Dim arrResponses = q0.Responses
' Step1: Identify headers
Dim arrHeaders = {}
Dim nResp
For nResp = 1 to arrResponses.Count
If Instr( ":" , arrResponses[nResp].Caption ) > 0 Then
arrHeaders = arrHeaders + nResp
Endif
Next nResp
' Step 2: shuffle the groups
Dim arrShuffled = Range(1, arrHeaders.Count-1)
' The '-1' above always keeps the Other response anchored at the bottom of the code frame
' Remove the following line if you do not want to shuffle the blocks
arrShuffled.Shuffle()
' Step 3: add the responses
Dim arrResult = {}
Dim nGroup
For nGroup = 1 to arrShuffled.Count
' Step 4: find the beginning and end of the group
Dim nOrgGroup = arrShuffled[nGroup]
Dim nStart = arrHeaders[nOrgGroup]
Dim nEnd = arrHeaders[nOrgGroup+1]
If nOrgGroup = arrHeaders.Count Then
nEnd = arrResponses.Count
Else
nEnd = arrHeaders[nOrgGroup+1]-1
EndIf
' Step 5: add the header
arrResult = arrResult + nStart
' Step 6: add the sub group
If nStart <> nEnd Then
Dim arrSubGroup = Range( nStart+1,nEnd)
' Remove the following line if you do not want to shuffle the groups (i.e. responses within the blocks)
arrSubGroup.Shuffle()
arrResult = arrResult + arrSubGroup
EndIf
Next nGroup
return arrResult
To hide the radio buttons for the group headers you can highlight them in the 'Modality' section of the response block properties and add the code below. The 'display' part hides the radio button and the 'disabled' part stops you from selecting by clicking on the response. The option and the result when testing is shown in the screenshots below.
style ="display:none;" disabled="true"
Please note that if you're using an ADP with your QEX, you have to add this extra piece of code into the Internet settings > CSS > Main CSS:
input[type=checkbox][id^=askia-input][disabled=true] + label,
input[type=radio][disabled=true] + label {
display: none;
}
You can stop the blocks from randomising by commenting out the code on line 16.
You can stop the responses within the blocks from randomising by commenting out the code on line 39.
The result of both of these changes individually is shown below:
You might be wondering how to do this with 'Rotation' rather than 'Randomisation'. The code below allows you to rotate an array. You can check this being set into question 'qrot' in the example QEX file. In this file, you can also see it has been incorporated into the main script (instead of the shuffle() keyword) and set into the response order of q2 to achieve block rotation.
Dim arrRot = {}
Dim arrInit = {3;5;7;9;11}
Dim arrDoubled = {}.Insert(arrInit).Insert(arrInit)
Dim numStart = Random(arrInit.count)
Dim i = 0
For i = numStart to (numStart + Size(arrInit) - 1)
arrRot.Insert(arrDoubled[i])
Next i
Return arrRot
You'll note this is not as simple as the shuffle() keyword and adds a fair few more lines of code to the block randomisation script.
We can get around this by defining the above code as a function (as our own new keyword e.g. Rotate()) in Design 5.4.5.0 onward and re-using it in the script. Create an empty text document and change its extension to be .asx instead of .txt. Insert it to your resources in AskiaDesign.
Double-click on the script extension to edit it. Add in your syntax define the function:
And then add these in to your routing. You can see this in the Design 5.4.5.0 QEX file here.
If the use of functions in AskiaDesign is new to you, then it's well worth reading this blog post to give yourself an in depth background to the functionality.
If you are interested in having a clean and simple example showing just one of the three options discussed above, they are available below: