Difference between revisions of "Expression Manager sample surveys"

From LimeSurvey Manual

m (Randomly Ask A Specific Number Of Questions In A Group (a sub-set of the questions): only for All in one or Group by group)
(No difference)

Revision as of 14:43, 2 August 2017


The best way to learn the new features of LimeSurvey 1.92+, especially Expression Manager, is to play with working examples and modify them to your needs.

We have created a set of sample surveys to demonstrate (and test) all of the new and enhanced functionality.  These surveys are in the distribution's /docs/demosurveys folder.  This page provides an overview of the functionality they provide, plus a convenient means of downloading them.

Relevance, Tailoring and Equations


This survey shows some of the main new features available in 1.92 using Expression Manager.

EM Features Demonstrated

  1. Relevance - support for more powerful conditional logic than is available via the 1.91+ Conditions editor
  2. Piping / Tailoring - easier, more powerful ability to pipe or tailor answers and question metadata than the old {INSERTANS:xxx} syntax
  3. Dynamic Tailoring - note that reports are change on the page as you answer questions.  In 1.91+, tailoring could only occur on subsequent pages.
  4. Micro-Tailoring - conditional tailoring withing sentences and reports using if() statements
  5. Equations - a new question type that lets you do calculations and store the results in the database, even if the calculation is hidden (thus, this is more powerful and flexible than the 1.91+ Assessments functionality)
  6. Conditional Validation - validation criteria, such as the minimum allowable value, can be conditional - e.g. based upon equations.

Screen Shots

This example computes the Body Mass Index, a calculation of your weight and height to determine whether you are overweight.  Note that initially all you see are the four mandatory questions.


Once you enter your information (and you get to choose whether to use metric or non-metric units), you see a tailored report that summarizes what you entered, and tells you your weight status:


Here is a different example, using metric units to show that it changes dynamically. Note that internally, there are hidden Equation-type questions that convert the data to metric (if needed), and store the metric height, weight, BMI, and weight status in the database without needing custom JavaScript.


This next set of examples shows that you can use mathematical and other functions within tailored reports. This is what the page looks like before you enter any data.  There is conditional logic to show blank cells if no (or non-numeric) data is entered, rather than showing "NaN" or "Divide by Zero".


As numbers are entered, the on-page report changes to show the computation being performed, and its result.


There are many other examples in this sample survey, but here is one more screen shot for good measure.  This one shows one of the dozens of ways you can fill out the Dynamic Relevance page.  As the help text notes, try different ages, and especially illogical combinations of responses to see the amusing messages generated at the bottom.  Also note that if you say that you have other than one child, the message will say "I hope you enjoy playing with your X children", rather than saying "I hope you enjoy playing with your child", showing that  you can easily micro-tailor sentences to make them match the gender and/or number of your subjects. You can even easily conjugate verbs and decline nouns based upon gender and number.




Sample Census


This is a pretend census that asks how many people are in your household, asks tailored follow-up for each person (up to 5), and generates a simple report.

It demonstrates how group-level relevance can make it easier to implement a "loop" of questions. After creating the group for Person 1, I exported the group. Since I used qcode variable names like p1_name instead of the SGQA code, I could use a text editor to quickly edit and re-import the group several times (e.g. it took about 10 seconds to edit and re-import each repeating group, ensuring that all variables had unique variables names and that the group-level logic was correct).

This also shows how you can prevent the Finished message from appearing until the survey is truly finished (e.g. all needed groups are completed).

EM Features Demonstrated

  1. Group-level relevance - The follow-up groups (Person 1-5) only show for up to the number of co-habitants specified.
  2. Tailoring - The final report summarizes the demographic data for each co-habitant
  3. Progressive question display - Whether in group or all-in-one mode, subsequent questions only show as soon as the preceding ones are answered.

Screen Shots

The survey generates a tailored report showing demographics of the specified number of co-habitants:


If you switch to question-at-a-time mode, you see that the index tailors the questions.  Since the user said that the person is a woman, it asks "What is her name".  And since we answered "Mary", the next question says "How old is Mary"?




