source: Dev/trunk/client/qed/main-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: 2.4 KB
Line 
1require([
2    'dojo/date',
3    'dojo/date/locale',
4    'dojo/hash',
5    'dojo/parser',
6    'qed/store',
7    'qed/app/Content',
8    'qed/app/Page',
9    'qed/app/Path',
10    'qed/lib/async',
11    'qed/model/classes/Response',
12    'qed/model/classes/SurveyRun',
13    'qed/pages/response',
14    'dojo/domReady!',
15    'qed/stddeps'
16],function(date,locale,hash,parser,store,Content,Page,Path,async,Response,SurveyRun,response) {
17    parser.parse();
18    Content.startup();
19
20    function error(msg) {
21        Content.set(new Page({
22            templateString: "<div>"+msg+"</div>"
23        }));
24    }
25
26    var path = new Path('/:surveyRunId');
27    var params = path.match(hash());
28    params.options = params.options || {};
29
30    if ( !params || !params.surveyRunId ) {
31        error("Something is wrong with the URL, don't know what survey to show you. Sorry.");
32        return;
33    }
34
35    var surveyRunId = params.surveyRunId;
36
37    function checkDates(surveyRun) {
38        var now = new Date();
39        var startDate = SurveyRun.StartDate.get(surveyRun);
40        var endDate = SurveyRun.EndDate.get(surveyRun);
41        if ( startDate && date.compare(startDate,now) > 0 ) {
42            error("This survey will start on "+locale.format(startDate,'date'));
43            throw false;
44        }
45        if ( endDate && date.compare(now,endDate) > 0 ) {
46            error("This survey ended on "+locale.format(endDate,'date'));
47            throw false;
48        }
49    }
50
51    store.get(surveyRunId)
52    .then(function(surveyRun){
53        checkDates(surveyRun);
54        if ( params.options.id ) {
55            return params.options.id;
56        } else {
57            if ( surveyRun.mode === "open") {
58                var response = Response.create();
59                response.surveyRunId = surveyRunId;
60                return store.put(response).then(function(res){
61                    return store.getIdentity(res);
62                });
63            } else {
64                error("Cannot respond to closed survey without response id. Sorry.");
65                throw false;
66            }
67        }
68        return surveyRun;
69    },function(){
70        error("No running survey found for the given id. Sorry.");
71        throw false;
72    })
73    .then(function(responseId){
74        hash(Path.format("/"+surveyRunId,{ id: responseId }));
75        Content.set(new response({
76            surveyRunId: surveyRunId,
77            options: {
78                id: responseId
79            }
80        }));
81    });
82});
Note: See TracBrowser for help on using the repository browser.