From ff64c012ae59fa6ecb6477dab5ac95a4ac15aa15 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Thu, 26 Oct 2017 15:43:38 +0200
Subject: [PATCH] OO-3096: field reason mandatory for roll call (if any)

---
 .../CancelRollCallConfirmationController.java |  2 +
 .../CloseRollCallConfirmationController.java  | 79 +++++++++----------
 .../modules/lecture/ui/ReasonComparator.java  | 45 +++++++++++
 3 files changed, 83 insertions(+), 43 deletions(-)
 create mode 100644 src/main/java/org/olat/modules/lecture/ui/ReasonComparator.java

diff --git a/src/main/java/org/olat/modules/lecture/ui/CancelRollCallConfirmationController.java b/src/main/java/org/olat/modules/lecture/ui/CancelRollCallConfirmationController.java
index 3eb1fd0fe93..e03b26ecb54 100644
--- a/src/main/java/org/olat/modules/lecture/ui/CancelRollCallConfirmationController.java
+++ b/src/main/java/org/olat/modules/lecture/ui/CancelRollCallConfirmationController.java
@@ -20,6 +20,7 @@
 package org.olat.modules.lecture.ui;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.olat.core.gui.UserRequest;
@@ -77,6 +78,7 @@ public class CancelRollCallConfirmationController extends FormBasicController {
 		List<String> reasonValueList = new ArrayList<>();
 		
 		List<Reason> allReasons = lectureService.getAllReasons();
+		Collections.sort(allReasons, new ReasonComparator());
 		for(Reason reason:allReasons) {
 			reasonKeyList.add(reason.getKey().toString());
 			reasonValueList.add(reason.getTitle());
diff --git a/src/main/java/org/olat/modules/lecture/ui/CloseRollCallConfirmationController.java b/src/main/java/org/olat/modules/lecture/ui/CloseRollCallConfirmationController.java
index c4f8532de0c..53e7e7f4812 100644
--- a/src/main/java/org/olat/modules/lecture/ui/CloseRollCallConfirmationController.java
+++ b/src/main/java/org/olat/modules/lecture/ui/CloseRollCallConfirmationController.java
@@ -50,6 +50,8 @@ import org.olat.modules.lecture.Reason;
 import org.olat.modules.lecture.RollCallSecurityCallback;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import edu.emory.mathcs.backport.java.util.Collections;
+
 /**
  * 
  * Initial date: 12 juin 2017<br>
@@ -138,33 +140,35 @@ public class CloseRollCallConfirmationController extends FormBasicController {
 		}
 		effectiveEndMinuteEl.setValue(minuteStr);
 
-		List<String> reasonKeyList = new ArrayList<>();
-		List<String> reasonValueList = new ArrayList<>();
-		reasonKeyList.add("-");
-		reasonValueList.add("-");
-		
 		List<Reason> allReasons = lectureService.getAllReasons();
-		for(Reason reason:allReasons) {
-			reasonKeyList.add(reason.getKey().toString());
-			reasonValueList.add(reason.getTitle());
-		}
-		effectiveEndReasonEl = uifactory.addDropdownSingleselect("effective.reason", "lecture.block.effective.reason", formLayout,
-				reasonKeyList.toArray(new String[reasonKeyList.size()]), reasonValueList.toArray(new String[reasonValueList.size()]), null);
-		effectiveEndReasonEl.setEnabled(secCallback.canEdit());
-		boolean found = false;
-		if(lectureBlock.getReasonEffectiveEnd() != null) {
-			String selectedReasonKey = lectureBlock.getReasonEffectiveEnd().getKey().toString();
-			for(String reasonKey:reasonKeyList) {
-				if(reasonKey.equals(selectedReasonKey)) {
-					effectiveEndReasonEl.select(reasonKey, true);
-					found = true;
-					break;
+		if(allReasons.size() > 0) {
+			if(allReasons.size() > 2) {
+				Collections.sort(allReasons, new ReasonComparator());
+			}
+			
+			int numOfReasons = allReasons.size();
+			List<String> reasonKeys = new ArrayList<String>(numOfReasons + 1);
+			List<String> reasonValues = new ArrayList<String>(numOfReasons + 1);
+			reasonKeys.add("-");
+			reasonValues.add("");
+			for(int i=numOfReasons; i-->0; ) {
+				Reason reason = allReasons.get(i);
+				reasonKeys.add(reason.getKey().toString());
+				reasonValues.add(reason.getTitle());
+			}
+			effectiveEndReasonEl = uifactory.addDropdownSingleselect("effective.reason", "lecture.block.effective.reason", formLayout,
+					reasonKeys.toArray(new String[reasonKeys.size()]), reasonValues.toArray(new String[reasonValues.size()]), null);
+			effectiveEndReasonEl.setEnabled(secCallback.canEdit());
+			if(lectureBlock.getReasonEffectiveEnd() != null) {
+				String selectedReasonKey = lectureBlock.getReasonEffectiveEnd().getKey().toString();
+				for(String reasonKey:reasonKeys) {
+					if(reasonKey.equals(selectedReasonKey)) {
+						effectiveEndReasonEl.select(reasonKey, true);
+						break;
+					}
 				}
 			}
 		}
-		if(!found) {
-			effectiveEndReasonEl.select(reasonKeyList.get(0), true);
-		}
 		
 		String blockComment = lectureBlock.getComment();
 		blockCommentEl = uifactory.addTextAreaElement("lecture.block.comment", 4, 72, blockComment, formLayout);
@@ -210,27 +214,24 @@ public class CloseRollCallConfirmationController extends FormBasicController {
 		boolean allOk = true;
 		
 		effectiveEndHourEl.clearError();
-		effectiveEndReasonEl.clearError();
 		//need to be the first validation
 		if(StringHelper.containsNonWhitespace(effectiveEndHourEl.getValue())
 				|| StringHelper.containsNonWhitespace(effectiveEndMinuteEl.getValue())) {
 			allOk &= validateInt(effectiveEndHourEl, 24);
 			allOk &= validateInt(effectiveEndMinuteEl, 60);
-
-			if(!effectiveEndReasonEl.isOneSelected()) {
-				effectiveEndReasonEl.setErrorKey("error.reason.mandatory", null);
-				allOk &= false;
-			} else if(effectiveEndReasonEl.isSelected(0)) {
-				if(getEffectiveEndDate() != null && this.differentEffectiveEndDate()) {
-					effectiveEndReasonEl.setErrorKey("error.reason.mandatory", null);
-					allOk &= false;
-				}
-			}
 		} else {
 			effectiveEndHourEl.setErrorKey("form.legende.mandatory", null);
 			allOk &= false;
 		}
 		
+		if(effectiveEndReasonEl != null) {
+			effectiveEndReasonEl.clearError();
+			if(!effectiveEndReasonEl.isOneSelected() || effectiveEndReasonEl.isSelected(0)) {
+				effectiveEndReasonEl.setErrorKey("error.reason.mandatory", null);
+				allOk &= false;
+			}
+		}
+		
 		if(effectiveLecturesEl != null) {
 			effectiveLecturesEl.clearError();
 			if(!effectiveLecturesEl.isOneSelected()) {
@@ -242,14 +243,6 @@ public class CloseRollCallConfirmationController extends FormBasicController {
 		return allOk & super.validateFormLogic(ureq);
 	}
 	
-	private boolean differentEffectiveEndDate() {
-		Date endDate = lectureBlock.getEndDate();
-		if(endDate == null) return true;
-		Date effectiveEndDate = getEffectiveEndDate();
-		long diff = endDate.getTime() - effectiveEndDate.getTime();
-		return Math.abs(diff) > 60000l;//bigger than a minute
-	}
-	
 	private boolean validateInt(TextElement element, int max) {
 		boolean allOk = true;
 		
@@ -320,7 +313,7 @@ public class CloseRollCallConfirmationController extends FormBasicController {
 			lectureBlock.setReasonEffectiveEnd(null);
 		} else {
 			lectureBlock.setEffectiveEndDate(effectiveEndDate);
-			if("-".equals(effectiveEndReasonEl.getSelectedKey())) {
+			if(effectiveEndReasonEl == null || "-".equals(effectiveEndReasonEl.getSelectedKey())) {
 				lectureBlock.setReasonEffectiveEnd(null);
 			} else {
 				Long reasonKey = new Long(effectiveEndReasonEl.getSelectedKey());
diff --git a/src/main/java/org/olat/modules/lecture/ui/ReasonComparator.java b/src/main/java/org/olat/modules/lecture/ui/ReasonComparator.java
new file mode 100644
index 00000000000..c86f8a5d003
--- /dev/null
+++ b/src/main/java/org/olat/modules/lecture/ui/ReasonComparator.java
@@ -0,0 +1,45 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.modules.lecture.ui;
+
+import java.util.Comparator;
+import java.util.Date;
+
+import org.olat.modules.lecture.Reason;
+
+/**
+ * 
+ * Initial date: 26 oct. 2017<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+public class ReasonComparator implements Comparator<Reason> {
+
+	@Override
+	public int compare(Reason r1, Reason r2) {
+		if(r1 == null && r2 == null) return 0;
+		if(r1 == null) return -1;
+		if(r2 == null) return 1;
+		
+		Date c1 = r1.getCreationDate();
+		Date c2 = r2.getCreationDate();
+		return c2.compareTo(c1);
+	}
+}
-- 
GitLab