[443] | 1 | define([ |
---|
| 2 | 'dojo/date', |
---|
| 3 | 'dojo/date/locale', |
---|
| 4 | 'dojo/hash', |
---|
| 5 | 'dojo/parser', |
---|
| 6 | './store', |
---|
| 7 | './app/Content', |
---|
| 8 | './app/Page', |
---|
| 9 | './app/Path', |
---|
| 10 | './lib/async', |
---|
| 11 | './model/classes/Response', |
---|
| 12 | './model/classes/SurveyRun', |
---|
| 13 | './pages/response', |
---|
| 14 | 'dojo/domReady!', |
---|
| 15 | './stddeps' |
---|
| 16 | ],function(date,locale,hash,parser,store,Content,Page,Path,async,Response,SurveyRun,ResponsePage) { |
---|
| 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 ResponsePage({ |
---|
| 76 | surveyRunId: surveyRunId, |
---|
| 77 | options: { |
---|
| 78 | id: responseId |
---|
| 79 | } |
---|
| 80 | })); |
---|
| 81 | }); |
---|
| 82 | }); |
---|