Questionnaire scripting

From Catglobe Wiki
Revision as of 08:11, 14 September 2022 by Administrator (talk | contribs)
Jump to: navigation, search

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 beforeCompletedhas 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 afterValidateQuestionto 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 afterValidateQuestionto 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 beforeShowQuestionevent handlers

5. Execute beforeShowPage event handlers

6. Render html

7. Execute each question afterShowQuestion event handlers

8. Execute afterShowPageevent 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]