Skip to content
Snippets Groups Projects
Commit bebccd26 authored by srosse's avatar srosse
Browse files

OO-4576: use an update statement to append log to user's course node log

parent 6c13f9cc
No related branches found
No related tags found
No related merge requests found
......@@ -61,7 +61,10 @@ public class UserNodeAuditManagerImpl implements UserNodeAuditManager {
@Override
public void appendToUserNodeLog(CourseNode courseNode, Identity identity, Identity assessedIdentity, String logText, Role by) {
String text = formatMessage(identity, logText, by) ;
cpm.appendText(courseNode, assessedIdentity, null, LOG_IDENTIFYER, text);
/*
Property logProperty = cpm.findCourseNodeProperty(courseNode, assessedIdentity, null, LOG_IDENTIFYER);
if (logProperty == null) {
logProperty = cpm.createCourseNodePropertyInstance(courseNode, assessedIdentity, null, LOG_IDENTIFYER, null, null, null, text);
......@@ -72,12 +75,14 @@ public class UserNodeAuditManagerImpl implements UserNodeAuditManager {
logProperty.setTextValue(limitedLogContent);
cpm.updateProperty(logProperty);
}
*/
}
@Override
public void appendToUserNodeLog(CourseNode courseNode, Identity identity, BusinessGroup assessedGroup, String logText, Role by) {
String text = formatMessage(identity, logText, by) ;
cpm.appendText(courseNode, null, assessedGroup, LOG_IDENTIFYER, text);
/*
Property logProperty = cpm.findCourseNodeProperty(courseNode, null, assessedGroup, LOG_IDENTIFYER);
if (logProperty == null) {
logProperty = cpm.createCourseNodePropertyInstance(courseNode, null, assessedGroup, LOG_IDENTIFYER, null, null, null, text);
......@@ -88,6 +93,7 @@ public class UserNodeAuditManagerImpl implements UserNodeAuditManager {
logProperty.setTextValue(limitedLogContent);
cpm.updateProperty(logProperty);
}
*/
}
private String formatMessage(Identity identity, String logText, Role by) {
......
......@@ -114,6 +114,9 @@ public interface CoursePropertyManager extends IdentityAnonymizerCallback {
*/
public Property findCourseNodeProperty(CourseNode node, Identity identity, BusinessGroup grp, String name);
public void appendText(CourseNode node, Identity identity, BusinessGroup grp, String name, String text);
/**
* Find a specific course node property (exact match. I.e. null values are taken into account)
* @param node
......
......@@ -150,6 +150,12 @@ public class PersistingCoursePropertyManager implements CoursePropertyManager {
return pm.findProperty(identity, grp, myCategory, name);
}
@Override
public void appendText(CourseNode node, Identity identity, BusinessGroup grp, String name, String text) {
String myCategory = buildCourseNodePropertyCategory(node);
pm.appendTextToProperty(identity, grp, myCategory, name, text);
}
@Override
public Property findCourseNodeProperty(CourseNode node, BusinessGroup grp, String name) {
String myCategory = buildCourseNodePropertyCategory(node);
......
......@@ -121,6 +121,11 @@ final class PreviewCoursePropertyManager implements CoursePropertyManager {
return propertiesList;
}
@Override
public void appendText(CourseNode node, Identity identity, BusinessGroup grp, String name, String text) {
// Not implemented
}
@Override
public Property findCourseNodeProperty(CourseNode node, BusinessGroup grp, String name) {
List<Property> propertyList = properties.get(buildPropertyHashKey(buildCourseNodePropertyCategory(node), null, grp, name));
......
......@@ -150,6 +150,10 @@ public class NarrowedPropertyManager {
return pm.findProperty(identity, grp, resourceable, category, name);
}
public void appendTextToProperty(Identity identity, BusinessGroup grp, String category, String name, String text) {
pm.appendTextProperty(identity, grp, resourceable, category, name, text);
}
/**
* Exact match
* @param grp
......
......@@ -34,6 +34,7 @@ import javax.persistence.TypedQuery;
import org.olat.basesecurity.IdentityRef;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.persistence.QueryBuilder;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.core.logging.AssertException;
......@@ -604,6 +605,46 @@ public class PropertyManager implements UserDataDeletable {
return props.get(0);
}
public void appendTextProperty(Identity identity, BusinessGroup grp,
OLATResourceable resourceable, String category, String name, String textValue) {
QueryBuilder sb = new QueryBuilder();
sb.append("update ").append(Property.class.getName()).append(" v ")
.append(" set v.textValue=concat(v.textValue,:text), lastModified=:now");
if (identity != null) {
sb.and().append(" v.identity.key=:identityKey");
} else if(grp != null) {
sb.and().append(" v.grp.key=:groupKey");
}
sb
.and().append("v.resourceTypeName=:resName")
.and().append("v.resourceTypeId=:resId")
.and().append("v.category=:cat")
.and().append("v.name=:name");
Query query = DBFactory.getInstance().getCurrentEntityManager()
.createQuery(sb.toString())
.setParameter("resName", resourceable.getResourceableTypeName())
.setParameter("resId", resourceable.getResourceableId())
.setParameter("cat", category)
.setParameter("name", name)
.setParameter("text", textValue)
.setParameter("now", new Date());
if (identity != null) {
query.setParameter("identityKey", identity.getKey());
} else if(grp != null) {
query.setParameter("groupKey", grp.getKey());
}
int row = query.executeUpdate();
if(row == 0) {
Property prop = createPropertyInstance(identity, grp, resourceable, category, name, null, null, null, textValue);
saveProperty(prop);
}
DBFactory.getInstance().commit();
}
/**
* The query is an exact match where null value are NOT allowed.
* @param businessGroup
......
......@@ -87,7 +87,6 @@ public class AssessmentManagerTest extends OlatTestCase {
private Identity student;
private final Float score = new Float(10);
private final Boolean passed = Boolean.TRUE;
private final Boolean fullyAssessed = Boolean.TRUE;
@Autowired
private EfficiencyStatementManager efficiencyStatementManager;
......
......@@ -29,18 +29,20 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.UUID;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
import org.olat.core.commons.persistence.DBFactory;
import org.olat.core.commons.persistence.DB;
import org.olat.core.id.Identity;
import org.apache.logging.log4j.Logger;
import org.olat.core.logging.Tracing;
import org.olat.course.CourseFactory;
import org.olat.course.ICourse;
import org.olat.course.nodes.CourseNode;
import org.olat.modules.assessment.Role;
import org.olat.repository.RepositoryEntry;
import org.olat.test.JunitTestHelper;
import org.olat.test.OlatTestCase;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author Christian Guretzki
......@@ -48,16 +50,42 @@ import org.olat.test.OlatTestCase;
public class UserNodeAuditManagerTest extends OlatTestCase {
private static final Logger log = Tracing.createLoggerFor(UserNodeAuditManagerTest.class);
@Autowired
private DB dbInstance;
@Test
public void appendToUserNodeLog() {
//import a course
Identity author = JunitTestHelper.createAndPersistIdentityAsRndUser("Auth-");
Identity assessedIdentity = JunitTestHelper.createAndPersistIdentityAsRndUser("Logger-");
RepositoryEntry repositoryEntry = JunitTestHelper.deployDemoCourse(author);
Long resourceableId = repositoryEntry.getOlatResource().getResourceableId();
ICourse course = CourseFactory.loadCourse(resourceableId);
dbInstance.commitAndCloseSession();
CourseNode courseNode = course.getRunStructure().getRootNode();
UserNodeAuditManager userNodeAuditManager = course.getCourseEnvironment().getAuditManager();
userNodeAuditManager.appendToUserNodeLog(courseNode, author, assessedIdentity, "Hello", Role.coach);
dbInstance.commitAndCloseSession();
userNodeAuditManager.appendToUserNodeLog(courseNode, assessedIdentity, assessedIdentity, " world", Role.user);
dbInstance.commitAndCloseSession();
String logText = userNodeAuditManager.getUserNodeLog(courseNode, assessedIdentity);
Assert.assertTrue(logText.contains("Hello"));
Assert.assertTrue(logText.contains("world"));
}
@Test
public void testCreateLimitedLogContent() {
//import a course
Identity author = JunitTestHelper.createAndPersistIdentityAsUser("Auth-" + UUID.randomUUID());
Identity author = JunitTestHelper.createAndPersistIdentityAsRndUser("Auth-");
RepositoryEntry repositoryEntry = JunitTestHelper.deployDemoCourse(author);
Long resourceableId = repositoryEntry.getOlatResource().getResourceableId();
log.info("Demo course imported - resourceableId: " + resourceableId);
ICourse course = CourseFactory.loadCourse(resourceableId);
DBFactory.getInstance().commitAndCloseSession();
dbInstance.commitAndCloseSession();
log.info("Start testCreateLimitedLogContent");
......@@ -67,20 +95,20 @@ public class UserNodeAuditManagerTest extends OlatTestCase {
logContent.append( createTestLogContent(1) );
String limitedLogContent = userNodeAuditManagerImpl.createLimitedLogContent(logContent.toString(), 400);
assertEquals("logContent should not be limited", logContent.toString(), limitedLogContent);
log.info("limitedLogContent:\n" + limitedLogContent);
log.info("limitedLogContent.length=" + limitedLogContent.length());
log.info("limitedLogContent: {}", limitedLogContent);
log.info("limitedLogContent.length= {}", limitedLogContent.length());
logContent.append( createTestLogContent(2) );
limitedLogContent = userNodeAuditManagerImpl.createLimitedLogContent(logContent.toString(), 400);
assertEquals("logContent should not be limited", logContent.toString(), limitedLogContent);
log.info("limitedLogContent:\n" + limitedLogContent);
log.info("limitedLogContent.length=" + limitedLogContent.length());
log.info("limitedLogContent: {}", limitedLogContent);
log.info("limitedLogContent.length={}", limitedLogContent.length());
logContent.append( createTestLogContent(3) );
limitedLogContent = userNodeAuditManagerImpl.createLimitedLogContent(logContent.toString(), 400);
assertEquals("logContent should not be limited", logContent.toString(), limitedLogContent);
log.info("limitedLogContent:\n" + limitedLogContent);
log.info("limitedLogContent.length=" + limitedLogContent.length());
log.info("limitedLogContent: {}", limitedLogContent);
log.info("limitedLogContent.length={}", limitedLogContent.length());
logContent.append( createTestLogContent(4) );
log.info("logContent.length()=" + logContent.length());
......@@ -90,8 +118,8 @@ public class UserNodeAuditManagerTest extends OlatTestCase {
assertTrue("Missing Log entry2",limitedLogContent.contains("LogEntry #2"));
assertTrue("Missing Log entry3",limitedLogContent.contains("LogEntry #3"));
assertTrue("Missing Log entry4",limitedLogContent.contains("LogEntry #4"));
log.info("limitedLogContent:\n" + limitedLogContent);
log.info("limitedLogContent.length=" + limitedLogContent.length());
log.info("limitedLogContent: {}", limitedLogContent);
log.info("limitedLogContent.length={}", limitedLogContent.length());
logContent.append( createTestLogContent(5) );
limitedLogContent = userNodeAuditManagerImpl.createLimitedLogContent(logContent.toString(), 400);
......@@ -100,8 +128,8 @@ public class UserNodeAuditManagerTest extends OlatTestCase {
assertTrue("Missing Log entry3",limitedLogContent.contains("LogEntry #3"));
assertTrue("Missing Log entry4",limitedLogContent.contains("LogEntry #4"));
assertTrue("Missing Log entry5",limitedLogContent.contains("LogEntry #5"));
log.info("limitedLogContent:\n" + limitedLogContent);
log.info("limitedLogContent.length=" + limitedLogContent.length());
log.info("limitedLogContent: {}", limitedLogContent);
log.info("limitedLogContent.length={}", limitedLogContent.length());
}
private String createTestLogContent(int entryNumber) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment