Skip to content

Commit

Permalink
SAK-33107 add the ability to scale point grades (sakaiproject#6095)
Browse files Browse the repository at this point in the history
* SAK-33107 add the ability to scale point grades

* Back out the codestyle changes
SAK-33107

* SAK-33107 Add back in scaleGrades changes

* Removed duplicate import
SAK-33107
  • Loading branch information
steveswinsburg authored Oct 17, 2018
1 parent 6b3b233 commit f0aedb7
Show file tree
Hide file tree
Showing 8 changed files with 293 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,87 +24,154 @@
import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang3.builder.CompareToBuilder;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.builder.CompareToBuilder;

/**
* JavaBean to hold data associated with a Gradebook assignment.
* The Course Grade is not considered an assignment.
* JavaBean to hold data associated with a Gradebook assignment. The Course Grade is not considered an assignment.
*/
@NoArgsConstructor
@ToString
public class Assignment implements Serializable, Comparable<Assignment> {
private static final long serialVersionUID = 1L;

/**
* the id of the assignment in the gradebook
* @return Returns the name of the assignment. The assignment name is unique among currently defined assignments. However, it is not a
* safe UID for persistance, since an assignment can be renamed. Also, an assignment can be deleted and a new assignment can be
* created re-using the old name.
*/
@Getter @Setter private Long id;
@Getter
@Setter
private String name;

/**
* the assignment name is unique among currently defined assignments. However, it is not a safe UID for persistance,
* since an assignment can be renamed. Also, an assignment can be deleted and a new assignment can be created re-using the old name.
*
* @return Returns the ID of the assignment in the gradebook
*/
@Getter @Setter private String name;
@Getter
@Setter
private Long id;

/**
* the total points the assignment is worth.
*/
@Getter @Setter private Double points;
@Getter
@Setter
private Double points;

/**
* the due date for the assignment, or null if none is defined.
*/
@Getter @Setter private Date dueDate;
@Getter
@Setter
private Date dueDate;

/**
* true if the assignment is maintained by some software other than the Gradebook itself.
*/
@Getter @Setter private boolean externallyMaintained;
/**
* true if the assignment is maintained by some software other than the Gradebook itself.
*/
@Getter
@Setter
private boolean externallyMaintained;

/**
* the external id, or null if the assignment is maintained by the Gradebook
*/
@Getter @Setter private String externalId;
/**
* the external id, or null if the assignment is maintained by the Gradebook
*/
@Getter
@Setter
private String externalId;

/**
* the external app name, or null if the assignment is maintained by the Gradebook
*/
@Getter @Setter private String externalAppName;
/**
* the external app name, or null if the assignment is maintained by the Gradebook
*/
@Getter
@Setter
private String externalAppName;

/**
* the external data, or null if the assignment is maintained by the Gradebook
*/
@Getter @Setter private String externalData;
@Getter
@Setter
private String externalData;

/**
* true if the assignment has been released for view to students
*/
@Getter @Setter private boolean released;
/**
* true if the assignment has been released for view to students
*/
@Getter
@Setter
private boolean released;

/**
* Note that any calls setSortOrder will not be persisted, if you want to change the sort order of an assignment you must call
* GradebookService.updateAssignmentOrder as that method properly handles the reordering of all other assignments for the gradebook.
*/
@Getter @Setter private Integer sortOrder;

@Getter @Setter private boolean counted;
@Getter @Setter private String categoryName;
@Getter @Setter private Double weight;
@Getter @Setter private boolean ungraded;
@Getter @Setter private boolean extraCredit;
@Getter @Setter private boolean categoryExtraCredit;
@Getter @Setter private Long categoryId;
@Getter @Setter private Integer categoryOrder;
@Getter @Setter private Integer categorizedSortOrder;
@Getter
@Setter
private Integer sortOrder;

@Getter
@Setter
private boolean counted;
@Getter
@Setter
private String categoryName;
@Getter
@Setter
private Double weight;
@Getter
@Setter
private boolean ungraded;
@Getter
@Setter
private boolean extraCredit;
@Getter
@Setter
private boolean categoryExtraCredit;
@Getter
@Setter
private Long categoryId;
@Getter
@Setter
private Integer categoryOrder;
@Getter
@Setter
private Integer categorizedSortOrder;

/**
* For editing. Not persisted.
*/
@Getter
@Setter
private boolean scaleGrades;

@Override
public int compareTo(Assignment o) {
public int compareTo(final Assignment o) {
return new CompareToBuilder()
.append(this.id, o.id)
.toComparison();
.append(this.id, o.id)
.toComparison();
}

@Override
public boolean equals(final Object o) {
if (!(o instanceof Assignment)) {
return false;
}
final Assignment other = (Assignment) o;
return new EqualsBuilder()
.append(this.id, other.id)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.append(this.id)
.toHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -612,11 +610,14 @@ public Object doInHibernate(final Session session) throws HibernateException {
}

// check if we need to scale the grades
// this will be if we have percentage grading and we change the points possible
boolean scaleGrades = false;
final Double originalPointsPossible = assignment.getPointsPossible();
if (gradebook.getGrade_type() == GradebookService.GRADE_TYPE_PERCENTAGE
&& !assignment.getPointsPossible().equals(assignmentDefinition.getPoints())) {
&& !assignment.getPointsPossible().equals(assignmentDefinition.getPoints())) {
scaleGrades = true;
}

if (gradebook.getGrade_type() == GradebookService.GRADE_TYPE_POINTS && assignmentDefinition.isScaleGrades()) {
scaleGrades = true;
}

Expand Down Expand Up @@ -647,7 +648,7 @@ public Object doInHibernate(final Session session) throws HibernateException {
updateAssignment(assignment);

if (scaleGrades) {
convertGradePointsForUpdatedTotalPoints(gradebook, assignment, originalPointsPossible);
scaleGrades(gradebook, assignment, originalPointsPossible);
}

return null;
Expand Down Expand Up @@ -3617,7 +3618,8 @@ public List<GradingEvent> getGradingEvents(final List<Long> assignmentIds, final
* @param gradebook the gradebook
* @param assignment assignment with original total point value
*/
private void convertGradePointsForUpdatedTotalPoints(final Gradebook gradebook, final GradebookAssignment assignment, final Double originalPointsPossible) {
private void scaleGrades(final Gradebook gradebook, final GradebookAssignment assignment,
final Double originalPointsPossible) {
if (gradebook == null || assignment == null || assignment.getPointsPossible() == null) {
throw new IllegalArgumentException("null values found in convertGradePointsForUpdatedTotalPoints.");
}
Expand All @@ -3627,21 +3629,49 @@ private void convertGradePointsForUpdatedTotalPoints(final Gradebook gradebook,

// scale for total points changed when on percentage grading
// TODO could scale for total points changed when on a points grading as well, though needs different logic
if (gradebook.getGrade_type() == GradebookService.GRADE_TYPE_PERCENTAGE) {
if (gradebook.getGrade_type() == GradebookService.GRADE_TYPE_PERCENTAGE && assignment.getPointsPossible() != null) {

// Calculate the new points value that we should be persisting based on the new total points
if (assignment.getPointsPossible() != null) {
for (final AssignmentGradeRecord gr : gradeRecords) {
if (gr.getPointsEarned() != null) {
final BigDecimal scoreAsPercentage = (new BigDecimal(gr.getPointsEarned())
.divide(new BigDecimal(originalPointsPossible), GradebookService.MATH_CONTEXT))
.multiply(new BigDecimal(100));
log.debug("Scaling percentage grades");

final Double scaledScore = calculateEquivalentPointValueForPercent(assignment.getPointsPossible(),
scoreAsPercentage.doubleValue());
for (final AssignmentGradeRecord gr : gradeRecords) {
if (gr.getPointsEarned() != null) {
final BigDecimal scoreAsPercentage = (new BigDecimal(gr.getPointsEarned())
.divide(new BigDecimal(originalPointsPossible), GradebookService.MATH_CONTEXT))
.multiply(new BigDecimal(100));

gr.setPointsEarned(scaledScore);
}
final Double scaledScore = calculateEquivalentPointValueForPercent(assignment.getPointsPossible(),
scoreAsPercentage.doubleValue());

log.debug("scoreAsPercentage: " + scoreAsPercentage);
log.debug("scaledScore: " + scaledScore);

gr.setPointsEarned(scaledScore);
}
}
}

if (gradebook.getGrade_type() == GradebookService.GRADE_TYPE_POINTS && assignment.getPointsPossible() != null) {

log.debug("Scaling point grades");

final BigDecimal previous = new BigDecimal(originalPointsPossible);
final BigDecimal current = new BigDecimal(assignment.getPointsPossible());
final BigDecimal factor = current.divide(previous, GradebookService.MATH_CONTEXT);

log.debug("previous points possible: " + previous);
log.debug("current points possible: " + current);
log.debug("factor: " + factor);

for (final AssignmentGradeRecord gr : gradeRecords) {
if (gr.getPointsEarned() != null) {

final BigDecimal currentGrade = new BigDecimal(gr.getPointsEarned());
final BigDecimal scaledGrade = currentGrade.multiply(factor, GradebookService.MATH_CONTEXT);

log.debug("currentGrade: " + currentGrade);
log.debug("scaledGrade: " + scaledGrade);

gr.setPointsEarned(scaledGrade.doubleValue());
}
}
}
Expand Down Expand Up @@ -3701,4 +3731,4 @@ private void excludeUncategorisedItemsFromCourseGradeCalculations(final Gradeboo
assignments.forEach(a -> a.setCounted(false));
batchPersistEntities(assignments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ label.addgradeitem.include = Include item in course grade calculations?
label.addgradeitem.nocategories = Categories have not been configured.
label.addgradeitem.categorywithweight = {0} ({1})
label.addgradeitem.createanother = Create Another
label.addgradeitem.scalegrades = Scale existing grades?
error.addgradeitem.points = Points required and must be a number greater than zero.
error.addgradeitem.title = Title required and must be unique.
error.addgradeitem.title.duplicate = The title ''{0}'' is a duplicate. Titles must be unique.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

package org.sakaiproject.gradebookng.tool.model;

/**
* Generic enum to signal an add or edit
*/
public enum UiMode {
ADD,
EDIT;
}
Loading

0 comments on commit f0aedb7

Please sign in to comment.