Actions

ExpressionScript examples/ja: Difference between revisions

From LimeSurvey Manual

m (FuzzyBot moved page Expression Manager examples/ja to ExpressionScript examples/ja without leaving a redirect: Part of translatable page "Expression Manager examples")
(Updating to match new version of source page)
Line 1: Line 1:
<languages />
<languages />
__TOC__
__TOC__


{{Note| 独自の例を追加して'''この記事を充実させ、式マネージャーを使おうとしているユーザーを支援してください。}}
{{Note| '''Please help extending this article''' by adding your own examples. This will help others getting started using the Expression Manager.}}




=構造=
=Structure=




この記事の最初の部分では、式の使用方法と式を検索/追加する場所について説明します。 2番目のセクションでは、式の使用例を紹介します。
The first part of the article will describe what Expressions can be used for and where to find/add an Expression. The second part provides examples on how to use Expression.




=式のカテゴリー=
=Categories of Expressions=




LimeSurveyは次の目的で式を使います。
LimeSurvey uses Expressions for the following purposes:
* [[Expression_Manager_examples/ja#Relevance|出現条件]]
* [[Expression_Manager_examples#Relevance|Relevance]]
* [[Expression_Manager_examples/ja#Validation|検証]]
* [[Expression_Manager_examples#Validation|Validation]]
* [[Expression_Manager_examples/ja#Text output|テキスト出力]]
* [[Expression_Manager_examples#Text output|Text output]]
* [[Expression_Manager_examples/ja#Answers storing|回答保存]]  
* [[Expression_Manager_examples#Answers storing|Answers storing]]  




==出現条件==
==Relevance==




'''目的: 質問グループ/質問を非表示にする'''
'''Purpose: Hiding question groups/questions'''


出現条件は、質問グループまたは質問を表示するかどうかを制御します。式の結果が "true"(つまり1)の場合、その要素が表示されます。それ以外の場合(式の結果が "false"(つまり0)の場合)、その要素は非表示になります。
Relevance controls whether a question group or question is shown or hidden. If the result of the Expression is "true" (i.e. 1), the element will be shown. Otherwise (if the result of the Expression is "false" (i.e. 0)), the element will be hidden.


{|class="wikitable"
{|class="wikitable"
Line 42: Line 40:




==検証==
==Validation==




'''目的: 質問/サブ質問/回答の入力/操作を検証する'''
'''Purpose: Validate input/actions on questions/subquestions/answers'''


検証は、回答者の質問に対する入力や操作が有効かどうかを制御します。式の結果が"true"(すなわち1)である場合、当該の要素および検証メッセージに"good"クラスが適用されます。一方、式の結果が"false"(すなわち0)である場合、当該の要素および検証メッセージに"error"クラスが適用されます。'''template.css'''でこれらのクラスのスタイルを宣言することができます。
Validation controls whether the respondent's input into a question or action is valid or not. If the result of the Expression is "true" (i.e. 1), the "good" class will be applied to the element and validation message. Otherwise, if the result of the Expression is "false" (i.e. 0)), the "error" class will be apllied to the element and validation message. You can declare a style for these classes in the '''template.css'''.


{|class="wikitable"
{|class="wikitable"
!種類!!検証対象!!場所!!スクリーンショット
!Type!!Validates!!Where to find!!Screenshot
|-
|-
|質問の検証||質問||質問の作成/編集 -> "ロジックタブ" -> "質問検証式"||[[File:Question validation equation.png|30px]]
|Question Validation||question||Create/Edit question -> "Logic tab" -> "Question validation equation"||[[File:Question validation equation.png|30px]]
|-
|-
|サブ質問の検証||サブ質問||質問の作成/編集 -> "ロジックタブ" -> "サブ質問の検証式"||[[File:Subquestion validation screenshot.png|30px]] ‎
|Subquestion Validation||subquestions||Create/Edit question -> "Logic tab" -> "Subquestion validation equation"||[[File:Subquestion validation screenshot.png|30px]] ‎
|}
|}


==テキスト出力==
==Text output==




'''目的: (何かをして)テキストを出力する'''
'''Purposes: (Do something) and write out some text'''


式を使用してテキストを書き出すことができます。たとえば、何かをテストまたは計算し、テストまたは計算の結果に応じて出力を指定することができます。また、先行する質問のテキスト、回答、質問タイプ等を挿入するために使用することもできます。テキスト出力は、テキストが表示されているあらゆる場所で使用でき、あらゆる種類の結果を返すことができます。
An Expression can be used to write out some text. For example, you can test or calculate something and specify the output depending on the result of the test or calculation. It can also be used to insert the question text, answers, question types, ... of a question used before the Expression is used. Text output can be used everywhere where a text is shown and can return every kind of results.


{|class="wikitable"
{|class="wikitable"
!ケース!!内容
!Use Case!!Description
|-
|-
|文言調整||先行する質問への回答で得た性別に応じて"Mr""Mrs"を使い分けるなど、テキストを調整します。
|Micro tailoring||Adjust a text, e.g., write "Mr" or "Mrs" depending on a gender question asked before
|-
|-
|計算||"あなたの家計収入はxxxです"のように、数値を計算し出力します。
|Calculation||Calculate a value and write it out, e.g., "Your family income is xxx"
|-
|-
|参照||"あなたの名前はxxxです。何歳ですか。"のように他の要素のテキストを挿入します。
|Reference||Insert a text from an element, e.g., "Your name is xxx. How old are you?"
|}
|}




==回答を保存する==
==Answers storing==




'''目的: (何かをして、)(テキストを書き出し)データベースに何かを保存する'''
'''Purposes: (Do something,) (write out some text) and store something in the database'''


[[Question type - Equation/ja|式質問タイプ]]を使用して、データベースに何かを保存することができます。式を使って何かを行い、式の質問([[Question type - Text display/ja|テキスト表示]]に似ています)を表示し、その式の結果をデータベースに格納します。回答テーブルに回答を格納します。結果は統計機能内で使用することもできますが、LimeSurveyエクスポート機能によって提供される形式の1つにインポートすることもできます。
[[Question type - Equation|The Equation Question Type]] can be used to store something in the database. You can do something with an Expression, show the Equation question (which is similar to a [[Question type - Text display|text display]]) and store the result of the Expression in the database. It stores the answer into the responses table. Then, the result can be used within the statistics feature or it can also be imported into one of the formats provided by the LimeSurvey export functionality.


=式の例=
=Examples for Expressions=




==先行する質問の回答を挿入する==
==Insert an answer from a previous question==




{| class="wikitable"
{| class="wikitable"
|目的||先行する質問の回答を挿入する
|Purpose||Insert an answer from a previous question
|-
|-
|タイプ||参照
|Type||Reference
|-
|-
|||質問1(質問コード"Q00": どの都市に住んでいますか。 <br /> 質問2(質問コード"Q01")「地名」に何年住んでいますか。 <br /> やりたいこと: 二つ目の質問の「地名」の代わりに一つ目の質問の回答を使う。
|Example||Question one (question code "Q00"): What city do you live in? <br /> Question two (question code "Q01") How long have you lived in CITY? <br /> To do: The answer from question one should be used instead of "CITY" from the second question.
|-
|-
|||{QOO}
|Expression||{QOO}
|-
|-
|説明|| 二つ目の質問のQ00の部分をQ00で得られた回答に置き換える。
|Description|| The answer provided in Q00 replaces the Q00 field from the second question
|-
|-
|ステップ||質問2を作成/編集する <br /> 質問のテキスト欄に"{QOO}に何年住んでいますか。"と記入する
|Steps||Create/Edit question two <br /> Insert "How long have you lived in {QOO} (years)?" into the question text field
|-
|-
|colspan="2" align="center"|[[File:Simple expression city name.png]]
|colspan="2" align="center"|[[File:Simple expression city name.png]]
|}
|}


==回答を自動入力するために非表示の質問を使う==
==Using hidden equation question to autofill an answer==




'''問題:'''
'''Problem:'''
Q1とQ2という2つの質問があるとします。 Q1では回答者に年齢を聞きます。Q2では、回答者の年齢により20歳未満、20歳、20歳以上の3つのグループに分類しています。
Imagine you have two questions Q1 and Q2. Q1 asks respondents for their age. Q2 divides respondents into three groups: age is below 20 years, age is 20, age is greater than 20.
したがって、Q2は前述の条件に従い、"1", "2", "3"の値を使用します。また、Q2を目に見えないようにしなければなりません(この質問はアンケートには表示されませんが、回答テーブル内の"バックグラウンドプロセス"を介して処理されます)。
So, Q2 should make use of the values "1", "2", "3", which correspond to the above conditions. Also, we should not forget to make Q2 invisible (the question will not be displayed in the survey, but the values will be via the "background process" within the responses table).


'''式の質問タイプを使用してデータベースに格納するには:'''
'''To fill in the database using the Equation question type:'''
* まず、数値入力質問としてを質問(コードQ1)を作成します。
* First, create a question with code Q1 as a numerical input question.
* 次に、式の質問を作成します(コードQ2)。
* Then create another question with code Q2 as an equation question.
* Q2の"表示設定"タブ:
* In the "Display settings" tab chapter of Q2:
** "常にこの質問を非表示にする"フィールドを"オン"に設定します。
** set "Always hide this question" field as "On"
** ""フィールドに式を入力します:
** type in "Equation" field the expression:
<pre>Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}</pre>
<pre>Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}</pre>


==プレースホルダーを使って要約ページを作成する==
==Create a summary page using placeholders==




このチュートリアルでは、アンケートの最後に[http://manual.limesurvey.org/Expression_Manager/ja#Access_to_Variables 式マネージャーのプレースホルダー]を使ってすべての質問と回答をリストする方法を示します。さらに、回答された質問のみ表示する方法を示します。
This tutorial demonstrates how you can create an overview at the end of the survey, listing all questions and answers via [http://manual.limesurvey.org/Expression_Manager#Access_to_Variables Expression Manager placeholders].
It also shows how to limit such an overview to answered questions only.


この例は、[[Media:Limesurvey_Expression_Manager_Placeholder_Demo_Survey.lss|アンケートプレースホルダーのアンケート例]]からダウンロードできます。
Our example can be downloaded from here: [[Media:Limesurvey_Expression_Manager_Placeholder_Demo_Survey.lss|Survey placeholders survey example]].




'''''質問タイプはここで使われます。'''''
'''''Question types being used here:'''''


'''1. 択一質問 / 単一のテキスト/数値質問'''
'''1. Single choice questions / single text/numeric questions'''
*リスト(ドロップダウン)
*List (dropdown)
*リスト(ラジオ)[L]
*List (radio) [L]
*はい/いいえ [Y]
*Yes/No [Y]
*長いフリーテキスト [T]
*Long free text [T]
*短いフリーテキスト [S]
*Short free text [S]
*数値入力 [N]
*Numerical input [N]
*[*]
*Equation [*]




'''2. サブ質問がある質問'''
'''2. Question with subquestions'''
*複数の短いテキスト[Q]
*Multiple short text [Q]
*複数選択[M]
*Multiple choice [M]
*配列[F]
*Array [F]
*配列(はい/いいえ/わからない)[C]
*Array (Yes/No/Uncertain) [C]
*配列(10点尺度)[B]
*Array (10 point choice) [B]




'''3. 尺度が2つある質問'''
'''3. Questions with 2 scales'''
*二元スケール配列 [1]
*Array dual scale [1]




'''4. XとYの尺度がある質問'''
'''4. Questions with X and Y scales'''
*配列(数値) [:]
*Array (Numbers) [:]




'''5. マスク質問'''
'''5. Mask questions'''
*テキスト表示 [X]
*Text display [X]




この簡単な例では、次のような単純なリストになります。
In this simple example, our overview will be a simple list with:


- ''質問テキスト:'' 利用者の回答
- ''Question text:'' User's answer




===択一の質問 / 単一のテキスト/数値を入力する質問===
===Single choice questions / single text/numeric questions===




これらすべての質問タイプについて、Limesurveyは単一の回答を保存します。
For all these question types, Limesurvey will store a single response:


*リスト(ドロップダウン)
*List (dropdown)
*リスト(ラジオ)
*List (radio)
*コメント付きのリスト
*List with comment
*5点選択
*5 point choice
*はい/いいえ
*Yes/No
*短いフリーテキスト
*Short free text
*長いフリーテキスト
*Long free text
*巨大なフリーテキスト
*Huge free text
*数値入力
*Numerical input
*
*Equation
*日付
*Date
*性別
*Gender


質問のコードがq1とすると、質問テキストと回答を次のように参照することができます。
Assuming the question code of the question is q1, we can reference the question text and answer using:


- {q1.question}: {q1.shown}
- {q1.question}: {q1.shown}


:
Example:


- 何歳ですか。: 25
- How old are you?: 25




その質問に回答したかどうかを確認したい場合は、出力を囲むようにIF文を記述します:
If you want to check whether that question was answered you can put an IF statement around your output:


<pre>{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}</pre>
<pre>{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}</pre>


これは、次のように"翻訳します": もし、質問q1が空白でなければ、出力されるのは、"- "と質問テキストと": "と回答テキスト(join()関数は要素を連結して新たな文字列にする)とし、空白の場合は、何も出力しません(""は空文字列です)。
This "translates" to: IF question with code q1 is not empty, output "- " AND the question text AND ": " AND the answer text (the join() function joins elements as a new string; ELSE: output nothing ("" means empty string).


===サブ質問付きの質問===
===Question with subquestions===




これらすべての質問タイプに対して、LimeSurveyはサブ質問を使用します。
For all these question types LimeSurvey uses subquestions:


*複数の短いテキスト
*Multiple short text
*複数選択
*Multiple choice
*コメント付き複数選択
*Multiple choice with comments
*配列
*Array
*配列(5点選択)
*Array (5 point choice)
*配列(10点選択)
*Array (10 point choice)
*配列(はい/いいえ/わからない)
*Array (Yes/No/Uncertain)
*配列(増加/同じ/減少)
*Array (Increase/Same/Decrease)
*配列(行列反転)
*Array by column


質問の質問コードがq2で、サブ質問にSQ001、SQ002(LimeSurveyによる自動採番)という番号が付けられていると仮定して、質問テキストと回答を参照することができます。
Assuming the question code of the question is q2 and the subquestions are numbered SQ001, SQ002, (auto-numbering done by LimeSurvey), we can reference the question text and answer using:


- {q2_SQ001.question}: {q2_SQ001.shown}
- {q2_SQ001.question}: {q2_SQ001.shown}
Line 218: Line 217:
- {q2_SQ002.question}: {q2_SQ002.shown}
- {q2_SQ002.question}: {q2_SQ002.shown}


複数選択質問については、選択された各オプションについてYを表示するようになりました。
For multiple choice questions it makes sense to now show the text of the ticked subquestion but to show a Y for each selected option:


- {q2_SQ001.question}: {q2_SQ001}
- {q2_SQ001.question}: {q2_SQ001}
Line 226: Line 225:
...
...


現時点では、サブ質問の質問文を出力することはできません。[http://bugs.limesurvey.org/view.php?id=8858 この機能に関するリクエスト]を参照してください。
Note that currently it is not possible to output the question text of questions with subquestions, see [http://bugs.limesurvey.org/view.php?id=8858 this feature request].




====例1 (質問タイプが複数選択の場合)====
====Example 1 (assuming question type is multiple choice)====




- あなたはこれらの自動車ブランドを知っていますか。
- Do you know these car brands?


-- Mercedes: Y
-- Mercedes: Y
Line 241: Line 240:




チェックされた項目だけを出力したい場合は、出力のまわりにIF文を置いて、チェックボックスの値をチェックすることができます。
If you want to output checked items only you can put an IF statement around your output and check for the checkbox value:


<pre>{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }</pre>
<pre>{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }</pre>




「Mercedes, Volvo」のように、選択した項目だけのリストを出力したい場合は、<i>listifop</i>関数を使用します。
Use the <i>listifop</i> function if you want to output a list of only the selected items, eg: Mercedes, Volvo


<pre>{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }</pre>
<pre>{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }</pre>




<small>that.q2</small>はq2のすべてのサブ質問に展開されます。詳しくは[[Expression_Manager_-_presentation/ja#.E4.BA.88.E7.B4.84.E5.A4.89.E6.95.B0_.22this.22.E3.80.81.22self.22.E3.80.81.22that.22|self、this、that]]を参照してください。
Where &nbsp;<small>that.q2</small>&nbsp; expands to all of q2's subquestions. See [[Expression_Manager_-_presentation#The_reserved_.22this.22.2C_.22self.22.2C_and_.22that.22_variables|self, this and that]] for more detail.


</br>
</br>


====2 (質問タイプが配列(10点尺度)の場合)====
====Example 2 (assuming question type is Array (10 point choice))====




- 以下の自動車ブランドを「1=ひどい」から「10=とても良い」の間で評価してください。
- Please rate the quality of the car brands below on a scale from 1=very bad to 10=very good?


-- Mercedes: 7
-- Mercedes: 7


<pre>{if(!is_empty(q3_SQ001),join("- ",q3_SQ001.question,": ",q3_SQ001.shown),"")}</pre>
-- Audi: 9


-- Volvo: 9
-- Volvo: 9




評価された項目だけを出力したい場合は、出力のまわりにIF文を置いて、現在のサブ質問に答えたかどうかを以下のようにして調べることができます。
If you want to output rated items only you can put an IF statement around your output and check if the current sub question was answered by using:


<pre>{ if( ! is_empty( q3_SQ001 ), join( "- ", q3_SQ001.question, ": ", q3_SQ001.shown ), "" ) }</pre>
<pre>{ if( ! is_empty( q3_SQ001 ), join( "- ", q3_SQ001.question, ": ", q3_SQ001.shown ), "" ) }</pre>




たとえば、「Audi / Volvo」のように、選択された項目のうち、8を超えるもののリストを出力したい場合は、<i>listifop</i>関数を使用します。
Use the <i>listifop</i> function if for example you want to only output a list of the selected items greater than eight, eg: Audi / Volvo


<pre>{ listifop( 'value', '>', 8, 'question', ' / ', that.q3.sgqa ) }</pre>
<pre>{ listifop( 'value', '>', 8, 'question', ' / ', that.q3.sgqa ) }</pre>
Line 278: Line 277:
</br>
</br>


===2つの尺度を持つ質問===
===Question with two scales===




{{Note|これは二元スケールの質問タイプのみに適用されます。}}
{{Note|This applies to the dual scale question type only.}}




次のコードが使われているとします。
Let's assume that the following codes are being used:
*質問コード: q4
*Question code: q4
*サブ質問コード: SQ001, SQ002, ...
*Subquestion codes: SQ001, SQ002, ...
*回答コード尺度1: A1, A2, ...
*Answer codes scale 1: A1, A2, ...
*回答コード尺度2: B1, B2, ...
*Answer codes scale 2: B1, B2, ...




{{Note|特定の尺度で特定のサブ質問の結果を参照するには、QuestionCode . '_' . SubQuestionCode . '_' . ScaleIDを使用する必要があります。'''"ScaleID"は0から、その他は1から始まるので注意してください。'''</br> 3番目のサブ質問と2番目の尺度の答えを示す例: q4_SQ003_1 - 質問4、サブ質問3、第2の尺度}}
{{Note|To refer to the result of a certain subquestion on a certain scale, you need to use QuestionCode . '_' . SubQuestionCode . '_' . ScaleID. '''Note that "ScaleID" is 0 for the first scale and 1 for the other!'''</br> Example for referring to the answer of the third subquestion and second scale: q4_SQ003_1 - Question 4, subquestion 3, second scale.}}




両方の尺度と最初の2つのサブ質問の結果を出力してみましょう。
Let's output the results for both scales and the first two subquestions:


- {q4_SQ001_0.question}: {q4_SQ001_0.shown} / {q4_SQ001_1.shown}
- {q4_SQ001_0.question}: {q4_SQ001_0.shown} / {q4_SQ001_1.shown}
Line 300: Line 299:
- {q4_SQ002_0.question}: {q4_SQ002_0.shown} / {q4_SQ002_1.shown}
- {q4_SQ002_0.question}: {q4_SQ002_0.shown} / {q4_SQ002_1.shown}


サブ質問テキストを出力するには、スケールIDをプレースホルダーに追加する必要があります(テキストは両方のスケールで同じです)。したがって、{q4_SQ001.question}の代わりに{q4_SQ001_0.question}{q4_SQ001_1.question}を使用する必要があります。
To output the subquestion text, you need to add the scale ID to the placeholder (though the texts are the same for both scales). So instead of {q4_SQ001.question} we have to use {q4_SQ001_0.question} or {q4_SQ001_1.question}.


サブ質問の結果を1つ以上回答があるもののみエクスポート/表示する場合は、各サブ質問に対して次の構文を使用します。
If you want to export/display the results of subquestions with at least one answer only, use this syntax for each subquestion:


<pre>{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}</pre>
<pre>{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}</pre>


===二次元の尺度を持つ質問===
===Question with X and Y scales===




これは、すべての行列型の質問に適用され、(前に示した表示した行だけでなく)すべてのセルに答えることができます。
This applies to all matrix questions which allow an answer for every cell (not just every row as shown previously):
*配列(テキスト)
*Array Texts
*配列(数値)
*Array Numbers




コードの例:
Assumed codes used:
*質問コード: q5
*Question code: q5
*サブ質問コード: SQ001, SQ002, ...
*Subquestion codes: SQ001, SQ002, ...
*回答コード: A1, A2, ...
*Answer codes: A1, A2, ...


特定の列から特定のサブ質問の結果を参照するには、QuestionCode . '_' . SubQuestionCode . '_' . AnswerCodeを使用する必要があります。3番目のサブ質問と2番目の列の回答を参照する例: q5_SQ003_A2
To refer to the result of a certain subquestion from a certain column, you need to use QuestionCode . '_' . SubQuestionCode . '_' . AnswerCode. Example for referring to the answer of the third subquestion and second column: q5_SQ003_A2.


両方の尺度と最初の2つのサブ質問の1から3列目の結果を出力しましょう。
Let's output the results for columns 1-3 of the first two subquestions:


- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}
- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}
Line 328: Line 327:




これらの質問タイプでは、各セル(X軸とY軸の組み合わせ)が1つの回答オプションに等しいため、各セルに対して既存のデータのテストが必要です。例:
Since for these question types each cell (combination of X axes and Y axes) equals one answer option, a test for existing data needs to be done for each cell. Example:


<pre>{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}</pre>
<pre>{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}</pre>




{{Note|サブ質問テキストを出力するには、回答コードをプレースホルダーに追加する必要があります(テキストはすべての回答で同じです)。したがって、{q5_SQ001.question}の代わりに{q5_SQ001_A1.question}{q5_SQ001_A2.question}を使用する必要があります。}}
{{Note|To output the subquestion text you need to add the answer code to the placeholder (though the texts are the same for all answer options). So instead of {q5_SQ001.question} we have to use {q5_SQ001_A1.question} or {q5_SQ001_A2.question}.}}


==先行する質問の回答が空白の場合に質問を非表示にする==
==Hide question if answer from previous question is empty==




{|class="wikitable"
{|class="wikitable"
|目的||先行する質問の回答が無記入の場合に質問を非表示にする
|Purpose||Hide question if answer from previous question is empty
|-
|-
|タイプ||質問の表示条件
|Type||Question Relevance
|-
|-
|||質問1: 質問コード"name"、質問テキスト"名前は何ですか。" <br /> 質問2: 質問テキスト"{name}さん、何歳ですか。" <br /> やりたいこと: 質問1のテキスト欄が無記入の場合、質問2を非表示にする
|Example||Question one: question code "name", question text "What's your name?" <br /> Question two: question text "{name}, how old are you?" <br /> To do: Hide question two if the textfield of question one is empty
|-
|-
|||!is_empty(name)
|Expression||!is_empty(name)
|-
|-
|説明||is_empty()は、変数が空かどうかをチェックします。"!"をつけると結果が逆になります。したがって、変数が空でないとき、式は真となり、質問が表示されます。
|Description||is_empty() determines whether a variable is considered to be empty. The "!" negates the result. So if the variable is not empty the Expression will be true and the question is shown
|-
|-
|ステップ||質問2を作成/編集する <br /> "!is_empty(name)""出現条件式:"に入力する
|Steps||Create/edit question two <br /> Insert "!is_empty(name)" into "Relevance equation:"
|-
|-
|サンプルファイル||[[Media:Hide_question_if_empty_question_group.zip|Hide_question_if_empty_question_group.zip]]
|Sample File||[[Media:Hide_question_if_empty_question_group.zip|Hide_question_if_empty_question_group.zip]]
|-
|-
|colspan="2" align="center"|[[File:Hide question if empty 1.png]][[File:Hide question if empty 2.png]]
|colspan="2" align="center"|[[File:Hide question if empty 1.png]][[File:Hide question if empty 2.png]]
Line 357: Line 356:




==先行する質問の回答が「はい」または「いいえ」の場合、質問グループを非表示にする==
==Hide question group if answer from previous question is Yes or No==




{|class="wikitable"
{|class="wikitable"
|目的||先行する質問の回答が「はい」または「いいえ」の場合、質問グループを非表示にする
|Purpose||Hide question group if answer from previous question is Yes or No
|-
|-
|タイプ||質問グループの出現条件
|Type||Question group Relevance
|-
|-
|||ページ1、質問グループ1、: 質問コード "PET"、質問テキスト "ペットを飼っていますか。" -はい -いいえ <br /> ページ2、質問グループ2: タイトル "ペットについて" <br /> やりたいこと: 質問1の回答がはい/いいえの場合、質問グループを表示/非表示にする
|Example||Page one, Question Group one, Question one: question code "PET", question text "Do you have a pet?" -Yes -No <br /> Page two, Question Group two: Title "About your pet(s)" <br /> To do: Show/Hide question group two if answer from question one is Yes/No
|-
|-
|||PET == "Y"
|Expression||PET == "Y"
|-
|-
|説明||PETは、回答をチェックする質問の質問コードです。接尾辞を使用しない場合、EMは "Qcode.code"を使用します。したがって、PET質問の回答コードと値"Y"を比較します。参加者が"はい"と答えた場合、式は真であり、質問グループ"ペットについて"が表示されます。
|Description||PET is the question code for the question you want to check the answer. If you don't use a suffix EM will use "Qcode.code". So you compare the answer code from the PET question to the value "Y". If the participant answers "Yes" the Expression is true and the question group "About your pet(s)" will be shown.
|-
|-
|ステップ||質問グループ2を作成/編集する <br /> "出現条件式"に"PET == "Y""と入力する
|Steps||Create/edit question group two <br /> Insert "PET == "Y"" into "Relevance equation:"
|-
|-
|サンプルファイル||[[Media:Hide question group if answer from previous question is Yes or No.zip|Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip]]
|Sample File||[[Media:Hide question group if answer from previous question is Yes or No.zip|Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip]]
|-
|-
|colspan="2" align="center"|[[File:Hide question group if answer from previous question is Yes or No 1.png]][[File:Hide question group if answer from previous question is Yes or No 2.png]]
|colspan="2" align="center"|[[File:Hide question group if answer from previous question is Yes or No 1.png]][[File:Hide question group if answer from previous question is Yes or No 2.png]]
Line 379: Line 378:




==複数回答入力質問フィールドの表示値==
==Display value of a multiple answer input question field==




{|class="wikitable"
{|class="wikitable"
|目的||複数回答入力質問フィールドの表示値
|Purpose||Display value of a multiple answer input question field
|-
|-
|タイプ||参照
|Type||Reference
|-
|-
|||質問1: 質問コード "AskChildAge"、質問テキスト"子供は何歳ですか"。サブ質問コード -Child1 -Child2 -Child3 - ChildXXX <br /> 質問2: 質問コード "ReportChildAge" 質問テキスト "一番目の子: - CHILD1はAGE1歳"
|Example||Question one: question code "AskChildAge", question text "How old are your children?". Subquestions codes -Child1 -Child2 -Child3 - ChildXXX <br /> Question two: question code "ReportChildAge" question text "About your first child: - CHILD1 is AGE1."
|-
|-
|||{AskChildAge_Child1.question}, {AskChildAge_Child1.value}
|Expression||{AskChildAge_Child1.question}, {AskChildAge_Child1.value}
|-
|-
|説明||次の質問でサブ質問の値を使用したいとします。次のような式を使用して、サブ質問の値にアクセスできます。 QcodeQuestion_QcodeSubquestion.value
|Description||You want to use the value of a subquestion in a following question. You can access the subquestion value with this kind of expression: QcodeQuestion_QcodeSubquestion.value
|-
|-
|ステップ||2つめの質問を作成/編集する <br /> このテキストを説明に入力する: "一番目の子: - {AskChildAge_Child1.question}{AskChildAge_Child1.value}"
|Steps||Create/edit the second question <br /> Insert this text in the description: "About your first child: - {AskChildAge_Child1.question} is {AskChildAge_Child1.value}."
|-
|-
|サンプルファイル||[[Media:Display_value_of_a_multiple_answer_input_question_field.zip|Display_value_of_a_multiple_answer_input_question_field.zip]]
|Sample File||[[Media:Display_value_of_a_multiple_answer_input_question_field.zip|Display_value_of_a_multiple_answer_input_question_field.zip]]
|-
|-
|colspan="2" align="center"|[[File:Display value of a multiple answer input question field 1.png]]
|colspan="2" align="center"|[[File:Display value of a multiple answer input question field 1.png]]
|}
|}


== "配列(数値)チェックボックス質問"で行ごとにチェックされた数を検証する==
==Validate number of boxes ticked per row for an "Array (Numbers) Checkbox" question==




{|class="wikitable"
{|class="wikitable"
|目的||行ごとにチェックされたボックスの数を検証する
|Purpose||Validate number of boxes ticked per row
|-
|-
|タイプ||検証
|Type||Validation
|-
|-
|||質問タイプ "配列(数値)(チェックボックスレイアウト)"
|Example||Question of type "Array Numbers (Checkbox Layout)"
|-
|-
|||sum(...)
|Expression||sum(...)
|-
|-
|説明||(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X)は、行AでチェックボックスがX個以上チェックされているかを検証しています。<br />A、B、Cの3行すべてをチェックする場合は、"&&"を使って式をつなげます。 <br />(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) <br />&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X) <br />&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
|Description||(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) determines whether at least X checkboxes in row A are checked. <br />If you want to check all three rows (A, B, C), you can connect the expression using "&&": <br />(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) <br />&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X) <br />&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
|-
|-
|ステップ||"配列(数値)(チェックボックスレイアウト)タイプの質問を作成/編集します。<br /> 高度な質問設定で"質問の検証式"に上記の式を挿入します(変数の名前は適宜調整する必要があります)。
|Steps||Create/edit question of type "Array Numbers (Checkbox Layout)".<br /> Insert the above expression into "Question validation equation" at the advanced question settings (you might have to adjust variable namings!).
|-
|-
|サンプルファイル||[[Media:limesurvey_survey_53654.lss|Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss]]
|Sample File||[[Media:limesurvey_survey_53654.lss|Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss]]
|-
|-
|colspan="2" align="center"|[[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 05.png]][[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 1.png]]
|colspan="2" align="center"|[[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 05.png]][[File:Validate number of boxes ticker per row for an Array (Numbers) Checkbox 1.png]]
|}
|}


== 2つの日付の差を計算する==
==Calculate difference between two dates==




{| class="wikitable"
{| class="wikitable"
|目的||2つの日付の差を計算する
|Purpose||Calculate difference between two dates
|-
|-
|タイプ||出現条件 / 式質問タイプ / 検証
|Type||Relevance / Equation question type / Validation
|-
|-
|||質問1(日付/時刻、コード: DOB): 誕生日はいつですか。 <br /> 質問2(日付/時刻、コード: datetoday): 今日の日付は。<br /> 質問3(テキスト表示) : 生まれてからXXXX日目です。 <br /> やりたいこと: 質問1と質問2の間の日数を計算して表示します。
|Example||Question one (date/time, code: DOB): What is your date of birth? <br /> Question two (date/time, code: datetoday): What is the date today? <br /> Question three (boilerplate): You are XXXX days old. <br /> To do: Calculate and display the number of days between the date given in question 1 and the question 2.
|-
|-
|||{(strtotime(datetoday)-strtotime(DOB))/60/60/24}<br>
|Expression||{(strtotime(datetoday)-strtotime(DOB))/60/60/24}<br>
|-
|-
|説明|| strtotimeは、1970年1月1日から指定された日付までの秒数を計算します。上記の式は、指定された2つの日付の間の秒数を計算します。"/60/60/24"は秒数から日数を計算しています。 <br /> "datetoday"を聞く代わりに、strtotime('now')または単にtime()を使えば、1970年1月1日から現在(すなわちアンケートに回答している時刻)までの秒数を得ることもできます。{(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25}という式によって、その人の年齢を計算することができます。この場合、適切な書式で日付を入力しないとstrtotime()関数が正しく機能しません。  
|Description|| strtotime calculates the number of seconds between the 1st of January 1970 and the given date. The above expression calculates the number of seconds between the two given dates. The term "/60/60/24" just calculates the number of days from the number of seconds.  <br /> Instead of asking for the "datetoday", you can also use strtotime('now') or simply time(), which directly returns the number of seconds from January 1970 until now (i.e. the time the survey was taken).  Thus you could quickly figure out the age of a person in years with  the equation {(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25} In any of these cases, it is critical that the date is entered in a proper format, otherwise the strtotime() function will not work right.  
|-
|-
|ステップ||2つの日付(誕生日と本日)の質問、テキスト表示の質問を作成します。 <br /> テキスト表示質問の質問テキストは、"{datetoday}現在、生まれてから{(strtotime(today)-strtotime(dob))/60/60/24}XXXX日目です。"とします。 <br /><br /> 注意: 関数strtotimeは、多くの日付フォーマットで動作しますが、すべての日付フォーマットで動作するわけではありません。問題がある場合は、日付形式をmm/dd/yyyy、yyyy-mm-dd、dd.mm.yyyyのいずれかにします。
|Steps||Create two date questions (for the date of birth and for today's date) and a boilerplate question. <br /> In the question text of the boilerplate question insert: "On {datetoday} you were {(strtotime(today)-strtotime(dob))/60/60/24} days days old." <br /><br /> Please note: The function strtotime can work with MANY but not all date formats. If you run into problems set your survey's date format to mm/dd/yyyy or yyyy-mm-dd or dd.mm.yyyy"
|-
|-
|サンプルファイル||[[Media:TestSurvey_Working_with_Dates.zip|Date_difference.zip]]
|Sample File||[[Media:TestSurvey_Working_with_Dates.zip|Date_difference.zip]]
|-
|-
|colspan="2" align="center"|[[File:Date_difference_example.png]]
|colspan="2" align="center"|[[File:Date_difference_example.png]]
|}
|}


==式マネージャーを評価に使用する==
==Using Expression Manager for Assessments==




[[Question type - Array/ja|配列質問タイプ]]と評価で式マネージャーをどのように使うかを示す別の例です。</ br>
Here comes another example on how to use the Expression Manager with [[Question type - Array|Array type questions]] and assessments:</ br>


配列の質問が2つあり、計算結果をデータベースに保存したいとします。実際は単純なものですが、配列を作成してテストし、ダミー回答を送信して動作するか、完了ページに結果が表示されるかを確認する必要があります。
Let's say you have 2 array questions and you want to save the results of the calculation data to your database. It is actually simple, you will need to create and test your arrays and submit a dummy response to see if it works and gives you the results in the completed page.




'''実装方法の詳細:'''
'''Implementation details:'''
*式タイプの質問を追加する
*add a question of type equation
*次の行を追加する(Q1、Q2が配列質問のコードとする):
*add the following line assuming that Q1 is your array question code while Q2 is the second one:
** <pre>{sum(Q1_SQ001.value,Q2_SQ001.value)}</pre>
** <pre>{sum(Q1_SQ001.value,Q2_SQ001.value)}</pre>


SQ001は任意のサブ質問の既定のコードです。サブ質問コードを変更した場合は、上記の式を適宜調整してください。
Note that SQ001 is the default code for any subquestion. If you change the subquestion code, adjust the equation above accordingly.




==配列でem_validation_qを使用する==
==Using em_validation_q in array==




[[QS:Em_validation_q/ja|質問の検証式]]を使用して、任意の条件で配列を制御できます。
You can use the [[QS:Em_validation_q|question validation equation]] to control an array with any condition.




'''実装方法の詳細:'''
'''Implementation details:'''
* 1つ目の配列 : 択一の配列
* for the first array : array of single choice
** 質問コードはARRAY
** Question code is ARRAY
** サブ質問コードは、SQ01、SQ02、SQ03、SQ04
** Sub question code are SQ01,SQ02,SQ03 and SQ04
** 全体の質問検証式を次のように更新<pre>!is_empty(ARRAY_SQ01) and !is_empty(ARRAY_SQ03) </pre>
** update the Whole question validation equation and put <pre>!is_empty(ARRAY_SQ01) and !is_empty(ARRAY_SQ03) </pre>
* 2つ目の配列 : テキスト配列
* for the second array : array of text
** 質問コードはARRAYTEXT
** Question code is ARRAYTEXT
** Y軸のサブ質問コードはSY01、SY02、SY03、SY04
** Sub question at Y axis code are SY01,SY02,SY03, and SY04
** 全体の質問検証式を次のように更新 <pre>count(self.sq_SY01 >= 1) and count(self.sq_SY03 >= 3)</pre>
** Update the Whole question validation equation and put <pre>count(self.sq_SY01 >= 1) and count(self.sq_SY03 >= 3)</pre>

Revision as of 10:20, 3 March 2020

Please help extending this article by adding your own examples. This will help others getting started using the Expression Manager.


Structure

The first part of the article will describe what Expressions can be used for and where to find/add an Expression. The second part provides examples on how to use Expression.


Categories of Expressions

LimeSurvey uses Expressions for the following purposes:


Relevance

Purpose: Hiding question groups/questions

Relevance controls whether a question group or question is shown or hidden. If the result of the Expression is "true" (i.e. 1), the element will be shown. Otherwise (if the result of the Expression is "false" (i.e. 0)), the element will be hidden.

種類 表示/非表示 場所 スクリーンショット
質問グループの出現条件 質問グループ 質問グループの作成/編集 -> "出現条件式:"
質問の出現条件 質問 質問の作成/編集 -> "出現条件式:"
サブ質問の出現条件 サブ質問 サブ質問の編集 -> "出現条件式:"


Validation

Purpose: Validate input/actions on questions/subquestions/answers

Validation controls whether the respondent's input into a question or action is valid or not. If the result of the Expression is "true" (i.e. 1), the "good" class will be applied to the element and validation message. Otherwise, if the result of the Expression is "false" (i.e. 0)), the "error" class will be apllied to the element and validation message. You can declare a style for these classes in the template.css.

Type Validates Where to find Screenshot
Question Validation question Create/Edit question -> "Logic tab" -> "Question validation equation"
Subquestion Validation subquestions Create/Edit question -> "Logic tab" -> "Subquestion validation equation"

Text output

Purposes: (Do something) and write out some text

An Expression can be used to write out some text. For example, you can test or calculate something and specify the output depending on the result of the test or calculation. It can also be used to insert the question text, answers, question types, ... of a question used before the Expression is used. Text output can be used everywhere where a text is shown and can return every kind of results.

Use Case Description
Micro tailoring Adjust a text, e.g., write "Mr" or "Mrs" depending on a gender question asked before
Calculation Calculate a value and write it out, e.g., "Your family income is xxx"
Reference Insert a text from an element, e.g., "Your name is xxx. How old are you?"


Answers storing

Purposes: (Do something,) (write out some text) and store something in the database

The Equation Question Type can be used to store something in the database. You can do something with an Expression, show the Equation question (which is similar to a text display) and store the result of the Expression in the database. It stores the answer into the responses table. Then, the result can be used within the statistics feature or it can also be imported into one of the formats provided by the LimeSurvey export functionality.

Examples for Expressions

Insert an answer from a previous question

Purpose Insert an answer from a previous question
Type Reference
Example Question one (question code "Q00"): What city do you live in?
Question two (question code "Q01") How long have you lived in CITY?
To do: The answer from question one should be used instead of "CITY" from the second question.
Expression {QOO}
Description The answer provided in Q00 replaces the Q00 field from the second question
Steps Create/Edit question two
Insert "How long have you lived in {QOO} (years)?" into the question text field

Using hidden equation question to autofill an answer

Problem: Imagine you have two questions Q1 and Q2. Q1 asks respondents for their age. Q2 divides respondents into three groups: age is below 20 years, age is 20, age is greater than 20. So, Q2 should make use of the values "1", "2", "3", which correspond to the above conditions. Also, we should not forget to make Q2 invisible (the question will not be displayed in the survey, but the values will be via the "background process" within the responses table).

To fill in the database using the Equation question type:

  • First, create a question with code Q1 as a numerical input question.
  • Then create another question with code Q2 as an equation question.
  • In the "Display settings" tab chapter of Q2:
    • set "Always hide this question" field as "On"
    • type in "Equation" field the expression:
Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}

Create a summary page using placeholders

This tutorial demonstrates how you can create an overview at the end of the survey, listing all questions and answers via Expression Manager placeholders. It also shows how to limit such an overview to answered questions only.

Our example can be downloaded from here: Survey placeholders survey example.


Question types being used here:

1. Single choice questions / single text/numeric questions

  • List (dropdown)
  • List (radio) [L]
  • Yes/No [Y]
  • Long free text [T]
  • Short free text [S]
  • Numerical input [N]
  • Equation [*]


2. Question with subquestions

  • Multiple short text [Q]
  • Multiple choice [M]
  • Array [F]
  • Array (Yes/No/Uncertain) [C]
  • Array (10 point choice) [B]


3. Questions with 2 scales

  • Array dual scale [1]


4. Questions with X and Y scales

  • Array (Numbers) [:]


5. Mask questions

  • Text display [X]


In this simple example, our overview will be a simple list with:

- Question text: User's answer


Single choice questions / single text/numeric questions

For all these question types, Limesurvey will store a single response:

  • List (dropdown)
  • List (radio)
  • List with comment
  • 5 point choice
  • Yes/No
  • Short free text
  • Long free text
  • Huge free text
  • Numerical input
  • Equation
  • Date
  • Gender

Assuming the question code of the question is q1, we can reference the question text and answer using:

- {q1.question}: {q1.shown}

Example:

- How old are you?: 25


If you want to check whether that question was answered you can put an IF statement around your output:

{if(!is_empty(q1),join("- ",q1.question,": ",q1.shown),"")}

This "translates" to: IF question with code q1 is not empty, output "- " AND the question text AND ": " AND the answer text (the join() function joins elements as a new string; ELSE: output nothing ("" means empty string).

Question with subquestions

For all these question types LimeSurvey uses subquestions:

  • Multiple short text
  • Multiple choice
  • Multiple choice with comments
  • Array
  • Array (5 point choice)
  • Array (10 point choice)
  • Array (Yes/No/Uncertain)
  • Array (Increase/Same/Decrease)
  • Array by column

Assuming the question code of the question is q2 and the subquestions are numbered SQ001, SQ002, (auto-numbering done by LimeSurvey), we can reference the question text and answer using:

- {q2_SQ001.question}: {q2_SQ001.shown}

- {q2_SQ002.question}: {q2_SQ002.shown}

For multiple choice questions it makes sense to now show the text of the ticked subquestion but to show a Y for each selected option:

- {q2_SQ001.question}: {q2_SQ001}

- {q2_SQ002.question}: {q2_SQ002}

...

Note that currently it is not possible to output the question text of questions with subquestions, see this feature request.


Example 1 (assuming question type is multiple choice)

- Do you know these car brands?

-- Mercedes: Y

-- Audi: N

-- Volvo: Y


If you want to output checked items only you can put an IF statement around your output and check for the checkbox value:

{ if( q2_SQ001=="Y", join( "- ", q2_SQ001.question, ": ", q2_SQ001.shown ), "" ) }


Use the listifop function if you want to output a list of only the selected items, eg: Mercedes, Volvo

{ listifop( 'value', '==', 'Y', 'question', ', ', that.q2.sgqa ) }


Where  that.q2  expands to all of q2's subquestions. See self, this and that for more detail.


Example 2 (assuming question type is Array (10 point choice))

- Please rate the quality of the car brands below on a scale from 1=very bad to 10=very good?

-- Mercedes: 7

-- Audi: 9

-- Volvo: 9


If you want to output rated items only you can put an IF statement around your output and check if the current sub question was answered by using:

{ if( ! is_empty( q3_SQ001 ), join( "- ", q3_SQ001.question, ": ", q3_SQ001.shown ), "" ) }


Use the listifop function if for example you want to only output a list of the selected items greater than eight, eg: Audi / Volvo

{ listifop( 'value', '>', 8, 'question', ' / ', that.q3.sgqa ) }


Question with two scales

This applies to the dual scale question type only.


Let's assume that the following codes are being used:

  • Question code: q4
  • Subquestion codes: SQ001, SQ002, ...
  • Answer codes scale 1: A1, A2, ...
  • Answer codes scale 2: B1, B2, ...


To refer to the result of a certain subquestion on a certain scale, you need to use QuestionCode . '_' . SubQuestionCode . '_' . ScaleID. Note that "ScaleID" is 0 for the first scale and 1 for the other!
Example for referring to the answer of the third subquestion and second scale: q4_SQ003_1 - Question 4, subquestion 3, second scale.


Let's output the results for both scales and the first two subquestions:

- {q4_SQ001_0.question}: {q4_SQ001_0.shown} / {q4_SQ001_1.shown}

- {q4_SQ002_0.question}: {q4_SQ002_0.shown} / {q4_SQ002_1.shown}

To output the subquestion text, you need to add the scale ID to the placeholder (though the texts are the same for both scales). So instead of {q4_SQ001.question} we have to use {q4_SQ001_0.question} or {q4_SQ001_1.question}.

If you want to export/display the results of subquestions with at least one answer only, use this syntax for each subquestion:

{if(count(q4_SQ001_0,q4_SQ001_1)>0,join("- ",q4_SQ001_0.question,": ",q4_SQ001_0.shown," / ",q4_SQ001_1.shown),"")}

Question with X and Y scales

This applies to all matrix questions which allow an answer for every cell (not just every row as shown previously):

  • Array Texts
  • Array Numbers


Assumed codes used:

  • Question code: q5
  • Subquestion codes: SQ001, SQ002, ...
  • Answer codes: A1, A2, ...

To refer to the result of a certain subquestion from a certain column, you need to use QuestionCode . '_' . SubQuestionCode . '_' . AnswerCode. Example for referring to the answer of the third subquestion and second column: q5_SQ003_A2.

Let's output the results for columns 1-3 of the first two subquestions:

- {q5_SQ001_A1.question}: {q5_SQ001_A1.shown} | {q5_SQ001_A2.shown} | {q5_SQ001_A3.shown}

- {q5_SQ002_A1.question}: {q5_SQ002_A1.shown} | {q5_SQ002_A2.shown} | {q5_SQ002_A3.shown}


Since for these question types each cell (combination of X axes and Y axes) equals one answer option, a test for existing data needs to be done for each cell. Example:

{if(!is_empty(q5_SQ001_A1),join("- ",q5_SQ001_A1.question,": ",q5_SQ001_A1.shown),"")}


To output the subquestion text you need to add the answer code to the placeholder (though the texts are the same for all answer options). So instead of {q5_SQ001.question} we have to use {q5_SQ001_A1.question} or {q5_SQ001_A2.question}.

Hide question if answer from previous question is empty

Purpose Hide question if answer from previous question is empty
Type Question Relevance
Example Question one: question code "name", question text "What's your name?"
Question two: question text "{name}, how old are you?"
To do: Hide question two if the textfield of question one is empty
Expression !is_empty(name)
Description is_empty() determines whether a variable is considered to be empty. The "!" negates the result. So if the variable is not empty the Expression will be true and the question is shown
Steps Create/edit question two
Insert "!is_empty(name)" into "Relevance equation:"
Sample File Hide_question_if_empty_question_group.zip


Hide question group if answer from previous question is Yes or No

Purpose Hide question group if answer from previous question is Yes or No
Type Question group Relevance
Example Page one, Question Group one, Question one: question code "PET", question text "Do you have a pet?" -Yes -No
Page two, Question Group two: Title "About your pet(s)"
To do: Show/Hide question group two if answer from question one is Yes/No
Expression PET == "Y"
Description PET is the question code for the question you want to check the answer. If you don't use a suffix EM will use "Qcode.code". So you compare the answer code from the PET question to the value "Y". If the participant answers "Yes" the Expression is true and the question group "About your pet(s)" will be shown.
Steps Create/edit question group two
Insert "PET == "Y"" into "Relevance equation:"
Sample File Hide_question group_if_answer_from_previous_question_is_Yes_or_No.zip


Display value of a multiple answer input question field

Purpose Display value of a multiple answer input question field
Type Reference
Example Question one: question code "AskChildAge", question text "How old are your children?". Subquestions codes -Child1 -Child2 -Child3 - ChildXXX
Question two: question code "ReportChildAge" question text "About your first child: - CHILD1 is AGE1."
Expression {AskChildAge_Child1.question}, {AskChildAge_Child1.value}
Description You want to use the value of a subquestion in a following question. You can access the subquestion value with this kind of expression: QcodeQuestion_QcodeSubquestion.value
Steps Create/edit the second question
Insert this text in the description: "About your first child: - {AskChildAge_Child1.question} is {AskChildAge_Child1.value}."
Sample File Display_value_of_a_multiple_answer_input_question_field.zip

Validate number of boxes ticked per row for an "Array (Numbers) Checkbox" question

Purpose Validate number of boxes ticked per row
Type Validation
Example Question of type "Array Numbers (Checkbox Layout)"
Expression sum(...)
Description (sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X) determines whether at least X checkboxes in row A are checked.
If you want to check all three rows (A, B, C), you can connect the expression using "&&":
(sum(Test_A_1, Test_A_2, Test_A_3, Test_A_4, Test_A_5) <= X)
&& (sum(Test_B_1, Test_B_2, Test_B_3, Test_B_4, Test_B_5) <= X)
&& (sum(Test_C_1, Test_C_2, Test_C_3, Test_C_4, Test_C_5) <= X)
Steps Create/edit question of type "Array Numbers (Checkbox Layout)".
Insert the above expression into "Question validation equation" at the advanced question settings (you might have to adjust variable namings!).
Sample File Validate number of boxes ticker per row for an Array (Numbers) Checkbox.lss

Calculate difference between two dates

Purpose Calculate difference between two dates
Type Relevance / Equation question type / Validation
Example Question one (date/time, code: DOB): What is your date of birth?
Question two (date/time, code: datetoday): What is the date today?
Question three (boilerplate): You are XXXX days old.
To do: Calculate and display the number of days between the date given in question 1 and the question 2.
Expression {(strtotime(datetoday)-strtotime(DOB))/60/60/24}
Description strtotime calculates the number of seconds between the 1st of January 1970 and the given date. The above expression calculates the number of seconds between the two given dates. The term "/60/60/24" just calculates the number of days from the number of seconds.
Instead of asking for the "datetoday", you can also use strtotime('now') or simply time(), which directly returns the number of seconds from January 1970 until now (i.e. the time the survey was taken). Thus you could quickly figure out the age of a person in years with the equation {(time() - strtotime(DOB)) / 60 / 60 / 24 / 365.25} In any of these cases, it is critical that the date is entered in a proper format, otherwise the strtotime() function will not work right.
Steps Create two date questions (for the date of birth and for today's date) and a boilerplate question.
In the question text of the boilerplate question insert: "On {datetoday} you were {(strtotime(today)-strtotime(dob))/60/60/24} days days old."

Please note: The function strtotime can work with MANY but not all date formats. If you run into problems set your survey's date format to mm/dd/yyyy or yyyy-mm-dd or dd.mm.yyyy"
Sample File Date_difference.zip

Using Expression Manager for Assessments

Here comes another example on how to use the Expression Manager with Array type questions and assessments:</ br>

Let's say you have 2 array questions and you want to save the results of the calculation data to your database. It is actually simple, you will need to create and test your arrays and submit a dummy response to see if it works and gives you the results in the completed page.


Implementation details:

  • add a question of type equation
  • add the following line assuming that Q1 is your array question code while Q2 is the second one:
    • {sum(Q1_SQ001.value,Q2_SQ001.value)}

Note that SQ001 is the default code for any subquestion. If you change the subquestion code, adjust the equation above accordingly.


Using em_validation_q in array

You can use the question validation equation to control an array with any condition.


Implementation details:

  • for the first array : array of single choice
    • Question code is ARRAY
    • Sub question code are SQ01,SQ02,SQ03 and SQ04
    • update the Whole question validation equation and put
      !is_empty(ARRAY_SQ01) and !is_empty(ARRAY_SQ03) 
  • for the second array : array of text
    • Question code is ARRAYTEXT
    • Sub question at Y axis code are SY01,SY02,SY03, and SY04
    • Update the Whole question validation equation and put
      count(self.sq_SY01 >= 1) and count(self.sq_SY03 >= 3)