Expression Manager/pt-br

Note: This feature is only available in LimeSurvey 1.92 or later.

=Quick Start Tutorial=

Overview
When customizing your surveys, you typically need a way to specify the following:
 * 1) Navigation/Branching - letting a subject's answers change the order in which questions are asked
 * 2) Tailoring/Piping - how to phrase the question (such as referring to prior answers, or conjugating sentences based upon the number or gender of your subjects), or how to generate custom reports (like assessment scores or tailored advice).
 * 3) Validation - ensuring that answers pass certain criteria, like min and max values, or matching an input pattern

ExpressionScript (EM) provides an intuitive way to specifying the logic for each of those features. Nearly anything that you can write as a standard mathematical equation is a valid expression, even if you are calling functions. EM currently provides access to 70 functions, and can be easily extended to support more. It also lets you access your variables using human-readable variable names (rather than SGQA names).

The following sections show the main places where ExpressionScript is used

Relevance (Controlling Navigation/Branching)
Some surveys use "Goto Logic", such that if you answer Question 1 with option C, then jump to Question 5. This approach is very limiting, since is hard to validate, and easily breaks if you have to re-order questions. EM uses a Boolean relevance equation to specify all of the conditions under which a question might be valid. If the question is relevant, then the question is asked, otherwise it is Not Applicable, and the value NULL is stored in the database. This is similar to what can be done via the Conditions editor, but EM lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA naming).

This image shows one way to review the relevance logic for a survey. It computes Body Mass Index. The relevance equation is shown in square bracket right after the variable name (which is in green). So, the relevance of weight, weight_units, height and height_units are all 1, meaning that those questions are always asked. However, the relevance for BMI is {!is_empty(height) and !is_empty(weight)}, which means that BMI will only be computed if the subject enters a value for both height and weight (thereby avoiding the risk of a divide by zero error). Also, the Report question is only shown if the subject answers all four main questions (height, height_units, weight, weight_units).



Relevance is shown and editable in the following places:

Viewing / Editing Question-Level Relevance
This equation computes the Body Mass Index (BMI). It is only asked if the person first enters their height and weight.



This is the edit screen for the BMI question.



Note that you do not use the curly braces when you enter a Relevance Equation.

Viewing / Editing Group-Level Relevance
This is a sample census survey. The first page asks how many people live with you and stores that in the "cohabs" variable. This page is only shown if you have more than one cohabitant (so it is shown for the second person cohabitating with you), and also only shows if you specified how Person One is related to you (p1_rel).



As you can see, the group also has question-level relevance criteria, such that each question only appears once you have answered the question before it (e.g. {!is_empty(p1_sex)}). EM combines the Group and Question-level relevance for you. Questions in a group are only asked  if the group as a whole is relevant. Then, only the subset of questions within the group that are relevant are asked.

Here is the screen for editing the group-level relevance for that question:



Note that you do not use the curly braces when you enter a Relevance Equation.

Tailoring/Piping
EM lets you easily do simple and complex conditional tailoring of your questions. Sometimes you just need simple substitution, like saying, "You said you purchased [ Product].  What did you like best about it?". Sometimes you need conditional substitution like " [ Mr./Mrs.] [ LastName], would you be willing to complete our survey?". In this case,  you want to use Mr. or Mrs. based upon the person's gender. Other times you need even more complex substitution (such as based upon a mathematical computation). EM supports each of these types of tailoring/piping.

Conditional Equations
The Body Mass Index example shows the ability to compute a person's BMI, even while letting them enter their height in weight in metric or non-metric units.



Here weight_kg is {if(weight_units == 'kg', weight, weight * .453592)}. This if function means that if the subject entered the weight using kilograms, use that value, otherwise multiple the entered value (which was in pounds) by .453592 to convert it to kilograms. The height_m variable uses a similar approach to compute the person's height in meters, even if he entered his height in inches.

BMI computes the weight formula as {weight_kg / (height_m * height_m)}.

Lastly, the report conditionally tailors the message for the subject, telling him what he entered. ("You said you are 2 meters tall and weight 70 kg.")

Although not well shown in the above image, weight_status uses nested if statements to categorize the person as underweight to severely obese. You can see its equation in the Show Logic View



From the edit window for this question, you can see two things:
 * 1) Tailoring must surround expressions with Curly Braces
 * 2) Expressions can span multiple lines if, as in this case, you want to make it easier to read the nested conditional logic.



Tailored Questions, Answers, and Reports
Note: Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a  tag which is not valid inside select options.

This example shows the BMI report.



Here is the edit window for the same question.



