Search in sources :

Example 16 with SubscriptionInfo

use of org.olat.core.commons.services.notifications.SubscriptionInfo in project openolat by klemens.

the class NotificationsManagerTest method testGetSubscriptionInfos.

// markPublisherNews
@Test
public void testGetSubscriptionInfos() {
    Identity id = JunitTestHelper.createAndPersistIdentityAsUser("fi1-" + UUID.randomUUID().toString());
    SubscriptionContext context = new SubscriptionContext("Course", new Long(789521), UUID.randomUUID().toString());
    PublisherData publisherData = new PublisherData("Forum", "e.g. forumdata=keyofforum", null);
    Publisher publisher = notificationManager.getOrCreatePublisher(context, publisherData);
    dbInstance.commitAndCloseSession();
    notificationManager.subscribe(id, context, publisherData);
    dbInstance.commitAndCloseSession();
    // get infos
    List<SubscriptionInfo> infos = notificationManager.getSubscriptionInfos(id, publisher.getType());
    Assert.assertNotNull(infos);
}
Also used : SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Publisher(org.olat.core.commons.services.notifications.Publisher) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) Test(org.junit.Test)

Example 17 with SubscriptionInfo

use of org.olat.core.commons.services.notifications.SubscriptionInfo in project openolat by klemens.

the class AssessmentNotificationsHandler method createSubscriptionInfo.

/**
 * @see org.olat.core.commons.services.notifications.NotificationsHandler#createSubscriptionInfo(org.olat.core.commons.services.notifications.Subscriber,
 *      java.util.Locale, java.util.Date)
 */
