Package org.olat.course.nodes.cal

Source Code of org.olat.course.nodes.cal.CourseCalendars

/**
* 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>
* frentix GmbH, Switzerland, http://www.frentix.com
* <p>
*/
package org.olat.course.nodes.cal;

import java.util.ArrayList;
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.Kalendar;
import org.olat.commons.calendar.model.KalendarConfig;
import org.olat.commons.calendar.ui.LinkProvider;
import org.olat.commons.calendar.ui.components.KalendarRenderWrapper;
import org.olat.core.gui.UserRequest;
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.course.run.calendar.CourseCalendarSubscription;
import org.olat.course.run.calendar.CourseLinkProviderController;
import org.olat.group.BusinessGroup;
import org.olat.repository.RepositoryManager;

public class CourseCalendars {

  private KalendarRenderWrapper courseKalendarWrapper;
  private List<KalendarRenderWrapper> calendars;

  public CourseCalendars(KalendarRenderWrapper courseKalendarWrapper, List<KalendarRenderWrapper> calendars) {
    this.courseKalendarWrapper = courseKalendarWrapper;
    this.calendars = calendars;
  }

  public List<KalendarRenderWrapper> getCalendars() {
    return calendars;
  }

  public void setCalendars(List<KalendarRenderWrapper> calendars) {
    this.calendars = calendars;
  }

  public KalendarRenderWrapper getCourseKalendarWrapper() {
    return courseKalendarWrapper;
  }

  public void setCourseKalendarWrapper(KalendarRenderWrapper courseKalendarWrapper) {
    this.courseKalendarWrapper = courseKalendarWrapper;
  }

  public Kalendar getKalendar() {
    return courseKalendarWrapper.getKalendar();
  }

  public CourseCalendarSubscription createSubscription(UserRequest ureq) {
    CourseCalendarSubscription calSubscription = new CourseCalendarSubscription(getKalendar(), ureq.getUserSession().getGuiPreferences());
    return calSubscription;
  }

  public static CourseCalendars createCourseCalendarsWrapper(UserRequest ureq, WindowControl wControl, OLATResourceable ores) {
    List<KalendarRenderWrapper> calendars = new ArrayList<KalendarRenderWrapper>();
    CalendarManager calendarManager = CalendarManagerFactory.getInstance().getCalendarManager();
    // add course calendar
    ICourse course = CourseFactory.loadCourse(ores);
    KalendarRenderWrapper 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, wControl);
    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<BusinessGroup> allGroups = cgm.getAllLearningGroupsFromAllContexts();
      addCalendars(ureq, allGroups, true, clpc, calendars);
      // right groups
      allGroups = cgm.getAllRightGroupsFromAllContexts();
      addCalendars(ureq, allGroups, true, clpc, calendars);
    } else {
      // learning groups
      List<BusinessGroup> ownerGroups = cgm.getOwnedLearningGroupsFromAllContexts(identity);
      addCalendars(ureq, ownerGroups, true, clpc, calendars);
      List<BusinessGroup> attendedGroups = cgm.getParticipatingLearningGroupsFromAllContexts(identity);
      for (BusinessGroup ownerGroup : ownerGroups) {
        if (attendedGroups.contains(ownerGroup)) attendedGroups.remove(ownerGroup);
      }
      addCalendars(ureq, attendedGroups, false, clpc, calendars);

      // right groups
      List<BusinessGroup> rightGroups = cgm.getParticipatingRightGroupsFromAllContexts(identity);
      addCalendars(ureq, rightGroups, false, clpc, calendars);
    }
    return new CourseCalendars(courseKalendarWrapper, calendars);
  }

  private static void addCalendars(UserRequest ureq, List<BusinessGroup> groups, boolean isOwner, LinkProvider linkProvider,
      List<KalendarRenderWrapper> calendars) {
    CollaborationToolsFactory collabFactory = CollaborationToolsFactory.getInstance();
    CalendarManager calendarManager = CalendarManagerFactory.getInstance().getCalendarManager();
    for (BusinessGroup bGroup : groups) {
      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);
    }
  }
}
TOP

Related Classes of org.olat.course.nodes.cal.CourseCalendars

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.