As you can see, anything in curly braces is treated as an expression, so is syntax-highlighted (color coded) in the prior image. If you had any typos (such as misspelled or undefined variable names or functions), EM would show an error, such as this, showing that height_unit is an undefined variable name (it is actually height_units), and rnd is an undefined function (the proper function name is round). In both cases, the errors are surrounded by a red box to make it easier to spot and fix them.



You can also see that you can quickly create complex reports, such as a table of entered values or tailored advice.

Please remember that all tailoring must surround expressions with Curly Braces, so that LimeSurvey knows which parts of the question are free text and which should be parsed through ExpressionScript.

Validation
EM controls how most of the advanced question options work. These control aspects like min/max numbers of answers; min/max individual values; min/max sum values; and checking that entered values match specified string patterns. You continue to enter those advanced question options as usual. However, now any value in one of those fields is considered an expression, so you can have min/max criteria with complex conditional relationships to other questions.

In all of these cases, since the advanced question option is always considered an expression, you do not use curly braces when specifying it.

The Sample Surveys pages shows many working examples of using expressions for validations.

=Introduction=

LimeSurvey 1.92 and later uses the new ExpressionScript (EM) module which will let LimeSurvey support more complex branching, assessments, validation, and tailoring. It will replace how LimeSurvey manages Replacements, Conditions, and Assessments on the back-end. It will also speed up processing considerably since it eliminates most run-time database reads. EM was developed by Dr. Thomas White (TMSWhite).

This wiki page is the definitive reference for ExpressionScript syntax and functionality.

Key Definitions

 * 1) Expression:  Anything surrounded by curly braces
 * 2) *As long as there is no white space immediately after the opening brace or before the closing curly brace
 * 3) *The contents of Expressions are evaluted by EM, so they can contain mathematical formulas, functions, and complex string and date processing.
 * 4) Tailoring: Sometimes called "piping", this is the process of conditionally modifying text
 * 5) *You have access to all 'replacement fields', TOKENs, and INSERTANS values
 * 6) *You also have easier access to questions, answers, and their properties.
 * 7) Relevance Equation:  A new question attribute controlling question visiblity
 * 8) *If there is a relevance equation, then the question is only shown if the relevance evaluates to true.
 * 9) *Internally, all array_filter and array_filter_exclude commands become subquestion-level relevance
 * 10) Equation Question Type:  A new question type that saves calculations or reports to the database
 * 11) *It is like a Boilerplate question, but its contents are saved to the database even if you set "Always Hide this Question"
 * 12) SGQA:  This is how variables are named in LimeSurvey <= 1.91+
 * 13) *Stands for Survey-Group-Question-Answer
 * 14) *SGQA variable names look like 123X5X382X971, and may have subquestion suffixes.
 * 15) *These variable names are specific to the underlying S/Q/G/A database codes, so often need to be changed
 * 16) Question Code:  This is the preferred variable name for EM
 * 17) *This can be a descriptive name indicating the purpose of the question, making it easier to read complex logic
 * 18) *Although not required to be unique in <= 1.91+, it must be unique if you want to use EM
 * 19) *Valid question codes should NOT start with a number, so when using the question code to number your questions, simply use "q1", or "q1a" or "g1q2".
 * 20) *This is what currently becomes the variable name if you export data to SPSS or R, so if you do statistical analysis, you probably already made this unique.

Do I have to use EM?
The short answer is No (but also yes).

EM is fully backwards-compatible with existing surveys. So, if you are happy to use Conditions and Assessments in the style that LimeSurvey used in versions <= 1.91+, you can continue to do so.

However, EM completely replaces how LimeSurvey internally deals with Conditions. Although you can still use the Conditions Editor to create and manage conditions, LimeSurvey 1.92 will convert those to the equivalent Relevance Equations. As part of the upgrade, LimeSurvey 1.92 will auto-convert all existing Conditions to Relevance Equations.

This should give you the best of both worlds - you can continue using LimeSurvey as you are used to, but will see the Relevance Equation equivalent so you can gradually migrate to Relevance Equations directly whenever you see fit.

Can I mix use of Conditions and Relevance?
Yes. You can use the Conditions editor for some questions and the Relevance editor for others. Conditions are auto-converted to Relevance when you save the question.

Note, we assume that if you are using the Conditions editor, that you want those Conditions to over-write any manually entered Relevance equation. So, if you have existing Conditions and want to manually edit the Relevance, please delete the Conditions for that question first. Specifically, copy the generated relevance equation to a text editor, use the Conditions menu to delete all of the conditions for that question (which will also delete the relevance), then edit the question and paste the generated relevance equation from the text editor back into the relevance field for that question (and save the question). If there is enough demand for deleting conditions without deleting the generated relevance equation, we could add a bulk conversion process.

