}
@RequestMapping("question")
public ModelAndView processNextQuestion(HttpSession session, HttpServletRequest request) {
// if there's no progress in session then this means our session expired
QuestionaryProgress progress = (QuestionaryProgress) session.getAttribute("progress");
if (progress == null) {
return new ModelAndView("selection");
}
List<AnswerIndex> userAnswers = new ArrayList<>();
for (Object paramName : request.getParameterMap().keySet()) {
String name = (String) paramName;
if (name.startsWith(PARAM_PREFIX)) {
String stringIdx = name.substring(PARAM_PREFIX.length());
Integer idx = Integer.valueOf(stringIdx);
AnswerIndex ai = new AnswerIndex();
ai.setIndex(idx);
ai.setAnswer(progress.getCurrentQuestion().getAnswers().get(idx));
userAnswers.add(ai);
}
}
progress.getAnswers().add(userAnswers);
if (progress.getCurrentQuestionIndex() == progress.getQuestionary().getQuestions().size() - 1) {
PercentileScore ps = new PercentileScore();
Score score = ps.evaluate(progress.getQuestionary(), progress.getAnswers());
User user = (User) session.getAttribute("user");
QuestionaryResult result = new QuestionaryResult();
result.setUserId(user.getId());
result.setDate(new Date());
result.setQuestionaryName(progress.getQuestionary().getName());
result.setResult(score.getValue());
resultsService.addResult(result);
return new ModelAndView("results", "score", score);
} else {
progress.setCurrentQuestionIndex(progress.getCurrentQuestionIndex() + 1);
int prog = 100 * progress.getCurrentQuestionIndex() / progress.getQuestionary().getQuestions().size();
Map<String, Object> model = new HashMap<String, Object>();
model.put("question", progress.getCurrentQuestion());
model.put("progress", prog);
return new ModelAndView("question", model);
}
}