Expression Manager to be updated
From LimeSurvey Manual
Quick start tutorial
Within LimeSurvey, you can further customize your surveys via the usage of the Expression Manager (EM). It can be used to specify the following:
- Navigation/Branching - allows a respondent's answers to change the order in which the questions are displayed;
- Tailoring/Piping - helps you phrase the question (such as referring to prior answers, or conjugating sentences based upon the age or gender of your subjects) or how to generate custom reports (like assessment scores or tailored advice);
- Validation - ensures that answers pass certain criteria, like min and max values or a certain input pattern.
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.
EM currently provides access to 70 functions and it can be easily extended to support more. It also lets you access your variables using human-readable variable names (rather than SGQA names).
The upcoming sections show the main places where the EM is used.
Some surveys use "Goto Logic", such that if you answer Question1 with option C, you are redirected to Question5. This approach is very limited since it is hard to validate it. Moreover, it easily breaks when you have to reorder questions. On the other hand, EM uses Boolean relevance equations to specify all the conditions under which a question might be valid. If the question is relevant, then the question is shown, otherwise it is not applicable, and the value "NULL" is stored in the database.
To better understand the relevance concept, let's focus on the following survey which computes the Body Mass Index (BMI) of survey respondents. To download it, click on the following link: Body Mass Index survey example.
The relevance equation is shown below in the Relevance column after the variable name. The relevance values of weight, weight_units, height, and height_units are all 1 (default value), meaning that those questions are always displayed. 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 getting a zero error). Also, question "Report" is only shown if the respondent answers all four main questions (height, heightunits, weight, weightunits).
Relevance is shown and editable when:
- you wish to view/edit question-level relevance
- you wish to view/edit group-level relevance
Viewing / Editing Question-Level Relevance
This equation computes the Body Mass Index (BMI). It is only asked if the person 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
Let's focus now on another example - simple census survey. To download it, click on the following link:Census survey example.
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 (it is shown for the second person cohabitating with you). Also, p2name, p2age. p2sum are displayed only if the question before each of them contains a a response.
So, the group also has question-level relevance criteria, such that some questions only appear if you answered certain questions before them (e.g., p2age is displayed if p2name has been answered). 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 screenshot 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 and weight in two different units (cms vs inches and kgs vs lbs):
Here weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject entered the weight using kilograms, use that value, otherwise multiply the entered value (pounds is the alternative) by 0.453592 to convert it to kilograms. The heightm variable uses a similar approach to compute the person's height in meters (height in cms/100), even if he entered his height in inches (1 meter=3.28084 inches).
BMI is computed as: {weightkg / (heightm * heightm)}.
Lastly, the report conditionally tailors the message for the subject, telling him what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")
In the above image, weightstatus 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:
- Tailoring must surround expressions with Curly Braces
- 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
The BMI report looks like this:
Here is the edit window for the same question.
Anything within curly braces is treated as an expression, being syntax-highlighted (color coded) in the prior image. If you have any typos (such as misspelled or undefined variable names or functions), EM would show an error. In our below example:
- heightunit.shown is an undefined variable name (it is actually heightunits.shown) 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 Expression Manager.
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 uses the new Expression Manager (EM) module which lets LimeSurvey support more complex branching, assessments, validation, and tailoring. It replaces 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 Expression Manager syntax and functionality.
Key Definitions
- Expression: Anything surrounded by curly braces
- As long as there is no white space immediately after the opening brace or before the closing curly brace
- The contents of Expressions are evaluted by EM, so they can contain mathematical formulas, functions, and complex string and date processing.
- Tailoring: Sometimes called "piping", this is the process of conditionally modifying text
- You have access to all 'replacement fields', participant data, and response data.
- You also have easier access to questions, answers, and their properties.
- Relevance Equation: A new question attribute controlling question visibility
- If there is a relevance equation, then the question is only shown if the relevance evaluates to true.
- Internally, all array_filter and array_filter_exclude commands become sub-question-level relevance
- Equation Question Type: A new question type that saves calculations or reports to the database
- It is like a Boilerplate question, but its contents are saved to the database even if you set "Always Hide this Question"
- Question Code: This is the preferred variable name for EM
- This can be a descriptive name indicating the purpose of the question, making it easier to read complex logic
- 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".
- This is what becomes the variable name if you export data to SPSS or R, so if you do statistical analysis, you need to create only unique question codes.
Do I have to use EM?
The short answer is " perhaps not". However, this heavily depends on the complexity of the survey you want to create.
For example, the Conditions editor covers some basic expressions that can be applied to the questions of your survey. However, the Conditions editor is limited. That is why the EM is used - it expands the realm of possibilities on how you can customize your survey.
Can I mix use of Conditions and Relevance?
Yes. You can use the Conditions editor for some questions and relevance equations for others.
You cannot have both conditions and expressions set up in the same question!. Once a condition is set up, it replaces whatever expression is written in the relevance equation field.
Yet, there is a way to use both expressions and conditions within a question. As mentioned above, a condition replaces the relevance equation field. Once done, check what is the newly created equation and copy it in a text editor. Delete the newly created condition from the Conditions editor and then edit the question, and add the desired expressions together with the conditions-based expression from your text editor file.
How should I choose between Conditions and Relevance?
Here is a list of pros and cons of each style:
Style | Pros | Cons | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Conditions | 1. Nice GUI for creating simple conditions 2. GUI well documented and understood by support team |
1. Only supports simple comparisons and does not "AND" and "OR" conditions well 2. Cascading conditions work erratically 3. Slow - database intensive -it can slow down long surveys 4. Some reported problems with reloading conditions 5. GUI doesn't scale well when there are dozens, hundreds, or thousands of questions 6. May be slow to convert paper-based surveys since it must use SGQA names 7. Often need a programmer to custom-code logic needed for complex branching | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Relevance | 1. Supports very complex logic, including 80+ functions and math/string operators 2. Perfect support for cascading logic 3. Fast - no extra database calls, supporting 1000+ question surveys 4. No problems with reloading logic since it does not require SGQA codes 5. Syntax-highlighting scales to 1000+ question surveys 6. Easy and fast to use for groups wanting to computerize existing paper-based suveys. 7. It easily supports semi-structured interviews and epidemiological surveys without the need of programmer |
}
For a more detailed explanation of the EM features, click on the following [[Useful futures enabled by EM|link[[.
Getting StartedThe best way to get started with EM is to:
What Functionality does Expression Manager Extend/Replace? (LimeSurvey <= 1.91+)Conditions => RelevanceConditions 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-TailoringAssessments 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-TailoringUsers 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. ValidationQuestion 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. EquationsEquations were not supported without fancy JavaScript. Equation Question TypeEquations could not be saved to the database (e.g. the final score for an assessment) without fancy JavaScript. How Will Expression Manager Replace/Extend That Functionality?The Expression Manager 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 Expression Manager (EM)EM "thinks" of its functionality in the following terms:
Relevance and Cascading RelevanceEvery 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 RelevanceExpression Manager 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 / PipingAnything 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:
EquationsThere 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 SyntaxAnything 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 Expression Manager 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: {if(is_empty(PFTotals),
'',
if(PFTotals >= -5 && PFTotals <= -4,
'Very Soft',
if(PFTotals >= -3 && PFTotals <= -2,
'Soft',
if(PFTotals == -1,
'Somewhat Soft',
if(PFTotals == 0,
'Moderate',
if(PFTotals == 1,
'Somewhat Hard',
if(PFTotals >= 2 && PFTotals <= 3,
'Hard',
if(PFTotals >= 4 && PFTotals <= 5,
'Very Hard',
''
)
)
)
)
)
)
)
)}
Expression Manager supports the following syntax:
OperatorsEM 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 should 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 OperatorThe main reasons you may want to use assignment are:
XSS securityWith XSS enable, some expression manager system can not be used :
Example and workaround
Access to VariablesExpression Manager provides read-only access to whichever variables we might need. For backwards compatibility, it provides access to the following:
In addition, Expression Manager 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, Expression Manager lets you access many properties of the Question:
HTML editor issue
If you use HTML editor, some characters are replaced by HTML entities.
If you use HTML editor you need to use :
Qcode Variable NamingHere 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' variablesQuite often, you want to evalute all parts of a question, such as counting how many sub-questions 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:
suffix is any of the normal qcode suffixes (e.g. NAOK, value, shown) sub-selector is one of:
Examples:
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.
The 'that' variable is like the 'self' variable, but lets you refer to other questions. Its syntax is:
qname is the question name without any sub-question extensions. So, say you create a question 'q1', that is its qname Examples:
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 Expression Manager) 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 sub-questions (e.g. single response questions). In those cases, prefixing a variable with 'that' is overkill, and you run the risk of getting unexpected results.
Usage of NAOKNAOK --> "Not Applicable" (NA) is alright (OK) Using NAOK, means that all or some of the variables are irrelevant (e.g. "Not Applicable" (NA) is alright (OK)). When you put some variable from question in any equation : if this question (or subquestion) is hidden by condition : this disable all equation. For example : count(Q1_SQ1,Q1_SQ2,Q1_SQ3,Q1_SQ4) give always an empty string if one subquestion of Q1 is filtered. To count the number of checked subquestion in such question can be count(Q1_SQ1.NAOK,Q1_SQ2.NAOK,Q1_SQ3.NAOK,Q1_SQ4.NAOK). If the sub question is hidden :Expression manager return an empty string. Without NAOK : if one question or one subquestion is hidden : Expression Manager return always an empty string, same to return false. The .shown always use the NAOK system (empty string if hidden) but if you need the code of the answer : it's always a good idea to add .NAOK after the question code. Except if you need it and know what you do. Another example and information is provided at Overriding Cascading Conditions
Access to FunctionsExpression Manager 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 FunctionsThe following functions are currently available:
Functions that are Planned or Being ConsideredOther 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.
Expression Manager Knows Which Variables are LocalIn order to properly build the JavaScript for page, Expression Manager 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 <input type='hidden' value='x'> fields are present and populated).
Cascading ConditionsIf 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 ConditionsSay 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.
How does Expression Manager Support Conditional Micro-Tailoring?Here is an example of micro-tailoring (where Question Type=='expr' means an Equation):
Mapping of LimeSurvey 1.91+ to Expression Manager Functionality
Syntax HighlightingTo help with entering and validating expressions, EM provides syntax highlighting with the following features:
Types and Meanings of Syntax Highlighting
Additional ReadingExpression Manager Sample SurveysUse Cases and HowTosStep-by-Step Examples.Reference for DevelopersRoadMap/Status/ToDo List</translate> |