Difference between revisions of "Repeat Groups and Rosters in SurveyCTO"

Jump to: navigation, search
Line 32: Line 32:


== Filtering in Repeated Questions ==
== Filtering in Repeated Questions ==
This example demonstrates how to remove already selected options from ''select_one'' fields in repeat groups. It has several applications, such as choosing a favorite something, 2nd favorite, etc., or programming flexibility about the order in which the respondent answers questions about a particular plot, crop, family member, etc</onlyinclude>. It is important that the repeat_count is always well defined, as using all the answers will result in the form not allowing you to progress.
This example demonstrates how to remove already selected options from ''select_one'' fields in repeat groups. It has several applications, such as choosing a favorite something, 2nd favorite, etc., or programming flexibility about the order in which the respondent answers questions about a particular plot, crop, family member, etc</onlyinclude>. It is important that the ''repeat_count'' is always well defined, as using all the answers will result in the form not allowing you to progress.


=== Code Example ===
=== Code Example ===
[https://drive.google.com/open?id=1MjQd_PHf1ezd6QTFFj_H-SSikLFJzFqyHRZ1njIUk-U Here is a code example]. We have a situation where a team is going out to map the plots of an association, but first they want to know the order in which they will map the 10 plots.  
[https://drive.google.com/open?id=1MjQd_PHf1ezd6QTFFj_H-SSikLFJzFqyHRZ1njIUk-U Here is a code example]. We have a situation where a team is going out to map the plots of an association, but first they want to know the order in which they will map the 10 plots.  


To set this up, we have a repeat group with a ''select_one'' field to choose the 1st plot that is mapped. In the choices tab, the values have corresponding values in the filter.  
To set this up, we have a '''repeat group''' with a ''select_one'' field to choose the 1st plot that is mapped. In the choices tab, the values have corresponding values in the filter.  


The choices become filtered after each repetition through the expression <code>not(selected(${plot_order}, filter))</code> in the ''choice_filter'' column. This removes choices that had already been selected for this question. Note that [[SurveyCTO Programming|SurveyCTO]] aggregates over each instance of ''${plot_order}'' in this filter expression, despite each of them being an individual ''select_one''.
The choices become filtered after each repetition through the expression <code>not(selected(${plot_order}, filter))</code> in the ''choice_filter'' column. This removes choices that had already been selected for this question. Note that [[SurveyCTO Programming|SurveyCTO]] aggregates over each instance of ''${plot_order}'' in this filter expression, despite each of them being an individual ''select_one''.
Line 43: Line 43:
The example includes a ''join()'' of the responses that dynamically updates, to help visualize how the filter is being populated.  
The example includes a ''join()'' of the responses that dynamically updates, to help visualize how the filter is being populated.  


Note that this could be coded without a repeat group, with the size of the filter increasing with each plot selected.
Note that this could be coded without a '''repeat group''', with the size of the filter increasing with each plot selected.

Revision as of 21:25, 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

Filtering in Repeated Questions

This example demonstrates how to remove already selected options from select_one fields in repeat groups. It has several applications, such as choosing a favorite something, 2nd favorite, etc., or programming flexibility about the order in which the respondent answers questions about a particular plot, crop, family member, etc. It is important that the repeat_count is always well defined, as using all the answers will result in the form not allowing you to progress.

Code Example

Here is a code example. We have a situation where a team is going out to map the plots of an association, but first they want to know the order in which they will map the 10 plots.

To set this up, we have a repeat group with a select_one field to choose the 1st plot that is mapped. In the choices tab, the values have corresponding values in the filter.

The choices become filtered after each repetition through the expression not(selected(${plot_order}, filter)) in the choice_filter column. This removes choices that had already been selected for this question. Note that SurveyCTO aggregates over each instance of ${plot_order} in this filter expression, despite each of them being an individual select_one.

The example includes a join() of the responses that dynamically updates, to help visualize how the filter is being populated.

Note that this could be coded without a repeat group, with the size of the filter increasing with each plot selected.