Actions

Setting conditions: Difference between revisions

From LimeSurvey Manual

No edit summary
 
No edit summary
Line 209: Line 209:


What you really need to setup is the following set of conditions:
What you really need to setup is the following set of conditions:
* on Q2: display question if answer to Q1 is Yes
* on Q2: display question if answer to Q1 is Male
* on Q3: display question if answer to Q1 is Male AND answer to Q2 is Yes
* on Q3: display question if answer to Q1 is Male AND answer to Q2 is Yes



Revision as of 11:05, 8 December 2009

General concepts

A question can be set to display ONLY IF certain conditions are met. Conditions can be used in all kind of surveys: question-by-question, group-by-group and all-in-one surveys. In all-in-one and group-by-group surveys, conditions are partly evaluated on the browser side (by using javascript): in this case questions are dynamically hidden or displayed depending on answers to previous questions.

You can build complex conditions by combining them. In this case you need to understand how different conditions are combined and evaluated (are they ORed or ANDed together).

The simple case: default scenario

Let's begin to explain the default case:

  • when you have several conditions, they are ANDed together if the values to be tested are different
    • this is the case for instance for several conditions based on different answers from previous questions: "If answer to Question1 equals 'Very Happy' AND answer to Question2 equals 'Happy'"
  • when you have several conditions they are ORed together if the values to be tested are the same
    • this is the case for instance for several possible answers of the same previous questions: "If answer to Question1 equals 'Very Happy' OR  answer to Question1 equals 'Happy'"

You will notice in the "questions window" you have different option to select from.

  • If you are trying to set up an  ORed condition between answers, select the line that has the "Group of checkboxes" function, then select those answers you want ORed together in the "answers window".
  • If you are trying to set up an  AND  condition between answers, select a line that has the "single checkbox" function, then select in the "answers window" if you want the answer to be "checked", or "unchecked".

Now, you may want to have ORed conditions even when they are using the same value to be tested: this is where scenarios can be used.

Using several scenarios

In fact, by default, all the conditions you add are added to the same default scenario (scenario number 1), and conditions beloging to this scenario are combined using the logic explained in the previous paragraph.

But you can also define conditions in other scenarios. In this case, here is how conditions are evaluated:

  • within a scenario, multiple conditions are evaluated as described in the previous paragraph: this helps evaluate each scenario independently.
  • Then, the complex multi-scenarios condition is evaluated to true if at least one scenario evaluates to true

You can now understand the "scenario" word used, because a complex multi-scenarios condition evaluates to True if at least one scenario evaluates to true.

File:Multi scenario.png

A question-entry is either:

  • a simple question (for all question types apart from Array questions)
    • for instance: text questions, radio or dropdown lists, and even multiple choices questions
  • or a row in Array questions (except for Array dual scale and Array Multiple Flexible questions),
    • for instance: Array of 5 radio buttons, array with label questions
  • or either the left part or the right part of an Array dual scale question
  • or each cell of an Array Flexible questions

As a matter of simplification, question-entries are just called "Question" and are listed with this label in the Add/Edit condition form.

You can set these conditions by clicking on the conditions icon in the question button bar, when viewing a question. When you choose Set Conditions, the edit conditions screen will appear allowing you to delete or create conditions for the current question. An example is shown below.

Existing conditions

The top part of the window always shows any conditions already set for this question:

File:Existing conditions.png

In the example above question B is set to only display if:

  • The answer to the question-entry FBI of previous question A is "1"
  • AND
    • EITHER the answer to the question-entry CIA of previous question A is "5"
    • OR the answer to the question-entry CIA of previous question A is "3"

You can delete this condition by clicking on the "Del" button.

In Add/Edit mode File:Conditions add.png, some specific actions are possible on each line such as:

  • edit condition
  • delete all conditions , delete this condition , delete all conditions from this scenario
  • edit scenario

In Copy mode , each line is prepended with a checkbox allowing you to select those conditions you want to copy to later questions:

File:Copying conditions.png

New conditions

Select the scenario

First select in which scenario you want to add the condition.

