source: Dev/trunk/client/qed/pages/response.js @ 424

Last change on this file since 424 was 420, checked in by hendrikvanantwerpen, 12 years ago

We can store answers for surveys now!

Introduces SurveyRun?, which can be edited. Workflow not quite clear yet. A
running survey can be accessed to leave a response. When the response
has an ID, it is loaded (used for closed surveys and continuations). A
researcher cannot create responses yet. He should also be able to add
comments to responses (that he creates).

Introduced caching of store requests.

Factored out path matching and formatting.

Put object creation in separate classes, to localize model/storage
dependency. Not consistent at the moment.

File size: 3.1 KB
Line 
1define([
2    'dojo/_base/declare',
3    'dojo/_base/event',
4    'dojo/_base/lang',
5    'dojo/promise/all',
6    'dojo/when',
7    '../store',
8    '../app/Page',
9    '../lib/async',
10    '../model/classes/Response',
11    '../model/classes/Survey',
12    '../model/classes/SurveyRun',
13    'dojo/text!./templates/response.html'
14],function(declare,event,lang,all,when,store,Page,async,Response,Survey,SurveyRun,template){
15    return declare([Page],{
16        templateString: template,
17        response: null,
18        postCreate: function(){
19            this.options = this.options || {};
20        },
21        startup: function() {
22            if ( this._started ) { return; }
23            this.inherited(arguments);
24            this._disableButtons();
25            var surveyRunId = this.surveyRunId;
26            var responseId = this.options && this.options.id;
27            if ( surveyRunId && responseId ) {
28                this._loadSurveyAndResponse(surveyRunId,responseId)
29                .then(lang.hitch(this,"_enableButtons"));
30            } else {
31                throw new Error("No valid uid or survey passed!");
32            }
33        },
34        _loadSurveyAndResponse: function(surveyRunId,responseId){
35            return all([store.get(surveyRunId),store.get(responseId)])
36            .then(lang.hitch(this,function(surveyAndResponse){
37                var surveyRun = surveyAndResponse[0];
38                this.response = surveyAndResponse[1];
39                if ( this.response.surveyRunId !== surveyRunId ) {
40                    throw "Survey does not match the response...";
41                }
42                this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey);
43                this.surveyWidget.set('survey',surveyRun.survey);
44                this.responseForm.set('value',this.response.answers || {});
45            }));
46        },
47        _enableButtons: function() {
48            this.submitButton.set('disabled',false);
49            this.continueButton.set('disabled',false);
50        },
51        _disableButtons: function() {
52            this.submitButton.set('disabled',true);
53            this.continueButton.set('disabled',true);
54        },
55        _getAnswersAndSaveResponse: function() {
56            this.response.answers = this.responseForm.get('value');
57            console.log(this.response);
58            return store.put(this.response);
59        },
60        _onSubmit: function(e) {
61            this.response.publicationDate = store.timestamp();
62            this._getAnswersAndSaveResponse();
63            this.content.domNode.innerHTML = "<div>Thanks for filling in the survey.</div>";
64            e && event.stop(e);
65            return false;
66        },
67        _onContinueLater: function(e) {
68            this._getAnswersAndSaveResponse();
69            this.content.domNode.innerHTML = "<div>To continue with this survey later, just save the URL in the location bar and revisit it later. Your answers will still be there.</div>";
70            e && event.stop(e);
71            return false;
72        },
73        _ignoreEvent: function(e) {
74            e && event.stop(e);
75            return false;
76        }
77    });
78});
Note: See TracBrowser for help on using the repository browser.