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);
}
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();
}
}
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();
}
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());
}
}
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);
}
}
}
}
Aggregations