Actions

Expression Manager: Difference between revisions

From LimeSurvey Manual

No edit summary
(No difference)

Revision as of 18:59, 19 December 2011

Introduction

LimeSurvey 2.0 uses the new Expression Manager (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).

Expression Manager is now available in LimeSurvey version 1.92 and beyond.

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

Key Definitions

  1. 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.
  1. Tailoring: Sometimes called "piping", this is the process of conditionally modifying text
    • You have access to all 'replacement fields', TOKENs, and INSERTANS values
    • You also have easier access to questions, answers, and their properties.
  1. Relevance Equation:  A new question attribute controlling question visiblity
    • 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
  1. 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"
  1. SGQA:  This is how variables are named in LimeSurvey <= 1.91+
    • Stands for Survey-Group-Question-Answer
    • SGQA variable names look like 123X5X382X971, and may have sub-question suffixes.
    • These variable names are specific to the underlying S/Q/G/A database codes, so often need to be changed
  1. 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
    • Although not required to be unique in <= 1.91+, it must be unique if you want to use EM
    • 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 2.0 will convert those to the equivalent Relevance Equations.  As part of the upgrade, LimeSurvey 2.0 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:

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/OR conditions well
2. Cascading conditions work erratically
3. Slow - database intensive, so can slow down long surveys
4. Some reported problems with re-loading 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 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, so supports 1000+ question surveys
4. No problems with re-loading logic since 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. Easily supports semi-structured interviews and epidemiological surveys without needing a programmers
1. No GUI for simple conditions - use syntax-highlighting instead
2. New, so support teams have not mastered EM yet.

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:
    • You have access to all common mathematical operators and functions
    • You have access to 70+ mathematical, date, and string processing functions
    • It is fairly easy for developers to add new functions if users need them
  1. Storing Calculations to Database
    • You can now compute simple and complex calculations and/or scale scores AND have them stored in the database without needing JavaScript.
    • You use the Equation question type to accomplish this.
  1. Assessments
    • You can now create assessments or scale scores from any question type, not just the subset that used to be supported
    • You can use Tailoring to show running or total assessment scores anywhere needed - even on the same page
    • You have more control over the reports generated based upon those assessment scores
    • You can store assessment scores in the database without needing JavaScript
    • You can hide assessment scores without needing JavaScript or CSS
  1. Replacement Fields
    • Instead of using {INSERTANS:SGQA}, you can just use the Question Code - this makes it easier to read and validate.
    • This also avoids the common need to edit questions to change the SGQA code to make everything work.
  1. Tailoring - you can conditionally display text based upon other values
    • Use the appropriate title for a subject, like (e.g. "Hello [Mr./Mrs.] Smith")
    • Output gramatically correct sentences based when singular/plural matter:  (e.g. "You have 1 child" vs. "You have 2 chldren")
    • Appropriately conjugate verbs and decline nouns based upon subject's gender and plurality.
  1. New Variable Attributes - you can access the following to do your tailoring:
    • (no suffix) -  an alias for qcode.code
    • .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
    • .NAOK - same as .code, but can be part of calculations or lists even if irrelevant
    • .value - the assessment value for the question if it is relevant (otherwise blank), or the text value if it is not a coded question
    • .valueNAOK - same as .value, but can be part of calculations or lists even if irrelevant
    • .shown - the answer as displayed to the user (this is what {INSERTANS:xxx}  does)
    • .qid - the question ID
    • .gid - the group ID
    • .sgqa - the SGQA value for the question
    • .jsName - the correct javascript variable name for the question, regardless whether defined on this page or another
    • .gseq - the question sequence (starting from 0)
    • .gseq - the group sequence (starting from 0)
    • .mandatory - whether the question is mandatory (Y/N)
    • .question - the text of the question
    • .relevance - the relevance equation for the question
    • .grelevance - the relevance equation for the group
    • .relevanceStatus - whether or not the question is currently relevant (1 if true, 0 if false)
    • .type - the question type (the one character code)
  1. Dynamic On-Page Changes
    • 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
    • Questions are also dynamically tailored based upon responses on the page, so you can see running totals, tailored sentences and customized reports.
  1. New Data Entry Screen
    • In addition to using the current data-entry system, you can just use Survey-All-In-One.
    • 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
    • This can be critical if your data entry person needs to see the tailoring, which is also dynamic.
  1. Eliminates the need for most custom JavaScript
    • EM easily supports complicated computations, scoring, tailoring and conditional logic.
    • 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
    • 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.
    • EM also handles all regular-expression-based validation, so you can robustly combine preg and equation-based question attributes.
  1. Easy Re-ordering (or deleting) of Questions and Groups
    • 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.
    • 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.
    • 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).
  1. The Question/Group Navigation Index is always available and accurate
    • Prior to version 1.92, these indexes were not available if there were complex conditions
    • With EM, we can guarantee that they are accurate.
    • Subjects can even jump back, to a prior question, change the answer, then jump forward (or submit)
      • When jumping forwards, EM will re-validate all of the intervening questions/groups.
      • If any questions become irrelevant, they will be NULLed in the database so that your data is internally consistent
      • 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.
  1. Auto-conversion of Conditions to Relevance
    • When you upgrade your database, all existing surveys that have conditions will have relevance equations generated for them
    • Whenever you import a survey, relevance equations will be created as needed
    • Whenever you add, delete, or modify conditions, EM will generate the appropriate relevance equation.
  1. Convenient Syntax Highlighting
    • 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.
    • 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.
    • 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).
      • The list of answer choices uses this syntax:  'answers':{key:val, ... }.
      • key has the syntax 'scale~code' where scale is the answer scale (e.g. for dual scale), and code is the answer code.
      • 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)
      • 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).
  1. Easy review of entire survey logic and content
    • There is a new Show Survey Logic feature that lets you see everything about the survey (or group or question) on a single page.
    • It shows the Group, Question, Sub-Question, and Answer-level details for the selected scope (survey vs. group vs. question)
    • It also shows the relevance, sub-question-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.
    • Eveything is syntax-highlighted so that you can see potential syntax errors (like unbalanced parentheses or use of variables before they were declared)
    • The syntax-highligting supports rapid navigation and editing of the survey.
      • If you click on a variable name, it opens a browser window (or tab) that shows you that question and lets you edit it.
      • 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.
      • 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)
    • 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.

