Skip to content

Commit

Permalink
Updates Adrian requested
Browse files Browse the repository at this point in the history
  • Loading branch information
ern committed May 6, 2024
1 parent bd7ceac commit b9258df
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
import lombok.Data;

/**
* This bean holds grading data that is later serialized to json.
* Depending on the users role in the site it will contain different
* information as follows:
*
* for Instructor roles
*
* This bean holds grading data that is later serialized to json.*
*/
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,128 +79,128 @@ public class GradesController extends AbstractSakaiApiController {
List<Long> assignmentIds = assignments.stream().map(org.sakaiproject.grading.api.Assignment::getId).collect(Collectors.toList());

// no need to continue if the site doesn't have gradebook items
if (!assignmentIds.isEmpty()) {
// collect site information
return siteService.getOptionalSite(siteId).map(site -> {
String userId = checkSakaiSession().getUserId();
Role role = site.getUserRole(userId);
boolean isMaintainer = StringUtils.equalsIgnoreCase(site.getMaintainRole(), role.getId());

List<String> userIds = isMaintainer ?
site.getRoles().stream()
if (assignmentIds.isEmpty()) return Collections.emptyList();

// collect site information
return siteService.getOptionalSite(siteId).map(site -> {
String userId = checkSakaiSession().getUserId();
Role role = site.getUserRole(userId);
boolean isMaintainer = StringUtils.equalsIgnoreCase(site.getMaintainRole(), role.getId());

List<String> userIds = isMaintainer
? site.getRoles().stream()
.map(Role::getId)
.filter(r -> !site.getMaintainRole().equals(r))
.flatMap(r -> site.getUsersHasRole(r).stream())
.collect(Collectors.toList())
: List.of(userId);
Map<Long, List<GradeDefinition>> gradeDefinitions = gradingService.getGradesWithoutCommentsForStudentsForItems(siteId, assignmentIds, userIds);

List<GradeRestBean> beans = new ArrayList<>();
// collect information for each gradebook item
for (org.sakaiproject.grading.api.Assignment a : assignments) {
GradeRestBean bean = new GradeRestBean(a);
bean.setSiteTitle(site.getTitle());
bean.setSiteRole(role.getId());

if (!a.getExternallyMaintained()) {
// collect information for internal gb item
List<GradeDefinition> gd = gradeDefinitions.get(a.getId());

if (isMaintainer) {
double total = 0;
for (GradeDefinition d : gd) {
if (Objects.equals(GradingConstants.GRADE_TYPE_POINTS, d.getGradeEntryType())) {
String grade = d.getGrade();
if (!StringUtils.isBlank(grade)) {
total += Double.parseDouble(grade);
}

Map<Long, List<GradeDefinition>> gradeDefinitions = gradingService.getGradesWithoutCommentsForStudentsForItems(siteId, assignmentIds, userIds);

List<GradeRestBean> beans = new ArrayList<>();
// collect information for each gradebook item
for (org.sakaiproject.grading.api.Assignment a : assignments) {
GradeRestBean bean = new GradeRestBean(a);
bean.setSiteTitle(site.getTitle());
bean.setSiteRole(role.getId());

if (!a.getExternallyMaintained()) {
// collect information for internal gb item
List<GradeDefinition> gd = gradeDefinitions.get(a.getId());

if (isMaintainer) {
double total = 0;
for (GradeDefinition d : gd) {
if (Objects.equals(GradingConstants.GRADE_TYPE_POINTS, d.getGradeEntryType())) {
String grade = d.getGrade();
if (!StringUtils.isBlank(grade)) {
total += Double.parseDouble(grade);
}
}
bean.setScore(total > 0 ? String.format("%.2f", total / gd.size()) : "-");
bean.setUngraded(userIds.size() - gd.size());
bean.setNotGradedYet(gd.isEmpty());
} else {
bean.setScore(!gd.isEmpty() ? StringUtils.trimToEmpty(gd.get(0).getGrade()) : "");
}

ToolConfiguration tc = site.getToolForCommonId("sakai.gradebookng");
String gbUrl = tc != null ? "/portal/directtool/" + tc.getId() : "";
bean.setUrl(gbUrl);
bean.setScore(total > 0 ? String.format("%.2f", total / gd.size()) : "-");
bean.setUngraded(userIds.size() - gd.size());
bean.setNotGradedYet(gd.isEmpty());
} else {
// collect information for external gb item
int submitted = 0, graded = 0;
double total = 0;
switch (a.getExternalAppName()) {
case AssignmentConstants.TOOL_ID:
bean.setUrl(entityManager.getUrl(a.getExternalId(), Entity.UrlType.PORTAL).orElse(""));
if (isMaintainer) {
try {
Assignment assignment = assignmentService.getAssignment(entityManager.newReference(a.getExternalId()));
for (AssignmentSubmission as : assignment.getSubmissions()) {
if (as.getUserSubmission()) submitted += 1;
if (as.getGraded()) graded += 1;
if (Assignment.GradeType.SCORE_GRADE_TYPE.equals(assignment.getTypeOfGrade())) {
String score = assignmentService.getGradeDisplay(as.getGrade(), assignment.getTypeOfGrade(), assignment.getScaleFactor());
if (NumberUtils.isParsable(score)) total += Double.parseDouble(score);
}
}
} catch (Exception e) {
log.warn("Could not access assignment [{}], {}", a.getExternalId(), e.toString());
}
} else {
try {
AssignmentSubmission submission = assignmentService.getSubmission(a.getExternalId(), userId);
if (submission != null && submission.getGraded() && submission.getGradeReleased()) {
bean.setNotGradedYet(false);
Assignment assignment = submission.getAssignment();
bean.setScore(assignmentService.getGradeDisplay(submission.getGrade(), assignment.getTypeOfGrade(), assignment.getScaleFactor()));
} else {
bean.setNotGradedYet(true);
bean.setScore(!gd.isEmpty() ? StringUtils.trimToEmpty(gd.get(0).getGrade()) : "");
}

ToolConfiguration tc = site.getToolForCommonId("sakai.gradebookng");
String gbUrl = tc != null ? "/portal/directtool/" + tc.getId() : "";
bean.setUrl(gbUrl);
} else {
// collect information for external gb item
int submitted = 0, graded = 0;
double total = 0;
switch (a.getExternalAppName()) {
case AssignmentConstants.TOOL_ID:
bean.setUrl(entityManager.getUrl(a.getExternalId(), Entity.UrlType.PORTAL).orElse(""));
if (isMaintainer) {
try {
Assignment assignment = assignmentService.getAssignment(entityManager.newReference(a.getExternalId()));
for (AssignmentSubmission as : assignment.getSubmissions()) {
if (as.getUserSubmission()) submitted += 1;
if (as.getGraded()) graded += 1;
if (Assignment.GradeType.SCORE_GRADE_TYPE.equals(assignment.getTypeOfGrade())) {
String score = assignmentService.getGradeDisplay(as.getGrade(), assignment.getTypeOfGrade(), assignment.getScaleFactor());
if (NumberUtils.isParsable(score)) total += Double.parseDouble(score);
}
} catch (Exception e) {
log.warn("Could not access submission for assignment [{}] user [{}], {}", a.getExternalId(), userId, e.toString());
}
} catch (Exception e) {
log.warn("Could not access assignment [{}], {}", a.getExternalId(), e.toString());
}
break;
case SamigoConstants.TOOL_ID:
GradingService samigoGradingService = new GradingService();
if (isMaintainer) {
List<AssessmentGradingData> gradings = samigoGradingService.getAllSubmissions(a.getExternalId());
for (AssessmentGradingData grading : gradings) {
if (Objects.equals(AssessmentGradingData.SUBMITTED, grading.getStatus())) submitted += 1;
// TODO: this is bum way of working out if an instructor has scored a quiz.
if (grading.getFinalScore() != null) {
if (grading.getFinalScore() != 0.0) graded += 1;
total = total + grading.getFinalScore();
}
}
} else {
AssessmentGradingData agr = samigoGradingService.getLastSubmittedAssessmentGradingByAgentId(a.getExternalId(), userId, null);
if (agr != null && Objects.equals(AssessmentGradingData.SUBMITTED, agr.getStatus()) && agr.getGradedDate() != null) {
} else {
try {
AssignmentSubmission submission = assignmentService.getSubmission(a.getExternalId(), userId);
if (submission != null && submission.getGraded() && submission.getGradeReleased()) {
bean.setNotGradedYet(false);
bean.setScore(agr.getFinalScore().toString());
Assignment assignment = submission.getAssignment();
bean.setScore(assignmentService.getGradeDisplay(submission.getGrade(), assignment.getTypeOfGrade(), assignment.getScaleFactor()));
} else {
bean.setNotGradedYet(true);
}
} catch (Exception e) {
log.warn("Could not access submission for assignment [{}] user [{}], {}", a.getExternalId(), userId, e.toString());
}
break;
default:
}
if (isMaintainer) {
bean.setScore(total > 0 ? String.format("%.2f", total / graded) : "-");
bean.setUngraded(submitted - graded);
}
if (StringUtils.isBlank(bean.getUrl())) bean.setUrl(gradingService.getUrlForAssignment(a));
}
break;
case SamigoConstants.TOOL_ID:
GradingService samigoGradingService = new GradingService();
if (isMaintainer) {
List<AssessmentGradingData> gradings = samigoGradingService.getAllSubmissions(a.getExternalId());
for (AssessmentGradingData grading : gradings) {
if (Objects.equals(AssessmentGradingData.SUBMITTED, grading.getStatus()))
submitted += 1;
// TODO: this is bum way of working out if an instructor has scored a quiz.
if (grading.getFinalScore() != null) {
if (grading.getFinalScore() != 0.0) graded += 1;
total = total + grading.getFinalScore();
}
}
} else {
AssessmentGradingData agr = samigoGradingService.getLastSubmittedAssessmentGradingByAgentId(a.getExternalId(), userId, null);
if (agr != null && Objects.equals(AssessmentGradingData.SUBMITTED, agr.getStatus()) && agr.getGradedDate() != null) {
bean.setNotGradedYet(false);
bean.setScore(agr.getFinalScore().toString());
} else {
bean.setNotGradedYet(true);
}
}
break;
default:
}

// add data to list
beans.add(bean);
if (isMaintainer) {
bean.setScore(total > 0 ? String.format("%.2f", total / graded) : "-");
bean.setUngraded(submitted - graded);
}
if (StringUtils.isBlank(bean.getUrl())) bean.setUrl(gradingService.getUrlForAssignment(a));
}
return beans;
}).orElse(Collections.emptyList());
} else {
return Collections.emptyList();
}

// add data to list
beans.add(bean);
}
return beans;
}).orElse(Collections.emptyList());
};

@GetMapping(value = "/users/me/grades", produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down

0 comments on commit b9258df

Please sign in to comment.