How should I choose between Conditions and Relevance?
Here is a list of pros and cons of each style:

The bottom is that if you are happy with how LimeSurvey 1.91+ works, there is no reason to change what you do.

What are some other benefits of using EM?
Here are some of the other reasons you might want to use EM.
 * 1) Calculations - you can create any calculation you can think of:
 * 2) *You have access to all common mathematical operators and functions
 * 3) *You have access to 70+ mathematical, date, and string processing functions
 * 4) *It is fairly easy for developers to add new functions if users need them
 * 5) Storing Calculations to Database
 * 6) *You can now compute simple and complex calculations and/or scale scores AND have them stored in the database without needing JavaScript.
 * 7) *You use the Equation question type to accomplish this.
 * 8) Assessments
 * 9) *You can now create assessments or scale scores from any question type, not just the subset that used to be supported
 * 10) *You can use Tailoring to show running or total assessment scores anywhere needed - even on the same page
 * 11) *You have more control over the reports generated based upon those assessment scores
 * 12) *You can store assessment scores in the database without needing JavaScript
 * 13) *You can hide assessment scores without needing JavaScript or CSS
 * 14) Replacement Fields
 * 15) *Instead of using {INSERTANS:SGQA}, you can just use the Question Code - this makes it easier to read and validate.
 * 16) *This also avoids the common need to edit questions to change the SGQA code to make everything work.
 * 17) Tailoring - you can conditionally display text based upon other values
 * 18) *Use the appropriate title for a subject, like (e.g. "Hello [ Mr./Mrs.] Smith")
 * 19) *Output gramatically correct sentences based when singular/plural matter:  (e.g. "You have 1 child" vs. "You have 2 children")
 * 20) *Appropriately conjugate verbs and decline nouns based upon subject's gender and plurality.
 * 21) New Variable Attributes - you can access the following to do your tailoring:
 * 22) * (no suffix) -  an alias for qcode.code
 * 23) *.code - the selected response code for the question if it is relevant (otherwise blank), or the text value if it is not a coded question
 * 24) *.NAOK - same as .code, but can be part of calculations or lists even if irrelevant
 * 25) *.value - the assessment value for the question if it is relevant (otherwise blank), or the text value if it is not a coded question
 * 26) *.valueNAOK - same as .value, but can be part of calculations or lists even if irrelevant
 * 27) *.shown - the answer as displayed to the user (this is what {INSERTANS:xxx}  does)
 * 28) *.qid - the question ID
 * 29) *.gid - the group ID
 * 30) *.sgqa - the SGQA value for the question
 * 31) *.jsName - the correct javascript variable name for the question, regardless whether defined on this page or another
 * 32) *.qseq - the question sequence (starting from 0)
 * 33) *.gseq - the group sequence (starting from 0)
 * 34) *.mandatory - whether the question is mandatory (Y/N)
 * 35) *.question - the text of the question
 * 36) *.relevance - the relevance equation for the question
 * 37) *.grelevance - the relevance equation for the group
 * 38) *.relevanceStatus - whether or not the question is currently relevant (1 if true, 0 if false)
 * 39) *.type - the question type (the one character code)
 * 40) Dynamic On-Page Changes
 * 41) *All Relevance, Calculation, and Tailoring works dynamically on a page - so changes in values instantly update the page
 * So, you have questions dynamically appear/disappear based upon whether they are relevant
 * 1) *Questions are also dynamically tailored based upon responses on the page, so you can see running totals, tailored sentences and customized reports.
 * 2) New Data Entry Screen
 * 3) *In addition to using the current data-entry system, you can just use Survey-All-In-One.
 * 4) *This supports the on-page relevance and tailoring, so data entry clerks can quickly tab through and they will only have to enter the relevant responses
 * 5) *This can be critical if your data entry person needs to see the tailoring, which is also dynamic.
 * 6) Eliminates the need for most custom JavaScript
 * 7) *EM easily supports complicated computations, scoring, tailoring and conditional logic.
 * 8) *Some things will still need JavaScript (like custom layouts and conditionally hiding question sub-elements), but your JavaScript can use the EM functions so that you can access questions by their Qcode instead of SGQA, and access any of the question properties listed above.

