Skip to content

Commit

Permalink
SAK-49493 LTI fix gradebook uid bug and improve initialization (#12966)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgarciaentornos authored and ern committed Nov 1, 2024
1 parent 9e54aa5 commit 4f7b791
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -933,5 +933,6 @@ public void updateExternalAssessmentComments(String gradebookUid, String siteId,
public Map<String, String> buildCategoryGradebookMap(List<String> selectedGradebookUids, String categoriesString, String siteId);
public Long getMatchingUserGradebookItemId(String siteId, String userId, String gradebookItemIdString);
public List<String> getGradebookInstancesForUser(String siteId, String userId);
public void initializeGradebooksForSite(String siteId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5030,6 +5030,17 @@ public List<String> getGradebookInstancesForUser(String siteId, String userId) {
return userGradebooks;
}

@Override
public void initializeGradebooksForSite(String siteId) {
List<String> gradebookUids = Arrays.asList(siteId);
if (isGradebookGroupEnabled(siteId)) {
gradebookUids = getGradebookGroupInstancesIds(siteId);
}
for (String gradebookUid : gradebookUids) {
getGradebook(gradebookUid, siteId);
}
}

private void createDefaultLetterGradeMapping(final Map gradeMap) {

if (getDefaultLetterGradePercentMapping().isEmpty()) {
Expand Down
50 changes: 20 additions & 30 deletions lti/lti-common/src/java/org/sakaiproject/lti/util/SakaiLTIUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1166,13 +1166,7 @@ public static String[] postLaunchHTML(Map<String, Object> content, Map<String, O

// SAK-47573 - Make sure the gradebook is initialised
GradingService gradingService = (GradingService) ComponentManager.get("org.sakaiproject.grading.api.GradingService");
List<String> gradebookUids = Arrays.asList(context);
if (gradingService.isGradebookGroupEnabled(context)) {
gradebookUids = gradingService.getGradebookGroupInstancesIds(context);
}
for (String gradebookUid : gradebookUids) {
Gradebook gb = gradingService.getGradebook(gradebookUid, context);
}
gradingService.initializeGradebooksForSite(context);

// See if there are the necessary items
String secret = getSecret(tool, content);
Expand Down Expand Up @@ -1572,13 +1566,7 @@ public static String[] postContentItemSelectionRequest(Long toolKey, Map<String,

// SAK-47573 - Make sure the gradebook is initialised
GradingService gradingService = (GradingService) ComponentManager.get("org.sakaiproject.grading.api.GradingService");
List<String> gradebookUids = Arrays.asList(context);
if (gradingService.isGradebookGroupEnabled(context)) {
gradebookUids = gradingService.getGradebookGroupInstancesIds(context);
}
for (String gradebookUid : gradebookUids) {
Gradebook gb = gradingService.getGradebook(gradebookUid, context);
}
gradingService.initializeGradebooksForSite(context);

User user = UserDirectoryService.getCurrentUser();

Expand Down Expand Up @@ -2570,6 +2558,7 @@ private static Object handleGradebook(String sourcedid, HttpServletRequest reque
log.warn("gradebookColumn or Id is null, cannot proceed with grading in site {} for column {}", siteId, title);
return "Grade failure siteId=" + siteId;
}
String gradebookUid = gradebookColumn.getGradebookUid() != null ? gradebookColumn.getGradebookUid() : siteId;

try {
// Indicate "who" is setting this grade - needs to be a real user account
Expand All @@ -2580,35 +2569,35 @@ private static Object handleGradebook(String sourcedid, HttpServletRequest reque
sess.setUserId(gb_user_id);
sess.setUserEid(gb_user_eid);
if (isRead) {
String actualGrade = gradingService.getAssignmentScoreString(gradebookColumn.getGradebookUid(), siteId, gradebookColumn.getId(), user_id);
String actualGrade = gradingService.getAssignmentScoreString(gradebookUid, siteId, gradebookColumn.getId(), user_id);
Double dGrade = null;
if (StringUtils.isNotBlank(actualGrade)) {
dGrade = new Double(actualGrade);
dGrade = dGrade / gradebookColumn.getPoints();
}
CommentDefinition commentDef = gradingService.getAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), user_id);
CommentDefinition commentDef = gradingService.getAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), user_id);
Map<String, Object> retMap = new TreeMap<>();
retMap.put("grade", dGrade);
if (commentDef != null) {
retMap.put("comment", commentDef.getCommentText());
}
retval = retMap;
} else if (isDelete) {
gradingService.setAssignmentScoreString(gradebookColumn.getGradebookUid(), siteId, gradebookColumn.getId(), user_id, null, "External Outcome");
gradingService.deleteAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), user_id);
log.info("Delete Score site={} gradebook={} title={} user_id={}", siteId, gradebookColumn.getGradebookUid(), title, user_id);
gradingService.setAssignmentScoreString(gradebookUid, siteId, gradebookColumn.getId(), user_id, null, "External Outcome");
gradingService.deleteAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), user_id);
log.info("Delete Score site={} gradebook={} title={} user_id={}", siteId, gradebookUid, title, user_id);
retval = Boolean.TRUE;
} else {
String gradeI18n = getRoundedGrade(scoreGiven, gradebookColumn.getPoints());
gradeI18n = (",").equals((ComponentManager.get(FormattedText.class)).getDecimalSeparator()) ? gradeI18n.replace(".",",") : gradeI18n;
gradingService.setAssignmentScoreString(gradebookColumn.getGradebookUid(), siteId, gradebookColumn.getId(), user_id, gradeI18n, "External Outcome");
gradingService.setAssignmentScoreString(gradebookUid, siteId, gradebookColumn.getId(), user_id, gradeI18n, "External Outcome");
if ( StringUtils.isBlank(comment) ) {
gradingService.deleteAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), user_id);
gradingService.deleteAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), user_id);
} else {
gradingService.setAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), user_id, comment);
gradingService.setAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), user_id, comment);
}

