Package org.olat.registration

Source Code of org.olat.registration.DisclaimerController

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

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

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.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.translator.Translator;
import org.olat.core.util.Util;
import org.olat.core.util.WebappHelper;
import org.olat.core.util.vfs.LocalFolderImpl;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSMediaResource;

/**
* Initial Date:  10.08.2004
*
* @author Mike Stock
*
* Comment: 
* Presents a disclaimer form with two text paragraphs and a checkbox. The text can be changed using the i18n tool.
* <p />
* The controller tries to read the following keys from the i18n files to add
* some optional features:
* <ul>
* <li>disclaimer.additionalcheckbox : if defined, a second checkbox is added
* with the text translated by this key</li>
* <li>disclaimer.filedownloadurl : a relative filename to a file that must be
* located in the olatdata/customizing/disclaimer/ directory. If defined and the
* file exists, a file download to this file is offered</li>
* </ul>
*
*/

public class DisclaimerController extends BasicController {

  private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(DisclaimerController.class);
 
  private final String SR_ERROR_DISCLAIMER_CHECKBOX = "sr.error.disclaimer.checkbox";
  private final String SR_ERROR_DISCLAIMER_CHECKBOXES = "sr.error.disclaimer.checkboxes";

  private VelocityContainer main;
  private Translator translator;
  private DisclaimerFormController disclaimerFormController;
  private Link downloadLink;
  private VFSLeaf downloadFile;

  /**
   * Display a disclaimer which can be accepted or denied.
   * @param ureq
   * @param wControl
   */
  public DisclaimerController(UserRequest ureq, WindowControl wControl) {
    this(ureq, wControl, false);
  }

  /**
   * Display a disclaimer which can be accepted or denied or in a read only manner
   * @param ureq
   * @param wControl
   * @param readOnly true: show only read only; false: allow user to accept
   */
  public DisclaimerController(UserRequest ureq, WindowControl wControl, boolean readOnly) {
    super(ureq, wControl);
    translator = Util.createPackageTranslator(DisclaimerController.class, ureq.getLocale());
    this.disclaimerFormController = new DisclaimerFormController(ureq, wControl, readOnly);
    listenTo(this.disclaimerFormController);
    main = new VelocityContainer("main", VELOCITY_ROOT + "/disclaimer.html", translator, this);
    main.put("dclform", this.disclaimerFormController.getInitialComponent());
   
    // add optinal download link, see class comments in DisclaimerFormController
    // Add the additional link to the form (depending on the configuration)
    if (RegistrationModule.isDisclaimerAdditionaLinkText()) {
      File disclaimerDir = new File(WebappHelper.getUserDataRoot() + "/customizing/disclaimer/");
      disclaimerDir.mkdirs();
      VFSContainer disclaimerContainer = new LocalFolderImpl(disclaimerDir);
      String i18nIfiedFilename = translate("disclaimer.filedownloadurl");
      this.downloadFile = (VFSLeaf)disclaimerContainer.resolve(i18nIfiedFilename);
      if (this.downloadFile != null) {
        this.downloadLink = LinkFactory.createLink("disclaimer.additionallinktext", main, this);
        this.downloadLink.setTarget("_blank");
       
        if (i18nIfiedFilename.toLowerCase().endsWith(".pdf")) {
          this.downloadLink.setCustomEnabledLinkCSS("b_with_small_icon_left b_filetype_pdf");
        } else if (i18nIfiedFilename.toLowerCase().endsWith(".html") || i18nIfiedFilename.toLowerCase().endsWith(".htm")) {
          this.downloadLink.setCustomEnabledLinkCSS("b_with_small_icon_left b_filetype_html");
        } else if (i18nIfiedFilename.toLowerCase().endsWith(".doc")) {
          this.downloadLink.setCustomEnabledLinkCSS("b_with_small_icon_left b_filetype_doc");
        }
      }
    }

   
    putInitialPanel(main);
  }
 
  /**
   * @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)
   */
  @Override
  public void event(UserRequest ureq, Component source, Event event) {
    if (source == this.downloadLink) {
      ureq.getDispatchResult().setResultingMediaResource(new VFSMediaResource(this.downloadFile));
      // Prevent "do not press reload" message.
      this.downloadLink.setDirty(false);
    }
  }
 
  @Override
  protected void event(UserRequest ureq, Controller source, Event event) {
    if (source == this.disclaimerFormController) {
     
      if (event == Event.DONE_EVENT) {
        // Verify that, if the additional checkbox is configured to be visible, it is checked as well
        boolean acceptCheckboxChecked = (this.disclaimerFormController.acceptCheckbox != null) ? (this.disclaimerFormController.acceptCheckbox.getSelectedKeys().size() != 0) : false;
        // configure additional checkbox, see class comments in DisclaimerFormController
        boolean additionalCheckboxConfigured = RegistrationModule.isDisclaimerAdditionalCheckbox();
        boolean additionalCheckboxChecked = (this.disclaimerFormController.additionalCheckbox != null) ? (this.disclaimerFormController.additionalCheckbox.getSelectedKeys().size() != 0) : false;
        if (!additionalCheckboxConfigured) {
          if (acceptCheckboxChecked) {
            fireEvent(ureq, Event.DONE_EVENT);
          } else {
            getWindowControl().setError(translate(SR_ERROR_DISCLAIMER_CHECKBOX));
          }
        } else {
          if (acceptCheckboxChecked && additionalCheckboxChecked) {
            fireEvent(ureq, Event.DONE_EVENT);
          } else {
            getWindowControl().setError(translate(SR_ERROR_DISCLAIMER_CHECKBOXES));
          }
        }
      } else if (event == Event.CANCELLED_EVENT) {
        fireEvent(ureq, Event.CANCELLED_EVENT);
      }
    }
  }
 
  /**
   * Change the locale of this controller.
   * @param locale
   */
  public void changeLocale(Locale locale) {
    translator.setLocale(locale);
    main.put("dclform", this.disclaimerFormController.getInitialComponent());
  }
 
  /**
   *
   * @see org.olat.core.gui.control.DefaultController#doDispose(boolean)
   */
  @Override
  protected void doDispose() {
    // nothing to do yet
  }

}
TOP

Related Classes of org.olat.registration.DisclaimerController

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.