diff --git a/src/main/java/org/olat/modules/coach/manager/CoachingDAO.java b/src/main/java/org/olat/modules/coach/manager/CoachingDAO.java index 067dc492a05eda23659db2b81b6db7b2307bfe97..cab522410260b9ab2b06f6f28d45380130fc5171 100644 --- a/src/main/java/org/olat/modules/coach/manager/CoachingDAO.java +++ b/src/main/java/org/olat/modules/coach/manager/CoachingDAO.java @@ -19,6 +19,7 @@ */ package org.olat.modules.coach.manager; +import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -37,6 +38,8 @@ import org.olat.core.commons.persistence.DB; import org.olat.core.commons.persistence.NativeQueryBuilder; import org.olat.core.commons.persistence.PersistenceHelper; import org.olat.core.id.Identity; +import org.olat.core.logging.OLog; +import org.olat.core.logging.Tracing; import org.olat.core.util.StringHelper; import org.olat.course.assessment.UserEfficiencyStatement; import org.olat.course.assessment.model.UserEfficiencyStatementLight; @@ -61,6 +64,8 @@ import org.springframework.stereotype.Service; */ @Service public class CoachingDAO { + + private static final OLog log = Tracing.createLoggerFor(CoachingDAO.class); @Autowired private DB dbInstance; @@ -655,19 +660,8 @@ public class CoachingDAO { for(Object rawObject:rawList) { Object[] rawStat = (Object[])rawObject; StudentStatEntry entry = new StudentStatEntry(((Number)rawStat[0]).longValue()); - - String repoIds = (String)rawStat[1]; { - if(StringHelper.containsNonWhitespace(repoIds)) - for(String repoId:repoIds.split(",")) { - entry.getRepoIds().add(repoId); - } - } - String launchIds = (String)rawStat[2]; - if(StringHelper.containsNonWhitespace(launchIds)) { - for(String launchId:launchIds.split(",")) { - entry.getLaunchIds().add(launchId); - } - } + appendArrayToSet(rawStat[1], entry.getRepoIds()); + appendArrayToSet(rawStat[2], entry.getLaunchIds()); map.put(entry.getStudentKey(), entry); } return rawList.size() > 0; @@ -706,23 +700,41 @@ public class CoachingDAO { entry = new StudentStatEntry(identityKey); map.put(identityKey, entry); } - String repoIds = (String)rawStat[1]; - if(StringHelper.containsNonWhitespace(repoIds)) { - for(String repoId:repoIds.split(",")) { - entry.getRepoIds().add(repoId); - } - } - String launchIds = (String)rawStat[2]; - if(StringHelper.containsNonWhitespace(launchIds)) { - for(String launchId:launchIds.split(",")) { - entry.getLaunchIds().add(launchId); - } - } + appendArrayToSet(rawStat[1], entry.getRepoIds()); + appendArrayToSet(rawStat[2], entry.getLaunchIds()); stats.put(entry.getStudentKey(), entry); } return rawList.size() > 0; } + /** + * Catch null value, strings and blob + * + * @param rawObject + * @param ids + */ + private void appendArrayToSet(Object rawObject, Set<String> ids) { + String rawString = null; + if(rawObject instanceof String) { + rawString = (String)rawObject; + } else if(rawObject instanceof byte[]) { + try { + byte[] rawByteArr = (byte[])rawObject; + rawString = new String(rawByteArr, "UTF-8"); + } catch (UnsupportedEncodingException e) { + log.error("", e); + } + } else if (rawObject != null) { + log.error("Unkown format: " + rawObject.getClass().getName() + " / " + rawObject); + } + + if(StringHelper.containsNonWhitespace(rawString)) { + for(String launchId:rawString.split(",")) { + ids.add(launchId); + } + } + } + private boolean getStudentsStatisticStatement(IdentityRef coach, Map<Long,StudentStatEntry> stats) { NativeQueryBuilder sb = new NativeQueryBuilder(1024, dbInstance); sb.append("select ") diff --git a/src/main/java/org/olat/restapi/repository/course/CourseWebService.java b/src/main/java/org/olat/restapi/repository/course/CourseWebService.java index acc02d4334b70ea8e5cfa7fa8d23571588e5c508..bbbc5321908dc535041307c90da832b866ddf7b4 100644 --- a/src/main/java/org/olat/restapi/repository/course/CourseWebService.java +++ b/src/main/java/org/olat/restapi/repository/course/CourseWebService.java @@ -371,9 +371,9 @@ public class CourseWebService { return Response.serverError().status(Status.UNAUTHORIZED).build(); } - ICourse course = CourseFactory.openCourseEditSession(courseId); + ICourse editedCourse = CourseFactory.openCourseEditSession(courseId); //change course config - CourseConfig courseConfig = course.getCourseEnvironment().getCourseConfig(); + CourseConfig courseConfig = editedCourse.getCourseEnvironment().getCourseConfig(); if(calendar != null) { courseConfig.setCalendarEnabled(calendar.booleanValue()); } @@ -393,10 +393,10 @@ public class CourseWebService { courseConfig.setSharedFolderSoftkey(sharedFolderSoftkey); } - CourseFactory.setCourseConfig(course.getResourceableId(), courseConfig); - CourseFactory.closeCourseEditSession(course.getResourceableId(),true); + CourseFactory.setCourseConfig(editedCourse.getResourceableId(), courseConfig); + CourseFactory.closeCourseEditSession(editedCourse.getResourceableId(),true); - CourseConfigVO vo = ObjectFactory.getConfig(course); + CourseConfigVO vo = ObjectFactory.getConfig(editedCourse); return Response.ok(vo).build(); } @@ -487,6 +487,47 @@ public class CourseWebService { return Response.ok(authors).build(); } + /** + * Get all coaches of the course (don't follow the groups) + * @response.representation.200.qname {http://www.example.com}userVO + * @response.representation.200.mediaType application/xml, application/json + * @response.representation.200.doc The array of coaches + * @response.representation.401.doc The roles of the authenticated user are not sufficient + * @response.representation.404.doc The course not found + * @param httpRequest The HTTP request + * @return It returns an array of <code>UserVO</code> + */ + @GET + @Path("tutors") + @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) + public Response getTutors(@Context HttpServletRequest httpRequest) { + if (!isAuthorEditor(course, httpRequest)) { + return Response.serverError().status(Status.UNAUTHORIZED).build(); + } + + RepositoryManager rm = RepositoryManager.getInstance(); + RepositoryEntry repositoryEntry = rm.lookupRepositoryEntry(course, true); + RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class); + List<Identity> coachList = repositoryService.getMembers(repositoryEntry, GroupRoles.coach.name()); + + int count = 0; + UserVO[] coaches = new UserVO[coachList.size()]; + for(Identity coach:coachList) { + coaches[count++] = UserVOFactory.get(coach); + } + return Response.ok(coaches).build(); + } + + /** + * Get all participants of the course (don't follow the groups) + * @response.representation.200.qname {http://www.example.com}userVO + * @response.representation.200.mediaType application/xml, application/json + * @response.representation.200.doc The array of participants + * @response.representation.401.doc The roles of the authenticated user are not sufficient + * @response.representation.404.doc The course not found + * @param httpRequest The HTTP request + * @return It returns an array of <code>UserVO</code> + */ @GET @Path("participants") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) diff --git a/src/test/java/org/olat/restapi/CourseTest.java b/src/test/java/org/olat/restapi/CourseTest.java index 3a57f3a0a8548067b767807a51f03cd6775b7243..7f7d2b05cbdf6ea1c4dda4c4a19696463dbb38cf 100644 --- a/src/test/java/org/olat/restapi/CourseTest.java +++ b/src/test/java/org/olat/restapi/CourseTest.java @@ -341,6 +341,33 @@ public class CourseTest extends OlatJerseyTestCase { dbInstance.intermediateCommit(); } + @Test + public void testGetTutors() throws IOException, URISyntaxException { + Identity coach = JunitTestHelper.createAndPersistIdentityAsRndUser("Course-coach"); + RepositoryEntry repositoryEntry = repositoryManager.lookupRepositoryEntry(course1, true); + repositoryService.addRole(coach, repositoryEntry, GroupRoles.coach.name()); + dbInstance.intermediateCommit(); + + //get them + Assert.assertTrue(conn.login("administrator", "openolat")); + URI uri = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/tutors").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(); + Assert.assertNotNull(body); + + List<UserVO> tutorVOs = parseUserArray(body); + Assert.assertNotNull(tutorVOs); + boolean found = false; + for(UserVO tutorVo:tutorVOs) { + if(tutorVo.getKey().equals(coach.getKey())) { + found = true; + } + } + Assert.assertTrue(found); + } + @Test public void testAddCoach() throws IOException, URISyntaxException { assertTrue(conn.login("administrator", "openolat")); @@ -357,6 +384,33 @@ public class CourseTest extends OlatJerseyTestCase { assertTrue(isTutor); } + @Test + public void testGetParticipants() throws IOException, URISyntaxException { + Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("Course-participant"); + RepositoryEntry repositoryEntry = repositoryManager.lookupRepositoryEntry(course1, true); + repositoryService.addRole(participant, repositoryEntry, GroupRoles.participant.name()); + dbInstance.intermediateCommit(); + + //get them + Assert.assertTrue(conn.login("administrator", "openolat")); + URI uri = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/participants").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(); + Assert.assertNotNull(body); + + List<UserVO> participantVOs = parseUserArray(body); + Assert.assertNotNull(participantVOs); + boolean found = false; + for(UserVO participantVo:participantVOs) { + if(participantVo.getKey().equals(participant.getKey())) { + found = true; + } + } + Assert.assertTrue(found); + } + @Test public void testAddParticipant() throws IOException, URISyntaxException { assertTrue(conn.login("administrator", "openolat"));