/**
* 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.st;
/**
* Initial Date: 22.03.04
*
* @author Felix Jost
*
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
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.editor.StatusDescription;
import org.olat.course.nodes.CourseNode;
import org.olat.course.run.scoring.ScoreCalculator;
import org.olat.course.run.userview.UserCourseEnvironment;
/**
*
* Description:<br>
* TODO: guido Class Description for EditScoreCalculationExpertForm
*
*/
class EditScoreCalculationExpertForm extends Form {
private static final String GET_PASSED_69741247660309 = "getPassed(\"69741247660309\")";
private static final String GET_SCORE_69741247660309_2 = "getScore(\"69741247660309\") * 2";
private TextAreaElement tscoreexpr, tpassedexpr;
private UserCourseEnvironment euce;
private ScoreCalculator sc;
private List<CourseNode> assessableNodesList;
private List<String> testElemWithNoResource = new ArrayList<String>();
/**
* Constructor for a score calculation edit form
* @param name
*/
public EditScoreCalculationExpertForm(String name, Translator translator, UserCourseEnvironment euce, List<CourseNode> assessableNodesList) {
super(name, translator);
this.euce = euce;
this.assessableNodesList = assessableNodesList;
tscoreexpr = new TextAreaElement("scorecalc.score", 6, 45);
tscoreexpr.setExample(GET_SCORE_69741247660309_2);
tpassedexpr = new TextAreaElement("scorecalc.passed", 6, 45);
tpassedexpr.setExample(GET_PASSED_69741247660309);
addFormElement("tscoreexpr", tscoreexpr);
addFormElement("tpassedexpr", tpassedexpr);
setSubmitKey("save");
setCancelButton();
}
/**
* @see org.olat.core.gui.components.Form#validate(org.olat.core.gui.UserRequest)
*/
public boolean validate() {
boolean tscoreexprOk = tscoreexpr.notLongerThan(5000, "input.toolong");
boolean tpassedexprOk = tpassedexpr.notLongerThan(5000, "input.toolong");
if(tscoreexprOk && tpassedexprOk == false){
return false;
}
String scoreExp = tscoreexpr.getValue().trim();
String passedExp = tpassedexpr.getValue().trim();
if (StringHelper.containsNonWhitespace(scoreExp)) {
/*
* not empty, now test precondition syntax and for existing soft references
*/
CourseEditorEnv cev = euce.getCourseEditorEnv();
ConditionExpression ce = new ConditionExpression("score",scoreExp);
ConditionErrorMessage[] cerrmsgs = cev.validateConditionExpression(ce);
/*
* display any error detected in the condition expression testing.
*/
if (cerrmsgs != null && cerrmsgs.length>0) {
//the error message
tscoreexpr.setErrorKeyWithParams(cerrmsgs[0].errorKey, cerrmsgs[0].errorKeyParams);
if (cerrmsgs[0].solutionMsgKey != null && !"".equals(cerrmsgs[0].solutionMsgKey)) {
//and a hint or example to clarify the error message
tscoreexpr.setExample(translate(cerrmsgs[0].solutionMsgKey, cerrmsgs[0].errorKeyParams));
}
return false;
}
testElemWithNoResource = getInvalidNodeDescriptions(ce);
}
if (StringHelper.containsNonWhitespace(passedExp)) {
/*
* not empty, now test precondition syntax and for existing soft references
*/
CourseEditorEnv cev = euce.getCourseEditorEnv();
ConditionExpression ce = new ConditionExpression("passed",passedExp);
ConditionErrorMessage[] cerrmsgs = cev.validateConditionExpression(ce);
/*
* display any error detected in the condition expression testing.
*/
if (cerrmsgs != null && cerrmsgs.length>0) {
//the error message
tpassedexpr.setErrorKeyWithParams(cerrmsgs[0].errorKey, cerrmsgs[0].errorKeyParams);
if (cerrmsgs[0].solutionMsgKey != null && !"".equals(cerrmsgs[0].solutionMsgKey)) {
//and a hint or example to clarify the error message
tpassedexpr.setExample(translate(cerrmsgs[0].solutionMsgKey, cerrmsgs[0].errorKeyParams));
}
return false;
}
}
//reset HINTS
tscoreexpr.setExample(GET_SCORE_69741247660309_2);
tpassedexpr.setExample(GET_PASSED_69741247660309);
return true;
}
/**
* @param sc
*/
public void setScoreCalculator(ScoreCalculator sc) {
this.sc = sc;
tscoreexpr.setValue(sc == null? "" : sc.getScoreExpression());
tpassedexpr.setValue(sc == null? "" : sc.getPassedExpression());
}
/**
* @return ScoreCalcualtor
*/
public ScoreCalculator getScoreCalulator() {
String scoreExp = tscoreexpr.getValue().trim();
String passedExp = tpassedexpr.getValue().trim();
if (scoreExp.equals("") && passedExp.equals("")) return null;
if (passedExp.equals("")) passedExp = null;
if (scoreExp.equals("")) scoreExp= null;
//ScoreCalculator sc = new ScoreCalculator(scoreExp, passedExp);
sc.setScoreExpression(scoreExp);
sc.setPassedExpression(passedExp);
sc.setExpertMode(true);
return sc;
}
/**
* Get the list with the node description of the "invalid" nodes.
* The "invalid" nodes are not associated with any test resource so they are actually not assessable.
* @param ce
* @return
*/
private List<String> getInvalidNodeDescriptions(ConditionExpression ce) {
List<String> nodeDescriptionList = new ArrayList<String>();
if (ce != null) {
Set<String> selectedNodesIds = ce.getSoftReferencesOf("courseNodeId");
for (Iterator nodeIter = assessableNodesList.iterator(); nodeIter.hasNext();) {
CourseNode node = (CourseNode) nodeIter.next();
if (selectedNodesIds.contains(node.getIdent())) {
StatusDescription isConfigValid = node.isConfigValid();
if (isConfigValid != null && isConfigValid.isError()) {
String nodeDescription = node.getShortName() + " (Id:" + node.getIdent() + ")";
if (!nodeDescriptionList.contains(nodeDescription)) {
nodeDescriptionList.add(nodeDescription);
}
}
}
}
}
return nodeDescriptionList;
}
public List<String> getInvalidNodeDescriptions() {
return testElemWithNoResource;
}
}