====== Filters and Conditional Questions ======

Filters are needed if the questionnaire flow should be individually adjusted. The simplest use would be not to show a question - that you know from an earlier question - is irrelevant to the questionee. 

A warning in advance: Most project administrators that use SoSci Survey have never before written computer programming code. You'll learn some very basic knowledge about programming with PHP in this chapter. Don't worry: The first step is always the hardest. You'll see that it's not as hard as it may seem -- and you'll be awarded by a steep learning curve. Some little frustration, you may suffer, will be compensated by a even more happy aha!

**Important:** For a better understanding, we recommend the chapther [[:en:create:php]].

**Important:** Should a filter not work as it should, please take a look into [[:en:create:filter-solving]].

**Important:** SoSci Survey saves all data from a respondent. If you allow a back button, the following situation may occur: A respondent answers question A, goes back, changes a filter question, and will not be presented question A any more in the subsequent questionnaire. The answers given to question A are still in the data record -- although the data tells that this respondent should not have seen question A.

**Tip:** If you separate respondents into a control group and an experimental group ([[:en:create:questions:random]], [[:en:create:randomization]]) and present them different questionnaire pages, it may be useful to (early) use ''[[:en:create:functions:setpageorder]]'' instead of ''[[:en:create:functions:gotopage]]'': The earlier in the interview SoSci Survey knows that pages will be skipped, the better can it adapt the progress bar.


===== Example: Yes-No-Filters =====

Before explaining filters and their function in depth, we will show you a quick-and-dirty solution for those of you who don't have the time and only need to skip some questions because the questionee answered a certain question with "no".

What you need to know to implement the filter:

  * The ID of the variable that contains the yes-no-question
  * the answer codes of this question
  * the page on which the questionnaire should continue, in case the questionee answers "no"

You will find the ID of the variable in the **Variables Listing**. This could look like this:

  [PT01] Dropdown Selection
  TV usage
  
  PT01 TV usage
    1 = yes
    2 = no
   -9 = no answer

