From 4afb9903da9a94cc2cf3db703e02d89edac2aa86 Mon Sep 17 00:00:00 2001
From: lmihalkovic <laurent.michalkovic@frentix.com>
Date: Fri, 13 May 2016 11:37:55 +0200
Subject: [PATCH] OO-2007: reusing membership fragment (the fragment code was
 extracted here)

---
 .../olat/course/nodes/co/COConfigForm.java    | 540 +++---------------
 .../course/nodes/co/COEditController.java     |  51 +-
 2 files changed, 105 insertions(+), 486 deletions(-)

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 da8a85c0564..caeaca1cd44 100755
--- a/src/main/java/org/olat/course/nodes/co/COConfigForm.java
+++ b/src/main/java/org/olat/course/nodes/co/COConfigForm.java
@@ -31,32 +31,25 @@ import java.util.List;
 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.SelectionElement;
-import org.olat.core.gui.components.form.flexible.elements.SingleSelection;
-import org.olat.core.gui.components.form.flexible.elements.StaticTextElement;
 import org.olat.core.gui.components.form.flexible.elements.TextElement;
 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;
+import org.olat.core.gui.components.form.flexible.impl.IFormFragmentController;
+import org.olat.core.gui.components.form.flexible.impl.IFormFragmentHost;
 import org.olat.core.gui.components.form.flexible.impl.elements.FormSubmit;
 import org.olat.core.gui.control.Controller;
 import org.olat.core.gui.control.Event;
 import org.olat.core.gui.control.WindowControl;
-import org.olat.core.gui.control.generic.closablewrapper.CloseableModalController;
+import org.olat.core.gui.translator.Translator;
 import org.olat.core.util.StringHelper;
+import org.olat.core.util.Util;
 import org.olat.core.util.mail.MailHelper;
-import org.olat.course.condition.AreaSelectionController;
-import org.olat.course.condition.GroupSelectionController;
-import org.olat.course.editor.CourseEditorEnv;
+import org.olat.course.editor.formfragments.MembersSelectorFormFragment;
 import org.olat.course.run.userview.UserCourseEnvironment;
-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.modules.IModuleConfiguration;
 import org.olat.modules.ModuleConfiguration;
