From c1f94529398840a73374c533cc7fea7b01e06b15 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 25 Jan 2012 10:39:24 +0100
Subject: [PATCH] OO-93: add all the users which used a test to the list of
 users in assessment tool

---
 .../ScoreAccountingArchiveController.java     |  3 +-
 .../archiver/ScoreAccountingHelper.java       |  5 +-
 .../assessment/AssessmentMainController.java  | 81 +++++--------------
 .../properties/CoursePropertyManager.java     |  4 +-
 .../PersistingCoursePropertyManager.java      | 36 ++++++---
 .../preview/PreviewCoursePropertyManager.java |  3 +-
 .../RepositoryEditPropertiesController.java   |  3 +-
 7 files changed, 58 insertions(+), 77 deletions(-)

diff --git a/src/main/java/org/olat/course/archiver/ScoreAccountingArchiveController.java b/src/main/java/org/olat/course/archiver/ScoreAccountingArchiveController.java
index d0f98a0022a..4d73b384b46 100644
--- a/src/main/java/org/olat/course/archiver/ScoreAccountingArchiveController.java
+++ b/src/main/java/org/olat/course/archiver/ScoreAccountingArchiveController.java
@@ -47,6 +47,7 @@ import org.olat.core.util.ExportUtil;
 import org.olat.core.util.Util;
 import org.olat.course.CourseFactory;
 import org.olat.course.ICourse;
+import org.olat.course.nodes.AssessableCourseNode;
 import org.olat.user.UserManager;
 
 /**
@@ -95,7 +96,7 @@ public class ScoreAccountingArchiveController extends DefaultController {
 		if (source == startButton) {
 			ICourse course = CourseFactory.loadCourse(ores);
 			List<Identity> users = ScoreAccountingHelper.loadUsers(course.getCourseEnvironment());
-			List nodes = ScoreAccountingHelper.loadAssessableNodes(course.getCourseEnvironment());
+			List<AssessableCourseNode> nodes = ScoreAccountingHelper.loadAssessableNodes(course.getCourseEnvironment());
 			
 			String result = ScoreAccountingHelper.createCourseResultsOverviewTable(users, nodes, course, ureq.getLocale());
 
diff --git a/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java b/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
index 7f8119b944d..78cbb7be22d 100644
--- a/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
+++ b/src/main/java/org/olat/course/archiver/ScoreAccountingHelper.java
@@ -404,7 +404,10 @@ public class ScoreAccountingHelper {
 			secGroups.add(re.getParticipantGroup());
 		}
 		
-		return securityManager.getIdentitiesOfSecurityGroups(secGroups);
+		List<Identity> userList = securityManager.getIdentitiesOfSecurityGroups(secGroups);
+		List<Identity> assessedList = courseEnv.getCoursePropertyManager().getAllIdentitiesWithCourseAssessmentData(userList);
+		userList.addAll(assessedList);
+		return userList;
 	}
 	
 	/**
diff --git a/src/main/java/org/olat/course/assessment/AssessmentMainController.java b/src/main/java/org/olat/course/assessment/AssessmentMainController.java
index 60eee9af801..6903e3a8431 100644
--- a/src/main/java/org/olat/course/assessment/AssessmentMainController.java
+++ b/src/main/java/org/olat/course/assessment/AssessmentMainController.java
@@ -29,11 +29,9 @@ import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 
 import org.apache.commons.lang.StringEscapeUtils;
 import org.olat.admin.user.UserTableDataModel;
@@ -290,7 +288,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 		if(focusOnIdentity != null) {
 			//fill the user list for the 
 			this.mode = MODE_USERFOCUS;
-			this.identitiesList = getAllIdentitisFromGroupmanagement();
+			this.identitiesList = getAllAssessableIdentities();
 			//fxdiff FXOLAT-108: improve results table of tests
 			doUserChooseWithData(ureq, identitiesList, null, null);
 			
@@ -331,7 +329,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 					main.setContent(index);
 				} else if (cmd.equals(CMD_USERFOCUS)) {
 					this.mode = MODE_USERFOCUS;
-					this.identitiesList = getAllIdentitisFromGroupmanagement();
+					this.identitiesList = getAllAssessableIdentities();
 					//fxdiff FXOLAT-108: improve results table of tests
 					doUserChooseWithData(ureq, identitiesList, null, null);
 				} else if (cmd.equals(CMD_GROUPFOCUS)) {
@@ -346,7 +344,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 				}
 			}
 		} else if (source == allUsersButton){	
-			this.identitiesList = getAllIdentitisFromGroupmanagement();
+			this.identitiesList = getAllAssessableIdentities();
 			// Init the user list with this identitites list
 			this.currentGroup = null;
 			doUserChooseWithData(ureq, this.identitiesList, null, this.currentCourseNode);
@@ -469,7 +467,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 					this.currentCourseNode = (AssessableCourseNode) node;
 					// cast should be save, only assessable nodes are selectable
 					if((repoTutor && coachedGroups.isEmpty()) || (callback.mayAssessAllUsers() || callback.mayViewAllUsersAssessments())) {
-						identitiesList = getAllIdentitisFromGroupmanagement();
+						identitiesList = getAllAssessableIdentities();
 						doUserChooseWithData(ureq, this.identitiesList, null, currentCourseNode);
 					} else {
 						doGroupChoose(ureq);
@@ -601,69 +599,32 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 	}
 
 	/**
-	 * @return List of all course participants
+	 * Load the identities which are participants of a group attached to the course,
+	 * participants of the course as members and all users which have make the tests.
+	 * @return List of identities
 	 */
