Search in sources :

Example 16 with CourseTreeVisitor

use of org.olat.course.run.userview.CourseTreeVisitor in project OpenOLAT by OpenOLAT.

the class MyForumsTest method myForums.

/**
 * Test retrieve the forum which the user subscribe in a course.
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void myForums() throws IOException, URISyntaxException {
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("_", "");
    RepositoryEntry myCourseRe = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(myCourseRe);
    ICourse myCourse = CourseFactory.loadCourse(myCourseRe);
    final Identity id = JunitTestHelper.createAndPersistIdentityAsUser("my-" + UUID.randomUUID().toString());
    dbInstance.commitAndCloseSession();
    // load my forums
    RestConnection conn = new RestConnection();
    assertTrue(conn.login(id.getName(), "A6B7C8"));
    // subscribed to nothing
    URI uri = UriBuilder.fromUri(getContextURI()).path("users").path(id.getKey().toString()).path("forums").build();
    HttpGet method = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    assertEquals(200, response.getStatusLine().getStatusCode());
    InputStream body = response.getEntity().getContent();
    ForumVOes forums = conn.parse(body, ForumVOes.class);
    Assert.assertNotNull(forums);
    Assert.assertNotNull(forums.getForums());
    Assert.assertEquals(0, forums.getForums().length);
    // subscribe to the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id, new Roles(false, false, false, false, false, false, false));
    new CourseTreeVisitor(myCourse, ienv).visit(new Visitor() {

        @Override
        public void visit(INode node) {
            if (node instanceof FOCourseNode) {
                FOCourseNode forumNode = (FOCourseNode) node;
                Forum forum = forumNode.loadOrCreateForum(myCourse.getCourseEnvironment());
                String businessPath = "[RepositoryEntry:" + myCourseRe.getKey() + "][CourseNode:" + forumNode.getIdent() + "]";
                SubscriptionContext forumSubContext = new SubscriptionContext("CourseModule", myCourse.getResourceableId(), forumNode.getIdent());
                PublisherData forumPdata = new PublisherData(OresHelper.calculateTypeName(Forum.class), forum.getKey().toString(), businessPath);
                NotificationsManager.getInstance().subscribe(id, forumSubContext, forumPdata);
            }
        }
    }, new VisibleTreeFilter());
    dbInstance.commitAndCloseSession();
    // retrieve my forums
    HttpGet method2 = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response2 = conn.execute(method2);
    assertEquals(200, response2.getStatusLine().getStatusCode());
    InputStream body2 = response2.getEntity().getContent();
    ForumVOes forums2 = conn.parse(body2, ForumVOes.class);
    Assert.assertNotNull(forums2);
    Assert.assertNotNull(forums2.getForums());
    Assert.assertEquals(1, forums2.getForums().length);
}
Also used : INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) InputStream(java.io.InputStream) ForumVOes(org.olat.modules.fo.restapi.ForumVOes) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Roles(org.olat.core.id.Roles) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) URI(java.net.URI) PublisherData(org.olat.core.commons.services.notifications.PublisherData) URL(java.net.URL) Forum(org.olat.modules.fo.Forum) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) File(java.io.File) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Example 17 with CourseTreeVisitor

use of org.olat.course.run.userview.CourseTreeVisitor in project OpenOLAT by OpenOLAT.

the class ForumCourseNodeWebService method getForum.

/**
 * Retrieves metadata of the published course node
 * @response.representation.200.qname {http://www.example.com}forumVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The course node metadatas
 * @response.representation.200.example {@link org.olat.modules.fo.restapi.Examples#SAMPLE_FORUMVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @response.representation.404.doc The course or parentNode not found
 * @param courseId The course resourceable's id
 * @param nodeId The node's id
 * @param httpRequest The HTTP request
 * @return The persisted structure element (fully populated)
 */
