Actions

ExpressionScript examples/ja: Difference between revisions

From LimeSurvey Manual

(Created page with "配列質問タイプと評価で式マネージャーをどのように使うかを示す別の例です。</ br>")
(Created page with "配列の質問が2つあり、計算結果をデータベースに保存したいとします。実際は単純なものですが、配列を作成してテストし、ダミ...")
Line 445: Line 445:
[[Question type - Array/ja|配列質問タイプ]]と評価で式マネージャーをどのように使うかを示す別の例です。</ br>
[[Question type - Array/ja|配列質問タイプ]]と評価で式マネージャーをどのように使うかを示す別の例です。</ 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.
配列の質問が2つあり、計算結果をデータベースに保存したいとします。実際は単純なものですが、配列を作成してテストし、ダミー回答を送信して動作するか、完了ページに結果が表示されるかを確認する必要があります。





Revision as of 08:30, 4 April 2020

独自の例を追加してこの記事を充実させ、式マネージャーを使おうとしているユーザーを支援してください。


構造

この記事の最初の部分では、式の使用方法と式を検索/追加する場所について説明します。 2番目のセクションでは、式の使用例を紹介します。


式のカテゴリー

LimeSurveyは次の目的で式を使います。


出現条件

目的: 質問グループ/質問を非表示にする

出現条件は、質問グループまたは質問を表示するかどうかを制御します。式の結果が "true"(つまり1)の場合、その要素が表示されます。それ以外の場合(式の結果が "false"(つまり0)の場合)、その要素は非表示になります。

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


検証

目的: 質問/サブ質問/回答の入力/操作を検証する

検証は、回答者の質問に対する入力や操作が有効かどうかを制御します。式の結果が"true"(すなわち1)である場合、当該の要素および検証メッセージに"good"クラスが適用されます。一方、式の結果が"false"(すなわち0)である場合、当該の要素および検証メッセージに"error"クラスが適用されます。template.cssでこれらのクラスのスタイルを宣言することができます。

種類 検証対象 場所 スクリーンショット
質問の検証 質問 質問の作成/編集 -> "ロジックタブ" -> "質問検証式"
サブ質問の検証 サブ質問 質問の作成/編集 -> "ロジックタブ" -> "サブ質問の検証式"

テキスト出力

目的: (何かをして)テキストを出力する

式を使用してテキストを書き出すことができます。たとえば、何かをテストまたは計算し、テストまたは計算の結果に応じて出力を指定することができます。また、先行する質問のテキスト、回答、質問タイプ等を挿入するために使用することもできます。テキスト出力は、テキストが表示されているあらゆる場所で使用でき、あらゆる種類の結果を返すことができます。

ケース 内容
文言調整 先行する質問への回答で得た性別に応じて"Mr"か"Mrs"を使い分けるなど、テキストを調整します。
計算 "あなたの家計収入はxxxです"のように、数値を計算し出力します。
参照 "あなたの名前はxxxです。何歳ですか。"のように他の要素のテキストを挿入します。


回答を保存する

目的: (何かをして、)(テキストを書き出し)データベースに何かを保存する

式質問タイプを使用して、データベースに何かを保存することができます。式を使って何かを行い、式の質問(テキスト表示に似ています)を表示し、その式の結果をデータベースに格納します。回答テーブルに回答を格納します。結果は統計機能内で使用することもできますが、LimeSurveyエクスポート機能によって提供される形式の1つにインポートすることもできます。

式の例

先行する質問の回答を挿入する

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

回答を自動入力するために非表示の質問を使う

問題: Q1とQ2という2つの質問があるとします。 Q1では回答者に年齢を聞きます。Q2では、回答者の年齢により20歳未満、20歳、20歳以上の3つのグループに分類しています。 したがって、Q2は前述の条件に従い、"1", "2", "3"の値を使用します。また、Q2を目に見えないようにしなければなりません(この質問はアンケートには表示されませんが、回答テーブル内の"バックグラウンドプロセス"を介して処理されます)。

式の質問タイプを使用してデータベースに格納するには:

  • まず、数値入力質問としてを質問(コードQ1)を作成します。
  • 次に、式の質問を作成します(コードQ2)。
  • Q2の"表示設定"タブ:
    • "常にこの質問を非表示にする"フィールドを"オン"に設定します。
    • "式"フィールドに式を入力します:
Q2=if(Q1.NAOK < 20, "1", if(Q1.NAOK > 20, "3", "2"))}

プレースホルダーを使って要約ページを作成する

このチュートリアルでは、アンケートの最後に式マネージャーのプレースホルダーを使ってすべての質問と回答をリストする方法を示します。さらに、回答された質問のみ表示する方法を示します。

この例は、アンケートプレースホルダーのアンケート例からダウンロードできます。


質問タイプはここで使われます。

1. 択一質問 / 単一のテキスト/数値質問

  • リスト(ドロップダウン)
  • リスト(ラジオ)[L]
  • はい/いいえ [Y]
  • 長いフリーテキスト [T]
  • 短いフリーテキスト [S]
  • 数値入力 [N]
  • 式 [*]


2. サブ質問がある質問

  • 複数の短いテキスト[Q]
  • 複数選択[M]
  • 配列[F]
  • 配列(はい/いいえ/わからない)[C]
  • 配列(10点尺度)[B]


3. 尺度が2つある質問

  • 二元スケール配列 [1]


4. XとYの尺度がある質問

  • 配列(数値) [:]


5. マスク質問

  • テキスト表示 [X]


この簡単な例では、次のような単純なリストになります。

- 質問テキスト: 利用者の回答


択一の質問 / 単一のテキスト/数値を入力する質問

