Package org.olat.course.condition

Source Code of org.olat.course.condition.ConditionConfigExpertForm

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

import org.olat.core.gui.components.form.Form;
import org.olat.core.gui.formelements.TextAreaElement;
import org.olat.core.gui.translator.Translator;
import org.olat.core.util.StringHelper;
import org.olat.course.condition.interpreter.ConditionErrorMessage;
import org.olat.course.condition.interpreter.ConditionExpression;
import org.olat.course.editor.CourseEditorEnv;
import org.olat.course.run.userview.UserCourseEnvironment;

/**
* Description:<br>
*
* @author Felix Jost
*/
public class ConditionConfigExpertForm extends Form {

  private TextAreaElement tprecond;
  private UserCourseEnvironment euce;
  private String conditionId;

  /**
   * Constructor for the condition configuration form in expert mode
   *
   * @param name The form name
   * @param cond The condition that is used to initialize the form
   * @param trans
   */
  public ConditionConfigExpertForm(String name, Translator translator, Condition cond, UserCourseEnvironment euce) {
    super(name, translator);
    this.euce = euce;
    this.conditionId = cond.getConditionId();
    tprecond = new TextAreaElement("form.expert.condition", 6, 45, (cond == null ? "" : cond.getConditionExpression()));
    addFormElement("precond", tprecond);
    setSubmitKey("save");
    setCancelButton();
  }

  /**
   * @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
   */
  public boolean validate() {
    boolean tprecondOk = tprecond.notLongerThan(5000, "input.toolong");
    if (tprecondOk == false) { return false; // user writes more than 5000
    // characters
    }
    if (tprecond.isEmpty()) { return true; // user leaves it blank to cancel
    // the precondition
    }
    /*
     *  not empty, now test precondition syntax and for existing soft references
     */
    CourseEditorEnv cev = euce.getCourseEditorEnv();
    ConditionExpression ce = new ConditionExpression(conditionId,tprecond.getValue());
    ConditionErrorMessage[] cerrmsg = cev.validateConditionExpression(ce);
    /*
     * display any error detected in the condition expression testing.
     */
    if (cerrmsg != null && cerrmsg.length >0) {
      //the error message
      tprecond.setErrorKeyWithParams(cerrmsg[0].errorKey, cerrmsg[0].errorKeyParams);
      if (cerrmsg[0].solutionMsgKey != null && !"".equals(cerrmsg[0].solutionMsgKey)) {
        //and a hint or example to clarify the error message
        tprecond.setExample(translate(cerrmsg[0].solutionMsgKey, cerrmsg[0].errorKeyParams));
      }
      return false;
    }
    //reset HINTS
    tprecond.setExample(null);
    return true;
  }

  /**
   * Update a condition using the data in the current form
   *
   * @param cond The condition that should be updated
   * @return Condition The updated condition or null if condition was empty
   */
  public Condition updateConditionFromFormData(Condition cond) {
    if (cond == null) cond = new Condition();

    TextAreaElement el = getTextAreaElement("precond");
    if (el != null) cond.setConditionExpression(el.getValue());

    cond.setExpertMode(true);
    cond.setConditionId(conditionId);
    if (StringHelper.containsNonWhitespace(cond.getConditionExpression())) { return cond; }
    cond.setConditionExpression(null);
    return cond;
  }

}
TOP

Related Classes of org.olat.course.condition.ConditionConfigExpertForm

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.