diff --git a/src/main/java/org/olat/course/nodes/ENCourseNode.java b/src/main/java/org/olat/course/nodes/ENCourseNode.java index 36429598d879ed2ad6bebdc71c6db980127ad0f3..014e45d239b28534982926dc0170400f1fdb9c05 100644 --- a/src/main/java/org/olat/course/nodes/ENCourseNode.java +++ b/src/main/java/org/olat/course/nodes/ENCourseNode.java @@ -38,6 +38,7 @@ import org.olat.core.gui.translator.PackageTranslator; import org.olat.core.gui.translator.Translator; import org.olat.core.id.Roles; import org.olat.core.util.Util; +import org.olat.core.util.filter.FilterFactory; import org.olat.course.ICourse; import org.olat.course.condition.ConditionEditController; import org.olat.course.editor.CourseEditorEnv; @@ -191,13 +192,15 @@ public class ENCourseNode extends AbstractAccessableCourseNode { /* * check group and area names for existence */ + String nodeId = getIdent(); + ModuleConfiguration mc = getModuleConfiguration(); String areaStr = (String) mc.get(CONFIG_AREANAME); - String nodeId = getIdent(); if (areaStr != null) { String[] areas = areaStr.split(","); for (int i = 0; i < areas.length; i++) { - String trimmed = areas[i] != null ? areas[i].trim() : areas[i]; + String trimmed = areas[i] != null ? + FilterFactory.getHtmlTagsFilter().filter(areas[i]).trim() : areas[i]; if (!trimmed.equals("") && !cev.existsArea(trimmed)) { StatusDescription sd = new StatusDescription(StatusDescription.WARNING, "error.notfound.name", "solution.checkgroupmanagement", new String[] { "NONE", trimmed }, translatorStr); @@ -210,7 +213,8 @@ public class ENCourseNode extends AbstractAccessableCourseNode { if (groupStr != null) { String[] groups = groupStr.split(","); for (int i = 0; i < groups.length; i++) { - String trimmed = groups[i] != null ? groups[i].trim() : groups[i]; + String trimmed = groups[i] != null ? + FilterFactory.getHtmlTagsFilter().filter(groups[i]).trim() : groups[i]; if (!trimmed.equals("") && !cev.existsGroup(trimmed)) { StatusDescription sd = new StatusDescription(StatusDescription.WARNING, "error.notfound.name", "solution.checkgroupmanagement", new String[] { "NONE", trimmed }, translatorStr); diff --git a/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java b/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java index b0d4ee6be242c5e2d13d79d88911bbc95a7156cd..2afb01b980cf03524c7fbe2ab15499a117116dd5 100644 --- a/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java +++ b/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java @@ -25,7 +25,6 @@ package org.olat.course.nodes.en; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.List; @@ -158,21 +157,15 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener @Override protected void formOK(UserRequest ureq) { // 1. group names - List<Long> groupKeys = getKeys(easyGroupList); - String groupName = ""; - if (StringHelper.containsNonWhitespace(easyGroupList.getValue())){ - groupName = easyGroupList.getValue(); - } - moduleConfig.set(ENCourseNode.CONFIG_GROUPNAME, groupName); - moduleConfig.set(ENCourseNode.CONFIG_GROUP_IDS, groupKeys); + KeysAndNames groupKeysAndNames = (KeysAndNames)easyGroupList.getUserObject(); + String groupNames = StringHelper.formatAsSortUniqCSVString(groupKeysAndNames.getNames()); + moduleConfig.set(ENCourseNode.CONFIG_GROUPNAME, groupNames); + moduleConfig.set(ENCourseNode.CONFIG_GROUP_IDS, groupKeysAndNames.getKeys()); // 2. area names - List<Long> areaKeys = getKeys(easyAreaList); - String areaName = ""; - if (StringHelper.containsNonWhitespace(easyAreaList.getValue())){ - areaName = easyAreaList.getValue(); - } - moduleConfig.set(ENCourseNode.CONFIG_AREANAME, areaName); - moduleConfig.set(ENCourseNode.CONFIG_AREA_IDS, areaKeys); + KeysAndNames areaKeysAndNames = (KeysAndNames)easyAreaList.getUserObject(); + String areaNames = StringHelper.formatAsSortUniqCSVString(areaKeysAndNames.getNames()); + moduleConfig.set(ENCourseNode.CONFIG_AREANAME, areaNames); + moduleConfig.set(ENCourseNode.CONFIG_AREA_IDS, areaKeysAndNames.getKeys()); // 3. cancel-enroll-enabled flag Boolean cancelEnrollEnabled = enableCancelEnroll.getSelectedKeys().size()==1; moduleConfig.set(ENCourseNode.CONF_CANCEL_ENROLL_ENABLED, cancelEnrollEnabled); @@ -192,26 +185,22 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener @Override protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) { // groups - - String groupInitVal; - @SuppressWarnings("unchecked") - List<Long> groupKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_GROUP_IDS); + List<Long> groupKeys = moduleConfig.getList(ENCourseNode.CONFIG_GROUP_IDS, Long.class); if(groupKeys == null) { - groupInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); - if(StringHelper.containsNonWhitespace(groupInitVal)) { - groupKeys = businessGroupService.toGroupKeys(groupInitVal, cev.getCourseGroupManager().getCourseEntry()); + String groupNames = (String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); + if(StringHelper.containsNonWhitespace(groupNames)) { + groupKeys = businessGroupService.toGroupKeys(groupNames, cev.getCourseGroupManager().getCourseEntry()); } else { groupKeys = new ArrayList<Long>(); } } - groupInitVal = getGroupNames(groupKeys); - + KeysAndNames groupInitVal = getGroupKeysAndNames(groupKeys); chooseGroupsLink = uifactory.addFormLink("chooseGroup", formLayout, "btn btn-default o_xsmall o_form_groupchooser"); chooseGroupsLink.setLabel("form.groupnames", null); chooseGroupsLink.setIconLeftCSS("o_icon o_icon-fw o_icon_group"); - easyGroupList = uifactory.addStaticTextElement("group", null, groupInitVal == null ? "" : groupInitVal, formLayout); - easyGroupList.setUserObject(groupKeys); + easyGroupList = uifactory.addStaticTextElement("group", null, groupInitVal.getDecoratedNames(), formLayout); + easyGroupList.setUserObject(groupInitVal); easyGroupList.setElementCssClass("text-muted"); hasGroups = businessGroupService.countBusinessGroups(null, cev.getCourseGroupManager().getCourseEntry()) > 0; @@ -222,21 +211,21 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } // areas - String areaInitVal; + @SuppressWarnings("unchecked") List<Long> areaKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_AREA_IDS); if(areaKeys == null) { - areaInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); - areaKeys = areaManager.toAreaKeys(areaInitVal, cev.getCourseGroupManager().getCourseResource()); + String areaNames = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); + areaKeys = areaManager.toAreaKeys(areaNames, cev.getCourseGroupManager().getCourseResource()); } - areaInitVal = getAreaNames(areaKeys); + KeysAndNames areaInitVal = getAreaKeysAndNames(areaKeys); chooseAreasLink = uifactory.addFormLink("chooseArea", formLayout, "btn btn-default o_xsmall o_form_areachooser"); chooseAreasLink.setLabel("form.areanames", null); chooseAreasLink.setIconLeftCSS("o_icon o_icon-fw o_icon_courseareas"); - easyAreaList = uifactory.addStaticTextElement("area", null, areaInitVal == null ? "" : areaInitVal, formLayout); - easyAreaList.setUserObject(areaKeys); + easyAreaList = uifactory.addStaticTextElement("area", null, areaInitVal.getDecoratedNames(), formLayout); + easyAreaList.setUserObject(areaInitVal); easyAreaList.setElementCssClass("text-muted"); hasAreas = areaManager.countBGAreasInContext(cev.getCourseGroupManager().getCourseResource()) > 0; @@ -493,14 +482,9 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener if (source == groupChooseC) { if (event == Event.DONE_EVENT) { cmc.deactivate(); - List<String> newGroupNames = groupChooseC.getSelectedNames(); - List<String> escapedNames = new ArrayList<>(newGroupNames.size()); - for(String newGroupName:newGroupNames) { - escapedNames.add(StringHelper.escapeHtml(newGroupName)); - } - String list = StringHelper.formatAsSortUniqCSVString(escapedNames); - easyGroupList.setValue(list == null ? "" : list); - easyGroupList.setUserObject(groupChooseC.getSelectedKeys()); + KeysAndNames c = getGroupKeysAndNames(groupChooseC.getSelectedKeys()); + easyGroupList.setValue(c.getDecoratedNames()); + easyGroupList.setUserObject(c); easyGroupList.getRootForm().submit(ureq); chooseGroupsLink.setI18nKey("choose"); } else if (Event.CANCELLED_EVENT == event) { @@ -509,14 +493,9 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } else if (source == areaChooseC) { if (event == Event.DONE_EVENT) { cmc.deactivate(); - List<String> newAreaNames = areaChooseC.getSelectedNames(); - List<String> escapedNames = new ArrayList<>(newAreaNames.size()); - for(String newAreaName:newAreaNames) { - escapedNames.add(StringHelper.escapeHtml(newAreaName)); - } - String list = StringHelper.formatAsSortUniqCSVString(escapedNames); - easyAreaList.setValue(list == null ? "" : list); - easyAreaList.setUserObject(areaChooseC.getSelectedKeys()); + KeysAndNames c = getAreaKeysAndNames(areaChooseC.getSelectedKeys()); + easyAreaList.setValue(c.getDecoratedNames()); + easyAreaList.setUserObject(c); easyAreaList.getRootForm().submit(ureq); chooseAreasLink.setI18nKey("choose"); } else if (event == Event.CANCELLED_EVENT) { @@ -530,12 +509,17 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener List<Long> c = new ArrayList<Long>(); c.addAll(getKeys(easyGroupList)); if (fixGroupError != null && fixGroupError.getUserObject() != null) { - c.removeAll(Arrays.asList((String[])fixGroupError.getUserObject())); + String[] keyArr = (String[])fixGroupError.getUserObject(); + if(keyArr != null && keyArr.length > 0) { + List<Long> fixedKeys = toKeys(keyArr[0]); + c.removeAll(fixedKeys); + } } c.addAll(groupCreateCntrllr.getCreatedGroupKeys()); - easyGroupList.setValue(getGroupNames(c)); - easyGroupList.setUserObject(c); + KeysAndNames keysAndNames = getGroupKeysAndNames(c); + easyGroupList.setValue(keysAndNames.getDecoratedNames()); + easyGroupList.setUserObject(keysAndNames); if (groupCreateCntrllr.getCreatedGroupNames().size() > 0 && !hasGroups) { chooseGroupsLink.setLinkTitle("select"); @@ -551,12 +535,17 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener List<Long> c = new ArrayList<Long>(); c.addAll(getKeys(easyAreaList)); if (fixAreaError != null && fixAreaError.getUserObject() != null) { - c.removeAll(Arrays.asList((String[])fixAreaError.getUserObject())); + String[] keyArr = (String[])fixAreaError.getUserObject(); + if(keyArr != null && keyArr.length > 0) { + List<Long> fixedKeys = toKeys(keyArr[0]); + c.removeAll(fixedKeys); + } } c.addAll(areaCreateCntrllr.getCreatedAreaKeys()); - easyAreaList.setValue(getAreaNames(c)); - easyAreaList.setUserObject(c); + KeysAndNames keysAndNames = getAreaKeysAndNames(c); + easyAreaList.setValue(keysAndNames.getDecoratedNames()); + easyAreaList.setUserObject(keysAndNames); if (areaCreateCntrllr.getCreatedAreaNames().size() > 0 && !hasAreas) { chooseAreasLink.setLinkTitle("select"); @@ -604,13 +593,12 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } private List<Long> getKeys(StaticTextElement element) { - @SuppressWarnings("unchecked") - List<Long> keys = (List<Long>)element.getUserObject(); + KeysAndNames keys = (KeysAndNames)element.getUserObject(); if(keys == null) { - keys = new ArrayList<Long>(); + keys = new KeysAndNames(); element.setUserObject(keys); } - return keys; + return keys.getKeys(); } private String toString(Collection<Long> keys) { @@ -622,26 +610,71 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener return sb.toString(); } - private String getGroupNames(List<Long> keys) { + private List<Long> toKeys(String keysString) { + List<Long> keyList = new ArrayList<>(); + String[] keys = keysString.split(","); + for(String key:keys) { + try { + keyList.add(Long.parseLong(key)); + } catch (NumberFormatException e) { + logWarn("Cannot parse this key: " + key, e); + } + } + return keyList; + } + + private KeysAndNames getGroupKeysAndNames(List<Long> keys) { StringBuilder sb = new StringBuilder(); + KeysAndNames keysAndNames = new KeysAndNames(); + keysAndNames.getKeys().addAll(keys); + List<BusinessGroupShort> groups = businessGroupService.loadShortBusinessGroups(keys); for(BusinessGroupShort group:groups) { if(sb.length() > 0) sb.append(" "); sb.append("<i class='o_icon o_icon-fw o_icon_group'> </i> "); sb.append(StringHelper.escapeHtml(group.getName())); + keysAndNames.getNames().add(group.getName()); + } - return sb.toString(); + keysAndNames.setDecoratedNames(sb.toString()); + return keysAndNames; } - private String getAreaNames(List<Long> keys) { + private KeysAndNames getAreaKeysAndNames(List<Long> keys) { StringBuilder sb = new StringBuilder(); List<BGArea> areas = areaManager.loadAreas(keys); + KeysAndNames keysAndNames = new KeysAndNames(); + keysAndNames.getKeys().addAll(keys); for(BGArea area:areas) { if(sb.length() > 0) sb.append(" "); sb.append("<i class='o_icon o_icon-fw o_icon_courseareas'> </i> "); sb.append(StringHelper.escapeHtml(area.getName())); + keysAndNames.getNames().add(area.getName()); } - return sb.toString(); + keysAndNames.setDecoratedNames(sb.toString()); + return keysAndNames; } + + private static class KeysAndNames { + + private final List<Long> keys = new ArrayList<>(); + private final List<String> names = new ArrayList<>(); + private String decoratedNames; + + public List<Long> getKeys() { + return keys; + } + + public List<String> getNames() { + return names; + } -} + public String getDecoratedNames() { + return decoratedNames; + } + + public void setDecoratedNames(String decoratedNames) { + this.decoratedNames = decoratedNames; + } + } +} \ No newline at end of file