Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
en:create:php-filters [21.09.2019 19:13] – [Instruction blocks] sarah.lahmeyeren:create:php-filters [23.09.2019 08:54] – Titles admin
Line 1: Line 1:
- +====== Filters and Conditional Questions (PHP Filters) ====== 
-====== Filters and Conditional Questions (php-filters) ======  +
- +
  
 If the participant's answer to a question is to have an effect on the further course of the interview, it is a filter question. You can define filters either directly in the question ([[en:create:simple-filters|question filter]], simple variant) or in the **questionnaire** (//PHP filter//). If the participant's answer to a question is to have an effect on the further course of the interview, it is a filter question. You can define filters either directly in the question ([[en:create:simple-filters|question filter]], simple variant) or in the **questionnaire** (//PHP filter//).
Line 16: Line 13:
 **Tip**: If you separate respondents into a control group and an experimental group ([[en:create:questions:random|Random generator]], [[en:create:randomization|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. **Tip**: If you separate respondents into a control group and an experimental group ([[en:create:questions:random|Random generator]], [[en:create:randomization|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 ===== 
-  
  
 +===== 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. A Yes-No-Filter is probably the most used filter. 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. A Yes-No-Filter is probably the most used filter.
  
Line 24: Line 21:
  
 **Example 2:** If a questionee answers "no", multiple pages are skipped. **Example 2:** If a questionee answers "no", multiple pages are skipped.
 +
  
 ==== Preparations ==== ==== Preparations ====
Line 47: Line 45:
 **Attention:** Should there be more variables, e.g. ''PT01_01'' and ''PT01_02'', check the question type. It is important you chose the question type "Selection" or "Dropdown Selection" if you only allow the choice of one option, __not__ "Multiple Choice". **Attention:** Should there be more variables, e.g. ''PT01_01'' and ''PT01_02'', check the question type. It is important you chose the question type "Selection" or "Dropdown Selection" if you only allow the choice of one option, __not__ "Multiple Choice".
  
-===== Entering a php-code =====+ 
 +===== Entering a PHP Code =====
  
 The filter should be entered as a //PHP code// when **Creating A Questionnaire**, at the exact point where the filter should be active (e.g. where the second question is shown or skipped). Simply drag-and-drop a //PHP code// from the right sight into your questionnaire page ([[::en:create:php|Introduction to PHP]]). The filter should be entered as a //PHP code// when **Creating A Questionnaire**, at the exact point where the filter should be active (e.g. where the second question is shown or skipped). Simply drag-and-drop a //PHP code// from the right sight into your questionnaire page ([[::en:create:php|Introduction to PHP]]).
Line 53: Line 52:
 **Important:** A PHP filter can __never__ be on the same page as the filter question (the question the answer refers to, in this case ''PT01''). SoSci Survey loads the questionnaire page by page in order to show the questionee the whole page. The moment the page with the filter question loads, the questionee cannot possibly have answered the questions - and thus the filter cannot react to the answer (''[[:en:create:functions:value|value()]]''). **Important:** A PHP filter can __never__ be on the same page as the filter question (the question the answer refers to, in this case ''PT01''). SoSci Survey loads the questionnaire page by page in order to show the questionee the whole page. The moment the page with the filter question loads, the questionee cannot possibly have answered the questions - and thus the filter cannot react to the answer (''[[:en:create:functions:value|value()]]'').
  
-===== Frequent use cases =====+===== Frequent Use Cases =====
  
 If you can handle the PHP function ''[[:en:create:functions:value]]'', you can implement a number of filters with it. If you can handle the PHP function ''[[:en:create:functions:value]]'', you can implement a number of filters with it.
  
  
-==== Whether to display question or not ====+==== Whether to Display Question or Not ====
  
 The following PHP code uses ''[[:en:create:functions:value]]'' to check whether response code ''1'' has been stored for variable ''PT01''. If this is the case, the question ''AB01'' is displayed using ''[[:en:create:functions:question|question()]]''. The following PHP code uses ''[[:en:create:functions:value]]'' to check whether response code ''1'' has been stored for variable ''PT01''. If this is the case, the question ''AB01'' is displayed using ''[[:en:create:functions:question|question()]]''.
Line 77: Line 76:
  
  
-==== Show different questions ====+==== Show Different Questions ====
  
 Depending on a previous answer (or a group assignment, e.g. by [[:en:create:randomization]]), should question A or question B be shown? Depending on a previous answer (or a group assignment, e.g. by [[:en:create:randomization]]), should question A or question B be shown?
Line 116: Line 115:
  
  
-==== Vary content ==== +==== Vary Content ====
  
 Instead of questions, contents, e.g. stimuli, can also be varied. More about this in the chapter [[:en:create:randomization]]. In the following example - depending on the code in variable AF01 - either the text module with the identifier "stimulus1" or "stimulus2" is displayed. Instead of questions, contents, e.g. stimuli, can also be varied. More about this in the chapter [[:en:create:randomization]]. In the following example - depending on the code in variable AF01 - either the text module with the identifier "stimulus1" or "stimulus2" is displayed.
Line 129: Line 127:
 </code> </code>
  
-==== Skip pages ==== 
  
 +==== Skip Pages ====
  
 When **compiling the questionnaire**, you now select the page on which you want to continue with a "no". Enter an //identifier// for this page, e.g. "no TV" (assign an identifier for a page). When **compiling the questionnaire**, you now select the page on which you want to continue with a "no". Enter an //identifier// for this page, e.g. "no TV" (assign an identifier for a page).
Line 147: Line 145:
 **Tip**: Read the rest of this chapter. It's worth it! **Tip**: Read the rest of this chapter. It's worth it!
  
-===== The keyword IF ===== 
  
 +===== 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. Filters are programmed through IF-THEN-ELSE-relations in the element //PHP-code//. **If** and **else** are the keywords in programming language.
Line 168: Line 166:
   }   }
  
-===== Show or do not show questions in the questionnaire =====+ 
 +===== Show or do not Show Questions in the Questionnaire =====
  
  
Line 192: Line 191:
 {{fig.filters.jump.png?nolink|Überspringen von Fragen durch Filter}} {{fig.filters.jump.png?nolink|Überspringen von Fragen durch Filter}}
  
-===== Conditions ===== 
  
 +===== Conditions =====
  
 A condition is always put into round brackets (). It may be complied with (the condition will return “true”) or not (“false”). A condition is always put into round brackets (). It may be complied with (the condition will return “true”) or not (“false”).
Line 208: Line 207:
 **Tip**: To check if a variable has the value 1 __or__ 2, read [[en:create:filter-boolean|]] **Tip**: To check if a variable has the value 1 __or__ 2, read [[en:create:filter-boolean|]]
  
-===== Skip pages ===== 
  
 +===== Skip Pages =====
  
 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. 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.
Line 221: Line 220:
 **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#Comfortable_programming_questions_and_ texts|Comfortable Programming]]) **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#Comfortable_programming_questions_and_ texts|Comfortable Programming]])
  
-===== Assigning page IDs =====+ 
 +===== 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()]]'' 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()]]''
Line 229: Line 229:
 {{scr.filters.page_ident.png?nolink|Eine Seiten-Kennung zuweisen}} {{scr.filters.page_ident.png?nolink|Eine Seiten-Kennung zuweisen}}
  
-==== Using as little PHP-code as possible ====+ 
 +==== Using as Little PHP Code as Possible ====
  
 {{scr.filters.simple.page1.png?nolink|Seite 1}} {{scr.filters.simple.page1.png?nolink|Seite 1}}
Line 237: Line 238:
 {{scr.filters.simple.page3.png?nolink|Seite 3}} {{scr.filters.simple.page3.png?nolink|Seite 3}}
  
-==== Using as much 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, the following form is identical to the above. 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.
Line 265: Line 266:
 **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. **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() ==== 
  
 +==== 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|Create a Questionnaire]]).   * 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|Create a Questionnaire]]).