What are some other helpful new features enabled by EM?
Regardless of whether you continue to use the Conditions Editor or manually compose Relevance Equations, you get these additional benefits:
 * 1) You can create more complex validation criteria
 * 2) *All of the advanced question attribute (like max_answers, min_num_value_n, max_num_value) can use Expressions.  So, you min/max criteria can be easily adjusted based upon prior responses, even if they are on the same page.
 * 3) *EM also handles all regular-expression-based validation, so you can robustly combine preg and equation-based question attributes.
 * 4) Easy Re-ordering (or deleting) of Questions and Groups
 * 5) *Prior to version 1.92, you could not re-order questions or groups if LimeSurvey thought that such-re-ordering could break conditions in which they were used.  Similarly, you could not delete questions if any other questions depended upon them.
 * 6) *With EM's syntax highlighting, it is easy to see and validate whether you try to use questions before they are declared.  So, we now let you re-order or delete questions and groups whenever you like.  EM will update all of the syntax highlighting to show you potential errors.
 * 7) *The re-order questions view has been enhanced to help with such review.  It now shows the question's relevance equation and tailoring, so you can immediately see whether any variables become pink (meaning they are used before being declared).
 * 8) The Question/Group Navigation Index is always available and accurate
 * 9) *Prior to version 1.92, these indexes were not available if there were complex conditions
 * 10) *With EM, we can guarantee that they are accurate.
 * 11) *Subjects can even jump back, to a prior question, change the answer, then jump forward (or submit)
 * 12) **When jumping forwards, EM will re-validate all of the intervening questions/groups.
 * 13) **If any questions become irrelevant, they will be NULLed in the database so that your data is internally consistent
 * 14) **If any questions become relevant or newly fail mandatory or validation rules, EM will stop on that page and force the user to answer those questions before jumping to their final destination.
 * 15) Auto-conversion of Conditions to Relevance
 * 16) *When you upgrade your database, all existing surveys that have conditions will have relevance equations generated for them
 * 17) *Whenever you import a survey, relevance equations will be created as needed
 * 18) *Whenever you add, delete, or modify conditions, EM will generate the appropriate relevance equation.
 * 19) Convenient Syntax Highlighting
 * 20) *When EM shows the relevance equation, it will show the Qcode, even if you entered an SGQA code, as we assume this will be easier to read.
 * 21) *All variables are color coded to show whether they were declared before or after the current question (or before or after the current group).  This lets you quickly detect and fix cases where you try to use variables for relevance (including array_filter), tailoring, or validation equations prior to declaring them.
 * 22) *In addition, if you hover your mouse over the color-coded variable, you will see the most important metadata about that question.   This includes the Group Sequence #, Question Sequence #, Qcode, Text of the question, and all available answer choices (if it is a question type with enumerated answer choices).
 * 23) **The list of answer choices uses this syntax:  'answers':{key:val, ... }.
 * 24) **key has the syntax 'scale~code' where scale is the answer scale (e.g. for dual scale), and code is the answer code.
 * 25) **val has the syntax 'value~shown' where value is the assessment value (if using assessments, otherwise code)(e.g. Qcode.value), and shown is the display value as seen by the subject (e.g. Qcode.shown)
 * 26) **This means that many surveys can use calculations without needing assessment mode.  If you have enumerated answer options  that are unique, non-decimal, and non-negative, you can simply do calculations on the Qcode.code values.
 * 27) Easy review of entire survey logic and content
 * 28) *There is a new Show Survey Logic feature that lets you see everything about the survey (or group or question) on a single page.
 * 29) *It shows the Group, Question, Sub-Question, and Answer-level details for the selected scope (survey vs. group vs. question)
 * 30) *It also shows the relevance, subquestion-level relevance (for array_filter and array_filter_exclude), and generated validation equation (for preg and any validation rules like min/max sum/number of values), and all non-blank question attributes.
 * 31) *Everything is syntax-highlighted so that you can see potential syntax errors (like unbalanced parentheses or use of variables before they were declared)
 * 32) *The syntax-highligting supports rapid navigation and editing of the survey.
 * 33) **If you click on a variable name, it opens a browser window (or tab) that shows you that question and lets you edit it.
 * 34) **If you click on a group name, it opens a browser window (or tab) showing the group-reorder view so that you can easily move questions around.
 * 35) **All of the question attributes are also syntax highlighted.  This lets you set and see expressions within advanced question options (like basing the max/min number/sum of values on an expression)
 * 36) *The EM author used similar view (a little cleaner) to let his collaborating Epidemiologists and Institutional Review Board validate and authorize surveys with thousands of questions in highly branched and tailored structured interviews

How Backwards Compatible is EM with 1.91+?
LimeSurvey 1.92 is fully backwards-compatible with 1.91+ with one exception/caveat:  1.92 handles less-than / greater-than comparisons against empty values differently than 1.91+.

