Package org.olat.course.nodes.projectbroker

Source Code of org.olat.course.nodes.projectbroker.ProjectListController

/**
* 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.nodes.projectbroker;

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

import org.olat.core.commons.fullWebApp.popup.BaseFullWebappPopupLayoutFactory;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
import org.olat.core.gui.components.panel.Panel;
import org.olat.core.gui.components.table.BooleanColumnDescriptor;
import org.olat.core.gui.components.table.DefaultColumnDescriptor;
import org.olat.core.gui.components.table.TableController;
import org.olat.core.gui.components.table.TableEvent;
import org.olat.core.gui.components.table.TableGuiConfiguration;
import org.olat.core.gui.components.velocity.VelocityContainer;
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.creator.ControllerCreator;
import org.olat.core.id.Identity;
import org.olat.course.nodes.projectbroker.datamodel.CustomField;
import org.olat.course.nodes.projectbroker.datamodel.Project;
import org.olat.course.nodes.projectbroker.datamodel.ProjectBroker;
import org.olat.course.nodes.projectbroker.service.ProjectBrokerManagerFactory;
import org.olat.course.nodes.projectbroker.service.ProjectBrokerModuleConfiguration;
import org.olat.course.properties.CoursePropertyManager;
import org.olat.course.run.userview.NodeEvaluation;
import org.olat.course.run.userview.UserCourseEnvironment;
import org.olat.group.BusinessGroup;
import org.olat.user.UserInfoMainController;

/**
*
* @author guretzki
*
*/

public class ProjectListController extends BasicController {
 
  // List commands
  private static final String TABLE_ACTION_SHOW_DETAIL     = "cmd.show.detail";
  private static final String TABLE_ACTION_ACCOUNT_MANAGER = "cmd.account.manager";
  private static final String TABLE_ACTION_SELECT          = "cmd.select";
  private static final String TABLE_ACTION_CANCEL_SELECT   = "cmd.cancel.select";
 

  private VelocityContainer contentVC;
  private Panel mainPanel;
  private ProjectListTableModel projectListTableModel;
  private TableController tableController;
  private Controller projectController;

  private Link createNewProjectButton;

  private Long courseId;
  private UserCourseEnvironment userCourseEnv;
  private NodeEvaluation nodeEvaluation;
 
  private ProjectBrokerModuleConfiguration moduleConfig;
  private Long projectBrokerId;
  private int numberOfCustomFieldInTable = 0;
  private int numberOfEventInTable = 0;
  private int nbrSelectedProjects;
  private boolean isParticipantInAnyProject;


