Package org.olat.course.run.calendar

Source Code of org.olat.course.run.calendar.CourseCalendarController

/**
* 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.
* <p>
*/

package org.olat.course.run.calendar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.olat.collaboration.CollaborationTools;
import org.olat.collaboration.CollaborationToolsFactory;
import org.olat.commons.calendar.CalendarManager;
import org.olat.commons.calendar.CalendarManagerFactory;
import org.olat.commons.calendar.model.KalendarConfig;
import org.olat.commons.calendar.ui.CalendarController;
import org.olat.commons.calendar.ui.LinkProvider;
import org.olat.commons.calendar.ui.WeeklyCalendarController;
import org.olat.commons.calendar.ui.components.KalendarRenderWrapper;
import org.olat.commons.calendar.ui.events.KalendarModifiedEvent;
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.DefaultController;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.id.Identity;
import org.olat.core.id.OLATResourceable;
import org.olat.course.CourseFactory;
import org.olat.course.ICourse;
import org.olat.course.groupsandrights.CourseGroupManager;
import org.olat.course.groupsandrights.CourseRights;
import org.olat.group.BusinessGroup;
import org.olat.repository.RepositoryManager;

public class CourseCalendarController extends DefaultController {

  private CalendarController calendarController;
  private KalendarRenderWrapper courseKalendarWrapper;
  private OLATResourceable ores;
 
  public CourseCalendarController(UserRequest ureq, WindowControl wControl, OLATResourceable course) {
    super(wControl);
    this.ores = course;
    List calendars = getListOfCalendarWrappers(ureq);
    CourseCalendarSubscription calendarSubscription = new CourseCalendarSubscription(
        courseKalendarWrapper.getKalendar(), ureq.getUserSession().getGuiPreferences());
    calendarController = new WeeklyCalendarController(
        ureq, wControl, calendars, WeeklyCalendarController.CALLER_COURSE, calendarSubscription);
    setInitialComponent(calendarController.getInitialComponent());
  }

  private List getListOfCalendarWrappers(UserRequest ureq) {
    List calendars = new ArrayList();
    CalendarManager calendarManager = CalendarManagerFactory.getInstance().getCalendarManager();
    // add course calendar
    ICourse course = CourseFactory.loadCourse(ores);
    courseKalendarWrapper = calendarManager.getCourseCalendar(course);
    CourseGroupManager cgm = course.getCourseEnvironment().getCourseGroupManager();
    Identity identity = ureq.getIdentity();
    boolean isPrivileged = cgm.isIdentityCourseAdministrator(identity) || cgm.hasRight(identity, CourseRights.RIGHT_COURSEEDITOR) || RepositoryManager.getInstance().isInstitutionalRessourceManagerFor(RepositoryManager.getInstance().lookupRepositoryEntry(course, false), identity);
    if (isPrivileged) {
      courseKalendarWrapper.setAccess(KalendarRenderWrapper.ACCESS_READ_WRITE);
    } else {
      courseKalendarWrapper.setAccess(KalendarRenderWrapper.ACCESS_READ_ONLY);
    }
    KalendarConfig config = calendarManager.findKalendarConfigForIdentity(courseKalendarWrapper.getKalendar(), ureq);
    if (config != null) {
      courseKalendarWrapper.getKalendarConfig().setCss(config.getCss());
      courseKalendarWrapper.getKalendarConfig().setVis(config.isVis());
    }
    // add link provider
    CourseLinkProviderController clpc = new CourseLinkProviderController(course, ureq, getWindowControl());
    courseKalendarWrapper.setLinkProvider(clpc);
    calendars.add(courseKalendarWrapper);
   
    // add course group calendars
    boolean isGroupManager = cgm.isIdentityCourseAdministrator(identity) || cgm.hasRight(identity, CourseRights.RIGHT_GROUPMANAGEMENT);
    if (isGroupManager) {
      // learning groups
      List allGroups = cgm.getAllLearningGroupsFromAllContexts();
      addCalendars(ureq, allGroups, true, clpc, calendars);
      // right groups
      allGroups = cgm.getAllRightGroupsFromAllContexts();
      addCalendars(ureq, allGroups, true, clpc, calendars);
    } else {
      // learning groups
      List ownerGroups = cgm.getOwnedLearningGroupsFromAllContexts(identity);
      addCalendars(ureq, ownerGroups, true, clpc, calendars);
      List attendedGroups = cgm.getParticipatingLearningGroupsFromAllContexts(identity);
      for (Iterator ownerGroupsIterator = ownerGroups.iterator(); ownerGroupsIterator.hasNext();) {
        BusinessGroup ownerGroup = (BusinessGroup) ownerGroupsIterator.next();
        if (attendedGroups.contains(ownerGroup))
          attendedGroups.remove(ownerGroup);
      }
      addCalendars(ureq, attendedGroups, false, clpc, calendars);

      // right groups
      List rightGroups = cgm.getParticipatingRightGroupsFromAllContexts(identity);
      addCalendars(ureq, rightGroups, false, clpc, calendars);
    }
    return calendars;
  }
 
  private void addCalendars(UserRequest ureq, List groups, boolean isOwner, LinkProvider linkProvider, List calendars) {
    CollaborationToolsFactory collabFactory = CollaborationToolsFactory.getInstance();
    CalendarManager calendarManager = CalendarManagerFactory.getInstance().getCalendarManager();
    for (Iterator iter = groups.iterator(); iter.hasNext();) {
      BusinessGroup bGroup = (BusinessGroup) iter.next();
      CollaborationTools collabTools = collabFactory.getOrCreateCollaborationTools(bGroup);
      if (!collabTools.isToolEnabled(CollaborationTools.TOOL_CALENDAR)) continue;
      KalendarRenderWrapper groupCalendarWrapper = calendarManager.getGroupCalendar(bGroup);
      // set calendar access
      int iCalAccess = CollaborationTools.CALENDAR_ACCESS_OWNERS;
      Long lCalAccess = collabTools.lookupCalendarAccess();
      if (lCalAccess != null) iCalAccess = lCalAccess.intValue();
      if (iCalAccess == CollaborationTools.CALENDAR_ACCESS_OWNERS && !isOwner) {
        groupCalendarWrapper.setAccess(KalendarRenderWrapper.ACCESS_READ_ONLY);
      } else {
        groupCalendarWrapper.setAccess(KalendarRenderWrapper.ACCESS_READ_WRITE);
      }
      KalendarConfig config = calendarManager.findKalendarConfigForIdentity(groupCalendarWrapper.getKalendar(), ureq);
      if (config != null) {
        groupCalendarWrapper.getKalendarConfig().setCss(config.getCss());
        groupCalendarWrapper.getKalendarConfig().setVis(config.isVis());
      }
      groupCalendarWrapper.setLinkProvider(linkProvider);
      calendars.add(groupCalendarWrapper);
    }
  }
 

  public void event(UserRequest ureq, Component source, Event event) {
    // nothing to do
  }

  public void event(UserRequest ureq, Controller source, Event event) {
    if (event instanceof KalendarModifiedEvent) {
      List calendars = getListOfCalendarWrappers(ureq);
      calendarController.setCalendars(calendars);
    }
  }

  protected void doDispose() {
    calendarController.dispose();
  }

}
TOP

Related Classes of org.olat.course.run.calendar.CourseCalendarController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.