One of the LimeSurvey demo surveys uses a set of conditions that translates to this relevance equation:  {(age < 16) or (age == 20) or ... or (age == 80)}. In LimeSurvey 1.91+, (age < 16) is FALSE when there is no answer (the value is blank). However, in LimeSurey 1.92, (age < 16) is TRUE when there is no answer, since both PHP and JavaScript treat blank as 0 in mathematical comparisons. Thus, 1.91+ would hide that question when age was unanswered, but 1.92 would show it. We went to great pains to prevent this, but since we needed to have the Expressions generate identical results in PHP and JavaScript, there was no way to make 1.92 treat "" < 16 as FALSE. Fortunately, there is an easy work-around for this. If you want (age < 16) to be FALSE, then use this expression instead: {(!is_empty(age) and age < 16)}. You can use the new Survey Logic File view to quickly identify and fix any such comparisons in your survey.

=Getting Started=

The best way to get started with EM is to:
 * Install the latest stable version from http://www.limesurvey.org/en/download
 * Import and explore the sample surveys.
 * Explore the use cases and HowTos and step-by-step examples.
 * Explore the EM documentation (this page)
 * Examine the built-in EM test suite
 * From any survey, under tools, select the EM option
 * Available Functions lists the 70+ functions and syntax
 * Unit Tests of Isolated Expressions
 * shows examples of using all EM functions and operators, and the PHP and JavaScript results
 * note there are few functions that generate different results in the PHP and JavaScript versions, so this page lets you plan your EM logic accordingly.

=What Functionality does ExpressionScript Extend/Replace? (LimeSurvey <= 1.91+)=

Conditions => Relevance
Conditions controlled which  questions are visible. The general syntax was SGQA operator Value, like 111X2X3 == "Y". Conditions could be ANDed or ORed together, but mixing ANDs and ORs was difficult. The conditions themselves were stored in a separate table, and large portion of LimeSurvey's code was devoted to managing Conditions. Because of extensive database access, processing large numbers of conditions could cause noticable performance problems. Furthermore, once you had conditions assigned to questions or groups, you were often not allowed to re-order or delete them.

Assessments => Equations and  Micro-Tailoring
Assessments let users create scale scores from a collection of questions. However, they could not dynamically change on the current page, and their values were not stored to the database.

