Actions

ExpressionScript Engine - Quick start guide: Difference between revisions

From LimeSurvey Manual

(Updating to match new version of source page)
No edit summary
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<languages />
<languages />
 
<translate>
<!--T:64-->
__TOC__
__TOC__




=Quick start guide=
=Quick start guide= <!--T:65-->




Within [https://www.limesurvey.org LimeSurvey], you can further customize your surveys via the usage of the Expression Manager (EM).
<!--T:66-->
Sidenote: "EM will be renamed into ExpressionScript (ES). If you read ExpressionScript somewhere, it is just the new name of EM."
Within [https://www.limesurvey.org LimeSurvey], you can further customize your surveys via the usage of the ExpressionScript (short: ES).
Sidenote: "ExpressionScript was named Expression Manager (EM) in earlier version. If you read Expression Manager somewhere, it is just the old name of ES."


EM can be used to specify the following:
<!--T:67-->
ES can be used to specify the following:


<!--T:68-->
# '''Navigation/Branching''' - allows a respondent's answers to change the order in which the questions are displayed;
# '''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);
# '''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.
# '''Validation''' - ensures that answers pass certain criteria, like min and max values or a certain input pattern.


EM provides an intuitive way to specify the logic for each of those features. Nearly anything that you can write as a standard mathematical equation is a valid expression.  
<!--T:69-->
ES provides an intuitive way to specify 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 you  
<!--T:70-->
ES currently provides access to 70 functions and it can be easily extended to support more. It also lets you access you  
variables using human-readable variable names (rather than [[SGQA_identifier|SGQA names]]).
variables using human-readable variable names (rather than [[SGQA_identifier|SGQA names]]).


The upcoming sections show the main places where the EM is used.
<!--T:71-->
The upcoming sections show the main places where the ES is used.




==Relevance (Controlling Navigation/Branching)==
==Relevance (Controlling Navigation/Branching)== <!--T:72-->




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 [https://en.wikipedia.org/wiki/Boolean_algebra 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.  
<!--T:73-->
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, ES uses [https://en.wikipedia.org/wiki/Boolean_algebra 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.  


<div class='simplebox'> '''Note:''' This is similar to what can be done via the [[Setting conditions|Conditions editor]], but EM lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA identifiers).</div>
<!--T:74-->
<div class='simplebox'> '''Note:''' This is similar to what can be done via the [[Setting conditions|Conditions editor]], but ES lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA identifiers).</div>




<!--T:75-->
<hr width="50%" style="margin:auto">  
<hr width="50%" style="margin:auto">  




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: [[Media:EM_survey_-_Cohabs.zip|Body Mass Index survey example]].
<!--T:76-->
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: [[Media:LS3_em_tailoring.zip|Body Mass Index survey example]].


<!--T:77-->
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).
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).




<!--T:78-->
<center>[[File:tutorial1.jpg]]</center>
<center>[[File:tutorial1.jpg]]</center>




<!--T:79-->
<div class="simplebox"> [[File:help.png]] '''Note:''' The above image comes from the [[Show logic file|survey logic file]] which allows you to look for syntax errors before activating the survey.</div>
<div class="simplebox"> [[File:help.png]] '''Note:''' The above image comes from the [[Show logic file|survey logic file]] which allows you to look for syntax errors before activating the survey.</div>




<!--T:80-->
Relevance is shown and editable when:
Relevance is shown and editable when:
*you wish to view/edit question-level relevance
*you wish to view/edit question-level relevance
Line 51: Line 66:




===Viewing / Editing Question-Level Relevance===
===Viewing / Editing Question-Level Relevance=== <!--T:81-->




<!--T:82-->
This equation computes the Body Mass Index (BMI). It is only asked if the person enters their height and weight.
This equation computes the Body Mass Index (BMI). It is only asked if the person enters their height and weight.




<!--T:83-->
<center>[[File:tutorial2.jpg]]</center>
<center>[[File:tutorial2.jpg]]</center>




<!--T:84-->
This is the edit screen for the "BMI" question.
This is the edit screen for the "BMI" question.




<!--T:85-->
<center>[[File:tutorial3.jpg]]</center>
<center>[[File:tutorial3.jpg]]</center>




<!--T:86-->
Note that you do not use the curly braces when you enter a relevance equation.
Note that you do not use the curly braces when you enter a relevance equation.




===Viewing / Editing Group-Level Relevance===
===Viewing / Editing Group-Level Relevance=== <!--T:87-->




