From 75ff191c08885ba70f9dc813dd99b3fac923c363 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20G=C3=A4rtner?= <andreas.gaertner@hotmail.com>
Date: Tue, 8 Dec 2015 14:08:20 +0100
Subject: [PATCH] Add feedback lock feature

---
 .../arsnova/controller/SessionController.java | 11 ++++++++
 .../java/de/thm/arsnova/dao/CouchDBDao.java   |  2 ++
 .../java/de/thm/arsnova/entities/Session.java | 11 ++++++++
 .../thm/arsnova/services/FeedbackService.java | 15 +++++++++++
 .../arsnova/services/IFeedbackService.java    |  2 ++
 .../thm/arsnova/services/ISessionService.java |  2 ++
 .../thm/arsnova/services/SessionService.java  | 25 +++++++++++++++++--
 7 files changed, 66 insertions(+), 2 deletions(-)

diff --git a/src/main/java/de/thm/arsnova/controller/SessionController.java b/src/main/java/de/thm/arsnova/controller/SessionController.java
index 7b9e1bdb..d1e4f32c 100644
--- a/src/main/java/de/thm/arsnova/controller/SessionController.java
+++ b/src/main/java/de/thm/arsnova/controller/SessionController.java
@@ -334,6 +334,17 @@ public class SessionController extends PaginationController {
 		return sessionService.changeSessionFeatures(sessionkey, features);
 	}
 
+	@RequestMapping(value = "/{sessionkey}/lockfeedbackinput", method = RequestMethod.POST)
+	@ApiOperation(value = "locks input of user live feedback",
+			nickname = "lockFeedbackInput")
+	public boolean lockFeedbackInput(
+			@ApiParam(value = "session-key from current session", required = true) @PathVariable final String sessionkey,
+			@ApiParam(value="lock", required=true) @RequestParam(required = true) final Boolean lock,
+			@ApiParam(value = "http servlet response", required = true) final HttpServletResponse response
+			) {
+		return sessionService.lockFeedbackInput(sessionkey, lock);
+	}
+
 	/* internal redirections */
 
 	@RequestMapping(value = "/{sessionKey}/lecturerquestion")
diff --git a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
index a9385e8e..153319dc 100644
--- a/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
+++ b/src/main/java/de/thm/arsnova/dao/CouchDBDao.java
@@ -488,6 +488,7 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 		sessionDocument.put("ppLevel", session.getPpLevel());
 		sessionDocument.put("sessionType", session.getSessionType());
 		sessionDocument.put("features", JSONObject.fromObject(session.getFeatures()));
