From c7d3bb0f76ae4c0efc27293238f360e3e0537aaf Mon Sep 17 00:00:00 2001
From: srosse <stephane.rosse@frentix.com>
Date: Tue, 12 Mar 2019 08:54:32 +0100
Subject: [PATCH] OO-3955: add filter for curriculum elements in participant
 folder

---
 .../course/nodes/pf/manager/PFManager.java    | 13 ++++-
 .../course/nodes/pf/ui/PFCoachController.java | 53 +++++++++++++------
 .../manager/BusinessGroupRelationDAO.java     |  4 +-
 .../manager/CurriculumElementDAO.java         | 21 ++++++++
 4 files changed, 70 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/olat/course/nodes/pf/manager/PFManager.java b/src/main/java/org/olat/course/nodes/pf/manager/PFManager.java
index 89660f5f5f4..8274c7f56fe 100644
--- a/src/main/java/org/olat/course/nodes/pf/manager/PFManager.java
+++ b/src/main/java/org/olat/course/nodes/pf/manager/PFManager.java
@@ -64,6 +64,8 @@ import org.olat.course.run.environment.CourseEnvironment;
 import org.olat.course.run.userview.UserCourseEnvironment;
 import org.olat.group.BusinessGroupRef;
 import org.olat.group.manager.BusinessGroupRelationDAO;
+import org.olat.modules.curriculum.CurriculumElementRef;
+import org.olat.modules.curriculum.manager.CurriculumElementDAO;
 import org.olat.repository.RepositoryEntry;
 import org.olat.repository.RepositoryEntryRelationType;
 import org.olat.repository.manager.RepositoryEntryRelationDAO;