@GET
@Path("{nodeId}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getForum(@PathParam("courseId") Long courseId, @PathParam("nodeId") String nodeId, @Context HttpServletRequest httpRequest) {
    ICourse course = CoursesWebService.loadCourse(courseId);
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    } else if (!CourseWebService.isCourseAccessible(course, false, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    CourseNode courseNode = course.getRunStructure().getNode(nodeId);
    if (courseNode == null || !(courseNode instanceof FOCourseNode)) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    UserRequest ureq = getUserRequest(httpRequest);
    CourseTreeVisitor courseVisitor = new CourseTreeVisitor(course, ureq.getUserSession().getIdentityEnvironment());
    if (courseVisitor.isAccessible(courseNode, new VisibleTreeFilter())) {
        FOCourseNode forumNode = (FOCourseNode) courseNode;
        Set<Long> subscriptions = new HashSet<Long>();
        NotificationsManager man = NotificationsManager.getInstance();
        List<String> notiTypes = Collections.singletonList("Forum");
        List<Subscriber> subs = man.getSubscribers(ureq.getIdentity(), notiTypes);
        for (Subscriber sub : subs) {
            Long forumKey = Long.parseLong(sub.getPublisher().getData());
            subscriptions.add(forumKey);
        }
        ForumVO forumVo = createForumVO(course, forumNode, subscriptions);
        return Response.ok(forumVo).build();
    } else {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
}
Also used : VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) Subscriber(org.olat.core.commons.services.notifications.Subscriber) NotificationsManager(org.olat.core.commons.services.notifications.NotificationsManager) FOCourseNode(org.olat.course.nodes.FOCourseNode) CourseNode(org.olat.course.nodes.CourseNode) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with CourseTreeVisitor

use of org.olat.course.run.userview.CourseTreeVisitor in project OpenOLAT by OpenOLAT.

the class NotificationsSubscribersTest method subscribe.

@Test
public void subscribe() throws IOException, URISyntaxException {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-1");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-2");
    // deploy a course with forums
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("-", "");
    RepositoryEntry courseEntry = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(courseEntry);
    // load the course and found the first forum
    ICourse course = CourseFactory.loadCourse(courseEntry);
    // find the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id1, new Roles(false, false, false, false, false, false, false));
    ForumVisitor forumVisitor = new ForumVisitor(course);
    new CourseTreeVisitor(course, ienv).visit(forumVisitor, new VisibleTreeFilter());
    FOCourseNode courseNode = forumVisitor.firstNode;
    Forum forum = forumVisitor.firstForum;
    Assert.assertNotNull(courseNode);
    Assert.assertNotNull(forum);
    // put subscribers
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    PublisherVO subscribersVO = new PublisherVO();
    // publisher data
    subscribersVO.setType("Forum");
    subscribersVO.setData(forum.getKey().toString());
    subscribersVO.setBusinessPath("[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
    // context
    subscribersVO.setResName("CourseModule");
    subscribersVO.setResId(course.getResourceableId());
    subscribersVO.setSubidentifier(courseNode.getIdent());
    subscribersVO.getUsers().add(UserVOFactory.get(id1));
    subscribersVO.getUsers().add(UserVOFactory.get(id2));
    // create the subscribers
    URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").build();
    HttpPut putMethod = conn.createPut(subscribersUri, MediaType.APPLICATION_JSON, true);
    conn.addJsonEntity(putMethod, subscribersVO);
    HttpResponse response = conn.execute(putMethod);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    EntityUtils.consume(response.getEntity());
    // get publisher
    SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
    Publisher publisher = notificationsManager.getPublisher(subsContext);
    Assert.assertNotNull(publisher);
    // get subscribers
    List<Subscriber> subscribers = notificationsManager.getSubscribers(publisher);
    Assert.assertNotNull(subscribers);
    Assert.assertEquals(2, subscribers.size());
    conn.shutdown();
}
Also used : VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Roles(org.olat.core.id.Roles) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) Publisher(org.olat.core.commons.services.notifications.Publisher) URI(java.net.URI) URL(java.net.URL) HttpPut(org.apache.http.client.methods.HttpPut) Forum(org.olat.modules.fo.Forum) Subscriber(org.olat.core.commons.services.notifications.Subscriber) PublisherVO(org.olat.core.commons.services.notifications.restapi.vo.PublisherVO) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) File(java.io.File) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) Test(org.junit.Test)

Example 19 with CourseTreeVisitor

use of org.olat.course.run.userview.CourseTreeVisitor in project OpenOLAT by OpenOLAT.

the class NotificationsSubscribersTest method unsubscribe.

