diff --git a/src/main/java/org/olat/course/assessment/EfficiencyStatementManager.java b/src/main/java/org/olat/course/assessment/EfficiencyStatementManager.java index eaf931ac397090878857a08e476d38faacd64edc..23e5875fa0759f13b38a97afafb7942046273846 100644 --- a/src/main/java/org/olat/course/assessment/EfficiencyStatementManager.java +++ b/src/main/java/org/olat/course/assessment/EfficiencyStatementManager.java @@ -624,7 +624,7 @@ public class EfficiencyStatementManager extends BasicManager implements UserData * @param courseRepoEntryKey * @return List of identities */ - protected List<Identity> findIdentitiesWithEfficiencyStatements(Long courseRepoEntryKey) { + public List<Identity> findIdentitiesWithEfficiencyStatements(Long courseRepoEntryKey) { try { StringBuilder sb = new StringBuilder(); sb.append("select distinct(statement.identity) from ").append(UserEfficiencyStatementImpl.class.getName()).append(" as statement ") @@ -736,12 +736,21 @@ public class EfficiencyStatementManager extends BasicManager implements UserData * Delete all efficiency-statements for certain identity. * @param identity Delete data for this identity. */ + @Override public void deleteUserData(Identity identity, String newDeletedUserName) { - List<EfficiencyStatement> efficiencyStatements = findEfficiencyStatements(identity); - for (Iterator<EfficiencyStatement> iter = efficiencyStatements.iterator(); iter.hasNext();) { - deleteEfficiencyStatement(identity, iter.next()); + try { + StringBuilder sb = new StringBuilder(); + sb.append("delete from ").append(UserEfficiencyStatementImpl.class.getName()).append(" as statement ") + .append(" where statement.identity.key=:identityKey"); + + int numOfDeletedStatements = dbInstance.getCurrentEntityManager() + .createQuery(sb.toString()) + .setParameter("identityKey", identity.getKey()) + .executeUpdate(); + + logDebug(numOfDeletedStatements + " efficiency statements deleted for identity=" + identity); + } catch (Exception e) { + logError("deleteUserData(EfficiencyStatements): " + identity, e); } - logDebug("All efficiency statements deleted for identity=" + identity); } - } diff --git a/src/main/java/org/olat/course/assessment/UserEfficiencyStatement.java b/src/main/java/org/olat/course/assessment/UserEfficiencyStatement.java index 14e7586b927f91f818ef53b27c9ee1a1151240fc..2a9a9f53cab2918bffeb7b0d484d0f989e8b3ebc 100644 --- a/src/main/java/org/olat/course/assessment/UserEfficiencyStatement.java +++ b/src/main/java/org/olat/course/assessment/UserEfficiencyStatement.java @@ -34,6 +34,8 @@ import org.olat.core.id.Identity; * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com */ public interface UserEfficiencyStatement { + + public Long getKey(); public Identity getIdentity(); diff --git a/src/test/java/org/olat/course/assessment/manager/EfficiencyStatementManagerTest.java b/src/test/java/org/olat/course/assessment/manager/EfficiencyStatementManagerTest.java index a584dddafdfe0633858a5a79c279592d8586fa43..5928c00d142604732f6849ff5294d0b1dbfe0ab9 100644 --- a/src/test/java/org/olat/course/assessment/manager/EfficiencyStatementManagerTest.java +++ b/src/test/java/org/olat/course/assessment/manager/EfficiencyStatementManagerTest.java @@ -19,23 +19,28 @@ */ package org.olat.course.assessment.manager; +import java.io.File; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Collections; +import java.util.Date; import java.util.List; +import java.util.UUID; +import org.junit.Assert; import org.junit.Test; -import org.olat.basesecurity.BaseSecurity; +import org.olat.basesecurity.GroupRoles; import org.olat.core.commons.persistence.DB; -import org.olat.core.commons.persistence.DBFactory; import org.olat.core.id.Identity; -import org.olat.core.id.Roles; -import org.olat.course.CorruptedCourseException; import org.olat.course.CourseFactory; import org.olat.course.ICourse; import org.olat.course.assessment.EfficiencyStatementManager; -import org.olat.course.config.CourseConfig; +import org.olat.course.assessment.UserEfficiencyStatement; +import org.olat.course.assessment.model.UserEfficiencyStatementLight; +import org.olat.modules.coach.CoachingLargeTest; import org.olat.repository.RepositoryEntry; -import org.olat.repository.RepositoryManager; -import org.olat.repository.model.SearchRepositoryEntryParameters; +import org.olat.repository.RepositoryService; +import org.olat.test.JunitTestHelper; import org.olat.test.OlatTestCase; import org.springframework.beans.factory.annotation.Autowired; @@ -50,62 +55,113 @@ public class EfficiencyStatementManagerTest extends OlatTestCase { @Autowired private DB dbInstance; @Autowired - private BaseSecurity securityManager; + private RepositoryService repositoryService; @Autowired - private RepositoryManager repositoryManager; + private EfficiencyStatementManager effManager; + /** + * Create and reload an efficiency statement. + * + * @throws URISyntaxException + */ @Test - public void testBigDatas() { - SearchRepositoryEntryParameters params = new SearchRepositoryEntryParameters(); - params.setRoles(new Roles(true, false, false, false, false, false, false)); - params.setResourceTypes(Collections.singletonList("CourseModule")); - List<RepositoryEntry> entries = repositoryManager.genericANDQueryWithRolesRestriction(params, 0, -1, true); + public void testEfficiencyStatement() throws URISyntaxException { + RepositoryEntry re = deployTestcourse(); + ICourse course = CourseFactory.loadCourse(re); - List<Identity> loadIdentities = securityManager - .getVisibleIdentitiesByPowerSearch(null, null, false, null, null, null, null, null, 0, 10000); - EfficiencyStatementManager efficiencyStatementManager = EfficiencyStatementManager.getInstance(); + //add some members + Identity participant = JunitTestHelper.createAndPersistIdentityAsRndUser("Eff-Part-1"); + repositoryService.addRole(participant, re, GroupRoles.participant.name()); + dbInstance.commitAndCloseSession(); + + //make statements + UserEfficiencyStatement statement = effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant, re.getOlatResource()); + dbInstance.commitAndCloseSession(); + + //load the efficiency statements + List<UserEfficiencyStatementLight> statementsLight = effManager.findEfficiencyStatementsLight(participant); + Assert.assertNotNull(statementsLight); + Assert.assertEquals(1, statementsLight.size()); + UserEfficiencyStatementLight statementLight = statementsLight.get(0); + Assert.assertEquals(statement.getKey(), statementLight.getKey()); + Assert.assertEquals(participant, statementLight.getIdentity()); + Assert.assertEquals(statement.getCourseRepoKey(), statementLight.getCourseRepoKey()); + Assert.assertEquals(re.getKey(), statementLight.getCourseRepoKey()); + Assert.assertEquals(course.getCourseTitle(), statementLight.getShortTitle()); + Assert.assertEquals(re.getOlatResource(), statementLight.getResource()); + Assert.assertEquals(re.getOlatResource().getKey(), statementLight.getArchivedResourceKey()); + Assert.assertNotNull(statementLight.getCreationDate()); + Assert.assertNotNull(statementLight.getLastModified()); + Assert.assertTrue(statementLight.getPassed()); + Assert.assertEquals(6.0f, statementLight.getScore(), 0.00001); + } + + + @Test + public void deleteUserData() throws URISyntaxException { + RepositoryEntry re1 = deployTestcourse(); + RepositoryEntry re2 = deployTestcourse(); - int count = 0; - for(RepositoryEntry entry:entries) { - Long resourceableId = entry.getOlatResource().getResourceableId(); + //add some members + Identity participant1 = JunitTestHelper.createAndPersistIdentityAsRndUser("Eff-Del-Part-1"); + Identity participant2 = JunitTestHelper.createAndPersistIdentityAsRndUser("Eff-Del-Part-2"); + repositoryService.addRole(participant1, re1, GroupRoles.participant.name()); + repositoryService.addRole(participant2, re1, GroupRoles.participant.name()); + repositoryService.addRole(participant1, re2, GroupRoles.participant.name()); + repositoryService.addRole(participant2, re2, GroupRoles.participant.name()); + dbInstance.commitAndCloseSession(); + + //make statements + UserEfficiencyStatement statement1_1 = effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant1, re1.getOlatResource()); + UserEfficiencyStatement statement1_2 = effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant1, re2.getOlatResource()); + UserEfficiencyStatement statement2_1 = effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant2, re1.getOlatResource()); + UserEfficiencyStatement statement2_2 = effManager.createUserEfficiencyStatement(new Date(), 6.0f, true, participant2, re2.getOlatResource()); + dbInstance.commitAndCloseSession(); + + //load the efficiency statements + List<UserEfficiencyStatementLight> statementsLight1 = effManager.findEfficiencyStatementsLight(participant1); + Assert.assertEquals(2, statementsLight1.size()); - try { - ICourse course = CourseFactory.loadCourse(resourceableId); - boolean enabled =course.getCourseEnvironment().getCourseConfig().isEfficencyStatementEnabled(); - if(!enabled) { - course = CourseFactory.openCourseEditSession(entry.getOlatResource().getResourceableId()); - CourseConfig courseConfig = course.getCourseEnvironment().getCourseConfig(); - courseConfig.setEfficencyStatementIsEnabled(true); - CourseFactory.setCourseConfig(course.getResourceableId(), courseConfig); - CourseFactory.saveCourse(course.getResourceableId()); - CourseFactory.closeCourseEditSession(course.getResourceableId(),true); - } - DBFactory.getInstance().commitAndCloseSession(); - - try { - int fromIndex = (int)(Math.random() * loadIdentities.size() - 1); - if(fromIndex < 100) { - fromIndex = 100; - } - List<Identity> assessedIdentities = loadIdentities.subList(fromIndex - 100, fromIndex); - //force the storing of the efficiencyStatement - this is usually done only at Learnresource/modify properties/Efficiency statement (ON) - efficiencyStatementManager.updateEfficiencyStatements(course, assessedIdentities); - - } catch (Exception e) { - e.printStackTrace(); - } - DBFactory.getInstance().commitAndCloseSession(); - DBFactory.getInstance().closeSession(); - } catch (CorruptedCourseException e) { - System.out.println("Error"); - } - - if(count++ % 100 == 0) { - dbInstance.commitAndCloseSession(); - } - } + //delete user 1 + effManager.deleteUserData(participant1, "deleted"); + dbInstance.commitAndCloseSession(); - + //check the efficiency statements + List<UserEfficiencyStatementLight> deletedStatementsLight1 = effManager.findEfficiencyStatementsLight(participant1); + Assert.assertTrue(deletedStatementsLight1.isEmpty()); + List<UserEfficiencyStatementLight> deletedStatementsLight2 = effManager.findEfficiencyStatementsLight(participant2); + Assert.assertEquals(2, deletedStatementsLight2.size()); + + //double check + List<Identity> identitesRe1 = effManager.findIdentitiesWithEfficiencyStatements(re1.getKey()); + Assert.assertEquals(1, identitesRe1.size()); + Assert.assertTrue(identitesRe1.contains(participant2)); + List<Identity> identitesRe2 = effManager.findIdentitiesWithEfficiencyStatements(re2.getKey()); + Assert.assertEquals(1, identitesRe2.size()); + Assert.assertTrue(identitesRe2.contains(participant2)); + + //triple check + List<UserEfficiencyStatementLight> reloadStatemets_1_1 = effManager.findEfficiencyStatementsLight(Collections.<Long>singletonList(statement1_1.getKey())); + Assert.assertTrue(reloadStatemets_1_1.isEmpty()); + List<UserEfficiencyStatementLight> reloadStatemets_1_2 = effManager.findEfficiencyStatementsLight(Collections.<Long>singletonList(statement1_2.getKey())); + Assert.assertTrue(reloadStatemets_1_2.isEmpty()); + List<UserEfficiencyStatementLight> reloadStatemets_2_1 = effManager.findEfficiencyStatementsLight(Collections.<Long>singletonList(statement2_1.getKey())); + Assert.assertEquals(1, reloadStatemets_2_1.size()); + List<UserEfficiencyStatementLight> reloadStatemets_2_2 = effManager.findEfficiencyStatementsLight(Collections.<Long>singletonList(statement2_2.getKey())); + Assert.assertEquals(1, reloadStatemets_2_2.size()); + } + + private RepositoryEntry deployTestcourse() throws URISyntaxException { + //deploy a course + URL courseWithForumsUrl = CoachingLargeTest.class.getResource("CoachingCourse.zip"); + File courseWithForums = new File(courseWithForumsUrl.toURI()); + String softKey = UUID.randomUUID().toString(); + RepositoryEntry re = CourseFactory.deployCourseFromZIP(courseWithForums, softKey, 4); + Assert.assertNotNull(re); + dbInstance.commitAndCloseSession(); + ICourse course = CourseFactory.loadCourse(re); + Assert.assertTrue(course.getCourseEnvironment().getCourseConfig().isEfficencyStatementEnabled()); + return re; } }