Difference between revisions of "Questionnaire scripting"
Line 1: | Line 1: | ||
= Questionnaire scripting = | = Questionnaire scripting = | ||
− | The context for questionnaire scripts defines three variables which should not be overwritten if the questionnaire author intends to handle events which the new viewer triggers - these variables are: | + | |
− | ● Viewer | + | The context for questionnaire scripts defines three variables which should not be overwritten if the questionnaire author intends to handle events which the new viewer triggers - these variables are: |
− | ● Questionnaire | + | |
− | ● AnswerSheet (alias for this) | + | ● Viewer |
+ | |||
+ | ● Questionnaire | ||
+ | |||
+ | ● AnswerSheet (alias for this) | ||
+ | |||
== Answer sheet event handlers == | == Answer sheet event handlers == | ||
− | Answer sheet event handlers are defined by calling the <code class="mwt-code" >.bind()</code> method on the <code class="mwt-code" >AnswerSheet</code> object. | + | Answer sheet event handlers are defined by calling the <code class="mwt-code" >.bind()</code> method on the <code class="mwt-code" >AnswerSheet</code> object. |
Example: | Example: | ||
Line 15: | Line 20: | ||
Notice that the questionnaire script section is not used if the questionnaire is reached by using [[GotoQuestionnaire|GotoQuestionnaire]]. | Notice that the questionnaire script section is not used if the questionnaire is reached by using [[GotoQuestionnaire|GotoQuestionnaire]]. | ||
− | If the event handler is called in a question script section, the lifetime is from visiting the page with the given question until the viewer is closed. It it therefore NOT recommended to attach answer sheet event handlers in a question script section. | + | If the event handler is called in a question script section, the lifetime is from visiting the page with the given question until the viewer is closed. It it therefore NOT recommended to attach answer sheet event handlers in a question script section. |
The following sections describe the actual events available. | The following sections describe the actual events available. | ||
Line 37: | Line 42: | ||
<code class="mwt-code" >AnswerSheet.bind('beforeMoveToFirstPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | <code class="mwt-code" >AnswerSheet.bind('beforeMoveToFirstPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | ||
− | This is only used in the pre-viewer. | + | This is only used in the pre-viewer. |
=== BeforeMoveToLastPage === | === BeforeMoveToLastPage === | ||
<code class="mwt-code" >AnswerSheet.bind('beforeMoveToLastPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | <code class="mwt-code" >AnswerSheet.bind('beforeMoveToLastPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | ||
Line 45: | Line 50: | ||
<code class="mwt-code" >AnswerSheet.bind('beforeShowPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | <code class="mwt-code" >AnswerSheet.bind('beforeShowPage', function(ev, answerSheet, questionnaire) { /* put code here */ });</code> | ||
− | See [Sequence of events at new questions] | + | See [[#Sequence of events at new questions]] |
=== AfterShowPage === | === AfterShowPage === | ||
<code class="mwt-code" >AnswerSheet.bind('afterShowPage', function(ev, answerSheet, questionnaire, element) { /* put code here */ });</code> | <code class="mwt-code" >AnswerSheet.bind('afterShowPage', function(ev, answerSheet, questionnaire, element) { /* put code here */ });</code> | ||
− | See [Sequence of events at new questions]. | + | See [[#Sequence of events at new questions]]. |
The <code class="mwt-code" >element</code> contains the top-most html element of the page that holds all of the questions. This element contains each questions top html element as a child. | The <code class="mwt-code" >element</code> contains the top-most html element of the page that holds all of the questions. This element contains each questions top html element as a child. | ||
Line 55: | Line 60: | ||
<code class="mwt-code" >AnswerSheet.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });</code> | <code class="mwt-code" >AnswerSheet.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });</code> | ||
− | See [Sequence of events at goto another question]. | + | See [[#Sequence of events at goto another question]]. |
<code class="mwt-code" >State</code> is an object with 1 property <code class="mwt-code" >.valid</code>, which contains the result of the <code class="mwt-code" >.validate()</code> on each question. This may be updated during the call to <code class="mwt-code" >afterValidateQuestion</code>to overwrite the state. | <code class="mwt-code" >State</code> is an object with 1 property <code class="mwt-code" >.valid</code>, which contains the result of the <code class="mwt-code" >.validate()</code> on each question. This may be updated during the call to <code class="mwt-code" >afterValidateQuestion</code>to overwrite the state. | ||
Line 61: | Line 66: | ||
<code class="mwt-code" >AnswerSheet.bind('afterCreateDataState', function(ev, data) { /* put code here */ });</code> | <code class="mwt-code" >AnswerSheet.bind('afterCreateDataState', function(ev, data) { /* put code here */ });</code> | ||
− | See [Sequence of events at goto another question]. | + | See [[#Sequence of events at goto another question]]. |
The <code class="mwt-code" >data</code> contains the actual data that is sent to the server. | The <code class="mwt-code" >data</code> contains the actual data that is sent to the server. | ||
Line 77: | Line 82: | ||
<code class="mwt-code" >Question.bind('beforeShowQuestion', function(ev, question) { /* put code here */ });</code> | <code class="mwt-code" >Question.bind('beforeShowQuestion', function(ev, question) { /* put code here */ });</code> | ||
− | See [Sequence of events at new questions] | + | See [[#Sequence of events at new questions]] |
=== AfterShowQuestion === | === AfterShowQuestion === | ||
<code class="mwt-code" >Question.bind('afterShowQuestion', function(ev, question, element) { /* put code here */ });</code> | <code class="mwt-code" >Question.bind('afterShowQuestion', function(ev, question, element) { /* put code here */ });</code> | ||
− | See [Sequence of events at new questions]. | + | See [[#Sequence of events at new questions]]. |
The <code class="mwt-code" >element</code> contains the top-most html element of the page that holds the question. | The <code class="mwt-code" >element</code> contains the top-most html element of the page that holds the question. | ||
Line 87: | Line 92: | ||
<code class="mwt-code" >Question.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });</code> | <code class="mwt-code" >Question.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });</code> | ||
− | See [Sequence of events at goto another question]. | + | See [[#Sequence of events at goto another question]]. |
<code class="mwt-code" >State</code> is an object with 1 property <code class="mwt-code" >.valid</code>, which contains the result of the <code class="mwt-code" >.validate()</code>. This may be updated during the call to <code class="mwt-code" >afterValidateQuestion</code>to overwrite the state. | <code class="mwt-code" >State</code> is an object with 1 property <code class="mwt-code" >.valid</code>, which contains the result of the <code class="mwt-code" >.validate()</code>. This may be updated during the call to <code class="mwt-code" >afterValidateQuestion</code>to overwrite the state. | ||
Line 98: | Line 103: | ||
All grid-type questions will add the <code class="mwt-code" >subquestion</code> parameter on the call. | All grid-type questions will add the <code class="mwt-code" >subquestion</code> parameter on the call. | ||
− | == Reference other questions | + | == Reference other questions == |
It is possible to reference other questions on the same page, by using the Question Label of this other questions, example: <code class="mwt-code" >Q1.attr('answerOptionColumns')</code> | It is possible to reference other questions on the same page, by using the Question Label of this other questions, example: <code class="mwt-code" >Q1.attr('answerOptionColumns')</code> | ||
+ | |||
== Sequence of events at new questions == | == Sequence of events at new questions == | ||
The primary loop of updates occur when the viewer has visited the server (or is starting up) and got a new batch of questions to display | The primary loop of updates occur when the viewer has visited the server (or is starting up) and got a new batch of questions to display | ||
Line 118: | Line 124: | ||
8. Execute <code class="mwt-code" >afterShowPage</code>event handlers | 8. Execute <code class="mwt-code" >afterShowPage</code>event handlers | ||
− | == Sequence of events at goto another question | + | |
+ | == Sequence of events at goto another question == | ||
Once the viewer is asked to change page, a sequence of events occur before calling the server and getting the next questions to display. | Once the viewer is asked to change page, a sequence of events occur before calling the server and getting the next questions to display. | ||
Line 145: | Line 152: | ||
11. Send the <code class="mwt-code" >moveToNextQuestion</code> command to server with the data state | 11. Send the <code class="mwt-code" >moveToNextQuestion</code> command to server with the data state | ||
− | 12. Trigger [Sequence of events at goto another question] | + | 12. Trigger [[#Sequence of events at goto another question]] |
Revision as of 08:19, 14 September 2022
Contents
Questionnaire scripting
The context for questionnaire scripts defines three variables which should not be overwritten if the questionnaire author intends to handle events which the new viewer triggers - these variables are:
● Viewer
● Questionnaire
● AnswerSheet (alias for this)
Answer sheet event handlers
Answer sheet event handlers are defined by calling the .bind()
method on the AnswerSheet
object.
Example:
AnswerSheet.bind('beforeCompleted', function(ev, answerSheet, questionnaire) { /* put code here */ });
The lifetime of the event handler if defined in the questionnaire script section is the until the viewer is closed.
Notice that the questionnaire script section is not used if the questionnaire is reached by using GotoQuestionnaire.
If the event handler is called in a question script section, the lifetime is from visiting the page with the given question until the viewer is closed. It it therefore NOT recommended to attach answer sheet event handlers in a question script section.
The following sections describe the actual events available.
BeforeCompleted
AnswerSheet.bind('beforeCompleted', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is called when the respondent clicks the "close" button, or when an countdown completes on the last page, but before the validation and sending the closeAnswerSheet
command to the server.
AfterCompleted
AnswerSheet.bind('afterCompleted', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is called after beforeCompleted
has completed.
BeforeMoveToNextPage
AnswerSheet.bind('beforeMoveToNextPage', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is called before validation of the page before the moveToNextPage
command is sent to the server.
BeforeMoveToPreviousPage
AnswerSheet.bind('beforeMoveToPreviousPage', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is called before validation of the page before the moveToPreviousPage
command is sent to the server.
BeforeMoveToFirstPage
AnswerSheet.bind('beforeMoveToFirstPage', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is only used in the pre-viewer.
BeforeMoveToLastPage
AnswerSheet.bind('beforeMoveToLastPage', function(ev, answerSheet, questionnaire) { /* put code here */ });
This is only used in the pre-viewer.
BeforeShowPage
AnswerSheet.bind('beforeShowPage', function(ev, answerSheet, questionnaire) { /* put code here */ });
See #Sequence of events at new questions
AfterShowPage
AnswerSheet.bind('afterShowPage', function(ev, answerSheet, questionnaire, element) { /* put code here */ });
See #Sequence of events at new questions.
The element
contains the top-most html element of the page that holds all of the questions. This element contains each questions top html element as a child.
AfterValidateQuestion
AnswerSheet.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });
See #Sequence of events at goto another question.
State
is an object with 1 property .valid
, which contains the result of the .validate()
on each question. This may be updated during the call to afterValidateQuestion
to overwrite the state.
AfterCreateDataState
AnswerSheet.bind('afterCreateDataState', function(ev, data) { /* put code here */ });
See #Sequence of events at goto another question.
The data
contains the actual data that is sent to the server.
Question event handlers
Similar to the answersheet event handlers, it is also possible to specify events for each question.
Example:Question.bind('beforeShowQuestion', function(ev, question) { /* put code here*/ });
It is not possible to define these events in the questionnaire script section, only in each questions individual script section.
The lifetime of the event handler viewer proceeds to the next page.
The following sections describe the actual events available.
BeforeShowQuestion
Question.bind('beforeShowQuestion', function(ev, question) { /* put code here */ });
See #Sequence of events at new questions
AfterShowQuestion
Question.bind('afterShowQuestion', function(ev, question, element) { /* put code here */ });
See #Sequence of events at new questions.
The element
contains the top-most html element of the page that holds the question.
AfterValidateQuestion
Question.bind('afterValidateQuestion', function(ev, question, state) { state.valid = /* put code here */; });
See #Sequence of events at goto another question.
State
is an object with 1 property .valid
, which contains the result of the .validate()
. This may be updated during the call to afterValidateQuestion
to overwrite the state.
AnswerChanged
Question.bind('answerChanged', function(ev, question) { /* put code here */ });
Question.bind('answerChanged', function(ev, question, subquestion) { /* put code here */ });
Whenever the respondent (or script) change the answers to a question, this event is called.
All grid-type questions will add the subquestion
parameter on the call.
Reference other questions
It is possible to reference other questions on the same page, by using the Question Label of this other questions, example: Q1.attr('answerOptionColumns')
Sequence of events at new questions
The primary loop of updates occur when the viewer has visited the server (or is starting up) and got a new batch of questions to display
1. Got new questions to display
2. Update answer sheet state
3. Run individual question scripts
4. Execute each question beforeShowQuestion
event handlers
5. Execute beforeShowPage
event handlers
6. Render html
7. Execute each question afterShowQuestion
event handlers
8. Execute afterShowPage
event handlers
Sequence of events at goto another question
Once the viewer is asked to change page, a sequence of events occur before calling the server and getting the next questions to display.
Here shown for goto next page, but similar events happen for before,first and last.
1. Check if we can goto next page
2. Set state to Normal
3. Execute beforeMoveToNextPage
event handlers
4. For each question:
5. Execute .validate() on question
6. Execute afterValidateQuestion
answer sheet event handlers
7. Execute afterValidateQuestion
question event handlers
8. After all questions are processed, and if determined valid
9. Execute createDataState()
on each question
10. Execute afterCreateDataState
event handlers
11. Send the moveToNextQuestion
command to server with the data state
12. Trigger #Sequence of events at goto another question