-import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * Description:<BR/> Configuration form for the contact form building block
@@ -75,43 +68,43 @@ public class COConfigForm extends FormBasicController {
 	private TextElement teElSubject;
 	private TextElement teArElBody;
 
-	private SelectionElement wantCoaches;
-	private SelectionElement wantParticipants;
-	
-	private SingleSelection coachesChoice;
-	private SingleSelection participantsChoice;
-
-	private StaticTextElement easyGroupParticipantsSelectionList;
-	private StaticTextElement easyAreaParticipantsSelectionList;
-	private StaticTextElement easyGroupCoachSelectionList;
-	private FormLink chooseGroupCoachesLink;
-	
-	private FormLink chooseGroupParticipantsLink;
-	
-	private FormLink chooseAreasCoachesLink;
-	private FormLink chooseAreasParticipantsLink;
-	
-	private StaticTextElement easyAreaCoachSelectionList;
-	
-	private AreaSelectionController areaChooseCoaches;
-	private GroupSelectionController groupChooseCoaches;
-	private AreaSelectionController areaChooseParticipants;
-	private GroupSelectionController groupChooseParticipants;
+	// --
+//	private SelectionElement wantCoaches;	
+//	private SingleSelection coachesChoice;	
+//	private FormLink chooseGroupCoachesLink;
+//	private GroupSelectionController groupChooseCoaches;
+//	private StaticTextElement easyGroupCoachSelectionList;
+//	private FormLink chooseAreasCoachesLink;
+//	private AreaSelectionController areaChooseCoaches;
+//	private StaticTextElement easyAreaCoachSelectionList;
+
+	// --
+//	private SelectionElement wantParticipants;
+//	private SingleSelection participantsChoice;
+//	private FormLink chooseGroupParticipantsLink;
+//	private GroupSelectionController groupChooseParticipants;
+//	private StaticTextElement easyGroupParticipantsSelectionList;
+//	private FormLink chooseAreasParticipantsLink;
+//	private AreaSelectionController areaChooseParticipants;
+//	private StaticTextElement easyAreaParticipantsSelectionList;
+	// --
+
+	private final MembersSelectorFormFragment membersFragment;
 	
 	private FormItemContainer recipentsContainer;	
 
 	private FormSubmit subm;
 	
-	private CloseableModalController cmc;
+//	private CloseableModalController cmc;
 	
 	private List<String> eList;
 	private ModuleConfiguration config;
-	private CourseEditorEnv cev;
+//	private CourseEditorEnv cev;
 	
-	@Autowired
-	private BGAreaManager areaManager;
-	@Autowired
-	private BusinessGroupService businessGroupService;
+//	@Autowired
+//	private BGAreaManager areaManager;
+//	@Autowired
+//	private BusinessGroupService businessGroupService;
 
 
 	/**
@@ -124,12 +117,19 @@ public class COConfigForm extends FormBasicController {
 	protected COConfigForm(UserRequest ureq, WindowControl wControl, ModuleConfiguration config, UserCourseEnvironment uce) {
 		super(ureq, wControl);
 		this.config = config;
-		this.cev = uce.getCourseEditorEnv();
+//		this.cev = uce.getCourseEditorEnv();
 		
+		this.membersFragment = new MembersSelectorFormFragment(ureq, wControl, uce);
+		registerFormFragment(membersFragment);	// register with parent for proper lifecycle handling
 		initForm(ureq);
 		this.validateFormLogic(ureq);
 	}
 
+	@Override
+	public void storeFormData(UserRequest ureq) {
+		this.membersFragment.storeConfiguration(ureq, IModuleConfiguration.fragment("emailTo", "", config));
+	}
+
 	/**
 	 * @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
 	 */
@@ -137,7 +137,7 @@ public class COConfigForm extends FormBasicController {
 	protected boolean validateFormLogic(UserRequest ureq) {
 		boolean isOK = true;
 		
-		if (!wantCoaches.isSelected(0) && !wantParticipants.isSelected(0) && !wantEmail.isSelected(0) && !wantOwners.isSelected(0)) {
+		if (!membersFragment.sendToCoaches() && !membersFragment.sendToPartips() && !wantEmail.isSelected(0) && !sendToOwners()) {
 			recipentsContainer.setErrorKey("no.recipents.specified", null);
 			isOK = false;
 		}
@@ -173,39 +173,10 @@ public class COConfigForm extends FormBasicController {
 				eList.add(eAd);
 			}
 		}
-		if(wantCoaches.isSelected(0)){
-			if(!coachesChoice.isOneSelected()){
-				coachesChoice.setErrorKey("error.no.choice.specified", null);
-				isOK = false;
-			}else{
-				coachesChoice.clearError();
-			}
-			if(coachesChoice.isSelected(2) &&(isEmpty(easyAreaCoachSelectionList)|| easyAreaCoachSelectionList == null)){
-				if(easyGroupCoachSelectionList.getValue() == null && isEmpty(easyGroupCoachSelectionList) || easyGroupCoachSelectionList.getValue().equals("")){
-					easyAreaCoachSelectionList.setErrorKey("error.no.group.specified", null);
-					easyGroupCoachSelectionList.setErrorKey("error.no.group.specified", null);
-					isOK = false;
-				}
-			}
-		}
-
-		if(wantParticipants.isSelected(0)){
-			if(!participantsChoice.isOneSelected()){
-				participantsChoice.setErrorKey("error.no.choice.specified", null);
-				isOK = false;
-			}else{
-				participantsChoice.clearError();
-			}
-			if(participantsChoice.isSelected(2) &&(isEmpty(easyAreaParticipantsSelectionList)|| easyAreaParticipantsSelectionList == null)){
-				if(easyGroupParticipantsSelectionList.getValue() == null && isEmpty(easyGroupParticipantsSelectionList)|| easyGroupParticipantsSelectionList.getValue().equals("")){
-					easyAreaParticipantsSelectionList.setErrorKey("error.no.group.specified", null);
-					easyGroupParticipantsSelectionList.setErrorKey("error.no.group.specified", null);
-					isOK = false;
-				}
-			}
-		}
-
-		return isOK & super.validateFormLogic(ureq);
+		
+		return isOK 
+				& membersFragment.validateFormLogic(ureq) 
+				& super.validateFormLogic(ureq);
 	}
 
 
@@ -232,114 +203,41 @@ public class COConfigForm extends FormBasicController {
 		return eList;
 	}
 
-	/**
-	 * returns the chosen groups, or null if no groups were chosen.
-	 * 
-	 * @return
-	 */
-	protected String getEmailGroupCoaches() {
-		if (!isEmpty(easyGroupCoachSelectionList) && wantCoaches.isSelected(0) && coachesChoice.isSelected(2)) {
-			return easyGroupCoachSelectionList.getValue();
-		}
-		return null;
-	}
-	
-	protected List<Long> getEmailGroupCoachesIds() {
-		if (!isEmpty(easyGroupCoachSelectionList) && wantCoaches.isSelected(0) && coachesChoice.isSelected(2)) {
-			return getKeys(easyGroupCoachSelectionList);
-		}
-		return null;
-	}
-	
-	protected String getEmailGroupParticipants() {
-		if (!isEmpty(easyGroupParticipantsSelectionList) && wantParticipants.isSelected(0)&& participantsChoice.isSelected(2)) {
-			return easyGroupParticipantsSelectionList.getValue();
-		}
-		return null;
-	}
-	
-	protected List<Long> getEmailGroupParticipantsIds() {
-		if (!isEmpty(easyGroupParticipantsSelectionList) && wantParticipants.isSelected(0)&& participantsChoice.isSelected(2)) {
-			return getKeys(easyGroupParticipantsSelectionList);
-		}
-		return null;
-	}
-
-	/**
-	 * returns the chosen learning areas, or null if no ares were chosen.
-	 */
-	protected String getEmailCoachesAreas() {
-		if(!isEmpty(easyAreaCoachSelectionList)&&wantCoaches.isSelected(0)&& coachesChoice.isSelected(2)) {
-			return easyAreaCoachSelectionList.getValue();
-		}
-		return null;
-	}
-	
-	protected List<Long> getEmailCoachesAreaIds() {
-		if(!isEmpty(easyAreaCoachSelectionList)&&wantCoaches.isSelected(0)&& coachesChoice.isSelected(2)) {
-			return getKeys(easyAreaCoachSelectionList);
-		}
-		return null;
-	}
-
-	protected String getEmailParticipantsAreas() {
-		if(!isEmpty(easyAreaParticipantsSelectionList)&& wantParticipants.isSelected(0)&& participantsChoice.isSelected(2)) {
-			return easyAreaParticipantsSelectionList.getValue();
-		}
-		return null;
-	}
-	
-	protected List<Long> getEmailParticipantsAreaIds() {
-		if(!isEmpty(easyAreaParticipantsSelectionList)&& wantParticipants.isSelected(0)&& participantsChoice.isSelected(2)) {
-			return getKeys(easyAreaParticipantsSelectionList);
-		}
-		return null;
-	}
-
-	protected boolean sendToPartips() {
-		return wantParticipants.isSelected(0);
-	}
-	
 	protected boolean sendToOwners() {
 		return wantOwners.isSelected(0);
 	}
 	
-	protected boolean sendToCoachesCourse(){
-		return coachesChoice.isSelected(1)&& wantCoaches.isSelected(0);
-	}
-	
-	protected boolean sendToCoachesAll(){
-		return coachesChoice.isSelected(0)&& wantCoaches.isSelected(0);
-	}
-	
-	protected boolean sendToCoachesGroup(){
-		return coachesChoice.isSelected(2) && wantCoaches.isSelected(0);
-	}
-	
-	protected boolean sendToParticipantsCourse(){
-		return participantsChoice.isSelected(1)&& wantParticipants.isSelected(0);
-	}
-	
-	protected boolean sendToParticipantsAll(){
-		return participantsChoice.isSelected(0)&& wantParticipants.isSelected(0);
-	}
-	
-	protected boolean sendToParticipantsGroup(){
-		return participantsChoice.isSelected(2) && wantParticipants.isSelected(0);
-	}
-	
 	@Override
 	protected void formOK(UserRequest ureq) {
 		fireEvent (ureq, Event.DONE_EVENT);
 	}
 
+	@Override
+	public IFormFragmentHost getFragmentHostInterface() {
+		return new IFormFragmentHost() {
+			final Translator parent = COConfigForm.this.getTranslator();
+			final Translator delegate = Util.createPackageTranslator(MembersSelectorFormFragment.class, parent.getLocale(), parent);
+			final IFormFragmentController adapter = IFormFragmentController.fragmentControllerAdapter(COConfigForm.this, canSubmit -> {
+				subm.setEnabled(canSubmit);
+			});
+
+			@Override
+			public Translator getFragmentTranslator() {
+				return delegate;
+			}
+
+			@Override
+			public IFormFragmentController getFragmentController() {
+				return adapter;
+			}
+		};
+	}
 
 	@Override
 	protected void initForm(FormItemContainer formLayout, Controller listener, UserRequest ureq) {
-	
-		
+
 		Boolean ownerSelection = config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOOWNERS);
-		Boolean coacheSelection = config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_ALL) || config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_COURSE) || config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP) != null || config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA) != null;
+//		Boolean coacheSelection = config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_ALL) || config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_COURSE) || config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP) != null || config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA) != null;
 
 		
 		setFormTitle("header", null);
