Skip to content

Commit

Permalink
SAK-42608 Samigo display user notifications for available assessments (
Browse files Browse the repository at this point in the history
…sakaiproject#11943)

Co-authored-by: Adrian Fish <[email protected]>
  • Loading branch information
jkjanetschek and adrianfish authored Oct 18, 2023
1 parent 8620d7a commit 14727b9
Show file tree
Hide file tree
Showing 8 changed files with 586 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public final class SamigoConstants {
public static final String EVENT_ASSESSMENT_PUBLISH = "sam.assessment.publish";
public static final String EVENT_ASSESSMENT_ATTACHMENT_NEW = "sam.assessment.attachment.new";
public static final String EVENT_ASSESSMENT_ATTACHMENT_DELETE = "sam.assessment.attachment.delete";
public static final String EVENT_ASSESSMENT_AVAILABLE = "sam.assessment.available";
public static final String EVENT_ASSESSMENT_UPDATE_AVAILABLE = "sam.assessment.update.available";
public static final String EVENT_ASSESSMENT_DELETE = "sam.assessment.delete";


//Published assessment events
public static final String EVENT_PUBLISHED_ASSESSMENT_REVISE = "sam.pubassessment.revise";
Expand All @@ -86,6 +90,8 @@ public final class SamigoConstants {
public static final String EVENT_PUBLISHED_ASSESSMENT_REPUBLISH = "sam.pubassessment.republish";
public static final String EVENT_PUBLISHED_ASSESSMENT_SETTING_EDIT = "sam.pubsetting.edit";
public static final String EVENT_PUBLISHED_ASSESSMENT_UNINDEXITEM = "sam.pubassessment.unindexitem";
public static final String EVENT_PUBLISHED_ASSESSMENT_RETRACTED = "sam.pubassessment.retracted";


//Question pool events
public static final String EVENT_QUESTIONPOOL_QUESTIONMOVED = "sam.questionpool.questionmoved";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.sakaiproject.component.cover.ServerConfigurationService;
import org.sakaiproject.samigo.api.SamigoAvailableNotificationService;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentBaseIfc;
import org.sakaiproject.tool.assessment.data.ifc.assessment.AssessmentIfc;
import org.sakaiproject.tool.assessment.facade.AgentFacade;
import org.sakaiproject.tool.assessment.facade.AssessmentFacade;
import org.sakaiproject.tool.assessment.facade.PublishedAssessmentFacade;
Expand All @@ -48,6 +49,12 @@
import org.sakaiproject.tool.assessment.ui.listener.util.ContextUtil;
import org.sakaiproject.tool.cover.ToolManager;

import static org.sakaiproject.samigo.util.SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_RETRACTED;
import static org.sakaiproject.samigo.util.SamigoConstants.EVENT_ASSESSMENT_AVAILABLE;
import org.sakaiproject.event.cover.EventTrackingService;

import java.util.Objects;

/**
* <p>Title: Samigo</p>2
* <p>Description: Sakai Assessment Manager</p>
Expand Down Expand Up @@ -78,6 +85,28 @@ public void processAction(ActionEvent ae) throws AbortProcessingException
author.setIsEditPoolFlow(false);
}

//cancel not fired event and fire event for userNotification alert deletion
AssessmentBean assessmentBean = (AssessmentBean) ContextUtil
.lookupBean("assessmentBean");
String assessmentId = assessmentBean.getAssessmentId();


//check if draft
if ((assessmentId != null && assessmentBean.getAssessment() == null)
|| (assessmentId != null && assessmentBean.getAssessment() != null
&& assessmentBean.getAssessment().getAssessmentBaseId() != null
&& !Long.toString(assessmentBean.getAssessment().getAssessmentBaseId()).equals(assessmentId))) {
PublishedAssessmentService publishedAssessmentService = new PublishedAssessmentService();
try {
PublishedAssessmentFacade assessment = publishedAssessmentService.getPublishedAssessment(assessmentId);
if (!Objects.equals(assessment.getStatus(), AssessmentIfc.RETRACT_FOR_EDIT_STATUS)) {
EventTrackingService.cancelDelays("siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), EVENT_ASSESSMENT_AVAILABLE);
EventTrackingService.post(EventTrackingService.newEvent(EVENT_PUBLISHED_ASSESSMENT_RETRACTED,"siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId() ,true));
}
} catch (Exception e) {
log.warn("Could not get published Assessment for id {}", assessmentId);
}
}

if (editType != null) {
if ("pendingAssessment".equals(editType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.component.cover.ServerConfigurationService;
import org.sakaiproject.email.cover.EmailService;
import org.sakaiproject.event.cover.EventTrackingService;
import org.sakaiproject.event.api.EventTrackingService;
import org.sakaiproject.exception.IdUnusedException;
import org.sakaiproject.grading.api.InvalidCategoryException;
import org.sakaiproject.rubrics.api.RubricsConstants;
Expand Down Expand Up @@ -98,6 +98,13 @@
import org.sakaiproject.util.ResourceLoader;
import org.springframework.web.client.HttpClientErrorException;


import org.sakaiproject.tool.assessment.data.dao.assessment.ExtendedTime;
import org.sakaiproject.time.api.Time;
import java.util.ListIterator;
import java.time.Instant;
import org.sakaiproject.tool.assessment.data.dao.assessment.ExtendedTime;

/**
* <p>Title: Samigo</p>2
* <p>Description: Sakai Assessment Manager</p>
Expand All @@ -121,11 +128,13 @@ public class PublishAssessmentListener
private RubricsService rubricsService;
private TaskService taskService;
private SamigoAvailableNotificationService samigoAvailableNotificationService;
private EventTrackingService eventTrackingService;

public PublishAssessmentListener() {
rubricsService = ComponentManager.get(RubricsService.class);
taskService = ComponentManager.get(TaskService.class);
samigoAvailableNotificationService = ComponentManager.get(SamigoAvailableNotificationService.class);
eventTrackingService = ComponentManager.get(EventTrackingService.class);
}

public void processAction(ActionEvent ae) throws AbortProcessingException {
Expand Down Expand Up @@ -271,13 +280,41 @@ private void publish(AssessmentFacade assessment,
ExtendedTimeFacade extendedTimeFacade = PersistenceService.getInstance().getExtendedTimeFacade();
extendedTimeFacade.copyEntriesToPub(pub.getData(), assessmentSettings.getExtendedTimes());

EventTrackingService.post(EventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_PUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + pub.getPublishedAssessmentId(), true));
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_PUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + pub.getPublishedAssessmentId(), true));

/*
* UserNotification: check if event should be fired immediately or/and must be delayed --> subsequent events are handled by TestsAndQuizzesUserNotificationHandler
*/
List<ExtendedTime> extendedTimes = assessmentSettings.getExtendedTimes();
Instant instant = pub.getStartDate().toInstant();
if (instant.isBefore(Instant.now())) {
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + pub.getPublishedAssessmentId(), true));
} else {
Instant earliestDelayInstant = instant;
if (assessmentSettings.getExtendedTimesSize() != 0) {
ListIterator<ExtendedTime> it = extendedTimes.listIterator();
boolean postEvent = false;
while (it.hasNext()) {
ExtendedTime exTime = (ExtendedTime) it.next();
Instant startInstant = exTime.getStartDate().toInstant();
if (startInstant.isBefore(Instant.now()) && !postEvent) {
postEvent = true;
} else if (startInstant.isBefore(instant)) {
earliestDelayInstant = startInstant;
}
}
if (postEvent) {
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + pub.getPublishedAssessmentId(), true));
}
}
eventTrackingService.delay(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + pub.getPublishedAssessmentId(), true), earliestDelayInstant);
}