Cascading Array Filters


This survey is based off the design of a survey courtesy of Joy Lane Research, LLC. They identified a bug in 1.91+ which prevented proper use of cascading array filters.  In 1.91+, say you first pick several options from Q01 and Q02, then pick a favorite from Q04.  If you uncheck the favorite option in Q01, that option does not disappear from Q04.   We also discovered the 'other' option were not being handled correctly. This survey shows that LimeSurvey 1.92 with Expression Manager makes the cascading work correctly.   It also lets us pick an 'other' option and have its label carry through to subsequent questions.  This survey also shows that you can simultaneously use array_filter and array_filter_exclude (see question Q05).

Screen Shots

In the following, note that:

  • Q02 only shows the set of products checked in Q01 (by using array_filter)
  • Q02 also shows "Another product", the text entered into the "Other electronic" field in Q01
  • Q04 only shows products from Q02 that were checked (so the array filter cascades)
  • Q05 only shows products from Q02 that were not checked in Q02 (using a cascaded array_filter_exclude)

Cascading array filter.jpg

Here, note the new behavior of array_filter_exclude (compared to version 1.91+):

  • If the exclusive option is selected, it hides all of the other choices, rather than disabling them
  • The exclusive option cascades as expected (in 1.91+, you would still see questions Q04 and Q05)
  • The underlying values are not cleared (as they are in 1.91+), so, if you uncheck the exclusive option, you will see the first screen shot again.

Cascading array filter2.jpg



Piping/Tailoring Using All Question Types and Attributes


If you are confused about how to name your variables (whether using the SGQA naming from 1.91+, or the new Qcode naming from 1.92), you are not alone.  Although the main documentation describes how to compose Qcode variable names, nothing beats seeing it in a working demo.  This survey shows how you can access question attributes and responses using Expression Manager.

Caution - we recommend turning off the built-in HTML editor (or converting it to the pop-up mode) when editing this survey. It can take a minute or more for the HTML editor to render the large HTML reports in edit mode.


  • Examples of every question type
  • All questions types that can use "other" have it included so you can see how that affects variable naming
  • Default values for all question type that accepts defaults
  • Tailoring - On and Off-Page Reports showing all 16 available EM dot notation suffixes.
  • These reports show all of the currently entered data (so show you how you could generate your own printable reports for users in lieu of the print-answers screen)
  • Proper Qcode and SGQA naming of all variables

Screen Shots

This is a huge survey, so we chose not to include screen shots.  Instead, please download and play with it.



Traditional Validation, Re-envisioned


This demonstrates all of the types of validation that can use expressions, and how they affect each of the question types that support validation.  In many cases, the validation tips start hidden and only appear if a question fails validation criteria  (thus also disappearing once the question passes validation criteria again).  These validation types are available for a broader range of question types than in 1.91+.

Validation Types Demonstrated

  1. min_num_value_n - minimum value for an answer
  2. max_num_value_n - max value for an answer
  3. min_answers - minimum number of answers required
  4. max_answers maximum number of answers allowed
  5. multiflexible_min - minimum value allowed for an answer (for multiflexi numbers question type)
  6. multiflexible_max - maximum value allowed for an answer (for multiflexi numbers question type)
  7. min_num_value - minimum allowed sum across all answers for the question
  8. max_num_value - maximum allowed sum across all answers for the question
  9. equals_num_value - the sum across all answers for the question must equal this value
  10. validation - this is the regular expression validation for the question - it can apply to individual cells

Screen Shots

Using new CSS styles, each validation type shows up a separate tip.  If shown (they can be hidden via the hide_tip option), the default option is to show them as pink if the question will fail validation constraints, and green if it will pass them.


If the user submits a page with validation errors, the page will be re-shown, but this time the validation errors will be shown in red.


This color coding also applies to validation sums.


Validation can be applied to individual cells within an array, such as this example where regular expression validations ensure that each entry is a properly formatted US phone number including area code.




Validation Equations