Will EM be in the 2.0 branch?

For those of you watching the development of 2.0, EM will be included in it too.  At present, the version of EM in 1.92 is considerably more robust and feature complete than the version in 2.0a.  However, as the Yii port progresses over the next several months, these new EM features will be ported to Yii and the 2.x branch.

Getting Started

The best way to get started with EM is to:

What Functionality does Expression Manager 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 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-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 diplayed. 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:

Question Code Relevance Question
Q1 1 What is your name?
Q2 Q1 {Q1}, how old are you?
Q3 Q2 So, you are {Q2} years old.  Are you married?
Q4 Q3 == "Y" {Q1}, how long have you been married?
Q5 Q4 How many children do you have, {Q1}?

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 and 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

Expression 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 deterimine 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 Expression Manager does not try to process embedded JavaScript).

Expression Manager 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 operators (=,+=,-=,*=,/=) - This will come in future versions -  no variables can currently have their values changed this way
  • 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)

EM syntax follows normal operator precedence:

Level Operator(s) Description
1 () parentheses for grouping or calling functions
2 ! - + unary operators: not, negation, unary-plus
3 * / times, divide
4 + - plus, minus
5 < <= > >= lt le gt ge relative comparisons
6 == != eq ne equality comparisons
7 and logical AND
8 or logical OR
9 , comma operator

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 you intent more clear, and avoids unexpected results if you were expecting strings but got numbers instead (or vice versa).

Access to Variables

Expression Manager 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)
  • INSERTANS:SGQA - the display value of an answer (e.g. "Yes")
  • All {XXX} values used by templates

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.

Furthermore, Expression Manager lets you access many properties of the Question:

