Expression Manager/fr

Remarque: Cette fonction est uniquement disponible dans LimeSurvey 1.92 ou plus tard.

=Démarrage rapide=

Vue d'ensemble
Quand vous paramétrez vos enquêtes, vous avez souvent besoin de pouvoir spécifier:
 * 1) Navigation/branchement - permet de modifier l'ordre dans lequel sont posées les questions en fonction de la réponse du sondé
 * 2) Peaufiner - comment poser la question (en se référant aux réponses précédentes, ou en conjuguant les phrases de votre question en fonction du nombre ou du genre de votre sondé), ou comment générer des rapports personnalisés (comme des évaluations ou des conseils sur mesure).
 * 3) Validation - être sûr que la question valide certains critères, comme des valeurs min et max, ou corresponde à un motif précis.

L'ExpressionScript (EM) procure une façon intuitive de spécifier la logique de toutes ces fonctionnalités. Presque tout ce que vous pouvez écrire comme équation mathématique standard est une expression valide, même en appelant des fonctions. EM procure l'accès à 70 fonctions et peut facilement être étendu pour en supporter d'avantage. Il vous permet également d’accéder à vos variables avec des noms de variables lisibles (plutôt qu'avec la notation SGQA).

Les sections suivantes montrent quelles sont les principales circonstances où l'ExpressionScript (EM) est utilisé

Filtre conditionnel (Contrôle de navigation/branchement)
Certaines enquêtes utilisent une logique "aller à" ("Goto Logic"), par exemple si vous répondez à la Question1 avec la réponse C, alors sautez à la Question5. Cette approche est très limitée, car il est très difficile de valider et de maintenir la cohérence si vous avez à ré-ordonner les questions. EM utilise une équation booléenne pour spécifier toutes les conditions sous lesquelles une question peut être valide. Si la question est pertinente, alors la question est posée, sinon elle n'est pas applicable et la valeur NULL est stockée dans la base. C'est similaire à ce qui peut être fait par l'intermédiaire de l'éditeur de conditions, mais EM vous permet de spécifier facilement des critères bien plus complexes et puissants (et vous permet d'utiliser des noms de variables plutôt que la notation SGQA)

Cette image montre une façon d'appréhender la logique de la pertinence pour un questionnaire. Il calcule l'Indice de Masse Corporelle. L'équation de la pertinence est indiquée dans les crochets juste après le nom de la variable (qui est en vert). Donc, la pertinence du poids (weight), des unités de poids (weight_units), de la taille (height) et des unités de taille (height_units) sont toutes à 1, cela signifie que ces questions sont toujours posées. Cependant, la pertinence pour l'IMC est {!is_empty(height) and !is_empty(weight)}, ce qui veut dire que l'IMC sera calculé seulement si le sujet entre une valeur à la fois pour la taille et le poids (évitant ainsi le risque de division par 0). De même la question de rapport est montré seulement si le sujet répond aux quatre questions (taille, unité de taille, poids, unités de poids).



La pertinence est affichée et modifiable aux endroits suivants :

Voir / Modifier l'équation de filtre conditionnel au niveau de la question
Cette équation calcule l'Indice de Masse Corporelle (IMC). Elle est posée uniquement si la personne entre d'abord sa taille et son poid



Ceci est l'écran d'édition pour la question sur l'IMC



Notez que vous n'avez pas à utiliser les parenthèses quand vous entrez une équation de pertinence

Voir / Modifier l'équation de filtre conditionnel au niveau du groupe
Ceci est une exemple de sondage de recensement ; La première page demande combien de personnes vivent avec vous et l'enregistre dans la variable "cohabs". Cette page est affichée seulement si vous avez plus d'un co-habitant (elle est donc affichée pour la deuxième personne qui cohabite avec vous), et également elle est affichée seulement si vous avez spécifié quelle est votre relation avec la première personne (p1_rel).



Comme vous pouvez le voir, le groupe aussi possède des critères de pertinence du niveau question, de telle façon que chaque question n’apparaît que lorsque vous avez répondu à la question précédente (e.g.{!is_empty(p1_sex)}). EM combine pour vous les équations de pertinence du niveau Groupe et Question pour vous. Les questions dans un groupe sont posées uniquement si le groupe dans son ensemble est pertinent. Ensuite, seul le sous-ensemble de questions du groupe qui sont pertinentes seront effectivement posées.

Voici l'écran pour éditer la pertinence de niveau groupe pour cette question.



Notez que vous n'avez pas à utiliser les parenthèses quand vous entrez une équation de pertinence

Modification conditionnelle
EM vous permet facilement de déterminer des conditions simples et complexes pour vos questions. Parfois vous avez juste besoin d'une simple substitution, comme quand vous dites, "Vous nous avez dit que vous aviez acheté [Produit]. Qu'avez vous particulièrement apprécié en lui ?". Parfois vous avez besoin d'une substitution conditionnelle comme "[M/Mme] [Nom], seriez vous prêt à remplir notre sondage ?. Dans ce cas vous voulez utiliser M ou Mme en fonction du sexe de la personne. D'autres fois vous avez besoin de substitutions encore plus complexes (basées par exemple sur un calcul mathématique). Em supporte tous ces types de customisations.

Equations Conditionnelles
L'exemple de l'Indice de Masse Corporelle montre la capacité à calculer l'IMC d'une personne même en la laissant entrer sa taille et son poids dans des unités métriques ou non métriques.



Ici weight_kg est {if(weight_units == 'kg', weight, weight * .453592)}. La fonction if signifie que si le sujet a entré son poids en utilisant les kilogrammes alors cette valeur est utilisée, sinon la valeur entrée (qui est donc en livres) est multipliée par 0.453592 pour la convertir en kilogrammes. La variable height_m utilise une approche similaire pour calculer la taille de la personne en mètres, même si elle a été entrée en pouces.

IMC calcule la formule du poids comme {weight_kg / (height_m * height_m)}.

Enfin le rapport personnalise le message en rappelant à l'utilisateur ce qu'il a tapé. (Vous avez affirmé que vous mesurez 2 m et que vous pesez 70 kg)

Bien que n'étant pas bien illustré dans l'image ci-dessus, weight_status utilise les conditions si imbriquées pour classer le poids de la personne de l'insuffisance pondérale à l'obésité sévère. Vous pouvez voir cette équation dans la vue "Montrer la logique"



Pour cette question vous pouvez voir deux choses dans la fenêtre d'édition :
 * 1) les expressions de conditions prennent des parenthèses
 * 2) les expressions peuvent s'étendre sur plusieurs lignes si, comme dans ce cas vous souhaitez rendre les conditions imbriquées plus faciles à lire



