Actions

ExpressionScriptサンプルアンケート

From LimeSurvey Manual

Revision as of 23:27, 7 April 2020 by Bravehorse (talk | contribs) (Created page with "==概要==")


はじめに

ExpressionScriptの使い方を学ぶ最も良い方法は、実際の例を試して、必要に応じて修正することです。

次のサンプルアンケートは、EMを使ってアンケートを強化する方法を確認し、テストするためのものです。これらのアンケートは、ディストリビューションの/docs/demosurveysフォルダーにあります。


出現条件、文言調整、式

概要

次の例では、回答者のBMI(体格指数)を計算します。 この例で使われている機能は以下のとおりです。

EM機能のデモ

  1. 出現条件 - 強力な条件付きロジックのサポート。
  2. パイピング/文言調整 - 回答と質問のメタデータをパイピングまたは調整する機能を提供します。
  3. 動的文言調整 - 質問に答えると、ページ上のレポートが変更されます。
  4. 条件付き文言調整 - if()ステートメントを使用した文やレポート内の条件付き文言調整
  5. - 計算が非表示の場合でも、計算を実行して結果をデータベースに保存できる新しい質問タイプ
  6. 条件付き検証 - 最小許容値などの検証基準は、式などに基づいて、条件付きにすることができます。


スクリーンショット

この例では、体重と身長から太りすぎかどうかを判断する体格指数を計算します。最初は、次の4つの必須質問が表示されます。



情報を入力すると(メートル法またはそれ以外の単位を使用するかどうかも選択)、入力した内容の要約と体重の状態を示すレポートが表示されます。



ここでは、メートル法以外の単位を使用して、結果がどのように動的に変化するかを示す例を紹介します。



カスタムJavaScriptを使わず、データを必要に応じてメートル法に変換し、メートル法で身長、体重、BMI、および体重の状態をデータベースに格納する式タイプの質問があります。

次の例では、レポート内で数学およびその他の関数を使用する方法を確認できます。これは、データを入力する前のページの様子です。未入力または数値以外が入力されている場合に"NaN"または"ゼロ除算エラー"を表示するのではなく、空白のセルを表示するための条件付きロジックがあります。



数字が入力されると、ページ上のレポートが変化して、実行した計算とその結果が表示されます。



このサンプルアンケートには他にも多くの例があります。たとえば、次のスクリーンショットでは、表示条件が動的に変化する入力ページの1例です。ヘルプテキストが示すように、さまざまな年齢、特に矛盾する回答の組み合わせを試し、下部に表示される面白いメッセージを確認してください。また、複数の子供がいると回答した場合は、"X人の子供と楽しく過ごしてください」ではなく、"Xの子供たちと楽しく過ごしてください"というメッセージが表示されることに注目してください。性別や数に応じて文言を調整できるだけでなく、簡単に動詞を活用させたり、性別や数に基づいて名詞を変化させたりすることもできます。



ダウンロード

サンプルアンケートにアクセスするには、LS3_em_tailoring.zipリンクをクリックしてください。

サンプル調査

概要

これは、世帯に何人の人がいるかを尋ねる調査の例です。

グループレベルの出現条件を使って質問の"ループ"を簡単に実装する例を示しています。Person 1のグループを作成した後、そのグループをエクスポートします。SGQAコードの代わりにp1_nameのようなqcode変数名を使用していたので、テキストエディタを使ってグループを何度も素早く編集して再インポートできます(例えば、すべての変数に一意の変数名があり、グループレベルのロジックが正しくなるよう、繰り返しグループを編集して再インポートするのに約10秒かかります)。

質問のコピー機能もありますが、上記のオプションとほど速くはできません。

これは、アンケートが本当に終了するまで(たとえば、必要なすべてのグループが完了するまで)、終了メッセージが表示されないようにする方法も示しています。


EM機能のデモ

  1. グループレベルの出現条件 - フォローアップグループ(Person 1〜5)は、指定された同居人の数までしか表示されません。
  2. 文言調整 - 最終レポートでは、同居者それぞれの統計データを要約します。
  3. 質問の段階的表示 - グループモードでもオールインワンモードでも、後続の質問は、前の質問に回答するまで表示されません。


スクリーンショット

アンケートから、指定された数の同居者の統計情報が表示されるよう調整されたレポートが生成されます。



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



ダウンロード

To download the survey sample, click here: LS3_group_relevance.zip.