これらすべての質問タイプについて、Limesurveyは単一の回答を保存します。

  • リスト(ドロップダウン)
  • リスト(ラジオ)
  • コメント付きのリスト
  • 5点選択
  • はい/いいえ
  • 短いフリーテキスト
  • 長いフリーテキスト
  • 巨大なフリーテキスト
  • 数値入力
  • 日付
  • 性別

質問のコードがq1とすると、質問テキストと回答を次のように参照することができます。

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

例:

- 何歳ですか。: 25


その質問に回答したかどうかを確認したい場合は、出力を囲むようにIF文を記述します:

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

これは、次のように"翻訳します": もし、質問q1が空白でなければ、出力されるのは、"- "と質問テキストと": "と回答テキスト(join()関数は要素を連結して新たな文字列にする)とし、空白の場合は、何も出力しません(""は空文字列です)。

Question with subquestions

これらすべての質問タイプに対して、LimeSurveyはサブ質問を使用します。

  • 複数の短いテキスト
  • 複数選択
  • コメント付き複数選択
  • 配列
  • 配列(5点選択)
  • 配列(10点選択)
  • 配列(はい/いいえ/わからない)
  • 配列(増加/同じ/減少)
  • 配列(行列反転)

質問の質問コードがq2で、サブ質問にSQ001、SQ002(LimeSurveyによる自動採番)という番号が付けられていると仮定して、質問テキストと回答を参照することができます。

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

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

複数選択質問については、選択された各オプションについてYを表示するようになりました。

- {q2_SQ001.question}: {q2_SQ001}

- {q2_SQ002.question}: {q2_SQ002}

...

現時点では、サブ質問の質問文を出力することはできません。この機能に関するリクエストを参照してください。


例1 (質問タイプが複数選択の場合)

- あなたは次の自動車ブランドを知っていますか。

-- Mercedes: Y

-- Audi: N

-- Volvo: Y


チェックされた項目だけを出力したい場合は、出力のまわりにIF文を置いて、チェックボックスの値をチェックすることができます。

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


「Mercedes, Volvo」のように、選択した項目だけのリストを出力したい場合は、listifop関数を使用します。

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


that.q2はq2のすべてのサブ質問に展開されます。詳しくはself、this、thatを参照してください。


例 2 (質問タイプが配列(10点尺度)の場合)

- 以下の自動車ブランドを「1=ひどい」から「10=とても良い」の間で評価してください。

-- Mercedes: 7

-- Audi: 9

-- Volvo: 9


評価された項目だけを出力したい場合は、出力のまわりにIF文を置いて、現在のサブ質問に答えたかどうかを以下のようにして調べることができます。

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


たとえば、「Audi / Volvo」のように、選択された項目のうち、8を超えるもののリストを出力したい場合は、listifop関数を使用します。

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


2つの尺度を持つ質問

これは二元スケールの質問タイプのみに適用されます。


次のコードが使われているとします。

  • 質問コード: q4
  • サブ質問コード: SQ001, SQ002, ...
  • 回答コード尺度1: A1, A2, ...
  • 回答コード尺度2: B1, B2, ...


特定の尺度で特定のサブ質問の結果を参照するには、QuestionCode . '_' . SubQuestionCode . '_' . ScaleIDを使用する必要があります。"ScaleID"は0から、その他は1から始まるので注意してください。
3番目のサブ質問と2番目の尺度の答えを示す例: q4_SQ003_1 - 質問4、サブ質問3、第2の尺度


両方の尺度と最初の2つのサブ質問の結果を出力してみましょう。

- {q4_SQ001_0.question}: {q4_SQ001_0.shown} / {q4_SQ001_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}を使用する必要があります。

サブ質問の結果を1つ以上回答があるもののみエクスポート/表示する場合は、各サブ質問に対して次の構文を使用します。

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

二次元の尺度を持つ質問

これは、すべての行列型の質問に適用され、(前に示した表示した行だけでなく)すべてのセルに答えることができます。

  • 配列(テキスト)
  • 配列(数値)


コードの例:

  • 質問コード: q5
  • サブ質問コード: SQ001, SQ002, ...
  • 回答コード: A1, A2, ...

特定の列から特定のサブ質問の結果を参照するには、QuestionCode . '_' . SubQuestionCode . '_' . AnswerCodeを使用する必要があります。3番目のサブ質問と2番目の列の回答を参照する例: q5_SQ003_A2

両方の尺度と最初の2つのサブ質問の1から3列目の結果を出力しましょう。

- {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}


これらの質問タイプでは、各セル(X軸とY軸の組み合わせ)が1つの回答オプションに等しいため、各セルに対して既存のデータのテストが必要です。例:

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


サブ質問テキストを出力するには、回答コードをプレースホルダーに追加する必要があります(テキストはすべての回答で同じです)。したがって、{q5_SQ001.question}の代わりに{q5_SQ001_A1.question}か{q5_SQ001_A2.question}を使用する必要があります。

先行する質問の回答が空白の場合に質問を非表示にする

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


先行する質問の回答が「はい」または「いいえ」の場合、質問グループを非表示にする

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


複数回答入力質問フィールドの表示値

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

"配列(数値)チェックボックス質問"で行ごとにチェックされた数を検証する

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

2つの日付の差を計算する

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

式マネージャーを評価に使用する

配列質問タイプと評価で式マネージャーをどのように使うかを示す別の例です。</ br>

配列の質問が2つあり、計算結果をデータベースに保存したいとします。実際は単純なものですが、配列を作成してテストし、ダミー回答を送信して動作するか、完了ページに結果が表示されるかを確認する必要があります。


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)