Skip to content

Commit

Permalink
SAK-46088 GBNG > improve performance of 'Set Score for Empty Cells' a…
Browse files Browse the repository at this point in the history
…lgorithm (sakaiproject#9650)
  • Loading branch information
bjones86 authored Aug 24, 2021
1 parent e1a44f1 commit a4cbede
Showing 1 changed file with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2247,48 +2247,44 @@ public boolean updateUngradedItems(final long assignmentId, final double grade)
public boolean updateUngradedItems(final long assignmentId, final double grade, final GbGroup group) {
final String siteId = getCurrentSiteId();
final Gradebook gradebook = getGradebook(siteId);
final Assignment assignment = getAssignment(assignmentId);

// get students
final List<String> studentUuids = (group == null) ? this.getGradeableUsers() : this.getGradeableUsers(group);

// get grades (only returns those where there is a grade)
final List<GradeDefinition> defs = this.gradebookService.getGradesForStudentsForItem(gradebook.getUid(),
assignmentId, studentUuids);
// get grades (only returns those where there is a grade, or comment; does not return those where there is no grade AND no comment)
final List<GradeDefinition> defs = this.gradebookService.getGradesForStudentsForItem(gradebook.getUid(), assignmentId, studentUuids);

// iterate and trim the studentUuids list down to those that don't have
// grades
for (final GradeDefinition def : defs) {
// Remove students who already have a grade
studentUuids.removeIf(studentUUID -> defs.stream().anyMatch(def -> studentUUID.equals(def.getStudentUid()) && StringUtils.isNotBlank(def.getGrade())));
defs.removeIf(def -> StringUtils.isNotBlank(def.getGrade()));

// don't remove those where the grades are blank, they need to be
// updated too
if (StringUtils.isNotBlank(def.getGrade())) {
studentUuids.remove(def.getStudentUid());
// Create new GradeDefinition objects for those students who do not have one
for (String studentUUID : studentUuids) {
if (defs.stream().noneMatch(def -> studentUUID.equals(def.getStudentUid()))) {
GradeDefinition def = new GradeDefinition();
def.setStudentUid(studentUUID);
def.setGradeEntryType(gradebook.getGrade_type());
def.setGradeReleased(gradebook.isAssignmentsDisplayed() && assignment.isReleased());
defs.add(def);
}
}

if (studentUuids.isEmpty()) {
// Short circuit
if (defs.isEmpty()) {
log.debug("Setting default grade. No students are ungraded.");
}

try {
// for each student remaining, add the grade
for (final String studentUuid : studentUuids) {

log.debug("Setting default grade. Values of assignmentId: {}, studentUuid: {}, grade: {}", assignmentId, studentUuid, grade);

// TODO if this is slow doing it one by one, might be able to
// batch it

// The service needs it otherwise it will assume 'null'
// so pull it back from the service and poke it in there!
final String comment = getAssignmentGradeComment(Long.valueOf(assignmentId), studentUuid);

this.gradebookService.saveGradeAndCommentForStudent(gradebook.getUid(), assignmentId, studentUuid,
FormatHelper.formatGradeForDisplay(String.valueOf(grade)), comment);
}
// Apply the new grade to the GradeDefinitions to be updated
for (GradeDefinition def : defs) {
def.setGrade(Double.toString(grade));
log.debug("Setting default grade. Values of assignmentId: {}, studentUuid: {}, grade: {}", assignmentId, def.getStudentUid(), grade);
}

// Batch update the GradeDefinitions, and post an event on completion
try {
gradebookService.saveGradesAndComments(gradebook.getUid(), assignmentId, defs);
EventHelper.postUpdateUngradedEvent(gradebook, assignmentId, String.valueOf(grade), getUserRoleOrNone());

return true;
} catch (final Exception e) {
log.error("An error occurred updating the assignment", e);
Expand Down

0 comments on commit a4cbede

Please sign in to comment.