source: Dev/trunk/src/client/qed-client/pages/response.js @ 478

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

Changes for response submission & deletion.

File size: 5.5 KB
Line 
1define([
2    "../app/Content",
3    "../app/Page",
4    "../lib/async",
5    "../model/classes/Response",
6    "../model/classes/Survey",
7    "../model/classes/SurveyRun",
8    "../store",
9    "dojo/_base/declare",
10    "dojo/_base/event",
11    "dojo/_base/json",
12    "dojo/_base/lang",
13    "dojo/promise/all",
14    "dojo/request",
15    "dojo/when",
16    "require",
17    "dojo/text!./templates/response.html"
18], function(Content, Page, async, Response, Survey, SurveyRun, store, declare, event, json, lang, all, request, when, require, template) {
19    return declare([Page],{
20        contextRequire: require,
21        templateString: template,
22        response: null,
23        postCreate: function(){
24            this.options = this.options || {};
25        },
26        startup: function() {
27            if ( this._started ) { return; }
28            this.inherited(arguments);
29            this._disableSubmit();
30            var surveyRunId = this.surveyRunId;
31            var responseId = this.options && this.options.id;
32            if ( surveyRunId && responseId ) {
33                this._loadSurveyAndResponse(surveyRunId,responseId)
34                .then(lang.hitch(this, function() {
35                    if ( this.response.publicationDate ) {
36                        this._showInfo("<div>You already submitted your survey and cannot change it anymore. You can still view your answers here.</div>");
37                        this._disableSubmit();
38                    } else {
39                        this._enableSubmit();
40                    }
41                }), lang.hitch(this,function() {
42                    this._showInfo("<div>The url seems to be incorrect, no survey found.</div>");
43                }));
44            } else {
45                throw new Error("No valid uid or survey passed!");
46            }
47        },
48        _loadSurveyAndResponse: function(surveyRunId,responseId){
49            return all([request.get('/api/surveyRuns/'+surveyRunId,{handleAs:'json'}),
50                        request.get('/api/responses/'+responseId,{handleAs:'json'})])
51            .then(lang.hitch(this,function(surveyAndResponse){
52                var surveyRun = surveyAndResponse[0];
53                this.response = surveyAndResponse[1];
54                if ( this.response.surveyRunId !== surveyRunId ) {
55                    throw "Survey does not match the response...";
56                }
57                this.titleNode.innerHTML = Survey.DisplayTitle.get(surveyRun.survey);
58                this.surveyWidget.set('survey', surveyRun.survey);
59                this.surveyWidget.set('value', this.response.answers || {});
60            }));
61        },
62        _enableSubmit: function() {
63            this.submitButton.set('disabled',false);
64            this.continueButton.set('disabled',false);
65            this.cancelButton.set('disabled',false);
66            this.surveyWidget.set('disabled', false);
67        },
68        _disableSubmit: function() {
69            this.submitButton.set('disabled',true);
70            this.continueButton.set('disabled',true);
71            this.cancelButton.set('disabled',true);
72            this.surveyWidget.set('disabled', true);
73        },
74        _showInfo: function(html) {
75            this.infoNode.innerHTML = html;
76        },
77        _getAnswersAndSaveResponse: function() {
78            var answers = this.surveyWidget.get('value');
79            this.response.answers = answers;
80            return request.put('/api/responses/'+store.getIdentity(this.response),{
81                handleAs:'json',
82                data:json.toJson(this.response),
83                headers:{"Content-Type": "application/json"}
84            }).then(lang.hitch(this,function(res){
85                this.response._rev = res.rev;
86                Content.notify("Your response is saved.");
87            }), function(err){
88                Content.notify(err,'error');
89            });
90        },
91        _onSubmit: function(e) {
92            this.response.publicationDate = store.timestamp();
93            this._getAnswersAndSaveResponse()
94            .then(lang.hitch(this,function(){
95                this._showInfo("<div>Thanks for filling in the survey. You cannot edit your answers anymore.</div>");
96                this._disableSubmit();
97            }));
98            if ( e ) { event.stop(e); }
99            return false;
100        },
101        _onContinueLater: function(e) {
102            this._getAnswersAndSaveResponse()
103            .then(lang.hitch(this,function(){
104                this._showInfo("<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>");
105            }));
106            if ( e ) { event.stop(e); }
107            return false;
108        },
109        _onCancel: function(e) {
110            this._disableSubmit();
111            this.surveyWidget.destroy();
112            request('/api/responses/'+store.getIdentity(this.response)+'?rev='+store.getRevision(this.response),{
113                method: 'DELETE',
114                handleAs:'json',
115                data:json.toJson(this.response),
116                headers:{"Content-Type": "application/json"}
117            }).then(lang.hitch(this,function(res){
118                this._showInfo("<div>Your response has been deleted, no answers have been saved.</div>");
119                Content.notify("Your response is deleted.");
120            }), function(err){
121                Content.notify(err,'error');
122            });
123            if ( e ) { event.stop(e); }
124            return false;
125        },
126        _ignoreEvent: function(e) {
127            if ( e ) { event.stop(e); }
128            return false;
129        }
130    });
131});
Note: See TracBrowser for help on using the repository browser.