Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public enum IMSGradingProgress {
public static final String PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT = "prop_new_assignment_add_to_gradebook";

public static final String NEW_ASSIGNMENT_ADD_TO_GRADEBOOK = "new_assignment_add_to_gradebook";
public static final String NEW_ASSIGNMENT_CATEGORY = "new_assignment_category";

/**
* Sakai property key to change the default value for the 'Add due date to calendar' checkbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4704,14 +4704,21 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
nProperties.remove(PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT);
final String assignmentAddToGradebookChoice = nProperties.get(NEW_ASSIGNMENT_ADD_TO_GRADEBOOK);
final String nAssignmentRef = AssignmentReferenceReckoner.reckoner().assignment(nAssignment).reckon().getReference();
if (GRADEBOOK_INTEGRATION_ADD.equals(assignmentAddToGradebookChoice)) {
remapImportedAssignmentCategoryProperty(nProperties, fromContext, toContext);
} else {
nProperties.remove(NEW_ASSIGNMENT_CATEGORY);
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

switch (assignmentAddToGradebookChoice) {
case GRADEBOOK_INTEGRATION_ADD, GRADEBOOK_INTEGRATION_ASSOCIATE -> {
org.sakaiproject.grading.api.Assignment originalGBAssignment = null;
org.sakaiproject.grading.api.Assignment newGbAssignment = null;

boolean isOriginalAssignmentExternal = gradingService.isExternalAssignmentDefined(oAssignment.getContext(), associatedGradebookAssignment);
if (!isOriginalAssignmentExternal) {
if (isOriginalAssignmentExternal) {
originalGBAssignment = gradingService.getExternalAssignment(oAssignment.getContext(), associatedGradebookAssignment);
} else if (StringUtils.isNotBlank(associatedGradebookAssignment)) {
// load the assignment for internal gb link
try {
originalGBAssignment = gradingService.getAssignmentByNameOrId(
Expand All @@ -4735,16 +4742,18 @@ public Map<String, String> transferCopyEntities(String fromContext, String toCon
log.debug("Assignment {} not found in gradebook for site {}", originalGBAssignment.getName(), nAssignment.getContext(), anfe);
}
}
} else {
originalGBAssignment = gradingService.getExternalAssignment(oAssignment.getContext(), associatedGradebookAssignment);
}

if (nAssignment.getDraft()) {
// assignment is in the draft state
if (isOriginalAssignmentExternal) {
if (isOriginalAssignmentExternal || GRADEBOOK_INTEGRATION_ADD.equals(assignmentAddToGradebookChoice)) {
// if this is an external defined assignments will create when publishing
nProperties.remove(PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT);
nProperties.put(NEW_ASSIGNMENT_ADD_TO_GRADEBOOK, GRADEBOOK_INTEGRATION_ADD);
if (isOriginalAssignmentExternal && originalGBAssignment != null) {
createCategoryForGbAssignmentIfNecessary(originalGBAssignment, oAssignment.getContext(), nAssignment.getContext())
.ifPresent(categoryId -> nProperties.put(NEW_ASSIGNMENT_CATEGORY, categoryId.toString()));
}
} else {
if (newGbAssignment != null) {
if (StringUtils.isNotBlank(newGbAssignment.getId().toString())) {
Expand Down Expand Up @@ -5641,6 +5650,48 @@ private String generateUniqueAssignmentTitle(String originalTitle, String contex
return uniqueTitle;
}

private void remapImportedAssignmentCategoryProperty(Map<String, String> properties, String fromGradebookId, String toGradebookId) {

String categoryIds = StringUtils.trimToNull(properties.get(NEW_ASSIGNMENT_CATEGORY));
if (categoryIds == null) {
return;
}

Optional<Long> destinationCategoryId = createCategoryForAssignmentPropertyIfNecessary(categoryIds, fromGradebookId, toGradebookId);
if (destinationCategoryId.isPresent()) {
properties.put(NEW_ASSIGNMENT_CATEGORY, destinationCategoryId.get().toString());
} else {
properties.remove(NEW_ASSIGNMENT_CATEGORY);
}
}

private Optional<Long> createCategoryForAssignmentPropertyIfNecessary(String categoryIds, String fromGradebookId, String toGradebookId) {

List<String> selectedCategories = Arrays.stream(categoryIds.split(","))
.map(StringUtils::trimToNull)
.filter(Objects::nonNull)
.filter(categoryId -> !"-1".equals(categoryId))
.collect(Collectors.toList());

if (selectedCategories.isEmpty()) {
return Optional.empty();
}

// Category ids are gradebook-specific. Resolve the source id to a category name,
// then match or create the equivalent category in the destination gradebook.
Optional<CategoryDefinition> sourceCategory = gradingService.getCategoryDefinitions(fromGradebookId, fromGradebookId).stream()
.filter(category -> category.getId() != null && selectedCategories.contains(category.getId().toString()))
.findFirst();

if (!sourceCategory.isPresent()) {
return Optional.empty();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

org.sakaiproject.grading.api.Assignment sourceAssignment = new org.sakaiproject.grading.api.Assignment();
sourceAssignment.setCategoryName(sourceCategory.get().getName());
return createCategoryForGbAssignmentIfNecessary(sourceAssignment, fromGradebookId, toGradebookId);
}

private Optional<Long> createCategoryForGbAssignmentIfNecessary(
org.sakaiproject.grading.api.Assignment gbAssignment, String fromGradebookId,
String toGradebookId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
import org.sakaiproject.entity.api.ResourcePropertiesEdit;
import org.sakaiproject.entity.api.EntityTransferrer;
import org.sakaiproject.entity.api.ResourceProperties;
import org.sakaiproject.grading.api.CategoryDefinition;
import org.sakaiproject.grading.api.GradebookInformation;
import org.sakaiproject.grading.api.GradingConstants;
import org.sakaiproject.grading.api.GradingService;
import org.sakaiproject.site.api.SiteService;
import org.sakaiproject.time.api.UserTimeService;
import org.sakaiproject.tool.api.SessionManager;
Expand All @@ -89,6 +93,7 @@ public class AssignmentTransferCopyEntitiesTest extends AbstractTransactionalJUn
@Autowired private SecurityService securityService;
@Autowired private SessionManager sessionManager;
@Autowired private ServerConfigurationService serverConfigurationService;
@Autowired private GradingService gradingService;
@Resource(name = "org.sakaiproject.time.api.UserTimeService")
private UserTimeService userTimeService;
@Autowired private UserDirectoryService userDirectoryService;
Expand Down Expand Up @@ -204,6 +209,66 @@ public void transferCopyEntitiesRecreatesDraftOpenDateAnnouncementWhenImportDefa
verify(announcementChannel).commitMessage(message, 0, "org.sakaiproject.announcement.impl.SiteEmailNotificationAnnc");
}

@Test
public void transferCopyEntitiesStoresDestinationCategoryForImportedDraftFromExternalGradebookItem() throws Exception {

String fromContext = UUID.randomUUID().toString();
String toContext = UUID.randomUUID().toString();
Assignment sourceAssignment = createPublishedAssignment(fromContext);
sourceAssignment.setTypeOfGrade(Assignment.GradeType.SCORE_GRADE_TYPE);
String sourceAssignmentRef = AssignmentReferenceReckoner.reckoner().assignment(sourceAssignment).reckon().getReference();
sourceAssignment.getProperties().put(AssignmentConstants.NEW_ASSIGNMENT_ADD_TO_GRADEBOOK,
AssignmentConstants.GRADEBOOK_INTEGRATION_ASSOCIATE);
sourceAssignment.getProperties().put(AssignmentConstants.PROP_ASSIGNMENT_ASSOCIATE_GRADEBOOK_ASSIGNMENT,
sourceAssignmentRef);
updateAssignment(sourceAssignment);

stubContextPermissions(toContext);
stubCategoryImport(fromContext, toContext, 11L, 22L, "Essays");

org.sakaiproject.grading.api.Assignment sourceGradebookAssignment = new org.sakaiproject.grading.api.Assignment();
sourceGradebookAssignment.setCategoryName("Essays");
when(gradingService.isExternalAssignmentDefined(fromContext, sourceAssignmentRef)).thenReturn(true);
when(gradingService.getExternalAssignment(fromContext, sourceAssignmentRef)).thenReturn(sourceGradebookAssignment);

getAssignmentServiceImpl().transferCopyEntities(fromContext, toContext, null, null);

Collection<Assignment> importedAssignments = assignmentService.getAssignmentsForContext(toContext);
assertEquals(1, importedAssignments.size());
Assignment importedAssignment = importedAssignments.iterator().next();
assertTrue(importedAssignment.getDraft());
assertEquals(AssignmentConstants.GRADEBOOK_INTEGRATION_ADD,
importedAssignment.getProperties().get(AssignmentConstants.NEW_ASSIGNMENT_ADD_TO_GRADEBOOK));
assertEquals("22", importedAssignment.getProperties().get(AssignmentConstants.NEW_ASSIGNMENT_CATEGORY));
}

@Test
public void transferCopyEntitiesRemapsDraftAssignmentCategoryProperty() throws Exception {

String fromContext = UUID.randomUUID().toString();
String toContext = UUID.randomUUID().toString();
Assignment sourceAssignment = createPublishedAssignment(fromContext);
sourceAssignment.setDraft(true);
sourceAssignment.setTypeOfGrade(Assignment.GradeType.SCORE_GRADE_TYPE);
sourceAssignment.getProperties().put(AssignmentConstants.NEW_ASSIGNMENT_ADD_TO_GRADEBOOK,
AssignmentConstants.GRADEBOOK_INTEGRATION_ADD);
sourceAssignment.getProperties().put(AssignmentConstants.NEW_ASSIGNMENT_CATEGORY, "11");
updateAssignment(sourceAssignment);

stubContextPermissions(toContext);
stubCategoryImport(fromContext, toContext, 11L, 22L, "Essays");

getAssignmentServiceImpl().transferCopyEntities(fromContext, toContext, null, null);

Collection<Assignment> importedAssignments = assignmentService.getAssignmentsForContext(toContext);
assertEquals(1, importedAssignments.size());
Assignment importedAssignment = importedAssignments.iterator().next();
assertTrue(importedAssignment.getDraft());
assertEquals(AssignmentConstants.GRADEBOOK_INTEGRATION_ADD,
importedAssignment.getProperties().get(AssignmentConstants.NEW_ASSIGNMENT_ADD_TO_GRADEBOOK));
assertEquals("22", importedAssignment.getProperties().get(AssignmentConstants.NEW_ASSIGNMENT_CATEGORY));
}

private Assignment createPublishedAssignment(String context) {

stubContextPermissions(context);
Expand Down Expand Up @@ -250,6 +315,25 @@ private void stubContextPermissions(String context) {
when(securityService.unlock(AssignmentServiceConstants.SECURE_UPDATE_ASSIGNMENT, contextReference)).thenReturn(true);
}

private void stubCategoryImport(String fromContext, String toContext, Long sourceCategoryId, Long destinationCategoryId,
String categoryName) {

CategoryDefinition sourceCategory = new CategoryDefinition();
sourceCategory.setId(sourceCategoryId);
sourceCategory.setName(categoryName);

CategoryDefinition destinationCategory = new CategoryDefinition();
destinationCategory.setId(destinationCategoryId);
destinationCategory.setName(categoryName);

GradebookInformation destinationGradebookInformation = new GradebookInformation();
destinationGradebookInformation.setCategoryType(GradingConstants.CATEGORY_TYPE_ONLY_CATEGORY);

when(gradingService.getGradebookInformation(toContext, toContext)).thenReturn(destinationGradebookInformation);
when(gradingService.getCategoryDefinitions(fromContext, fromContext)).thenReturn(List.of(sourceCategory));
when(gradingService.getCategoryDefinitions(toContext, toContext)).thenReturn(List.of(destinationCategory));
}

private AssignmentServiceImpl getAssignmentServiceImpl() {

return (AssignmentServiceImpl) AopTestUtils.getTargetObject(assignmentService);
Expand Down
Loading