Let's focus now on another example - a simple census survey. To download it, click on the following link: [[Media:EM survey - Cohabs.zip|Census survey example]].  
<!--T:88-->
Let's focus now on another example - a simple census survey. To download it, click on the following link: [[Media:LS3_group_relevance.zip|Census survey example]].  


<!--T:89-->
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 response.
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 response.




<!--T:90-->
<center>[[File:tutorial4.jpg]]</center>
<center>[[File:tutorial4.jpg]]</center>




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 was 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.'''
<!--T:91-->
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 was answered). ES 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.'''


<!--T:92-->
Here is the screenshot for editing the group-level relevance of ''Cohabitant 2'':
Here is the screenshot for editing the group-level relevance of ''Cohabitant 2'':




<!--T:93-->
<center>[[File:tutorial5.jpg]]</center>
<center>[[File:tutorial5.jpg]]</center>




<!--T:94-->
Note that you do not use the curly braces when you enter a relevance equation.
Note that you do not use the curly braces when you enter a relevance equation.


 
==Tailoring/Piping== <!--T:95-->
==Tailoring/Piping==




EM lets you easily do simple and complex conditional tailoring. Sometimes you just need a simple substitution, like saying, "You said you purchased <nowiki>[</nowiki>Product]. What did you like best about it?". Sometimes you need conditional substitution like "<nowiki>[</nowiki>Mr./Mrs.] <nowiki>[</nowiki>LastName], would you be willing to complete our survey?". In this case, you want to use "Mr. or Mrs." based on 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.
<!--T:96-->
ES lets you easily do simple and complex conditional tailoring. Sometimes you just need a simple substitution, like saying, "You said you purchased <nowiki>[</nowiki>Product]. What did you like best about it?". Sometimes you need conditional substitution like "<nowiki>[</nowiki>Mr./Mrs.] <nowiki>[</nowiki>LastName], would you be willing to complete our survey?". In this case, you want to use "Mr. or Mrs." based on the person's gender. Other times you need even more complex substitution (such as based upon a mathematical computation). ES supports each of these types of tailoring/piping.




===Conditional Equations===
===Conditional Equations=== <!--T:97-->




<!--T:98-->
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):
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):




<!--T:99-->
<center>[[File:tutorial1.jpg]]</center>
<center>[[File:tutorial1.jpg]]</center>




<!--T:100-->
In this case, weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject enters 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 has entered his height in inches (1 meter=3.28084 inches).
In this case, weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject enters 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 has entered his height in inches (1 meter=3.28084 inches).


<!--T:101-->
BMI is computed as: {weightkg / (heightm * heightm)}.
BMI is computed as: {weightkg / (heightm * heightm)}.


<!--T:102-->
Lastly, the report conditionally tailors the message for the subject, telling her what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")
Lastly, the report conditionally tailors the message for the subject, telling her what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")


<!--T:103-->
In the below image, weightstatus uses nested "if()" statements to categorize the person as underweight to severely obese. You can see its equation by checking its logic:
In the below image, weightstatus uses nested "if()" statements to categorize the person as underweight to severely obese. You can see its equation by checking its logic:




<!--T:104-->
<center>[[File:tailoring8.jpg]]</center>
<center>[[File:tailoring8.jpg]]</center>




<!--T:105-->
From the edit window for this question, you can see two things:
From the edit window for this question, you can see two things:
#Tailoring must surround expressions with curly braces
#Tailoring must surround expressions with curly braces
Line 123: Line 158:




<!--T:106-->
<center>[[File:tailoring9.jpg]]</center>
<center>[[File:tailoring9.jpg]]</center>




===Tailored Questions, Answers, and Reports===
===Tailored Questions, Answers, and Reports=== <!--T:107-->




<!--T:108-->
<div class="simplebox">[[File:help.png]] '''Note:''' Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a <nowiki><span></nowiki> tag which is not valid inside select options.</div>
<div class="simplebox">[[File:help.png]] '''Note:''' Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a <nowiki><span></nowiki> tag which is not valid inside select options.</div>


<!--T:109-->
The BMI report looks like this:
The BMI report looks like this:




<!--T:110-->
<center>[[File:tailoring10.jpg]]</center>
<center>[[File:tailoring10.jpg]]</center>




<!--T:111-->
Here is the edit window for the same question.
Here is the edit window for the same question.