public SubscriptionInfo createSubscriptionInfo(final Subscriber subscriber, Locale locale, Date compareDate) {
    SubscriptionInfo si = null;
    Publisher p = subscriber.getPublisher();
    if (!NotificationsUpgradeHelper.checkCourse(p)) {
        // course don't exist anymore
        notificationsManager.deactivate(p);
        return notificationsManager.getNoSubscriptionInfo();
    }
    try {
        Date latestNews = p.getLatestNewsDate();
        Identity identity = subscriber.getIdentity();
        // can't be loaded when already deleted
        if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
            Long courseId = new Long(p.getData());
            final ICourse course = loadCourseFromId(courseId);
            if (courseStatus(course)) {
                // course admins or users with the course right to have full access to
                // the assessment tool will have full access to user tests
                CourseGroupManager cgm = course.getCourseEnvironment().getCourseGroupManager();
                final boolean hasFullAccess = (cgm.isIdentityCourseAdministrator(identity) ? true : cgm.hasRight(identity, CourseRights.RIGHT_ASSESSMENT));
                final Set<Identity> coachedUsers = new HashSet<Identity>();
                if (!hasFullAccess) {
                    // initialize list of users, only when user has not full access
                    List<BusinessGroup> coachedGroups = cgm.getOwnedBusinessGroups(identity);
                    List<Identity> coachedIdentites = businessGroupService.getMembers(coachedGroups, GroupRoles.participant.name());
                    coachedUsers.addAll(coachedIdentites);
                }
                List<AssessableCourseNode> testNodes = getCourseTestNodes(course);
                Translator translator = Util.createPackageTranslator(AssessmentManager.class, locale);
                for (AssessableCourseNode test : testNodes) {
                    List<AssessmentEntry> assessments = courseNodeAssessmentDao.loadAssessmentEntryBySubIdent(cgm.getCourseEntry(), test.getIdent());
                    for (AssessmentEntry assessment : assessments) {
                        Date modDate = assessment.getLastModified();
                        Identity assessedIdentity = assessment.getIdentity();
                        if (modDate.after(compareDate) && (hasFullAccess || coachedUsers.contains(assessedIdentity))) {
                            BigDecimal score = assessment.getScore();
                            if (test instanceof ScormCourseNode) {
                                ScormCourseNode scormTest = (ScormCourseNode) test;
                                // check if completed or passed
                                String status = ScormAssessmentManager.getInstance().getLastLessonStatus(assessedIdentity.getName(), course.getCourseEnvironment(), scormTest);
                                if (!"passed".equals(status) && !"completed".equals(status)) {
                                    continue;
                                }
                            }
                            String desc;
                            String type = translator.translate("notifications.entry." + test.getType());
                            if (score == null) {
                                desc = translator.translate("notifications.entry.attempt", new String[] { test.getShortTitle(), NotificationHelper.getFormatedName(assessedIdentity), type });
                            } else {
                                String scoreStr = AssessmentHelper.getRoundedScore(score);
                                desc = translator.translate("notifications.entry", new String[] { test.getShortTitle(), NotificationHelper.getFormatedName(assessedIdentity), scoreStr, type });
                            }
                            String urlToSend = null;
                            String businessPath = null;
                            if (p.getBusinessPath() != null) {
                                businessPath = p.getBusinessPath() + "[Users:0][Node:" + test.getIdent() + "][Identity:" + assessedIdentity.getKey() + "]";
                                urlToSend = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
                            }
                            SubscriptionListItem subListItem = new SubscriptionListItem(desc, urlToSend, businessPath, modDate, CSS_CLASS_USER_ICON);
                            if (si == null) {
                                String title = translator.translate("notifications.header", new String[] { course.getCourseTitle() });
                                String css = CourseNodeFactory.getInstance().getCourseNodeConfigurationEvenForDisabledBB(test.getType()).getIconCSSClass();
                                si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, css), null);
                            }
                            si.addSubscriptionListItem(subListItem);
                        }
                    }
                }
            }
        }
        if (si == null) {
            si = notificationsManager.getNoSubscriptionInfo();
        }
        return si;
    } catch (Exception e) {
        log.error("Error while creating assessment notifications", e);
        checkPublisher(p);
        return notificationsManager.getNoSubscriptionInfo();
    }
}
Also used : CourseGroupManager(org.olat.course.groupsandrights.CourseGroupManager) BusinessGroup(org.olat.group.BusinessGroup) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) ICourse(org.olat.course.ICourse) Publisher(org.olat.core.commons.services.notifications.Publisher) TitleItem(org.olat.core.commons.services.notifications.model.TitleItem) AssessmentEntry(org.olat.modules.assessment.AssessmentEntry) ScormCourseNode(org.olat.course.nodes.ScormCourseNode) Date(java.util.Date) BigDecimal(java.math.BigDecimal) AssertException(org.olat.core.logging.AssertException) AssessableCourseNode(org.olat.course.nodes.AssessableCourseNode) SubscriptionListItem(org.olat.core.commons.services.notifications.model.SubscriptionListItem) Translator(org.olat.core.gui.translator.Translator) Identity(org.olat.core.id.Identity) HashSet(java.util.HashSet)

Example 18 with SubscriptionInfo

use of org.olat.core.commons.services.notifications.SubscriptionInfo in project openolat by klemens.

the class DocumentPoolNotificationsHandler method createSubscriptionInfo.

