From 3036ff01985431dceaea4d2d6e6743ea8ad0ede6 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 23 May 2013 09:22:47 +0200
Subject: [PATCH] no-jira: add some REST method to add participants and coach
 to a course

---
 .../course/CourseGroupWebService.java         | 23 +++--
 .../repository/course/CourseWebService.java   | 89 +++++++++++++++++++
 .../java/org/olat/restapi/CourseTest.java     | 39 ++++++++
 3 files changed, 143 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/olat/restapi/repository/course/CourseGroupWebService.java b/src/main/java/org/olat/restapi/repository/course/CourseGroupWebService.java
index fc4afda97ca..fec09072bec 100644
--- a/src/main/java/org/olat/restapi/repository/course/CourseGroupWebService.java
+++ b/src/main/java/org/olat/restapi/repository/course/CourseGroupWebService.java
@@ -211,15 +211,22 @@ public class CourseGroupWebService {
 
 		UserRequest ureq = RestSecurityHelper.getUserRequest(request);
     BusinessGroupService bgm = CoreSpringFactory.getImpl(BusinessGroupService.class);
-
-		String name = group.getName();
-		String desc = group.getDescription();
-		Integer min = normalize(group.getMinParticipants());
-		Integer max = normalize(group.getMaxParticipants());
-		
 		RepositoryEntry courseRe = RepositoryManager.getInstance().lookupRepositoryEntry(course, false);
-		BusinessGroup bg = bgm.createBusinessGroup(ureq.getIdentity(), name, desc, min, max, false, false, courseRe);
-		GroupVO savedVO = ObjectFactory.get(bg);
+    
+		BusinessGroup bg;
+    if(group.getKey() != null && group.getKey() > 0) {
+    	//group already exists
+    	bg = bgm.loadBusinessGroup(group.getKey());
+    	bgm.addResourceTo(bg, courseRe);
+    } else {
+    	String name = group.getName();
+  		String desc = group.getDescription();
+  		Integer min = normalize(group.getMinParticipants());
+  		Integer max = normalize(group.getMaxParticipants());
+  		
+  		bg = bgm.createBusinessGroup(ureq.getIdentity(), name, desc, min, max, false, false, courseRe);
+    }
+    GroupVO savedVO = ObjectFactory.get(bg);
 		return Response.ok(savedVO).build();
 	}
 	
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 0e099c45097..609045735e1 100644
--- a/src/main/java/org/olat/restapi/repository/course/CourseWebService.java
+++ b/src/main/java/org/olat/restapi/repository/course/CourseWebService.java
@@ -62,6 +62,7 @@ import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
 import org.olat.core.util.StringHelper;
 import org.olat.core.util.coordinate.LockResult;
+import org.olat.core.util.mail.MailPackage;
 import org.olat.core.util.resource.OresHelper;
 import org.olat.core.util.vfs.VFSItem;
 import org.olat.core.util.xml.XStreamHelper;
@@ -641,6 +642,94 @@ public class CourseWebService {
 		return Response.ok().build();
 	}
 	
+	/**
+	 * Add a coach to the course
+   * @response.representation.200.doc The user is a coach of the course
+	 * @response.representation.401.doc The roles of the authenticated user are not sufficient
+   * @response.representation.404.doc The course or the user not found
+	 * @param courseId The course resourceable's id
+	 * @param identityKey The user identifier
+	 * @param httpRequest The HTTP request
+	 * @return It returns 200  if the user is added as coach of the course
+	 */
+	@PUT
+	@Path("tutors/{identityKey}")
+	public Response addCoach(@PathParam("courseId") Long courseId, @PathParam("identityKey") Long identityKey,
+			@Context HttpServletRequest httpRequest) {
+		if(!isAuthor(httpRequest)) {
+			return Response.serverError().status(Status.UNAUTHORIZED).build();
+		}
+		
+		OLATResourceable course = getCourseOLATResource(courseId);
+		if(course == null) {
+			return Response.serverError().status(Status.NOT_FOUND).build();
+		}  else if (!isAuthorEditor(course, httpRequest)) {
+			return Response.serverError().status(Status.UNAUTHORIZED).build();
+		}
+
+		BaseSecurity securityManager = BaseSecurityManager.getInstance();
+		Identity tutor = securityManager.loadIdentityByKey(identityKey, false);
+		if(tutor == null) {
+			return Response.serverError().status(Status.NOT_FOUND).build();
+		}
+		
+		Identity identity = getIdentity(httpRequest);
+		UserRequest ureq = getUserRequest(httpRequest);
+		
+		//add the author as owner of the course
+		RepositoryManager rm = RepositoryManager.getInstance();
+		RepositoryEntry repositoryEntry = rm.lookupRepositoryEntry(course, true);
+		List<Identity> tutors = Collections.singletonList(tutor);
+		IdentitiesAddEvent iae = new IdentitiesAddEvent(tutors);
+		rm.addTutors(identity, ureq.getUserSession().getRoles(), iae, repositoryEntry, new MailPackage(false));
+		
+		return Response.ok().build();
+	}
+	
+	/**
+	 * Add an participant to the course
+   * @response.representation.200.doc The user is a participant of the course
+	 * @response.representation.401.doc The roles of the authenticated user are not sufficient
+   * @response.representation.404.doc The course or the user not found
+	 * @param courseId The course resourceable's id
+	 * @param identityKey The user identifier
+	 * @param httpRequest The HTTP request
+	 * @return It returns 200  if the user is added as owner and author of the course
+	 */
+	@PUT
+	@Path("participants/{identityKey}")
+	public Response addParticipant(@PathParam("courseId") Long courseId, @PathParam("identityKey") Long identityKey,
+			@Context HttpServletRequest httpRequest) {
+		if(!isAuthor(httpRequest)) {
+			return Response.serverError().status(Status.UNAUTHORIZED).build();
+		}
+		
+		OLATResourceable course = getCourseOLATResource(courseId);
+		if(course == null) {
+			return Response.serverError().status(Status.NOT_FOUND).build();
+		}  else if (!isAuthorEditor(course, httpRequest)) {
+			return Response.serverError().status(Status.UNAUTHORIZED).build();
+		}
+
+		BaseSecurity securityManager = BaseSecurityManager.getInstance();
+		Identity participant = securityManager.loadIdentityByKey(identityKey, false);
+		if(participant == null) {
+			return Response.serverError().status(Status.NOT_FOUND).build();
+		}
+		
+		Identity identity = getIdentity(httpRequest);
+		UserRequest ureq = getUserRequest(httpRequest);
+		
+		//add the author as owner of the course
+		RepositoryManager rm = RepositoryManager.getInstance();
+		RepositoryEntry repositoryEntry = rm.lookupRepositoryEntry(course, true);
+		List<Identity> participants = Collections.singletonList(participant);
+		IdentitiesAddEvent iae = new IdentitiesAddEvent(participants);
+		rm.addParticipants(identity, ureq.getUserSession().getRoles(), iae, repositoryEntry, new MailPackage(false));
+		
+		return Response.ok().build();
+	}
+	
 	private OLATResource getCourseOLATResource(Long courseId) {
 		String typeName = OresHelper.calculateTypeName(CourseModule.class);
 		OLATResource ores = OLATResourceManager.getInstance().findResourceable(courseId, typeName);
diff --git a/src/test/java/org/olat/restapi/CourseTest.java b/src/test/java/org/olat/restapi/CourseTest.java
index 9ec57d2d33b..9b6a19823f4 100644
--- a/src/test/java/org/olat/restapi/CourseTest.java
+++ b/src/test/java/org/olat/restapi/CourseTest.java
@@ -219,6 +219,7 @@ public class CourseTest extends OlatJerseyTestCase {
 		HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
 		HttpResponse response = conn.execute(method);
 		assertEquals(200, response.getStatusLine().getStatusCode());
+		EntityUtils.consume(response.getEntity());
 
 		//is auth0 author
 		BaseSecurity securityManager = BaseSecurityManager.getInstance();
@@ -318,6 +319,44 @@ public class CourseTest extends OlatJerseyTestCase {
 		DBFactory.getInstance().intermediateCommit();
 	}
 	
+	@Test
+	public void testAddCoach() throws IOException, URISyntaxException {
+		assertTrue(conn.login("administrator", "openolat"));
+		URI request = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/tutors/" + auth1.getKey()).build();
+		HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
+		HttpResponse response = conn.execute(method);
+		assertEquals(200, response.getStatusLine().getStatusCode());
+		EntityUtils.consume(response.getEntity());
+
+		//is auth0 coach/tutor
+		RepositoryManager rm = RepositoryManager.getInstance();
+		BaseSecurity securityManager = BaseSecurityManager.getInstance();
+		RepositoryEntry repositoryEntry = rm.lookupRepositoryEntry(course1, true);
+		SecurityGroup tutorGroup = repositoryEntry.getTutorGroup();
+		boolean isTutor = securityManager.isIdentityInSecurityGroup(auth1, tutorGroup);
+		DBFactory.getInstance().intermediateCommit();
+		assertTrue(isTutor);
+	}
+	
+	@Test
+	public void testAddParticipant() throws IOException, URISyntaxException {
+		assertTrue(conn.login("administrator", "openolat"));
+		URI request = UriBuilder.fromUri(getContextURI()).path("/repo/courses/" + course1.getResourceableId() + "/participants/" + auth2.getKey()).build();
+		HttpPut method = conn.createPut(request, MediaType.APPLICATION_JSON, true);
+		HttpResponse response = conn.execute(method);
+		assertEquals(200, response.getStatusLine().getStatusCode());
+		EntityUtils.consume(response.getEntity());
+
+		//is auth2 participant
+		RepositoryManager rm = RepositoryManager.getInstance();
+		BaseSecurity securityManager = BaseSecurityManager.getInstance();
+		RepositoryEntry repositoryEntry = rm.lookupRepositoryEntry(course1, true);
+		SecurityGroup participant = repositoryEntry.getParticipantGroup();
+		boolean isParticipant = securityManager.isIdentityInSecurityGroup(auth2, participant);
+		DBFactory.getInstance().intermediateCommit();
+		assertTrue(isParticipant);
+	}
+	
 	protected List<UserVO> parseUserArray(InputStream body) {
 		try {
 			ObjectMapper mapper = new ObjectMapper(jsonFactory); 
-- 
GitLab