If you don't need ORed conditions between different question-entries, just don't modify the default scenario () that is automatically selected in the condition edition form.

On the contrary, if you need to define a condition in another scenario than the default one, click the new scenario icon File:Plus.png, and type in the scenario number: File:Default scenario edit.png

The number id of a scenario has no importance and different scenarios can have non-continuous ids.

Select the question-entry

Click on the question-entry (from the 'Question' select box) that you wish to use for your condition.

Please note that, as described above, a single array-question may be composed of several question-entries: this will have an impact on how conditions will be evaluated.

When a question-entry is selected, the available predefined answers for this question-entry are displayed in the answer box below. If the question-entry has no predefined-answers, the advanced tab is selected instead.

Select the comparison operator

Several comparison operators can be used:

  • The basic ones are equality, and inequality
  • The greater/lower than operator familly is also available, and is usually only useful when using numeric question-entries
    • Please note that comparisons on numeric fields honor the numeric order: for instance 2 < 10
    • If you're using a question entry whose type is text, then the condition could be evaluated as "2" > "10" (string order).
    • Numeric questions, as well as text questions having the 'numbers_only' attribute set, will use the numeric order for comparisons
  • An advanced comparison operator using regular expressions is also available. When selected, the answer input box will automatically switch to the advanced mode.

Selecting an answer

Using predefined answers

You can then select one or more predefined answers:

  • the possible answers for that question
    • Choose the answer that you want to use
    • You can choose multiple answers in one go by using the CTRL button and clicking on more than one answer in the right hand select list

Using the advanced tab

Enter the text value you want to use as the comparison right operand which can be:

  • a simple text
  • a number (comparison will use on numeric order if the question-entry selected is of a numeric type)
  • an SGQA tag pointing at a previous question. In this case the condition will be evaluated against an older answer (for instance: display this question if the answer to the previous "Date-2 question" is greater than the answer to the previous "Date-1 question")
  • If the comparison operator is regular expression, then this box must be filled with a valid regular expression

After that click on the "Add Condition" button.

Editting/Deleting conditions

Apart from adding new conditions, the Add/Edit can be used to

  • edit conditions
  • delete conditions
  • edit scenario

Edit conditions

By clicking on the edit icon on a condition line, the edit condition form at the bottom is automatically displayed with the current settings for this condition. Note that in this mode you can only select one predefined answer.

Click the update condition button to update this condition.

Delete conditions

You can:

  • delete a single condition by clicking on this icon
  • delete all conditions within a scenario by clicking on this icon
  • delete all conditions from this question by clicking on this icon

Edit Scenario

You can change the number assigned to a scenario by clicking this Icon and fill-in the form that appears on the same line.

Alternatively, you can renumber scenarios by clicking on this icon .

As said above, scenario numbers have no impact in the way conditions are evaluated. However, modifying the scenario numbers may be interresting in order to:

  • decide in which order the scenario are displayed when using the printable version of a survey
  • merge conditions from 2 scenarios (in order to merge scenario 10 to scenario 1, just renumber scenario 10 with scenario id 1).

Copying conditions to later questions

It is not uncommon for a group of questions to have the same condition. Luckily you can copy this condition to any subsequent question from the conditions designer once a first condition has been set.

Switch to the copy mode by clicking the on the Copy conditions icon .

File:Copy conditions example.png

The conditions applying to the current questions are displayed with a checkbox on their left. You can:

  • select each condition individually by checking the checkbox on the left of the condition
  • or select all conditions from a scenario at a time by checking the checkbox on the left of the scenario

Then select all subsequent questions in the survey on which you want to copy the selected conditions from the bottom Select box by highlighting them (using the CTRL key to select multiples). Then click on the "Copy Conditions" button to copy them across.

It is usually best to leave this until you have finished entering all your survey questions, and are satisfied with the question order.

Things to watch out for

General considerations

There are a few basic rules you should keep in mind before setting conditions on a question:

  • Once a single condition has been set for a question, that question will not display unless that condition is met.
  • Conditions can only be set based on questions that appear before the question on which the condition is set.
  • You can modify conditions even after a survey has been activated. This should be done with caution, as there is no "consistency checking" applied here.

