Index: lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/service/AssessmentServiceImpl.java =================================================================== diff -u -rdc675aacc123d04d0a5c82278c1db77df33c6c24 -r311966039e3523fc031ef73008a13657f75f5426 --- lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/service/AssessmentServiceImpl.java (.../AssessmentServiceImpl.java) (revision dc675aacc123d04d0a5c82278c1db77df33c6c24) +++ lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/service/AssessmentServiceImpl.java (.../AssessmentServiceImpl.java) (revision 311966039e3523fc031ef73008a13657f75f5426) @@ -279,8 +279,7 @@ // the latest result is already copied, so no need to copy it again return; } - - + // copy results from leader to user in both cases (when there is no userResult yet and when if it's been changed // by the leader) userResult.setStartDate(leaderResult.getStartDate()); @@ -994,22 +993,23 @@ lastFinishedResult = getLastFinishedAssessmentResult(assessmentUid, targetUserId); } + Map questionToResultMap = lastResult.getQuestionResults().stream() + .collect(Collectors.toMap(q -> q.getQbToolQuestion().getUid(), q -> q)); + Map questionToFinishedResultMap = lastFinishedResult == null ? null + : lastFinishedResult.getQuestionResults().stream() + .collect(Collectors.toMap(q -> q.getQbToolQuestion().getUid(), q -> q)); + for (Set questionsForOnePage : pagedQuestionDtos) { for (QuestionDTO questionDto : questionsForOnePage) { - //load last finished results for hedging type of questions (in order to prevent retry) - Set questionResults = lastResult.getQuestionResults(); - if ((questionDto.getType() == QbQuestion.TYPE_MARK_HEDGING) && (lastResult.getFinishDate() == null) - && (lastFinishedResult != null)) { - questionResults = lastFinishedResult.getQuestionResults(); + // load last finished results for hedging type of questions (in order to prevent retry) + AssessmentQuestionResult questionResult = (questionDto.getType() == QbQuestion.TYPE_MARK_HEDGING) + && (lastResult.getFinishDate() == null) && (lastFinishedResult != null) + ? questionToFinishedResultMap.get(questionDto.getUid()) + : questionToResultMap.get(questionDto.getUid()); + if (questionResult != null) { + loadupQuestionResultIntoQuestionDto(questionDto, questionResult); } - - for (AssessmentQuestionResult questionResult : questionResults) { - if (questionDto.getUid().equals(questionResult.getQbToolQuestion().getUid())) { - loadupQuestionResultIntoQuestionDto(questionDto, questionResult); - break; - } - } } } } @@ -1027,14 +1027,14 @@ questionDto.setPenalty(questionResult.getPenalty()); questionDto.setConfidenceLevel(questionResult.getConfidenceLevel()); - for (OptionDTO optionDto : questionDto.getOptionDtos()) { + Map answerMap = questionResult.getOptionAnswers().stream() + .collect(Collectors.toMap(a -> a.getOptionUid(), a -> a)); - for (AssessmentOptionAnswer optionAnswer : questionResult.getOptionAnswers()) { - if (optionDto.getUid().equals(optionAnswer.getOptionUid())) { - optionDto.setAnswerBoolean(optionAnswer.getAnswerBoolean()); - optionDto.setAnswerInt(optionAnswer.getAnswerInt()); - break; - } + for (OptionDTO optionDto : questionDto.getOptionDtos()) { + AssessmentOptionAnswer optionAnswer = answerMap.get(optionDto.getUid()); + if (optionAnswer != null) { + optionDto.setAnswerBoolean(optionAnswer.getAnswerBoolean()); + optionDto.setAnswerInt(optionAnswer.getAnswerInt()); } } Index: lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/web/controller/LearningController.java =================================================================== diff -u -rb296c7b26b2bfa11fe103c4da6fb29bed4d71731 -r311966039e3523fc031ef73008a13657f75f5426 --- lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/web/controller/LearningController.java (.../LearningController.java) (revision b296c7b26b2bfa11fe103c4da6fb29bed4d71731) +++ lams_tool_assessment/src/java/org/lamsfoundation/lams/tool/assessment/web/controller/LearningController.java (.../LearningController.java) (revision 311966039e3523fc031ef73008a13657f75f5426) @@ -249,6 +249,8 @@ } //add random questions (actually replacing them with real ones) AssessmentResult lastResult = service.getLastAssessmentResult(assessment.getUid(), user.getUserId()); + Map questionToResultMap = lastResult.getQuestionResults().stream() + .collect(Collectors.toMap(q -> q.getQbToolQuestion().getUid(), q -> q)); for (QuestionReference questionReference : questionReferences) { if (questionReference.isRandomQuestion()) { @@ -262,16 +264,13 @@ availableRandomQuestions.remove(randomQuestion); } else { - //pick element from the last result + // pick element from the last result for (Iterator iter = availableRandomQuestions.iterator(); iter.hasNext();) { AssessmentQuestion availableRandomQuestion = iter.next(); - - for (AssessmentQuestionResult questionResult : lastResult.getQuestionResults()) { - if (availableRandomQuestion.getUid().equals(questionResult.getQbToolQuestion().getUid())) { - randomQuestion = availableRandomQuestion; - iter.remove(); - break; - } + if (questionToResultMap.containsKey(availableRandomQuestion.getUid())) { + randomQuestion = availableRandomQuestion; + iter.remove(); + break; } } } @@ -1041,51 +1040,50 @@ // release object from the cache (it's required when we have modified result object in the same request) AssessmentResult result = service.getLastFinishedAssessmentResultNotFromChache(assessment.getUid(), userId); + Map questionToResultMap = result.getQuestionResults().stream() + .collect(Collectors.toMap(q -> q.getQbToolQuestion().getUid(), q -> q)); + for (Set questionsForOnePage : pagedQuestionDtos) { for (QuestionDTO questionDto : questionsForOnePage) { + AssessmentQuestionResult questionResult = questionToResultMap.get(questionDto.getUid()); + if (questionResult != null) { + // copy questionResult's info to the question + questionDto.setMark(questionResult.getMark()); + questionDto.setResponseSubmitted(questionResult.getFinishDate() != null); + questionDto.setPenalty(questionResult.getPenalty()); - // find corresponding questionResult - for (AssessmentQuestionResult questionResult : result.getQuestionResults()) { - if (questionDto.getUid().equals(questionResult.getQbToolQuestion().getUid())) { + //question feedback + questionDto.setQuestionFeedback(null); + for (OptionDTO optionDto : questionDto.getOptionDtos()) { + if (questionResult.getQbOption() != null + && optionDto.getUid().equals(questionResult.getQbOption().getUid())) { + questionDto.setQuestionFeedback(optionDto.getFeedback()); + break; + } + } - // copy questionResult's info to the question - questionDto.setMark(questionResult.getMark()); - questionDto.setResponseSubmitted(questionResult.getFinishDate() != null); - questionDto.setPenalty(questionResult.getPenalty()); - - //question feedback - questionDto.setQuestionFeedback(null); + // required for showing right/wrong answers icons on results page correctly + if ((questionDto.getType() == QbQuestion.TYPE_VERY_SHORT_ANSWERS + || questionDto.getType() == QbQuestion.TYPE_NUMERICAL) + && questionResult.getQbOption() != null) { + boolean isAnsweredCorrectly = false; for (OptionDTO optionDto : questionDto.getOptionDtos()) { - if (questionResult.getQbOption() != null - && optionDto.getUid().equals(questionResult.getQbOption().getUid())) { - questionDto.setQuestionFeedback(optionDto.getFeedback()); + if (optionDto.getUid().equals(questionResult.getQbOption().getUid())) { + isAnsweredCorrectly = optionDto.getMaxMark() > 0; break; } } + questionDto.setAnswerBoolean(isAnsweredCorrectly); + } - // required for showing right/wrong answers icons on results page correctly - if ((questionDto.getType() == QbQuestion.TYPE_VERY_SHORT_ANSWERS - || questionDto.getType() == QbQuestion.TYPE_NUMERICAL) - && questionResult.getQbOption() != null) { - boolean isAnsweredCorrectly = false; - for (OptionDTO optionDto : questionDto.getOptionDtos()) { - if (optionDto.getUid().equals(questionResult.getQbOption().getUid())) { - isAnsweredCorrectly = optionDto.getMaxMark() > 0; - break; - } - } - questionDto.setAnswerBoolean(isAnsweredCorrectly); - } - - if (StringUtils.isNotBlank(questionResult.getJustification())) { - questionDto.setJustification(questionResult.getJustification()); - } - - // required for markandpenalty area and if it's on - on question's summary page - List questionResults = service - .getAssessmentQuestionResultList(assessment.getUid(), userId, questionDto.getUid()); - questionDto.setQuestionResults(questionResults); + if (StringUtils.isNotBlank(questionResult.getJustification())) { + questionDto.setJustification(questionResult.getJustification()); } + + // required for markandpenalty area and if it's on - on question's summary page + List questionResults = service.getAssessmentQuestionResultList(assessment.getUid(), + userId, questionDto.getUid()); + questionDto.setQuestionResults(questionResults); } } }