@Test
public void unsubscribe() throws IOException, URISyntaxException {
    Identity id1 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-3");
    Identity id2 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-4");
    Identity id3 = JunitTestHelper.createAndPersistIdentityAsRndUser("rest-sub-5");
    // deploy a course with forums
    URL courseWithForumsUrl = MyForumsTest.class.getResource("myCourseWS.zip");
    Assert.assertNotNull(courseWithForumsUrl);
    File courseWithForums = new File(courseWithForumsUrl.toURI());
    String softKey = UUID.randomUUID().toString().replace("-", "");
    RepositoryEntry courseEntry = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4);
    Assert.assertNotNull(courseEntry);
    // load the course and found the first forum
    ICourse course = CourseFactory.loadCourse(courseEntry);
    // find the forum
    IdentityEnvironment ienv = new IdentityEnvironment(id1, new Roles(false, false, false, false, false, false, false));
    ForumVisitor forumVisitor = new ForumVisitor(course);
    new CourseTreeVisitor(course, ienv).visit(forumVisitor, new VisibleTreeFilter());
    FOCourseNode courseNode = forumVisitor.firstNode;
    Forum forum = forumVisitor.firstForum;
    Assert.assertNotNull(courseNode);
    Assert.assertNotNull(forum);
    // the 3 users subscribed to the forum
    PublisherData publisherData = new PublisherData("Forum", forum.getKey().toString(), "[RepositoryEntry:" + courseEntry.getKey() + "][CourseNode:" + courseNode.getIdent() + "]");
    SubscriptionContext subsContext = new SubscriptionContext("CourseModule", course.getResourceableId(), courseNode.getIdent());
    notificationsManager.subscribe(id1, subsContext, publisherData);
    notificationsManager.subscribe(id2, subsContext, publisherData);
    notificationsManager.subscribe(id3, subsContext, publisherData);
    dbInstance.commitAndCloseSession();
    // get the subscriber
    RestConnection conn = new RestConnection();
    Assert.assertTrue(conn.login("administrator", "openolat"));
    URI subscribersUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subsContext.getResName()).path(subsContext.getResId().toString()).path(subsContext.getSubidentifier()).build();
    HttpGet getMethod = conn.createGet(subscribersUri, MediaType.APPLICATION_JSON, true);
    HttpResponse getResponse = conn.execute(getMethod);
    Assert.assertEquals(200, getResponse.getStatusLine().getStatusCode());
    List<SubscriberVO> subscriberVOes = parseGroupArray(getResponse.getEntity().getContent());
    Assert.assertNotNull(subscriberVOes);
    Assert.assertEquals(3, subscriberVOes.size());
    SubscriberVO subscriberId2VO = null;
    for (SubscriberVO subscriberVO : subscriberVOes) {
        if (subscriberVO.getIdentityKey().equals(id2.getKey())) {
            subscriberId2VO = subscriberVO;
        }
    }
    // delete id2
    URI deleteSubscriberUri = UriBuilder.fromUri(getContextURI()).path("notifications").path("subscribers").path(subscriberId2VO.getSubscriberKey().toString()).build();
    HttpDelete deleteMethod = conn.createDelete(deleteSubscriberUri, MediaType.APPLICATION_JSON);
    HttpResponse deleteResponse = conn.execute(deleteMethod);
    Assert.assertEquals(200, deleteResponse.getStatusLine().getStatusCode());
    // check
    Publisher publisher = notificationsManager.getPublisher(subsContext);
    List<Subscriber> survivingSubscribers = notificationsManager.getSubscribers(publisher);
    Assert.assertNotNull(survivingSubscribers);
    Assert.assertEquals(2, survivingSubscribers.size());
    for (Subscriber subscriber : survivingSubscribers) {
        Assert.assertNotEquals(id2, subscriber.getIdentity());
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) HttpGet(org.apache.http.client.methods.HttpGet) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) RepositoryEntry(org.olat.repository.RepositoryEntry) PublisherData(org.olat.core.commons.services.notifications.PublisherData) URI(java.net.URI) URL(java.net.URL) Forum(org.olat.modules.fo.Forum) SubscriberVO(org.olat.core.commons.services.notifications.restapi.vo.SubscriberVO) Subscriber(org.olat.core.commons.services.notifications.Subscriber) SubscriptionContext(org.olat.core.commons.services.notifications.SubscriptionContext) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) HttpResponse(org.apache.http.HttpResponse) Roles(org.olat.core.id.Roles) Publisher(org.olat.core.commons.services.notifications.Publisher) File(java.io.File) Test(org.junit.Test)

Example 20 with CourseTreeVisitor

use of org.olat.course.run.userview.CourseTreeVisitor in project OpenOLAT by OpenOLAT.

the class UserCalendarWebService method getCalendars.