Sometimes you need custom validation that cannot be achieved using the traditional min/max criteria.  Enter the new em_validation_q and em_validation_sq options that let you construct complex validation equations at the question and sub-question level, respectively.  Especially for the sub-question-level, we also introduce the "this" variable to make it easy to validate each cell in an array without needing to know its variable name.

EM Features Demonstrated

  1. em_validation_q - this is an equation that determines whether the whole question is valid
  2. em_validation_q_tip - this is the message to show if the question fails em_validation_q criteria
  3. em_validation_sq - this is the equation that determines whether each sub-question (array cell) is valid
  4. em_validation_sq_tip - this is the message to show if any of the sub-questions is invalid.

In general, when em_validation_sq is used, if any cell is invalid, the background color for that cell turns pink to indicate that there is an error.

Screen Shots


This question ensures that you enter the ages of your children in descending order by applying this validation  equation:

q1_sq1 >= q1_sq2 && q1_sq2 >= q1_sq3 && q1_sq3 >= q1_sq4


This question ensures that no more than 3 questions are answered on any given row by applying this validation equation

(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= 3) && (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= 3) && (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= 3)

You can also write this as follows, and LimeSurvey automatically converts it at run-time to the above-listed expression.

(sum(self.sq_A) <= 3) && (sum(self.sq_B) <= 3) && (sum(self.sq_C) <= 3)




Sub-Question Relevance


This shows how array_filter and validation criteria can interact.  For validations that apply to sums, only relevant (visible) values are considered.

EM Features Demonstrated

  1. array_filter
  2. array_filter_exclude
  3. min_num_value
  4. max_num_value
  5. equals_num_value

It also show dynamic reporting of the numbers of questions answered in the core questions.

Screen Shots

Changing the number of visible rows dynamically changes the sum.  Marking a sub-question as irrelevant doesn't clear its value. Rather, if it is irrelevant, it's values don't contribute to any equation.


Note what happens when I check the "Fifth sub-question" for the first question ("Which rows should appear below").  Note that the sum for the second "Enter some numbers" question is now 5. Even though sub-question 5 for that question still have the value 14 (e.g. if you un-check the fifth sub-question, you will see the value of 14 again), that value does not contribute to the sum since it is currently irrelevant.  All irrelevant data is cleared (NULLed in the database) on submit, but stays available on the page in case users want or need to change their minds about answers.




Using Comma as Radix Separator (Decimal Point)


In 1.91+, several of the question types were not able to use a comma as the radix separator, so countries that are used to using a comma to indicate the start of the decimal part of the number were forced to have their users use a period instead.  This has been fixed for all question types in 1.92+

In addition, EM now ensures that only valid numbers can be entered into numeric fields.  In 1.91+, you could enter numbers  like "1.2.3" or "-1.2-1", since it only checked that you were entering a number, decimal point, or negative sign.  In 1.92+, if you enter an invalid number, the field will be cleared and you have to re-enter the number.

Note that the numeric values  are always converted to using a period as the radix separator within the database.  That way statistical analyses will work appropriately.

Question Types Using Radix Separator

  1. Numerical input [N]
  2. Multiple numerical input [K]
  3. Array (Numbers) [:]
  4. Array (Texts) [;], when using the numbers_only advanced question option
  5. List (radio) [L], when using the other_numbers_only advanced question option
  6. Short free text [S], when using the numbers_only advanced question option
  7. Multiple short text [Q], when using the numbers_only advanced question option
  8. Multiple choice [M], when using the other_numbers_only advanced question option
  9. Multiple choice with comments [P], when using the other_numbers_only advanced question option

Screen Shots

Note that the following can use comma as the radix separator

  • validation messages (e.g. "Each answer must be between 1,5 and 8,2")
  • totals (e.g. for the second question)


However, although you can now use commas for data entry into the Array (Texts) question, the row and column totals (and grand total) all display their values using a period as the radix separator.




Randomization Groups


This survey demonstrates how to use the random_group advanced question option in 1.92+.

Each time you start the survey, the question order is randomized.  However, once the survey is started, the randomization order remains fixed, even if you change languages.

Screen Shots

