x

Main chapters

  1. Get started
  2. Workspace
  3. Editor
  4. Question types
  5. Share
  6. Results
  7. Configuration
  8. Installation and updates
  9. Profile and personal settings
  10. Account and billing
  11. FAQs
  12. Development
 Actions

Check survey logic - Advanced

From LimeSurvey Manual
(Redirected from Validate Survey Logic)


General

An important option that helps you create and easily maintain complex surveys is the survey logic overview.

Throughout development and testing of the survey, and before activating it, it is very important to validate the survey logic. This is especially true when you use complex relevance, tailoring, and validation equations - you need to be sure that nothing will break when you run the survey.

This feature lets you quickly validate the accuracy of your survey, group(s), and question(s). In the classic editor, click Tools ⇒ Survey logic overview in the survey toolbar; on the page of a question, Tools ⇒ Check logic shows the logic of that question. In the new editor, click Check logic in the menu of a question or question group in the survey structure.


Tools menu of the classic editor with the Survey logic overview languages English and German highlighted


In a multilingual survey, choose the language. With more than three languages, the menu shows Other languages for the additional ones.

Description

The survey logic overview shows everything that you specified for each question and group (e.g., name, text, help, conditions/relevance, validation rules, defaults, subquestions, answers) in a convenient tabular format. It highlights the errors. The question and group IDs open the question or group in a new browser tab, and the variable names used within equations open the question that sets them. This makes it easy to quickly edit any errors and refresh the logic check page to confirm the accuracy of the survey before activating it.

The button Open printable view above the table opens the same overview without the administration menus, for printing.

The display is also designed to be be readable by researchers and study sponsors so that they can validate the accuracy of the survey design and logic. Checking the survey logic updates the cache for all expressions used within an active survey.

It includes the following columns:

  • # - shows the Group and Question sequence counts, starting from 0.
  • Name [ID] - shows the question code for the group/question/subquestion. These codes can be used as variables within expressions. ID is the question id (QID), or group id (GID). This field also shows the question type (e.g., Multiple choice [M])).


To find out more about which variables can be used within expressions, read the following wiki subsection.


  • Condition [Validation] (Default value) - shows the following:
    • Condition - the syntax-highlighted relevance equation for the question or group. If it is always true (to be shown in any scenario), the value will be 1.
    • Validation - ExpressionScript automatically generates the validation equation based upon the selected question attributes (e.g., min/max number of answers, min/max/equals sum values, min/max individual values or regular expression validation). This section shows the generated validation equation so that you can detect if there are any errors (such as undefined variables).
    • Default - if the question has a default value, it is shown here, syntax-highlighted (since the default could be an expression).
  • Text [Help] (Tip) - shows the following:
    • Text - the text of the group, question, subquestion, or answer. It is syntax-highlighted to show any embedded tailoring, thus letting you verify that you have declared all the variables you plan to use in the tailoring.
    • Help - this shows the help text for the question, also syntax-highlighted.
    • Tip - this shows the internally generated validation tip, based upon the question attributes. This same tip is used in all survey styles, plus in the printable survey and data entry screens.
    • Question Attributes - this shows a table of all relevant question attributes for this question. Attributes that might be equations are syntax-highlighted so that you can validate their accuracy.

Rows are color coded as follows:

  • Groups - are shown with a light grey background
  • Questions - are shown with a light green background
  • Subquestions - are shown with a pale yellow background
  • Answers - are shown with a plain white background

Answers have an additional attribute in the Condition column:

  • Value - this is the default internal value used by calculations. If you are using Assessments, this will be the assessment value. Otherwise, this will be the same as the answer name.


The survey description, welcome and end messages, end URL, Survey data policy notice and label are listed within the survey logic overview (above the table) if their corresponding fields are not empty!

Usage

At the top of the page, there is a summary message. If all is well, it says "No syntax errors detected in this survey." (or, when you check a group or question: "This group, by itself, does not contain any syntax errors." / "This question, by itself, does not contain any syntax errors."). If there are errors, it says "X questions contain errors that need to be corrected." Warnings are summarized as "X questions contain warnings that need to be verified."