private void getCalendars(CalendarVisitor calVisitor, UserRequest ureq) {
    Roles roles = ureq.getUserSession().getRoles();
    Identity retrievedUser = ureq.getIdentity();
    CalendarModule calendarModule = CoreSpringFactory.getImpl(CalendarModule.class);
    if (calendarModule.isEnabled()) {
        if (calendarModule.isEnablePersonalCalendar()) {
            KalendarRenderWrapper personalWrapper = getPersonalCalendar(ureq.getIdentity());
            calVisitor.visit(personalWrapper);
        }
        if (calendarModule.isEnableCourseToolCalendar() || calendarModule.isEnableCourseElementCalendar()) {
            RepositoryManager rm = RepositoryManager.getInstance();
            ACService acManager = CoreSpringFactory.getImpl(ACService.class);
            SearchRepositoryEntryParameters repoParams = new SearchRepositoryEntryParameters(retrievedUser, roles, "CourseModule");
            repoParams.setOnlyExplicitMember(true);
            repoParams.setIdentity(retrievedUser);
            IdentityEnvironment ienv = new IdentityEnvironment();
            ienv.setIdentity(retrievedUser);
            ienv.setRoles(roles);
            List<RepositoryEntry> entries = rm.genericANDQueryWithRolesRestriction(repoParams, 0, -1, true);
            for (RepositoryEntry entry : entries) {
                AccessResult result = acManager.isAccessible(entry, retrievedUser, false);
                if (result.isAccessible()) {
                    try {
                        final ICourse course = CourseFactory.loadCourse(entry);
                        CourseConfig config = course.getCourseEnvironment().getCourseConfig();
                        UserCourseEnvironment userCourseEnv = new UserCourseEnvironmentImpl(ienv, course.getCourseEnvironment());
                        if (config.isCalendarEnabled()) {
                            KalendarRenderWrapper wrapper = CourseCalendars.getCourseCalendarWrapper(ureq, userCourseEnv, null);
                            calVisitor.visit(wrapper);
                        } else {
                            CalCourseNodeVisitor visitor = new CalCourseNodeVisitor();
                            new CourseTreeVisitor(course, ienv).visit(visitor, new VisibleTreeFilter());
                            if (visitor.isFound()) {
                                KalendarRenderWrapper wrapper = CourseCalendars.getCourseCalendarWrapper(ureq, userCourseEnv, null);
                                calVisitor.visit(wrapper);
                            }
                        }
                    } catch (Exception e) {
                        log.error("", e);
                    }
                }
            }
        }
        if (calendarModule.isEnableGroupCalendar()) {
            CollaborationManager collaborationManager = CoreSpringFactory.getImpl(CollaborationManager.class);
            // start found forums in groups
            BusinessGroupService bgm = CoreSpringFactory.getImpl(BusinessGroupService.class);
            SearchBusinessGroupParams params = new SearchBusinessGroupParams(retrievedUser, true, true);
            params.addTools(CollaborationTools.TOOL_CALENDAR);
            List<BusinessGroup> groups = bgm.findBusinessGroups(params, null, 0, -1);
            for (BusinessGroup group : groups) {
                KalendarRenderWrapper wrapper = collaborationManager.getCalendar(group, ureq, false);
                calVisitor.visit(wrapper);
            }
        }
    }
}
Also used : SearchRepositoryEntryParameters(org.olat.repository.model.SearchRepositoryEntryParameters) UserCourseEnvironment(org.olat.course.run.userview.UserCourseEnvironment) BusinessGroup(org.olat.group.BusinessGroup) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) Roles(org.olat.core.id.Roles) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) KalendarRenderWrapper(org.olat.commons.calendar.ui.components.KalendarRenderWrapper) WebApplicationException(javax.ws.rs.WebApplicationException) SearchBusinessGroupParams(org.olat.group.model.SearchBusinessGroupParams) CourseConfig(org.olat.course.config.CourseConfig) UserCourseEnvironmentImpl(org.olat.course.run.userview.UserCourseEnvironmentImpl) BusinessGroupService(org.olat.group.BusinessGroupService) ACService(org.olat.resource.accesscontrol.ACService) AccessResult(org.olat.resource.accesscontrol.AccessResult) CalendarModule(org.olat.commons.calendar.CalendarModule) RepositoryManager(org.olat.repository.RepositoryManager) Identity(org.olat.core.id.Identity) IdentityEnvironment(org.olat.core.id.IdentityEnvironment) CollaborationManager(org.olat.collaboration.CollaborationManager)

Aggregations

CourseTreeVisitor (org.olat.course.run.userview.CourseTreeVisitor)26 VisibleTreeFilter (org.olat.course.run.userview.VisibleTreeFilter)26 ICourse (org.olat.course.ICourse)24 Subscriber (org.olat.core.commons.services.notifications.Subscriber)16 IdentityEnvironment (org.olat.core.id.IdentityEnvironment)16 FOCourseNode (org.olat.course.nodes.FOCourseNode)16 Identity (org.olat.core.id.Identity)14 Roles (org.olat.core.id.Roles)14 INode (org.olat.core.util.nodes.INode)14 Visitor (org.olat.core.util.tree.Visitor)14 GET (javax.ws.rs.GET)12 Produces (javax.ws.rs.Produces)12 NotificationsManager (org.olat.core.commons.services.notifications.NotificationsManager)12 ArrayList (java.util.ArrayList)10 HashSet (java.util.HashSet)10 UserRequest (org.olat.core.gui.UserRequest)10 BCCourseNode (org.olat.course.nodes.BCCourseNode)10 RepositoryEntry (org.olat.repository.RepositoryEntry)10 RestSecurityHelper.getUserRequest (org.olat.restapi.security.RestSecurityHelper.getUserRequest)10 URI (java.net.URI)8