@@ -372,143 +270,10 @@ public class COConfigForm extends FormBasicController {
 		
 		wantOwners.addActionListener(FormEvent.ONCLICK);
 		
+		// include existing fragment
+		IModuleConfiguration emailToFrag = IModuleConfiguration.fragment("emailTo", "", config);
+		membersFragment.initFormFragment(ureq, this, this, emailToFrag);
 
-		// COACHES: from course or groups
-		wantCoaches = uifactory.addCheckboxesHorizontal("coaches", "message.want.coaches", formLayout, new String[]{"xx"},new String[]{null});
-		if(coacheSelection != null && coacheSelection) wantCoaches.select("xx", true);
-
-		wantCoaches.addActionListener(FormEvent.ONCLICK);
-		
-		
-		coachesChoice = uifactory.addRadiosVertical(
-				"coachesChoice", null, formLayout, 
-				new String[]{"all", "course", "group"},
-				new String[]{translate("form.message.coaches.all"), translate("form.message.coaches.course"), translate("form.message.coaches.group")}
-		);
-		if(config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_ALL)) coachesChoice.select("all", true);
-		if(config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOCOACHES_COURSE)) coachesChoice.select("course", true);
-		if(config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP) != null || config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA) != null) coachesChoice.select("group", true);
-		coachesChoice.addActionListener(FormEvent.ONCLICK);
-		coachesChoice.setVisible(false);
-		
-		
-		chooseGroupCoachesLink = uifactory.addFormLink("groupCoachesChoose", formLayout, "btn btn-default o_xsmall o_form_groupchooser");
-		chooseGroupCoachesLink.setIconLeftCSS("o_icon o_icon-fw o_icon_group");
-		chooseGroupCoachesLink.setVisible(false);
-		chooseGroupCoachesLink.setLabel("form.message.group", null);
-		if(!cev.getCourseGroupManager().hasBusinessGroups()){
-			chooseGroupCoachesLink.setI18nKey("groupCreate");
-		}
-		
-		chooseGroupCoachesLink.setElementCssClass("o_omit_margin");
-
-		String groupCoachesInitVal;
-		@SuppressWarnings("unchecked")
-		List<Long> groupCoachesKeys = (List<Long>)config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP_ID);
-		if(groupCoachesKeys == null) {
-			groupCoachesInitVal = (String) config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP);
-			groupCoachesKeys = businessGroupService.toGroupKeys(groupCoachesInitVal, cev.getCourseGroupManager().getCourseEntry());
-		}
-		groupCoachesInitVal = getGroupNames(groupCoachesKeys);
-
-		easyGroupCoachSelectionList = uifactory.addStaticTextElement("groupCoaches", null, groupCoachesInitVal, formLayout);
-		easyGroupCoachSelectionList.setUserObject(groupCoachesKeys);		
-		easyGroupCoachSelectionList.setVisible(false);
-		easyGroupCoachSelectionList.setElementCssClass("text-muted");
-		
-				
-		chooseAreasCoachesLink = uifactory.addFormLink("areaCoachesChoose", formLayout, "btn btn-default o_xsmall o_form_areachooser");
-		chooseAreasCoachesLink.setIconLeftCSS("o_icon o_icon-fw o_icon_courseareas");
-		chooseAreasCoachesLink.setLabel("form.message.area", null);
-		chooseAreasCoachesLink.setElementCssClass("o_omit_margin");
-		if(!cev.getCourseGroupManager().hasAreas()){
-			chooseAreasCoachesLink.setI18nKey("areaCreate");
-		}
-		
-		String areaCoachesInitVal;
-		@SuppressWarnings("unchecked")
-		List<Long> areaCoachesKeys = (List<Long>)config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA_IDS);
-		if(areaCoachesKeys == null) {
-			areaCoachesInitVal = (String)config.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA);
-			areaCoachesKeys = areaManager.toAreaKeys(areaCoachesInitVal, cev.getCourseGroupManager().getCourseResource());
-		}
-		areaCoachesInitVal = getAreaNames(areaCoachesKeys);
-
-		easyAreaCoachSelectionList = uifactory.addStaticTextElement("areaCoaches", null, areaCoachesInitVal, formLayout);
-		easyAreaCoachSelectionList.setUserObject(areaCoachesKeys);
-		easyAreaCoachSelectionList.setVisible(false);
-		easyAreaCoachSelectionList.setElementCssClass("text-muted");
-				
-		
-		// PARTICIPANTS: from course or groups
-		Boolean particiapntSelection = config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_ALL) || config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE) || config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP) != null || config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA) != null;
-		
-		wantParticipants = uifactory.addCheckboxesHorizontal("participants", "message.want.participants", formLayout, new String[]{"xx"},new String[]{null});
-		if(particiapntSelection != null && particiapntSelection) wantParticipants.select("xx", true);
-		wantParticipants.addActionListener(FormEvent.ONCLICK);
-		
-		participantsChoice = uifactory.addRadiosVertical(
-				"participantsChoice", null, formLayout, 
-				new String[]{"all", "course", "group"},
-				new String[]{translate("form.message.participants.all"), translate("form.message.participants.course"), translate("form.message.participants.group")}
-		);
-		if(config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_ALL)) participantsChoice.select("all", true);
-		if(config.getBooleanSafe(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE)) participantsChoice.select("course", true);
-		if(config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP) != null || config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA) != null) participantsChoice.select("group", true);
-		participantsChoice.addActionListener(FormEvent.ONCLICK);
-		participantsChoice.setVisible(false); 
-		
-		chooseGroupParticipantsLink = uifactory.addFormLink("groupParticipantsChoose", formLayout, "btn btn-default o_xsmall o_form_groupchooser");
-		chooseGroupParticipantsLink.setIconLeftCSS("o_icon o_icon-fw o_icon_group");
-		chooseGroupParticipantsLink.setVisible(false);
-		chooseGroupParticipantsLink.setLabel("form.message.group", null);
-		chooseGroupParticipantsLink.setElementCssClass("o_omit_margin");
-
-		if(cev.getCourseGroupManager().getAllBusinessGroups().isEmpty()){
-			chooseGroupParticipantsLink.setI18nKey("groupCreate");
-		}
-		
-		String groupParticipantsInitVal;
-		@SuppressWarnings("unchecked")
-		List<Long> groupParticipantsKeys = (List<Long>)config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID);
-		if(groupParticipantsKeys == null) {
-			groupParticipantsInitVal = (String)config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP);
-			groupParticipantsKeys = businessGroupService.toGroupKeys(groupParticipantsInitVal, cev.getCourseGroupManager().getCourseEntry());
-		}
-		groupParticipantsInitVal = getGroupNames(groupParticipantsKeys);
-
-		easyGroupParticipantsSelectionList = uifactory.addStaticTextElement("groupParticipants", null, groupParticipantsInitVal, formLayout);
-		easyGroupParticipantsSelectionList.setUserObject(groupParticipantsKeys);
-		easyGroupParticipantsSelectionList.setVisible(false);
-		easyGroupParticipantsSelectionList.setElementCssClass("text-muted");
-		
-		
-		chooseAreasParticipantsLink = uifactory.addFormLink("areaParticipantsChoose", formLayout, "btn btn-default o_xsmall o_form_areachooser");
-		chooseAreasParticipantsLink.setIconLeftCSS("o_icon o_icon-fw o_icon_courseareas");
-		chooseAreasParticipantsLink.setVisible(false);
-		chooseAreasParticipantsLink.setLabel("form.message.area", null);
-		chooseAreasParticipantsLink.setElementCssClass("o_omit_margin");
-
-		if(cev.getCourseGroupManager().getAllAreas().isEmpty()){
-			chooseAreasParticipantsLink.setI18nKey("areaCreate");
-		}
-		
-		String areaParticipantsInitVal;
-		@SuppressWarnings("unchecked")
-		List<Long> areaParticipantsKeys = (List<Long>)config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID);
-		if(areaParticipantsKeys == null) {
-			areaParticipantsInitVal = (String)config.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA);
-			areaParticipantsKeys = areaManager.toAreaKeys(areaParticipantsInitVal, cev.getCourseGroupManager().getCourseResource());
-		}
-		areaParticipantsInitVal = getAreaNames(areaParticipantsKeys);
-
-		easyAreaParticipantsSelectionList = uifactory.addStaticTextElement("areaParticipants", null, areaParticipantsInitVal, formLayout);
-		easyAreaParticipantsSelectionList.setUserObject(areaParticipantsKeys);
-		easyAreaParticipantsSelectionList.setVisible(false);
-		easyAreaParticipantsSelectionList.setElementCssClass("text-muted");
-	
-		uifactory.addSpacerElement("s4", formLayout, false);
-				
 		//subject
 		String mS = (String) config.get(COEditController.CONFIG_KEY_MSUBJECT_DEFAULT);
 		String mSubject = (mS != null) ? mS : "";
@@ -526,17 +291,7 @@ public class COConfigForm extends FormBasicController {
 	}
 
 	private void update () {
-		coachesChoice.setVisible(wantCoaches.isSelected(0));
-		chooseGroupCoachesLink.setVisible(coachesChoice.isSelected(2) && wantCoaches.isSelected(0));
-		chooseAreasCoachesLink.setVisible(coachesChoice.isSelected(2) && wantCoaches.isSelected(0));
-		easyGroupCoachSelectionList.setVisible(coachesChoice.isSelected(2) && wantCoaches.isSelected(0));
-		easyAreaCoachSelectionList.setVisible(coachesChoice.isSelected(2) && wantCoaches.isSelected(0));
-		
-		participantsChoice.setVisible(wantParticipants.isSelected(0));
-		chooseGroupParticipantsLink.setVisible(participantsChoice.isSelected(2) && wantParticipants.isSelected(0));
-		chooseAreasParticipantsLink.setVisible(participantsChoice.isSelected(2) && wantParticipants.isSelected(0));
-		easyGroupParticipantsSelectionList.setVisible(participantsChoice.isSelected(2) && wantParticipants.isSelected(0));
-		easyAreaParticipantsSelectionList.setVisible(participantsChoice.isSelected(2) && wantParticipants.isSelected(0));
+		membersFragment.refreshContents();
 		
 		teArElEmailToAdresses.setVisible(wantEmail.isSelected(0));
 		teArElEmailToAdresses.clearError();
@@ -545,168 +300,29 @@ public class COConfigForm extends FormBasicController {
 			eList = null;
 		}
 		
-		easyGroupParticipantsSelectionList.clearError();
-		easyAreaParticipantsSelectionList.clearError();
-		easyGroupCoachSelectionList.clearError();
-		easyAreaCoachSelectionList.clearError();
-		
-		coachesChoice.clearError();
-		participantsChoice.clearError();
-		
 		recipentsContainer.clearError();
 		flc.setDirty(true);
 	}
 	
 	@Override
 	protected void formInnerEvent(UserRequest ureq, FormItem source, FormEvent event) {
-		if (source == chooseGroupCoachesLink) {
-			removeAsListenerAndDispose(cmc);
-			removeAsListenerAndDispose(groupChooseCoaches);
-
-			groupChooseCoaches = new GroupSelectionController(ureq, getWindowControl(), true,
-					cev.getCourseGroupManager(), getKeys(easyGroupCoachSelectionList));
-			listenTo(groupChooseCoaches);
-			
-
-			cmc = new CloseableModalController(getWindowControl(), "close", groupChooseCoaches.getInitialComponent());
-			listenTo(cmc);
-			cmc.activate();
-			subm.setEnabled(false);
-		} else if(source == chooseGroupParticipantsLink){
-			removeAsListenerAndDispose(cmc);
-			removeAsListenerAndDispose(groupChooseParticipants);
-			
-			groupChooseParticipants = new GroupSelectionController(ureq, getWindowControl(), true,
-					cev.getCourseGroupManager(), getKeys(easyGroupParticipantsSelectionList));
-			listenTo(groupChooseParticipants);
-			
-
-			cmc = new CloseableModalController(getWindowControl(), "close", groupChooseParticipants.getInitialComponent());
-			listenTo(cmc);
-			cmc.activate();
-			subm.setEnabled(false);
-		} else if (source == chooseAreasCoachesLink) {
-			// already areas -> choose areas
-			removeAsListenerAndDispose(cmc);
-			removeAsListenerAndDispose(areaChooseCoaches);
-			
-			areaChooseCoaches = new AreaSelectionController (ureq, getWindowControl(), true,
-					cev.getCourseGroupManager(), getKeys(easyAreaCoachSelectionList));
-			listenTo(areaChooseCoaches);
-
-			cmc = new CloseableModalController(getWindowControl(), "close", areaChooseCoaches.getInitialComponent());
-			listenTo(cmc);
-			cmc.activate();
-			subm.setEnabled(false);
-		} else if (source == chooseAreasParticipantsLink){
-			// already areas -> choose areas
-			removeAsListenerAndDispose(cmc);
-			removeAsListenerAndDispose(areaChooseParticipants);
-			
-			areaChooseParticipants = new AreaSelectionController (ureq, getWindowControl(), true,
-					cev.getCourseGroupManager(), getKeys(easyAreaParticipantsSelectionList));
-			listenTo(areaChooseParticipants);
-
-			cmc = new CloseableModalController(getWindowControl(), "close", areaChooseParticipants.getInitialComponent());
-			listenTo(cmc);
-			cmc.activate();
-			subm.setEnabled(false);
-		} 
-
+		/*boolean processed = */this.membersFragment.processFormEvent(ureq, source, event);
+		
 		update();
 	}
 	
 	@Override
 	protected void event(UserRequest ureq, Controller source, Event event) {
 		subm.setEnabled(true);
-		if (source == groupChooseCoaches) {
-			if (event == Event.DONE_EVENT) {
-				cmc.deactivate();
-				easyGroupCoachSelectionList.setValue(getGroupNames(groupChooseCoaches.getSelectedKeys()));
-				easyGroupCoachSelectionList.setUserObject(groupChooseCoaches.getSelectedKeys());
-				chooseGroupCoachesLink.setI18nKey("groupCoachesChoose");
-				flc.setDirty(true);
-			} else if (Event.CANCELLED_EVENT == event) {
-				cmc.deactivate();
-			}
-		} else if (source == areaChooseCoaches) {
-			if (event == Event.DONE_EVENT) {
-				cmc.deactivate();
-				easyAreaCoachSelectionList.setValue(getAreaNames(areaChooseCoaches.getSelectedKeys()));
-				easyAreaCoachSelectionList.setUserObject(areaChooseCoaches.getSelectedKeys());
-				chooseAreasCoachesLink.setI18nKey("areaCoachesChoose");
-				flc.setDirty(true);
-			} else if (event == Event.CANCELLED_EVENT) {
-				cmc.deactivate();
-			}
-		} else if (source == groupChooseParticipants) {
-			if (event == Event.DONE_EVENT) {
-				cmc.deactivate();
-				easyGroupParticipantsSelectionList.setValue(getGroupNames(groupChooseParticipants.getSelectedKeys()));
-				easyGroupParticipantsSelectionList.setUserObject(groupChooseParticipants.getSelectedKeys());
-				chooseGroupParticipantsLink.setI18nKey("groupParticipantsChoose");
-				flc.setDirty(true);
-			} else if (Event.CANCELLED_EVENT == event) {
-				cmc.deactivate();
-			}
-		} else if (source == areaChooseParticipants) {
-			if (event == Event.DONE_EVENT) {
-				cmc.deactivate();
-				easyAreaParticipantsSelectionList.setValue(getAreaNames(areaChooseParticipants.getSelectedKeys()));
-				easyAreaParticipantsSelectionList.setUserObject(areaChooseParticipants.getSelectedKeys());
-				chooseAreasParticipantsLink.setI18nKey("areaParticipantsChoose");
-				flc.setDirty(true);
-			} else if (event == Event.CANCELLED_EVENT) {
-				cmc.deactivate();
-			}
-		}
+
+		// the parent takes care of dealing with fragments
+		super.event(ureq, source, event);
 		
 	}
 
 	@Override
 	protected void doDispose() {
-		//
-	}
-	
-	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 getGroupNames(List<Long> keys) {
-		StringBuilder sb = new StringBuilder();
-		List<BusinessGroupShort> groups = businessGroupService.loadShortBusinessGroups(keys);
-		for(BusinessGroupShort group:groups) {
-			if(sb.length() > 0) sb.append("&nbsp;&nbsp;");
-			sb.append("<i class='o_icon o_icon-fw o_icon_group'>&nbsp;</i> ");
-			sb.append(StringHelper.escapeHtml(group.getName()));
-		}
-		return sb.toString();
+		membersFragment.dispose();
 	}
 	
-	private String getAreaNames(List<Long> keys) {
-		StringBuilder sb = new StringBuilder();
-		List<BGArea> areas = areaManager.loadAreas(keys);
-		for(BGArea area:areas) {
-			if(sb.length() > 0) sb.append("&nbsp;&nbsp;");
-			sb.append("<i class='o_icon o_icon-fw o_icon_courseareas'>&nbsp;</i> ");
-			sb.append(StringHelper.escapeHtml(area.getName()));
-		}
-		return sb.toString();
-	}
-
 }
\ No newline at end of file
diff --git a/src/main/java/org/olat/course/nodes/co/COEditController.java b/src/main/java/org/olat/course/nodes/co/COEditController.java
index cbc4ebd56a9..a1cbf5ff523 100755
--- a/src/main/java/org/olat/course/nodes/co/COEditController.java
+++ b/src/main/java/org/olat/course/nodes/co/COEditController.java
@@ -40,6 +40,7 @@ import org.olat.course.assessment.AssessmentHelper;
 import org.olat.course.condition.Condition;
 import org.olat.course.condition.ConditionEditController;
 import org.olat.course.editor.NodeEditController;
+import org.olat.course.editor.formfragments.MembersSelectorFormFragment;
 import org.olat.course.nodes.COCourseNode;
 import org.olat.course.run.userview.UserCourseEnvironment;
 import org.olat.modules.ModuleConfiguration;
