Package org.olat.course.archiver

Source Code of org.olat.course.archiver.CourseLogsArchiveController

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

import java.io.File;
import java.util.Date;
import java.util.Locale;

import org.olat.core.commons.modules.bc.FolderRunController;
import org.olat.core.commons.taskExecutor.TaskExecutorManager;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.components.htmlheader.jscss.JSAndCSSComponent;
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.velocity.VelocityContainer;
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.closablewrapper.CloseableModalController;
import org.olat.core.id.OLATResourceable;
import org.olat.core.id.UserConstants;
import org.olat.core.util.Util;
import org.olat.core.util.vfs.LocalFolderImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.course.CourseFactory;
import org.olat.course.ICourse;
import org.olat.course.statistic.AsyncExportManager;
import org.olat.course.statistic.ExportManager;
import org.olat.home.HomeMainController;
import org.olat.repository.RepositoryManager;
import org.olat.user.UserManager;

/**
* Description: Archives the user chosen courselogfiles
*
* Initial Date: Dec 6, 2004
* @author Alex
*/
public class CourseLogsArchiveController extends BasicController {
 
  private Panel myPanel;
  private VelocityContainer myContent;
 
  private LogFileChooserForm logFileChooserForm;
  private Link showFileButton;
  private OLATResourceable ores;
 
  /**
   * Constructor for the course logs archive controller
   * @param ureq
   * @param wControl
   * @param course
   */
  public CourseLogsArchiveController(UserRequest ureq, WindowControl wControl, OLATResourceable ores) {
    super(ureq, wControl);
    this.ores = ores;
    this.myPanel = new Panel("myPanel");
    myPanel.addListener(this);

    myContent = createVelocityContainer("start_courselogs");
   
    boolean isOLATAdmin = ureq.getUserSession().getRoles().isOLATAdmin();
    boolean isOresOwner = RepositoryManager.getInstance().isOwnerOfRepositoryEntry(ureq.getIdentity(), RepositoryManager.getInstance().lookupRepositoryEntry(ores, false));
    boolean isOresInstitutionalManager = RepositoryManager.getInstance().isInstitutionalRessourceManagerFor(RepositoryManager.getInstance().lookupRepositoryEntry(ores, false), ureq.getIdentity());
    boolean aLogV = isOresOwner || isOresInstitutionalManager;
    boolean uLogV = isOLATAdmin;
    boolean sLogV = isOresOwner || isOresInstitutionalManager;
   
    if (AsyncExportManager.getInstance().asyncArchiveCourseLogOngoingFor(ureq.getIdentity())) {
      // then show the ongoing feedback
      ICourse course = CourseFactory.loadCourse(ores);
      final String courseTitle = course.getCourseTitle();
      showExportOngoing(courseTitle);
    } else if (isOLATAdmin || aLogV || uLogV || sLogV){
        myContent.contextPut("hasLogArchiveAccess", new Boolean(true));
      logFileChooserForm = new LogFileChooserForm("logfilechooserform", getTranslator(), ureq.getLocale(), isOLATAdmin, aLogV, uLogV, sLogV);
      logFileChooserForm.addListener(this);
      myContent.put(logFileChooserForm);
      ICourse course = CourseFactory.loadCourse(ores);
      myContent.contextPut("body", translate("course.logs.existingarchiveintro", course.getCourseTitle()));
      showFileButton = LinkFactory.createButton("showfile", myContent, this);
      File exportDir = CourseFactory.getDataExportDirectory(ureq.getIdentity(), course.getCourseTitle());
      boolean exportDirExists = false;
      if (exportDir!=null && exportDir.exists() && exportDir.isDirectory()) {
        exportDirExists = true;
      }
      myContent.contextPut("hascourselogarchive", new Boolean(exportDirExists));
      myPanel.setContent(myContent);
    } else {
        myContent.contextPut("hasLogArchiveAccess", new Boolean(false));
        myPanel.setContent(myContent);
    }

    putInitialPanel(myPanel);
  }

