Skip to content

Commit

Permalink
SAK-41295: Course grade miscalculates if there's an Extra Credit cate…
Browse files Browse the repository at this point in the history
…gory and 1 or more other categories are empty (sakaiproject#6939)
  • Loading branch information
jesusmmp authored and ottenhoff committed Jul 3, 2019
1 parent 9d61d26 commit cda59eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,18 @@ public Double getNonNullAutoCalculatedGrade() {
}

public void initNonpersistentFields(final double totalPointsPossible, final double totalPointsEarned) {
Double percentageEarned;
BigDecimal percentageEarned;
this.totalPointsPossible = totalPointsPossible;
this.calculatedPointsEarned = totalPointsEarned;
final BigDecimal bdTotalPointsPossible = new BigDecimal(totalPointsPossible);
final BigDecimal bdTotalPointsEarned = new BigDecimal(totalPointsEarned);
final BigDecimal bdTotalPointsPossible = BigDecimal.valueOf(totalPointsPossible);
final BigDecimal bdTotalPointsEarned = BigDecimal.valueOf(totalPointsEarned);
if (totalPointsPossible == 0.0) {
percentageEarned = null;
this.autoCalculatedGrade = null;
} else {
percentageEarned = Double.valueOf(bdTotalPointsEarned.divide(bdTotalPointsPossible, GradebookService.MATH_CONTEXT)
.multiply(new BigDecimal("100")).doubleValue());
percentageEarned = bdTotalPointsEarned.divide(bdTotalPointsPossible, GradebookService.MATH_CONTEXT)
.multiply(new BigDecimal("100"));
this.autoCalculatedGrade = percentageEarned.doubleValue();
}
this.autoCalculatedGrade = percentageEarned;
}

// Added by -Qu for totalPoints implementation in GB2 bugid:4371 9/2011
Expand All @@ -217,20 +217,23 @@ public void setCalculatedPointsEarned(final double literalTotalPointsEarned) {
}

public void initNonpersistentFields(final double totalPointsPossible, final double totalPointsEarned,
final double literalTotalPointsEarned) {
Double percentageEarned;
final double literalTotalPointsEarned, final double totalPointsExtra) {
BigDecimal percentageEarned;
BigDecimal percentageExtraEarned;
this.calculatedPointsEarned = literalTotalPointsEarned;
this.totalPointsPossible = totalPointsPossible;
final BigDecimal bdTotalPointsPossible = new BigDecimal(totalPointsPossible);
final BigDecimal bdTotalPointsEarned = new BigDecimal(totalPointsEarned);
final BigDecimal bdTotalPointsPossible = BigDecimal.valueOf(totalPointsPossible);
final BigDecimal bdTotalPointsEarned = BigDecimal.valueOf(totalPointsEarned);

if (totalPointsPossible <= 0.0) {
percentageEarned = null;
this.autoCalculatedGrade = null;
} else {
percentageEarned = Double.valueOf(bdTotalPointsEarned.divide(bdTotalPointsPossible, GradebookService.MATH_CONTEXT)
.multiply(new BigDecimal("100")).doubleValue());
percentageEarned = bdTotalPointsEarned.divide(bdTotalPointsPossible, GradebookService.MATH_CONTEXT)
.multiply(new BigDecimal("100"));
percentageExtraEarned = BigDecimal.valueOf(totalPointsExtra).multiply(new BigDecimal("100"), GradebookService.MATH_CONTEXT);
percentageEarned = percentageEarned.add(percentageExtraEarned, GradebookService.MATH_CONTEXT);
this.autoCalculatedGrade = percentageEarned.doubleValue();
}
this.autoCalculatedGrade = percentageEarned;
}

public Double getCalculatedPointsEarned() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -839,9 +839,10 @@ public Object doInHibernate(final Session session) throws HibernateException {
countedAssigns);
final double totalPointsEarned = ((Double) totalEarned.get(0));
final double literalTotalPointsEarned = ((Double) totalEarned.get(1));
final double extraPointsEarned = ((Double) totalEarned.get(2));
final double totalPointsPossible = getTotalPointsInternal(gradebook, cates, cgr.getStudentId(), studentGradeRecs,
countedAssigns, false);
cgr.initNonpersistentFields(totalPointsPossible, totalPointsEarned, literalTotalPointsEarned);
cgr.initNonpersistentFields(totalPointsPossible, totalPointsEarned, literalTotalPointsEarned, extraPointsEarned);
if (log.isDebugEnabled()) {
log.debug("Points earned = " + cgr.getPointsEarned());
}
Expand Down Expand Up @@ -1008,7 +1009,8 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
return returnList;
}

double totalPointsEarned = 0;
BigDecimal totalPointsEarned = new BigDecimal(0);
BigDecimal extraPointsEarned = new BigDecimal(0);
BigDecimal literalTotalPointsEarned = new BigDecimal(0d);