  /**
   * @param ureq
   * @param wControl
   * @param userCourseEnv
   * @param ne
   * @param previewMode
   */
  protected ProjectListController(UserRequest ureq, WindowControl wControl, UserCourseEnvironment userCourseEnv, NodeEvaluation ne, boolean previewMode) {
    super(ureq, wControl);
    this.userCourseEnv = userCourseEnv;
    this.nodeEvaluation = ne;
    courseId = userCourseEnv.getCourseEnvironment().getCourseResourceableId();
    moduleConfig = new ProjectBrokerModuleConfiguration(ne.getCourseNode().getModuleConfiguration());
   
    contentVC = createVelocityContainer("project_list");
    // set header info with project-broker run mode
    String infoProjectBrokerRunMode = "";
    if (moduleConfig.isAcceptSelectionManually() && moduleConfig.isAutoSignOut()) {
      infoProjectBrokerRunMode = translate("info.projectbroker.runmode.accept.manually.auto.sign.out", Integer.toString(moduleConfig.getNbrParticipantsPerTopic()) );
    } else if (moduleConfig.isAcceptSelectionManually()) {
      if (moduleConfig.getNbrParticipantsPerTopic() == ProjectBrokerModuleConfiguration.NBR_PARTICIPANTS_UNLIMITED) {
        infoProjectBrokerRunMode = translate("info.projectbroker.runmode.accept.manually.unlimited" );
      } else {
        infoProjectBrokerRunMode = translate("info.projectbroker.runmode.accept.manually.limited", Integer.toString(moduleConfig.getNbrParticipantsPerTopic()) );
      }
    } else {
      if (moduleConfig.getNbrParticipantsPerTopic() == ProjectBrokerModuleConfiguration.NBR_PARTICIPANTS_UNLIMITED) {
        infoProjectBrokerRunMode = translate("info.projectbroker.runmode.accept.automatically.unlimited" );
      } else {
        infoProjectBrokerRunMode = translate("info.projectbroker.runmode.accept.automatically.limited", Integer.toString(moduleConfig.getNbrParticipantsPerTopic()) );
      }     
    }
    contentVC.contextPut("infoProjectBrokerRunMode", infoProjectBrokerRunMode);
    mainPanel = new Panel("projectlist_panel");
    CoursePropertyManager cpm = userCourseEnv.getCourseEnvironment().getCoursePropertyManager();
    if (  (ProjectBrokerManagerFactory.getProjectGroupManager().isAccountManager(ureq.getIdentity(), cpm, ne.getCourseNode() ) && !previewMode)
        || userCourseEnv.getCourseEnvironment().getCourseGroupManager().isIdentityCourseAdministrator(ureq.getIdentity())
        || ureq.getUserSession().getRoles().isOLATAdmin()) {
      contentVC.contextPut("isAccountManager", true);
      createNewProjectButton = LinkFactory.createButtonSmall("create.new.project.button", contentVC, this);
    } else {
      contentVC.contextPut("isAccountManager", false);
    }
    // push title and learning objectives, only visible on intro page
    contentVC.contextPut("menuTitle", ne.getCourseNode().getShortTitle());
    contentVC.contextPut("displayTitle", ne.getCourseNode().getLongTitle());
 
    projectBrokerId = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectBrokerId(cpm, ne.getCourseNode());
    if (projectBrokerId == null) {
      // no project-broker exist => create a new one, happens only once
      ProjectBroker projectBroker = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectBroker();
      projectBrokerId = projectBroker.getKey();
      ProjectBrokerManagerFactory.getProjectBrokerManager().saveProjectBrokerId(projectBrokerId, cpm, ne.getCourseNode());
      getLogger().info("no project-broker exist => create a new one projectBrokerId=" + projectBrokerId);
    }
   
    tableController = this.createTableController(ureq, wControl);
    updateProjectListModelOf(tableController, ureq.getIdentity());
    contentVC.put("projectList", tableController.getInitialComponent());
    mainPanel.setContent(contentVC);

    putInitialPanel(mainPanel);
  }
 
 
  /**
   * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.components.Component, org.olat.core.gui.control.Event)
   */
  public void event(UserRequest ureq, Component source, Event event) {
    if (source == createNewProjectButton) {
      String projectTitle = translate("new.project.title");
      int i = 1;
      while (ProjectBrokerManagerFactory.getProjectBrokerManager().existProjectName(projectBrokerId, projectTitle)) {
        projectTitle = translate("new.project.title") + i++;
      }
      String projectGroupName = translate("project.member.groupname", projectTitle);
      String projectGroupDescription = translate("project.member.groupdescription", projectTitle);
      BusinessGroup projectGroup = ProjectBrokerManagerFactory.getProjectGroupManager().createProjectGroupFor(projectBrokerId,ureq.getIdentity(), projectGroupName, projectGroupDescription, courseId);
      Project project = ProjectBrokerManagerFactory.getProjectBrokerManager().createAndSaveProjectFor(projectTitle, projectTitle, projectBrokerId, projectGroup);
      ProjectBrokerManagerFactory.getProjectGroupManager().sendGroupChangeEvent(project, courseId, ureq.getIdentity());
      getLogger().debug("Created a new project=" + project);
      projectController = new ProjectController(ureq, this.getWindowControl(), userCourseEnv, nodeEvaluation, project, true, moduleConfig);
      projectController.addControllerListener(this);
      mainPanel.pushContent(projectController.getInitialComponent());
    }
  }