@Override
public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
    Publisher p = subscriber.getPublisher();
    Date latestNews = p.getLatestNewsDate();
    try {
        SubscriptionInfo si;
        String taxonomyKey = documentPoolModule.getTaxonomyTreeKey();
        if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews) && StringHelper.isLong(taxonomyKey)) {
            Taxonomy taxonomy = taxonomyService.getTaxonomy(new TaxonomyRefImpl(new Long(taxonomyKey)));
            if (taxonomy == null) {
                return notificationsManager.getNoSubscriptionInfo();
            }
            Identity identity = subscriber.getIdentity();
            Roles roles = securityManager.getRoles(identity);
            boolean isTaxonomyAdmin = roles.isOLATAdmin();
            Translator translator = Util.createPackageTranslator(DocumentPoolMainController.class, locale);
            String templates = translator.translate("document.pool.templates");
            TaxonomyTreeBuilder builder = new TaxonomyTreeBuilder(taxonomy, identity, null, isTaxonomyAdmin, documentPoolModule.isTemplatesDirectoryEnabled(), templates, locale);
            TreeModel model = builder.buildTreeModel();
            si = new SubscriptionInfo(subscriber.getKey(), p.getType(), getTitleItemForPublisher(), null);
            new TreeVisitor(node -> {
                TaxonomyTreeNode tNode = (TaxonomyTreeNode) node;
                if (tNode.getTaxonomyLevel() != null && tNode.isDocumentsLibraryEnabled() && tNode.isCanRead()) {
                    VFSContainer container = taxonomyService.getDocumentsLibrary(tNode.getTaxonomyLevel());
                    String prefixBusinessPath = "[DocumentPool:" + taxonomy.getKey() + "][TaxonomyLevel:" + tNode.getTaxonomyLevel().getKey() + "][path=";
                    createSubscriptionInfo(container, prefixBusinessPath, compareDate, si, p, translator);
                } else if (tNode.getType() == TaxonomyTreeNodeType.templates) {
                    VFSContainer container = taxonomyService.getDocumentsLibrary(taxonomy);
                    String prefixBusinessPath = "[DocumentPool:" + taxonomy.getKey() + "][Templates:0s][path=";
                    createSubscriptionInfo(container, prefixBusinessPath, compareDate, si, p, translator);
                }
            }, model.getRootNode(), false).visitAll();
        } else {
            si = NotificationsManager.getInstance().getNoSubscriptionInfo();
        }
        return si;
    } catch (Exception e) {
        log.error("Cannot create document pool notifications for subscriber: " + subscriber.getKey(), e);
        return notificationsManager.getNoSubscriptionInfo();
    }
}
Also used : TitleItem(org.olat.core.commons.services.notifications.model.TitleItem) Util(org.olat.core.util.Util) TreeVisitor(org.olat.core.util.tree.TreeVisitor) SubscriptionListItem(org.olat.core.commons.services.notifications.model.SubscriptionListItem) NotificationHelper(org.olat.core.commons.services.notifications.NotificationHelper) OlatRelPathImpl(org.olat.core.util.vfs.OlatRelPathImpl) Date(java.util.Date) FileInfo(org.olat.core.commons.modules.bc.FileInfo) DocumentPoolModule(org.olat.modules.docpool.DocumentPoolModule) Autowired(org.springframework.beans.factory.annotation.Autowired) TaxonomyTreeNodeType(org.olat.modules.taxonomy.model.TaxonomyTreeNodeType) NotificationsHandler(org.olat.core.commons.services.notifications.NotificationsHandler) Locale(java.util.Locale) TaxonomyService(org.olat.modules.taxonomy.TaxonomyService) Service(org.springframework.stereotype.Service) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) FileUtils(org.olat.core.util.FileUtils) FolderManager(org.olat.core.commons.modules.bc.FolderManager) StringHelper(org.olat.core.util.StringHelper) OLog(org.olat.core.logging.OLog) Taxonomy(org.olat.modules.taxonomy.Taxonomy) Translator(org.olat.core.gui.translator.Translator) TaxonomyRefImpl(org.olat.modules.taxonomy.model.TaxonomyRefImpl) TaxonomyTreeBuilder(org.olat.modules.taxonomy.manager.TaxonomyTreeBuilder) TreeModel(org.olat.core.gui.components.tree.TreeModel) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) Publisher(org.olat.core.commons.services.notifications.Publisher) VFSContainer(org.olat.core.util.vfs.VFSContainer) BusinessControlFactory(org.olat.core.id.context.BusinessControlFactory) DocumentPoolMainController(org.olat.modules.docpool.ui.DocumentPoolMainController) List(java.util.List) Subscriber(org.olat.core.commons.services.notifications.Subscriber) Identity(org.olat.core.id.Identity) PublisherData(org.olat.core.commons.services.notifications.PublisherData) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) BaseSecurity(org.olat.basesecurity.BaseSecurity) Tracing(org.olat.core.logging.Tracing) Roles(org.olat.core.id.Roles) TaxonomyTreeNode(org.olat.modules.taxonomy.model.TaxonomyTreeNode) TaxonomyRefImpl(org.olat.modules.taxonomy.model.TaxonomyRefImpl) Taxonomy(org.olat.modules.taxonomy.Taxonomy) VFSContainer(org.olat.core.util.vfs.VFSContainer) TaxonomyTreeNode(org.olat.modules.taxonomy.model.TaxonomyTreeNode) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) Roles(org.olat.core.id.Roles) Publisher(org.olat.core.commons.services.notifications.Publisher) Date(java.util.Date) TreeVisitor(org.olat.core.util.tree.TreeVisitor) TreeModel(org.olat.core.gui.components.tree.TreeModel) Translator(org.olat.core.gui.translator.Translator) Identity(org.olat.core.id.Identity) TaxonomyTreeBuilder(org.olat.modules.taxonomy.manager.TaxonomyTreeBuilder)

