Differences

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

Link to this comparison view

en:create:checks-php [22.07.2019 16:36] (current)
johannes.jungilligens created
Line 1: Line 1:
 +====== Individual Response Check ========
 +
 +Formal requirements for an answer -- for example, that all items of a scale have been answered or that an open text input follows a certain pattern -- can be defined with a few clicks in the question ([[:​en:​create:​checks]]).
 +
 +More complex checks can be done with a little [[:​en:​create:​php|PHP-Code]]. A PHP filter is used together with the function [[:​en:​create:​functions:​repeatpage|repeatPage()]].
 +
 +**Important:​** The PHP code for checking the answer must be placed at the top of the next page __after__ the page of the question. So, if the question to check is asked on page 2, the PHP code to check must be at the top of page 3.
 +
 +**Note:** Consider in advance whether checking the answer is really useful. This is the case if participants unintentionally enter their information in the wrong format. If, on the other hand, you expect participants to fail to adhere to the guidelines due to lack of motivation ("just click through"​) or intentionally,​ then it is often better to accept the incorrect information and filter it during data cleansing.
 +
 +**Tip:** For more details on programming PHP filters, see [[:​en:​create:​php-filters]].
 +
 +**Tip:** Use the ''​[[:​en:​create:​functions:​markfail|markFail()]]''​ function to mark input fields with incorrect information.
 +
 +You can display an error message if you enter the ID of a text module as an argument in the ''​repeatPage()''​ function (see below). Alternatively,​ you can also display a corresponding text block using [[:​en:​create:​functions:​text|text()]]. For the second variant, when creating the text module, select that the text is displayed in the style of a //warning// -- then it receives the same formatting as the standard error messages.
 +
 +
 +===== Example: Multiple Selection Matrix =======
 +
 +In an ordinary scale you can easily check whether the participant has selected an option in each line. But if you use a [[:​en:​create:​combine|combined question]], which allows the participant several crosses per line (multiple choice matrix), then you need some PHP code to check if really something was selected in each line.
 +
 +The following example assumes that 4 multi-select questions ("​MA01"​ to "​MA04"​) with 10 items each were combined to a multiple choice matrix. If there is no cross in a line, it should be highlighted and the text "​MA05"​ should be displayed as error message. ​
 +
 +
 +==== Variant A =====
 +
 +One possibility is to simply count the selected items for each line (item). If nothing was selected, the item number is put on a list ''​$missing''​ and afterwards the errors are marked.
 +
 +The function [[:​de:​create:​functions:​id]] creates the variable identifier to retrieve the answer (e.g. "​MA01_05"​) from the identifier of the category ("​MA"​),​ the number of the question (e.g. 1) and the number of the item (e.g. 5).
 +
 +<code php>
 +// The unanswered items are collected in the $missing list.
 +$missing = array();
 +// The getItems() function returns a list of item numbers (=lines).
 +// The construction FOREACH goes through all of them in a loop
 +foreach (getItems('​MA01',​ '​all'​) as $item) {
 +  // Go through all columns (questions) for this item in a FOR loop
 +  $anyChecked = false;
 +  for ($col = 1; $col <= 4; $col++) {
 +    $itemID = id('​MA',​ $col, $item);
 +    if (value($itemID) == 2) {
 +      $anyChecked = true;
 +    }
 +  }
 +  // Note item, if nothing was marked
 +  if (!$anyChecked) {
 +    $missing[] = $item;
 +  }
 +}
 +
 +// Check whether an item remained unanswered
 +if (count($missing) > 0) {
 +  // Highlight the missing items
 +  foreach ($missing as $item) {
 +    markFail(id('​MA01',​ $item));
 +  }
 +  // Display message and repeat page
 +  repeatPage('​MA05'​);​
 +}
 +</​code>​
 +
 +
 +==== Variant B =====
 +
 +If you prefer to work with sets of numbers, you can determine the unchecked items (code: 1) per column and form their overlap.
 +
 +<code php>
 +$missing01 = getItems('​MA01',​ '​==',​ 1);
 +$missing02 = getItems('​MA02',​ '​==',​ 1);
 +$missing03 = getItems('​MA03',​ '​==',​ 1);
 +$missing04 = getItems('​MA04',​ '​==',​ 1);
 +$missing = array_intersect($missing01,​ $missing02, $missing03, $missing04);​
 +
 +// Check whether an item remained unanswered
 +if (count($missing) > 0) {
 +  // Highlight the missing items
 +  foreach ($missing as $item) {
 +    markFail(id('​MA01',​ $item));
 +  }
 +  // Display message and repeat page
 +  repeatPage('​MA05'​);​
 +}
 +</​code>​
 +
 +
 +===== Example: All Items or None at All ======
 +
 +The following PHP code checks whether all items on a scale were answered -- or none at all. The question identifier (in this example ''​AB01''​) must be changed according to the actual question identifier, the error message must be saved as a text module. If the identifier of this text module is not "​error_AB01",​ this identifier must also be changed.
 +
 +<code php>
 +// Create a list of all items in the question
 +$items = getItems('​AB01'​);​
 +// Determining the answered items
 +$answered = getItems('​AB01',​ '>',​ 0);
 +// error and back, if not all or none were answered
 +if ((count($answered) > 0) and (count($answered) < count($items))) {
 +  repeatPage('​error_AB01'​);​
 +}
 +</​code>​
  
en/create/checks-php.txt · Last modified: 22.07.2019 16:36 by johannes.jungilligens
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki