diff --git a/src/main/java/org/olat/course/condition/ConditionConfigEasyController.java b/src/main/java/org/olat/course/condition/ConditionConfigEasyController.java index ccf7d651c3a6983260f8836f64c59c9cbe24fcfe..f111d26168e80f57569bd6e836ae390b0b501958 100644 --- a/src/main/java/org/olat/course/condition/ConditionConfigEasyController.java +++ b/src/main/java/org/olat/course/condition/ConditionConfigEasyController.java @@ -638,14 +638,10 @@ public class ConditionConfigEasyController extends FormBasicController implement String csvMissAreas = toString(missingAreas); String[] params = new String[] { "-", csvMissAreas }; - /* - * create error with link to fix it - */ + // create error with link to fix it String vc_errorPage = velocity_root + "/erroritem.html"; FormLayoutContainer errorAreaItemLayout = FormLayoutContainer.createCustomFormLayout( - "errorareaitem", getTranslator(), vc_errorPage - ); - + "errorareaitem", getTranslator(), vc_errorPage); areaChooseSubContainer.setErrorComponent(errorAreaItemLayout, this.flc); // FXINGIN LINK ONLY IF DEFAULT CONTEXT EXISTS @@ -674,8 +670,8 @@ public class ConditionConfigEasyController extends FormBasicController implement } } - boolean easyGroupOK = (!isEmpty(easyGroupList) && !activeGroupSelection.isEmpty()); - boolean easyAreaOK = (!isEmpty(easyAreaList) && !activeAreaSelection.isEmpty()); + boolean easyGroupOK = (!isEmpty(easyGroupList) && activeGroupSelection != null && !activeGroupSelection.isEmpty()); + boolean easyAreaOK = (!isEmpty(easyAreaList) && activeAreaSelection != null && !activeAreaSelection.isEmpty()); if (easyGroupOK || easyAreaOK) { // clear general error groupSubContainer.clearError(); diff --git a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java index 69586259288e2cd6d7ed39cb6cdba0b213e3257b..88b1d2b1cb2e32f8c6df98793b454cb11af81c2d 100644 --- a/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java +++ b/src/main/java/org/olat/course/groupsandrights/PersistingCourseGroupManager.java @@ -211,7 +211,7 @@ public class PersistingCourseGroupManager extends BasicManager implements Course public boolean existGroup(String nameOrKey) { SearchBusinessGroupParams params = new SearchBusinessGroupParams(); if(StringHelper.isLong(nameOrKey)) { - params.setKey(new Long(nameOrKey)); + params.setKeys(Collections.singletonList(new Long(nameOrKey))); }else { params.setExactName(nameOrKey); } diff --git a/src/main/java/org/olat/course/nodes/ENCourseNode.java b/src/main/java/org/olat/course/nodes/ENCourseNode.java index 12e4c1d58aa8c08ab3bba97da819abbdd4e1e354..f20617d0476c21c35d39c8a81feb33b8eb07e8c4 100644 --- a/src/main/java/org/olat/course/nodes/ENCourseNode.java +++ b/src/main/java/org/olat/course/nodes/ENCourseNode.java @@ -89,9 +89,15 @@ public class ENCourseNode extends AbstractAccessableCourseNode { /** CONFIG_GROUPNAME configuration parameter key. */ public static final String CONFIG_GROUPNAME = "groupname"; + /** CONFIG_GROUPNAME configuration parameter key. */ + public static final String CONFIG_GROUP_IDS = "groupkeys"; /** CONFIG_AREANAME configuration parameter key. */ public static final String CONFIG_AREANAME = "areaname"; + /** CONFIG_AREANAME configuration parameter key. */ + public static final String CONFIG_AREA_IDS = "areakeys"; + + /** CONF_CANCEL_ENROLL_ENABLED configuration parameter key. */ public static final String CONF_CANCEL_ENROLL_ENABLED = "cancel_enroll_enabled"; diff --git a/src/main/java/org/olat/course/nodes/co/COConfigForm.java b/src/main/java/org/olat/course/nodes/co/COConfigForm.java index db5d28557baae8cf996be4e31f58a142b5aada46..0aeb63031f826ae9d964eb1de40f61d006f85040 100644 --- a/src/main/java/org/olat/course/nodes/co/COConfigForm.java +++ b/src/main/java/org/olat/course/nodes/co/COConfigForm.java @@ -310,8 +310,8 @@ public class COConfigForm extends FormBasicController { } } - boolean easyGroupOK = !activeGroupSelection.isEmpty(); - boolean easyAreaOK = !activeAreaSelection.isEmpty(); + boolean easyGroupOK = activeGroupSelection != null && !activeGroupSelection.isEmpty(); + boolean easyAreaOK = activeAreaSelection != null && !activeAreaSelection.isEmpty(); if (easyGroupOK || easyAreaOK) { // clear general error flc.clearError(); 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 73535600cada68825f33a688416b699144d82928..242f55897f1ad235e7e69cf771408df6bb512c1d 100644 --- a/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java +++ b/src/main/java/org/olat/course/nodes/en/ENEditGroupAreaFormController.java @@ -26,16 +26,18 @@ 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; import java.util.Set; +import org.olat.core.CoreSpringFactory; import org.olat.core.gui.UserRequest; import org.olat.core.gui.components.form.flexible.FormItem; import org.olat.core.gui.components.form.flexible.FormItemContainer; import org.olat.core.gui.components.form.flexible.elements.FormLink; import org.olat.core.gui.components.form.flexible.elements.MultipleSelectionElement; -import org.olat.core.gui.components.form.flexible.elements.TextElement; +import org.olat.core.gui.components.form.flexible.elements.StaticTextElement; import org.olat.core.gui.components.form.flexible.impl.FormBasicController; import org.olat.core.gui.components.form.flexible.impl.FormEvent; import org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer; @@ -59,6 +61,11 @@ import org.olat.course.condition.GroupSelectionController; import org.olat.course.editor.CourseEditorEnv; import org.olat.course.editor.NodeEditController; import org.olat.course.nodes.ENCourseNode; +import org.olat.group.BusinessGroup; +import org.olat.group.BusinessGroupService; +import org.olat.group.BusinessGroupShort; +import org.olat.group.area.BGArea; +import org.olat.group.area.BGAreaManager; import org.olat.group.ui.BGControllerFactory; import org.olat.group.ui.NewAreaController; import org.olat.group.ui.NewBGController; @@ -79,11 +86,11 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener private CourseEditorEnv cev; private MultipleSelectionElement enableCancelEnroll; - private TextElement easyGroupTE; + private StaticTextElement easyGroupList; private FormLink chooseGroupsLink; private FormLink createGroupsLink; - private TextElement easyAreaTE; + private StaticTextElement easyAreaList; private FormLink chooseAreasLink; private FormLink createAreasLink; @@ -105,12 +112,18 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener private OLATResourceable groupConfigChangeEventOres; private CloseableModalController cmc; + + private final BGAreaManager areaManager; + private final BusinessGroupService businessGroupService; public ENEditGroupAreaFormController(UserRequest ureq, WindowControl wControl, ModuleConfiguration moduleConfig, CourseEditorEnv cev) { super(ureq, wControl, null); Translator pT = Util.createPackageTranslator(Condition.class, ureq.getLocale(), getTranslator()); this.setTranslator(pT); + areaManager = CoreSpringFactory.getImpl(BGAreaManager.class); + businessGroupService = CoreSpringFactory.getImpl(BusinessGroupService.class); + singleUserEventCenter = ureq.getUserSession().getSingleUserEventCenter(); groupConfigChangeEventOres = OresHelper.createOLATResourceableType(MultiUserEvent.class); singleUserEventCenter.registerFor(this, ureq.getIdentity(), groupConfigChangeEventOres); @@ -118,9 +131,9 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener this.moduleConfig = moduleConfig; this.cev = cev; - hasAreas = cev.getCourseGroupManager().getAllAreasFromAllContexts().size() > 0; - hasGroups = cev.getCourseGroupManager().getAllLearningGroupsFromAllContexts().size() > 0; - + hasAreas = areaManager.countBGAreasInContext(cev.getCourseGroupManager().getCourseResource()) > 0; + hasGroups = businessGroupService.countBusinessGroups(null, cev.getCourseGroupManager().getCourseResource()) > 0; + initForm(ureq); } @@ -137,36 +150,27 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener */ @Override protected void formOK(UserRequest ureq) { - - easyGroupTE.setValue( - StringHelper.formatAsSortUniqCSVString( - Arrays.asList(easyGroupTE.getValue().split(",")) - ) - ); - - easyAreaTE.setValue( - StringHelper.formatAsSortUniqCSVString( - Arrays.asList(easyAreaTE.getValue().split(",")) - ) - ); - // 1. group names + List<Long> groupKeys = getKeys(easyGroupList); String groupName = ""; - if (StringHelper.containsNonWhitespace(easyGroupTE.getValue())){ - groupName = easyGroupTE.getValue(); + if (StringHelper.containsNonWhitespace(easyGroupList.getValue())){ + groupName = easyGroupList.getValue(); } moduleConfig.set(ENCourseNode.CONFIG_GROUPNAME, groupName); + moduleConfig.set(ENCourseNode.CONFIG_GROUP_IDS, groupKeys); // 2. area names + List<Long> areaKeys = getKeys(easyAreaList); String areaName = ""; - if (StringHelper.containsNonWhitespace(easyAreaTE.getValue())){ - areaName = easyAreaTE.getValue(); + if (StringHelper.containsNonWhitespace(easyAreaList.getValue())){ + areaName = easyAreaList.getValue(); } moduleConfig.set(ENCourseNode.CONFIG_AREANAME, areaName); - // 3. chnacel-enroll-enabled flag + moduleConfig.set(ENCourseNode.CONFIG_AREA_IDS, areaKeys); + // 3. cancel-enroll-enabled flag Boolean cancelEnrollEnabled = enableCancelEnroll.getSelectedKeys().size()==1; moduleConfig.set(ENCourseNode.CONF_CANCEL_ENROLL_ENABLED, cancelEnrollEnabled); - hasAreas = cev.getCourseGroupManager().getAllAreasFromAllContexts().size() > 0; - hasGroups = cev.getCourseGroupManager().getAllLearningGroupsFromAllContexts().size() > 0; + hasAreas = areaManager.countBGAreasInContext(cev.getCourseGroupManager().getCourseResource()) > 0; + hasGroups = businessGroupService.countBusinessGroups(null, cev.getCourseGroupManager().getCourseResource()) > 0; // Inform all listeners about the changed condition fireEvent(ureq, NodeEditController.NODECONFIG_CHANGED_EVENT); } @@ -181,19 +185,25 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener "groupChooseSubContainer", getTranslator() ); groupChooseSubContainer.setLabel("form.groupnames", null); - formLayout.add(groupChooseSubContainer); + formLayout.add(groupChooseSubContainer); - String groupInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); - if (groupInitVal != null) { - groupInitVal = StringHelper.formatAsSortUniqCSVString( - Arrays.asList(((String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME)).split(",")) - ); + String groupInitVal; + @SuppressWarnings("unchecked") + List<Long> groupKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_GROUP_IDS); + if(groupKeys != null) { + groupInitVal = getGroupNames(groupKeys); + } else { + groupInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); + if(StringHelper.containsNonWhitespace(groupInitVal)) { + groupInitVal = StringHelper.formatAsSortUniqCSVString(Arrays.asList(groupInitVal.split(","))); + groupKeys = getGroupKeys(groupInitVal); + } else { + groupKeys = new ArrayList<Long>(); + } } - - easyGroupTE = uifactory.addTextElement("group", null, 1024, groupInitVal, groupChooseSubContainer); - easyGroupTE.setDisplaySize(24); - easyGroupTE.setExampleKey("form.groupnames.example", null); - + easyGroupList = uifactory.addStaticTextElement("group", null, groupInitVal == null ? "" : groupInitVal, groupChooseSubContainer); + easyGroupList.setUserObject(groupKeys); + chooseGroupsLink = uifactory.addFormLink("choose", groupChooseSubContainer,"b_form_groupchooser"); createGroupsLink = uifactory.addFormLink("create", groupChooseSubContainer,"b_form_groupchooser"); hasGroups = cev.getCourseGroupManager().getAllLearningGroupsFromAllContexts().size() > 0; @@ -205,23 +215,27 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener groupsAndAreasSubContainer = (FormItemContainer) FormLayoutContainer.createHorizontalFormLayout("groupSubContainer", getTranslator()); formLayout.add(groupsAndAreasSubContainer); - - String areaInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); - if (areaInitVal != null) { - areaInitVal = StringHelper.formatAsSortUniqCSVString( - Arrays.asList(((String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME)).split(",")) - ); + + String areaInitVal; + @SuppressWarnings("unchecked") + List<Long> areaKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_AREA_IDS); + if(areaKeys != null) { + areaInitVal = getAreaNames(areaKeys); + } else { + areaInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); + if(StringHelper.containsNonWhitespace(areaInitVal)) { + areaInitVal = StringHelper.formatAsSortUniqCSVString(Arrays.asList(areaInitVal.split(","))); + areaKeys = this.getAreaKeys(areaInitVal); + } else { + areaKeys = new ArrayList<Long>(); + } } - - easyAreaTE = uifactory.addTextElement("area", null, 1024, areaInitVal, areaChooseSubContainer); - easyAreaTE.setDisplaySize(24); - easyAreaTE.setExampleKey("form.areanames.example", null); + easyAreaList = uifactory.addStaticTextElement("area", null, areaInitVal == null ? "" : areaInitVal, areaChooseSubContainer); + easyAreaList.setUserObject(areaKeys); chooseAreasLink = uifactory.addFormLink("choose", areaChooseSubContainer,"b_form_groupchooser"); createAreasLink = uifactory.addFormLink("create", areaChooseSubContainer,"b_form_groupchooser"); - - hasAreas = cev.getCourseGroupManager().getAllAreasFromAllContexts().size() > 0; - + // enrolment Boolean initialCancelEnrollEnabled = (Boolean) moduleConfig.get(ENCourseNode.CONF_CANCEL_ENROLL_ENABLED); enableCancelEnroll = uifactory.addCheckboxesHorizontal("enableCancelEnroll", "form.enableCancelEnroll", formLayout, new String[] { "ison" }, new String[] { "" }, null); @@ -240,57 +254,56 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener private boolean validateGroupFields () { boolean retVal = true; - String[] activeGroupSelection = new String[0]; - String[] activeAreaSelection = new String[0]; + List<Long> activeGroupSelection = null; + List<Long> activeAreaSelection = null; - if (!easyGroupTE.isEmpty()) { + if (!isEmpty(easyGroupList)) { // check whether groups exist - activeGroupSelection = easyGroupTE.getValue().split(","); - boolean exists = false; - Set<String> missingGroups = new HashSet<String>(); - - for (int i = 0; i < activeGroupSelection.length; i++) { - String trimmed = activeGroupSelection[i].trim(); - exists = cev.existsGroup(trimmed); - if (!exists && trimmed.length() > 0 && !missingGroups.contains(trimmed)) { - missingGroups.add(trimmed); + activeGroupSelection = getKeys(easyGroupList); + + Set<Long> missingGroups = new HashSet<Long>(); + List<BusinessGroupShort> existingGroups = businessGroupService.loadShortBusinessGroups(activeGroupSelection); + a_a: + for(Long activeGroupKey:activeGroupSelection) { + for(BusinessGroupShort group:existingGroups) { + if(group.getKey().equals(activeGroupKey)) { + continue a_a; + } } + missingGroups.add(activeGroupKey); } if (missingGroups.size() > 0) { retVal = false; String labelKey = missingGroups.size() == 1 ? "error.notfound.name" : "error.notfound.names"; - String csvMissGrps = StringHelper.formatAsCSVString(missingGroups); + String csvMissGrps = toString(missingGroups); String[] params = new String[] { "-", csvMissGrps }; - /* - * create error with link to fix it - */ + // create error with link to fix it String vc_errorPage = velocity_root + "/erroritem.html"; FormLayoutContainer errorGroupItemLayout = FormLayoutContainer.createCustomFormLayout( - "errorgroupitem", getTranslator(), vc_errorPage - ); - - groupChooseSubContainer.setErrorComponent(errorGroupItemLayout, this.flc); - // FIXING LINK ONLY IF A DEFAULTCONTEXT EXISTS - fixGroupError = new FormLinkImpl("error.fix", "create"); - // link - fixGroupError.setCustomEnabledLinkCSS("b_button"); - errorGroupItemLayout.add(fixGroupError); - - fixGroupError.setErrorKey(labelKey, params); - fixGroupError.showError(true); - fixGroupError.showLabel(false); - // hinty to pass the information if one group is - // missing or if 2 or more groups are missing - // (see fixGroupErrer.getUserObject to understand) - // e.g. if userobject String[].lenght == 1 -> one group only - // String[].lenght > 1 -> show bulkmode creation group - if (missingGroups.size() > 1) { - fixGroupError.setUserObject(new String[] { csvMissGrps, "dummy" }); - } else { - fixGroupError.setUserObject(new String[] { csvMissGrps }); - } + "errorgroupitem", getTranslator(), vc_errorPage); + + groupChooseSubContainer.setErrorComponent(errorGroupItemLayout, this.flc); + // FIXING LINK ONLY IF A DEFAULTCONTEXT EXISTS + fixGroupError = new FormLinkImpl("error.fix", "create"); + // link + fixGroupError.setCustomEnabledLinkCSS("b_button"); + errorGroupItemLayout.add(fixGroupError); + + fixGroupError.setErrorKey(labelKey, params); + fixGroupError.showError(true); + fixGroupError.showLabel(false); + // hinty to pass the information if one group is + // missing or if 2 or more groups are missing + // (see fixGroupErrer.getUserObject to understand) + // e.g. if userobject String[].lenght == 1 -> one group only + // String[].lenght > 1 -> show bulkmode creation group + if (missingGroups.size() > 1) { + fixGroupError.setUserObject(new String[] { csvMissGrps, "dummy" }); + } else { + fixGroupError.setUserObject(new String[] { csvMissGrps }); + } groupChooseSubContainer.showError(true); } else { @@ -300,67 +313,69 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } - if (!easyAreaTE.isEmpty()) { + if (!isEmpty(easyAreaList)) { // check whether areas exist - activeAreaSelection = easyAreaTE.getValue().split(","); + activeAreaSelection = getKeys(easyAreaList); - List<String> activeAreaList = new ArrayList<String>(); - for (int i=activeAreaSelection.length; i-->0; ) { - activeAreaList.add(activeAreaSelection[i].trim()); + List<Long> missingAreas = new ArrayList<Long>(); + List<BGArea> cnt = areaManager.loadAreas(activeAreaSelection); + a_a: + for(Long activeAreaKey:activeAreaSelection) { + for (BGArea element : cnt) { + if (element.getKey().equals(activeAreaKey)) { + continue a_a; + } + } + missingAreas.add(activeAreaKey); } - List<String> missingAreas = cev.validateAreas(activeAreaList); + if (missingAreas.size() > 0) { retVal = false; String labelKey = missingAreas.size() == 1 ? "error.notfound.name" : "error.notfound.names"; - String csvMissAreas = StringHelper.formatAsCSVString(missingAreas); + String csvMissAreas = toString(missingAreas); String[] params = new String[] { "-", csvMissAreas }; - /* - * create error with link to fix it - */ + // create error with link to fix it String vc_errorPage = velocity_root + "/erroritem.html"; FormLayoutContainer errorAreaItemLayout = FormLayoutContainer.createCustomFormLayout( - "errorareaitem", getTranslator(), vc_errorPage - ); - + "errorareaitem", getTranslator(), vc_errorPage); - areaChooseSubContainer.setErrorComponent(errorAreaItemLayout, this.flc); - // FXINGIN LINK ONLY IF DEFAULT CONTEXT EXISTS - fixAreaError = new FormLinkImpl("error.fix", "create");// erstellen - // link - fixAreaError.setCustomEnabledLinkCSS("b_button"); - errorAreaItemLayout.add(fixAreaError); - - fixAreaError.setErrorKey(labelKey, params); - fixAreaError.showError(true); - fixAreaError.showLabel(false); - - // hint to pass the information if one area is - // missing or if 2 or more areas are missing - // (see fixGroupErrer.getUserObject to understand) - // e.g. if userobject String[].lenght == 1 -> one group only - // String[].lenght > 1 -> show bulkmode creation group - if (missingAreas.size() > 1) { - fixAreaError.setUserObject(new String[] { csvMissAreas, "dummy" }); - } else { - fixAreaError.setUserObject(new String[] { csvMissAreas }); - } + areaChooseSubContainer.setErrorComponent(errorAreaItemLayout, this.flc); + // FXINGIN LINK ONLY IF DEFAULT CONTEXT EXISTS + fixAreaError = new FormLinkImpl("error.fix", "create");// erstellen + // link + fixAreaError.setCustomEnabledLinkCSS("b_button"); + errorAreaItemLayout.add(fixAreaError); + + fixAreaError.setErrorKey(labelKey, params); + fixAreaError.showError(true); + fixAreaError.showLabel(false); + + // hint to pass the information if one area is + // missing or if 2 or more areas are missing + // (see fixGroupErrer.getUserObject to understand) + // e.g. if userobject String[].lenght == 1 -> one group only + // String[].lenght > 1 -> show bulkmode creation group + if (missingAreas.size() > 1) { + fixAreaError.setUserObject(new String[] { csvMissAreas, "dummy" }); + } else { + fixAreaError.setUserObject(new String[] { csvMissAreas }); + } - areaChooseSubContainer.showError(true); } else { areaChooseSubContainer.clearError(); } } - boolean easyGroupOK = (!easyGroupTE.isEmpty() && activeGroupSelection.length != 0); - boolean easyAreaOK = (!easyAreaTE.isEmpty() && activeAreaSelection.length != 0); + boolean easyGroupOK = activeGroupSelection != null && activeGroupSelection.size() > 0; + boolean easyAreaOK = activeAreaSelection != null && activeAreaSelection.size() > 0; if (easyGroupOK || easyAreaOK) { // clear general error - this.flc.clearError(); + flc.clearError(); } else { // error concerns both fields -> set it as switch error - this.flc.setErrorKey("form.noGroupsOrAreas", null); + flc.setErrorKey("form.noGroupsOrAreas", null); retVal = false; } @@ -372,19 +387,15 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener groupChooseSubContainer.clearError(); groupsAndAreasSubContainer.clearError(); } - return retVal; } - @Override protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) { - if (source == chooseGroupsLink) { - removeAsListenerAndDispose(groupChooseC); groupChooseC = new GroupSelectionController(ureq, getWindowControl(), "group", - cev.getCourseGroupManager(), null /* easyGroupTE.getValue() */);//TODO gm + cev.getCourseGroupManager(), getKeys(easyGroupList)); listenTo(groupChooseC); removeAsListenerAndDispose(cmc); @@ -398,66 +409,43 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener subm.setEnabled(false); } else if (source == createGroupsLink) { - // no groups in group management -> directly show group create dialog - String[] csvGroupName = easyGroupTE.isEmpty() ? new String[0] : easyGroupTE.getValue().split(","); - - OLATResource courseResource = this.cev.getCourseGroupManager().getCourseResource(); + removeAsListenerAndDispose(cmc); removeAsListenerAndDispose(groupCreateCntrllr); - groupCreateCntrllr = BGControllerFactory.getInstance().createNewBGController( - ureq, getWindowControl(), - true, courseResource, - true, easyGroupTE.getValue() - ); + // no groups in group management -> directly show group create dialog + + OLATResource courseResource = cev.getCourseGroupManager().getCourseResource(); + groupCreateCntrllr = new NewBGController(ureq, getWindowControl(), true, courseResource, true, null); listenTo(groupCreateCntrllr); - removeAsListenerAndDispose(cmc); - cmc = new CloseableModalController( - getWindowControl(), "close", - groupCreateCntrllr.getInitialComponent() - ); + cmc = new CloseableModalController(getWindowControl(), "close", groupCreateCntrllr.getInitialComponent()); listenTo(cmc); - cmc.activate(); subm.setEnabled(false); - } else if (source == chooseAreasLink) { + removeAsListenerAndDispose(cmc); + removeAsListenerAndDispose(areaChooseC); // already areas -> choose areas - removeAsListenerAndDispose(areaChooseC); areaChooseC = new AreaSelectionController (ureq, getWindowControl() , "area", - cev.getCourseGroupManager(), null /* easyAreaTE.getValue() */);//TODO gm + cev.getCourseGroupManager(), getKeys(easyAreaList)); listenTo(areaChooseC); - - - removeAsListenerAndDispose(cmc); - cmc = new CloseableModalController( - getWindowControl(), "close", - areaChooseC.getInitialComponent() - ); + + cmc = new CloseableModalController(getWindowControl(), "close", areaChooseC.getInitialComponent()); listenTo(cmc); - cmc.activate(); subm.setEnabled(false); - } else if (source == createAreasLink) { - // no areas -> directly show creation dialog + removeAsListenerAndDispose(cmc); removeAsListenerAndDispose(areaCreateCntrllr); + // no areas -> directly show creation dialog OLATResource courseResource = cev.getCourseGroupManager().getCourseResource(); - areaCreateCntrllr = BGControllerFactory.getInstance().createNewAreaController( - ureq, getWindowControl(), courseResource, true, easyAreaTE.getValue() - ); + areaCreateCntrllr = new NewAreaController(ureq, getWindowControl(), courseResource, true, null); listenTo(areaCreateCntrllr); - removeAsListenerAndDispose(cmc); - cmc = new CloseableModalController( - getWindowControl(), "close", - areaCreateCntrllr.getInitialComponent() - ); + cmc = new CloseableModalController(getWindowControl(), "close", areaCreateCntrllr.getInitialComponent()); listenTo(cmc); - cmc.activate(); subm.setEnabled(false); - } else if (source == fixGroupError) { /* * user wants to fix problem with fixing group error link e.g. create one @@ -465,7 +453,7 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener */ String[] csvGroupName = (String[]) fixGroupError.getUserObject(); - easyGroupTE.setEnabled(false); + easyGroupList.setEnabled(false); removeAsListenerAndDispose(groupCreateCntrllr); OLATResource courseResource = this.cev.getCourseGroupManager().getCourseResource(); groupCreateCntrllr = BGControllerFactory.getInstance().createNewBGController( @@ -491,7 +479,7 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener */ String[] csvAreaName = (String[]) fixAreaError.getUserObject(); - easyAreaTE.setEnabled(false); + easyAreaList.setEnabled(false); removeAsListenerAndDispose(areaCreateCntrllr); OLATResource courseResource = this.cev.getCourseGroupManager().getCourseResource(); areaCreateCntrllr = BGControllerFactory.getInstance().createNewAreaController( @@ -512,59 +500,42 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } } - @Override - @SuppressWarnings("unused") protected void event(UserRequest ureq, Controller source, Event event) { - subm.setEnabled(true); - if (source == groupChooseC) { if (event == Event.DONE_EVENT) { cmc.deactivate(); - easyGroupTE.setValue(StringHelper.formatAsSortUniqCSVString(groupChooseC.getSelectedEntries())); - easyGroupTE.getRootForm().submit(ureq); - + easyGroupList.setValue(StringHelper.formatAsSortUniqCSVString(groupChooseC.getSelectedNames())); + easyGroupList.setUserObject(groupChooseC.getSelectedKeys()); + easyGroupList.getRootForm().submit(ureq); } else if (Event.CANCELLED_EVENT == event) { cmc.deactivate(); - return; - - } else if (event == Event.CHANGED_EVENT && !hasGroups){ - //singleUserEventCenter.fireEventToListenersOf(new MultiUserEvent("changed"), groupConfigChangeEventOres); - //why? fireEvent(ureq, new BGContextEvent(BGContextEvent.RESOURCE_ADDED,getDefaultBGContext())); } - } else if (source == areaChooseC) { if (event == Event.DONE_EVENT) { cmc.deactivate(); - easyAreaTE.setValue(StringHelper.formatAsSortUniqCSVString(areaChooseC.getSelectedEntries())); - easyAreaTE.getRootForm().submit(ureq); - + easyAreaList.setValue(StringHelper.formatAsSortUniqCSVString(areaChooseC.getSelectedNames())); + easyAreaList.setUserObject(areaChooseC.getSelectedKeys()); + easyAreaList.getRootForm().submit(ureq); } else if (event == Event.CANCELLED_EVENT) { cmc.deactivate(); - return; - - } else if (event == Event.CHANGED_EVENT && !hasAreas) { - //singleUserEventCenter.fireEventToListenersOf(new MultiUserEvent("changed"), groupConfigChangeEventOres); - //why? fireEvent(ureq, new BGContextEvent(BGContextEvent.RESOURCE_ADDED,getDefaultBGContext())); } - } else if (source == groupCreateCntrllr) { - - easyGroupTE.setEnabled(true); + easyGroupList.setEnabled(true); cmc.deactivate(); if (event == Event.DONE_EVENT) { - - List <String>c = new ArrayList<String>(); - c.addAll(Arrays.asList(easyGroupTE.getValue().split(","))); + List<Long> c = new ArrayList<Long>(); + c.addAll(getKeys(easyGroupList)); if (fixGroupError != null && fixGroupError.getUserObject() != null) { c.removeAll(Arrays.asList((String[])fixGroupError.getUserObject())); } - c.addAll (groupCreateCntrllr.getCreatedGroupNames()); + c.addAll(groupCreateCntrllr.getCreatedGroupKeys()); - easyGroupTE.setValue(StringHelper.formatAsSortUniqCSVString(c)); + easyGroupList.setValue(getGroupNames(c)); + easyGroupList.setUserObject(c); if (groupCreateCntrllr.getCreatedGroupNames().size() > 0 && !hasGroups) { chooseGroupsLink.setVisible(true); @@ -572,30 +543,28 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener singleUserEventCenter.fireEventToListenersOf(new MultiUserEvent("changed"), groupConfigChangeEventOres); } - easyGroupTE.getRootForm().submit(ureq); + easyGroupList.getRootForm().submit(ureq); } - } else if (source == areaCreateCntrllr) { - - easyAreaTE.setEnabled(true); + easyAreaList.setEnabled(true); cmc.deactivate(); - if (event == Event.DONE_EVENT) { - List <String>c = new ArrayList<String>(); - c.addAll(Arrays.asList(easyAreaTE.getValue().split(","))); + List<Long> c = new ArrayList<Long>(); + c.addAll(getKeys(easyAreaList)); if (fixAreaError != null && fixAreaError.getUserObject() != null) { c.removeAll(Arrays.asList((String[])fixAreaError.getUserObject())); } - c.addAll (areaCreateCntrllr.getCreatedAreaNames()); + c.addAll(areaCreateCntrllr.getCreatedAreaKeys()); - easyAreaTE.setValue(StringHelper.formatAsSortUniqCSVString(c)); + easyAreaList.setValue(getAreaNames(c)); + easyAreaList.setUserObject(c); if (areaCreateCntrllr.getCreatedAreaNames().size() > 0 && !hasAreas) { chooseAreasLink.setVisible(true); createAreasLink.setVisible(false); singleUserEventCenter.fireEventToListenersOf(new MultiUserEvent("changed"), groupConfigChangeEventOres); } - easyAreaTE.getRootForm().submit(ureq); + easyAreaList.getRootForm().submit(ureq); } } } @@ -613,13 +582,101 @@ class ENEditGroupAreaFormController extends FormBasicController implements Gener } private void updateGroupsAndAreasCheck() { - - hasGroups = cev.getCourseGroupManager().getAllLearningGroupsFromAllContexts().size() > 0; + hasGroups = businessGroupService.countBusinessGroups(null, cev.getCourseGroupManager().getCourseResource()) > 0; chooseGroupsLink.setVisible(hasGroups); createGroupsLink.setVisible(!hasGroups); - hasAreas = cev.getCourseGroupManager().getAllAreasFromAllContexts().size() > 0; + hasAreas = areaManager.countBGAreasInContext(cev.getCourseGroupManager().getCourseResource()) > 0; chooseAreasLink.setVisible(hasAreas); createAreasLink.setVisible(!hasAreas); } + + private boolean isEmpty(StaticTextElement element) { + List<Long> keys = getKeys(element); + if(keys == null || keys.isEmpty()) { + return true; + } + return false; + } + + private List<Long> getKeys(StaticTextElement element) { + @SuppressWarnings("unchecked") + List<Long> keys = (List<Long>)element.getUserObject(); + if(keys == null) { + keys = new ArrayList<Long>(); + element.setUserObject(keys); + } + return keys; + } + + private String toString(Collection<Long> keys) { + StringBuilder sb = new StringBuilder(); + for(Long key:keys) { + if(sb.length() > 0) sb.append(','); + sb.append(key); + } + return sb.toString(); + } + + private String getGroupNames(List<Long> keys) { + StringBuilder sb = new StringBuilder(); + List<BusinessGroupShort> groups = businessGroupService.loadShortBusinessGroups(keys); + for(BusinessGroupShort group:groups) { + if(sb.length() > 0) sb.append(", "); + sb.append(group.getName()); + } + return sb.toString(); + } + + private String getAreaNames(List<Long> keys) { + StringBuilder sb = new StringBuilder(); + for(Long key:keys) { + BGArea area = areaManager.loadArea(key); + if(area != null) { + if(sb.length() > 0) sb.append(", "); + sb.append(area.getName()); + } + } + return sb.toString(); + } + + private List<Long> getGroupKeys(String groupNames) { + List<Long> groupKeys = new ArrayList<Long>(); + if(StringHelper.containsNonWhitespace(groupNames)) { + String[] groupNameArr = groupNames.split(","); + List<BusinessGroup> groups = cev.getCourseGroupManager().getAllLearningGroupsFromAllContexts(); + for(String groupName:groupNameArr) { + groupName = groupName.trim(); + for(BusinessGroup group:groups) { + if(groupName.equalsIgnoreCase(group.getName())) { + groupKeys.add(group.getKey()); + break; + } + } + } + } + return groupKeys; + } + + private List<Long> getAreaKeys(String areaNames) { + List<Long> areaKeys = new ArrayList<Long>(); + if(StringHelper.containsNonWhitespace(areaNames)) { + List<BGArea> areas = cev.getCourseGroupManager().getAllAreasFromAllContexts(); + String[] areaNameArr = areaNames.split(","); + StringBuilder sb = new StringBuilder(); + for(String areaName:areaNameArr) { + areaName = areaName.trim(); + for(BGArea area:areas) { + if(areaName.equalsIgnoreCase(area.getName())) { + if(sb.length() > 0) { + sb.append(','); + } + sb.append(area.getKey()); + break; + } + } + } + } + return areaKeys; + } } diff --git a/src/main/java/org/olat/course/nodes/en/ENRunController.java b/src/main/java/org/olat/course/nodes/en/ENRunController.java index 9ff1819252e2fb2d5752c2fb2eb387002e7b9478..3fff6497749fb9685ec38ff0aa27b7afce50193c 100644 --- a/src/main/java/org/olat/course/nodes/en/ENRunController.java +++ b/src/main/java/org/olat/course/nodes/en/ENRunController.java @@ -26,7 +26,6 @@ package org.olat.course.nodes.en; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.olat.core.CoreSpringFactory; @@ -46,6 +45,7 @@ import org.olat.core.gui.control.controller.BasicController; import org.olat.core.id.Identity; import org.olat.core.logging.OLog; import org.olat.core.logging.Tracing; +import org.olat.core.util.StringHelper; import org.olat.core.util.coordinate.CoordinatorManager; import org.olat.core.util.event.GenericEventListener; import org.olat.core.util.resource.OLATResourceableJustBeforeDeletedEvent; @@ -55,6 +55,7 @@ import org.olat.course.nodes.ObjectivesHelper; import org.olat.course.properties.CoursePropertyManager; import org.olat.course.run.userview.UserCourseEnvironment; import org.olat.group.BusinessGroup; +import org.olat.group.area.BGArea; import org.olat.group.ui.BusinessGroupTableModelWithMaxSize; import org.olat.modules.ModuleConfiguration; import org.olat.util.logging.activity.LoggingResourceable; @@ -79,7 +80,8 @@ public class ENRunController extends BasicController implements GenericEventList private ModuleConfiguration moduleConfig; - private List enrollableGroupNames, enrollableAreaNames; + private List<Long> enrollableGroupKeys; + private List<Long> enrollableAreaKeys; private VelocityContainer enrollVC; private ENCourseNode enNode; @@ -87,9 +89,9 @@ public class ENRunController extends BasicController implements GenericEventList private TableController tableCtr; // Managers - private EnrollmentManager enrollmentManager; - private CourseGroupManager courseGroupManager; - private CoursePropertyManager coursePropertyManager; + private final EnrollmentManager enrollmentManager; + private final CourseGroupManager courseGroupManager; + private final CoursePropertyManager coursePropertyManager; // workflow variables private BusinessGroup enrolledGroup; @@ -115,23 +117,39 @@ public class ENRunController extends BasicController implements GenericEventList //this.trans = new PackageTranslator(PACKAGE, ureq.getLocale()); // init managers enrollmentManager = CoreSpringFactory.getImpl(EnrollmentManager.class); - this.courseGroupManager = userCourseEnv.getCourseEnvironment().getCourseGroupManager(); - this.coursePropertyManager = userCourseEnv.getCourseEnvironment().getCoursePropertyManager(); + courseGroupManager = userCourseEnv.getCourseEnvironment().getCourseGroupManager(); + coursePropertyManager = userCourseEnv.getCourseEnvironment().getCoursePropertyManager(); // Get groupnames from configuration - String groupNamesConfig = (String) moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); - String areaNamesConfig = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); - this.enrollableGroupNames = splitNames(groupNamesConfig); - this.enrollableAreaNames = splitNames(areaNamesConfig); + enrollableGroupKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_GROUP_IDS); + if(enrollableGroupKeys == null) { + String groupNamesConfig = (String)moduleConfig.get(ENCourseNode.CONFIG_GROUPNAME); + if(StringHelper.containsNonWhitespace(groupNamesConfig)) { + enrollableGroupKeys = getGroupKeys(groupNamesConfig); + } else { + enrollableGroupKeys = new ArrayList<Long>(); + } + } + + enrollableAreaKeys = (List<Long>)moduleConfig.get(ENCourseNode.CONFIG_AREA_IDS); + if(enrollableAreaKeys != null) { + String areaInitVal = (String) moduleConfig.get(ENCourseNode.CONFIG_AREANAME); + if(StringHelper.containsNonWhitespace(areaInitVal)) { + enrollableAreaKeys = getAreaKeys(areaInitVal); + } else { + enrollableAreaKeys = new ArrayList<Long>(); + } + } + cancelEnrollEnabled = ((Boolean) moduleConfig.get(ENCourseNode.CONF_CANCEL_ENROLL_ENABLED)).booleanValue(); Identity identity = userCourseEnv.getIdentityEnvironment().getIdentity(); - enrolledGroup = enrollmentManager.getBusinessGroupWhereEnrolled(identity, this.enrollableGroupNames, this.enrollableAreaNames, courseGroupManager); - waitingListGroup = enrollmentManager.getBusinessGroupWhereInWaitingList(identity, this.enrollableGroupNames, this.enrollableAreaNames, courseGroupManager); - registerGroupChangedEvents(enrollableGroupNames,enrollableAreaNames, courseGroupManager, ureq.getIdentity()); + enrolledGroup = enrollmentManager.getBusinessGroupWhereEnrolled(identity, enrollableGroupKeys, enrollableAreaKeys, courseGroupManager); + waitingListGroup = enrollmentManager.getBusinessGroupWhereInWaitingList(identity, enrollableGroupKeys, enrollableAreaKeys, courseGroupManager); + registerGroupChangedEvents(enrollableGroupKeys, enrollableAreaKeys, ureq.getIdentity()); // Set correct view enrollVC = createVelocityContainer("enrollmultiple"); - List groups = enrollmentManager.loadGroupsFromNames(this.enrollableGroupNames, this.enrollableAreaNames, courseGroupManager); + List<BusinessGroup> groups = enrollmentManager.loadGroupsFromNames(enrollableGroupKeys, enrollableAreaKeys, courseGroupManager); tableCtr = createTableController(ureq, enrollmentManager.hasAnyWaitingList(groups)); @@ -175,7 +193,7 @@ public class ENRunController extends BasicController implements GenericEventList if (actionid.equals(CMD_ENROLL_IN_GROUP)) { log.debug("CMD_ENROLL_IN_GROUP ureq.getComponentID()=" + ureq.getComponentID() + " ureq.getComponentTimestamp()=" + ureq.getComponentTimestamp()); EnrollStatus enrollStatus = enrollmentManager.doEnroll(ureq.getIdentity(), choosenGroup, enNode, coursePropertyManager, getWindowControl(), getTranslator(), - enrollableGroupNames, enrollableAreaNames, courseGroupManager); + enrollableGroupKeys, enrollableAreaKeys, courseGroupManager); if (enrollStatus.isEnrolled() ) { enrolledGroup = choosenGroup; } else if (enrollStatus.isInWaitingList() ) { @@ -209,7 +227,6 @@ public class ENRunController extends BasicController implements GenericEventList public void event(Event event) { if (event instanceof OLATResourceableJustBeforeDeletedEvent) { - OLATResourceableJustBeforeDeletedEvent delEvent = (OLATResourceableJustBeforeDeletedEvent) event; dispose(); } } @@ -237,8 +254,8 @@ public class ENRunController extends BasicController implements GenericEventList private void doEnrollMultipleView(UserRequest ureq) { // 1. Fetch groups from database - List groups = enrollmentManager.loadGroupsFromNames(this.enrollableGroupNames, this.enrollableAreaNames, courseGroupManager); - List members = this.courseGroupManager.getNumberOfMembersFromGroups(groups); + List<BusinessGroup> groups = enrollmentManager.loadGroupsFromNames(enrollableGroupKeys, enrollableAreaKeys, courseGroupManager); + List<Integer> members = courseGroupManager.getNumberOfMembersFromGroups(groups); // 2. Build group list groupListModel = new BusinessGroupTableModelWithMaxSize(groups, members, getTranslator(), ureq.getIdentity(), cancelEnrollEnabled); tableCtr.setTableDataModel(groupListModel); @@ -274,23 +291,23 @@ public class ENRunController extends BasicController implements GenericEventList * @see org.olat.core.gui.control.DefaultController#doDispose(boolean) */ protected void doDispose() { - deregisterGroupChangedEvents(enrollableGroupNames,enrollableAreaNames, courseGroupManager); + deregisterGroupChangedEvents(enrollableGroupKeys, enrollableAreaKeys); } /* * Add as listener to BusinessGroups so we are being notified about changes. */ - private void registerGroupChangedEvents(List enrollableGroupNames, List enrollableAreaNames, CourseGroupManager courseGroupManager, Identity identity) { - List groups = enrollmentManager.loadGroupsFromNames(enrollableGroupNames, enrollableAreaNames, courseGroupManager); - for (Iterator iter = groups.iterator(); iter.hasNext();) { - CoordinatorManager.getInstance().getCoordinator().getEventBus().registerFor(this, identity, (BusinessGroup) iter.next()); + private void registerGroupChangedEvents(List<Long> enrollableGroupNames, List<Long> enrollableAreaNames, Identity identity) { + List<BusinessGroup> groups = enrollmentManager.loadGroupsFromNames(enrollableGroupNames, enrollableAreaNames, courseGroupManager); + for (BusinessGroup group: groups) { + CoordinatorManager.getInstance().getCoordinator().getEventBus().registerFor(this, identity, group); } } - private void deregisterGroupChangedEvents(List enrollableGroupNames, List enrollableAreaNames, CourseGroupManager courseGroupManager) { - List groups = enrollmentManager.loadGroupsFromNames(enrollableGroupNames, enrollableAreaNames, courseGroupManager); - for (Iterator iter = groups.iterator(); iter.hasNext();) { - CoordinatorManager.getInstance().getCoordinator().getEventBus().deregisterFor(this, (BusinessGroup) iter.next()); + private void deregisterGroupChangedEvents(List<Long> enrollableGroupNames, List<Long> enrollableAreaNames) { + List<BusinessGroup> groups = enrollmentManager.loadGroupsFromNames(enrollableGroupNames, enrollableAreaNames, courseGroupManager); + for (BusinessGroup group:groups) { + CoordinatorManager.getInstance().getCoordinator().getEventBus().deregisterFor(this, group); } } @@ -298,15 +315,44 @@ public class ENRunController extends BasicController implements GenericEventList ////////////////// // Helper Methods ////////////////// - private List splitNames(String namesList) { - List names = new ArrayList(); - if (namesList != null) { - String[] name = namesList.split(","); - for (int i = 0; i < name.length; i++) { - names.add(name[i].trim()); + private List<Long> getGroupKeys(String groupNames) { + List<Long> groupKeys = new ArrayList<Long>(); + if(StringHelper.containsNonWhitespace(groupNames)) { + String[] groupNameArr = groupNames.split(","); + List<BusinessGroup> groups = courseGroupManager.getAllLearningGroupsFromAllContexts(); + for(String groupName:groupNameArr) { + groupName = groupName.trim(); + for(BusinessGroup group:groups) { + if(groupName.equalsIgnoreCase(group.getName())) { + groupKeys.add(group.getKey()); + break; + } + } + } + } + return groupKeys; + } + + private List<Long> getAreaKeys(String areaNames) { + List<Long> areaKeys = new ArrayList<Long>(); + if(StringHelper.containsNonWhitespace(areaNames)) { + List<BGArea> areas = courseGroupManager.getAllAreasFromAllContexts(); + String[] areaNameArr = areaNames.split(","); + StringBuilder sb = new StringBuilder(); + for(String areaName:areaNameArr) { + areaName = areaName.trim(); + for(BGArea area:areas) { + if(areaName.equalsIgnoreCase(area.getName())) { + if(sb.length() > 0) { + sb.append(','); + } + sb.append(area.getKey()); + break; + } + } } } - return names; + return areaKeys; } } \ No newline at end of file diff --git a/src/main/java/org/olat/course/nodes/en/EnrollmentManager.java b/src/main/java/org/olat/course/nodes/en/EnrollmentManager.java index 032d3042d7c49378fd12d443797b264f2c2c337c..1b61b5a7bd388f42d90511a04a94e1b7b014a656 100644 --- a/src/main/java/org/olat/course/nodes/en/EnrollmentManager.java +++ b/src/main/java/org/olat/course/nodes/en/EnrollmentManager.java @@ -25,16 +25,12 @@ package org.olat.course.nodes.en; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.olat.basesecurity.BaseSecurity; -import org.olat.core.commons.persistence.DBFactory; import org.olat.core.gui.control.WindowControl; import org.olat.core.gui.translator.Translator; import org.olat.core.id.Identity; -import org.olat.core.logging.Tracing; import org.olat.core.manager.BasicManager; import org.olat.core.util.coordinate.CoordinatorManager; import org.olat.core.util.coordinate.SyncerExecutor; @@ -50,6 +46,8 @@ import org.olat.course.nodes.ENCourseNode; import org.olat.course.properties.CoursePropertyManager; import org.olat.group.BusinessGroup; import org.olat.group.BusinessGroupService; +import org.olat.group.area.BGAreaManager; +import org.olat.group.model.SearchBusinessGroupParams; import org.olat.group.ui.BGConfigFlags; import org.olat.group.ui.BGMailHelper; import org.olat.properties.Property; @@ -70,6 +68,8 @@ import org.springframework.stereotype.Service; @Service("enrollmentManager") public class EnrollmentManager extends BasicManager { + @Autowired + private BGAreaManager areaManager; @Autowired private BaseSecurity securityManager; @Autowired @@ -77,13 +77,14 @@ public class EnrollmentManager extends BasicManager { public EnrollStatus doEnroll(final Identity identity, final BusinessGroup group, final ENCourseNode enNode, final CoursePropertyManager coursePropertyManager, - final WindowControl wControl, final Translator trans, List groupNames, List areaNames, CourseGroupManager cgm) { + final WindowControl wControl, final Translator trans, List<Long> groupKeys, List<Long> areaKeys, CourseGroupManager cgm) { + final EnrollStatus enrollStatus = new EnrollStatus(); - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("doEnroll", this.getClass()); + if (isLogDebugEnabled()) logDebug("doEnroll"); // check if the user is already enrolled (user can be enrooled only in one group) - if ( ( getBusinessGroupWhereEnrolled( identity, groupNames, areaNames, cgm) == null) - && ( getBusinessGroupWhereInWaitingList( identity, groupNames, areaNames, cgm) == null) ) { - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("Identity is not enrolled identity=" + identity.getName() + " group=" + group.getName() , this.getClass()); + if ( ( getBusinessGroupWhereEnrolled( identity, groupKeys, areaKeys, cgm) == null) + && ( getBusinessGroupWhereInWaitingList( identity, groupKeys, areaKeys, cgm) == null) ) { + if (isLogDebugEnabled()) logDebug("Identity is not enrolled identity=" + identity.getName() + " group=" + group.getName()); // 1. Check if group has max size defined. If so check if group is full // o_clusterREVIEW cg please review it - also where does the group.getMaxParticipants().equals("") come from?? // and: why can't we just have a group here and a max participants count and an identity to enrol? @@ -93,14 +94,14 @@ public class EnrollmentManager extends BasicManager { //TODO gsync CoordinatorManager.getInstance().getCoordinator().getSyncer().doInSync(group, new SyncerExecutor(){ public void execute() { - Tracing.logInfo("doEnroll start: group="+OresHelper.createStringRepresenting(group), identity.getName(), EnrollmentManager.class); + logInfo("doEnroll start: group="+OresHelper.createStringRepresenting(group), identity.getName()); Codepoint.codepoint(EnrollmentManager.class, "doInSync1"); // 6_1_0-RC15: reload group object here another node might have changed this in the meantime BusinessGroup reloadedGroup = businessGroupService.loadBusinessGroup(group); if (reloadedGroup.getMaxParticipants() != null && !reloadedGroup.getMaxParticipants().equals("")) { int participantsCounter = securityManager.countIdentitiesOfSecurityGroup(reloadedGroup.getPartipiciantGroup()); - Tracing.logInfo("doEnroll - participantsCounter: " + participantsCounter + ", maxParticipants: " + reloadedGroup.getMaxParticipants().intValue(), identity.getName(), EnrollmentManager.class); + logInfo("doEnroll - participantsCounter: " + participantsCounter + ", maxParticipants: " + reloadedGroup.getMaxParticipants().intValue(), identity.getName()); if (participantsCounter >= reloadedGroup.getMaxParticipants().intValue()) { // already full, show error and updated choose page again if (!reloadedGroup.getWaitingListEnabled().booleanValue()) { @@ -114,28 +115,28 @@ public class EnrollmentManager extends BasicManager { boolean done = addUserToParticipantList(identity, reloadedGroup, enNode, coursePropertyManager, wControl, trans); Codepoint.codepoint(EnrollmentManager.class, "doInSync2"); enrollStatus.setIsEnrolled(done); - Tracing.logInfo("doEnroll - setIsEnrolled ", identity.getName(), EnrollmentManager.class); + logInfo("doEnroll - setIsEnrolled ", identity.getName()); } } else { - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("doEnroll beginTransaction", this.getClass()); + if (isLogDebugEnabled()) logDebug("doEnroll beginTransaction"); boolean done = addUserToParticipantList(identity, reloadedGroup, enNode, coursePropertyManager, wControl, trans); enrollStatus.setIsEnrolled(done); - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("doEnroll committed", this.getClass()); + if (isLogDebugEnabled()) logDebug("doEnroll committed"); } - Tracing.logInfo("doEnroll end", identity.getName(), EnrollmentManager.class); + logInfo("doEnroll end", identity.getName()); } });// end of doInSync Codepoint.codepoint(EnrollmentManager.class, "afterDoInSync"); } else { enrollStatus.setErrorMessage(trans.translate("error.group.already.enrolled")); } - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("doEnroll finished", this.getClass()); + if (isLogDebugEnabled()) logDebug("doEnroll finished"); return enrollStatus; } public void doCancelEnrollment(final Identity identity, final BusinessGroup enrolledGroup, final ENCourseNode enNode, final CoursePropertyManager coursePropertyManager, WindowControl wControl, Translator trans) { - if (Tracing.isDebugEnabled(this.getClass())) Tracing.logDebug("doCancelEnrollment", this.getClass()); + if (isLogDebugEnabled()) logDebug("doCancelEnrollment"); // 1. Remove group membership, fire events, do loggin etc. final BGConfigFlags flags = BGConfigFlags.createLearningGroupDefaultFlags(); //TODO gsync @@ -198,38 +199,41 @@ public class EnrollmentManager extends BasicManager { * @return BusinessGroup in which the identity is enrolled, null if identity * is nowhere enrolled. */ - protected BusinessGroup getBusinessGroupWhereEnrolled(Identity identity, List groupNames, List areaNames, CourseGroupManager cgm) { - Iterator iterator = groupNames.iterator(); + protected BusinessGroup getBusinessGroupWhereEnrolled(Identity identity, List<Long> groupKeys, List<Long> areaKeys, CourseGroupManager cgm) { // 1. check in groups - while (iterator.hasNext()) { - String groupName = (String) iterator.next(); - List groups = cgm.getParticipatingLearningGroupsFromAllContexts(identity, groupName); + if(groupKeys != null && !groupKeys.isEmpty()) { + SearchBusinessGroupParams params = new SearchBusinessGroupParams(); + params.setAttendee(true); + params.setIdentity(identity); + params.setKeys(groupKeys); + List<BusinessGroup> groups = businessGroupService.findBusinessGroups(params, cgm.getCourseResource(), 0, 1); if (groups.size() > 0) { - // Usually it is only possible to be in one group. However, - // theoretically the - // admin can put the user in a second enrollment group or the user could - // theoretically be in a second group context. For now, we only look for - // the first - // group. All groups found after the first one are disgarded. - return (BusinessGroup) groups.get(0); + // Usually it is only possible to be in one group. However, + // theoretically the + // admin can put the user in a second enrollment group or the user could + // theoretically be in a second group context. For now, we only look for + // the first + // group. All groups found after the first one are discarded. + return groups.get(0); } } // 2. check in areas - iterator = areaNames.iterator(); - while (iterator.hasNext()) { - String areaName = (String) iterator.next(); - List groups = cgm.getParticipatingLearningGroupsInAreaFromAllContexts(identity, areaName); - if (groups.size() > 0) { - // Usually it is only possible to be in one group. However, - // theoretically the - // admin can put the user in a second enrollment group or the user could - // theoretically be in a second group context. For now, we only look for - // the first - // group. All groups found after the first one are disgarded. - return (BusinessGroup) groups.get(0); + if(areaKeys != null && !areaKeys.isEmpty()) { + for (Long areaKey:areaKeys) { + String areaName = areaKey.toString();//TODO gm + List<BusinessGroup> groups = cgm.getParticipatingLearningGroupsInAreaFromAllContexts(identity, areaName); + if (groups.size() > 0) { + // Usually it is only possible to be in one group. However, + // theoretically the + // admin can put the user in a second enrollment group or the user could + // theoretically be in a second group context. For now, we only look for + // the first + // group. All groups found after the first one are discarded. + return groups.get(0); + } } } - return null; // + return null; } /** @@ -238,13 +242,13 @@ public class EnrollmentManager extends BasicManager { * @return true if this identity is any waiting-list group in this course that * has a name that is in the group names list */ - protected BusinessGroup getBusinessGroupWhereInWaitingList(Identity identity, List groupNames, List areaNames, CourseGroupManager cgm) { - List groups = loadGroupsFromNames(groupNames, areaNames, cgm); - BusinessGroup businessGroup; + protected BusinessGroup getBusinessGroupWhereInWaitingList(Identity identity, List<Long> groupKeys, List<Long> areaKeys, CourseGroupManager cgm) { + List<BusinessGroup> groups = loadGroupsFromNames(groupKeys, areaKeys, cgm); // loop over all business-groups - for (Iterator iter = groups.iterator(); iter.hasNext();) { - businessGroup = (BusinessGroup) iter.next(); - if (securityManager.isIdentityInSecurityGroup(identity, businessGroup.getWaitingGroup())) { return businessGroup; } + for (BusinessGroup businessGroup:groups) { + if (securityManager.isIdentityInSecurityGroup(identity, businessGroup.getWaitingGroup())) { + return businessGroup; + } } return null; } @@ -256,26 +260,13 @@ public class EnrollmentManager extends BasicManager { * not found it won't be in the list. So groupNames.size() can very * well by different than loadGroupsFromNames().size() */ - protected List loadGroupsFromNames(List groupNames, List areaNames, CourseGroupManager cgm) { - List groups = new ArrayList(); - // 1. add groups - Iterator iterator = groupNames.iterator(); - while (iterator.hasNext()) { - String groupName = (String) iterator.next(); - List mygroups = cgm.getLearningGroupsFromAllContexts(groupName); - for (Iterator it = mygroups.iterator(); it.hasNext();) { - BusinessGroup bg = (BusinessGroup) it.next(); - if (!groups.contains(bg)) groups.add(bg); - } - } + protected List<BusinessGroup> loadGroupsFromNames(List<Long> groupKeys, List<Long> areaKeys, CourseGroupManager cgm) { + List<BusinessGroup> groups = businessGroupService.loadBusinessGroups(groupKeys); + List<BusinessGroup> areaGroups = areaManager.findBusinessGroupsOfAreaKeys(areaKeys); // add groups from areas - iterator = areaNames.iterator(); - while (iterator.hasNext()) { - String areaName = (String) iterator.next(); - List mygroups = cgm.getLearningGroupsInAreaFromAllContexts(areaName); - for (Iterator it = mygroups.iterator(); it.hasNext();) { - BusinessGroup bg = (BusinessGroup) it.next(); - if (!groups.contains(bg)) groups.add(bg); + for (BusinessGroup areaGroup:areaGroups) { + if (!groups.contains(areaGroup)) { + groups.add(areaGroup); } } return groups; @@ -288,10 +279,11 @@ public class EnrollmentManager extends BasicManager { * @return true : YES, there are waiting-list<br> * false: NO, no waiting-list */ - protected boolean hasAnyWaitingList(List groups) { - for (Iterator iter = groups.iterator(); iter.hasNext();) { - BusinessGroup businessGroup = (BusinessGroup) iter.next(); - if (businessGroup.getWaitingListEnabled().booleanValue()) { return true; } + protected boolean hasAnyWaitingList(List<BusinessGroup> groups) { + for (BusinessGroup businessGroup :groups) { + if (businessGroup.getWaitingListEnabled().booleanValue()) { + return true; + } } return false; } diff --git a/src/main/java/org/olat/group/area/BGAreaManager.java b/src/main/java/org/olat/group/area/BGAreaManager.java index 60d26b1cfbfda74ed96c87a9bc5c98bfac973c8a..bdd251cd8965cd98de793ba3514df13a16501680 100644 --- a/src/main/java/org/olat/group/area/BGAreaManager.java +++ b/src/main/java/org/olat/group/area/BGAreaManager.java @@ -145,6 +145,7 @@ public interface BGAreaManager { * @return A list of business groups */ public List<BusinessGroup> findBusinessGroupsOfAreas(List<BGArea> areas); + public List<BusinessGroup> findBusinessGroupsOfAreaKeys(List<Long> areaKeys); /** * Searches for all business groups that are associated with the given diff --git a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java index d74f36ae77a62ec4c9a5c90b6755540e9c37f2a3..c612cbdd57cb7b6b7ea9984d35d5bd5a5cb6aac9 100644 --- a/src/main/java/org/olat/group/area/BGAreaManagerImpl.java +++ b/src/main/java/org/olat/group/area/BGAreaManagerImpl.java @@ -237,16 +237,21 @@ public class BGAreaManagerImpl extends BasicManager implements BGAreaManager { @Override public List<BusinessGroup> findBusinessGroupsOfAreas(List<BGArea> areas) { if(areas == null || areas.isEmpty()) return Collections.emptyList(); - - StringBuilder sb = new StringBuilder(); - sb.append("select distinct bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") - .append(" where bgarel.groupArea.key in (:areaKeys)"); - List<Long> areaKeys = new ArrayList<Long>(); for(BGArea area:areas) { areaKeys.add(area.getKey()); } + return findBusinessGroupsOfAreaKeys(areaKeys); + } + + @Override + public List<BusinessGroup> findBusinessGroupsOfAreaKeys(List<Long> areaKeys) { + if(areaKeys == null || areaKeys.isEmpty()) return Collections.emptyList(); + StringBuilder sb = new StringBuilder(); + sb.append("select distinct bgarel.businessGroup from ").append(BGtoAreaRelationImpl.class.getName()).append(" as bgarel ") + .append(" where bgarel.groupArea.key in (:areaKeys)"); + List<BusinessGroup> result = DBFactory.getInstance().getCurrentEntityManager() .createQuery(sb.toString(), BusinessGroup.class) .setParameter("areaKeys", areaKeys) diff --git a/src/main/java/org/olat/group/manager/BusinessGroupDAO.java b/src/main/java/org/olat/group/manager/BusinessGroupDAO.java index 8fb15c5f7f15e0471a0510cd0da16cfb20aefd4c..d64ebb1f728f60b6c533623e3ca3e8a2ea85c9d7 100644 --- a/src/main/java/org/olat/group/manager/BusinessGroupDAO.java +++ b/src/main/java/org/olat/group/manager/BusinessGroupDAO.java @@ -436,9 +436,9 @@ public class BusinessGroupDAO { query.append(")"); } - if(params.getKey() != null) { + if(params.getKeys() != null && !params.getKeys().isEmpty()) { where = where(query, where); - query.append("bgi.key=:id"); + query.append("bgi.key in (:groupKeys)"); } if(resource != null) { @@ -530,8 +530,8 @@ public class BusinessGroupDAO { if(params.isPublicGroup()) { dbq.setParameter("atDate", new Date(), TemporalType.TIMESTAMP); } - if(params.getKey() != null) { - dbq.setParameter("id", params.getKey()); + if(params.getKeys() != null && !params.getKeys().isEmpty()) { + dbq.setParameter("groupKeys", params.getKeys()); } if (resource != null) { diff --git a/src/main/java/org/olat/group/model/SearchBusinessGroupParams.java b/src/main/java/org/olat/group/model/SearchBusinessGroupParams.java index b54ad164d3df483ba8453bd30aa185503b9dd242..64ad1cf239c2d31d4d355ca7b6e14dbb994e1faf 100644 --- a/src/main/java/org/olat/group/model/SearchBusinessGroupParams.java +++ b/src/main/java/org/olat/group/model/SearchBusinessGroupParams.java @@ -38,7 +38,7 @@ public class SearchBusinessGroupParams { private List<String> types; private List<String> tools; private String nameOrDesc; - private Long key; + private List<Long> keys; private String name; private String exactName; private String description; @@ -95,12 +95,12 @@ public class SearchBusinessGroupParams { } } - public Long getKey() { - return key; + public List<Long> getKeys() { + return keys; } - public void setKey(Long key) { - this.key = key; + public void setKeys(List<Long> keys) { + this.keys = keys; } public String getName() { diff --git a/src/main/java/org/olat/group/ui/main/BGMainController.java b/src/main/java/org/olat/group/ui/main/BGMainController.java index cc973ed59c1bd8641853f6a37c4ef8169837a582..56e1ef1c8d6f7e27cceee1cbcf7e7616d824c842 100644 --- a/src/main/java/org/olat/group/ui/main/BGMainController.java +++ b/src/main/java/org/olat/group/ui/main/BGMainController.java @@ -724,7 +724,9 @@ public class BGMainController extends MainLayoutBasicController implements Activ String owner = searchController.getOwner(); SearchBusinessGroupParams params = new SearchBusinessGroupParams(); - params.setKey(id); + if(id != null) { + params.setKeys(Collections.singletonList(id)); + } params.setName(name); params.setDescription(description); params.setOwnerName(owner); diff --git a/src/main/java/org/olat/group/ui/main/BusinessGroupListController.java b/src/main/java/org/olat/group/ui/main/BusinessGroupListController.java index 9c481b2dda4f0e8a9f590c6143d8916351a95f42..2be31f338ebe2b4e98473e15d6b0a08190a1965f 100644 --- a/src/main/java/org/olat/group/ui/main/BusinessGroupListController.java +++ b/src/main/java/org/olat/group/ui/main/BusinessGroupListController.java @@ -277,7 +277,9 @@ public class BusinessGroupListController extends BasicController { String ownerName = event.getOwnerName(); SearchBusinessGroupParams params = new SearchBusinessGroupParams(); - params.setKey(id); + if(id != null) { + params.setKeys(Collections.singletonList(id)); + } params.setName(StringHelper.containsNonWhitespace(name) ? name : null); params.setDescription(StringHelper.containsNonWhitespace(description) ? description : null); params.setOwnerName(StringHelper.containsNonWhitespace(ownerName) ? ownerName : null);