-	/*List<Identity> getAllIdentitisFromGroupmanagement() {
-		List<Identity> allUsersList = new ArrayList<Identity>();
-		BaseSecurity secMgr = BaseSecurityManager.getInstance();
-		Iterator<BusinessGroup> iter = this.coachedGroups.iterator();
-		List<SecurityGroup> secGroups = new ArrayList<SecurityGroup>();
-		while (iter.hasNext()) {
-			BusinessGroup group = iter.next();
-			SecurityGroup secGroup = group.getPartipiciantGroup();
-			secGroups.add(secGroup);
-			List<Identity> identities = secMgr.getIdentitiesOfSecurityGroup(secGroup);
-			for (Iterator<Identity> identitiyIter = identities.iterator(); identitiyIter.hasNext();) {
-				Identity identity = identitiyIter.next();
-				if (!PersistenceHelper.listContainsObjectByKey(allUsersList, identity)) {
-					// only add if not already in list
-					allUsersList.add(identity);
-				}
-			}
-		}
-		
-		List<Long> idKeys = secMgr.getIdentitiesOfSecurityGroups(secGroups);
-		System.out.println();
-		
-		//fxdiff VCRP-1,2: access control of resources
-		if((repoTutor && coachedGroups.isEmpty()) || (callback.mayAssessAllUsers() || callback.mayViewAllUsersAssessments())) {
-			RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(ores, false);
-			if(re.getParticipantGroup() != null) {
-				for (Identity identity : secMgr.getIdentitiesOfSecurityGroup(re.getParticipantGroup())) {
-					if (!PersistenceHelper.listContainsObjectByKey(allUsersList, identity)) {
-						allUsersList.add(identity);
-					}
-				}
-			}
-		}
-		
-		return allUsersList;
-	}*/
-	
-	List<Identity> getAllIdentitisFromGroupmanagement() {
+	private List<Identity> getAllAssessableIdentities() {
 		List<SecurityGroup> secGroups = new ArrayList<SecurityGroup>();
 		for (BusinessGroup group: coachedGroups) {
 			secGroups.add(group.getPartipiciantGroup());
 		}
-		
-		BaseSecurity secMgr = BaseSecurityManager.getInstance();
-		List<Identity> usersList = secMgr.getIdentitiesOfSecurityGroups(secGroups);
-		Set<Identity> smashDuplicates = new HashSet<Identity>(usersList);
-		List<Identity> allUsersList = new ArrayList<Identity>(usersList);
-		
+
 		//fxdiff VCRP-1,2: access control of resources
 		if((repoTutor && coachedGroups.isEmpty()) || (callback.mayAssessAllUsers() || callback.mayViewAllUsersAssessments())) {
 			RepositoryEntry re = RepositoryManager.getInstance().lookupRepositoryEntry(ores, false);
 			if(re.getParticipantGroup() != null) {
-				for (Identity identity : secMgr.getIdentitiesOfSecurityGroup(re.getParticipantGroup())) {
-					if (!smashDuplicates.contains(identity)) {
-						allUsersList.add(identity);
-					}
-				}
+				secGroups.add(re.getParticipantGroup());
 			}
 		}
 
-		return allUsersList;
+		BaseSecurity secMgr = BaseSecurityManager.getInstance();
+		List<Identity> usersList = secMgr.getIdentitiesOfSecurityGroups(secGroups);
+
+		ICourse course = CourseFactory.loadCourse(ores);
+		CoursePropertyManager pm = course.getCourseEnvironment().getCoursePropertyManager();
+		List<Identity> assessedRsers = pm.getAllIdentitiesWithCourseAssessmentData(usersList);
+		usersList.addAll(assessedRsers);
+		return usersList;
 	}
 
 	/**
@@ -888,7 +849,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 	
 	private void doBulkChoose(UserRequest ureq) {
 		ICourse course = CourseFactory.loadCourse(ores);
-		List<Identity> allowedIdentities = getAllIdentitisFromGroupmanagement();
+		List<Identity> allowedIdentities = getAllAssessableIdentities();
 		removeAsListenerAndDispose(bamc);
 		bamc = new BulkAssessmentMainController(ureq, getWindowControl(), course, allowedIdentities);
 		listenTo(bamc);
@@ -1178,7 +1139,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 			course.getCourseEnvironment().getAssessmentManager().preloadCache();
 			// 2) preload controller local user environment cache
 			start = System.currentTimeMillis();
-			List<Identity> identities = getAllIdentitisFromGroupmanagement();
+			List<Identity> identities = getAllAssessableIdentities();
 			
 			CourseNode node = course.getCourseEnvironment().getRunStructure().getRootNode();
 			CoursePropertyManager pm = course.getCourseEnvironment().getCoursePropertyManager();
@@ -1250,7 +1211,7 @@ AssessmentMainController(UserRequest ureq, WindowControl wControl, OLATResourcea
 				}
 				
 				mode = MODE_USERFOCUS;
-				identitiesList = getAllIdentitisFromGroupmanagement();
+				identitiesList = getAllAssessableIdentities();
 				doUserChooseWithData(ureq, identitiesList, null, currentCourseNode);
 				menuTree.setSelectedNode(userNode);
 
diff --git a/src/main/java/org/olat/course/properties/CoursePropertyManager.java b/src/main/java/org/olat/course/properties/CoursePropertyManager.java
index a4f3eaa28fa..8b1d9fd19e3 100644
--- a/src/main/java/org/olat/course/properties/CoursePropertyManager.java
+++ b/src/main/java/org/olat/course/properties/CoursePropertyManager.java
@@ -25,6 +25,7 @@
 
 package org.olat.course.properties;
 
+import java.util.Collection;
 import java.util.List;
 
 import org.olat.core.id.Identity;
@@ -130,7 +131,8 @@ public interface CoursePropertyManager extends IdentityAnonymizerCallback {
 
 	/**
 	 * @return a list of all identities that have generated any assessment properties within this courses
+	 * @param excludeIdentities Exclude a list of identities
 	 */
-	public List getAllIdentitiesWithCourseAssessmentData();
+	public List<Identity> getAllIdentitiesWithCourseAssessmentData(Collection<Identity> excludeIdentities);
 
 }