  /**
   * @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 == logFileChooserForm) {
      if (event == Form.EVNT_VALIDATION_OK) {
       
        final boolean logAdminChecked = logFileChooserForm.logAdminChecked();
        final boolean logUserChecked = logFileChooserForm.logUserChecked();
        final boolean logStatisticChecked = logFileChooserForm.logStatChecked();
       
        final Date begin = logFileChooserForm.getBeginDate();
        final Date end = logFileChooserForm.getEndDate();
       
        UserManager um = UserManager.getInstance();
        final String charset = um.getUserCharset(ureq.getIdentity());
       
        ICourse course = CourseFactory.loadCourse(ores);
        final String courseTitle = course.getCourseTitle();
        final String targetDir = CourseFactory.getOrCreateDataExportDirectory(ureq.getIdentity(), courseTitle).getPath();
       
        final Long resId = ores.getResourceableId();
        final Locale theLocale = ureq.getLocale();
        final String email = ureq.getIdentity().getUser().getProperty(UserConstants.EMAIL, ureq.getLocale());
       
       
        AsyncExportManager.getInstance().asyncArchiveCourseLogFiles(ureq.getIdentity(), new Runnable() {

          @Override
          public void run() {
            showExportFinished();
          }
         
        },
        resId, targetDir, begin, end, logAdminChecked, logUserChecked, logStatisticChecked, charset, theLocale, email);

        showExportOngoing(courseTitle);
      }
      else if (event == Form.EVNT_FORM_CANCELLED) {
        myPanel.setContent(myContent);
      }
    }
    else if (source == showFileButton){
      ICourse course = CourseFactory.loadCourse(ores);
      String personalFolderDir = CourseFactory.getPersonalDirectory(ureq.getIdentity()).getPath();
      String targetDir = CourseFactory.getOrCreateDataExportDirectory(ureq.getIdentity(), course.getCourseTitle()).getPath();
     
      String relPath = "";
     
      if (targetDir.startsWith(personalFolderDir)) {
        // that should always be the case
       
        relPath = targetDir.substring(personalFolderDir.length()).replace("\\", "/");
        targetDir = targetDir.substring(0, personalFolderDir.length());
      }
     
      VFSContainer targetFolder = new LocalFolderImpl(new File(targetDir));
      FolderRunController bcrun = new FolderRunController(targetFolder, true, ureq, getWindowControl());
      Component folderComponent = bcrun.getInitialComponent();
      if (relPath.length()!=0) {
        if (!relPath.endsWith("/")) {
          relPath = relPath + "/";
        }
        bcrun.activate(ureq, relPath);
      }
     
      String personalFolder = Util.createPackageTranslator(HomeMainController.class, ureq.getLocale(), null).translate("menu.bc");
     
      CloseableModalController clc = new CloseableModalController(getWindowControl(), translate("close"), folderComponent, true, personalFolder);
     
      clc.activate();
     
    }
  }

  private void showExportOngoing(final String courseTitle) {
    VelocityContainer vcOngoing = createVelocityContainer("courselogs_ongoing");
    vcOngoing.contextPut("body", translate("course.logs.ongoing", courseTitle));
    myPanel.setContent(vcOngoing);

    // initialize polling
    myPanel.put("updatecontrol", new JSAndCSSComponent("intervall", this.getClass(), null, null, false, null, 3000));
  }

  protected void showExportFinished() {
    ICourse course = CourseFactory.loadCourse(ores);
    VelocityContainer vcFeedback = createVelocityContainer("courselogs_feedback");
    showFileButton = LinkFactory.createButton("showfile", vcFeedback, this);
    vcFeedback.contextPut("body", translate("course.logs.feedback", course.getCourseTitle()));
    myPanel.setContent(vcFeedback);

    // note: polling can't be switched off unfortunatelly
    //       this is due to the fact that the jsandcsscomponent can only modify
    //       certain parts of the page and it would require a full page refresh
    //       to get rid of the poller - and that's not possible currently

    showInfo("course.logs.finished", course.getCourseTitle());
  }

  /**
   * @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
   */
  protected void doDispose() {
    //has nothing to dispose so far
  }

}
TOP

Related Classes of org.olat.course.archiver.CourseLogsArchiveController

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.