Difference between revisions of "Repeat Groups and Rosters in SurveyCTO"
(Created page with "This sections lists code examples that fulfill special requirements related to '''rosters''' and '''repeat groups'''. These can be used to develop interesting functionalities within forms, particularly with responses from a household, plot, or crop roster ==Read First== *'''Repeat Groups''' and '''rosters''' can be utilized to carry out useful commands *Examples include using '''repeat groups''' to repeat a set of questions over previously se...") |
|||
Line 30: | Line 30: | ||
*can be trickier to code | *can be trickier to code | ||
*output crop IDs do not line up with repeat count, need to account for this in [[Data Cleaning|data cleaning]] | *output crop IDs do not line up with repeat count, need to account for this in [[Data Cleaning|data cleaning]] | ||
==Filtering in Repeated Questions== |
Revision as of 21:23, 19 July 2023
This sections lists code examples that fulfill special requirements related to rosters and repeat groups. These can be used to develop interesting functionalities within forms, particularly with responses from a household, plot, or crop roster
Read First
- Repeat Groups and rosters can be utilized to carry out useful commands
- Examples include using repeat groups to repeat a set of questions over previously selected responses and filtering remove already selected options from select_one fields
- While some of the examples may seem a very specific, they can be generalized to any task that has the same outline
Repeat Group Using Previous Choices
There are many cases when you want to repeat a set of questions over previously selected responses, such as a set of crops cultivated or activities performed. Here we use an example based on a select_multiple question of 14 potential crops, where we want to ask follow up questions about the selected crops.
Both approaches require the use of the index() command, which yields the number of the repeat. Note that if NO CROPS is selected in the select_multiple question, the repeat groups are skipped. Also, it isn't possible to select NO CROPS and any other crops from the choices.
Code Example
Here is a code example showing the 2 main ways to achieve this. The comments in the labels of the calculated fields describe the steps taken in the SurveyCTO form.
1) Repeating for all options and adding a constraint inside the group
Here we code the repeat group count to cycle through all the possible options of the 'crop' choices - 14 in total - and add a constraint to the questions contained within the repeat group. The set up only needs a variable (${crop_name1}) to pull the crop name from the select_multiple options. There are two advantages and one drawback to this method:
- easier to code
- output links the crop ID to the repeat count
- can create large number of fields/variables when there are a lot of choices, particularly for nested repeats (e.g. crops within plots within seasons) which can slow down form processing on the tablet
2) Only repeating for selected options
This method utilizes how the select_multiple fields are stored (e.g. 1, 4, 5). It selects the corresponding number (i.e. the crop ID) in the list in order through the selected-at(${crops2}, index()-1)
calculate expression. Note that the '-1' is needed as the first field in the list is 0 (not 1), as in other programming languages like Python.
Once we know the crop ID, the crop name is pulled in the same manner as the other example. As the number of repeats is dynamic, we need to tell the repeat group to only repeat the number of times for which there are selected crops, which is: count-selected(${crops2})
. The setup of this method requires calculated fields for the crop ID and the crop name. There are two advantages and two drawbacks to this method:
- more elegant, less missing data
- runs faster on large forms
- can be trickier to code
- output crop IDs do not line up with repeat count, need to account for this in data cleaning