Conditions using the 'no answer' predefined answer

Setting the following condition "Show question Q20 if answer to question Q1 is 'no answer'" really means "show question Q20 if question Q1 was displayed and received no answer". This is not equivalent to "show question Q20 if question Q1 was not displayed"

Chained conditions

If you set conditions on a question that, itself, has conditions, then there may arise occasions where the survey behaves in ways you might not have predicted.

In the example above a question is displayed 'Do you like being male?' which has conditions set, and which will only display if the answer to What is your gender? is M. If you were to add a condition to this question requiring a specific answer from the Do you like being male? question, then this question will never display, because the question Do you like being male will not be presented.

It is highly recommended that you copy the conditions from the earlier question to the one you're editting.

For instance, you want the following:

  • Q1 is a Gender question
  • Q2 is a Yes/No and is displayed if answer to Q1 is Male
  • Q3 is a ranking question and is displayed if Q2 is Yes

What you really need to setup is the following set of conditions:

  • on Q2: display question if answer to Q1 is Male
  • on Q3: display question if answer to Q1 is Male AND answer to Q2 is Yes

After correction, the correct set of conditions for Q3 should look like:

So if you are designing a complicated survey with large number of conditions, make sure you test the survey for as many different combinations of results as you can think of.

Limitations and workarounds

Progress-bar

If you create a survey were many questions get skipped because of conditions, the progress bar will jump a big step forward, or the survey ends at 50%.

To avoid such behavior, the questions that could be skipped, should be arranged between the shown questions, so that only one or two questions get skipped per answer.

For Example: based on question one (yes or no question) 15 questions will be asked question 1A to 15A when the answer of question one was yes, 1B to 15B when the answer to question one was no.

If you arrange the questions in one group and arrange them like: 2A, 2B, 3A, 3B, and so on you will get a nearly correct progress bar, while arranging the questions like 2A, 3A, 4A, [...], 2B, 3B, 4B, ... you will get a progress bar from 0 to 50% or from 50% to 100%, depending on the answer to the first question.

AND conditions for Multiple-choices questions

As multiple options questions are considered as a single question-entry, you can only design ORed conditions between their predefined answers.

If you want a set of ANDed conditions between possible answers of a multiple choices like question, you may want to try the following workaround. Since with the Array Multiple Flexible (number) question, each cell is a separate question-entry, you can:

  1. define an Array Multiple Flexible (number) question, with only one answer (this will display one row), and a label set assigned (this will display the possible multiple choices answers as the column headers).
  2. then set the question attributes so that the checkbox layout is used.

You will then be able to define conditions on each checkbox and have them be ANDed together. Be careful though, because the possible answers are not Yes or No but 1 or 0 as this question type is using numeric answers.

Setting conditions for "Other" Options in the Multiple Options Question Type

In the multiple options question type, you can use the "Other" provided in the question type as a valid choice but you cannot set conditions on it. For example: Question No 1 says "Which color do your prefer to use?" Answer checkbox choices are Blue, Black and Other. If the participant chooses Blue, you can set a condition. If the participant chooses Black, you can set a different condition. However, if the participant chooses "Other" and types in something in the text box, there is NO way a condition can be set. Limesurvey does not recognize if the participant chose the "Other" condition. This is NOT a bug but a limitation.

There is no real field recorded for the 'other' checkbox for this question type. Thus, the only way to know if the user has answered the 'other' part of the question or not would be to check if the value of the text written in the other input field is empty or not.

However, due to the specific way we handle Multiple choice questions, testing the 'other' text input field has never been implemented for type M and P questions.

As a workaround, do not to use the other option of these question types, but rather add an explicit answer called 'Other', then add an additional short text question which will be displayed if this 'Other' checkbox was clicked.

How to use parenthesis for the boolean expressions?

You can't!

If you want to design something like:

 (condition1 OR condition2) AND (condition3 OR condition4)

you'll have to set up:

 (condition1 AND condition3) OR

 (condition1 AND condition4) OR

 (condition2 AND condition3) OR

 (condition2 AND condition4)