Syntax Meaning Example Example Result
Qcode an alias for Qcode.code {join(',',name,gender)} 'Tom','M'
Qcode.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 {join(',',name.code,gender.code)} 'Tom','M'
Qcode.NAOK same as Qcode - see discussion of NAOK {gender.NAOK} 'M'
Qcode.value the assessment value for the question if it is relevant (otherwise blank), or the text value if it is not a coded question {gender.value} '1'
Qcode.valueNAOK same as Qcode.value - see discussion about NAOK {gender.valueNAOK} '1'
Qcode.shown the dispay value for the question {join(',',name.shown,gender.shown)} 'Tom','Male'
Qcode.question the text of the question {gender.question} 'What is your gender?'
Qcode.mandatory whether the question is mandatory (Y/N) {gender.mandatory} 'N'
Qcode.qid the internal question number (not the sequential number) {gender.qid} 337
Qcode.type the question type {gender.type} 'G'
Qcode.jsName the correct javascript name for the question, regardless whether declared on or off this page {gender.jsName} 'java1827X3X337'
Qcode.gid the internal group number (not the sequential number) {gender.gid} 3
Qcode.qseq the sequential number of the question, starting from 0 {gender.qseq} 5
Qcode.gseq the sequential number of the group, starting from 0 {gender.gseq} 1
Qcode.relevanceStatus whether the question is currently relevant (0 or 1) {gender.relevanceStatus) 1
Qcode.relevance the question-level relevance equation {gender.relevance} '!is_empty(name)'
Qcode.grelevance the  group-level relevance equation {gender.grelevance} 'num_children >= 5'
Qcode.sgqa the SGQA value for this question {gender.sgqa} '1827X3X337'

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

QuestionCode . '_' . AnswerID . '_' . ScaleId

Type Description Code SubQs Answer Options Scales Answer Code Answer Shown Relevance
5 5 Point Choice Radio-Buttons Q1 1-5 {Q1} {Q1.shown} {Q1==3}
B Array (10 Point Choice) Radio-Buttons Q2 L1-L6 1-10 {Q2_L2} {Q2_L2.shown} {Q2_L2==7}
A Array (5 Point Choice) Radio-Buttons Q3 1-5 1-5 {Q3_1} {Q3_1.shown} {Q3_1>=3}
1 Array (Flexible Labels) Dual Scale Q4 sq1-sq5 0:a1-a3 1:b1-b3 {Q4_sq1_0} {Q4_sq1_1.shown} {Q4_sq1_1=='b2'}
H Array (Flexible) - Column Format Q5 1-5 s,m,t {Q5_1} {Q5_1.shown} {Q5_1=='s'}
F Array (Flexible) - Row Format Q6 F1-F5 1-5 {Q6_F3} {Q6_F3.shown} {Q6_F3==4}
E Array (Increase/Same/Decrease) Radio-Buttons Q7 1-7 I,S,D {Q7_4} {Q7_4.shown} {Q7_4=='D'}
: Array (Multi Flexi) 1 To 10 Q8 ls1,todo,ls2 min,max,avg {Q8_ls1_max} {Q8_ls2_avg.shown} {Q8_ls2_min==7}
; Array (Multi Flexi) Text Q9 hp,st,sw 1st,2nd,3rd {Q9_hp_3rd} {Q9_hp_3rd.shown} {Q9_hp_3rd=='Peter'}
C Array (Yes/Uncertain/No) Radio-Buttons Q10 1-5 Y,N,U {Q10_1} {Q10_1.shown} {Q10_3=='Y'}
X Boilerplate Question Q11 {Q11.shown}
D Date Q12 {Q12} {Q12.shown}
* Equation Q13 {Q13} {Q13.shown} {Q13>5}
~124~ File Upload (records number of files uploaded) Q14 {Q14} {Q14>0}
G Gender Drop-Down List Q15 M,F {Q15} {Q15.shown} {Q15=='M'}
U Huge Free Text Q16 {Q16} {Q16.shown} {strlen(Q16)>100}
I Language Question Q17 {Q17} {Q17.shown} {Q17=='en'}
! List - Dropdown Q18 1-5 {Q18} {Q18.shown} {Q18==3}
L List Drop-Down/Radio-Button List Q19 A-Z {Q19} {Q19.shown} {Q19=='X'}
O List With Comment Drop-Down/Radio-Button List + Textarea Q20 A-F {Q20},{Q20_comment} {Q20.shown} {Q20=='B'}
T Long Free Text Q21 {Q21} {Q21.shown} {strstr(Q21,'hello')>0}
M Multiple Choice Checkbox Q22 A-F {Q22_E} {Q22_E.shown} {Q22_E=='Y'}
P Multiple Choice With Comments Checkbox + Text Q23 A-F {Q23_D}, {Q23_D_comment} {Q23_D.shown} {!is_empty(Q23)}
K Multiple Numerical Question Q24 self,mom,dad {Q24_self} {Q24_self.shown} {Q24_self>30}
Q Multiple Short Text Q25 A-F {Q25_B} {Q25_B.shown} {substr(Q25_B,1,1)=='Q')}
N Numerical Question Type Q26 {Q26} {Q26.shown} {Q26 > 30}
R Ranking Style Q27 1-4 {Q27_1} {Q27_1.shown} {Q27_1==3}
S Short Free Text Q28 {Q28} {Q28.shown} {Q28=='mine'}
Y Yes/No Radio-Buttons Q29 {Q29} {Q29.shown} {Q29=='Y'}