final Map cateScoreMap = new HashMap();
Expand All @@ -1021,19 +1023,19 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
if (gradeRec.getPointsEarned() != null && !gradeRec.getPointsEarned().equals("") && !gradeRec.getDroppedFromGrade()) {
final GradebookAssignment go = gradeRec.getAssignment();
if (go.isIncludedInCalculations() && countedAssigns.contains(go)) {
final Double pointsEarned = gradeRec.getPointsEarned();
final BigDecimal pointsEarned = BigDecimal.valueOf(gradeRec.getPointsEarned());
// if(gbGradeType == GradebookService.GRADE_TYPE_POINTS)
// {
if (gradebook.getCategory_type() == GradebookService.CATEGORY_TYPE_NO_CATEGORY) {
if (!excused) {
totalPointsEarned += pointsEarned;
literalTotalPointsEarned = (new BigDecimal(pointsEarned)).add(literalTotalPointsEarned);
totalPointsEarned = totalPointsEarned.add(pointsEarned, GradebookService.MATH_CONTEXT);
literalTotalPointsEarned = pointsEarned.add(literalTotalPointsEarned, GradebookService.MATH_CONTEXT);
assignmentsTaken.add(go.getId());
}
} else if (gradebook.getCategory_type() == GradebookService.CATEGORY_TYPE_ONLY_CATEGORY && go != null) {
if (!excused) {
totalPointsEarned += pointsEarned;
literalTotalPointsEarned = (new BigDecimal(pointsEarned)).add(literalTotalPointsEarned);
totalPointsEarned = totalPointsEarned.add(pointsEarned, GradebookService.MATH_CONTEXT);
literalTotalPointsEarned = pointsEarned.add(literalTotalPointsEarned, GradebookService.MATH_CONTEXT);
assignmentsTaken.add(go.getId());
}
} else if (gradebook.getCategory_type() == GradebookService.CATEGORY_TYPE_WEIGHTED_CATEGORY && go != null
Expand All @@ -1044,9 +1046,9 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
&& cate.getId().equals(go.getCategory().getId())) {
if (!excused) {
assignmentsTaken.add(go.getId());
literalTotalPointsEarned = (new BigDecimal(pointsEarned)).add(literalTotalPointsEarned);
literalTotalPointsEarned = pointsEarned.add(literalTotalPointsEarned, GradebookService.MATH_CONTEXT);
if (cateScoreMap.get(cate.getId()) != null) {
cateScoreMap.put(cate.getId(), ((Double) cateScoreMap.get(cate.getId())) + pointsEarned);
cateScoreMap.put(cate.getId(), ((BigDecimal)cateScoreMap.get(cate.getId())).add(pointsEarned, GradebookService.MATH_CONTEXT));
} else {
cateScoreMap.put(cate.getId(), pointsEarned);
}
Expand All @@ -1070,10 +1072,10 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
&& cate.getId().equals(asgn.getCategory().getId()) && !asgn.isExtraCredit()) {

if (cateTotalScoreMap.get(cate.getId()) == null) {
cateTotalScoreMap.put(cate.getId(), asgn.getPointsPossible());
cateTotalScoreMap.put(cate.getId(), new BigDecimal(asgn.getPointsPossible()));
} else {
cateTotalScoreMap.put(cate.getId(),
((Double) cateTotalScoreMap.get(cate.getId())) + asgn.getPointsPossible());
((BigDecimal) cateTotalScoreMap.get(cate.getId())).add(new BigDecimal(asgn.getPointsPossible())));
}

}
Expand All @@ -1083,16 +1085,22 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
}

if (assignmentsTaken.isEmpty()) {
totalPointsEarned = -1;
totalPointsEarned = new BigDecimal(-1);
}

if (gradebook.getCategory_type() == GradebookService.CATEGORY_TYPE_WEIGHTED_CATEGORY) {
for (int i = 0; i < categories.size(); i++) {
final Category cate = (Category) categories.get(i);
if (cate != null && !cate.isRemoved() && cateScoreMap.get(cate.getId()) != null
&& cateTotalScoreMap.get(cate.getId()) != null) {
totalPointsEarned += ((Double) cateScoreMap.get(cate.getId())) * cate.getWeight()
/ ((Double) cateTotalScoreMap.get(cate.getId()));
if (cate.getIsExtraCredit()) {
extraPointsEarned = extraPointsEarned.add(((BigDecimal) cateScoreMap.get(cate.getId())).multiply(new BigDecimal(cate.getWeight()), GradebookService.MATH_CONTEXT)
.divide((BigDecimal) cateTotalScoreMap.get(cate.getId()), GradebookService.MATH_CONTEXT));
}
else {
totalPointsEarned = totalPointsEarned.add(((BigDecimal) cateScoreMap.get(cate.getId())).multiply(new BigDecimal(cate.getWeight()), GradebookService.MATH_CONTEXT)
.divide((BigDecimal) cateTotalScoreMap.get(cate.getId()), GradebookService.MATH_CONTEXT));
}
}
}
}
Expand All @@ -1101,8 +1109,9 @@ private List getTotalPointsEarnedInternal(final String studentId, final Gradeboo
log.debug("getTotalPointsEarnedInternal for studentId=" + studentId + " returning " + totalPointsEarned);
}
final List returnList = new ArrayList();
returnList.add(totalPointsEarned);
returnList.add((new BigDecimal(literalTotalPointsEarned.doubleValue(), GradebookService.MATH_CONTEXT)).doubleValue());
returnList.add(totalPointsEarned.doubleValue());
returnList.add(literalTotalPointsEarned.doubleValue());
returnList.add(extraPointsEarned.doubleValue());

return returnList;
}
Expand Down

0 comments on commit cda59eb

Please sign in to comment.