define([
'dojo/date',
'dojo/date/locale',
'dojo/hash',
'dojo/parser',
'./store',
'./app/Content',
'./app/Page',
'./app/Path',
'./lib/async',
'./model/classes/Response',
'./model/classes/SurveyRun',
'./pages/response',
'dojo/domReady!',
'./stddeps'
],function(date,locale,hash,parser,store,Content,Page,Path,async,Response,SurveyRun,ResponsePage) {
parser.parse();
Content.startup();
function error(msg) {
Content.set(new Page({
templateString: "
"+msg+"
"
}));
}
var path = new Path('/:surveyRunId');
var params = path.match(hash());
params.options = params.options || {};
if ( !params || !params.surveyRunId ) {
error("Something is wrong with the URL, don't know what survey to show you. Sorry.");
return;
}
var surveyRunId = params.surveyRunId;
function checkDates(surveyRun) {
var now = new Date();
var startDate = SurveyRun.StartDate.get(surveyRun);
var endDate = SurveyRun.EndDate.get(surveyRun);
if ( startDate && date.compare(startDate,now) > 0 ) {
error("This survey will start on "+locale.format(startDate,'date'));
throw false;
}
if ( endDate && date.compare(now,endDate) > 0 ) {
error("This survey ended on "+locale.format(endDate,'date'));
throw false;
}
}
store.get(surveyRunId)
.then(function(surveyRun){
checkDates(surveyRun);
if ( params.options.id ) {
return params.options.id;
} else {
if ( surveyRun.mode === "open") {
var response = Response.create();
response.surveyRunId = surveyRunId;
return store.put(response).then(function(res){
return store.getIdentity(res);
});
} else {
error("Cannot respond to closed survey without response id. Sorry.");
throw false;
}
}
return surveyRun;
},function(){
error("No running survey found for the given id. Sorry.");
throw false;
})
.then(function(responseId){
hash(Path.format("/"+surveyRunId,{ id: responseId }));
Content.set(new ResponsePage({
surveyRunId: surveyRunId,
options: {
id: responseId
}
}));
});
});