This translation is older than the original page and might be outdated. See what has changed.
Translations of this page:
 

This is an old revision of the document!


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 (Check Responses).

More complex checks can be done with a little PHP-Code. A PHP filter is used together with the function 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 Filter Questions (PHP Filters).

Tip: Use the 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 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 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 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).

// 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');
}

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.

$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');
}

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.

// 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');
}
en/create/checks-php.1563806201.txt.gz · 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