@@ -65,29 +66,29 @@ public class COEditController extends ActivateableTabbableDefaultController impl
 	public static final String CONFIG_KEY_EMAILTOGROUPS = "emailToGroups";
 	
 	/** config key: to email addresses to be extracted from specified learn areas */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_AREA = "emailToAreaCoaches";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_AREA 			= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_AREA;
 	/** config key: to email addresses to be extracted from specified learn areas */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_AREA_IDS = "emailToAreaCoachesIds";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_AREA_IDS 		= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_AREA_IDS;
 	
 	/** config key: keys of the course participants list */
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_ALL = "emailToParticipantsAll";
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_ALL 		= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_ALL;
 	/** config key: keys of the group participants list */
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID = "emailToGroupParticipantsIds";
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID 	= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_GROUP_ID;
 	/** config key: email goes to group participants */
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP = "emailToGroupParticipants";
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID = "emailToAreaParticipantsIds";
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP 	= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_GROUP;
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID 	= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_AREA_ID;
 	/** config key: email goes to group participants */
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_AREA = "emailToAreaParticipants";
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_AREA 		= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_AREA;
 	/** config key: email goes to course participants */
-	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE = "emailToCourseParticipants";
+	public static final String CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE 	= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_PARTICIPANTS_COURSE;
 	/** config key: email goes to group coaches */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_GROUP = "emailToGroupCoaches";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_GROUP 			= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_GROUP;
 	/** config key: key of the group coaches list */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_GROUP_ID = "emailToGroupCoachesIds";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_GROUP_ID 		= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_GROUP_ID;
 	/** config key: key of the course coaches list */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_ALL = "emailtoCoachesAll";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_ALL 			= "emailto" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_ALL;
 	/** config key: email goes to course coaches */