Here is the randomization paradigm.  The first random question on the page will either be Q1,Q4, or Q7. The second randomized question on the page will either be Q2, Q5, or Q8.


Here is the randomization generated the first time I tested this survey.


A different randomization was generated the second time I tested the survey.


But, when I switched to French (without re-starting the survey), the randomization order remained intact.




Randomly Ask One Question Per Group


This survey shows how you can configure a survey to randomly display one question per group.  In it, there are 5 groups of 6 questions each.  At the outset, in Group 0, five hidden Equation questions, called ask1-ask5, are populated.  Each one has the value of {floor(rand(1,6.9999))} in the question text field, which means that the variables ask1-ask5 will each have a value between 1 and 6.  Then, each question in the group has a relevance equation like "ask1==N" where N is the Nth question in the group (so the third question in group 1 has the relevance equation  "ask1==3").

This survey works equally well in survey-at-a-time, group-by-group, and question-by-question modes.  Since the randomization is set in the first group, and that group is effectively hidden (since all of the ask1-ask5 questions are hidden), the randomization stays the same for the subject; but each different subject will have a distinct randomization.

Features Demonstrated

  1. Equation question type
  2. Randomization functions
  3. Conditional (if) function

Screen Shots

This is Group0, which uses the Equation question type to select random values from 1-6 for each group (except the last group, which only has 4 questions).  Note that the if() function first checks whether ask1 has already been set, and if so, uses that value. If the value hasn't been set, then it uses a random value to set the value of ask1.


This Group shows how the variable (ask4) from Group0 is used to control which question is visible within the fourth group.




Randomly Ask A Specific Number Of Questions In A Group (a sub-set of the questions)

  This is only for all random question in one group and for Group by group or All in one format


This survey shows how to ask a random sub-set of questions in a group. For example, show a random 5 out of 10 questions in a group.

The survey has one group containing 10 questions. All questions are assigned the same randomization group name so will be displayed in a random order on page load. Each question is given a relevance equation that the sum of the "relevanceStatus" of all other questions in the group is less than the number of questions you want to show. Since relevanceStatus is assigned as the questions are rendered, this effectively totals the number of preceding questions.

So, in our 5 out of 10 example, the equation for Q1 would be:

sum(Q2.relevanceStatus, Q3.relevanceStatus, Q4.relevanceStatus, Q5.relevanceStatus, Q6.relevanceStatus, Q7.relevanceStatus, Q8.relevanceStatus, Q9.relevanceStatus, Q10.relevanceStatus) LT 5

For Q2, it would be

sum(Q1.relevanceStatus, Q3.relevanceStatus, Q4.relevanceStatus, Q5.relevanceStatus, Q6.relevanceStatus, Q7.relevanceStatus, Q8.relevanceStatus, Q9.relevanceStatus, Q10.relevanceStatus) LT 5

And so on...

Features Demonstrated

  1. relevanceStatus variable
  2. Randomizing



Rating User-Entered List of  Products


This example shows how you can ask users to list a set of products that interest them, and then have them rate those products.

EM Features Demonstrated

  1. Tailoring answers - when  rating products, the labels come from the comment field of the preceding multiple choice question
  2. Tailoring scales - many advanced questions options, like scale headings, can also be tailored.

Screen Shots

This image shows that even though only products 1,3, and 5 were selected, only those 3 are displayed (using array_filter). Moreover, the row labels in the second question is either the Product number (if nothing is entered into the comment field), or the contents of the comment field.

Mcc filter1.jpg

This image shows how the tailored answers are specified.

Mcc filter2.jpg

Here is the Show Logic File for that question, which lets us verify that the conditional logic will work as desired (e.g. there are no syntax errors)

Mcc filter3.jpg

This image shows a group in which you specify a 5 point rating scale (Options 1-5), the title of the scale, the the products you want to rate.  The final question shows that each of the parts of the array question can be tailored.

Mcc filter4.jpg

This image shows the logic file view of the last question.  As you can see, the sub-questions, answers, and scale headers can be tailored.

Mcc filter5.jpg