Questions adaptées, réponses et rapports
Remarque : l'adaptation dynamique peut ne pas fonctionner si les options de réponse sont disponibles dans des cases à cocher sur la même question. Ceci résulte du fait que l'adaptation insère une balise  qui n'est pas valide à l'intérieur de la sélection d'options.

Cet exemple montre le rapport d'IMC



Ici, la fenêtre d'édition pour la même question



Comme vous pouvez le voir, rien de ce qui est entre parenthèses n'est considéré comme une expression, et donc rien ne présente de surbrillance en fonction de la syntaxe (code de couleur). En cas de fautes de frappe (noms de variables ou fonctions mal orthographiés ou inconnus), EM montrerait une erreur, comme cela, en montrant que height_unit est un nom de variable non défini (c'est en fait height_units), et que rnd est une fonction non définie (le bon nom de la fonction est ronde). Dans les deux cas, les erreurs seraient entourées par une zone rouge pour les rendre plus facile à déceler et à corriger.



Vous pouvez également voir que vous pouvez rapidement créer des rapports complexes, comme une table des valeurs saisies ou des conseils personnalisés.

Rappelons que toutes les conditions d'adaptation doivent être entourées avec des parenthèses, ainsi LimeSurvey sait quelles sont les parties de la question en texte libre et lesquelles doivent être analysées à travers le gestionnaire d'expressions.