+		sessionDocument.put("feedbackLock", false);
 		try {
 			database.saveDocument(sessionDocument);
 		} catch (final IOException e) {
@@ -1549,6 +1550,7 @@ public class CouchDBDao implements IDatabaseDao, ApplicationEventPublisherAware
 			s.put("ppLevel", session.getPpLevel());
 			s.put("learningProgressOptions", JSONObject.fromObject(session.getLearningProgressOptions()));
 			s.put("features", JSONObject.fromObject(session.getFeatures()));
+			s.put("feedbackLock", session.getFeedbackLock());
 			database.saveDocument(s);
 			session.set_rev(s.getRev());
 
diff --git a/src/main/java/de/thm/arsnova/entities/Session.java b/src/main/java/de/thm/arsnova/entities/Session.java
index eeb11981..a681dc2a 100644
--- a/src/main/java/de/thm/arsnova/entities/Session.java
+++ b/src/main/java/de/thm/arsnova/entities/Session.java
@@ -56,6 +56,7 @@ public class Session implements Serializable {
 	private String ppFaculty;
 	private String ppLevel;
 	private String sessionType;
+	private boolean feedbackLock;
 
 	private String _id;
 	private String _rev;
@@ -90,6 +91,7 @@ public class Session implements Serializable {
 		copy.ppFaculty = original.ppFaculty;
 		copy.ppLevel = original.ppLevel;
 		copy.sessionType = original.sessionType;
+		copy.feedbackLock = original.feedbackLock;
 
 		copy._id = original._id;
 		copy._rev = original._rev;
@@ -329,6 +331,15 @@ public class Session implements Serializable {
 		this.sessionType = sessionType;
 	}
 
+	@ApiModelProperty(required = true, value = "the feedback lock status")
+	public boolean getFeedbackLock() {
+		return feedbackLock;
+	}
+
+	public void setFeedbackLock(Boolean lock) {
+		this.feedbackLock = lock;
+	}
+
 	@Override
 	public String toString() {
 		return "Session [keyword=" + keyword + ", type=" + type + ", creator=" + creator + "]";
diff --git a/src/main/java/de/thm/arsnova/services/FeedbackService.java b/src/main/java/de/thm/arsnova/services/FeedbackService.java
index 03112362..0d48aaad 100644
--- a/src/main/java/de/thm/arsnova/services/FeedbackService.java
+++ b/src/main/java/de/thm/arsnova/services/FeedbackService.java
@@ -109,6 +109,21 @@ public class FeedbackService implements IFeedbackService, ApplicationEventPublis
 		}
 	}
 
+	@Override
+	public void cleanFeedbackVotesInSession(final String keyword, final int cleanupFeedbackDelayInMins) {
+		final Session session = databaseDao.getSessionFromKeyword(keyword);
+		List<User> affectedUsers = feedbackStorage.cleanFeedbackVotesInSession(session, cleanupFeedbackDelayInMins);
+		Set<Session> sessionSet = new HashSet<Session>();
+		sessionSet.add(session);
+
+		// Send feedback reset event to all affected users
+		for (User user : affectedUsers) {
+			this.publisher.publishEvent(new DeleteFeedbackForSessionsEvent(this, sessionSet, user));
+		}
+		// send the new feedback to all clients in affected session
+		this.publisher.publishEvent(new NewFeedbackEvent(this, session));
+	}
+
 	@Override
 	public Feedback getFeedback(final String keyword) {
 		final Session session = databaseDao.getSessionFromKeyword(keyword);
diff --git a/src/main/java/de/thm/arsnova/services/IFeedbackService.java b/src/main/java/de/thm/arsnova/services/IFeedbackService.java
index 407f323f..ef87c5fe 100644
--- a/src/main/java/de/thm/arsnova/services/IFeedbackService.java
+++ b/src/main/java/de/thm/arsnova/services/IFeedbackService.java
@@ -26,6 +26,8 @@ import de.thm.arsnova.entities.User;
 public interface IFeedbackService {
 	void cleanFeedbackVotes();
 
+	void cleanFeedbackVotesInSession(String keyword, int cleanupFeedbackDelayInMins);
+
 	Feedback getFeedback(String keyword);
 
 	int getFeedbackCount(String keyword);
diff --git a/src/main/java/de/thm/arsnova/services/ISessionService.java b/src/main/java/de/thm/arsnova/services/ISessionService.java
index 22ccbf90..fa405d81 100644
--- a/src/main/java/de/thm/arsnova/services/ISessionService.java
+++ b/src/main/java/de/thm/arsnova/services/ISessionService.java
@@ -77,4 +77,6 @@ public interface ISessionService {
 	SessionFeature getSessionFeatures(String sessionkey);
 
 	SessionFeature changeSessionFeatures(String sessionkey, SessionFeature features);
+
+	boolean lockFeedbackInput(String sessionkey, Boolean lock);
 }
diff --git a/src/main/java/de/thm/arsnova/services/SessionService.java b/src/main/java/de/thm/arsnova/services/SessionService.java
index a2df7abf..e9e14221 100644
--- a/src/main/java/de/thm/arsnova/services/SessionService.java
+++ b/src/main/java/de/thm/arsnova/services/SessionService.java
@@ -48,6 +48,7 @@ import de.thm.arsnova.entities.transport.ImportExportSession;
 import de.thm.arsnova.entities.transport.LearningProgressValues;
 import de.thm.arsnova.events.DeleteSessionEvent;
 import de.thm.arsnova.events.FeatureChangeEvent;
+import de.thm.arsnova.events.LockFeedbackEvent;
 import de.thm.arsnova.events.NewSessionEvent;
 import de.thm.arsnova.events.StatusSessionEvent;
 import de.thm.arsnova.exceptions.BadRequestException;
@@ -104,6 +105,9 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
 	@Autowired
 	private IUserService userService;
 
+	@Autowired
+	private IFeedbackService feedbackService;
+
 	@Autowired
 	private ILearningProgressFactory learningProgressFactory;
 
@@ -316,7 +320,8 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
 		existingSession.setPpLevel(session.getPpLevel());
 		existingSession.setPpLicense(session.getPpLicense());
 		existingSession.setPpSubject(session.getPpSubject());
-		
+		existingSession.setFeedbackLock(session.getFeedbackLock());
+
 		handleLogo(session);
 		existingSession.setPpLogo(session.getPpLogo());
 
@@ -421,7 +426,23 @@ public class SessionService implements ISessionService, ApplicationEventPublishe
 		this.publisher.publishEvent(new FeatureChangeEvent(this, session));
 		return databaseDao.updateSession(session).getFeatures();
 	}
-	
+
+	@Override
+	public boolean lockFeedbackInput(String sessionkey, Boolean lock) {
+		final Session session = databaseDao.getSessionFromKeyword(sessionkey);
+		final User user = userService.getCurrentUser();
+		if (!session.isCreator(user)) {
+			throw new UnauthorizedException();
+		}
+		if (!lock) {
+			feedbackService.cleanFeedbackVotesInSession(sessionkey, 0);
+		}
+
+		session.setFeedbackLock(lock);
+		this.publisher.publishEvent(new LockFeedbackEvent(this, session));
+		return databaseDao.updateSession(session).getFeedbackLock();
+	}
+
 	/**
 	 * 
 	 * @param session 
-- 
GitLab