for (Object sectionObj : pub.getSectionSet()){
PublishedSectionData sectionData = (PublishedSectionData) sectionObj;
for (Object itemObj : sectionData.getItemSet()){
PublishedItemData itemData = (PublishedItemData) itemObj;
EventTrackingService.post(EventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_SAVEITEM, "/sam/" + AgentFacade.getCurrentSiteId() + "/publish, publishedItemId=" + itemData.getItemIdString(), true));
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_SAVEITEM, "/sam/" + AgentFacade.getCurrentSiteId() + "/publish, publishedItemId=" + itemData.getItemIdString(), true));

try {
Optional<ToolItemRubricAssociation> rubricAssociation = rubricsService.getRubricAssociation(RubricsConstants.RBCS_TOOL_SAMIGO, assessmentSettings.getAssessmentId().toString() + "." + itemData.getOriginalItemId().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@

import lombok.extern.slf4j.Slf4j;
import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.event.api.EventTrackingService;
import org.sakaiproject.samigo.api.SamigoAvailableNotificationService;
import org.sakaiproject.samigo.api.SamigoReferenceReckoner;
import org.sakaiproject.samigo.util.SamigoConstants;
import org.apache.commons.lang3.StringUtils;
import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.event.cover.EventTrackingService;
import org.sakaiproject.site.api.Site;
import org.sakaiproject.site.cover.SiteService;
import org.sakaiproject.spring.SpringBeanLocator;
Expand Down Expand Up @@ -70,6 +70,12 @@
import org.sakaiproject.tool.cover.ToolManager;
import org.sakaiproject.util.ResourceLoader;

import org.sakaiproject.tool.assessment.data.dao.assessment.ExtendedTime;
import org.sakaiproject.time.api.Time;
import java.util.ListIterator;
import java.time.Instant;
import org.sakaiproject.component.cover.ComponentManager;

@Slf4j
public class RepublishAssessmentListener implements ActionListener {

Expand All @@ -82,7 +88,10 @@ public class RepublishAssessmentListener implements ActionListener {
private TaskService taskService = ComponentManager.get(TaskService.class);;
private static final ResourceLoader rl = new ResourceLoader("org.sakaiproject.tool.assessment.bundle.AssessmentSettingsMessages");
private final SamigoAvailableNotificationService samigoAvailableNotificationService = ComponentManager.get(SamigoAvailableNotificationService.class);

private EventTrackingService eventTrackingService;
public RepublishAssessmentListener() {
eventTrackingService = ComponentManager.get(EventTrackingService.class);
}
public void processAction(ActionEvent ae) throws AbortProcessingException {
AssessmentBean assessmentBean = (AssessmentBean) ContextUtil
.lookupBean("assessmentBean");
Expand All @@ -94,7 +103,7 @@ public void processAction(ActionEvent ae) throws AbortProcessingException {

// Go to database to get the newly updated data. The data inside beans might not be up to date.
PublishedAssessmentFacade assessment = publishedAssessmentService.getPublishedAssessment(publishedAssessmentId);
EventTrackingService.post(EventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_REPUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", publishedAssessmentId=" + publishedAssessmentId, true));
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_REPUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", publishedAssessmentId=" + publishedAssessmentId, true));

assessment.setStatus(AssessmentBaseIfc.ACTIVE_STATUS);
publishedAssessmentService.saveAssessment(assessment);
Expand All @@ -105,15 +114,16 @@ public void processAction(ActionEvent ae) throws AbortProcessingException {
if (author.getIsRepublishAndRegrade() && hasGradingData) {
regradeRepublishedAssessment(publishedAssessmentService, assessment);
}

EventTrackingService.post(EventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_REPUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", publishedAssessmentId=" + publishedAssessmentId, true));
PublishedAssessmentSettingsBean publishedAssessmentSettings = (PublishedAssessmentSettingsBean) ContextUtil.lookupBean("publishedSettings");
postUserNotification(assessment, publishedAssessmentSettings);
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_PUBLISHED_ASSESSMENT_REPUBLISH, "siteId=" + AgentFacade.getCurrentSiteId() + ", publishedAssessmentId=" + publishedAssessmentId, true));
assessment.setStatus(AssessmentBaseIfc.ACTIVE_STATUS);
publishedAssessmentService.saveAssessment(assessment);
updateGB(assessment);

PublishRepublishNotificationBean publishRepublishNotification = (PublishRepublishNotificationBean) ContextUtil.lookupBean("publishRepublishNotification");

PublishedAssessmentSettingsBean publishedAssessmentSettings = (PublishedAssessmentSettingsBean) ContextUtil.lookupBean("publishedSettings");

PublishAssessmentListener publishAssessmentListener = new PublishAssessmentListener();
String subject = publishRepublishNotification.getNotificationSubject();
String notificationMessage = publishAssessmentListener.getNotificationMessage(publishRepublishNotification, publishedAssessmentSettings.getTitle(), publishedAssessmentSettings.getReleaseTo(),
Expand Down Expand Up @@ -173,6 +183,39 @@ public void processAction(ActionEvent ae) throws AbortProcessingException {
samigoAvailableNotificationService.scheduleAssessmentAvailableNotification(publishedAssessmentId);
author.setOutcome("author");
}

private void postUserNotification(PublishedAssessmentFacade assessment, PublishedAssessmentSettingsBean publishedAssessmentSettings) {

List<ExtendedTime> extendedTimes = publishedAssessmentSettings.getExtendedTimes();
Instant instant = assessment.getStartDate().toInstant();
if (instant.isBefore(Instant.now())) {
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_UPDATE_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), true));
if (publishedAssessmentSettings.getExtendedTimesSize() != 0) {
ListIterator<ExtendedTime> it = extendedTimes.listIterator();
while (it.hasNext()) {
ExtendedTime exTime = (ExtendedTime) it.next();
Instant startInstant = exTime.getStartDate().toInstant();
if (startInstant.isAfter(Instant.now())) {
eventTrackingService.delay(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), true), startInstant);
}
}
}
} else {
eventTrackingService.delay(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), true), instant);
if (publishedAssessmentSettings.getExtendedTimesSize() != 0) {
ListIterator<ExtendedTime> it = extendedTimes.listIterator();
while (it.hasNext()) {
ExtendedTime exTime = (ExtendedTime) it.next();
Instant startInstant = exTime.getStartDate().toInstant();
if (startInstant.isBefore(Instant.now())) {
eventTrackingService.post(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_UPDATE_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), true));
} else if (startInstant.isAfter(Instant.now()) && !instant.equals(startInstant)) {
eventTrackingService.delay(eventTrackingService.newEvent(SamigoConstants.EVENT_ASSESSMENT_AVAILABLE, "siteId=" + AgentFacade.getCurrentSiteId() + ", assessmentId=" + assessment.getAssessmentId() + ", publishedAssessmentId=" + assessment.getPublishedAssessmentId(), true), startInstant);
}
}
}
}
}

private void regradeRepublishedAssessment (PublishedAssessmentService pubService, PublishedAssessmentFacade publishedAssessment) {
Map publishedItemHash = pubService.preparePublishedItemHash(publishedAssessment);
Expand Down
Loading

0 comments on commit 14727b9

Please sign in to comment.