This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:create:filters [16.01.2017 10:30] – [Example: Yes-No-Filters] cofrin | en:create:filters [27.11.2020 21:10] (current) – sophia.schauer | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Filters and Conditional Questions ====== | ====== Filters and Conditional Questions ====== | ||
- | Filters are needed | + | A filter question is always required |
- | 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: | + | * //Filter Question// -- The question whose answer should influence |
+ | * //Filter Condition// | ||
+ | * //Filter Effects// -- The "then part" of the filter, the effect on the interview (e.g. displaying an additional question) if the filter condition is met. | ||
- | **Important: | ||
- | **Important: | + | In SoSci Survey different types of filter |
- | **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 | + | |
+ | * No program code (PHP code) required | ||
+ | * Simple filters only | ||
+ | * Only available for certain | ||
+ | * A // | ||
+ | * Allows arbitrarily complex filters | ||
+ | * Allows | ||
+ | * Requires working with PHP program code | ||
+ | * Using [[: | ||
+ | * Requires to work with JavaScript program code | ||
+ | * Allows dynamic changes to the current questionnaire page | ||
+ | * More about it: [[: | ||
+ | * Will be partially integrated into //Question Filter// in future program versions | ||
- | **Tip:** If you separate respondents into a control group and an experimental group ([[: | + | **Important:** Do you use filters __and__ do you allow a back button |
- | |||
- | ===== 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 " | ||
- | |||
- | 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 " | ||
- | |||
- | 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 " | ||
- | |||
- | When **Composing the Questionnaire** choose the page on which the questionnaire should continue in case the questionee answers " | ||
- | |||
- | Got to the page **following** the yes-no-question PT01. Add a // | ||
- | |||
- | <code php> | ||
- | if (value(' | ||
- | goToPage(' | ||
- | } | ||
- | </ | ||
- | |||
- | **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, | ||
- | 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: | ||
- | |||
- | **Tip:** In case your filter doesn' | ||
- | |||
- | **Tip:** Filters and programming aren't magic. The server simply does step-by-step what you've told him to in the // | ||
- | |||
- | ===== The keyword IF ===== | ||
- | |||
- | Filters are programmed through IF-THEN-ELSE-relations in the element // | ||
- | Following **if**, you set the condition into brackets. If this condition is complied with, the command following the condition will be executed: | ||
- | |||
- | '' | ||
- | |||
- | Additionally you may add the command **else** that will be executed in case the condition above is not fulfilled: | ||
- | |||
- | '' | ||
- | |||
- | ===== Show or not show questions in the questionnaire ===== | ||
- | |||
- | A filter always uses two things: A cause and a consequence on the questionnaire' | ||
- | |||
- | * 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, | ||
- | |||
- | It's important to know that SoSciSurvey doesn' | ||
- | After getting the questionees data through him pressing " | ||
- | |||
- | {{: | ||
- | |||
- | 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: | ||
- | |||
- | 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. | ||
- | |||
- | {{: | ||
- | |||
- | ===== Conditions ===== | ||
- | |||
- | A condition is always put into round brackets (). It may be complied with (the condition will return " | ||
- | |||
- | A condition can look differently. The most common use is the comparision of two values (e.g. two numbers): | ||
- | |||
- | * '' | ||
- | * '' | ||
- | * ''>'' | ||
- | * ''<'' | ||
- | |||
- | **Attention: | ||
- | |||
- | **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, | ||
- | |||
- | **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# | ||
- | |||
- | ==== Assigning page IDs ==== | ||
- | |||
- | For page 3 in the questionnaire you will need a page ID. Let's use " | ||
- | |||
- | The ID can simply be assigned when composing the questionnaire. Click on the page and enter the ID on the upper left. | ||
- | |||
- | {{: | ||
- | |||
- | ==== Using as little PHP-code as possible ==== | ||
- | |||
- | {{: | ||
- | |||
- | {{: | ||
- | |||
- | {{: | ||
- | |||
- | |||
- | ==== 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, | ||
- | |||
- | **Page 1** | ||
- | <code php> | ||
- | question(' | ||
- | </ | ||
- | |||
- | **Page 2** | ||
- | <code php> | ||
- | if (value(' | ||
- | goToPage(' | ||
- | } | ||
- | question(' | ||
- | question(' | ||
- | </ | ||
- | |||
- | **Page 3 with ID " | ||
- | <code php> | ||
- | // Page 3 must have the ID " | ||
- | question(' | ||
- | question(' | ||
- | </ | ||
- | |||
- | **Important: | ||
- | |||
- | ==== 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 '' | ||
- | * All functions specific to SoSciSurvey are listed and explained at [[functions|PHP-functions]] | ||
- | |||
- | ===== Instruction blocks ===== | ||
- | |||
- | The example shows a curly bracket ('' | ||
- | |||
- | **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(' | ||
- | question(' | ||
- | question(' | ||
- | } else { | ||
- | question(' | ||
- | question(' | ||
- | question(' | ||
- | } | ||
- | question(' | ||
- | </ | ||
- | |||
- | ===== 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**, | ||
- | - Create a //new question//, //Name// " | ||
- | - Enter the following into the question TF01: | ||
- | - //Question title// "Do you have an internet connection at home?", | ||
- | - //This question demands a complete answer// " | ||
- | - Click //save// | ||
- | - In the //quick input section for options//, enter two items: " | ||
- | |||
- | {{: | ||
- | |||
- | Look at the **variables listing**. You will find the following | ||
- | |||
- | {{: | ||
- | |||
- | 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: '' | ||
- | |||
- | Create a new questionnaire at **compose questionnaire**, | ||
- | |||
- | **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 '' | ||
- | |||
- | If the question TF01 was asked on page 1 of the questionnaire, | ||
- | |||
- | <code php> | ||
- | if (value(' | ||
- | question(' | ||
- | question(' | ||
- | question(' | ||
- | } | ||
- | question(' | ||
- | </ | ||
- | |||
- | The condition checks, whether the answer was equal to 1. In our words, whether the questionee has chosen " | ||
- | |||
- | All questionees -- regardless of their answer to the filter question TF01 -- will see the question AB01. Did the questionnee answer " | ||
- | |||
- | ==== Using else ==== | ||
- | |||
- | Using the command '' | ||
- | |||
- | <code php> | ||
- | if (value(' | ||
- | question(' | ||
- | question(' | ||
- | question(' | ||
- | } else { | ||
- | question(' | ||
- | } | ||
- | question(' | ||
- | |||
- | ===== Skip pages ===== | ||
- | |||
- | If not using any '' | ||
- | |||
- | <code php> | ||
- | if (value(' | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | </ | ||
- | |||
- | In this and some other cases, you better use the command '' | ||
- | |||
- | To use the '' | ||
- | |||
- | The following examples will output the same page (if the questionnee has answered " | ||
- | |||
- | ** Option 1: Jump to page " | ||
- | <code php> | ||
- | if (value(' | ||
- | | ||
- | | ||
- | | ||
- | } else { | ||
- | goToPage(' | ||
- | } | ||
- | </ | ||
- | |||
- | ** Option 2: Skip the irrelevant questions ** | ||
- | <code php> | ||
- | if (value(' | ||
- | goToPage(' | ||
- | } | ||
- | | ||
- | | ||
- | | ||
- | </ | ||
- | |||
- | 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 | ||
- | [[# | ||
- | |||
- | ===== The right ID ===== | ||
- | |||
- | The example uses a selection. A simple selection will save just one value, the chosen option. This can be called with '' | ||
- | |||
- | 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)! | ||
- | |||
- | {{: | ||
- | |||
- | 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**. | ||
- | |||
- | {{: | ||
- | |||
- | 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(' | ||
- | question(' | ||
- | question(' | ||
- | } else { | ||
- | question(' | ||
- | } | ||
- | </ | ||
- | |||
- | 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 " | ||
- | |||
- | {{: | ||
- | |||
- | 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 '' | ||
- | * Combining the commands '' | ||
- | |||
- | <code php> | ||
- | // Screenout with individual text | ||
- | if (value(' | ||
- | text(' | ||
- | buttonHide(); | ||
- | } | ||
- | </ | ||
- | |||
- | **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 ({{: | ||
- | * The debug mode is also helpful, in case of empty pages or unexpected outcomes of '' | ||
- | * SoSciSurvey can tell you what value was saved under a certain item/a certain question. | ||
- | * You can print the value of value(): \\ '' | ||
- | * You can print whether a condition returns true (it is complied) or false (not complied): \\ '' | ||
- | * Jumping through pages using '' | ||
- | * The command '' | ||
- | * After having jumped through pages using '' | ||
- | |||
- | ===== 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 |