use of org.olat.core.util.tree.TreeVisitor in project openolat by klemens.
the class QTIEditorMainController method createChangeMessage.
/**
* helper method to create the change log message
*
* @return
*/
private String createChangeMessage() {
// FIXME:pb:break down into smaller pieces
final StringBuilder result = new StringBuilder();
if (isRestrictedEdit()) {
Visitor v = new Visitor() {
/*
* a history key is built as follows
* sectionkey+"/"+itemkey+"/"+questionkey+"/"+responsekey
*/
String sectionKey = null;
Map<String, String> itemMap = new HashMap<>();
public void visit(INode node) {
if (node instanceof AssessmentNode) {
AssessmentNode an = (AssessmentNode) node;
String key = "null/null/null/null";
if (history.containsKey(key)) {
// some assessment top level data changed
Memento mem = history.get(key);
result.append("---+ Changes in test " + formatVariable(startedWithTitle) + ":");
result.append(an.createChangeMessage(mem));
}
} else if (node instanceof SectionNode) {
SectionNode sn = (SectionNode) node;
String tmpKey = ((Section) sn.getUnderlyingQTIObject()).getIdent();
String key = tmpKey + "/null/null/null";
if (history.containsKey(key)) {
// some section only data changed
Memento mem = history.get(key);
result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
result.append(sn.createChangeMessage(mem));
}
} else if (node instanceof ItemNode) {
ItemNode in = (ItemNode) node;
SectionNode sn = (SectionNode) in.getParent();
String parentSectkey = ((Section) ((SectionNode) in.getParent()).getUnderlyingQTIObject()).getIdent();
Item item = (Item) in.getUnderlyingQTIObject();
Question question = item.getQuestion();
String itemKey = item.getIdent();
String prefixKey = "null/" + itemKey;
String questionIdent = question != null ? question.getQuestion().getId() : "null";
String key = prefixKey + "/" + questionIdent + "/null";
StringBuilder changeMessage = new StringBuilder();
boolean hasChanges = false;
if (!itemMap.containsKey(itemKey)) {
Memento questMem = null;
Memento respMem = null;
if (history.containsKey(key)) {
// question changed!
questMem = history.get(key);
hasChanges = true;
}
// if(!hasChanges){
// check if a response changed
// new prefix for responses
prefixKey += "/null/";
// list contains org.olat.ims.qti.editor.beecom.objects.Response
List<Response> responses = question != null ? question.getResponses() : null;
if (responses != null && responses.size() > 0) {
// check for changes in each response
for (Iterator<Response> iter = responses.iterator(); iter.hasNext(); ) {
Response resp = iter.next();
if (history.containsKey(prefixKey + resp.getIdent())) {
// this response changed!
Memento tmpMem = history.get(prefixKey + resp.getIdent());
if (respMem != null) {
respMem = respMem.getTimestamp() > tmpMem.getTimestamp() ? tmpMem : respMem;
} else {
hasChanges = true;
respMem = tmpMem;
}
}
}
}
// output message
if (hasChanges) {
Memento mem = null;
if (questMem != null && respMem != null) {
// use the earlier memento
mem = questMem.getTimestamp() > respMem.getTimestamp() ? respMem : questMem;
} else if (questMem != null) {
mem = questMem;
} else if (respMem != null) {
mem = respMem;
}
changeMessage.append(in.createChangeMessage(mem));
itemMap.put(itemKey, itemKey);
if (!parentSectkey.equals(sectionKey)) {
// either this item belongs to a new section or no section
// is active
result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
result.append("\n").append(changeMessage);
sectionKey = parentSectkey;
} else {
result.append("\n").append(changeMessage);
}
}
}
}
}
private String formatVariable(String var) {
if (StringHelper.containsNonWhitespace(var)) {
return var;
}
return "[no entry]";
}
};
TreeVisitor tv = new TreeVisitor(v, menuTreeModel.getRootNode(), false);
tv.visitAll();
}
/*
*
*/
return result.toString();
}
use of org.olat.core.util.tree.TreeVisitor in project OpenOLAT by OpenOLAT.
the class OLATUpgrade_11_2_1 method processCourse.
private boolean processCourse(RepositoryEntry entry) {
try {
ICourse course = CourseFactory.loadCourse(entry);
CourseNode rootNode = course.getRunStructure().getRootNode();
final List<TACourseNode> taskNodes = new ArrayList<>();
new TreeVisitor(new Visitor() {
@Override
public void visit(INode node) {
if (node instanceof TACourseNode) {
taskNodes.add((TACourseNode) node);
}
}
}, rootNode, false).visitAll();
for (TACourseNode taskNode : taskNodes) {
processTaskCourseNode(course, entry, taskNode);
}
return true;
} catch (CorruptedCourseException e) {
log.warn("Corrupted course: " + entry.getDisplayname() + " (" + entry.getKey() + ")", e);
return true;
} catch (Exception e) {
log.error("", e);
return true;
}
}
use of org.olat.core.util.tree.TreeVisitor in project OpenOLAT by OpenOLAT.
the class OLATUpgrade_11_0_0 method processCourseAssessmentData.
// select count(*) from o_property where name in ('SCORE','PASSED','ATTEMPTS','COMMENT','COACH_COMMENT','ASSESSMENT_ID','FULLY_ASSESSED');
private boolean processCourseAssessmentData(RepositoryEntry courseEntry) {
boolean allOk = true;
try {
final Long courseResourceId = courseEntry.getOlatResource().getResourceableId();
final ICourse course = CourseFactory.loadCourse(courseEntry);
// load all assessable identities
List<Identity> assessableIdentities = getAllAssessableIdentities(course, courseEntry);
Map<AssessmentDataKey, AssessmentEntryImpl> curentNodeAssessmentMap = new HashMap<>();
{
// load already migrated data
List<AssessmentEntryImpl> currentNodeAssessmentList = loadAssessmentEntries(courseEntry);
for (AssessmentEntryImpl currentNodeAssessment : currentNodeAssessmentList) {
AssessmentDataKey key = new AssessmentDataKey(currentNodeAssessment.getIdentity().getKey(), courseResourceId, currentNodeAssessment.getSubIdent());
curentNodeAssessmentMap.put(key, currentNodeAssessment);
}
}
Map<AssessmentDataKey, AssessmentEntryImpl> nodeAssessmentMap = new HashMap<>();
{
// processed properties
List<Property> courseProperties = loadAssessmentProperties(courseEntry);
for (Property property : courseProperties) {
String propertyCategory = property.getCategory();
if (StringHelper.containsNonWhitespace(propertyCategory)) {
int nodeIdentIndex = propertyCategory.indexOf("::");
if (nodeIdentIndex > 0) {
String nodeIdent = propertyCategory.substring(propertyCategory.indexOf("::") + 2);
AssessmentDataKey key = new AssessmentDataKey(property.getIdentity().getKey(), property.getResourceTypeId(), nodeIdent);
if (curentNodeAssessmentMap.containsKey(key)) {
continue;
}
AssessmentEntryImpl nodeAssessment;
if (nodeAssessmentMap.containsKey(key)) {
nodeAssessment = nodeAssessmentMap.get(key);
if (nodeAssessment.getCreationDate().after(property.getCreationDate())) {
nodeAssessment.setCreationDate(property.getCreationDate());
}
if (nodeAssessment.getLastModified().before(property.getLastModified())) {
nodeAssessment.setLastModified(property.getLastModified());
}
} else {
nodeAssessment = createAssessmentEntry(property.getIdentity(), property, course, courseEntry, nodeIdent);
}
copyAssessmentProperty(property, nodeAssessment, course);
nodeAssessmentMap.put(key, nodeAssessment);
}
}
}
}
// check the transient qti ser
CourseNode rootNode = course.getRunStructure().getRootNode();
new TreeVisitor(new Visitor() {
@Override
public void visit(INode node) {
if (node instanceof AssessableCourseNode) {
processNonPropertiesStates(assessableIdentities, (AssessableCourseNode) node, course, courseEntry, nodeAssessmentMap, curentNodeAssessmentMap);
}
}
}, rootNode, true).visitAll();
dbInstance.commitAndCloseSession();
int count = 0;
for (AssessmentEntryImpl courseNodeAssessment : nodeAssessmentMap.values()) {
dbInstance.getCurrentEntityManager().persist(courseNodeAssessment);
if (++count % 50 == 0) {
dbInstance.commit();
}
}
dbInstance.commitAndCloseSession();
allOk = verifyCourseAssessmentData(assessableIdentities, courseEntry);
dbInstance.commitAndCloseSession();
if (allOk) {
List<STCourseNode> nodes = hasAssessableSTCourseNode(course);
if (nodes.size() > 0) {
log.info("Has assessables ST nodes");
for (Identity identity : assessableIdentities) {
IdentityEnvironment identityEnv = new IdentityEnvironment(identity, null);
UserCourseEnvironmentImpl userCourseEnv = new UserCourseEnvironmentImpl(identityEnv, course.getCourseEnvironment());
userCourseEnv.getScoreAccounting().evaluateAll(true);
dbInstance.commit();
}
}
}
} catch (Exception e) {
log.error("", e);
}
return allOk;
}
use of org.olat.core.util.tree.TreeVisitor in project OpenOLAT by OpenOLAT.
the class CourseRuntimeController method isAllowedToLeave.
private boolean isAllowedToLeave(UserCourseEnvironmentImpl uce) {
if (uce.getParticipatingGroups().size() > 0) {
CourseNode rootNode = uce.getCourseEnvironment().getRunStructure().getRootNode();
OLATResource courseResource = uce.getCourseEnvironment().getCourseGroupManager().getCourseResource();
AtomicBoolean bool = new AtomicBoolean(false);
new TreeVisitor(new Visitor() {
@Override
public void visit(INode node) {
if (!bool.get() && node instanceof ENCourseNode) {
try {
ENCourseNode enNode = (ENCourseNode) node;
boolean cancelEnrollEnabled = enNode.getModuleConfiguration().getBooleanSafe(ENCourseNode.CONF_CANCEL_ENROLL_ENABLED);
if (!cancelEnrollEnabled && enNode.isUsedForEnrollment(uce.getParticipatingGroups(), courseResource)) {
bool.set(true);
}
} catch (Exception e) {
logError("", e);
}
}
}
}, rootNode, true).visitAll();
if (bool.get()) {
// is in a enrollment group
return false;
}
}
return (uce.isParticipant() || !uce.getParticipatingGroups().isEmpty());
}
use of org.olat.core.util.tree.TreeVisitor in project OpenOLAT by OpenOLAT.
the class EditorMainController method dropNodeAsChild.
private void dropNodeAsChild(UserRequest ureq, ICourse course, String droppedNodeId, String targetNodeId, boolean asChild, boolean atTheEnd) {
// setDirty when moving
menuTree.setDirty(true);
CourseNode droppedNode = cetm.getCourseNode(droppedNodeId);
int position;
CourseEditorTreeNode insertParent;
if (asChild) {
insertParent = cetm.getCourseEditorNodeById(targetNodeId);
position = atTheEnd ? -1 : 0;
} else {
CourseEditorTreeNode selectedNode = cetm.getCourseEditorNodeById(targetNodeId);
if (selectedNode.getParent() == null) {
// root node
insertParent = selectedNode;
position = 0;
} else {
insertParent = course.getEditorTreeModel().getCourseEditorNodeById(selectedNode.getParent().getIdent());
position = 0;
for (position = insertParent.getChildCount(); position-- > 0; ) {
if (insertParent.getChildAt(position).getIdent().equals(selectedNode.getIdent())) {
position++;
break;
}
}
}
}
CourseEditorTreeNode moveFrom = course.getEditorTreeModel().getCourseEditorNodeById(droppedNode.getIdent());
// check if an ancestor is not dropped on a child
if (course.getEditorTreeModel().checkIfIsChild(insertParent, moveFrom)) {
showError("movecopynode.error.overlap");
fireEvent(ureq, Event.CANCELLED_EVENT);
return;
}
// don't generate red screen for that. If the position is too high -> add the node at the end
if (position >= insertParent.getChildCount()) {
position = -1;
}
try {
if (position >= 0) {
insertParent.insert(moveFrom, position);
} else {
insertParent.addChild(moveFrom);
}
} catch (IndexOutOfBoundsException e) {
logError("", e);
// reattach the node as security, if not, the node is lost
insertParent.addChild(moveFrom);
}
moveFrom.setDirty(true);
// mark subtree as dirty
TreeVisitor tv = new TreeVisitor(new Visitor() {
public void visit(INode node) {
CourseEditorTreeNode cetn = (CourseEditorTreeNode) node;
cetn.setDirty(true);
}
}, moveFrom, true);
tv.visitAll();
CourseFactory.saveCourseEditorTreeModel(course.getResourceableId());
showInfo("movecopynode.info.condmoved");
ThreadLocalUserActivityLogger.log(CourseLoggingAction.COURSE_EDITOR_NODE_MOVED, getClass());
euce.getCourseEditorEnv().validateCourse();
StatusDescription[] courseStatus = euce.getCourseEditorEnv().getCourseStatus();
updateCourseStatusMessages(ureq.getLocale(), courseStatus);
}
Aggregations