Replacements => Micro-Tailoring
Users could tailor some messages and questions based  upon prior responses. For example, a question might be, {TOKEN:FIRSTNAME}, you said {INSERTANS:111X3X4} was your favorite sport. However, it was not possible to do conditional tailoring (like say "Mr." or "Mrs." depending upon the person's gender), or conjugate verbs or decline nouns without fancy JavaScript. Authors could implement surveys that seemed to tailor questions, but it required separate questions for each permutation, and complex conditions to decide which questions to display.

Validation
Question could be validated with Regular expressions, or minimum/maximum values, or let an SGQA response serve as the minimum or maximum value. However, validations could not be based upon calculations of other variables without fancy JavaScript.

Equations
Equations were not supported without fancy JavaScript.

Equation Question Type
Equations could not be saved to the database (e.g. the final score for an assessment) without fancy JavaScript.

=How Will ExpressionScript Replace/Extend That Functionality?=

The ExpressionScript is a new core module within LimeSurvey that makes it much easier to support the type of complex functionality that used to require custom JavaScript. It is also replacing the way LimeSurvey currently manages Conditions and Assessments.

New Terminology When Referring to ExpressionScript (EM)
EM "thinks" of its functionality in the following terms:
 * Relevance-based Branching - if a question is relevant, then ask it, otherwise don't (e.g. make it invisible, and mark it as NULL in the database).  There is a new Relevance field for all Question types, and also for each Group (so you can apply a set of conditions to an entire group without having to copy the same condition to each question, and/or combine group and question-level conditional logic).
 * Tailoring - Once you know which questions should be asked, tailoring (sometimes called piping) specifies how the question should be asked. This lets you support not only simple subsitution (like {TOKEN:FIRSTNAME}), but also conjugation of verbs and declination of nouns based upon the gender or number of your subjects.  It also lets you change the message you deliver to a subject based upon whether they answered (or how they answered) other questions.
 * Equations - EM adds a new question type called Equation which stores the result of an Expression.  These equations results are computed and written to the database, even if you hide them on the page.  Thus, they are useful for hidden scoring calculations, navigation based upon complex equations, assessments, and reports that should be generated and easily available within the database.

Relevance and Cascading Relevance
Every question type now has a Relevance option which controls whether the question is displayed. EM processes each of the Relevance Equations in the order they should appear in the survey. If the expression is true (or missing - to support legacy surveys), the question will be displayed. If it is not relevant, then the question will be hidden, and the value will be NULLed in the database. If there are no relevant questions in a group, the entire group will be skipped.

Moreover, if any of the variables within an expression is irrelevant, then the expression always evaluates to false. This enables Cascading Relevance so that you do not have to write very long Relevance equations for each question.

Say you have 5 questions Q1-Q5, and you only want to show Q2 if Q1 was answered, and Q3 if Q2 was answered, etc.  The relevance equations might be:

The relevance calculations also work in JavaScript - so you could put all the above questions on one page and it would still work as expected. In fact, EM totally replaces how EM processes Survey vs. Group vs. Question-at-a-time survey formats. They now all use the exactly same navigation engine so they work identically regardless of survey style.

As long as you are on the same page, any data you entered will still be there, just hidden. So, if you enter some information, then choose an option that makes them irrelevant, then make them relevant again, your answers will still be available. However, as soon as you move to a different page, all irrelevant responses will be lost to integrity of the dataset.

Group-Level Relevance
ExpressionScript also supports group-level relevance. This makes it easier to implement looping. Say you want to collect information about up to 10 entities (such a products or people in a household), where you first determine how many entities need follow-up (such as by asking how many people live in a household, or having people check which products they like from a long list). After knowning how many entities need follow-up, you can use Group-level relevance like {count >= 1}, {count >=2}, ... {count >= 10} for each of the 10 groups of follow-up questions. Within each group, you can have question-level conditional logic (e.g. gender or age-specific follow-up questions for each subject). The question and group-level relevance equations are ANDed together to determine which should be shown.

Tailoring / Piping
Anything within curly braces is now treated as an Expression (with one exception described below). Expressions have acccess to all of the LimeReplacementFields, all of the variables (via several aliases), all typical equation operators (mathematical, logical, and comparison), and dozens of functions (that even work dynamically on the client-side).

Using these equations, you can do things such as:
 * 1) Conditionally show tailored messages to the respondants based upon prior responses
 * 2) Create Assessments and show Assessment results (or conditionally branch or show messages) based upon those results, all without using the Assessments module itself
 * 3) Conjugate verbs and decline nouns within questions, answers, and reports.
 * 4) Show summaries of responses before the "Show your answers" page at the end of the survey

Equations
There is a new question type called  Equation. It is like a Boilerplate questions, except that it stores the value of what is displayed in the database. So, if the Equation Question text contains an Assessment computation, that value would be stored in the database in a variable that can be displayed within public or private statistics.

This solves a common request for storing Assessment scores within the database

Syntax
Anything contained within curly braces is now considered an Expression (with one exception:  there must be no leading or trailing whitespace - this is needed to ensure the ExpressionScript does not try to process embedded JavaScript).

Note, it is OK for expressions to span multiple lines, as long as there is no whitespace after the opening  curly brace or before the closing curly brace. This is especially helpful for nested if statements like this:

ExpressionScript supports the following syntax:
 * All standard mathematical operators (e.g. +,-,*,/,!)
 * All standard comparison operators (e.g. <,<=,==,!=,>,>=, plus these equivalents:  lt,le,eq,ne,gt,ge)
 * Parentheses (so you can group sub-expressions)
 * Conditional operators (e.g. &&,| | and these equivalents: and,or)
 * Single and double-quoted strings (which can each embed strings with the other quote type)
 * Comma operator (so can have a list of expressions and just return the final result)
 * Assignment operator (=)
 * Pre-defined variables (to refer to questions, question attributes, and responses) - e.g. all of the SGQA codes
 * Pre-defined functions (there are already 70+, and it is easy to add more)

Operators
EM syntax follows normal operator precedence:

Note, for consistency between JavaScript and PHP, the plus operator (+) does addition if both operands are numeric, but does concatenation if both parts are non-numeric strings. However, we recommend using the join function for concatenation, as that makes your intent more clear, and avoids unexpected results if you were expecting strings but got numbers instead (or vice versa).

Caution about using Assignment Operator (=)
Note, you should avoid using the assignment operators unless absolutely necessary, since they may cause unexpected side-effects. For example, if you change the value of a previous response, the cascading relevance and validation logic between that question and the current question is not re-computed, so you could end up with internally inconsistent data (e.g. questions that stay answered but should have been NULLed, or questions that are skipped but should have been answered). In general, if you want to assign a value to a variable, you sould create an Equation question type, and use an expression to set its value. However, there are some rare times that people really need this operator, so we made it available.

