Skip to content

Commit

Permalink
SAK-33530 - Enter grades using comma as decimal separator (sakaiproje…
Browse files Browse the repository at this point in the history
…ct#4940)

Cannot enter a grade in percentage using comma as decimal separator
Miguel Pellicer authored and ottenhoff committed Nov 10, 2017

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent d704f61 commit e3e34e2
Showing 3 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -2311,8 +2311,9 @@ private Double convertInputGradeToPoints(int gradeEntryType, LetterGradePercentM
}
} else {
try {
percentage = Double.parseDouble(grade);
} catch (NumberFormatException nfe) {
NumberFormat nbFormat = NumberFormat.getInstance(new ResourceLoader().getLocale());
percentage = new Double (nbFormat.parse(grade).doubleValue());
} catch (NumberFormatException | ParseException nfe) {
throw new IllegalArgumentException("Invalid % grade passed to convertInputGradeToPoints");
}
}
Original file line number Diff line number Diff line change
@@ -35,7 +35,6 @@
import javax.xml.bind.JAXBException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.sakaiproject.authz.api.Member;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.component.api.ServerConfigurationService;
@@ -84,6 +83,7 @@
import org.sakaiproject.user.api.User;
import org.sakaiproject.user.api.UserDirectoryService;
import org.sakaiproject.user.api.UserNotDefinedException;
import org.sakaiproject.util.FormattedText;
import org.sakaiproject.util.ResourceLoader;

import lombok.Setter;
@@ -541,17 +541,22 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student
// Fix a problem when the grades comes from the old Gradebook API with locale separator, always compare the values using the same
// separator
if (StringUtils.isNotBlank(oldGradeAdjusted)) {
oldGradeAdjusted = oldGradeAdjusted.replace(newGradeAdjusted.contains(",") ? "." : ",",
newGradeAdjusted.contains(",") ? "," : ".");
oldGradeAdjusted = oldGradeAdjusted.replace(",".equals(FormattedText.getDecimalSeparator()) ? "." : ",",
",".equals(FormattedText.getDecimalSeparator()) ? "," : ".");
}
if (StringUtils.isNotBlank(storedGradeAdjusted)) {
storedGradeAdjusted = storedGradeAdjusted.replace(newGradeAdjusted.contains(",") ? "." : ",",
newGradeAdjusted.contains(",") ? "," : ".");
storedGradeAdjusted = storedGradeAdjusted.replace(",".equals(FormattedText.getDecimalSeparator()) ? "." : ",",
",".equals(FormattedText.getDecimalSeparator()) ? "," : ".");
}

if (gradingType == GradingType.PERCENTAGE) {
// the passed in grades represents a percentage so the number needs to be adjusted back to points
final Double newGradePercentage = NumberUtils.toDouble(newGrade);
Double newGradePercentage = new Double("0.0");

if(StringUtils.isNotBlank(newGrade)){
newGradePercentage = FormatHelper.validateDouble(newGrade);
}

final Double newGradePointsFromPercentage = (newGradePercentage / 100) * maxPoints;
newGradeAdjusted = FormatHelper.formatDoubleToDecimal(newGradePointsFromPercentage);

@@ -563,10 +568,13 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student
// comparing apples with apples, we first determine the number of decimal places
// on the score, so the converted points-as-percentage is in the expected format.

final Double oldGradePercentage = NumberUtils.toDouble(oldGrade);
final Double oldGradePercentage = FormatHelper.validateDouble(oldGradeAdjusted);
final Double oldGradePointsFromPercentage = (oldGradePercentage / 100) * maxPoints;

oldGradeAdjusted = FormatHelper.formatDoubleToMatch(oldGradePointsFromPercentage, storedGradeAdjusted);

oldGradeAdjusted = oldGradeAdjusted.replace(",".equals(FormattedText.getDecimalSeparator()) ? "." : ",",
",".equals(FormattedText.getDecimalSeparator()) ? "," : ".");
}

// we dont need processing of the stored grade as the service does that when persisting.
@@ -579,6 +587,10 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student
oldGradeAdjusted = StringUtils.trimToNull(StringUtils.removeEnd(oldGradeAdjusted, ".0"));
newGradeAdjusted = StringUtils.trimToNull(StringUtils.removeEnd(newGradeAdjusted, ".0"));

storedGradeAdjusted = StringUtils.trimToNull(StringUtils.removeEnd(storedGradeAdjusted, ",0"));
oldGradeAdjusted = StringUtils.trimToNull(StringUtils.removeEnd(oldGradeAdjusted, ",0"));
newGradeAdjusted = StringUtils.trimToNull(StringUtils.removeEnd(newGradeAdjusted, ",0"));

if (log.isDebugEnabled()) {
log.debug("storedGradeAdjusted: " + storedGradeAdjusted);
log.debug("oldGradeAdjusted: " + oldGradeAdjusted);
@@ -595,7 +607,7 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student

// no change
if (StringUtils.equals(storedGradeAdjusted, newGradeAdjusted)) {
final Double storedGradePoints = NumberUtils.toDouble(storedGradeAdjusted);
final Double storedGradePoints = FormatHelper.validateDouble(storedGradeAdjusted);
if (storedGradePoints.compareTo(maxPoints) > 0) {
return GradeSaveResponse.OVER_LIMIT;
} else {
@@ -613,7 +625,7 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student
GradeSaveResponse rval = null;

if (StringUtils.isNotBlank(newGradeAdjusted)) {
final Double newGradePoints = NumberUtils.toDouble(newGradeAdjusted);
final Double newGradePoints = FormatHelper.validateDouble(newGradeAdjusted);

// if over limit, still save but return the warning
if (newGradePoints.compareTo(maxPoints) > 0) {
Original file line number Diff line number Diff line change
@@ -91,6 +91,10 @@ public static String formatDoubleToMatch(final Double score, final String toMatc
numberOfDecimalPlaces = toMatch.split("\\.")[1].length();
}

if (toMatch.indexOf(",") >= 0) {
numberOfDecimalPlaces = toMatch.split("\\,")[1].length();
}

return FormatHelper.formatDoubleToDecimal(score, numberOfDecimalPlaces);
}

0 comments on commit e3e34e2

Please sign in to comment.