Index: lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/web/action/MonitoringAction.java =================================================================== RCS file: /usr/local/cvsroot/lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/web/action/MonitoringAction.java,v diff -u -r1.23.2.3 -r1.23.2.4 --- lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/web/action/MonitoringAction.java 11 Feb 2016 03:15:17 -0000 1.23.2.3 +++ lams_tool_survey/src/java/org/lamsfoundation/lams/tool/survey/web/action/MonitoringAction.java 29 Apr 2016 12:49:36 -0000 1.23.2.4 @@ -76,476 +76,551 @@ public class MonitoringAction extends Action { - public ISurveyService surveyService; - private static final String MSG_LABEL_QUESTION = "label.question"; - private static final String MSG_LABEL_OPEN_RESPONSE = "label.open.response"; - private static final String MSG_LABEL_SESSION_NAME = "label.session.name"; - private static final String MSG_LABEL_POSSIBLE_ANSWERS = "message.possible.answers"; - private static final String MSG_LABEL_LEARNER = "label.learner"; + public ISurveyService surveyService; + private static final String MSG_LABEL_QUESTION = "label.question"; + private static final String MSG_LABEL_OPEN_RESPONSE = "label.open.response"; + private static final String MSG_LABEL_SESSION_NAME = "label.session.name"; + private static final String MSG_LABEL_POSSIBLE_ANSWERS = "message.possible.answers"; + private static final String MSG_LABEL_LEARNER_NAME = "monitoring.label.user.name"; + private static final String MSG_LABEL_LOGIN = "monitoring.label.user.loginname"; - public static Logger log = Logger.getLogger(MonitoringAction.class); + public static Logger log = Logger.getLogger(MonitoringAction.class); - @Override - public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException, JSONException { - String param = mapping.getParameter(); + @Override + public ActionForward execute(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException, JSONException { + String param = mapping.getParameter(); - if (param.equals("summary")) { - return summary(mapping, form, request, response); - } + if (param.equals("summary")) { + return summary(mapping, form, request, response); + } - if (param.equals("listAnswers")) { - return listAnswers(mapping, form, request, response); - } - if (param.equals("getAnswersJSON")) { - return getAnswersJSON(mapping, form, request, response); - } + if (param.equals("listAnswers")) { + return listAnswers(mapping, form, request, response); + } + if (param.equals("getAnswersJSON")) { + return getAnswersJSON(mapping, form, request, response); + } - if (param.equals("listReflections")) { - return listReflections(mapping, form, request, response); - } - if (param.equals("getReflectionsJSON")) { - return getReflectionsJSON(mapping, form, request, response); - } + if (param.equals("listReflections")) { + return listReflections(mapping, form, request, response); + } + if (param.equals("getReflectionsJSON")) { + return getReflectionsJSON(mapping, form, request, response); + } - if (param.equals("exportSurvey")) { - return exportSurvey(mapping, form, request, response); - } + if (param.equals("exportSurvey")) { + return exportSurvey(mapping, form, request, response); + } - if (param.equals("setSubmissionDeadline")) { - return setSubmissionDeadline(mapping, form, request, response); + if (param.equals("setSubmissionDeadline")) { + return setSubmissionDeadline(mapping, form, request, response); + } + + return mapping.findForward(SurveyConstants.ERROR); } - return mapping.findForward(SurveyConstants.ERROR); - } + /** + * Summary page action. + * + * @param mapping + * @param form + * @param request + * @param response + * @return + */ - /** - * Summary page action. - * - * @param mapping - * @param form - * @param request - * @param response - * @return - */ + private ActionForward summary(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) { - private ActionForward summary(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { + // get session from shared session. + HttpSession ss = SessionManager.getSession(); - // get session from shared session. - HttpSession ss = SessionManager.getSession(); + // initial Session Map + SessionMap sessionMap = new SessionMap(); + request.getSession() + .setAttribute(sessionMap.getSessionID(), sessionMap); + request.setAttribute(SurveyConstants.ATTR_SESSION_MAP_ID, + sessionMap.getSessionID()); + // save contentFolderID into session + sessionMap.put(AttributeNames.PARAM_CONTENT_FOLDER_ID, WebUtil + .readStrParam(request, AttributeNames.PARAM_CONTENT_FOLDER_ID)); - // initial Session Map - SessionMap sessionMap = new SessionMap(); - request.getSession().setAttribute(sessionMap.getSessionID(), sessionMap); - request.setAttribute(SurveyConstants.ATTR_SESSION_MAP_ID, sessionMap.getSessionID()); - // save contentFolderID into session - sessionMap.put(AttributeNames.PARAM_CONTENT_FOLDER_ID, - WebUtil.readStrParam(request, AttributeNames.PARAM_CONTENT_FOLDER_ID)); + request.setAttribute("initialTabId", WebUtil.readLongParam(request, + AttributeNames.PARAM_CURRENT_TAB, true)); - request.setAttribute("initialTabId", WebUtil.readLongParam(request, AttributeNames.PARAM_CURRENT_TAB, true)); + Long contentId = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_CONTENT_ID); + ISurveyService service = getSurveyService(); - Long contentId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_CONTENT_ID); - ISurveyService service = getSurveyService(); + // get summary + SortedMap> summary = service + .getSummary(contentId); - // get summary - SortedMap> summary = service.getSummary(contentId); + // get survey + Survey survey = service.getSurveyByContentId(contentId); - // get survey - Survey survey = service.getSurveyByContentId(contentId); + // get statistic + SortedMap statis = service + .getStatistic(contentId); - // get statistic - SortedMap statis = service.getStatistic(contentId); + // cache into sessionMap + sessionMap.put(SurveyConstants.ATTR_SUMMARY_LIST, summary); + sessionMap.put(SurveyConstants.ATTR_STATISTIC_LIST, statis); + sessionMap.put(SurveyConstants.PAGE_EDITABLE, new Boolean( + SurveyWebUtils.isSurveyEditable(survey))); + sessionMap.put(SurveyConstants.ATTR_SURVEY, survey); + sessionMap.put(AttributeNames.PARAM_TOOL_CONTENT_ID, contentId); + sessionMap.put(SurveyConstants.ATTR_IS_GROUPED_ACTIVITY, + service.isGroupedActivity(contentId)); - // cache into sessionMap - sessionMap.put(SurveyConstants.ATTR_SUMMARY_LIST, summary); - sessionMap.put(SurveyConstants.ATTR_STATISTIC_LIST, statis); - sessionMap.put(SurveyConstants.PAGE_EDITABLE, new Boolean(SurveyWebUtils.isSurveyEditable(survey))); - sessionMap.put(SurveyConstants.ATTR_SURVEY, survey); - sessionMap.put(AttributeNames.PARAM_TOOL_CONTENT_ID, contentId); - sessionMap.put(SurveyConstants.ATTR_IS_GROUPED_ACTIVITY, service.isGroupedActivity(contentId)); + // check if there is submission deadline + Date submissionDeadline = survey.getSubmissionDeadline(); - // check if there is submission deadline - Date submissionDeadline = survey.getSubmissionDeadline(); + if (submissionDeadline != null) { - if (submissionDeadline != null) { + UserDTO learnerDto = (UserDTO) ss.getAttribute(AttributeNames.USER); + TimeZone learnerTimeZone = learnerDto.getTimeZone(); + Date tzSubmissionDeadline = DateUtil.convertToTimeZoneFromDefault( + learnerTimeZone, submissionDeadline); + MonitoringAction.log.info("Time:" + tzSubmissionDeadline.getTime()); + // store submission deadline to sessionMap + sessionMap.put(SurveyConstants.ATTR_SUBMISSION_DEADLINE, + tzSubmissionDeadline.getTime()); - UserDTO learnerDto = (UserDTO) ss.getAttribute(AttributeNames.USER); - TimeZone learnerTimeZone = learnerDto.getTimeZone(); - Date tzSubmissionDeadline = DateUtil.convertToTimeZoneFromDefault(learnerTimeZone, submissionDeadline); - MonitoringAction.log.info("Time:" + tzSubmissionDeadline.getTime()); - // store submission deadline to sessionMap - sessionMap.put(SurveyConstants.ATTR_SUBMISSION_DEADLINE, tzSubmissionDeadline.getTime()); + } + return mapping.findForward(SurveyConstants.SUCCESS); } - return mapping.findForward(SurveyConstants.SUCCESS); - } + private ActionForward listAnswers(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) { + Long sessionId = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_SESSION_ID); + Long questionUid = WebUtil.readLongParam(request, + SurveyConstants.ATTR_QUESTION_UID); - private ActionForward listAnswers(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { - Long sessionId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID); - Long questionUid = WebUtil.readLongParam(request, SurveyConstants.ATTR_QUESTION_UID); + // get user list + ISurveyService service = getSurveyService(); + SurveyQuestion question = service.getQuestion(questionUid); + request.setAttribute(SurveyConstants.ATTR_QUESTION, question); + request.setAttribute(AttributeNames.PARAM_TOOL_SESSION_ID, sessionId); + return mapping.findForward(SurveyConstants.SUCCESS); + } - // get user list - ISurveyService service = getSurveyService(); - SurveyQuestion question = service.getQuestion(questionUid); - request.setAttribute(SurveyConstants.ATTR_QUESTION, question); - request.setAttribute(AttributeNames.PARAM_TOOL_SESSION_ID, sessionId); - return mapping.findForward(SurveyConstants.SUCCESS); - } - private ActionForward getAnswersJSON(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws JSONException, IOException { - - Long sessionId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID); - Long questionUid = WebUtil.readLongParam(request, SurveyConstants.ATTR_QUESTION_UID); + private ActionForward getAnswersJSON(ActionMapping mapping, + ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws JSONException, IOException { - // paging parameters of tablesorter - int size = WebUtil.readIntParam(request, "size"); - int page = WebUtil.readIntParam(request, "page"); - Integer sortByName = WebUtil.readIntParam(request, "column[0]", true); - String searchString = request.getParameter("fcol[0]"); - - int sorting = SurveyConstants.SORT_BY_DEAFAULT; - if ( sortByName != null ) - sorting = sortByName.equals(0) ? SurveyConstants.SORT_BY_NAME_ASC : SurveyConstants.SORT_BY_NAME_DESC; + Long sessionId = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_SESSION_ID); + Long questionUid = WebUtil.readLongParam(request, + SurveyConstants.ATTR_QUESTION_UID); - //return user list according to the given sessionID - ISurveyService service = getSurveyService(); - SurveyQuestion question = service.getQuestion(questionUid); - List users = service.getQuestionAnswersForTablesorter(sessionId, questionUid, page, size, sorting, searchString); - - JSONArray rows = new JSONArray(); - JSONObject responsedata = new JSONObject(); - responsedata.put("total_rows", service.getCountUsersBySession(sessionId, searchString)); + // paging parameters of tablesorter + int size = WebUtil.readIntParam(request, "size"); + int page = WebUtil.readIntParam(request, "page"); + Integer sortByName = WebUtil.readIntParam(request, "column[0]", true); + String searchString = request.getParameter("fcol[0]"); - for (Object[] userAndAnswers: users) { + int sorting = SurveyConstants.SORT_BY_DEAFAULT; + if (sortByName != null) + sorting = sortByName.equals(0) ? SurveyConstants.SORT_BY_NAME_ASC + : SurveyConstants.SORT_BY_NAME_DESC; - JSONObject responseRow = new JSONObject(); - - SurveyUser user = (SurveyUser) userAndAnswers[0]; - responseRow.put(SurveyConstants.ATTR_USER_NAME, StringEscapeUtils.escapeHtml(user.getLastName() + " " + user.getFirstName())); + // return user list according to the given sessionID + ISurveyService service = getSurveyService(); + SurveyQuestion question = service.getQuestion(questionUid); + List users = service.getQuestionAnswersForTablesorter( + sessionId, questionUid, page, size, sorting, searchString); - if ( userAndAnswers.length > 1 && userAndAnswers[1] != null) { - responseRow.put("choices", SurveyWebUtils.getChoiceList((String)userAndAnswers[1])); - } - if ( userAndAnswers.length > 2 && userAndAnswers[2] != null) { - // Data is handled differently in learner depending on whether it is an extra text added - // to a multiple choice, or a free text entry. So need to handle the output differently. - // See learner/result.jsp and its handling of question.type == 3 vs question.appendText - String answer; - if ( question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY ) { - // don't escape as it was escaped & BR'd before saving - answer = (String)userAndAnswers[2]; - } else { - // need to escape it, as it isn't escaped in the database - answer = StringEscapeUtils.escapeHtml((String)userAndAnswers[2]); - answer = answer.replaceAll("\n", "
"); + JSONArray rows = new JSONArray(); + JSONObject responsedata = new JSONObject(); + responsedata.put("total_rows", + service.getCountUsersBySession(sessionId, searchString)); + + for (Object[] userAndAnswers : users) { + + JSONObject responseRow = new JSONObject(); + + SurveyUser user = (SurveyUser) userAndAnswers[0]; + responseRow.put( + SurveyConstants.ATTR_USER_NAME, + StringEscapeUtils.escapeHtml(user.getLastName() + " " + + user.getFirstName())); + + if (userAndAnswers.length > 1 && userAndAnswers[1] != null) { + responseRow.put("choices", SurveyWebUtils + .getChoiceList((String) userAndAnswers[1])); + } + if (userAndAnswers.length > 2 && userAndAnswers[2] != null) { + // Data is handled differently in learner depending on whether + // it is an extra text added + // to a multiple choice, or a free text entry. So need to handle + // the output differently. + // See learner/result.jsp and its handling of question.type == 3 + // vs question.appendText + String answer; + if (question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { + // don't escape as it was escaped & BR'd before saving + answer = (String) userAndAnswers[2]; + } else { + // need to escape it, as it isn't escaped in the database + answer = StringEscapeUtils + .escapeHtml((String) userAndAnswers[2]); + answer = answer.replaceAll("\n", "
"); + } + responseRow.put("answerText", answer); + } + rows.put(responseRow); } - responseRow.put("answerText", answer); - } - rows.put(responseRow); + responsedata.put("rows", rows); + response.setContentType("application/json;charset=utf-8"); + response.getWriter().print(new String(responsedata.toString())); + return null; } - responsedata.put("rows", rows); - response.setContentType("application/json;charset=utf-8"); - response.getWriter().print(new String(responsedata.toString())); - return null; - } - private ActionForward listReflections(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { + private ActionForward listReflections(ActionMapping mapping, + ActionForm form, HttpServletRequest request, + HttpServletResponse response) { - Long sessionId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID); - - ISurveyService service = getSurveyService(); - Survey survey = service.getSurveyBySessionId(sessionId); - - request.setAttribute(AttributeNames.PARAM_TOOL_SESSION_ID, sessionId); - return mapping.findForward(SurveyConstants.SUCCESS); - } - - private ActionForward getReflectionsJSON(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) throws JSONException, IOException { - - Long sessionId = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID); + Long sessionId = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_SESSION_ID); - // paging parameters of tablesorter - int size = WebUtil.readIntParam(request, "size"); - int page = WebUtil.readIntParam(request, "page"); - Integer sortByName = WebUtil.readIntParam(request, "column[0]", true); - String searchString = request.getParameter("fcol[0]"); - - int sorting = SurveyConstants.SORT_BY_DEAFAULT; - if ( sortByName != null ) - sorting = sortByName.equals(0) ? SurveyConstants.SORT_BY_NAME_ASC : SurveyConstants.SORT_BY_NAME_DESC; + ISurveyService service = getSurveyService(); + Survey survey = service.getSurveyBySessionId(sessionId); - //return user list according to the given sessionID - ISurveyService service = getSurveyService(); - List users = service.getUserReflectionsForTablesorter(sessionId, page, size, sorting, searchString); - - JSONArray rows = new JSONArray(); - JSONObject responsedata = new JSONObject(); - responsedata.put("total_rows", service.getCountUsersBySession(sessionId, searchString)); + request.setAttribute(AttributeNames.PARAM_TOOL_SESSION_ID, sessionId); + return mapping.findForward(SurveyConstants.SUCCESS); + } - for (Object[] userAndReflection : users) { + private ActionForward getReflectionsJSON(ActionMapping mapping, + ActionForm form, HttpServletRequest request, + HttpServletResponse response) throws JSONException, IOException { - JSONObject responseRow = new JSONObject(); - - SurveyUser user = (SurveyUser) userAndReflection[0]; - responseRow.put(SurveyConstants.ATTR_USER_NAME, StringEscapeUtils.escapeHtml(user.getLastName() + " " + user.getFirstName())); + Long sessionId = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_SESSION_ID); - if ( userAndReflection.length > 1 && userAndReflection[1] != null) { - String reflection = StringEscapeUtils.escapeHtml((String)userAndReflection[1]); - responseRow.put(SurveyConstants.ATTR_REFLECTION, reflection.replaceAll("\n", "
")); - } - - rows.put(responseRow); + // paging parameters of tablesorter + int size = WebUtil.readIntParam(request, "size"); + int page = WebUtil.readIntParam(request, "page"); + Integer sortByName = WebUtil.readIntParam(request, "column[0]", true); + String searchString = request.getParameter("fcol[0]"); + + int sorting = SurveyConstants.SORT_BY_DEAFAULT; + if (sortByName != null) + sorting = sortByName.equals(0) ? SurveyConstants.SORT_BY_NAME_ASC + : SurveyConstants.SORT_BY_NAME_DESC; + + // return user list according to the given sessionID + ISurveyService service = getSurveyService(); + List users = service.getUserReflectionsForTablesorter( + sessionId, page, size, sorting, searchString); + + JSONArray rows = new JSONArray(); + JSONObject responsedata = new JSONObject(); + responsedata.put("total_rows", + service.getCountUsersBySession(sessionId, searchString)); + + for (Object[] userAndReflection : users) { + + JSONObject responseRow = new JSONObject(); + + SurveyUser user = (SurveyUser) userAndReflection[0]; + responseRow.put( + SurveyConstants.ATTR_USER_NAME, + StringEscapeUtils.escapeHtml(user.getLastName() + " " + + user.getFirstName())); + + if (userAndReflection.length > 1 && userAndReflection[1] != null) { + String reflection = StringEscapeUtils + .escapeHtml((String) userAndReflection[1]); + responseRow.put(SurveyConstants.ATTR_REFLECTION, + reflection.replaceAll("\n", "
")); + } + + rows.put(responseRow); + } + responsedata.put("rows", rows); + response.setContentType("application/json;charset=utf-8"); + response.getWriter().print(new String(responsedata.toString())); + return null; } - responsedata.put("rows", rows); - response.setContentType("application/json;charset=utf-8"); - response.getWriter().print(new String(responsedata.toString())); - return null; - } - - /** - * Export Excel format survey data. - * - * @param mapping - * @param form - * @param request - * @param response - * @return - */ - private ActionForward exportSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { - Long toolSessionID = new Long(WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_SESSION_ID)); - ISurveyService service = getSurveyService(); + /** + * Export Excel format survey data. + * + * @param mapping + * @param form + * @param request + * @param response + * @return + */ + private ActionForward exportSurvey(ActionMapping mapping, ActionForm form, + HttpServletRequest request, HttpServletResponse response) { + Long toolSessionID = new Long(WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_SESSION_ID)); - SortedMap>> groupList = service - .exportBySessionId(toolSessionID); - String errors = null; - MessageService resource = getMessageService(); - try { - // create an empty excel file - Workbook workbook = new SXSSFWorkbook(); - Sheet sheet = workbook.createSheet("Survey"); - sheet.setColumnWidth(0, 5000); - Row row; - Cell cell; - int idx = 0; - Set>>> entries = groupList.entrySet(); - for (Entry>> entry : entries) { - SurveySession session = entry.getKey(); - SortedMap> map = entry.getValue(); - // display survey title, instruction and questions - Survey survey = session.getSurvey(); - // survey title - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(removeHTMLTags(survey.getTitle())); + ISurveyService service = getSurveyService(); - // survey instruction - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(removeHTMLTags(survey.getInstructions())); + SortedMap>> groupList = service + .exportBySessionId(toolSessionID); + String errors = null; + MessageService resource = getMessageService(); + try { + // create an empty excel file + Workbook workbook = new SXSSFWorkbook(); + Sheet sheet = workbook.createSheet("Survey"); + sheet.setColumnWidth(0, 5000); + Row row; + Cell cell; + int idx = 0; + Set>>> entries = groupList + .entrySet(); + for (Entry>> entry : entries) { + SurveySession session = entry.getKey(); + SortedMap> map = entry + .getValue(); + // display survey title, instruction and questions + Survey survey = session.getSurvey(); + // survey title + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(removeHTMLTags(survey.getTitle())); - // display 2 empty row - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(""); - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(""); + // survey instruction + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(removeHTMLTags(survey.getInstructions())); - // display session name - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(resource.getMessage(MonitoringAction.MSG_LABEL_SESSION_NAME)); - cell = row.createCell(1); - cell.setCellValue(removeHTMLTags(session.getSessionName())); + // display 2 empty row + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(""); + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(""); - // begin to display question and its answers - Set>> questionEntries = map.entrySet(); - int questionIdx = 0; - for (Entry> questionEntry : questionEntries) { - // display 1 empty row - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(""); + // display session name + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_SESSION_NAME)); + cell = row.createCell(1); + cell.setCellValue(removeHTMLTags(session.getSessionName())); - questionIdx++; - SurveyQuestion question = questionEntry.getKey(); - List answers = questionEntry.getValue(); + // begin to display question and its answers + Set>> questionEntries = map + .entrySet(); + int questionIdx = 0; + for (Entry> questionEntry : questionEntries) { + // display 1 empty row + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(""); - // display question content - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(resource.getMessage(MonitoringAction.MSG_LABEL_QUESTION) + " " + questionIdx); - cell = row.createCell(1); - cell.setCellValue(removeHTMLTags(question.getDescription())); + questionIdx++; + SurveyQuestion question = questionEntry.getKey(); + List answers = questionEntry.getValue(); - // display options content - Set options = question.getOptions(); + // display question content + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_QUESTION) + + " " + questionIdx); + cell = row.createCell(1); + cell.setCellValue(removeHTMLTags(question.getDescription())); - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(resource.getMessage(MonitoringAction.MSG_LABEL_POSSIBLE_ANSWERS)); + // display options content + Set options = question.getOptions(); - int optionIdx = 0; - for (SurveyOption option : options) { - optionIdx++; - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + optionIdx); - cell = row.createCell(1); - cell.setCellValue(removeHTMLTags(option.getDescription())); - } - if (question.isAppendText() || question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { - optionIdx++; - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + optionIdx); - cell = row.createCell(1); - cell.setCellValue(resource.getMessage(MonitoringAction.MSG_LABEL_OPEN_RESPONSE)); - } + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_POSSIBLE_ANSWERS)); - // display 1 empty row - row = sheet.createRow(idx++); - cell = row.createCell(0); - cell.setCellValue(""); + int optionIdx = 0; + for (SurveyOption option : options) { + optionIdx++; + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + + optionIdx); + cell = row.createCell(1); + cell.setCellValue(removeHTMLTags(option + .getDescription())); + } + if (question.isAppendText() + || question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { + optionIdx++; + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + + optionIdx); + cell = row.createCell(1); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_OPEN_RESPONSE)); + } - // ////////////////////////// - // display answer list - // ////////////////////////// - // first display option title : a1 , a2, a3 etc + // display 1 empty row + row = sheet.createRow(idx++); + cell = row.createCell(0); + cell.setCellValue(""); - int cellIdx = 0; - row = sheet.createRow(idx++); - cell = row.createCell(cellIdx); - cell.setCellValue(resource.getMessage(MonitoringAction.MSG_LABEL_LEARNER)); - // increase one more option number if there are open entry option - int optionsNum = options.size(); - if (question.isAppendText() || question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { - optionsNum++; - } - for (cellIdx = 1; cellIdx <= optionsNum; cellIdx++) { - cell = row.createCell(cellIdx); - cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + cellIdx); - } + // ////////////////////////// + // display answer list + // ////////////////////////// + // first display option title : a1 , a2, a3 etc - // display all users' answers for this question in multiple rows - for (AnswerDTO answer : answers) { - row = sheet.createRow(idx++); - cellIdx = 0; - cell = row.createCell(cellIdx); - cell.setCellValue(answer.getReplier().getLoginName()); - // for answer's options - for (SurveyOption option : options) { - cellIdx++; - cell = row.createCell(cellIdx); - if (answer.getAnswer() == null) { - break; - } - String[] choices = answer.getAnswer().getChoices(); - for (String choice : choices) { - if (StringUtils.equals(choice, option.getUid().toString())) { - cell.setCellValue("X"); + int cellIdx = 0; + row = sheet.createRow(idx++); + cell = row.createCell(cellIdx); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_LOGIN)); + cellIdx++; + cell = row.createCell(cellIdx); + cell.setCellValue(resource + .getMessage(MonitoringAction.MSG_LABEL_LEARNER_NAME)); + + int optionsNum = options.size(); + + int iterOpts; + for (iterOpts = 1; iterOpts <= optionsNum; iterOpts++) { + cellIdx++; + cell = row.createCell(cellIdx); + cell.setCellValue(SurveyConstants.OPTION_SHORT_HEADER + + iterOpts); + } + + // display all users' answers for this question in multiple + // rows + for (AnswerDTO answer : answers) { + row = sheet.createRow(idx++); + cellIdx = 0; + cell = row.createCell(cellIdx); + cell.setCellValue(answer.getReplier().getLoginName()); + cellIdx++; + cell = row.createCell(cellIdx); + cell.setCellValue(answer.getReplier().getLastName() + ", " + answer.getReplier().getFirstName()); + // for answer's options + for (SurveyOption option : options) { + cellIdx++; + cell = row.createCell(cellIdx); + if (answer.getAnswer() == null) { + break; + } + String[] choices = answer.getAnswer().getChoices(); + for (String choice : choices) { + if (StringUtils.equals(choice, option.getUid() + .toString())) { + cell.setCellValue("X"); + } + } + } + // for textEntry option + if (question.isAppendText() + || question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { + cell = row.createCell(++cellIdx); + if (answer.getAnswer() != null) { + cell.setCellValue(removeHTMLTags(answer + .getAnswer().getAnswerText())); + } + } + + } } - } } - // for textEntry option - if (question.isAppendText() || question.getType() == SurveyConstants.QUESTION_TYPE_TEXT_ENTRY) { - cell = row.createCell(++cellIdx); - if (answer.getAnswer() != null) { - cell.setCellValue(removeHTMLTags(answer.getAnswer().getAnswerText())); - } - } - } + String fileName = "lams_survey_" + toolSessionID + ".xlsx"; + response.setContentType("application/x-download"); + response.setHeader("Content-Disposition", "attachment;filename=" + + fileName); + + ServletOutputStream out = response.getOutputStream(); + workbook.write(out); + out.close(); + + } catch (Exception e) { + MonitoringAction.log.error(e); + errors = new ActionMessage("error.monitoring.export.excel", + e.toString()).toString(); } - } - String fileName = "lams_survey_" + toolSessionID + ".xlsx"; - response.setContentType("application/x-download"); - response.setHeader("Content-Disposition", "attachment;filename=" + fileName); - - ServletOutputStream out = response.getOutputStream(); - workbook.write(out); - out.close(); - - } catch (Exception e) { - MonitoringAction.log.error(e); - errors = new ActionMessage("error.monitoring.export.excel", e.toString()).toString(); + if (errors != null) { + try { + PrintWriter out = response.getWriter(); + out.write(errors); + out.flush(); + } catch (IOException e) { + } + } + return null; } - if (errors != null) { - try { - PrintWriter out = response.getWriter(); - out.write(errors); - out.flush(); - } catch (IOException e) { - } - } - return null; - } + /** + * Set Submission Deadline + * + * @param mapping + * @param form + * @param request + * @param response + * @return + */ + public ActionForward setSubmissionDeadline(ActionMapping mapping, + ActionForm form, HttpServletRequest request, + HttpServletResponse response) { + surveyService = getSurveyService(); - /** - * Set Submission Deadline - * - * @param mapping - * @param form - * @param request - * @param response - * @return - */ - public ActionForward setSubmissionDeadline(ActionMapping mapping, ActionForm form, HttpServletRequest request, - HttpServletResponse response) { - surveyService = getSurveyService(); + Long contentID = WebUtil.readLongParam(request, + AttributeNames.PARAM_TOOL_CONTENT_ID); + Survey survey = surveyService.getSurveyByContentId(contentID); - Long contentID = WebUtil.readLongParam(request, AttributeNames.PARAM_TOOL_CONTENT_ID); - Survey survey = surveyService.getSurveyByContentId(contentID); + Long dateParameter = WebUtil.readLongParam(request, + SurveyConstants.ATTR_SUBMISSION_DEADLINE, true); + Date tzSubmissionDeadline = null; + if (dateParameter != null) { + Date submissionDeadline = new Date(dateParameter); + HttpSession ss = SessionManager.getSession(); + UserDTO teacher = (UserDTO) ss.getAttribute(AttributeNames.USER); + TimeZone teacherTimeZone = teacher.getTimeZone(); + tzSubmissionDeadline = DateUtil.convertFromTimeZoneToDefault( + teacherTimeZone, submissionDeadline); + } + survey.setSubmissionDeadline(tzSubmissionDeadline); + surveyService.saveOrUpdateSurvey(survey); - Long dateParameter = WebUtil.readLongParam(request, SurveyConstants.ATTR_SUBMISSION_DEADLINE, true); - Date tzSubmissionDeadline = null; - if (dateParameter != null) { - Date submissionDeadline = new Date(dateParameter); - HttpSession ss = SessionManager.getSession(); - UserDTO teacher = (UserDTO) ss.getAttribute(AttributeNames.USER); - TimeZone teacherTimeZone = teacher.getTimeZone(); - tzSubmissionDeadline = DateUtil.convertFromTimeZoneToDefault(teacherTimeZone, submissionDeadline); + return null; } - survey.setSubmissionDeadline(tzSubmissionDeadline); - surveyService.saveOrUpdateSurvey(survey); - return null; - } + /** + * Removes all the html tags from a string + * + * @param string + * @return + */ + private String removeHTMLTags(String string) { + return string.replaceAll("\\<.*?>", "").replaceAll(" ", " "); + } - /** - * Removes all the html tags from a string - * - * @param string - * @return - */ - private String removeHTMLTags(String string) { - return string.replaceAll("\\<.*?>", "").replaceAll(" ", " "); - } + // ************************************************************************************* + // Private method + // ************************************************************************************* + private ISurveyService getSurveyService() { + WebApplicationContext wac = WebApplicationContextUtils + .getRequiredWebApplicationContext(getServlet() + .getServletContext()); + return (ISurveyService) wac.getBean(SurveyConstants.SURVEY_SERVICE); + } - // ************************************************************************************* - // Private method - // ************************************************************************************* - private ISurveyService getSurveyService() { - WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() - .getServletContext()); - return (ISurveyService) wac.getBean(SurveyConstants.SURVEY_SERVICE); - } - - /** - * Return ResourceService bean. - */ - private MessageService getMessageService() { - WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServlet() - .getServletContext()); - return (MessageService) wac.getBean("lasurvMessageService"); - } + /** + * Return ResourceService bean. + */ + private MessageService getMessageService() { + WebApplicationContext wac = WebApplicationContextUtils + .getRequiredWebApplicationContext(getServlet() + .getServletContext()); + return (MessageService) wac.getBean("lasurvMessageService"); + } }