Index: lams_central/src/java/org/lamsfoundation/lams/gradebook/service/GradeBookService.java =================================================================== diff -u -r7895606feec6effedc08860e4e1bb7f5157109c5 -rf4328ca969294c06bf12815f299236c17398c99c --- lams_central/src/java/org/lamsfoundation/lams/gradebook/service/GradeBookService.java (.../GradeBookService.java) (revision 7895606feec6effedc08860e4e1bb7f5157109c5) +++ lams_central/src/java/org/lamsfoundation/lams/gradebook/service/GradeBookService.java (.../GradeBookService.java) (revision f4328ca969294c06bf12815f299236c17398c99c) @@ -26,22 +26,24 @@ import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.SortedMap; import org.apache.log4j.Logger; import org.lamsfoundation.lams.gradebook.GradeBookUserActivity; import org.lamsfoundation.lams.gradebook.GradeBookUserLesson; import org.lamsfoundation.lams.gradebook.dao.IGradeBookDAO; -import org.lamsfoundation.lams.gradebook.dto.GradeBookActivityDTO; -import org.lamsfoundation.lams.gradebook.dto.GradeBookGridRow; -import org.lamsfoundation.lams.gradebook.dto.GradeBookUserDTO; +import org.lamsfoundation.lams.gradebook.dto.GBActivityGridRowDTO; +import org.lamsfoundation.lams.gradebook.dto.GBUserGridRowDTO; +import org.lamsfoundation.lams.gradebook.dto.GradeBookGridRowDTO; import org.lamsfoundation.lams.learningdesign.Activity; import org.lamsfoundation.lams.learningdesign.CompetenceMapping; import org.lamsfoundation.lams.learningdesign.ToolActivity; import org.lamsfoundation.lams.lesson.LearnerProgress; import org.lamsfoundation.lams.lesson.Lesson; import org.lamsfoundation.lams.monitoring.service.IMonitoringService; +import org.lamsfoundation.lams.tool.OutputType; import org.lamsfoundation.lams.tool.ToolOutput; import org.lamsfoundation.lams.tool.ToolOutputDefinition; import org.lamsfoundation.lams.tool.ToolSession; @@ -66,19 +68,24 @@ private IGradeBookDAO gradeBookDAO; /** - * Gets the user gradebook and outputs data for a given activity + * Gets a list of GradeBookGridRowDTO for a given lesson and learner * + * The result should be used for the "user view" of gradebook, where users + * are listed, then expanding a sub-grid will bring up the activites + * + * the mark applies to the ebture kessib + * * @param lesson * @param learner * @return Collection */ @SuppressWarnings("unchecked") - public Collection getUserGradeBookActivityDTOs(Lesson lesson, User learner) { + public List getUserGradeBookActivityDTOs(Lesson lesson, User learner) { - logger.debug("Getting gradebook data for lesson: " + lesson.getLessonId() + ". For user: " + logger.debug("Getting gradebook user data for lesson: " + lesson.getLessonId() + ". For user: " + learner.getUserId()); - Collection gradeBookActivityDTOs = new ArrayList(); + List gradeBookActivityDTOs = new ArrayList(); LearnerProgress learnerProgress = monitoringService.getLearnerProgress(learner.getUserId(), lesson .getLessonId()); @@ -98,33 +105,240 @@ .getActivityId()); if (firstActivity.isToolActivity() && firstActivity instanceof ToolActivity) { - GradeBookActivityDTO activityDTO = getGradeBookActivityDTO(firstActivity, learner, learnerProgress); + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(firstActivity, learner, learnerProgress); gradeBookActivityDTOs.add(activityDTO); } for (Activity activity : activities) { if (activity.getActivityId().longValue() != firstActivity.getActivityId().longValue()) { - GradeBookActivityDTO activityDTO = getGradeBookActivityDTO(activity, learner, learnerProgress); + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(activity, learner, learnerProgress); gradeBookActivityDTOs.add(activityDTO); } } return gradeBookActivityDTOs; } /** - * Gets the gradebook data for a user for a given activity + * Gets a list of GradeBookGridRowDTO for a given lesson and learner * + * The result should be used for the "user view" of gradebook, where users + * are listed, then expanding a sub-grid will bring up the activites + * + * the mark applies to the activity + * + * @param lesson + * @param learner + * @return Collection + */ + @SuppressWarnings("unchecked") + public List getUserGradeBookActivityDTOs(Lesson lesson, Activity activity) { + + List gradeBookUserDTOs = new ArrayList(); + + Set learners = (Set) lesson.getAllLearners(); + + if (learners != null) { + for (User learner : learners) { + GBUserGridRowDTO gUserDTO = new GBUserGridRowDTO(); + gUserDTO.setFirstName(learner.getFirstName()); + gUserDTO.setLastName(learner.getLastName()); + gUserDTO.setLogin(learner.getLogin()); + + GradeBookUserActivity gradeBookUserActivity = gradeBookDAO.getGradeBookUserDataForActivity(activity + .getActivityId(), learner.getUserId()); + + // Set the progress + LearnerProgress learnerProgress = monitoringService.getLearnerProgress(learner.getUserId(), lesson + .getLessonId()); + gUserDTO.setStatus(getActivityStatusStr(learnerProgress, activity)); + + + // Set the outputs and activity url, if there is one + if (activity.isToolActivity() && activity instanceof ToolActivity) { + ToolActivity toolAct = (ToolActivity) activity; + // Get the tool outputs for this user if there are any + ToolSession toolSession = toolService.getToolSessionByLearner(learner, toolAct); + if (toolSession != null) { + // Set the activityLearner URL for this gradebook activity + gUserDTO.setActivityUrl(Configuration.get(ConfigurationKeys.SERVER_URL) + + toolAct.getTool().getLearnerProgressUrl() + "&userID=" + learner.getUserId() + + "&toolSessionID=" + toolSession.getToolSessionId().toString()); + + gUserDTO.setOutput(this.getToolOutputsStr(toolAct, toolSession, learner)); + + } + } + + // Add marks and feedback + if (gradeBookUserActivity != null) { + gUserDTO.setFeedback(gradeBookUserActivity.getFeedback()); + gUserDTO.setMark(gradeBookUserActivity.getMark()); + + } + gradeBookUserDTOs.add(gUserDTO); + } + } + + return gradeBookUserDTOs; + + } + + /** + * Gets the user gradebook and outputs data for a given lesson, which is a + * list of GBActivityGridRowDTO, this one is specifically for the user view + * + * @param lesson + * @param learner + * @return Collection + */ + @SuppressWarnings("unchecked") + public List getUserGradeBookActivityDTOsActivityView(Lesson lesson, User learner) { + + logger.debug("Getting gradebook user data for lesson: " + lesson.getLessonId() + ". For user: " + + learner.getUserId()); + + List gradeBookActivityDTOs = new ArrayList(); + + LearnerProgress learnerProgress = monitoringService.getLearnerProgress(learner.getUserId(), lesson + .getLessonId()); + + Set activities = (Set) lesson.getLearningDesign().getActivities(); + + /* + * Hibernate CGLIB is failing to load the first activity in + * the sequence as a ToolActivity for some mysterious reason + * Causes a ClassCastException when you try to cast it, even + * if it is a ToolActivity. + * + * THIS IS A HACK to retrieve the first tool activity + * manually so it can be cast as a ToolActivity - if it is one + */ + Activity firstActivity = monitoringService.getActivityById(lesson.getLearningDesign().getFirstActivity() + .getActivityId()); + + if (firstActivity.isToolActivity() && firstActivity instanceof ToolActivity) { + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(firstActivity, learner, learnerProgress); + gradeBookActivityDTOs.add(activityDTO); + } + + for (Activity activity : activities) { + if (activity.getActivityId().longValue() != firstActivity.getActivityId().longValue()) { + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(activity, learner, learnerProgress); + gradeBookActivityDTOs.add(activityDTO); + } + } + return gradeBookActivityDTOs; + } + + /** + * Given a lesson, this method returns the GBUserGridRowDTO for act + * + * @param lesson + * @return + */ + public List getActivityGradeBookUserDTOs(Lesson lesson) { + + logger.debug("Getting gradebook data for lesson: " + lesson.getLessonId()); + + List gradeBookActivityDTOs = new ArrayList(); + + Set activities = (Set) lesson.getLearningDesign().getActivities(); + + /* + * Hibernate CGLIB is failing to load the first activity in + * the sequence as a ToolActivity for some mysterious reason + * Causes a ClassCastException when you try to cast it, even + * if it is a ToolActivity. + * + * THIS IS A HACK to retrieve the first tool activity + * manually so it can be cast as a ToolActivity - if it is one + */ + Activity firstActivity = monitoringService.getActivityById(lesson.getLearningDesign().getFirstActivity() + .getActivityId()); + + if (firstActivity.isToolActivity() && firstActivity instanceof ToolActivity) { + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(firstActivity, lesson); + gradeBookActivityDTOs.add(activityDTO); + } + + for (Activity activity : activities) { + if (activity.getActivityId().longValue() != firstActivity.getActivityId().longValue()) { + GBActivityGridRowDTO activityDTO = getGradeBookActivityDTO(activity, lesson); + gradeBookActivityDTOs.add(activityDTO); + } + } + + return gradeBookActivityDTOs; + + } + + /** + * Gets the gradebook data for a user for a given activity and lesson + * * @param activity + * @return GradeBookActivityDTO + */ + public GBActivityGridRowDTO getGradeBookActivityDTO(Activity activity, Lesson lesson) { + GBActivityGridRowDTO gactivityDTO = new GBActivityGridRowDTO(); + gactivityDTO.setActivityId(activity.getActivityId()); + gactivityDTO.setActivityTitle(activity.getTitle()); + + if (activity.isToolActivity() && activity instanceof ToolActivity) { + ToolActivity toolAct = (ToolActivity) activity; + + // Get the competences for this activity + Set competenceMappings = toolAct.getCompetenceMappings(); + String competenceMappingsStr = ""; + if (competenceMappings != null) { + for (CompetenceMapping mapping : competenceMappings) { + competenceMappingsStr += mapping.getCompetence().getTitle() + ", "; + } + + // trim the last comma off + if (competenceMappingsStr.length() > 0) { + competenceMappingsStr = competenceMappingsStr.substring(0, competenceMappingsStr.lastIndexOf(",")); + } + } + gactivityDTO.setCompetences(competenceMappingsStr); + + List gradeBookUserActivities = gradeBookDAO + .getAllGradeBookUserActivitiesForActivity(activity.getActivityId()); + + if (gradeBookUserActivities != null) { + + double sum = 0; + double count = 0; + for (GradeBookUserActivity gact : gradeBookUserActivities) { + if (gact.getMark() != null) { + count++; + sum += gact.getMark(); + } + } + + if (count != 0) { + gactivityDTO.setAverage(sum / count); + } + } + + } + + return gactivityDTO; + } + + /** + * Gets the gradebook data for a user for a given activity and user + * + * @param activity * @param learner * @param learnerProgress * @return GradeBookActivityDTO */ - public GradeBookActivityDTO getGradeBookActivityDTO(Activity activity, User learner, LearnerProgress learnerProgress) { + public GBActivityGridRowDTO getGradeBookActivityDTO(Activity activity, User learner, LearnerProgress learnerProgress) { logger.debug("Getting gradebook data for activity: " + activity.getActivityId() + ". For user: " + learner.getUserId()); - GradeBookActivityDTO gactivityDTO = new GradeBookActivityDTO(); + GBActivityGridRowDTO gactivityDTO = new GBActivityGridRowDTO(); gactivityDTO.setActivityId(activity.getActivityId()); gactivityDTO.setActivityTitle(activity.getTitle()); @@ -134,19 +348,8 @@ gactivityDTO.setMark(gradeBookActivity.getMark()); gactivityDTO.setFeedback(gradeBookActivity.getFeedback()); } - if (learnerProgress != null) { - byte progressState = learnerProgress.getProgressState(activity); - if (progressState == LearnerProgress.ACTIVITY_COMPLETED) { - gactivityDTO.setStatus("COMPLETED"); - } else if (progressState == LearnerProgress.ACTIVITY_ATTEMPTED) { - gactivityDTO.setStatus("ATTEMPTED"); - } else { - gactivityDTO.setStatus("NOT ATTEMPTED"); - } - } else { - gactivityDTO.setStatus("NOT ATTEMPTED"); - } + gactivityDTO.setStatus(getActivityStatusStr(learnerProgress, activity)); if (activity.isToolActivity() && activity instanceof ToolActivity) { ToolActivity toolAct = (ToolActivity) activity; @@ -169,37 +372,59 @@ // Get the tool outputs for this user if there are any ToolSession toolSession = toolService.getToolSessionByLearner(learner, toolAct); if (toolSession != null) { - SortedMap map = toolService.getOutputDefinitionsFromTool(toolAct - .getToolContentId()); - - Set toolOutputs = new HashSet(); - // Set the activityLearner URL for this gradebook activity gactivityDTO.setActivityUrl(Configuration.get(ConfigurationKeys.SERVER_URL) + toolAct.getTool().getLearnerProgressUrl() + "&userID=" + learner.getUserId() + "&toolSessionID=" + toolSession.getToolSessionId().toString()); - // Setting the tool outputs - String toolOutputsStr = ""; - for (String outputName : map.keySet()) { + gactivityDTO.setOutput(this.getToolOutputsStr(toolAct, toolSession, learner)); - try { - ToolOutput toolOutput = toolService.getOutputFromTool(outputName, toolSession, learner - .getUserId()); - - if (toolOutput != null) { - toolOutputs.add(toolOutput); - toolOutputsStr += toolOutput.getDescription() + ": " + toolOutput.getValue().getString() - + "
"; - } - - } catch (RuntimeException e) { - logger.debug("Runtime exception when attempted to get outputs for activity: " - + toolAct.getActivityId() + ", continuing for other activities", e); - } - } - toolOutputsStr = (toolOutputsStr.equals("")) ? "No output available." : toolOutputsStr; - gactivityDTO.setOutput(toolOutputsStr); + // SortedMap map = toolService.getOutputDefinitionsFromTool(toolAct + // .getToolContentId()); + // + // Set toolOutputs = new HashSet(); + // + // + // + // // Setting the tool outputs + // String toolOutputsStr = ""; + // boolean noOutputs = true; + // if (map.keySet().size() > 0) { + // + // for (String outputName : map.keySet()) { + // + // try { + // ToolOutput toolOutput = toolService.getOutputFromTool(outputName, toolSession, learner + // .getUserId()); + // + // if (toolOutput != null && toolOutput.getValue().getType() != OutputType.OUTPUT_COMPLEX) { + // toolOutputs.add(toolOutput); + // + // toolOutputsStr += ""; + // + // noOutputs = false; + // } + // + // } catch (RuntimeException e) { + // logger.debug("Runtime exception when attempted to get outputs for activity: " + // + toolAct.getActivityId() + ", continuing for other activities", e); + // } + // } + // toolOutputsStr += ""; + // } + // + // // Fix up outputs html if there are not outputs available + // if (noOutputs) { + // toolOutputsStr = "No output available."; + // } else { + // toolOutputsStr = ""; + + } + } + + // Fix up outputs html if there are not outputs available + if (noOutputs) { + toolOutputsStr = "No output available."; + } else { + toolOutputsStr = "