/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.
*/
package org.olat.course.nodes.co;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import org.olat.basesecurity.GroupRoles;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
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.controller.BasicController;
import org.olat.core.gui.control.generic.messages.MessageUIFactory;
import org.olat.core.gui.translator.Translator;
import org.olat.core.id.Identity;
import org.olat.core.util.StringHelper;
import org.olat.core.util.Util;
import org.olat.core.util.mail.ContactList;
import org.olat.core.util.mail.ContactMessage;
import org.olat.course.groupsandrights.CourseGroupManager;
import org.olat.course.run.userview.UserCourseEnvironment;
import org.olat.group.BusinessGroup;
import org.olat.group.BusinessGroupService;
import org.olat.group.area.BGAreaManager;
import org.olat.modules.ModuleConfiguration;
import org.olat.modules.co.ContactFormController;
import org.olat.repository.RepositoryService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Description:<BR/> Run controller for the contact form building block <P/>
*
* Initial Date: Oct 13, 2004
* @author gnaegi
* @author Dirk Furrer
*/
public class CORunController extends BasicController {
private ContactFormController coFoCtr;
private final CourseGroupManager cgm;
@Autowired
private BGAreaManager areaManager;
@Autowired
private BusinessGroupService businessGroupService;
@Autowired
private RepositoryService repositoryService;
/**
* Constructor for the contact form run controller
*
* @param moduleConfiguration
* @param ureq
* @param wControl
* @param coCourseNode
*/
public CORunController(ModuleConfiguration moduleConfiguration, UserRequest ureq, WindowControl wControl,
UserCourseEnvironment userCourseEnv) {
super(ureq, wControl);
cgm = userCourseEnv.getCourseEnvironment().getCourseGroupManager();
//set translator with fall back translator.
Translator fallback = Util.createPackageTranslator(ContactFormController.class, ureq.getLocale());
setTranslator(Util.createPackageTranslator(CORunController.class, ureq.getLocale(), fallback));
List<String> emailListConfig = moduleConfiguration.getList(COEditController.CONFIG_KEY_EMAILTOADRESSES, String.class);
String mSubject = (String) moduleConfiguration.get(COEditController.CONFIG_KEY_MSUBJECT_DEFAULT);
String mBody = (String) moduleConfiguration.get(COEditController.CONFIG_KEY_MBODY_DEFAULT);
// Adding learning objectives using a consumable panel. Will only be
// displayed on the first page
Boolean partipsCourseConfigured = moduleConfiguration.getBooleanEntry(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_COURSE);
Boolean partipsAllConfigured = moduleConfiguration.getBooleanEntry(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_ALL);
Boolean coachesAllConfigured = moduleConfiguration.getBooleanEntry(COEditController.CONFIG_KEY_EMAILTOCOACHES_ALL);
Boolean ownersConfigured = moduleConfiguration.getBooleanEntry(COEditController.CONFIG_KEY_EMAILTOOWNERS);
Stack<ContactList> contactLists = new Stack<ContactList>();
String participantGroupNames = (String)moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP);
List<Long> participantGroupKeys = moduleConfiguration.getList(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_GROUP_ID, Long.class);
if((participantGroupKeys == null || participantGroupKeys.isEmpty()) && StringHelper.containsNonWhitespace(participantGroupNames)) {
participantGroupKeys = businessGroupService.toGroupKeys(participantGroupNames, cgm.getCourseEntry());
}
String participantAreaNames = (String)moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA);
List<Long> participantAreaKeys = moduleConfiguration.getList(COEditController.CONFIG_KEY_EMAILTOPARTICIPANTS_AREA_ID, Long.class);
if((participantAreaKeys == null || participantAreaKeys.isEmpty()) && StringHelper.containsNonWhitespace(participantAreaNames)) {
participantAreaKeys = businessGroupService.toGroupKeys(participantAreaNames, cgm.getCourseEntry());
}
String coachGroupNames = (String)moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP);
List<Long> coachGroupKeys = moduleConfiguration.getList(COEditController.CONFIG_KEY_EMAILTOCOACHES_GROUP_ID, Long.class);
if((coachGroupKeys == null || coachGroupKeys.isEmpty()) && StringHelper.containsNonWhitespace(coachGroupNames)) {
coachGroupKeys = businessGroupService.toGroupKeys(coachGroupNames, cgm.getCourseEntry());
}
String coachAreaNames = (String)moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA);
List<Long> coachAreaKeys = moduleConfiguration.getList(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA_IDS, Long.class);
if((coachAreaKeys == null || coachAreaKeys.isEmpty()) && StringHelper.containsNonWhitespace(coachAreaNames)) {
coachAreaKeys = businessGroupService.toGroupKeys(coachAreaNames, cgm.getCourseEntry());
}
if(coachAreaNames != null){
ContactList cl = retrieveCoachesFromAreas(coachAreaKeys);
contactLists.push(cl);
}
if(coachGroupNames != null){
ContactList cl = retrieveCoachesFromGroups(coachGroupKeys);
contactLists.push(cl);
}
if(participantGroupNames != null){
ContactList cl = retrieveParticipantsFromGroups(participantGroupKeys);
contactLists.push(cl);
}
if(participantAreaNames != null){
ContactList cl = retrieveParticipantsFromAreas(participantAreaKeys);
contactLists.push(cl);
}
if (coachesAllConfigured != null && coachesAllConfigured.booleanValue()) {
ContactList cl = retrieveCoachesFromCourse();
contactLists.push(cl);
List<BusinessGroup> groups = cgm.getAllBusinessGroups();
List<Long> grp_keys = new ArrayList<Long>();
for(BusinessGroup group:groups){
grp_keys.add(group.getKey());
}
cl = retrieveCoachesFromGroups(grp_keys);
contactLists.push(cl);
cl = retrieveCoachesFromAreas(grp_keys);
contactLists.push(cl);
}
if (partipsAllConfigured != null && partipsAllConfigured.booleanValue()) {
ContactList cl = retrieveParticipantsFromCourse();
contactLists.push(cl);
List<BusinessGroup> groups = cgm.getAllBusinessGroups();
List<Long> grp_keys = new ArrayList<Long>();
for(BusinessGroup group:groups){
grp_keys.add(group.getKey());
}
cl = retrieveParticipantsFromGroups(grp_keys);
contactLists.push(cl);
cl = retrieveParticipantsFromAreas(grp_keys);
contactLists.push(cl);
}
if (partipsCourseConfigured != null && partipsCourseConfigured.booleanValue()){
ContactList cl = retrieveParticipantsFromCourse();
contactLists.push(cl);
}
if (ownersConfigured != null && ownersConfigured){
ContactList cl = retrieveOwnersFromCourse();
contactLists.push(cl);
}
String areaNames = (String) moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA);
@SuppressWarnings("unchecked")
List<Long> areaKeys = (List<Long>) moduleConfiguration.get(COEditController.CONFIG_KEY_EMAILTOCOACHES_AREA_IDS);
if(areaKeys == null && StringHelper.containsNonWhitespace(areaNames)) {
areaKeys = areaManager.toAreaKeys(areaNames, cgm.getCourseResource());
}
if (coachesAllConfigured != null && coachesAllConfigured.booleanValue()) {
ContactList cl = retrieveCoachesFromAreas(areaKeys);
contactLists.push(cl);
}
if (partipsAllConfigured != null && partipsAllConfigured.booleanValue()) {
ContactList cl = retrieveParticipantsFromAreas(areaKeys);
contactLists.push(cl);
}
// Adding contact form
if (emailListConfig != null) {
ContactList emailList = new ContactList(translate("recipients"));
for (Iterator<String> iter = emailListConfig.iterator(); iter.hasNext();) {
String email = iter.next();
emailList.add(email);
}
contactLists.push(emailList);
}
if (contactLists.size() > 0) {
ContactMessage cmsg = new ContactMessage(ureq.getIdentity());
while (!contactLists.empty()) {
ContactList cl = contactLists.pop();
cmsg.addEmailTo(cl);
}
cmsg.setBodyText(mBody);
cmsg.setSubject(mSubject);
coFoCtr = new ContactFormController(ureq, getWindowControl(), false, false, false, cmsg);
listenTo(coFoCtr);//dispose as this controller is disposed
putInitialPanel(coFoCtr.getInitialComponent());
} else { // no email adresses at all
String message = translate("error.msg.send.no.rcps");
Controller mCtr = MessageUIFactory.createInfoMessage(ureq, getWindowControl(), null, message);
listenTo(mCtr);//to be disposed as this controller gets disposed
putInitialPanel(mCtr.getInitialComponent());
}
}
private ContactList retrieveCoachesFromGroups(List<Long> groupKeys) {
List<Identity> coaches = new ArrayList<Identity>(new HashSet<Identity>(cgm.getCoachesFromBusinessGroups(groupKeys)));
ContactList cl = new ContactList(translate("form.message.chckbx.coaches"));
cl.addAllIdentites(coaches);
return cl;
}
private ContactList retrieveCoachesFromAreas(List<Long> areaKeys) {
List<Identity> coaches = cgm.getCoachesFromAreas(areaKeys);
Set<Identity> coachesWithoutDuplicates = new HashSet<Identity>(coaches);
coaches = new ArrayList<Identity>(coachesWithoutDuplicates);
ContactList cl = new ContactList(translate("form.message.chckbx.coaches"));
cl.addAllIdentites(coaches);
return cl;
}
private ContactList retrieveCoachesFromCourse() {
List<Identity> coaches = cgm.getCoaches();
ContactList cl = new ContactList(translate("form.message.chckbx.partips"));
cl.addAllIdentites(coaches);
return cl;
}
private ContactList retrieveParticipantsFromGroups(List<Long> groupKeys) {
List<Identity> participiants = cgm.getParticipantsFromBusinessGroups(groupKeys);
ContactList cl = new ContactList(translate("form.message.chckbx.partips"));
cl.addAllIdentites(participiants);
return cl;
}
private ContactList retrieveParticipantsFromCourse() {
List<Identity> participiants = cgm.getParticipants();
ContactList cl = new ContactList(translate("form.message.chckbx.partips"));
cl.addAllIdentites(participiants);
return cl;
}
private ContactList retrieveParticipantsFromAreas(List<Long> areaKeys) {
List<Identity> participiants = cgm.getParticipantsFromAreas(areaKeys);
ContactList cl = new ContactList(translate("form.message.chckbx.partips"));
cl.addAllIdentites(participiants);
return cl;
}
private ContactList retrieveOwnersFromCourse(){;
List<Identity> ownerList = repositoryService.getMembers(cgm.getCourseEntry(), GroupRoles.owner.name());
ContactList cl = new ContactList(translate("form.message.chckbx.owners"));
cl.addAllIdentites(ownerList);
return cl;
}
/**
* @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest,
* @see org.olat.core.gui.components.Component, @see org.olat.core.gui.control.Event)
*/
public void event(UserRequest ureq, Component source, Event event) {
// no components to listen to
}
/**
* @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
*/
protected void doDispose() {
//
}
}