diff --git a/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java b/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
index d2cda015cc9..0291d74a037 100644
--- a/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
+++ b/src/main/java/org/olat/course/properties/PersistingCoursePropertyManager.java
@@ -25,11 +25,14 @@
 
 package org.olat.course.properties;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
 
+import org.olat.basesecurity.IdentityImpl;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.commons.persistence.DBFactory;
 import org.olat.core.commons.persistence.DBQuery;
@@ -198,25 +201,34 @@ public class PersistingCoursePropertyManager extends BasicManager implements Cou
 	/**
 	 * @see org.olat.course.properties.CoursePropertyManager#getAllIdentitiesWithCourseAssessmentData()
 	 */
-	public List getAllIdentitiesWithCourseAssessmentData() {
-		StringBuffer query = new StringBuffer();
-		query.append("select distinct i from ");
-		query.append(" org.olat.basesecurity.IdentityImpl as i,");
-		query.append(" org.olat.properties.Property as p");
-		query.append(" where i = p.identity and p.resourceTypeName = :resname");
-		query.append(" and p.resourceTypeId = :resid");
-		query.append(" and p.identity is not null");
-		query.append(" and ( p.name = '").append(AssessmentManager.SCORE);
-		query.append("' or p.name = '").append(AssessmentManager.PASSED);
-		query.append("' )");
+	public List<Identity> getAllIdentitiesWithCourseAssessmentData(Collection<Identity> excludeIdentities) {
+		StringBuilder query = new StringBuilder();
+		query.append("select distinct i from ")
+			.append(IdentityImpl.class.getName()).append(" as i,")
+			.append(Property.class.getName()).append(" as p")
+			.append(" where i = p.identity and p.resourceTypeName = :resname")
+			.append(" and p.resourceTypeId = :resid")
+			.append(" and p.identity is not null")
+			.append(" and p.name in ('").append(AssessmentManager.SCORE).append("','").append(AssessmentManager.PASSED).append("')");
+		
+		if(excludeIdentities != null && !excludeIdentities.isEmpty()) {
+			query.append(" and p.identity.key not in (:excludeIdentities) ");
+		}
 
 		DB db = DBFactory.getInstance();
 		DBQuery dbq = db.createQuery(query.toString());
 		ICourse course = CourseFactory.loadCourse(ores);
 		dbq.setLong("resid", course.getResourceableId().longValue());
 		dbq.setString("resname", course.getResourceableTypeName());
+		if(excludeIdentities != null && !excludeIdentities.isEmpty()) {
+			List<Long> excludeKeys = new ArrayList<Long>();
+			for(Identity identity:excludeIdentities) {
+				excludeKeys.add(identity.getKey());
+			}
+			dbq.setParameterList("excludeIdentities", excludeKeys);
+		}
 
-		List res = dbq.list();
+		List<Identity> res = dbq.list();
 		return res;
 	}
 
diff --git a/src/main/java/org/olat/course/run/preview/PreviewCoursePropertyManager.java b/src/main/java/org/olat/course/run/preview/PreviewCoursePropertyManager.java
index 1f5a7ef4ec0..f00f5e7cd97 100644
--- a/src/main/java/org/olat/course/run/preview/PreviewCoursePropertyManager.java
+++ b/src/main/java/org/olat/course/run/preview/PreviewCoursePropertyManager.java
@@ -26,6 +26,7 @@
 package org.olat.course.run.preview;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -204,7 +205,7 @@ final class PreviewCoursePropertyManager extends BasicManager implements CourseP
 	/**
 	 * @see org.olat.course.properties.CoursePropertyManager#getAllIdentitiesWithCourseAssessmentData()
 	 */
-	public List getAllIdentitiesWithCourseAssessmentData() {
+	public List<Identity> getAllIdentitiesWithCourseAssessmentData(Collection<Identity> excludeIdentities) {
 		throw new AssertException("Not implemented for preview.");
 	}
 }
diff --git a/src/main/java/org/olat/repository/controllers/RepositoryEditPropertiesController.java b/src/main/java/org/olat/repository/controllers/RepositoryEditPropertiesController.java
index bb22fb9ed30..77212649915 100644
--- a/src/main/java/org/olat/repository/controllers/RepositoryEditPropertiesController.java
+++ b/src/main/java/org/olat/repository/controllers/RepositoryEditPropertiesController.java
@@ -43,6 +43,7 @@ import org.olat.core.gui.control.WindowControl;
 import org.olat.core.gui.control.controller.BasicController;
 import org.olat.core.gui.control.generic.modal.DialogBoxController;
 import org.olat.core.gui.control.generic.modal.DialogBoxUIFactory;
+import org.olat.core.id.Identity;
 import org.olat.core.id.OLATResourceable;
 import org.olat.core.logging.OLog;
 import org.olat.core.logging.Tracing;
@@ -345,7 +346,7 @@ public class RepositoryEditPropertiesController extends BasicController {
 				if((changedCourseConfig.isEfficencyStatementEnabled() != initialCourseConfig.isEfficencyStatementEnabled() && ceffC.getLoggingAction()!=null)) {
 					if(changedCourseConfig.isEfficencyStatementEnabled()) {
             // first create the efficiencies, send event to agency (all courses add link)
-						List identitiesWithData = course.getCourseEnvironment().getCoursePropertyManager().getAllIdentitiesWithCourseAssessmentData();
+						List<Identity> identitiesWithData = course.getCourseEnvironment().getCoursePropertyManager().getAllIdentitiesWithCourseAssessmentData(null);
 						EfficiencyStatementManager.getInstance().updateEfficiencyStatements(course, identitiesWithData, false);							
 					} else {
             // delete really the efficiencies of the users.
-- 
GitLab