Skip to content

Commit

Permalink
SAK-49584 Gradebook GradeType ordinal starts at 1 (sakaiproject#12209)
Browse files Browse the repository at this point in the history
  • Loading branch information
ern authored Dec 20, 2023
1 parent cc31e07 commit df583be
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,27 @@
*/
package org.sakaiproject.grading.api;

import java.util.HashMap;
import java.util.Map;

/**
* The grading types that a gradebook could be configured as
*/
public enum GradeType {

POINTS,
PERCENTAGE,
LETTER;
POINTS(1),
PERCENTAGE(2),
LETTER(3);

/*
private int value;
private final int value;

GradeType(int value) {
this.value = value;
}
*/

/**
* Get the value for the type
*
* @return
*/
/*
public int getValue() {
return this.value;
public GradeType getValue() {
return GradeType.values()[this.value];
}

// maintain a map of the types so we can lookup the enum based on type
private static Map<Integer, GradeType> map = new HashMap<>();
static {
for (final GradeType type : GradeType.values()) {
map.put(type.value, type);
}
public boolean equals(GradeType gradeType) {
return this.value == gradeType.value;
}
public static GradeType valueOf(final int value) {
return map.get(value);
}
*/
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@
import java.math.RoundingMode;

public interface GradingConstants {

// These have been deprecated in favour of the {@link GradingType} enum
@Deprecated
public static final int GRADE_TYPE_POINTS = 1;
@Deprecated
public static final int GRADE_TYPE_PERCENTAGE = 2;
@Deprecated
public static final int GRADE_TYPE_LETTER = 3;

public static final int CATEGORY_TYPE_NO_CATEGORY = 1;
public static final int CATEGORY_TYPE_ONLY_CATEGORY = 2;
public static final int CATEGORY_TYPE_WEIGHTED_CATEGORY = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@
public interface GradingService extends EntityProducer {
// Application service hooks.

// These have been deprecated in favour of the {@link GradingType} enum
@Deprecated
public static final int GRADE_TYPE_POINTS = 1;
@Deprecated
public static final int GRADE_TYPE_PERCENTAGE = 2;
@Deprecated
public static final int GRADE_TYPE_LETTER = 3;

public static final int CATEGORY_TYPE_NO_CATEGORY = 1;
public static final int CATEGORY_TYPE_ONLY_CATEGORY = 2;
public static final int CATEGORY_TYPE_WEIGHTED_CATEGORY = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,11 @@ public String getItemType() {
return this.itemType;
}

if (gb.getGradeType() == GradeType.POINTS) {
if (GradeType.POINTS.equals(gb.getGradeType())) {
this.itemType = item_type_points;
} else if (gb.getGradeType() == GradeType.PERCENTAGE) {
} else if (GradeType.PERCENTAGE.equals(gb.getGradeType())) {
this.itemType = item_type_percentage;
} else if (gb.getGradeType() == GradeType.LETTER) {
} else if (GradeType.LETTER.equals(gb.getGradeType())) {
this.itemType = item_type_letter;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,15 +533,15 @@ public GradeDefinition getGradeDefinitionForStudentForItem(final String gradeboo

gradeDef.setExcused(gradeRecord.getExcludedFromGrade());

if (gradebook.getGradeType() == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradebook.getGradeType())) {
final List<AssignmentGradeRecord> gradeList = new ArrayList<>();
gradeList.add(gradeRecord);
convertPointsToLetterGrade(gradebook, gradeList);
final AssignmentGradeRecord gradeRec = gradeList.get(0);
if (gradeRec != null) {
gradeDef.setGrade(gradeRec.getLetterEarned());
}
} else if (gradebook.getGradeType() == GradeType.PERCENTAGE) {
} else if (GradeType.PERCENTAGE.equals(gradebook.getGradeType())) {
final Double percent = calculateEquivalentPercent(assignment.getPointsPossible(),
gradeRecord.getPointsEarned());
if (percent != null) {
Expand Down Expand Up @@ -862,12 +862,12 @@ public void updateAssignment(final String gradebookUid, final Long assignmentId,
// check if we need to scale the grades
boolean scaleGrades = false;
final Double originalPointsPossible = assignment.getPointsPossible();
if (gradebook.getGradeType() == GradeType.PERCENTAGE
if (GradeType.PERCENTAGE.equals(gradebook.getGradeType())
&& !assignment.getPointsPossible().equals(assignmentDefinition.getPoints())) {
scaleGrades = true;
}

if (gradebook.getGradeType() == GradeType.POINTS && assignmentDefinition.getScaleGrades()) {
if (GradeType.POINTS.equals(gradebook.getGradeType()) && assignmentDefinition.getScaleGrades()) {
scaleGrades = true;
}

Expand Down Expand Up @@ -983,7 +983,7 @@ private double getTotalPointsInternal(final Gradebook gradebook, final List<Cate
final boolean literalTotal) {

final GradeType gbGradeType = gradebook.getGradeType();
if (gbGradeType != GradeType.POINTS && gbGradeType != GradeType.PERCENTAGE) {
if (!GradeType.POINTS.equals(gbGradeType) && !GradeType.PERCENTAGE.equals(gbGradeType)) {
log.error("Wrong grade type in GradebookCalculationImpl.getTotalPointsInternal");
return -1;
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ private List<Double> getTotalPointsEarnedInternal(final String studentId, final
final List<AssignmentGradeRecord> gradeRecs, final List<GradebookAssignment> countedAssigns) {

final GradeType gbGradeType = gradebook.getGradeType();
if (gbGradeType != GradeType.POINTS && gbGradeType != GradeType.PERCENTAGE) {
if (!GradeType.POINTS.equals(gbGradeType) && !GradeType.PERCENTAGE.equals(gbGradeType)) {
log.error("Wrong grade type in GradebookCalculationImpl.getTotalPointsEarnedInternal");
return Collections.<Double>emptyList();
}
Expand Down Expand Up @@ -1690,9 +1690,9 @@ public List<GradeDefinition> getGradesForStudentsForItem(final String gradebookU
final List<String> studentsWithGradeRec = new ArrayList<>();
final List<AssignmentGradeRecord> gradeRecs = getAllAssignmentGradeRecordsForGbItem(gradableObjectId, studentIds);
if (gradeRecs != null) {
if (gradebook.getGradeType() == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradebook.getGradeType())) {
convertPointsToLetterGrade(gradebook, gradeRecs);
} else if (gradebook.getGradeType() == GradeType.PERCENTAGE) {
} else if (GradeType.PERCENTAGE.equals(gradebook.getGradeType())) {
convertPointsToPercentage(gradebook, gradeRecs);
}

Expand Down Expand Up @@ -1789,9 +1789,9 @@ private GradeDefinition convertGradeRecordToGradeDefinition(final AssignmentGrad
final GradeType gradeEntryType = gradebook.getGradeType();
gradeDef.setGradeEntryType(gradeEntryType);
String grade = null;
if (gradeEntryType == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradeEntryType)) {
grade = gradeRecord.getLetterEarned();
} else if (gradeEntryType == GradeType.PERCENTAGE) {
} else if (GradeType.PERCENTAGE.equals(gradeEntryType)) {
final Double percentEarned = gradeRecord.getPercentEarned();
grade = percentEarned != null ? percentEarned.toString() : null;
} else {
Expand Down Expand Up @@ -1821,7 +1821,7 @@ public boolean isGradeValid(String gradebookUuid, String grade) {
Gradebook gradebook = getGradebook(gradebookUuid);
GradeType gradeType = getGradebook(gradebookUuid).getGradeType();
LetterGradePercentMapping mapping = null;
if (gradeType == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradeType)) {
mapping = getLetterGradePercentMapping(gradebook);
}

Expand Down Expand Up @@ -1868,8 +1868,7 @@ private boolean isGradeValid(final String grade, final GradeType gradeEntryType,

} else {

if (gradeEntryType == GradeType.POINTS ||
gradeEntryType == GradeType.PERCENTAGE) {
if (GradeType.POINTS.equals(gradeEntryType) || GradeType.PERCENTAGE.equals(gradeEntryType)) {
try {
final NumberFormat nbFormat = NumberFormat.getInstance(resourceLoader.getLocale());
final Double gradeAsDouble = nbFormat.parse(grade).doubleValue();
Expand All @@ -1892,7 +1891,7 @@ private boolean isGradeValid(final String grade, final GradeType gradeEntryType,
log.debug("Passed grade is not a numeric value");
}

} else if (gradeEntryType == GradeType.LETTER) {
} else if (GradeType.LETTER.equals(gradeEntryType)) {
if (gradeMapping == null) {
throw new IllegalArgumentException("Null mapping passed to isGradeValid for a letter grade-based gradeook");
}
Expand Down Expand Up @@ -1923,7 +1922,7 @@ public List<String> identifyStudentsWithInvalidGrades(final String gradebookUid,
GradeType gradeType = gradebook.getGradeType();

LetterGradePercentMapping gradeMapping = null;
if (gradeType == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradeType)) {
gradeMapping = getLetterGradePercentMapping(gradebook);
}

Expand Down Expand Up @@ -2086,7 +2085,7 @@ public void saveGradesAndComments(final String gradebookUid, final Long gradable
final String graderId = sessionManager.getCurrentSessionUserId();
final Date now = new Date();
LetterGradePercentMapping mapping = null;
if (gradebook.getGradeType() == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradebook.getGradeType())) {
mapping = getLetterGradePercentMapping(gradebook);
}

Expand Down Expand Up @@ -2235,16 +2234,15 @@ private Double convertInputGradeToPoints(final GradeType gradeEntryType, final L
}

Double convertedValue = null;
if (gradeEntryType == GradeType.POINTS) {
if (GradeType.POINTS.equals(gradeEntryType)) {
try {
final NumberFormat nbFormat = NumberFormat.getInstance(resourceLoader.getLocale());
final Double pointValue = nbFormat.parse(grade).doubleValue();
convertedValue = pointValue;
} catch (NumberFormatException | ParseException nfe) {
throw new InvalidGradeException("Invalid grade passed to convertInputGradeToPoints");
}
} else if (gradeEntryType == GradeType.PERCENTAGE ||
gradeEntryType == GradeType.LETTER) {
} else if (GradeType.PERCENTAGE.equals(gradeEntryType) || GradeType.LETTER.equals(gradeEntryType)) {

// for letter or %-based grading, we need to calculate the equivalent point value
if (gbItemPointsPossible == null) {
Expand All @@ -2253,7 +2251,7 @@ private Double convertInputGradeToPoints(final GradeType gradeEntryType, final L
}

Double percentage = null;
if (gradeEntryType == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradeEntryType)) {
if (mapping == null) {
throw new IllegalArgumentException("No mapping passed to convertInputGradeToPoints for a letter-based gb");
}
Expand Down Expand Up @@ -2533,10 +2531,9 @@ public String getLowestPossibleGradeForGbItem(final String gradebookUid, final L

if (gbItem.getUngraded()) {
lowestPossibleGrade = null;
} else if (gradebook.getGradeType() == GradeType.PERCENTAGE ||
gradebook.getGradeType() == GradeType.POINTS) {
} else if (GradeType.PERCENTAGE.equals(gradebook.getGradeType()) || GradeType.POINTS.equals(gradebook.getGradeType())) {
lowestPossibleGrade = "0";
} else if (gbItem.getGradebook().getGradeType() == GradeType.LETTER) {
} else if (GradeType.LETTER.equals(gbItem.getGradebook().getGradeType())) {
final LetterGradePercentMapping mapping = getLetterGradePercentMapping(gradebook);
lowestPossibleGrade = mapping.getGrade(0d);
}
Expand Down Expand Up @@ -3058,9 +3055,9 @@ public Optional<CategoryScoreData> calculateCategoryScore(final Object gradebook
Double grade;

// determine the grade we should be using depending on the grading type
if (gb.getGradeType() == GradeType.PERCENTAGE) {
if (GradeType.PERCENTAGE.equals(gb.getGradeType())) {
grade = calculateEquivalentPointValueForPercent(pointsPossible, NumberUtils.createDouble(rawGrade));
} else if (gb.getGradeType() == GradeType.LETTER) {
} else if (GradeType.LETTER.equals(gb.getGradeType())) {
grade = gradingSchema.get(rawGrade);
} else {
grade = NumberUtils.createDouble(rawGrade);
Expand Down Expand Up @@ -3712,7 +3709,7 @@ private void scaleGrades(final Gradebook gradebook, final GradebookAssignment as
final String currentUserUid = sessionManager.getCurrentSessionUserId();

// scale for total points changed when on percentage grading
if (gradebook.getGradeType() == GradeType.PERCENTAGE && assignment.getPointsPossible() != null) {
if (GradeType.PERCENTAGE.equals(gradebook.getGradeType()) && assignment.getPointsPossible() != null) {

log.debug("Scaling percentage grades");

Expand All @@ -3732,7 +3729,7 @@ private void scaleGrades(final Gradebook gradebook, final GradebookAssignment as
}
}
}
else if (gradebook.getGradeType() == GradeType.POINTS && assignment.getPointsPossible() != null) {
else if (GradeType.POINTS.equals(gradebook.getGradeType()) && assignment.getPointsPossible() != null) {

log.debug("Scaling point grades");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ public GradeSaveResponse saveGrade(final Long assignmentId, final String student
",".equals(formattedText.getDecimalSeparator()) ? "," : ".");
}

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

Expand Down Expand Up @@ -1154,7 +1154,7 @@ public List<GbGradeComparisonItem> buildMatrixForGradeComparison(Assignment assi
el.setIsCurrentUser(userEid.equals(el.getEid()));

el.setGrade(FormatHelper.formatGrade(el.getGrade()) + (
GradeType.PERCENTAGE == gradingType ? "%" : ""
GradeType.PERCENTAGE.equals(gradingType) ? "%" : ""
));
return el;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private String determineKeyForGrade(final double percentage, final int range) {
}

private double getPercentage(final Double grade, final Assignment assignment, final GradeType gradingType) {
if (GradeType.PERCENTAGE == gradingType) {
if (GradeType.PERCENTAGE.equals(gradingType)) {
return grade;
} else {
return grade / assignment.getPoints() * 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ private Map<String, Object> serializeSettings() {
result.put("isCourseLetterGradeDisplayed", this.settings.getCourseLetterGradeDisplayed());
result.put("isCourseAverageDisplayed", this.settings.getCourseAverageDisplayed());
result.put("isCoursePointsDisplayed", this.settings.getCoursePointsDisplayed());
result.put("isPointsGradeEntry", this.settings.getGradeType() == GradeType.POINTS);
result.put("isPercentageGradeEntry", this.settings.getGradeType() == GradeType.PERCENTAGE);
result.put("isPointsGradeEntry", GradeType.POINTS.equals(this.settings.getGradeType()));
result.put("isPercentageGradeEntry", GradeType.PERCENTAGE.equals(this.settings.getGradeType()));
result.put("isCategoriesEnabled", this.settings.getCategoryType() != GradingCategoryType.NO_CATEGORY);
result.put("isCategoryTypeWeighted", this.settings.getCategoryType() == GradingCategoryType.WEIGHTED_CATEGORY);
result.put("isStudentOrderedByLastName", this.uiSettings.getNameSortOrder() == GbStudentNameSortOrder.LAST_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void error(final IValidationError error) {

// points
final Label pointsLabel = new Label("pointsLabel");
if (gradingType == GradeType.PERCENTAGE) {
if (GradeType.PERCENTAGE.equals(gradingType)) {
pointsLabel.setDefaultModel(new ResourceModel("label.addgradeitem.percentage"));
} else {
pointsLabel.setDefaultModel(new ResourceModel("label.addgradeitem.points"));
Expand Down Expand Up @@ -155,7 +155,7 @@ public void error(final IValidationError error) {
protected void onUpdate(final AjaxRequestTarget target) {

// conditional option to scale
if (gradingType == GradeType.POINTS) {
if (GradeType.POINTS.equals(gradingType)) {

final Double existing = AddOrEditGradeItemPanelContent.this.existingPoints;
final Double current = points.getModelObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ public boolean isVisible() {
assignmentItem.add(dueDate);

final WebMarkupContainer gradeScore = new WebMarkupContainer("gradeScore");
if (GradeType.PERCENTAGE == gradeType) {
if (GradeType.PERCENTAGE.equals(gradeType)) {
gradeScore.add(new Label("grade",
new StringResourceModel("label.percentage.valued", null,
new Object[] { FormatHelper.formatGrade(rawGrade) })) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ public String getIdValue(final GbGroup g, final int index) {

private boolean getExtraCredit(Double grade, Assignment assignment, GradeType gradeType) {

return (GradeType.PERCENTAGE == gradeType && grade > 100) ||
(GradeType.POINTS == gradeType && grade > assignment.getPoints());
return (GradeType.PERCENTAGE.equals(gradeType) && grade > 100) ||
(GradeType.POINTS.equals(gradeType) && grade > assignment.getPoints());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4539,11 +4539,11 @@ private void setUpGradeInformation(String gradebookUid, String selAssignmentName

// get the grade entry type for the gradebook
GradeType gradeEntryType = gradingService.getGradeEntryType(gradebookUid);
if (gradeEntryType == GradeType.LETTER) {
if (GradeType.LETTER.equals(gradeEntryType)) {
gradeByLetter = true;
gradeByPoints = false;
gradeByPercent = false;
} else if (gradeEntryType == GradeType.PERCENTAGE) {
} else if (GradeType.PERCENTAGE.equals(gradeEntryType)) {
gradeByLetter = false;
gradeByPoints = false;
gradeByPercent = true;
Expand Down Expand Up @@ -6080,7 +6080,7 @@ private boolean validateGradeInput()
if (!gradeValid) {
// see if we can figure out why
String errorMessageRef = GRADE_INVALID_GENERIC;
if (gradingService.getGradeEntryType(gradebookUid) != GradeType.LETTER) {
if (!GradeType.LETTER.equals(gradingService.getGradeEntryType(gradebookUid))) {
if(!isNumber(gradePoint))
{
errorMessageRef = GRADE_GREATER_ZERO;
Expand Down
Loading

0 comments on commit df583be

Please sign in to comment.