<!--T:112-->
<center>[[File:tailoring11.jpg]]</center>
<center>[[File:tailoring11.jpg]]</center>




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:
<!--T:113-->
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), ES would show an error. In our below example:
* heightunit.shown is an undefined variable name (it is actually heightunits.shown) and  
* heightunit.shown is an undefined variable name (it is actually heightunits.shown) and  
* "rnd()" is an undefined function (the proper function name is "round()").  
* "rnd()" is an undefined function (the proper function name is "round()").  


<!--T:114-->
In both cases, the errors are located within a red box to make it easier to spot and fix them.
In both cases, the errors are located within a red box to make it easier to spot and fix them.




<!--T:115-->
<center>[[File:tailoring12.jpg]]</center>
<center>[[File:tailoring12.jpg]]</center>




<!--T:116-->
You can also see that you can quickly create complex reports, such as a table of entered values or tailored advice.
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.
<!--T:117-->
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 by the ExpressionScript engine.




==Validation==
==Validation== <!--T:118-->




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. 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.
<!--T:119-->
ES 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. 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.


<!--T:120-->
In all of these cases, since the advanced question attribute is always considered an expression, you do not use curly braces when specifying it.
In all of these cases, since the advanced question attribute is always considered an expression, you do not use curly braces when specifying it.


The [[Expression Manager sample surveys|sample surveys]] page shows many working examples containing a variety of validation expressions.
<!--T:121-->
The [[ExpressionScript sample surveys|sample surveys]] page shows many working examples containing a variety of validation expressions.


=Expression Manager - presentation=
=ExpressionScript - presentation= <!--T:122-->




To find out more about the Expression Manager and how you can use different expressions to enhance your survey, please click on the following [https://manual.limesurvey.org/Expression_Manager_-_presentation link].
<!--T:123-->
To find out more about ExpressionScript and how you can use different expressions to enhance your survey, please click on the following [https://manual.limesurvey.org/ExpressionScript_-_Presentation link].
</translate>

Latest revision as of 17:13, 19 August 2020


Quick start guide

Within LimeSurvey, you can further customize your surveys via the usage of the ExpressionScript (short: ES). Sidenote: "ExpressionScript was named Expression Manager (EM) in earlier version. If you read Expression Manager somewhere, it is just the old name of ES."

ES can be used to specify the following:

  1. Navigation/Branching - allows a respondent's answers to change the order in which the questions are displayed;
  2. 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);
  3. Validation - ensures that answers pass certain criteria, like min and max values or a certain input pattern.

ES provides an intuitive way to specify the logic for each of those features. Nearly anything that you can write as a standard mathematical equation is a valid expression.

ES currently provides access to 70 functions and it can be easily extended to support more. It also lets you access you variables using human-readable variable names (rather than SGQA names).

The upcoming sections show the main places where the ES is used.


Relevance (Controlling Navigation/Branching)

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, ES 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.

Note: This is similar to what can be done via the Conditions editor, but ES lets you easily specify much more complex and powerful criteria (and lets you use the variable name rather than SGQA identifiers).




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).



Note: The above image comes from the survey logic file which allows you to look for syntax errors before activating the survey.


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 - a 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 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 was answered). ES 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 of Cohabitant 2:



Note that you do not use the curly braces when you enter a relevance equation.

Tailoring/Piping

ES lets you easily do simple and complex conditional tailoring. Sometimes you just need a 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 on the person's gender. Other times you need even more complex substitution (such as based upon a mathematical computation). ES 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):



In this case, weightkg is {if(weightunits == "kg", weight, weight * 0.453592)}. This "if()" function means that if the subject enters 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 has 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 her what he entered. (e.g., "You said you are 2 meters tall and weight 70 kg.")

In the below image, weightstatus uses nested "if()" statements to categorize the person as underweight to severely obese. You can see its equation by checking its logic:



From the edit window for this question, you can see two things:

  1. Tailoring must surround expressions with curly braces
  2. Expressions can span multiple lines if, as in this case, you want to make it easier to read the nested conditional logic.



Tailored Questions, Answers, and Reports

Note: Dynamic tailoring may not work if answer options are made available in select boxes on the same question page. This results from the fact that tailoring inserts a <span> tag which is not valid inside select options.

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), ES 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 located within 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 by the ExpressionScript engine.


Validation

ES 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. 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 attribute is always considered an expression, you do not use curly braces when specifying it.

The sample surveys page shows many working examples containing a variety of validation expressions.

ExpressionScript - presentation

To find out more about ExpressionScript and how you can use different expressions to enhance your survey, please click on the following link.