  /**
   * @see org.olat.core.gui.control.DefaultController#event(org.olat.core.gui.UserRequest, org.olat.core.gui.control.Controller, org.olat.core.gui.control.Event)
   */
  public void event(UserRequest urequest, Controller source, Event event) {
    if ( (source == tableController) && (event instanceof TableEvent) ) {
      TableEvent te = (TableEvent) event;
      Project currentProject = (Project)tableController.getTableDataModel().getObject(te.getRowId());
      if ( te.getActionId().equals(TABLE_ACTION_SHOW_DETAIL)) {
        activateProjectController(currentProject, urequest);
      } else if ( te.getActionId().equals(TABLE_ACTION_ACCOUNT_MANAGER)) {
        activateUserController(currentProject, urequest);
      } else if ( te.getActionId().equals(TABLE_ACTION_SELECT)) {
        getLogger().debug("start enrollProjectParticipant identity=" + urequest.getIdentity() + " to project=" + currentProject);
        List<Project> projects = ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(projectBrokerId)
        boolean enrolled = ProjectBrokerManagerFactory.getProjectBrokerManager().enrollProjectParticipant(urequest.getIdentity(), currentProject, moduleConfig, nbrSelectedProjects, isParticipantInAnyProject);
        if (enrolled) {
          ProjectBrokerManagerFactory.getProjectBrokerEmailer().sendEnrolledEmailToParticipant(urequest.getIdentity(), currentProject, this.getTranslator());
          if (currentProject.isMailNotificationEnabled()) {
            ProjectBrokerManagerFactory.getProjectBrokerEmailer().sendEnrolledEmailToManager(urequest.getIdentity(), currentProject, this.getTranslator());
          }
          ProjectBrokerManagerFactory.getProjectGroupManager().sendGroupChangeEvent(currentProject, courseId, urequest.getIdentity());
        } else {
          showInfo("info.msg.could.not.enroll");
        }
        updateProjectListModelOf(tableController, urequest.getIdentity());
      } else if ( te.getActionId().equals(TABLE_ACTION_CANCEL_SELECT)) {
        getLogger().debug("start cancelProjectEnrollmentOf identity=" + urequest.getIdentity() + " to project=" + currentProject);
        boolean cancelEnrollmend = ProjectBrokerManagerFactory.getProjectBrokerManager().cancelProjectEnrollmentOf(urequest.getIdentity(), currentProject, moduleConfig);
        if (cancelEnrollmend) {
          ProjectBrokerManagerFactory.getProjectBrokerEmailer().sendCancelEnrollmentEmailToParticipant(urequest.getIdentity(), currentProject, this.getTranslator());
          if (currentProject.isMailNotificationEnabled()) {
            ProjectBrokerManagerFactory.getProjectBrokerEmailer().sendCancelEnrollmentEmailToManager(urequest.getIdentity(), currentProject, this.getTranslator());
          }
          ProjectBrokerManagerFactory.getProjectGroupManager().sendGroupChangeEvent(currentProject, courseId, urequest.getIdentity());
        } else {
          showInfo("info.msg.could.not.cancel.enrollment");
        }
        updateProjectListModelOf(tableController, urequest.getIdentity());
      } else {
        getLogger().warn("Controller-event-handling: Unkown event=" + event);
      }
      fireEvent(urequest, event);
    } else if ( (source == projectController) && (event == Event.BACK_EVENT) ) {
      mainPanel.popContent();
      updateProjectListModelOf(tableController, urequest.getIdentity());
    else if ( (source == projectController) && (event instanceof CancelNewProjectEvent) ) {
      CancelNewProjectEvent cancelEvent = (CancelNewProjectEvent)event;
      getLogger().info("event form cancelled => delete project");
      ProjectBrokerManagerFactory.getProjectBrokerManager().deleteProject(cancelEvent.getProject(), true, userCourseEnv.getCourseEnvironment(), nodeEvaluation.getCourseNode());
      mainPanel.popContent();
      updateProjectListModelOf(tableController, urequest.getIdentity());
   
  }


  private void updateProjectListModelOf(TableController tableController, Identity identity) {
    List<Project> projects = new ArrayList<Project>(ProjectBrokerManagerFactory.getProjectBrokerManager().getProjectListBy(projectBrokerId))
    nbrSelectedProjects = ProjectBrokerManagerFactory.getProjectBrokerManager().getNbrSelectedProjects(identity, projects);
    isParticipantInAnyProject = ProjectBrokerManagerFactory.getProjectBrokerManager().isParticipantInAnyProject( identity,  projects);
    projectListTableModel = new ProjectListTableModel(projects, identity, getTranslator(), moduleConfig, numberOfCustomFieldInTable, numberOfEventInTable, nbrSelectedProjects, isParticipantInAnyProject);
    tableController.setTableDataModel(projectListTableModel);
   
  }


  private void activateUserController(final Project projectAt, UserRequest urequest) {
    if (projectAt.getProjectLeaders().isEmpty()) {
      this.showInfo("show.info.no.project.leader");
    } else if (projectAt.getProjectLeaders().size() > 0) {
      // Open visiting card in new popup
      ControllerCreator ctrlCreator = new ControllerCreator() {
        public Controller createController(UserRequest lureq, WindowControl lwControl) {
          return new UserInfoMainController(lureq, lwControl, projectAt.getProjectLeaders().get(0));
        }
      };
      // wrap the content controller into a full header layout
      ControllerCreator layoutCtrlr = BaseFullWebappPopupLayoutFactory.createAuthMinimalPopupLayout(urequest, ctrlCreator);
      this.openInNewBrowserWindow(urequest, layoutCtrlr);
    }
  }


  private void activateProjectController(Project project, UserRequest urequest) {
    if (projectController != null) {
      projectController.dispose();
    }
    projectController = new ProjectController(urequest, this.getWindowControl(), userCourseEnv, nodeEvaluation, project, false, moduleConfig);
    projectController.addControllerListener(this);
    mainPanel.pushContent(projectController.getInitialComponent());
  }

  /**
   *
   * @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
   */
  protected void doDispose() {
    if (projectController != null) {
      projectController.dispose();
      projectController = null;
    }
    if (tableController != null) {
      tableController.dispose();
      tableController = null;
    }
   
  }

  private TableController createTableController(final UserRequest ureq, WindowControl wControl) {
    numberOfCustomFieldInTable = 0;
    numberOfEventInTable = 0;   
    TableGuiConfiguration tableConfig = new TableGuiConfiguration();
    tableConfig.setTableEmptyMessage(translate("projectlist.no.projects"));
    tableConfig.setPreferencesOffered(true, "projectbrokerList");
    tableConfig.setShowAllLinkEnabled(false);// Do not allow show all because many entries takes too long to render
    TableController tableController = new TableController(tableConfig, ureq, wControl, this.getTranslator(), this, true);
    int dataColumn = 0;
    tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.title", dataColumn++, TABLE_ACTION_SHOW_DETAIL, ureq.getLocale()));
    DefaultColumnDescriptor projectManagerDescriptor = new DefaultColumnDescriptor("projectlist.tableheader.account.manager", dataColumn++, TABLE_ACTION_ACCOUNT_MANAGER, ureq.getLocale());
    projectManagerDescriptor.setIsPopUpWindowAction( true, "height=600, width=900, location=no, menubar=no, resizable=yes, status=no, scrollbars=yes, toolbar=no");
    tableController.addColumnDescriptor(projectManagerDescriptor);
    // Custom-Fields
    List<CustomField> customFieldList = moduleConfig.getCustomFields();
    for (Iterator iterator = customFieldList.iterator(); iterator.hasNext();) {
      CustomField customField = (CustomField) iterator.next();
      if (customField.isTableViewEnabled()) {
        numberOfCustomFieldInTable++;
        DefaultColumnDescriptor columnDescriptor = new DefaultColumnDescriptor(customField.getName(), dataColumn++,null, ureq.getLocale());
        columnDescriptor.setTranslateHeaderKey(false);
        tableController.addColumnDescriptor(columnDescriptor);       
      }
    }
    // Project Events
    for (Project.EventType eventType : Project.EventType.values()) {
      if (moduleConfig.isProjectEventEnabled(eventType) && moduleConfig.isProjectEventTableViewEnabled(eventType)) {
        numberOfEventInTable ++;
        tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.event." + eventType.getI18nKey(), dataColumn++,null, ureq.getLocale()));       
      }
    }
   
    tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.state", dataColumn++,null, ureq.getLocale()));
    tableController.addColumnDescriptor(new DefaultColumnDescriptor("projectlist.tableheader.numbers", dataColumn++, null, ureq.getLocale()));
    tableController.addColumnDescriptor(new BooleanColumnDescriptor("projectlist.tableheader.select", dataColumn++, TABLE_ACTION_SELECT,
        translate("table.action.select"), "-" ));
    tableController.addColumnDescriptor(new BooleanColumnDescriptor("projectlist.tableheader.cancel.select", dataColumn++, TABLE_ACTION_CANCEL_SELECT,
        translate("projectlist.tableheader.cancel.select"), "-" ));

    return tableController;

  }

}
TOP

Related Classes of org.olat.course.nodes.projectbroker.ProjectListController

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.