Skip to content

Commit

Permalink
SAK-43238 Embedded Announcement block in Lessons display order (sakai…
Browse files Browse the repository at this point in the history
  • Loading branch information
austin48 authored Dec 3, 2020
1 parent 3c26a20 commit d9eb47c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ widget_date=Date:
#widget_time=Time:

# EntityProvider properties
announcement=Represents announcements for a given site (including merged announcements) or all announcements for a user. Also includes Message Of The Day and public announcements. There are two optional URL parameters available: 'n' to set the total number of announcements returned, and 'd' to set the number of days in the past to retrieve announcements for. If these parameters are not set, they are retrieved from the tool configuration, or the standard defaults of n=3 and d=10 are used. For example /announcements/site/mercury.json?n=20&d=100 will retrieve up to 20 total announcements for up to 100 days ago.
announcement=Represents announcements for a given site (including merged announcements) or all announcements for a user. Also includes Message Of The Day and public announcements. There are three optional URL parameters available: 'n' to set the total number of announcements returned, 'd' to set the number of days in the past to retrieve announcements for, and 'a' to set an ascending sort order of true or false. If the system is configured with the (default) 'Announcement Reorder' property, announcements will be additionally sorted by the message_order. If these parameters are not set, they are retrieved from the tool configuration, or the standard defaults of n=3, d=10, a=0 are used. For example /announcements/site/mercury.json?n=20&d=100&a=1 will retrieve up to 20 total announcements for up to 100 days ago with an ascending order.
announcement.action.site=Retrieve the announcements for a site. If you are not logged in, you will only receive the public announcements for this site.
announcement.action.user=Retrieve the announcements for the current user. If you are not logged in, you will only receive the MOTD announcements.
announcement.action.user=Retrieve the announcements for the current user. If you are not logged in, you will only receive the MOTD announcements. Announcements will be sorted by date and ignore the system's 'Announcement Reorder' configuration.
announcement.action.motd=Retrieve only the MOTD announcements.
announcement.action.msg=Retrieve an announcement based on the path used by the announcements service internally, ie /announcement/msg/{siteId}/{channelId}/{announcementId}. Context is normally the site ID and the channelId is normally "main" unless there are multiple channels in a site. For example /announcement/msg/21b1984d-af58-43da-8583-f4adee769aa2/main/5641323b-761a-4a4d-8761-688f4928141b
announcement.action.message=Retrieve an announcement based on the given siteId and messageId. eg /announcement/message/{siteId}/{msgId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
Expand All @@ -42,7 +43,11 @@
import org.sakaiproject.announcement.api.AnnouncementMessage;
import org.sakaiproject.announcement.api.AnnouncementMessageHeader;
import org.sakaiproject.announcement.api.AnnouncementService;
import org.sakaiproject.announcement.tool.AnnouncementAction;
import org.sakaiproject.announcement.tool.AnnouncementWrapper;
import org.sakaiproject.announcement.tool.AnnouncementWrapperComparator;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.component.cover.ComponentManager;
import org.sakaiproject.entity.api.EntityManager;
import org.sakaiproject.entity.api.EntityPermissionException;
Expand Down Expand Up @@ -98,7 +103,10 @@ public class AnnouncementEntityProviderImpl extends AbstractEntityProvider imple
public static int DEFAULT_DAYS_IN_PAST = 10;
private static final long MILLISECONDS_IN_DAY = (24 * 60 * 60 * 1000);
private static ResourceLoader rb = new ResourceLoader("announcement");


@Setter
private ServerConfigurationService serverConfigurationService;

/**
* Prefix for this provider
*/
Expand Down Expand Up @@ -130,6 +138,7 @@ private List<?> getAnnouncements(String siteId, Map<String,Object> params, boole
//we use this zero value to determine if we need to look up from the tool config, or use the defaults if still not set.
int numberOfAnnouncements = NumberUtils.toInt((String)params.get("n"), 0);
int numberOfDaysInThePast = NumberUtils.toInt((String)params.get("d"), 0);
boolean announcementSortAsc = NumberUtils.toInt((String)params.get("a"), 0) == 1 ? true:false;

//get currentUserId for permissions checks, although unused for motdView and onlyPublic
String currentUserId = sessionManager.getCurrentSessionUserId();
Expand Down Expand Up @@ -213,36 +222,57 @@ private List<?> getAnnouncements(String siteId, Map<String,Object> params, boole

log.debug("numberOfAnnouncements: {}", numberOfAnnouncements);
log.debug("numberOfDaysInThePast: {}", numberOfDaysInThePast);
log.debug("announcementSortAsc: {}", announcementSortAsc);

//get the Sakai Time for the given java Date
Time t = timeService.newTime(getTimeForDaysInPast(numberOfDaysInThePast).getTime());

//get the announcements for each channel
List<Message> announcements = new ArrayList<Message>();

boolean enableReorder = serverConfigurationService.getBoolean(AnnouncementAction.SAK_PROP_ANNC_REORDER, AnnouncementAction.SAK_PROP_ANNC_REORDER_DEFAULT);
final String sortCurrentOrder = enableReorder ? AnnouncementAction.SORT_MESSAGE_ORDER : AnnouncementAction.SORT_DATE;
ViewableFilter msgFilter = AnnouncementAction.SORT_MESSAGE_ORDER.equals(sortCurrentOrder) ? null : new ViewableFilter(null, t, numberOfAnnouncements);

//for each channel
for(String channel: channels) {
for (String channel : channels) {
try {
announcements.addAll(announcementService.getMessages(channel, new ViewableFilter(null, t, numberOfAnnouncements), true, false));
announcements.addAll(announcementService.getMessages(channel, msgFilter, announcementSortAsc, false));
} catch (PermissionException | IdUnusedException | NullPointerException ex) {
//user may not have access to view the channel but get all public messages in this channel
AnnouncementChannel announcementChannel = (AnnouncementChannel)announcementService.getChannelPublic(channel);
if(announcementChannel != null){
AnnouncementChannel announcementChannel = (AnnouncementChannel) announcementService.getChannelPublic(channel);
if (announcementChannel != null) {
List<Message> publicMessages = announcementChannel.getMessagesPublic(null, true);
for(Message message : publicMessages){
for (Message message : publicMessages) {
//Add message only if it is within the time range
if(isMessageWithinPastNDays(message, numberOfDaysInThePast) && announcementService.isMessageViewable((AnnouncementMessage) message)){
if (isMessageWithinPastNDays(message, numberOfDaysInThePast) && announcementService.isMessageViewable((AnnouncementMessage) message)) {
announcements.add(message);
}
}
}
}
}

if(log.isDebugEnabled()) {
log.debug("announcements.size(): {}", announcements.size());

if (AnnouncementAction.SORT_MESSAGE_ORDER.equals(sortCurrentOrder)) {
try {
List<AnnouncementWrapper> messageList = new ArrayList<>();
final AnnouncementChannel defaultChannel = (AnnouncementChannel) announcementService.getChannel("/announcement/channel/" + siteId + "/main");
for (Message msg : announcements) {
AnnouncementChannel curChannel = (AnnouncementChannel) announcementService.getChannel(msg.getReference().replace("msg", "channel").replaceAll("main/(.*)", "main"));
messageList.add(new AnnouncementWrapper((AnnouncementMessage) msg, curChannel, defaultChannel, null, null));
}
Comparator<AnnouncementWrapper> sortedAnnouncements = new AnnouncementWrapperComparator(sortCurrentOrder, announcementSortAsc);
messageList.sort(sortedAnnouncements);
announcements.clear();
announcements.addAll(messageList);
} catch (Exception e) {
log.warn("Error sorting announcements by {}, {}", AnnouncementAction.SORT_MESSAGE_ORDER, e.toString());
}
}


log.debug("announcements.size(): {}", announcements.size());


//convert raw announcements into decorated announcements
List<DecoratedAnnouncement> decoratedAnnouncements = new ArrayList<DecoratedAnnouncement>();

Expand All @@ -257,11 +287,15 @@ private List<?> getAnnouncements(String siteId, Map<String,Object> params, boole
}
}

//sort
Collections.sort(decoratedAnnouncements);

//reverse so it is date descending. This could be dependent on a parameter that specifies the sort order
Collections.reverse(decoratedAnnouncements);
if (!AnnouncementAction.SORT_MESSAGE_ORDER.equals(sortCurrentOrder) || channels.size() > 1) {
//sort
Collections.sort(decoratedAnnouncements);

if (!announcementSortAsc) {
//reverse so it is date descending. This could be dependent on a parameter that specifies the sort order
Collections.reverse(decoratedAnnouncements);
}
}

//trim to final number, within bounds of list size.
if(numberOfAnnouncements > decoratedAnnouncements.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Comparator for announcements.
*/
@Slf4j
class AnnouncementWrapperComparator implements Comparator<AnnouncementWrapper> {
public class AnnouncementWrapperComparator implements Comparator<AnnouncementWrapper> {

private static RuleBasedCollator collator_ini = (RuleBasedCollator)Collator.getInstance();
private Collator collator = Collator.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<property name="timeService"><ref bean="org.sakaiproject.time.api.TimeService"/></property>
<property name="toolManager"><ref bean="org.sakaiproject.tool.api.ToolManager"/></property>
<property name="entityManager"><ref bean="org.sakaiproject.entity.api.EntityManager"/></property>

<property name="serverConfigurationService" ref="org.sakaiproject.component.api.ServerConfigurationService"/>
</bean>

</beans>
Expand Down

0 comments on commit d9eb47c

Please sign in to comment.