-	public static final String CONFIG_KEY_EMAILTOCOACHES_COURSE = "emailToCourseCoaches";
+	public static final String CONFIG_KEY_EMAILTOCOACHES_COURSE 		= "emailTo" + MembersSelectorFormFragment.CONFIG_KEY_COACHES_COURSE;
 	/** config key: email goes to course owners */
 	public static final String CONFIG_KEY_EMAILTOOWNERS = "emailToOwners";
 	/** config key: email goes to email address */
@@ -162,19 +163,21 @@ public class COEditController extends ActivateableTabbableDefaultController impl
 			if (event == Event.CANCELLED_EVENT) {
 				return;
 			} else if (event == Event.DONE_EVENT) {
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_GROUP, configForm.getEmailGroupCoaches());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_AREA, configForm.getEmailCoachesAreas());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_GROUP_ID, configForm.getEmailGroupCoachesIds());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_AREA_IDS, configForm.getEmailCoachesAreaIds());
-				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOCOACHES_ALL, configForm.sendToCoachesAll());
-				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOCOACHES_COURSE, configForm.sendToCoachesCourse());
+				this.configForm.storeFormData(ureq);
+
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_GROUP, configForm.getEmailGroupCoaches());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_AREA, configForm.getEmailCoachesAreas());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_GROUP_ID, configForm.getEmailGroupCoachesIds());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOCOACHES_AREA_IDS, configForm.getEmailCoachesAreaIds());
+//				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOCOACHES_ALL, configForm.sendToCoachesAll());
+//				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOCOACHES_COURSE, configForm.sendToCoachesCourse());
 				
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP, configForm.getEmailGroupParticipants());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID, configForm.getEmailGroupParticipantsIds());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_AREA, configForm.getEmailParticipantsAreas());
-				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID, configForm.getEmailParticipantsAreaIds());
-				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE, configForm.sendToParticipantsCourse());
-				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOPARTICIPANTS_ALL, configForm.sendToParticipantsAll());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP, configForm.getEmailGroupParticipants());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID, configForm.getEmailGroupParticipantsIds());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_AREA, configForm.getEmailParticipantsAreas());
+//				moduleConfiguration.set(CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID, configForm.getEmailParticipantsAreaIds());
+//				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE, configForm.sendToParticipantsCourse());
+//				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOPARTICIPANTS_ALL, configForm.sendToParticipantsAll());
 				
 				moduleConfiguration.setBooleanEntry(CONFIG_KEY_EMAILTOOWNERS, configForm.sendToOwners());
 				moduleConfiguration.set(CONFIG_KEY_EMAILTOADRESSES, configForm.getEmailList());
-- 
GitLab