Validation
EM contrôle la façon dont la plupart des options avancées de question fonctionnent. Ceci contrôle des variables comme nombre de réponses min/max ; valeurs individuelles min/max ; somme des valeurs min/max; et vérifie que les valeurs entrées correspondent aux chaînes modèles spécifiées. Vous continuez à saisir ces options avancées de question comme d'habitude. Cependant, maintenant, n'importe quelle valeur dans l'un de ces champs est considérée comme une expression, de sorte que vous pouvez avoir des critères min/max avec des relations conditionnelles complexes avec d'autres questions.

Dans tous ces cas, puisque l'option avancée de question est toujours considérée comme une expression, vous n'avez pas à utiliser des parenthèses lors de la spécification.

Les pages Enquêtes-Échantillon illustrent par de nombreux exemples pratiques l'utilisation d'expressions pour les validations.

=Introduction=

LimeSurvey 1.92 et suivants utilise le nouveau module gestionnaire d'expressions (ExpressionScript : EM) qui permet à LimeSurvey de gérer des critères plus complexes d'évaluation, de branchement, de personnalisation. Il remplace la gestion en arrière-plan des conditions, des remplacements et des évaluations. Il permet également d'accélérer considérablement le traitement, car il élimine la plupart des lectures de base de données lors de l'exécution. EM a été développé par le Dr Thomas White (TMSWhite).

Cette page du wiki est la référence définitive pour la syntaxe et les fonctionnalités du gestionnaire d'expression (ExpressionScript).

Définitions clés

 * 1) "'L'expression"': Tout ce qui a entouré par parenthèses
 * 2) *Tant qu'il n'y a pas d'espace blanc immédiatement après la parenthèse d'ouverture ou avant la parenthèse fermante
 * 3) *Le contenu des expressions est évalué par EM, de sorte qu'elles peuvent contenir des formules mathématiques, des fonctions, des chaînes complexes et des transformations de dates.
 * 4) "'Le peaufinage"': parfois appelé "la tuyauterie", c'est le processus de modification conditionnelle de texte (piping, tayloring)
 * 5) *Vous avez accès à toutes les valeurs de "remplacement de champs", des jetons, et d'insertion (INSERTANS)
 * 6) *Vous avez également un accès plus facile à des questions, des réponses, et de leurs propriétés.
 * 7) "Équation de pertinence" : un nouvel attribut de contrôle de visibilté de la question
 * 8) *Si il y a une équation de pertinence, alors la question ne s'affiche que si la pertinence est évaluée à vrai.
 * 9) *En interne, toutes les commandes array_filter et array_filter_exclude relèvent mainteant du niveau sous-question
 * 10) "'Le type de question équation"' : Un nouveau type de question qui permet d'économiser des calculs ou des rapports de la base de données
 * 11) *C'est comme une question standard, mais son contenu est enregistré dans la base de données, même si vous réglez "Toujours Masquer cette Question"
 * 12) "'SGQA"": C'est la manière dont les variables sont nommées dans LimeSurvey <= 1.91+
 * 13) *Signifie Survey-Groupe-Question-Réponse
 * 14) *SGQA les noms de variables ressemblent à 123X5X382X971, et peuvent avoir des suffixes pour les sous-questions.
 * 15) *Les noms de ces variables sont spécifiques à la base de données de codes sous-jacente S/Q/G/U, donc ont souvent besoin d'être changés
 * 16) "'Code de question": C'est la meilleure variable pour EM
 * 17) *Ce peut être un nom descriptif indiquant l'objet de la question, afin de faciliter la lecture dans une logique complexe
 * 18) *Bien que non requis pour être unique dans LS <= 1.91+, il doit être unique si vous souhaitez utiliser EM
 * 19) *Les codes de question valides ne doivent PAS commencer par un chiffre, de sorte que lors de l'utilisation du code de question pour numéroter vos questions, il suffit d'utiliser "t1", ou "q1a" ou "g1q2".
 * 20) *C'est ce qui devient le nom de la variable si vous exportez des données dans SPSS ou de R, donc si vous faites de l'analyse statistique, vous les avez probablement déjà rendus uniques.