To help caution you about this operator, it is shown in red font within the syntax equations (so that you don't confuse it with "==")

Using Assignment Operator
The main reasons you may want to use assignment are:
 * You need to set the default value for a question that does not accept defaults via equation (such as list radio, where the user interface lets you pick one of the answer options, but does not let you enter an equation).  However, be careful, as LimeSurvey will not be able to validate that your equation generates one of the allowable answers for that question.
 * You need to forcibly change the response to a previous question based upon a later response
 * etc...

You can use all expression manager system for this purpose. It's better to use an Equation question type to this purpose.

Some example:
 * Set answer to a short text question in lowercase :
 * Set an answer with condition :

XSS security
With XSS enable, some expression manager system can not be used :
 * starting a HTML tag in expression but ending in another expression
 * use a complex expression in URL.

Example and workaround
 * is broken with XSS security, here you can use
 * , here you can use an equation question because using a complete question code is OK :

Access to Variables
ExpressionScript provides read-only access to whichever variables we might need. For backwards compatibility, it provides access to the following:
 * TOKEN:xxx - the value of a TOKEN (e.g. TOKEN:FIRSTNAME, TOKEN:ATTRIBUTE_5) (Only for NOT anonymous survey).
 * |INSERTANS:SGQA - the display value of an answer (e.g. "Yes"). For ExpressionScript it's the same that using {QCODE.shown}.
 * All {XXX} values used by templates
 * In question text, you can use {QID} replaced by the question id and {SGQ} replaced by the SGQA of the question

In addition, ExpressionScript lets you refer to variables by the Question Code (the 'title' column in the questions table within the database). This is also the variable label used when you export your data to SPSS, R, or SAS. For example, if you have questions about name, age, and gender, you could call those variables name, age, and gender instead of 12345X13X22, 12345X13X23, and  12345X13X24. This makes equations easier for everyone to read and validate the logic, plus makes it possible to shuffle questions around without having to keep track of group or question numbers.

Important: It is only safe to refer to variables that occur in preceding pages or questions.

Furthermore, ExpressionScript lets you access many properties of the Question:

HTML editor issue
If you use HTML editor, some characters are replaced by HTML entities.
 * & by &amp;amp;
 * < by &amp;lt;
 * > by &amp;gt;

If you use HTML editor you need to use :
 * and for &
 * lt for <
 * le for <=
 * gt for >
 * ge for >=

Qcode Variable Naming
Here are the details of how to construct a Qcode (and access some properties) by question type. In general, Qcodes are constructed as:

QuestionCode. '_' . SubQuestionID. '_' . ScaleId

For comment and other, question code are QuestionCode_comment and QuestionCode_other

The reserved 'this', 'self', and 'that' variables
Quite often, you want to evalute all parts of a question, such as counting how many subquestions have been answered, or summing the scores. Other times, you want to process just certain rows or columns of a question (such as getting the row or column sums and storing them in the database). These reserved variables make that process relatively painless.

The 'this' variable is used exclusively within the "Whole question validation equation" and "Sub-question validation equation" advanced question options. It expands to the variable names of each of the cells within those questions. So, if you want to make sure that each entry is greater than three, you would set the "Sub-question validation equation" to (this > 3).

The 'self' and 'that' variable are more powerful, and serve as macros which are expanded prior to processing equations. The syntax choices are:
 * self
 * self.suffix
 * self.sub-selector
 * self.sub-selector.suffix

suffix is any of the normal qcode suffixes (e.g. NAOK, value, shown)

sub-selector is one of:
 * comments - only subquestions that are comments (e.g. from multiple choice with comment and list with comment)
 * nocomments - only subquestions that are not comments
 * sq_X - where X is a row or column identifier.  Only subquestions matching pattern X are selected. Note that search is done on complete code identifier, then sq_X match and include subquestions nX, X, Xn (e.g. if you use sq_1, subquestions a1, 1a, 1, 11 or 001 was included). Put attention at dual scale question type where subquestions code are QCODE_SQCODE_1 and QCODE_SQCODE_1 and to ranking question type where subquestions code are QCODE_1,QCODE_2 ....

Examples:
 * Has any part of a question been answered?  {count(self.NAOK)>0}
 * What is the assessment score for this question?  {sum(self.value)}

You can also use these to get row and column totals. Say you have a array of numbers with rows A-E and columns 1-5.
 * What is the grand total?  {sum(self.NAOK)}
 * What is the total of row B?  {sum(self.sq_B.NAOK)}
 * What is the total of column 3? {sum(self.sq_3.NAOK)}

The 'that' variable is like the 'self' variable, but lets you refer to other questions. Its syntax is:
 * that.qname
 * that.qname.suffix
 * that.qname.sub-selector
 * that.qname.sub-selector.suffix

qname is the question name without any subquestion extensions. So, say you create a question 'q1', that is its qname

Examples:
 * Has any part of question q1 been answered?  {count(that.q1.NAOK)>0}
 * What is the assessment score for q2?  {sum(that.q2.NAOK)}
 * What is the grand total of q3? {sum(that.q3.NAOK)}
 * What is the total of row C in q4?  {sum(that.q4.sq_C.NAOK)}
 * What is the total of column 2 in q4? {sum(that.q4.sq_2.NAOK)}

The 'self' and 'that' variables can be used in any relevance,  validation, or tailoring.

The one caveat is that when you use the Show Logic File feature, it will show you the expanded value of 'self' and 'that'. This lets you see the actual equation that will be generated so that you (and ExpressionScript) can validate that the variables exist. This may seem confusing since you may see quite lenghty equations. However, if you edit the question, you will see the original equation using 'self' and/or 'that'

Also note that you should not use these variables if (a) you want to explicitly name each variable used in an equation, or (b) use variables that do not have subquestions (e.g. single response questions). In those cases, prefixing a variable with 'that' is overkill, and you run the risk of getting unexpected results.

Access to Functions
ExpressionScript provides access to mathematical, string, and user-defined functions, as shown below. It has PHP and JavaScript equivalents for these functions so that they work identically on server-side (PHP) and client-side (JavaScript). It is easy to add new functions.

Implemented Functions
The following functions are currently available:

Functions that are Planned or Being Considered
Other functions that are planned (or being considered) but which are not implemented yet include the following. Some of these are for backwards compatability with another survey tool.

ExpressionScript Knows Which Variables are Local
In order to properly build the JavaScript for page, ExpressionScript needs to know which variables are set on the page, and what their JavaScript ID is (e.g. for document.getElementById(x)). It also must know which variables are set on other pages (so that it can ensure that the needed  fields are present and populated).

Cascading Conditions
If any of the variables are irrelevant, the whole equation will be irrelevant (false). For example, in the following table, N/A means that one of the variables was not relevant

Overriding Cascading Conditions
Say you want to show a running total of all relevant answers. You might try to use the equation {sum(q1,q2,q3,...,qN)}. However, this gets translated internally to LEMif(LEManyNA('q1','q2','q3',...,'qN'),'',sum(LEMval('q1'),LEMval('q2'),LEMval('q3'),...,LEMval('qN'))). So, if any of the values q1-qN are irrelevant, the equation will always return false. In this case, the sum will show 0 until all questions are answered.

To get around this, each variable can have a ".NAOK" suffix (meaning that Not Applicable is OK) added to it. In such cases, the following behavior occurs. Say you have a variable q1.NAOK
 * 1) q1 is not added to the LEManyNA clause
 * 2) LEMval('q1') will  continue to check whether the response is relevant, and will return '' if it is not (so individual irrelevant responses will be ignored, but they will not void the entire expression).