So the ID of the variable is "PT01", the answer code for "yes" is "1" and the code for "no" is "2". Ideally you have configured the question to demand a complete answer (//Check for completion//). In this case the code -9 won't be applicable.  

When **Composing the Questionnaire** choose the page on which the questionnaire should continue in case the questionee answers "no". Enter an //ID// for the page, e.g. "noTV" ([[#entering_a_page_ID|entering a page ID]]).

Got to the page **following** the yes-no-question PT01. Add a //PHP-Code// element ([[php|Introduction to php]]) and enter the following:

<code php>
if (value('PT01') == 2) {
  goToPage('noTV');
}
</code>

**Tip:** Read the rest of this chapter. It's worth it!

===== Introduction to filters =====

A warning beforehand: If you use SoSci Survey for the first time, it is highly probable you never did programming before. Programming filters uses a small amount of basic programming knowledge (e.g. if-constructions, variables, functions). If things don't work right the first time, if you start swearing at your computer, if you despair at it: You are in good company. Many other project admins have experienced similar things. 
Just don't give up. The learning curve is quite steep, but you'll learn a lot that might come in handy some time. Not only in SoSciSurvey.

**Important:** To fully understand this chapter, we recommend you take a look at the chapter [[php|introduction to PHP]] first.

**Tip:** In case your filter doesn't work, check if it is on the //same page// as the filter question. This is the __most common mistake__ when programming filters and, well, it just won't work. The function ''value()'' must never be on the same page as the value (the question) it calls: Always place the function on a page after the original question.

**Tip:** Filters and programming aren't magic. The server simply does step-by-step what you've told him to in the //php-code//. If the outcome isn't what you expected, go through the code step-by-step. Use the debug mode ({{:button.debug.here.gif?nolink|Start questionnaire in debug mode}}), to follow the steps more easily. 

===== The keyword IF =====

Filters are programmed through IF-THEN-ELSE-relations in the element //PHP-code//. **if** and **else** are the keywords in programming language.
Following **if**, you set the condition into brackets. If this condition is complied with, the command following the condition will be executed:

''**if (**condition**)** command''

Additionally you may add the command **else** that will be executed in case the condition above is not fulfilled:

''**if (**condition**)** command_1 **else** command_2''

===== Show or not show questions in the questionnaire =====

A filter always uses two things: A cause and a consequence on the questionnaire's process. 

  * The cause is usually an answer by the questionee
  * The most common consequence is a certain question (or a whole block of questions) be shown or not.

The following example bases on a question on page 2 of the questionnaire, asking the questionee how much he earns per month. He had to choose between several categories: 1=no income, 2=up to 500$, and so on. This will be the filter question.

It's important to know that SoSciSurvey doesn't know the questionees answer when building page 2. The server won't know this until the questionee has answered the question and pressed the "next" button (this is when the data will be sent back to the server). This is why putting filter questions and their respective filters on the same page won't work. 
After getting the questionees data through him pressing "next", the server can start building page 3.

{{:en:create:fig.filters.principle.png?450|Function principle of filters}}

Now, what if the questions on page 3 should be skipped if any option except option 1 was chosen? (in other words: Page 3 should only be shown if the questionee chose option 1) We need to think a little further: To achieve this, the filter needs to redirect all questionees who __did not__ choose option 1 to page 4.

This is how most filters work in SoSciSurvey: You skip certain questions or whole pages. In some cases you need to cling several filters together.

An example: on page 10 you show a multiple choice question with 5 car brands to choose from. About each brand, you want to ask 4 questions on 2 pages. To do so, you will need 5 filters. The first filter will be on page 11. It checks whether option 1 was chosen. If not, it will skip page 11 and 12 and redirect to page 13 (because the questions about brand 1 were spread about two pages). On page 13 the next filter kicks in: it checks whether option 2 was chosen. Then it goes on like this.

When the filter reaches a point where an option was actually chosen (let's say, option 3 was checked) then the filter on page 15 does not redirect but asks the questions on page 15 and 16, right until page 17 where the next filter checks if option 4 was chosen.

{{:en:create:fig.filters.jump.png?600|Skipping questions using filters}}

===== Conditions =====

A condition is always put into round brackets (). It may be complied with (the condition will return "true") or not ("false").

A condition can look differently. The most common use is the comparision of two values (e.g. two numbers):

  * ''=='' (two equal signs) checks if two values are the same,
  * ''!='' (exclamation mark and equal sign) checks if two values are not the same,
  * ''>'' (greater than sign) checks if value one is greater than value two (only works with numerical values),
  * ''<'' (less than sign) checks if value one is less than value two (only works with numerical values).

**Attention:** To check if two variables are the same, always use two equal signs (''=='')! Using only one equal sign (''='') will try to allocate a value to a variable!

**Tip:** To check if a variable has the value 1 __or__ 2, read [[filter-boolean|linking several conditions]].

===== A simple Filter =====

Back to the simple example: On page 1 of the questionnaire we ask the question AB01 (selection about the income). Our goal is to ask the questions EK01 and EK 02 only if the questionee chose option 1 in AB01.

This means also: if the questionee chooses an option greater than 2 (income up to 500$), page 3 will be skipped.

The following example describes a questionnaire with 3 pages. On page 2 a filter will check whether the questionee has selected an income greater than 500$. In this case, the rest of the page will be skipped and the questionnaire continues on page 3.

**Tip:** How to implement php in the questionnaire is fully described in the chapter [[php|Introduction to php]]. Essentially, to use filters you need to drag-and-drop the element //PHP-Code// (above the list of questions in the "compose questionnaire"-area) into the questionnaire.

**Tip:** You don't need to enter the questions themselves as PHP-code - you can still drag-and-drop them from the selection list into the page.
([[php#bequemer_programmieren_fragen_und_texte|convenient programming]]).

==== Assigning page IDs ====

For page 3 in the questionnaire you will need a page ID. Let's use "tvconsum". We need this to be able to jump to this page using ''[[:en:create:functions:gotopage|goToPage()]]''.

The ID can simply be assigned when composing the questionnaire. Click on the page and enter the ID on the upper left.

{{:en:create:scr.filters.page_ident.png?nolink|Assigning a page ID}}

==== Using as little PHP-code as possible ====

{{:en:create:scr.filters.simple.page1.png?nolink|Page 1}}

{{:en:create:scr.filters.simple.page2.png?nolink|Page 2}}

{{:en:create:scr.filters.simple.page3.png?nolink|Page 3}}


==== Using as much PHP-code as possible ====

Usually you will try to use PHP-code only where needed -- like above. Just because questionnaires can be written much quicker in PHP, this manual often uses the following form. Contentwise, the following form is identical to the above.

**Page 1**
<code php>
question('AB01');  // Monthly income
</code>

**Page 2**
<code php>
if (value('AB01') > 2) {
  goToPage('tvconsum');  // Directly goes to page 3
}
question('EK01');  // Study funding
question('EK02');  // Other income
</code>

**Page 3 with ID "tvconsum"**
<code php>
// Page 3 must have the ID "tvconsum"
question('TK01');  // Daily TV consumption
question('TK02');  // Favorite TV station
</code>

**Important:** Questions can be dragged-and-dropped into the questionnaire page as well as being implemented via ''question()'' into PHP. While you are technically able to do both, doing so will result in the question being displayed twice and receiving respective error messages.

==== Tips about the function value() ====

  * There are two easy ways to find out which value will be saved in an answer: The **Variables listing** and the debug mode while testing the questionnaire ([[questionnaire|Creating a questionnaire]])
  * The function ''[[:en:create:functions:value|value()]]'' prints the answer the questionee gave. This will only work after the questionee has hit the "next"-button after he answered the question.
  * All functions specific to SoSciSurvey are listed and explained at [[functions|PHP-functions]]

===== Instruction blocks =====

The example shows a curly bracket (''{'') following the if-condition. Curly brackets in PHP summarize several functions/commands (e.g. several questions). Although there is not always more than one command following the IF-condition, we recommend that you always use the brackets: If you add another command later on and forget the brackets, you will most probably spend a long time figuring out why that error occurs.

**Tip:** Use curly brackets for every instruction block and indent the PHP-code using spaces (don't use tabs as the entry fields have problems with those). This way you won't lose the overview even if writing long passages of php.

**Example for page 2**
<code php>
if (value('AB01') <= 2) {
  question('AB02');  // Study funding
  question('AB03');  // Other income
} else {
  question('AB04');  // Working hours per week
  question('AB05');  // Employment
  question('AB06');  // Employment limitation
}
question('AB07');  // This question will be shown to all questionees
</code>

===== Step by step =====

This paragraph explains the programming of a small filter step by step.

==== The filter question ====

Back to using IF-THEN-conditions as filter questions: Every question can become a filter question, when its answer is used as a condition.
First, create any question.

  - Create a new section in the **list of questions**, //ID// "TF", //Name// "Filter test"
  - Create a //new question//, //Name// "Filter selection", //Type// "Selection" 
  - Enter the following into the question TF01:
    - //Question title// "Do you have an internet connection at home?",
    - //This question demands a complete answer// "Yes".
    - Click //save//
    - In the //quick input section for options//, enter two items: "yes" and "no".

{{:en:create:scr.filters.question_yes_no.png?nolink|Selection as filter question}}

Look at the **variables listing**. You will find the following

{{:en:create:scr.filters.varlist.select.png?nolink|Selection in the variables listing}}

The ID of the question is TF01. The question type is selection, so the answers are saved right under the ID TF01 -- if the question would be a scale for example, the answers would be saved below the //items// instead of the //ID//. The variables listing tells you the values TF01 can contain: ''1'' for the answer "yes" and ''2'' for "no". ''-9'' is also possible (the question was not answered), though it should not occur as we configured the question to demand a complete answer. You find more details in the chapter [[:en:results:values|codes and output values]].

Create a new questionnaire at **compose questionnaire**, //Name// "filter1", //Title// "My first filter question".

**Tip:** The option **This question demands a complete answer** should in general be used very sparse. But as filters need to decide things upon this answer, it is recommended to use the option for filter questions.

==== Filters about the selection ====

To use TF01 as filter question, you need the command ''[[:en:create:functions:value|value()]]''. This function looks up, which answer the questionee gave to this question. In our example, the command ''%%value('TF01')%%'' would print 1 or 2 (not -9 because it must be answered). As usual, the requirement is that the question was asked on a questionnaire page before the current one.

If the question TF01 was asked on page 1 of the questionnaire, a PHP-Code on page 2 would look like this:

<code php>
if (value('TF01') == 1) {
  question('IN01');  // Type of internet connection
  question('IN02');  // Commercial use of the internet connection
  question('IN03');  // Service provider
}
question('AB01'); // Job description
</code>

The condition checks, whether the answer was equal to 1. In our words, whether the questionee has chosen "yes". If this condition is true, page 2 will ask three questions about the internet usage (IN01 to IN03).

All questionees -- regardless of their answer to the filter question TF01 -- will see the question AB01. Did the questionnee answer "no" to TF01, he will only see one question on page 2: AB01. Did he answer "yes", he will see four questions in total on page 2.

==== Using else ====

Using the command ''else'' you can ask questions that should be shown, in case the IF-condition returns "false". In our example, we could ask, why the questionee does not have an internet connection:

<code php>
if (value('TF01') == 1) {
  question('IN01');  // Type of internet connection
  question('IN02');  // Commercial use of the internet connection
  question('IN03');  // Service provider
} else {
  question('IN04');  // Why no internet connection?
}
question('AB01'); // Job description |

===== Skip pages =====

If not using any ''else''-commands and having no other questions on the page, it can happen that the software loads an empty page. Although SoSciSurvey will not show empty pages to questionees since version 2.2.02, this happening is considered bad programming. Empty pages would happen in the following example, if the questionee had answered "no":

<code php>
if (value('TF_01') == 1) {
   question('IN01');  // Type of internet connection
   question('IN02');  // Commercial use of the internet connection
   question('IN03');  // Service provider
}
</code>

In this and some other cases, you better use the command ''[[:en:create:functions:gotopage|goToPage()]]''. This will directly jump to the stated page in the questionnaire. In the example above, page 2 is only relevant for people with internet connection. All others should jump to page 3.

To use the ''goToPage()''-command, you need to configure an ID for the page the questionnaire should jump to. In the following example the target page has the ID ''%%"usage"%%''. If you only need to jump to the page following the current one, you can also use the "ID" ''%%"next"%%''.

The following examples will output the same page (if the questionnee has answered "no"), because following the ''goToPage()'' command, nothing will be executed. The ''goToPage()''-command leaves the current page and will never return.

** Option 1: Jump to page "usage" if there's nothing to ask. **
<code php>
if (value('TF_01') == 1) {
   question('IN01');  // Type of internet connection
   question('IN02');  // Commercial use of the internet connection
   question('IN03');  // Service provider
} else {
  goToPage('usage');  // Jump to page 3 with the ID "usage"
}
</code>

** Option 2: Skip the irrelevant questions **
<code php>
if (value('TF01') != 1) {  // "!=" means "is not". You could also check if: value() == 2
  goToPage('usage');  // Jump to page 3 with the ID "usage"
  }
   question('IN01');  // Type of internet connection
   question('IN02');  // Commercial use of the internet connection
   question('IN03');  // Service provider
</code>

Example 2 can be used to skip more than one page as well.

**Tip:** Asking some benchmark data reveals the questionee is not of interest to you? Have a look at 
[[#screenoutungeeignete_teilnehmer_filtern|Screenout: Filter unsuitable questionees]]

===== The right ID =====

The example uses a selection. A simple selection will save just one value, the chosen option. This can be called with ''value()'' and the ID (e.g. TF01).

If using scales, text entry fields or multiple choice questions, you need to use the ID of the respective item, not the question (e.g. TF01_02)!

{{:en:create:scr.filters.multiselect.png?nolink|Example for multiple choice selections TF03}}

The multiple choice selection TF03 contains 4 items. The item with the ID 3 (TF03_03) asks if the questionee uses a mobile phone to access the internet. You will always find the right ID in the **variables listing**.

{{:en:create:scr.filters.varlist.checkbox.png?nolink|Variables of a multiple choice selection}}

All items of a multiple choice question will either have the value 1 (not selected) or 2 (selected). If you want to filter the questionees that use the mobile phone on page 2, you use the following PHP-code:

** Filter for multiple choice selections **
<code php>
if (value('TF03_03') == 2) {  // Has item 3 been selected?
  question('IN10');  // Download speed
  question('IN11');  // Use of apps
} else {
  question('IN09');  // Why no mobile device?
}
</code>

A big drawback to multiple choice questions is the fact, that you cannot check if the questionee has answered the question as no checked items is considered a valid answer.

You can avoid this problem by using a 2-step-scale instead of a multiple choice selection and naming the extremes "yes" and "no". So you will get the same values for all items (1 for no, 2 for yes) and an additional "-9"-value for items that were not answered. In the example we chose the minimum "no", the maximum "yes" and the direction of scale as descending, because the "yes"-option should show on the left.

{{:en:create:scr.filters.example_scale.png?nolink|variables in a scale}}

The chapter [[filter-itemcount|at least one item]] shows some tricks on how to use scales in filters.

===== Screenout: Filter unsuitable participants =====

In case your survey has a certain target group, you might want to filter unsuitable participants and dismiss them after some screening questions. To do so, ask the screening questions early in the questionnaire and use a common filter afterwards.

  * Using the command ''[[:en:create:functions:gotopage#end_interview|goToPage('end')]]'' will lead your questionnee to the end of the questionnaire. The interview will be marked as completed and the questionnee will be shown the sendoff.
  * Combining the commands ''[[:de:create:functions:text|text()]]'' and ''[[:en:create:functions:buttonhide|buttonHide()]]'' can be used to show a individual sendoff. The interview will __not__ be marked as "completed". Save the sendoff text in **Text elements and labels**

<code php>
// Screenout with individual text
if (value('AB01') > 3) {
  text('screenout');
  buttonHide();
}
</code>

**Tip:** By repeating a filter you are able to check different variables. Using boolean operators you can check several variables in one filter ([[filter-boolean|concatenating conditions]]).

===== Tips about filters / Problem solving =====

Filters are very flexible tools. You can nest them if needed. You can check complex conditions. We will explain more in further chapters (see below).

Usually filters don't work as needed on the first try. If things don't work, there are several possibilities to check for errors:

  * Check the **Variables listing** whether you have got the right variables ID.
  * The filter __must not__ be on the same page as the filter question! In that case the filter will be executed before the questionee has answered the question.
  * Check your questionnaire in debug mode ({{:button.debug.here.gif?nolink|Start questionnaire in debug mode}}). In that case, SoSciSurvey will tell you the submitted value of every ID after hitting //next//.
  * The debug mode is also helpful, in case of empty pages or unexpected outcomes of ''goToPage()''-commands. For every jump using ''goToPage()'', debug information will show the line "create Page X".
  * SoSciSurvey can tell you what value was saved under a certain item/a certain question. 
    * You can print the value of value(): \\ ''debug('TF03_03');''
    * You can print whether a condition returns true (it is complied) or false (not complied): \\ ''debug(value('TF02_02') == 2);''.
  * Jumping through pages using ''goToPage()'' is recommended only, if questions should be skipped. In case you want to ask alternate questions, the way to do so is by implementing them into the IF-block.
  * The command ''goToPage()'' will immediately build the stated page (not after hitting //next//!). It will attach this page to the current page (if there were questions asked before the filter). 
  * After having jumped through pages using ''goToPage()'' and hitting //next//, the questionnaire will continue normally.

===== Complex filters =====

Most filters can be sufficiently programmed using the above programming basics. But not all. The following chapters will show you solutions for more complex filters:

  * [[filter-texts|Filtering with text entries]]
  * [[filter-boolean|Concatenating conditions]] - logic operations
  * [[filter-elseif|Conditions with more than two options]] - 1, 2 or 3
  * [[filter-itemcount|At least one item chosen?]] - Loops
  * [[filter-items|Use Selected Items]] - Arrays