Each question that has syntax errors gets the background of its leftmost column (i.e. #) color-coded red. Also, a red label "This question has at least X error(s)." is displayed under the Name [ID] column. The following errors are common:

  • Undefined variable - if you have not defined all of your variables, or mistyped array_filter (or different sets of answer options for array_filter), then some of your validation questions will show errors. Undefined variables are shown in red text, boxed with a red line.
  • Bad syntax - as you start to use relevance equations, you may use too many or too few parentheses. Such syntax problems are highlighted and boxed in red. If you hover the mouse over any such red-boxed text, a tool-tip will describe the error.

Colors in ExpressionScript syntax

Conditions and equations are syntax-highlighted to easier figure out what you are looking at:

  1. Green / Light Blue: A variable that references a question earlier in the survey (green: on an earlier page, light blue: on the same page)
  2. Blue: A function
  3. Grey: A string expression
  4. Brown: A TOKEN expression (participant data)
  5. Black: Operator

Things to check:

  1. Purple: A variable that references a question later in the survey. Usually this is an error and needs to be checked.
  2. Red or red frame: A non-existing variable or reference to an earlier question or a syntax error - usually needs to be checked.


Undefined variables

If undefined variables are used, the respective variable name will be color-coded in red and surrounded by a red line. If you hover your mouse over the variable name, it will say "Undefined variable". In this example, the text of p3sum refers to p3nam instead of p3name:


Survey logic overview row with the undefined variable p3nam framed in red and the tooltip Undefined variable


  Attention : LimeSurvey does not allow survey administrators to create questions that use the same question code. When you import a question group, conflicting question codes are renamed, and the import shows a warning such as "Question code X was updated to Y.". When you import a single question in the classic editor, switch on Automatically rename question code if already exists?, otherwise the import fails. If a survey still contains duplicate question codes (for example a survey from an old version), the survey logic overview shows "This variable name has already been used." under the question code, with a link to the question that uses the code first. Correct it: ExpressionScript and the export of the responses (for example to R or SPSS) use the question code as the variable name.



Survey logic overview rows where the question name shows the error This variable name has already been used

Bad syntax

Most of the expression-related mistakes are related to bad syntax. This is related to the fact that survey administrators usually miss to add a curly bracket, to properly make use of parentheses, or they use expressions wrongly:


Survey logic overview row with an expression framed in red because of a missing closing parenthesis
Survey logic overview row with an expression framed in red, the extra comma marked and the tooltip Not a valid expression

The whole expression is framed in red, and the wrong part has its own red frame. Hover over the expression to see the problem, for example "Missing 1 closing left parentheses" (first example) or "Not a valid expression" (second example, an extra comma before the closing parenthesis).


Here are many good examples on the usage of syntax highlighting.


Bad custom JavaScript

Custom JavaScript in a question text or help text can also show up as an error. If a curly brace is directly followed by code, as in if(true){document.write('hi there!')}, ExpressionScript tries to evaluate the code as an expression and reports an error, here "Undefined function". Put a space or a line break after the opening curly brace, see Keep curly brackets apart:


Survey logic overview row with JavaScript in the help text, where document.write is framed in red with the tooltip Undefined function

Speeding editing and validation

All of the syntax-highlighted text has tooltips embedded, which are clickable:

  1. Tooltips
    • Functions - hovering the mouse lets you see the purpose and syntax definition of the function;
    • Variable Names - hovering the mouse lets you see the position (group, question sequence), question text, and allowable answers for the question.
  2. Actions
    • Variable Names - clicking on the variable name opens the question so that you can edit it (the question and group IDs open in a new browser tab). This makes it easy to navigate and verify logic - simply keep clicking on variable names of relevance or validation criteria for the question to see where they come from and how they are used.


Examples

The following examples are taken from the ExpressionScript sample surveys. You can find screenshots of running surveys, explanations, and downloads on that page.


Body Mass Index

Here are screenshots of this example.

This is the reorder view of the Body Mass Index calculation (classic editor: Overview questions & groups ⇒ Reorder). The relevance equation of each question is shown in square brackets after the question code, followed by the question text, for example the equations of weightkg, heightm and BMI:


Reorder tab of the Survey organizer listing the BMI questions with their relevance equations in square brackets


For a better survey overview, check the survey logic page:


Survey logic overview of the BMI example with weight, weightunits, weightkg, height and heightunits, their validation and equations


This survey example is also a good example of nested if() statements to generate the "weightstatus".


Survey logic check of the BMI example with the equations heightm, BMI and weightstatus with nested if() and the report1 text display

Cascading logic

Here are screenshots of this example.

It shows the subquestion validation logic that is automatically generated when you use array_filter and array_filter_exclude. This example also shows how you can substitute the tailored "Other" value (the answer for Q02_other is Q01_other).


Survey logic check of the cascading example with array_filter relevance on the Q02 subquestions and Q02_other showing Q01_other


Q05 in this example shows simultaneous use of array_filter and array_filter_exclude on Q01 and Q02, respectively. This example demonstrates cascading array_filter capabilities. Note that one of the main reasons for showing the question and subquestion level validation criteria is to help ensure you have not made any typos in specifying the array_filter or array_filter_exclude variable names (or in case you use different variable names for your list of filtered subquestions). If you have such typos, all the invalid variable names will appear in red indicating that they are undefined, letting you quickly fix the problem.


Survey logic check of Q04, Q04A and Q05 with the relevance equations generated by array_filter and array_filter_exclude on Q01 and Q02


Dynamic relevance

This example demonstrates dynamic cascading relevance logic to control display of question visibility. You can download this example here (Sample Census).

Also note that questions are displayed only if certain criteria are met. For example, gender is only displayed if name was answered, age only if gender was answered, cohabs only if age was answered, and the group Person 1 only if cohabs > 0.


Survey logic check of the census example with cascading relevance on gender, age and cohabs and the Person 1 group shown if cohabs is above 0

Group-level relevance

This example shows how group-level relevance appears in the logic check. Here are screenshots of the example described below.

As you can see, the group-level relevance equation (cohabs > 1 && p1rel != "") appears in the grey Person 2 row for G-2.

You may also notice that all of the questions except p2sum are mandatory (marked with an asterisk). However, if the group is irrelevant, so are all its questions. As a result, those questions are only truly mandatory if the group is relevant.

You may also note that certain questions are displayed only if the answer to the previous question is not empty. You may see below that if p2sex is not filled in, p2name is not going to be displayed, even though it is a mandatory question. The mandatory question p2age is also not going to be displayed if p2name is not filled in. These questions can be considered "conditionally mandatory".

Additionally, note that the tip messages are also automatically created for you. They are organized by value range (min/max), sum value range (min/max/equals), number of answers (min/max), etc (it depends on the used question type and active attributes). Sometimes you want to validate an answer range but don't want to display what might appear to be silly validation tips to the user. In such cases, you can use the hide_tip question option (as in this case, to avoid telling the user that the age must be between 0 and 115 unless they try to enter a bad value - see p2age).


Survey logic check of the Person 2 group with group relevance on cohabs and p1rel, tailored question texts and hide_tip on p2age

Comma as radix (decimal) separator

Although LimeSurvey fully supports the use of comma as radix (decimal) separator at run-time, you must still use a decimal as the radix separator at the design-time (e.g., when specifying min/max values in advanced question attributes). The working example can be found here.

Write such numbers with a leading zero (0.5, not .5): the survey logic overview shows a number that starts with a decimal point as an error.

Also, remember that the validation logic is created for you automatically from the enabled question attributes. The equations may look overwhelming, but you don't need to worry about them.


Survey logic check of numerical questions Q1 and Q2 with min and max values such as 1.5 and 8.2 and the generated validation equations