Example 19 with SubscriptionInfo

use of org.olat.core.commons.services.notifications.SubscriptionInfo in project openolat by klemens.

the class AbstractTaskNotificationHandler method createSubscriptionInfo.

/**
 * @see org.olat.core.commons.services.notifications.NotificationsHandler#createSubscriptionInfo(org.olat.core.commons.services.notifications.Subscriber,
 *      java.util.Locale, java.util.Date)
 */
public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
    Publisher p = subscriber.getPublisher();
    Date latestNews = p.getLatestNewsDate();
    SubscriptionInfo si;
    // there could be news for me, investigate deeper
    try {
        if (NotificationsManager.getInstance().isPublisherValid(p) && compareDate.before(latestNews)) {
            String folderRoot = p.getData();
            if (isLogDebugEnabled()) {
                logDebug("folderRoot=", folderRoot);
            }
            final List<FileInfo> fInfos = FolderManager.getFileInfos(folderRoot, compareDate);
            final Translator translator = Util.createPackageTranslator(AbstractTaskNotificationHandler.class, locale);
            RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", p.getResId()), false);
            if (re == null) {
                if (!checkPublisher(p)) {
                    return NotificationsManager.getInstance().getNoSubscriptionInfo();
                }
            } else if (re.getRepositoryEntryStatus().isClosed()) {
                return NotificationsManager.getInstance().getNoSubscriptionInfo();
            }
            String displayName = re.getDisplayname();
            si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(translator.translate(getNotificationHeaderKey(), new String[] { displayName }), getCssClassIcon()), null);
            SubscriptionListItem subListItem;
            for (Iterator<FileInfo> it_infos = fInfos.iterator(); it_infos.hasNext(); ) {
                FileInfo fi = it_infos.next();
                MetaInfo metaInfo = fi.getMetaInfo();
                String filePath = fi.getRelPath();
                if (isLogDebugEnabled())
                    logDebug("filePath=", filePath);
                String fullUserName = getUserNameFromFilePath(metaInfo, filePath);
                Date modDate = fi.getLastModified();
                String desc = translator.translate(getNotificationEntryKey(), new String[] { filePath, fullUserName });
                String businessPath = p.getBusinessPath();
                String urlToSend = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
                String iconCssClass = null;
                if (metaInfo != null) {
                    iconCssClass = metaInfo.getIconCssClass();
                }
                subListItem = new SubscriptionListItem(desc, urlToSend, businessPath, modDate, iconCssClass);
                si.addSubscriptionListItem(subListItem);
            }
        } else {
            si = NotificationsManager.getInstance().getNoSubscriptionInfo();
        }
    } catch (Exception e) {
        getLogger().error("Cannot create task notifications for subscriber: " + subscriber.getKey(), e);
        checkPublisher(p);
        si = NotificationsManager.getInstance().getNoSubscriptionInfo();
    }
    return si;
}
Also used : MetaInfo(org.olat.core.commons.modules.bc.meta.MetaInfo) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) Publisher(org.olat.core.commons.services.notifications.Publisher) RepositoryEntry(org.olat.repository.RepositoryEntry) TitleItem(org.olat.core.commons.services.notifications.model.TitleItem) Date(java.util.Date) SubscriptionListItem(org.olat.core.commons.services.notifications.model.SubscriptionListItem) FileInfo(org.olat.core.commons.modules.bc.FileInfo) Translator(org.olat.core.gui.translator.Translator)