配列フィルターの入れ子

概要

This survey is based on the design of a survey courtesy of Joy Lane Research, LLC.


スクリーンショット

In the following, note that:

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




ダウンロード

Click on the following link to download the above example: LS2_cascading_array_filter survey example.


すべての質問タイプと属性を使用したパイピング/文言調整

概要

If you are confused about how to name your variables (SGQA vs Qcodes), you are not alone. Although the main documentation describes how to compose Qcode variable names, nothing beats seeing it in a working demo. This survey shows how you can access question attributes and responses using the ExpressionScript.


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


コンテンツ

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


スクリーンショット

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


ダウンロード

Click on the following link to download the survey: LS3_EM_question_attributes.lss


従来型の検証

概要

This demonstrates all the validation types that make use of expressions and how they affect each of the question types that support validation. In many cases, the validation tips start hidden and only appear if a question fails validation criteria (thus also disappearing once the question passes the validation criteria).


デモで使われている検証タイプ

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


スクリーンショット

Using new CSS styles, each validation type shows up a separate tip. If they are shown, they can be hidden via the hide_tip option. The default option is to show them with red font if the question fails the validation criteria, and green if it passes them:



A user cannot submit a page with validation errors. He will be immediately warned if he entered wrong information:



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



ダウンロード

To download the above example, click on the following link: LS3_Validation_tests.zip.


検証式

概要

Sometimes you need custom validation that cannot be achieved using the traditional min/max criteria. For this, you will need to use the em_validation_q and em_validation_sq options which let you construct complex validation equations at the question and subquestion level, respectively.


At the subquestion level, we also introduce the "this" variable to make it easy to validate each cell in an array without needing to know its variable name. For more details about it, click here.


EM機能のデモ

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

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


スクリーンショット


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

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





In the below example, the validation ensures that no more than 3 questions are answered on any given row by applying this validation equation:

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

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

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



ダウンロード

To download the survey sample used in the above examples, click on the following link: ls2_test_em_sq_validation.lss.


サブ質問の出現条件

概要

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


EM機能のデモ

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

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


スクリーンショット

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



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



ダウンロード

To download our example, click here: ls2_subquestion_relevance.lss.


カンマを小数点として使用する

概要

The ExpressionScript ensures that only valid numbers can be entered into numeric fields. If you enter an invalid number, you will be warned that something is wrong (e.g., in the case of the fruity theme, the whole question is "red-ed").

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

To change the radix separator, access the Text elements options located under the Settings tab, and look for the decimal mark functionality.


For more information on what a radix separator is, click [https://en.wikipedia.org/wiki/Radix_point


小数点記号を使用する質問の種類

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


スクリーンショット

You may note in the below screenshots that comma can be used as a radix separator.



ダウンロード

To download the above example, click on the following link: ls2_comma_as_radix_separator.lss.


グループのランダム化

概要

This survey demonstrates how to make use of the random_group attribute.

If this attribute is used, each time you start the survey, the question order is randomized. However, once the survey is started, the randomization order remains fixed, even if you change languages.


スクリーンショット

Let's check together the below example. The first random question on the page will either be Q1 , Q4, or Q7. The second randomized question on the page will either be Q2, Q5, or Q8.



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



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



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



ダウンロード

To download the above example, click on the following link: Randomization_Group_Test.lss


グループごとにランダムに質問する

概要

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

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


Features Demonstrated

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


Screenshots

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



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



Download

To download the above survey example, click here: Random questions within a group survey example.


Randomly Ask A Specific Number Of Questions In A Group (a subset of the questions)

  This applies only to all random questions from one group and only if the Group by group or All in one format is used.



Overview

This survey shows how to ask a random subset of questions in a group. For example, show 5 random questions out of 10 questions located within a group.

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

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

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

For Q2, it would be:

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

And so on...


Features Demonstrated

  1. relevanceStatus variable
  2. Randomizing


Download

To download the above example, click on the following link: Random 5 out of 10 survey example.

Rating User-Entered List of Products

Overview

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


EM Features Demonstrated

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


Screenshots

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



This image shows how the tailored answers are specified:



Here is the Show Logic File for that question, which lets us verify that the conditional logic works as desired (e.g., to see whether there are any syntax errors):



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



The next image shows the logic file view of the last question. As you can observe, the subquestions, answers, and scale headers can be tailored.



Download

To download the survey example from above, click on the following link: Rating user-entered list of products survey example.