Dois-je utiliser EM le gestionnaire d'équations ?
La Réponse courte est non (mais aussi oui).

EM est entièrement rétro-compatible avec les questionnaires existants. Donc, si vous êtes satisfait de la façon dont LimeSurvey traitait les conditions d'utilisation et les évaluations dans ses versions <= 1.91+, vous pouvez continuer à le faire.

Cependant, EM remplace complètement la façon interne dont LimeSurvey traite les conditions. Qouique vous puissiez toujours utiliser l'Éditeur de Conditions pour créer et gérer des conditions, LimeSurvey 1.92 va convertir l'équivalent de la Pertinence des Équations. Dans le cadre de la mise à niveau, LimeSurvey 1.92 va auto-convertir toutes les conditions en pertinence d'équations.

Cela devrait vous donner le meilleur des deux mondes - vous pouvez continuer à utiliser LimeSurvey comme vous en avez l'habitude mais allez regarder la pertinence d'équation équivalente, de sorte que vous pouvez migrer progressivement à lapertinence d'équations directement chaque fois que vous voyez l'ajustement.

Puis-je mélanger conditions et pertinence d'utilisation ?
Oui. Vous pouvez utiliser l'éditeur de conditions pour certaines questions, et l'éditeur de pertinence pour les autres. Les Conditions sont converties en pertinence lorsque vous enregistrez la question.

Remarque, nous supposons que si vous utilisez l'éditeur de conditions, c'est que vous voulez modifier une équation de pertinence saisie manuellement. Donc, si vous avez des conditions existantes et que souhaitez modifier manuellement la pertinence, s'il vous plaît supprimez d'abord les conditions pour cette question. Plus précisément, copiez la pertinence d'équation générée dans un éditeur de texte, utilisez le menu des conditions pour supprimer toutes les conditions de la question (ce qui va également supprimer la pertinence), puis éditez la question et collez l'équation de pertinence générée avec l'éditeur de texte dans le champ de pertinence pour la question (et enregistrez la question). Si il y a suffisamment de demandes pour la suppression des conditions sans la suppression de l'équation de pertinence générée, nous ajouterons un bloc de processus de conversion.

Comment devrais-je choisir entre les conditions et la pertinence ?
Voici une liste des avantages et des inconvénients de chaque méthode :

En résumé, si vous êtes satisfait de la façon dont LimeSurvey 1.91+ fonctionne, il n'y a pas de raison de changer ce que vous faites.

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

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

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

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

=Getting Started=

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

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

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

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

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

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

Equations
Equations were not supported without fancy JavaScript.

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

=How Will ExpressionScript Replace/Extend That Functionality?=

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Operators
EM syntax follows normal operator precedence:

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

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

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

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

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

Some example:
 * Set answer to a short text question in lowercase :
 * Set a default answer to an array question type at start of a survey :
 * Set a default answer to an array texts question type at start of a survey :
 * Set an answer with condition :

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

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

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

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

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

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

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

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

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

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

For comment and other, question code are QuestionCode_comment and QuestionCode_other

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

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

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

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

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

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

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

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

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

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

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

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

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

Usage of NAOK
When you put some variable from question in any equation : if this question (or subquestion) is hidden by condition : this disable all equation.

For example : count(Q1_SQ1,Q1_SQ2,Q1_SQ3,Q1_SQ4) give always an empty string if one subquestion of Q1 is filtered. To count the number of checked subquestion in such question can be count(Q1_SQ1.NAOK,Q1_SQ2.NAOK,Q1_SQ3.NAOK,Q1_SQ4.NAOK). If the sub question is hidden :Exprssion manager return an empty string.

Without NAOK : if one question or one subquestion is hidden : ExpressionScript return always an empty string, same to return false.

The .shown always use the NAOK system (empty string if hidden) but if you need the code of the answer : it's always a good idea to add .NAOK after the question code. Except if you need it and know what you do.

Another example and information is provided at Overriding Cascading Conditions

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

Implemented Functions
The following functions are currently available:

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

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

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

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

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

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

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

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

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

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

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

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

Types and Meanings of Syntax Highlighting
=Additional Reading=