Skip to content

Commit

Permalink
SAK-27900 - Allow numerical values for final grade override. (sakaipr…
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmerino authored and ern committed Aug 23, 2017
1 parent 49a7f49 commit 504a97d
Showing 1 changed file with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.sakaiproject.gradebookng.tool.panels;

import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.ajax.AjaxRequestTarget;
Expand Down Expand Up @@ -53,6 +57,7 @@ public void onInitialize() {
// TODO this could all be passed in through the model if it was changed to a map, as per CourseGradeItemCellPanel...
final GbUser studentUser = this.businessService.getUser(studentUuid);
final String currentUserUuid = getCurrentUserId();
final Locale currentUserLocale = this.businessService.getUserPreferredLocale();
final GbRole currentUserRole = getUserRole();
final Gradebook gradebook = getGradebook();
final boolean courseGradeVisible = this.businessService.isCourseGradeVisible(currentUserUuid);
Expand Down Expand Up @@ -91,7 +96,7 @@ public void onInitialize() {

@Override
public void onSubmit(final AjaxRequestTarget target, final Form<?> form) {
final String newGrade = (String) form.getModelObject();
String newGrade = (String) form.getModelObject();

// validate the grade entered is a valid one for the selected grading schema
// though we allow blank grades so the override is removed
Expand All @@ -100,10 +105,16 @@ public void onSubmit(final AjaxRequestTarget target, final Form<?> form) {

if (StringUtils.isNotBlank(newGrade)) {
final Map<String, Double> schema = gbInfo.getSelectedGradingScaleBottomPercents();

if (!schema.containsKey(newGrade)) {
error(new ResourceModel("message.addcoursegradeoverride.invalid").getObject());
target.addChildren(form, FeedbackPanel.class);
return;
try {
newGrade = getGradeFromNumber(newGrade, schema, currentUserLocale);
}
catch (NumberFormatException e) {
error(new ResourceModel("message.addcoursegradeoverride.invalid").getObject());
target.addChildren(form, FeedbackPanel.class);
return;
}
}
}

Expand Down Expand Up @@ -195,5 +206,46 @@ private String formatPoints(final CourseGrade courseGrade, final Gradebook grade
return rval;

}

/**
* Helper to accept numerical grades and get the scale value.
* Returns the scale whose value equals to the numeric value received, or if it doesn't exists, the highest value lower.
*
* @param newGrade the grade to convert
* @param schema the current schema of Gradebook
* @param currentUserLocale the locale to format the grade with the right decimal separator
* @return fully formatted string ready for display
*/
private String getGradeFromNumber(String newGrade, Map<String, Double> schema, Locale currentUserLocale) {
Double currentGradeValue = new Double(0.0);
Double maxValue = new Double(0.0);
try {
NumberFormat nf = NumberFormat.getInstance(currentUserLocale);
ParsePosition parsePosition = new ParsePosition(0);
Number n = nf.parse(newGrade,parsePosition);
if (parsePosition.getIndex() != newGrade.length())
throw new NumberFormatException("Grade has a bad format.");
Double dValue = n.doubleValue();

for (Entry<String, Double> entry : schema.entrySet()) {
Double tempValue = entry.getValue();
if (dValue.equals(tempValue)) {
return entry.getKey();
}
else {
if (maxValue.compareTo(tempValue) < 0) maxValue=tempValue;
if ((dValue.compareTo(tempValue) > 0 ) && (tempValue.compareTo(currentGradeValue) >= 0 )) {
currentGradeValue = tempValue;
newGrade=entry.getKey();
}
}
if (dValue.compareTo(maxValue) > 0) throw new NumberFormatException("Grade exceeds the maximum number allowed in current scale.");
}
return newGrade;
}
catch (NumberFormatException e) {
throw new NumberFormatException("Grade is not a number, neither a scale value.");
}
}

}

0 comments on commit 504a97d

Please sign in to comment.