Access to Functions

Expression 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 Functions

The following functions are currently available:

||abs||Absolute value||number abs(number) ||acos||Arc cosine||number acos(number) ||addslashes||Quote string with slashes||string addslashes(string) ||asin||Arc sine||number asin(number) ||atan||Arc tangent||number atan(number) ||atan2||Arc tangent of two variables||number atan2(number, number) ||ceil||Round fractions up||number ceil(number) ||checkdate||Returns true(1) if it is a valid date in gregorian calendar||bool checkdate(month,day,year) ||cos||Cosine||number cos(number) ||date||Format a local date/time||string date(format [, timestamp=time()]) ||exp||Calculates the exponent of e||number exp(number) ||floor||Round fractions down||number floor(number) ||gmdate||Format a GMT date/time||string gmdate(format [, timestamp=time()]) ||html_entity_decode||Convert all HTML entities to their applicable characters (always uses ENT_QUOTES and UTF-8)||string html_entity_decode(string) ||htmlentities||Convert all applicable characters to HTML entities (always uses ENT_QUOTES and UTF-8)||string htmlentities(string) ||expr_mgr_htmlspecialchars||Convert special characters to HTML entities (always uses ENT_QUOTES and UTF-8)||string htmlspecialchars(string) ||expr_mgr_htmlspecialchars_decode||Convert special HTML entities back to characters (always uses ENT_QUOTES and UTF-8)||string htmlspecialchars_decode(string) ||idate||Format a local time/date as integer||string idate(string [, timestamp=time()]) ||implode||Join array elements with a string||string implode(glue,arg1,arg2,...,argN) ||intval||Get the integer value of a variable||int intval(number [, base=10]) ||is_bool||Finds out whether a variable is a boolean||bool is_bool(var) ||is_empty||Determine whether a variable is considered to be empty||bool is_empty(var) ||is_float||Finds whether the type of a variable is float||bool is_float(var) ||is_int||Find whether the type of a variable is integer||bool is_int(var) ||is_nan||Finds whether a value is not a number||bool is_nan(var) ||is_null||Finds whether a variable is NULL||bool is_null(var) ||is_numeric||Finds whether a variable is a number or a numeric string||bool is_numeric(var) ||is_string||Find whether the type of a variable is string||bool is_string(var) ||log||Natural logarithm||number log(number) ||ltrim||Strip whitespace (or other characters) from the beginning of a string||string ltrim(string [, charlist]) ||max||Find highest value||number max(arg1, arg2, ... argN) ||min||Find lowest value||number min(arg1, arg2, ... argN) ||mktime||Get UNIX timestamp for a date (each of the 6 arguments are optional)||number mktime([hour [, minute [, second [, month [, day [, year ]]]]]]) ||nl2br||Inserts HTML line breaks before all newlines in a string||string nl2br(string) ||number_format||Format a number with grouped thousands||string number_format(number) ||pow||Exponential expression||number pow(base, exp) ||quoted_printable_decode||Convert a quoted-printable string to an 8 bit string||string quoted_printable_decode(string) ||quoted_printable_encode||Convert a 8 bit string to a quoted-printable string||string quoted_printable_encode(string) ||quotemeta||Quote meta characters||string quotemeta(string) ||rand||Generate a random integer||int rand() OR int rand(min, max) ||round||Rounds a number to an optional precision||number round(val [, precision]) ||rtrim||Strip whitespace (or other characters) from the end of a string||string rtrim(string [, charlist]) ||sin||Sine||number sin(arg) ||sprintf||Return a formatted string||string sprintf(format, arg1, arg2, ... argN) ||sqrt||Square root||number sqrt(arg) ||str_pad||Pad a string to a certain length with another string||string str_pad(input, pad_length [, pad_string]) ||str_repeat||Repeat a string||string str_repeat(input, multiplier) ||str_replace||Replace all occurrences of the search string with the replacement string||string str_replace(search, replace, subject) ||strcasecmp||Binary safe case-insensitive string comparison||int strcasecmp(str1, str2) ||strcmp||Binary safe string comparison||int strcmp(str1, str2) ||strip_tags||Strip HTML and PHP tags from a string||string strip_tags(str, allowable_tags) ||stripos||Find position of first occurrence of a case-insensitive string||int stripos(haystack, needle [, offset=0]) ||stripslashes||Un-quotes a quoted string||string stripslashes(string) ||stristr||Case-insensitive strstr||string stristr(haystack, needle [, before_needle=false]) ||strlen||Get string length||int strlen(string) ||strpos||Find position of first occurrence of a string||int strpos(haystack, needle [ offset=0]) ||strrev||Reverse a string||string strrev(string) ||strstr||Find first occurrence of a string||string strstr(haystack, needle) ||strtolower||Make a string lowercase||string strtolower(string) ||strtoupper||Make a string uppercase||string strtoupper(string) ||substr||Return part of a string||string substr(string, start [, length]) ||tan||Tangent||number tan(arg) ||time||Return current UNIX timestamp||number time() ||trim||Strip whitespace (or other characters) from the beginning and end of a string||string trim(string [, charlist]) ||ucwords||Uppercase the first character of each word in a string||string ucwords(string)
Function Meaning Syntax
count count the number of answered (non-blank)questions in the list number count(arg1, arg12, ..., argN)
if Excel-style if(test,result_if_true,result_if_false) if(test,result_if_true,result_if_false)
list Return comma-separated list of values string list(arg1, arg2, ... argN)
pi Get value of pi number pi()
regexMatch compare a string to a regular expression bool regexMatch(pattern,input)
stddev Calculate the Sample Standard Deviation for the list of numbers number stddev(arg1, arg2, ... argN)
sum Calculate the sum of values in an array number sum(arg1, arg2, ... argN)

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.