Line 272: Line 273:
   * All functions specific to SoSciSurvey are listed and explained at [[functions|PHP functions]].   * All functions specific to SoSciSurvey are listed and explained at [[functions|PHP functions]].
  
-===== Instruction blocks ===== 
  
 +===== 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. 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.
Line 292: Line 293:
 </code> </code>
  
-===== Step by step =====+===== Step by Step =====
  
 This paragraph explains the programming of a small filter step by step. This paragraph explains the programming of a small filter step by step.
  
-==== The filter question ====+ 
 +==== 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. 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.
Line 321: Line 323:
 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. 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 ====+ 
 +==== 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. 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.
Line 339: Line 342:
  
 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. 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 else ====
Line 352: Line 356:
   question('IN04');  // Why no internet connection at home?   question('IN04');  // Why no internet connection at home?
 } }
-question('AB01'); // Job description |+question('AB01'); // Job description 
  
-===== Skip pages =====+===== 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": 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":
Line 373: Line 378:
 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. 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.
  
-** Jump to page “usage” if there's nothing to ask **+** Option 1: Jump to page “usage” if there's nothing to ask **
 <code php> <code php>
 if (value('TF_01') == 1) { if (value('TF_01') == 1) {
Line 398: Line 403:
 **Tip**: Asking some benchmark data reveals the questionee is not of interest to you? [[#screenoutfilter_unsuitable_participants|Screenout: Filter unsuitable participants]] **Tip**: Asking some benchmark data reveals the questionee is not of interest to you? [[#screenoutfilter_unsuitable_participants|Screenout: Filter unsuitable participants]]
  
-===== The right ID =====+**Tip**: Ever since SoSci Survey Version 2.2.02 empty pages will be skipped by default. Thereby you often times don't have to worry about empty pages. 
 + 
 + 
 +===== The Proper 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). 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).
Line 434: Line 442:
 The chapter [[filter-itemcount|At least one item]] shows some tricks on how to use scales in filters. The chapter [[filter-itemcount|At least one item]] shows some tricks on how to use scales in filters.
  
-===== Screenout: Filter unsuitable participants =====+===== 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. 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.
Line 452: Line 460:
 **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|Linking Multiple Conditions]]). **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|Linking Multiple Conditions]]).
  
-===== Tips about filters / Problem solving =====+===== 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). 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).
Line 469: Line 477:
   * After having jumped through pages using ''goToPage()'' and hitting next, the questionnaire will continue normally.   * After having jumped through pages using ''goToPage()'' and hitting next, the questionnaire will continue normally.
  
-===== Complex filters =====+===== Advanced 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: 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:
en/create/php-filters.txt · Last modified: 19.03.2021 10:02 by sophia.schauer
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki