From 8f3fa3a8a09d95deeb310d6a0114ed1bf3208b72 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Tue, 7 Nov 2017 10:02:46 +0100
Subject: [PATCH] CL-597: add the possibility to autoclose new lecture block
 created with the REST API

---
 .../restapi/LectureBlocksWebService.java      | 19 +++++--
 .../org/olat/restapi/LecturesBlocksTest.java  | 57 +++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/olat/modules/lecture/restapi/LectureBlocksWebService.java b/src/main/java/org/olat/modules/lecture/restapi/LectureBlocksWebService.java
index a46fcf2ccd6..62beb7b50b8 100644
--- a/src/main/java/org/olat/modules/lecture/restapi/LectureBlocksWebService.java
+++ b/src/main/java/org/olat/modules/lecture/restapi/LectureBlocksWebService.java
@@ -40,6 +40,8 @@ import javax.ws.rs.core.Response.Status;
 
 import org.olat.core.id.Roles;
 import org.olat.modules.lecture.LectureBlock;
+import org.olat.modules.lecture.LectureBlockStatus;
+import org.olat.modules.lecture.LectureRollCallStatus;
 import org.olat.modules.lecture.LectureService;
 import org.olat.modules.lecture.RepositoryEntryLectureConfiguration;
 import org.olat.modules.lecture.model.LectureBlockRefImpl;
@@ -75,7 +77,6 @@ public class LectureBlocksWebService {
 	 * @return The lecture blocks
 	 */
 	@GET
-	@Path("")
 	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 	public Response getLectureBlocks(@Context HttpServletRequest httpRequest) {
 		Roles roles = getRoles(httpRequest);
@@ -105,7 +106,6 @@ public class LectureBlocksWebService {
 	 * @return It returns the updated / created lecture block.
 	 */
 	@PUT
-	@Path("")
 	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 	@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 	public Response putLectureBlocks(LectureBlockVO block, @Context HttpServletRequest httpRequest) {
@@ -118,7 +118,10 @@ public class LectureBlocksWebService {
 	}
 	
 	/**
-	 * Create or update a lecture block.
+	 * Create or update a lecture block. The status of the blocks will be set to
+	 * autoclose only for newly created blocks. By update, the states of the
+	 * block and the roll call will not be updated.
+	 * 
 	 * @response.representation.200.qname {http://www.example.com}lectureBlocksVO
 	 * @response.representation.200.mediaType application/xml, application/json
 	 * @response.representation.200.doc The updated configuration
@@ -130,7 +133,6 @@ public class LectureBlocksWebService {
 	 * @return It returns the updated / created lecture block.
 	 */
 	@POST
-	@Path("")
 	@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
 	@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
 	public Response postLectureBlocks(LectureBlockVO block, @Context HttpServletRequest httpRequest) {
@@ -145,12 +147,18 @@ public class LectureBlocksWebService {
 	private LectureBlock saveLectureBlock(LectureBlockVO blockVo) {
 		LectureBlock block;
 		int currentPlannedLectures;
+		boolean syncParticipants = false;
 		if(blockVo.getKey() != null && blockVo.getKey() > 0) {
 			block = lectureService.getLectureBlock(blockVo);
 			currentPlannedLectures = block.getPlannedLecturesNumber();
 		} else {
 			block = lectureService.createLectureBlock(entry);
 			currentPlannedLectures = -1;
+			if("autoclosed".equals(blockVo.getRollCallStatus())) {
+				block.setStatus(LectureBlockStatus.done);
+				block.setRollCallStatus(LectureRollCallStatus.autoclosed);
+				syncParticipants = true;
+			}
 		}
 		
 		if(blockVo.getExternalId() != null) {
@@ -188,6 +196,9 @@ public class LectureBlocksWebService {
 		if(currentPlannedLectures > 0 && currentPlannedLectures != savedLectureBlock.getPlannedLecturesNumber()) {
 			lectureService.adaptRollCalls(savedLectureBlock);
 		}
+		if(syncParticipants) {
+			lectureService.syncParticipantSummaries(savedLectureBlock);
+		}
 		return savedLectureBlock;
 	}
 	
diff --git a/src/test/java/org/olat/restapi/LecturesBlocksTest.java b/src/test/java/org/olat/restapi/LecturesBlocksTest.java
index 0d774036968..d3d84e880c0 100644
--- a/src/test/java/org/olat/restapi/LecturesBlocksTest.java
+++ b/src/test/java/org/olat/restapi/LecturesBlocksTest.java
@@ -47,6 +47,8 @@ import org.olat.core.commons.persistence.DB;
 import org.olat.core.id.Identity;
 import org.olat.course.ICourse;
 import org.olat.modules.lecture.LectureBlock;
+import org.olat.modules.lecture.LectureBlockStatus;
+import org.olat.modules.lecture.LectureRollCallStatus;
 import org.olat.modules.lecture.LectureService;
 import org.olat.modules.lecture.RepositoryEntryLectureConfiguration;
 import org.olat.modules.lecture.model.LectureBlockRefImpl;
@@ -200,6 +202,61 @@ public class LecturesBlocksTest extends OlatJerseyTestCase {
 		Assert.assertNotNull(dbBlock.getEndDate());
 	}
 	
+	/**
+	 * Check that the done and autoclosed status are set.
+	 * 
+	 * @throws IOException
+	 * @throws URISyntaxException
+	 */
+	@Test
+	public void putLecturesBlock_autoclosed()
+	throws IOException, URISyntaxException {
+		Identity author = JunitTestHelper.createAndPersistIdentityAsAuthor("lect-1");
+		ICourse course = CoursesWebService.createEmptyCourse(author, "Course with absence", "Course with absence", new CourseConfigVO());
+		RepositoryEntry entry = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
+		dbInstance.commit();
+
+		RestConnection conn = new RestConnection();
+		Assert.assertTrue(conn.login("administrator", "openolat"));
+
+		String externalId = UUID.randomUUID().toString();
+		LectureBlockVO lectureBlockVo = new LectureBlockVO();
+		lectureBlockVo.setTitle("A block to close");
+		lectureBlockVo.setDescription("A description");
+		lectureBlockVo.setManagedFlagsString("all");
+		lectureBlockVo.setPlannedLectures(4);
+		lectureBlockVo.setExternalId(externalId);
+		lectureBlockVo.setStartDate(new Date());
+		lectureBlockVo.setEndDate(new Date());
+		lectureBlockVo.setStatus("done");
+		lectureBlockVo.setRollCallStatus("autoclosed");
+
+		URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("entries")
+				.path(entry.getKey().toString()).path("lectureblocks").build();
+		HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
+		conn.addJsonEntity(method, lectureBlockVo);
+		HttpResponse response = conn.execute(method);
+		
+		// check the response
+		Assertions.assertThat(response.getStatusLine().getStatusCode()).isIn(200, 201);
+
+		LectureBlockVO blockVo = conn.parse(response.getEntity().getContent(), LectureBlockVO.class);
+		Assert.assertNotNull(blockVo);
+		
+		// check the database
+		LectureBlock dbBlock = lectureService.getLectureBlock(new LectureBlockRefImpl(blockVo.getKey()));
+		Assert.assertNotNull(dbBlock);
+		Assert.assertEquals("A block to close", dbBlock.getTitle());
+		Assert.assertEquals("A description", dbBlock.getDescription());
+		Assert.assertEquals("all", dbBlock.getManagedFlagsString());
+		Assert.assertEquals(4, dbBlock.getPlannedLecturesNumber());
+		Assert.assertEquals(externalId, dbBlock.getExternalId());
+		Assert.assertNotNull(dbBlock.getStartDate());
+		Assert.assertNotNull(dbBlock.getEndDate());
+		Assert.assertEquals(LectureBlockStatus.done, dbBlock.getStatus());
+		Assert.assertEquals(LectureRollCallStatus.autoclosed, dbBlock.getRollCallStatus());
+	}
+	
 	@Test
 	public void getLecturesBlockConfiguration()
 	throws IOException, URISyntaxException {
-- 
GitLab