Syntax Meaning Comments
e() returns the value of e
formatDate(X,PAT) return the string value of date X formatted according to Java data format pattern PAT
formatNumber(X,PAT) return the string value of number X formatted according to Java number format pattern PAT
getAnsOption(X) returns the text corresponding to the selected option for answer X this is the same as X.shown
getAnsOption(X,Y) returns the text corresponding to the option at index Y of node X
getRelevance(X) returns the relevance equation for question X
getStartTime() returns the date corresponding to the system time when the interview was started
getType(X) returns the string name of the datatype - e.g. *NA* if isNA()
gotoFirst() jumps to the first relevant set of questions - this violates the normal flow of the system
gotoNext() jumps to the next set of relevant questions - this violates the normal flow of the system
gotoPrevious() jumps to the previous set of relevant questions - this violates the normal flow of the system
isAsked(X) returns true if the answer is neither *NA*, *INVALID*, nor *UNASKED*
isInvalid(X) returns true if the answer is of type *INVALID*
isNA(X) returns true if the answer is of type *NA*
isNotUnderstood(X) returns true if the answer if of type *HUH*
isRefused(X) returns true if the answer is of type *REFUSED*
isSpecial(X) returns true if the answer is of type *UNASKED*, *NA*, *REFUSED*, *INVALID*, *UNKNOWN*, or *HUH*
isUnknown(X) returns true if the answer is of type *UNKNOWN*
jumpTo(X) jump to the group containing the named question -- this violates the normal flow of the system
jumpToFirstUnasked() jump to the first unasked question thus bypassing previous answered questions this violates the normal flow of the system
lastIndexOf(X,Y) returns the last index (base 0) of string Y in string X. Returns -1 if Y is not contained within X
list(X,...) a string containing a comma separated list of the positive values with "and" separating the last two
mean(X,...) returns the mean of a list of values
numAnsOptions(X) returns the number of answer options that question X has
orlist(X,...) a string containing a comma separated list of the positive values, with "or" separting the last two
parseDate(X,PAT) returns the date value of string X parsed with Java date format pattern PAT
parseNumber(X,PAT) returns the numerical value of string X parsed with Java number format pattern PAT
showAllResponsesExcept(questionList,attributeList,attributeTitleList) questionList = pipe-delimited list of question identifiers; attributeList = pipe-delimited list of attributes (like question#, title, text, type - so you can decide what to show); attributeTitleList = pipe-delimited list of table headers, so can internationalize the report.
showTheseResponses(questionList,attributeList,attributeTitleList) questionList = pipe-delimited list of question identifiers; attributeList = pipe-delimited list of attributes (like question#, title, text, type - so you can decide what to show); attributeTitleList = pipe-delimited list of table headers, so can internationalize the report.

Expression Manager Knows Which Variables are Local

In 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 Conditions

More specifically, 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

Operator Example a b Result
(unary) +a N/A false
! !a N/A false
== (or eq) a == b N/A 5 false
== (or eq) a == b N/A 0 false
== (or eq) a == b N/A N/A false
!= (or ne) a != b N/A 5 false
!= (or ne) a != b N/A N/A false
!= (or ne) a != b N/A 0 false
> (or gt) a > b N/A 5 false
>= (or ge) a >= b N/A 5 false
< (or lt) a < b N/A 5 false
<= (or le) a <= b N/A 5 false
and a and b N/A 5 false
and a and b N/A N/A false
or a or b N/A N/A false
or a or b N/A 5 false
a + b N/A 5 false
* a * b N/A 5 false
/ a / b 5 N/A false
() (a) N/A false
(exp) (a && b) N/A 5 false
(exp) op (exp) (b + b) > (a && b) N/A 5 false
function sum(a,b,b) N/A 5 false
function max(a,b) N/A 5 false
function min(a,b) N/A 5 false
function implode(', ',a,b,a,b) N/A 5 false
function if(a,a,b) N/A 5 false
function is_empty(a) N/A false
function is_empty(a) 0 (or blank) true
function !is_empty(a) N/A false

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 converage 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 Expression Manager Support Conditional Micro-Tailoring?

Here is an example of micro-tailoring (where Question Type=='expr' means an Equation):

Question Code Relevance Question Type Question
name 1 text What is your name?
age 1 text How old are you?
badage !is_empty(age) expr {(age<16) or (age>80)}
agestop badage message Sorry, {name}, you are too {if( (age<16),'young',if( (age>80),'old','middle-aged') ) } for this test.
kids !badage yesno Do you have children?
parents 1 expr {!badage && kids=='Y'}
numKids parents text How many children do you have?
kid1 parents && numKids >= 1 text How old is your first child?
kid2 parents && numKids >= 2 text How old is your second child?
kid3 parents && numKids >= 3 text How old is your third child?
kid4 parents && numKids >= 4 text How old is your fourth child?
kid5 parents && numKids >= 5 text How old is your fifth child?
sumage 1 expr {sum(kid1.NAOK,kid2.NAOK,kid3.NAOK,kid4.NAOK,kid5.NAOK)}
report parents yesno {name}, you said you are {age} and that you have {numKids}.  The sum of ages of your first {min(numKids,5)} kids is {sumage}

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.

Expression Manager 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 Expression Manager Functionality

Old Feature New Feature Comments
Conditions Relevance You can use very complex conditional equations, and access a broader range of variables
Assessments Equation Any assessment scores can be re-structured into an Equation. This both ensures that the score is written to the database, and also lets you see dynamic changes to the score value on the current page
Replacements Expression Manager The core engine takes the input string and treats everything within curly braces as an Expression - so it handles all historical replacements types.  To avoid messing up embedded JavaScript, Expression Manager only processes content between curly braces as long as (a)  there is no leading or trailing whitespace within the curly braces - e.g. {expr} is an expression, but { expr}, {expr }, and { expr } are not expressions.  Furthermore, Expression Manager does not process content within its own strings (e.g. {list('hi','there {braces}'} generates "hi there {braces}").  It also ignores escaped curly braces (e.g. \{this is not an expression\})
Validation Expression Manager The plan is to take the current min/max Question Attributes and have Expression Manager process them.  That way the min/max values can be expressions themselves

Syntax Highlighting

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

Types and Meanings of Syntax Highlighting

Color Sample Meaning Tooltip Comments
tan background Sample the whole equation none Anything within curly braces that is recognized as an equation (e.g. there is no leading or trailing whitepace) will be color-coded with a tan background to help distinguish it from surrounding text
blue bold text Sample function name meaning and allowable syntax function names, or things that should be functions since they are followed by an opening parenthesis, are presented in bold blue text.  Tooltips show the meaning and allowable syntax for the function.
grey text 'Sample' string none single and double-quoted strings are shown in grey text
bold maroon text Sample variable set on the same page, but after the current question [name or SGQA code]: question; value; answerList showing codes for each value Any variable that is set on the same page but after the current question is shown in bold maroon text.  This color coding should alert authors to possible errors in question sequencing; but it is allowed since some authors may want this sequence of variables a page, especially for dynamic reports.  The tooltip shows its name (if you used INSERTANS:xxx) or SGQA code (if you used the new naming system), the actual question, and its current value (or blank if not set).  If the question type expects responses from an enumerated value set, the mapping of the codes to display values is show.
bold cyan text Sample variable set on the same page, prior to the current question [name or SGQA code]: question; value; answerList showing codes for each value Any variable that is set on the same page and prior to the current question is shown in bold cyan text.  The tooltip shows its name (if you used INSERTANS:xxx) or SGQA code (if you used the new naming system), the actual question, and its current value (or blank if not set).  If the question type expects responses from an enumerated value set, the mapping of the codes to display values is show.
bold green text Sample variable set on a prior page [name or SGQA code]: question; value; answerList showing codes for each value Any variable that is set on a prior pageis shown in bold green text.  The tooltip shows its name (if you used INSERTANS:xxx) or SGQA code (if you used the new naming system), the actual question, and its current value (or blank if not set).  If the question type expects responses from an enumerated value set, the mapping of the codes to display values is show.
bold pink text Sample variable set on a later page [name or SGQA code]: question; value; answerList showing codes for each value Any variable that is set on a prior pageis shown in bold pink text.  These are erorrs since the variable is  being used before it is declareed.  The tooltip shows its name (if you used INSERTANS:xxx) or SGQA code (if you used the new naming system), the actual question, and its current value (or blank if not set).  If the question type expects responses from an enumerated value set, the mapping of the codes to display values is show.
bold tan text Sample a lime relacement value the value Lime Replacement Strings (like {TOKEN:xxx}, {PRIVACY_MESSAGE} are shown in bold tan text.  The tooltip shows the current value.
red text Sample assignment operators warning message If you use one of the assignment operators (=,+=,-=,*=,/=), that operator will be displayed in red text.  This is meant to help prevent accidental re-assignment of values when you really meant to check whether a == b instead of  setting the value of a = b.
normal black text Sample punctuation none All other punctionation within the expression is shown as normal black text.
red-boxed text a bold red line surrounds the error syntax error description of the error Any detected syntax errors are shown by surrounding the error with a red box.  The tooltip shows the error.  Examples include unmatched parentheses, use of undefined functions, passing the wrong number of arguments to functions, poorly structured expressions (e.g. missing operators between variables), trying to assign a new value to a read-only variable, trying to assign values to non-variables, or using unsupported syntax.  Note that the syntax error dectection system may only report one error in an expression even if there are multiple errors; however, if any errors are detected, at least one error will be shown.

Relationship to LimeSurvey Ideas / Feature Requests

Expression Manager provides complete or partial solutions to each of the following:

Sample Surveys

The main demo is in the <limesurvey>/docs/demosurveys/limesurvey2_sample_survey_english.lss

Additional Reading

Reference for Developers

RoadMap/Status/ToDo List