@@ -93,6 +95,8 @@ public class PFManager {
 	@Autowired
 	private UserManager userManager;
 	@Autowired
+	private CurriculumElementDAO curriculumElementDao;
+	@Autowired
 	private BusinessGroupRelationDAO businessGroupRelationDao;
 	@Autowired
 	private RepositoryEntryRelationDAO repositoryEntryRelationDao;
@@ -556,10 +560,15 @@ public class PFManager {
 		return getParticipants(pfNode, userPropertyHandlers, locale, identityList, courseEnv);
 	}
 	
-	public List<DropBoxRow> getParticipants(List<BusinessGroupRef> businessGroupRefs, PFCourseNode pfNode, List<UserPropertyHandler> userPropertyHandlers, 
+	public List<DropBoxRow> getParticipants(List<BusinessGroupRef> businessGroupRefs, List<CurriculumElementRef> curriculumElements,
+			PFCourseNode pfNode, List<UserPropertyHandler> userPropertyHandlers, 
 			Locale locale, CourseEnvironment courseEnv) {
+		List<Identity> allIdentities = new ArrayList<>(32);
 		List<Identity> identityList = businessGroupRelationDao.getMembers(businessGroupRefs, GroupRoles.participant.name());
-		return getParticipants(pfNode, userPropertyHandlers, locale, identityList, courseEnv);
+		allIdentities.addAll(identityList);
+		List<Identity> identityElementList = curriculumElementDao.getMembers(curriculumElements, GroupRoles.participant.name());
+		allIdentities.addAll(identityElementList);
+		return getParticipants(pfNode, userPropertyHandlers, locale, allIdentities, courseEnv);
 	}
 	
 	private List<DropBoxRow> getParticipants(PFCourseNode pfNode, List<UserPropertyHandler> userPropertyHandlers, 
diff --git a/src/main/java/org/olat/course/nodes/pf/ui/PFCoachController.java b/src/main/java/org/olat/course/nodes/pf/ui/PFCoachController.java
index 24313b07342..541e46dc32e 100644
--- a/src/main/java/org/olat/course/nodes/pf/ui/PFCoachController.java
+++ b/src/main/java/org/olat/course/nodes/pf/ui/PFCoachController.java
@@ -68,10 +68,12 @@ import org.olat.course.nodes.pf.manager.PFView;
 import org.olat.course.nodes.pf.ui.DropBoxTableModel.DropBoxCols;
 import org.olat.course.run.environment.CourseEnvironment;
 import org.olat.course.run.userview.UserCourseEnvironment;
-import org.olat.course.run.userview.UserCourseEnvironmentImpl;
 import org.olat.group.BusinessGroup;
 import org.olat.group.BusinessGroupRef;
 import org.olat.group.model.BusinessGroupRefImpl;
+import org.olat.modules.curriculum.CurriculumElement;
+import org.olat.modules.curriculum.CurriculumElementRef;
+import org.olat.modules.curriculum.model.CurriculumElementRefImpl;
 import org.olat.resource.OLATResource;
 import org.olat.user.HomePageConfig;
 import org.olat.user.HomePageDisplayController;
@@ -89,6 +91,8 @@ public class PFCoachController extends FormBasicController implements Controller
 	protected static final String USER_PROPS_ID = PFCoachController.class.getCanonicalName();
 
 	protected static final int USER_PROPS_OFFSET = 500;
+	private static final String CURRICULUM_EL_PREFIX = "curriculumelement-";
+	private static final String BUSINESS_GROUP_PREFIX = "businessgroup-";
 	
 	private PFCourseNode pfNode;
 	
@@ -293,26 +297,35 @@ public class PFCoachController extends FormBasicController implements Controller
 	}
 	
 	private void initFilters() {
-		List<FlexiTableFilter> groupFilters = new ArrayList<>();
+		List<FlexiTableFilter> filters = new ArrayList<>();
+		
+		List<BusinessGroup> coachedGroups = userCourseEnv.isAdmin()
+				? courseEnv.getCourseGroupManager().getAllBusinessGroups() : userCourseEnv.getCoachedGroups(); 
+		List<CurriculumElement> coachedElements = userCourseEnv.isAdmin()
+				? courseEnv.getCourseGroupManager().getAllCurriculumElements() : userCourseEnv.getCoachedCurriculumElements();
 		
-		List<BusinessGroup> coachedGroups = null;
-		if(userCourseEnv.isAdmin()) {
-			coachedGroups = courseEnv.getCourseGroupManager().getAllBusinessGroups();
-		} else if(userCourseEnv instanceof UserCourseEnvironmentImpl) {
-			UserCourseEnvironmentImpl uce = (UserCourseEnvironmentImpl)userCourseEnv;
-			coachedGroups = uce.getCoachedGroups(); 
-		}
 		if(coachedGroups != null) {
 			for(BusinessGroup coachedGroup:coachedGroups) {
 				String groupName = StringHelper.escapeHtml(coachedGroup.getName());
-				groupFilters.add(new FlexiTableFilter(groupName, coachedGroup.getKey().toString(), "o_icon o_icon_group"));
+				filters.add(new FlexiTableFilter(groupName, BUSINESS_GROUP_PREFIX.concat(coachedGroup.getKey().toString()), "o_icon o_icon_group"));
+			}
+		}
+		
+		if(!coachedElements.isEmpty()) {
+			if(!filters.isEmpty()) {
+				filters.add(FlexiTableFilter.SPACER);
+			}
+
+			for(CurriculumElement coachedElement: coachedElements) {
+				String groupName = StringHelper.escapeHtml(coachedElement.getDisplayName());
+				filters.add(new FlexiTableFilter(groupName, CURRICULUM_EL_PREFIX.concat(coachedElement.getKey().toString()), "o_icon o_icon_curriculum_element"));
 			}
 		}
 		
-		if(!groupFilters.isEmpty()) {
-			groupFilters.add(FlexiTableFilter.SPACER);
-			groupFilters.add(new FlexiTableFilter(translate("show.all"), "", true));
-			dropboxTable.setExtendedFilterButton(translate("filter.groups"), groupFilters);
+		if(!filters.isEmpty()) {
+			filters.add(FlexiTableFilter.SPACER);
+			filters.add(new FlexiTableFilter(translate("show.all"), "", true));
+			dropboxTable.setExtendedFilterButton(translate("filter.groups"), filters);
 		}
 	}
 	
@@ -323,12 +336,18 @@ public class PFCoachController extends FormBasicController implements Controller
 			rows = pfManager.getParticipants(getIdentity(), pfNode, userPropertyHandlers, getLocale(), courseEnv, userCourseEnv.isAdmin());
 		} else {
 			List<BusinessGroupRef> businessGroups = new ArrayList<>(extendedFilters.size());
+			List<CurriculumElementRef> curriculumElements = new ArrayList<>(extendedFilters.size());
 			for(FlexiTableFilter extendedFilter:extendedFilters) {
-				if(StringHelper.isLong(extendedFilter.getFilter())) {
-					businessGroups.add(new BusinessGroupRefImpl(Long.parseLong(extendedFilter.getFilter())));
+				String filter = extendedFilter.getFilter();
+				if(filter.startsWith(BUSINESS_GROUP_PREFIX)) {
+					String key = filter.substring(BUSINESS_GROUP_PREFIX.length(), filter.length());
+					businessGroups.add(new BusinessGroupRefImpl(Long.valueOf(key)));
+				} else if(filter.startsWith(CURRICULUM_EL_PREFIX)) {
+					String key = filter.substring(CURRICULUM_EL_PREFIX.length(), filter.length());
+					curriculumElements.add(new CurriculumElementRefImpl(Long.valueOf(key)));
 				}
 			}
-			rows = pfManager.getParticipants(businessGroups, pfNode, userPropertyHandlers, getLocale(), courseEnv);
+			rows = pfManager.getParticipants(businessGroups, curriculumElements, pfNode, userPropertyHandlers, getLocale(), courseEnv);
 		}
 		
 		tableModel.setObjects(rows);
diff --git a/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java b/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java
index 5dd9da1d6a9..9de84a54430 100644
--- a/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java
+++ b/src/main/java/org/olat/group/manager/BusinessGroupRelationDAO.java
@@ -321,7 +321,7 @@ public class BusinessGroupRelationDAO {
 	}
 	
 	public List<Identity> getMembers(List<? extends BusinessGroupRef> groups, String... roles) {
-		if(groups == null || groups.isEmpty()) return Collections.emptyList();
+		if(groups == null || groups.isEmpty()) return new ArrayList<>();
 		
 		StringBuilder sb = new StringBuilder();
 		sb.append("select ident from businessgroup as bgroup ")
@@ -345,7 +345,7 @@ public class BusinessGroupRelationDAO {
 	}
 	
 	public List<Long> getMemberKeys(List<? extends BusinessGroupRef> groups, String... roles) {
-		if(groups == null || groups.isEmpty()) return Collections.emptyList();
+		if(groups == null || groups.isEmpty()) return new ArrayList<>();
 		
 		StringBuilder sb = new StringBuilder();
 		sb.append("select membership.identity.key from businessgroup as bgroup ")
diff --git a/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java b/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java
index e55306664b0..d881917928d 100644
--- a/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java
+++ b/src/main/java/org/olat/modules/curriculum/manager/CurriculumElementDAO.java
@@ -765,6 +765,27 @@ public class CurriculumElementDAO {
 				.getResultList();
 	}
 	
+	public List<Identity> getMembers(List<CurriculumElementRef> elements, String... roles) {
+		if(elements == null || elements.isEmpty()) return new ArrayList<>();
+		
+		List<Long> elementKeys = elements.stream()
+				.map(CurriculumElementRef::getKey).collect(Collectors.toList());
+		List<String> roleList = CurriculumRoles.toList(roles);
+		
+		StringBuilder sb = new StringBuilder(256);
+		sb.append("select ident from curriculumelement el")
+		  .append(" inner join el.group baseGroup")
+		  .append(" inner join baseGroup.members membership")
+		  .append(" inner join membership.identity ident")
+		  .append(" inner join fetch ident.user identUser")
+		  .append(" where el.key in (:elementKeys) and membership.role in (:roles)");
+		return dbInstance.getCurrentEntityManager()
+				.createQuery(sb.toString(), Identity.class)
+				.setParameter("elementKeys", elementKeys)
+				.setParameter("roles", roleList)
+				.getResultList();
+	}
+	
 	public List<CurriculumElementMembership> getMembershipInfos(List<CurriculumRef> curriculums, Collection<CurriculumElement> elements, Identity... identities) {
 		StringBuilder sb = new StringBuilder(256);
 		sb.append("select el.key, membership from curriculumelement el")
-- 
GitLab