Example 20 with SubscriptionInfo

use of org.olat.core.commons.services.notifications.SubscriptionInfo in project openolat by klemens.

the class CalendarNotificationHandler method createSubscriptionInfo.

@Override
public SubscriptionInfo createSubscriptionInfo(Subscriber subscriber, Locale locale, Date compareDate) {
    SubscriptionInfo si = null;
    Publisher p = subscriber.getPublisher();
    Date latestNews = p.getLatestNewsDate();
    // can't be loaded when already deleted
    if (notificationsManager.isPublisherValid(p) && compareDate.before(latestNews)) {
        Long id = p.getResId();
        String type = p.getSubidentifier();
        try {
            Translator translator = Util.createPackageTranslator(CalendarModule.class, locale);
            String calType = null;
            String title = null;
            if (type.equals(CalendarController.ACTION_CALENDAR_COURSE)) {
                RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(OresHelper.createOLATResourceableInstance("CourseModule", id), false);
                if (re.getRepositoryEntryStatus().isClosed() || re.getRepositoryEntryStatus().isUnpublished()) {
                    return NotificationsManager.getInstance().getNoSubscriptionInfo();
                }
                String displayName = re.getDisplayname();
                calType = CalendarManager.TYPE_COURSE;
                title = translator.translate("cal.notifications.header.course", new String[] { displayName });
            } else if (type.equals(CalendarController.ACTION_CALENDAR_GROUP)) {
                BusinessGroup group = businessGroupDao.load(id);
                calType = CalendarManager.TYPE_GROUP;
                if (group == null) {
                    return notificationsManager.getNoSubscriptionInfo();
                }
                title = translator.translate("cal.notifications.header.group", new String[] { group.getName() });
            }
            if (calType != null) {
                Formatter form = Formatter.getInstance(locale);
                si = new SubscriptionInfo(subscriber.getKey(), p.getType(), new TitleItem(title, CSS_CLASS_CALENDAR_ICON), null);
                String bPath;
                if (StringHelper.containsNonWhitespace(p.getBusinessPath())) {
                    bPath = p.getBusinessPath();
                } else if ("CalendarManager.course".equals(p.getResName())) {
                    try {
                        OLATResourceable ores = OresHelper.createOLATResourceableInstance(CourseModule.getCourseTypeName(), p.getResId());
                        RepositoryEntry re = repositoryManager.lookupRepositoryEntry(ores, true);
                        // Fallback
                        bPath = "[RepositoryEntry:" + re.getKey() + "]";
                    } catch (Exception e) {
                        log.error("Error processing calendar notifications of publisher:" + p.getKey(), e);
                        return notificationsManager.getNoSubscriptionInfo();
                    }
                } else {
                    // cannot make link without business path
                    return notificationsManager.getNoSubscriptionInfo();
                }
                Kalendar cal = calendarManager.getCalendar(calType, id.toString());
                Collection<KalendarEvent> calEvents = cal.getEvents();
                for (KalendarEvent kalendarEvent : calEvents) {
                    if (showEvent(compareDate, kalendarEvent)) {
                        log.debug("found a KalendarEvent: " + kalendarEvent.getSubject() + " with time: " + kalendarEvent.getBegin() + " modified before: " + compareDate.toString(), null);
                        // found a modified event in this calendar
                        Date modDate = null;
                        if (kalendarEvent.getLastModified() > 0) {
                            modDate = new Date(kalendarEvent.getLastModified());
                        } else if (kalendarEvent.getCreated() > 0) {
                            modDate = new Date(kalendarEvent.getCreated());
                        } else if (kalendarEvent.getBegin() != null) {
                            modDate = kalendarEvent.getBegin();
                        }
                        String subject = kalendarEvent.getSubject();
                        String author = kalendarEvent.getCreatedBy();
                        if (author == null)
                            author = "";
                        String location = "";
                        if (StringHelper.containsNonWhitespace(kalendarEvent.getLocation())) {
                            location = kalendarEvent.getLocation() == null ? "" : translator.translate("cal.notifications.location", new String[] { kalendarEvent.getLocation() });
                        }
                        String dateStr;
                        if (kalendarEvent.isAllDayEvent()) {
                            dateStr = form.formatDate(kalendarEvent.getBegin());
                        } else {
                            dateStr = form.formatDate(kalendarEvent.getBegin()) + " - " + form.formatDate(kalendarEvent.getEnd());
                        }
                        String desc = translator.translate("cal.notifications.entry", new String[] { subject, dateStr, location, author });
                        String businessPath = bPath + "[path=" + kalendarEvent.getID() + ":0]";
                        String urlToSend = BusinessControlFactory.getInstance().getURLFromBusinessPathString(businessPath);
                        SubscriptionListItem subListItem = new SubscriptionListItem(desc, urlToSend, businessPath, modDate, CSS_CLASS_CALENDAR_ICON);
                        si.addSubscriptionListItem(subListItem);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Unexpected exception", e);
            checkPublisher(p);
            si = notificationsManager.getNoSubscriptionInfo();
        }
    } else {
        si = notificationsManager.getNoSubscriptionInfo();
    }
    return si;
}
Also used : BusinessGroup(org.olat.group.BusinessGroup) OLATResourceable(org.olat.core.id.OLATResourceable) Formatter(org.olat.core.util.Formatter) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) SubscriptionInfo(org.olat.core.commons.services.notifications.SubscriptionInfo) Publisher(org.olat.core.commons.services.notifications.Publisher) RepositoryEntry(org.olat.repository.RepositoryEntry) TitleItem(org.olat.core.commons.services.notifications.model.TitleItem) Date(java.util.Date) SubscriptionListItem(org.olat.core.commons.services.notifications.model.SubscriptionListItem) Kalendar(org.olat.commons.calendar.model.Kalendar) Translator(org.olat.core.gui.translator.Translator)

Aggregations

SubscriptionInfo (org.olat.core.commons.services.notifications.SubscriptionInfo)56 Publisher (org.olat.core.commons.services.notifications.Publisher)44 SubscriptionListItem (org.olat.core.commons.services.notifications.model.SubscriptionListItem)38 Date (java.util.Date)36 Translator (org.olat.core.gui.translator.Translator)30 TitleItem (org.olat.core.commons.services.notifications.model.TitleItem)28 Identity (org.olat.core.id.Identity)24 RepositoryEntry (org.olat.repository.RepositoryEntry)18 ArrayList (java.util.ArrayList)10 Subscriber (org.olat.core.commons.services.notifications.Subscriber)10 OLATResourceable (org.olat.core.id.OLATResourceable)10 ICourse (org.olat.course.ICourse)10 NotificationsHandler (org.olat.core.commons.services.notifications.NotificationsHandler)8 Locale (java.util.Locale)6 FileInfo (org.olat.core.commons.modules.bc.FileInfo)6 MetaInfo (org.olat.core.commons.modules.bc.meta.MetaInfo)6 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)6 SubscriptionItem (org.olat.core.commons.services.notifications.SubscriptionItem)6 NoSubscriptionInfo (org.olat.core.commons.services.notifications.model.NoSubscriptionInfo)6 AssertException (org.olat.core.logging.AssertException)6