log.info("Stored Score site={} gradebook={} title={} user_id={} score={}", siteId, gradebookColumn.getGradebookUid(), title, user_id, scoreGiven);
log.info("Stored Score site={} gradebook={} title={} user_id={} score={}", siteId, gradebookUid, title, user_id, scoreGiven);

retval = Boolean.TRUE;
}
Expand Down Expand Up @@ -2677,6 +2666,7 @@ public static Object handleGradebookLTI13(Site site, Long tool_id, Map<String,
// Look up the gradebook column so we can find the max points
GradingService gradingService = (GradingService) ComponentManager
.get("org.sakaiproject.grading.api.GradingService");
String gradebookUid = gradebookColumn.getGradebookUid() != null ? gradebookColumn.getGradebookUid() : siteId;

// Fall through to send the grade to a gradebook column
// Now read, set, or delete the grade...
Expand All @@ -2691,14 +2681,14 @@ public static Object handleGradebookLTI13(Site site, Long tool_id, Map<String,
sess.setUserId(gb_user_id);
sess.setUserEid(gb_user_eid);
if (scoreGiven == null) {
gradingService.setAssignmentScoreString(gradebookColumn.getGradebookUid(), siteId, gradebookColumn.getId(), userId, null, "External Outcome");
gradingService.setAssignmentScoreString(gradebookUid, siteId, gradebookColumn.getId(), userId, null, "External Outcome");
// Since LTI 13 uses update semantics on grade delete, we accept the comment if it is there
if ( StringUtils.isBlank(comment) ) {
gradingService.deleteAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), userId);
gradingService.deleteAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), userId);
} else {
gradingService.setAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), userId, comment);
gradingService.setAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), userId, comment);
}
log.info("Delete Score site={} gradebook={} title={} userId={}", siteId, gradebookColumn.getGradebookUid(), title, userId);
log.info("Delete Score site={} gradebook={} title={} userId={}", siteId, gradebookUid, title, userId);
return Boolean.TRUE;
} else {
Double gradebookColumnPoints = gradebookColumn.getPoints();
Expand All @@ -2712,11 +2702,11 @@ public static Object handleGradebookLTI13(Site site, Long tool_id, Map<String,
gradeI18n = (",").equals((ComponentManager.get(FormattedText.class)).getDecimalSeparator()) ? gradeI18n.replace(".",",") : gradeI18n;
gradingService.setAssignmentScoreString(gradebookColumn.getGradebookUid(), siteId, gradebookColumn.getId(), userId, gradeI18n, "External Outcome");
if ( StringUtils.isBlank(comment) ) {
gradingService.deleteAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), userId);
gradingService.deleteAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), userId);
} else {
gradingService.setAssignmentScoreComment(gradebookColumn.getGradebookUid(), gradebookColumn.getId(), userId, comment);
gradingService.setAssignmentScoreComment(gradebookUid, gradebookColumn.getId(), userId, comment);
}
log.info("Stored Score site={} gradebook={} title={} userId={} score={}", siteId, gradebookColumn.getGradebookUid(), title, userId, scoreGiven);
log.info("Stored Score site={} gradebook={} title={} userId={} score={}", siteId, gradebookUid, title, userId, scoreGiven);
return Boolean.TRUE;
}
} catch (NumberFormatException | AssessmentNotFoundException e) {
Expand Down

0 comments on commit 4f7b791

Please sign in to comment.