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