So, the solution to the running total problem is to use the equation sum(q1.NAOK,q2.NAOK,q3.NAOK,...,qN.NAOK).

The use of the .NAOK suffix also lets authors design surveys that have several possible paths but then converge on common paths later. For example, say subjects answer a survey in a way that is outside the normal range of responses. The author could alert the subjects that they may not get valid results, and ask them whether they really want to  continue with the survey. If they say Yes, then the rest of the questions will be shown. The condition for the "rest of the questions" would check whether the initial responses were answered within the normal range OR whether the subject said Yes to the question that is only relevant if they answered outside the normal range.

How does ExpressionScript Support Conditional Micro-Tailoring?
Here is an example of micro-tailoring (where Question Type=='expr' means an Equation):

All of these questions can be on a single page (e.g. in the same group), and only the relevant questions will display. Moreover, as you enter the ages of children, the sum expression in the last question will dynamically update on the page.

ExpressionScript provides this functionality by surrounding each expression with a named element. Every time a value changes, it recomputes the expression that should appear in that element and regenerates the display. You can have dozens, or even hundreds, of such tailored expressions on the same page, and the page will re-display all of them in a single screen refresh.

Mapping of LimeSurvey 1.91+ to ExpressionScript Functionality
=Syntax Highlighting=

To help with entering and validating expressions, EM provides syntax highlighting with the following features:

Types and Meanings of Syntax Highlighting
=Additional Reading=