From bbfcddf1ee068f16e23724c4772940f3feffe349 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Fri, 24 Oct 2014 10:02:09 +0200
Subject: [PATCH] OO-1276: retrieve assessment data of the participants of the
 repository entry

---
 .../olat/course/nodes/cl/CheckboxManager.java |  11 ++
 .../nodes/cl/manager/CheckboxManagerImpl.java |  36 +++++-
 .../nodes/cl/manager/CheckboxManagerTest.java | 110 ++++++++++++++++++
 3 files changed, 151 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/olat/course/nodes/cl/CheckboxManager.java b/src/main/java/org/olat/course/nodes/cl/CheckboxManager.java
index a88dd3d809e..f968ac3b7bc 100644
--- a/src/main/java/org/olat/course/nodes/cl/CheckboxManager.java
+++ b/src/main/java/org/olat/course/nodes/cl/CheckboxManager.java
@@ -83,6 +83,17 @@ public interface CheckboxManager {
 	
 	public float calculateScore(Identity identity, OLATResourceable ores, String resSubPath);
 	
+	/**
+	 * Return the assessment data of the participants of the repository entry and or the business groups
+	 * specified. If the repository entry and the business groups are omitted, all the assessment data
+	 * are returned.
+	 * 
+	 * @param ores
+	 * @param resSubPath
+	 * @param re
+	 * @param groups
+	 * @return
+	 */
 	public List<AssessmentData> getAssessmentDatas(OLATResourceable ores, String resSubPath, RepositoryEntry re, List<BusinessGroup> groups);
 	
 	public VFSContainer getFileContainer(CourseEnvironment courseEnv, CheckListCourseNode cNode, Checkbox checkbox);
diff --git a/src/main/java/org/olat/course/nodes/cl/manager/CheckboxManagerImpl.java b/src/main/java/org/olat/course/nodes/cl/manager/CheckboxManagerImpl.java
index e5d4ca8342a..956335a8f3d 100644
--- a/src/main/java/org/olat/course/nodes/cl/manager/CheckboxManagerImpl.java
+++ b/src/main/java/org/olat/course/nodes/cl/manager/CheckboxManagerImpl.java
@@ -36,7 +36,6 @@ import javax.persistence.TypedQuery;
 
 import org.olat.basesecurity.Group;
 import org.olat.basesecurity.IdentityImpl;
-import org.olat.basesecurity.model.GroupMembershipImpl;
 import org.olat.core.commons.modules.bc.FolderConfig;
 import org.olat.core.commons.modules.bc.vfs.OlatRootFolderImpl;
 import org.olat.core.commons.persistence.DB;
@@ -450,12 +449,34 @@ public class CheckboxManagerImpl implements CheckboxManager {
 		if(StringHelper.containsNonWhitespace(resSubPath)) {
 			sb.append(" and box.resSubPath=:resSubPath");
 		}
-		if(businessGroups != null && businessGroups.size() > 0) {
-			sb.append(" and check.identity.key in ( select membership.identity.key from ").append(GroupMembershipImpl.class.getName()).append(" membership ")
+
+		boolean hasBusinessGroups = businessGroups != null && businessGroups.size() > 0;
+		if(hasBusinessGroups) {
+			sb.append(" and ");
+			if(re != null) {
+				sb.append(" ( ");
+			}
+			
+			sb.append(" check.identity.key in ( select membership.identity.key from bgroupmember membership ")
 			  .append("   where membership.group in (:baseGroups) and membership.role='").append(GroupRole.participant).append("'")
 			  .append(" )");
 		}
-		
+		if(re != null) {
+			if(hasBusinessGroups) {
+				sb.append(" or ");
+			} else {
+				sb.append(" and ");
+			}
+			
+			sb.append(" check.identity.key in ( select membership.identity.key from repoentrytogroup as rel, bgroup as reBaseGroup, bgroupmember membership ")
+			  .append("   where rel.entry.key=:repoKey and rel.group=reBaseGroup and membership.group=reBaseGroup and membership.role='").append(GroupRole.participant).append("'")
+			  .append(" )");
+
+			if(hasBusinessGroups) {
+				sb.append(" ) ");
+			} 
+		}
+
 		TypedQuery<DBCheck> query = dbInstance.getCurrentEntityManager()
 				.createQuery(sb.toString(), DBCheck.class)
 				.setParameter("resName", ores.getResourceableTypeName())
@@ -463,14 +484,17 @@ public class CheckboxManagerImpl implements CheckboxManager {
 		if(StringHelper.containsNonWhitespace(resSubPath)) {
 			query.setParameter("resSubPath", resSubPath);
 		}
-		if(businessGroups != null && businessGroups.size() > 0) {
+
+		if(hasBusinessGroups) {
 			List<Group> groups = new ArrayList<>(businessGroups.size());
 			for(BusinessGroup businessGroup:businessGroups) {
 				groups.add(businessGroup.getBaseGroup());
 			}
 			query.setParameter("baseGroups", groups);
 		}
-		//TODO group exists student where i'm coach?
+		if(re != null) {
+			query.setParameter("repoKey", re.getKey());
+		}
 		
 		List<DBCheck> checks = query.getResultList();
 		Map<Long, AssessmentData> identToBox = new HashMap<Long,AssessmentData>();
diff --git a/src/test/java/org/olat/course/nodes/cl/manager/CheckboxManagerTest.java b/src/test/java/org/olat/course/nodes/cl/manager/CheckboxManagerTest.java
index 518d6f4368f..44b7b60af83 100644
--- a/src/test/java/org/olat/course/nodes/cl/manager/CheckboxManagerTest.java
+++ b/src/test/java/org/olat/course/nodes/cl/manager/CheckboxManagerTest.java
@@ -20,12 +20,14 @@
 package org.olat.course.nodes.cl.manager;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 import java.util.UUID;
 
 import junit.framework.Assert;
 
 import org.junit.Test;
+import org.olat.basesecurity.GroupRoles;
 import org.olat.core.commons.persistence.DB;
 import org.olat.core.id.Identity;
 import org.olat.core.id.OLATResourceable;
@@ -35,6 +37,11 @@ import org.olat.course.nodes.cl.model.Checkbox;
 import org.olat.course.nodes.cl.model.CheckboxList;
 import org.olat.course.nodes.cl.model.DBCheck;
 import org.olat.course.nodes.cl.model.DBCheckbox;
+import org.olat.group.BusinessGroup;
+import org.olat.group.manager.BusinessGroupDAO;
+import org.olat.group.manager.BusinessGroupRelationDAO;
+import org.olat.repository.RepositoryEntry;
+import org.olat.repository.manager.RepositoryEntryRelationDAO;
 import org.olat.test.JunitTestHelper;
 import org.olat.test.OlatTestCase;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -50,7 +57,13 @@ public class CheckboxManagerTest extends OlatTestCase {
 	@Autowired
 	private DB dbInstance;
 	@Autowired
+	private BusinessGroupDAO businessGroupDao;
+	@Autowired
 	private CheckboxManagerImpl checkboxManager;
+	@Autowired
+	private BusinessGroupRelationDAO businessGroupRelationDao;
+	@Autowired
+	private RepositoryEntryRelationDAO repositoryEntryRelationDao;
 	
 	@Test
 	public void createCheckBox() {
@@ -291,7 +304,104 @@ public class CheckboxManagerTest extends OlatTestCase {
 		Assert.assertTrue(collectedChecks.contains(check3_1));
 		Assert.assertTrue(collectedChecks.contains(check3_2));
 	}
+
+	@Test
+	public void loadAssessmentDatas_inCourse() {
+		RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
+		Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("check-18");
+		repositoryEntryRelationDao.addRole(id, entry, GroupRoles.participant.name());
+		dbInstance.commitAndCloseSession();
+		
+		String checkboxId = UUID.randomUUID().toString();
+		String resSubPath = UUID.randomUUID().toString();
+		DBCheckbox checkbox = checkboxManager.createDBCheckbox(checkboxId, entry.getOlatResource(), resSubPath);
+		DBCheck check = checkboxManager.createCheck(checkbox, id, null, Boolean.TRUE);
+		dbInstance.commitAndCloseSession();
+		
+		//load and check the check
+		List<AssessmentData> loadedChecks = checkboxManager.getAssessmentDatas(entry.getOlatResource(), resSubPath, entry, null);
+		Assert.assertNotNull(loadedChecks);
+		Assert.assertEquals(1, loadedChecks.size());
+		AssessmentData data = loadedChecks.get(0);
+		Assert.assertNotNull(data);
+		Assert.assertNotNull(data.getChecks());
+		Assert.assertEquals(1, data.getChecks().size());
+		Assert.assertEquals(check, data.getChecks().get(0));
+		Assert.assertEquals(id, data.getIdentity());
+	}
+	
+	@Test
+	public void loadAssessmentDatas_inGroup() {
+		RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
+		Identity id = JunitTestHelper.createAndPersistIdentityAsRndUser("check-19");
+		BusinessGroup group = businessGroupDao.createAndPersist(null, "gcheck", "gcheck-desc", 0, 10, true, true, false, false, false);
+		businessGroupRelationDao.addRole(id, group, GroupRoles.participant.name());
+		dbInstance.commitAndCloseSession();
+		
+		String checkboxId = UUID.randomUUID().toString();
+		String resSubPath = UUID.randomUUID().toString();
+		DBCheckbox checkbox = checkboxManager.createDBCheckbox(checkboxId, entry.getOlatResource(), resSubPath);
+		DBCheck check = checkboxManager.createCheck(checkbox, id, null, Boolean.TRUE);
+		dbInstance.commitAndCloseSession();
+		
+		List<BusinessGroup> groups = Collections.singletonList(group);
+		List<AssessmentData> loadedChecks = checkboxManager.getAssessmentDatas(entry.getOlatResource(), resSubPath, null, groups);
+		Assert.assertNotNull(loadedChecks);
+		Assert.assertEquals(1, loadedChecks.size());
+		AssessmentData data = loadedChecks.get(0);
+		Assert.assertNotNull(data);
+		Assert.assertNotNull(data.getChecks());
+		Assert.assertEquals(1, data.getChecks().size());
+		Assert.assertEquals(check, data.getChecks().get(0));
+		Assert.assertEquals(id, data.getIdentity());
+	}
 	
+	@Test
+	public void loadAssessmentDatas_inGroupAndCourse() {
+		RepositoryEntry entry = JunitTestHelper.createAndPersistRepositoryEntry();
+		Identity groupParticipant = JunitTestHelper.createAndPersistIdentityAsRndUser("check-20");
+		BusinessGroup group = businessGroupDao.createAndPersist(null, "gcheck", "gcheck-desc", 0, 10, true, true, false, false, false);
+		businessGroupRelationDao.addRole(groupParticipant, group, GroupRoles.participant.name());
+
+		Identity courseParticipant = JunitTestHelper.createAndPersistIdentityAsRndUser("check-21");
+		repositoryEntryRelationDao.addRole(courseParticipant, entry, GroupRoles.participant.name());
+		dbInstance.commitAndCloseSession();
+		
+		//add some noise
+		Identity courseOwner = JunitTestHelper.createAndPersistIdentityAsRndUser("check-22");
+		repositoryEntryRelationDao.addRole(courseOwner, entry, GroupRoles.owner.name());
+		Identity groupWaiting = JunitTestHelper.createAndPersistIdentityAsRndUser("check-23");
+		businessGroupRelationDao.addRole(groupWaiting, group, GroupRoles.waiting.name());
+		dbInstance.commitAndCloseSession();
+		
+		
+		String checkboxId = UUID.randomUUID().toString();
+		String resSubPath = UUID.randomUUID().toString();
+		DBCheckbox checkbox = checkboxManager.createDBCheckbox(checkboxId, entry.getOlatResource(), resSubPath);
+		DBCheck checkGroup = checkboxManager.createCheck(checkbox, groupParticipant, null, Boolean.TRUE);
+		DBCheck checkCourse = checkboxManager.createCheck(checkbox, courseParticipant, null, Boolean.TRUE);
+		DBCheck checkNotVisible1 = checkboxManager.createCheck(checkbox, groupWaiting, null, Boolean.FALSE);
+		DBCheck checkNotVisible2 = checkboxManager.createCheck(checkbox, courseOwner, null, Boolean.FALSE);
+		dbInstance.commitAndCloseSession();
+		
+		List<BusinessGroup> groups = Collections.singletonList(group);
+		List<AssessmentData> loadedChecks = checkboxManager.getAssessmentDatas(entry.getOlatResource(), resSubPath, entry, groups);
+		Assert.assertNotNull(loadedChecks);
+		Assert.assertEquals(2, loadedChecks.size());
+		
+		List<DBCheck> collectedChecks = new ArrayList<>();
+		for(AssessmentData loadedCheck:loadedChecks) {
+			for(DBCheck loaded:loadedCheck.getChecks()) {
+				collectedChecks.add(loaded);
+			}
+		}
+		
+		Assert.assertEquals(2, collectedChecks.size());
+		Assert.assertTrue(collectedChecks.contains(checkGroup));
+		Assert.assertTrue(collectedChecks.contains(checkCourse));
+		Assert.assertFalse(collectedChecks.contains(checkNotVisible1));
+		Assert.assertFalse(collectedChecks.contains(checkNotVisible2));
+	}
 	
 	@Test
 	public void countChecks_resource() {
-- 
GitLab