Search in sources :

Example 1 with Variables

use of org.olat.ims.qti.container.Variables in project OpenOLAT by OpenOLAT.

the class QTIHelper method declareVariables.

/**
 */
public static Variables declareVariables(Element el_outcomes) {
    String varName;
    Variables variables = new Variables();
    if (el_outcomes == null)
        return variables;
    List decvars = el_outcomes.selectNodes("decvar");
    /*
		 * <decvar defaultval = "0" varname = "Var_SumofScores" vartype = "Integer"
		 * minvalue = "-10" maxvalue = "10" cutvalue = "0"/> <decvar minvalue = "0"
		 * maxvalue = "1" defaultval = "0"/>
		 */
    for (Iterator iter = decvars.iterator(); iter.hasNext(); ) {
        Element decvar = (Element) iter.next();
        // dtd CDATA 'SCORE'
        varName = decvar.attributeValue("varname");
        if (varName == null)
            varName = "SCORE";
        String varType = decvar.attributeValue("vartype");
        // default
        if (varType == null)
            varType = "Integer";
        Variable v = null;
        if (varType.equals("Integer") || varType.equals("Decimal")) {
            String def = decvar.attributeValue("defaultval");
            String min = decvar.attributeValue("minvalue");
            String max = decvar.attributeValue("maxvalue");
            String cut = decvar.attributeValue("cutvalue");
            v = new DecimalVariable(varName, max, min, cut, def);
            variables.setVariable(v);
        } else
            throw new RuntimeException("vartype " + varType + " not supported (declaration)");
    }
    return variables;
}
Also used : Variables(org.olat.ims.qti.container.Variables) Variable(org.olat.ims.qti.container.Variable) DecimalVariable(org.olat.ims.qti.container.DecimalVariable) Element(org.dom4j.Element) Iterator(java.util.Iterator) List(java.util.List) DecimalVariable(org.olat.ims.qti.container.DecimalVariable)

Example 2 with Variables

use of org.olat.ims.qti.container.Variables in project OpenOLAT by OpenOLAT.

the class QTI_respcondition method process.

/**
 * Response Condition
 *
 * ims qti 1.2.1 respcondition
 * <!ELEMENT respcondition (qticomment? , conditionvar , setvar* , displayfeedback*)>
 * <!ELEMENT conditionvar (not | and | or | unanswered | other | varequal | varlt | varlte |
 *      vargt | vargte | varsubset | varinside | varsubstring | durequal | durlt | durlte | durgt | durgte)+>
 *
 * <!ELEMENT setvar (#PCDATA)>
 * <!ATTLIST setvar  %I_VarName; action     (Set | Add | Subtract | Multiply | Divide )  'Set' >
 * mit I_VarName = varname CDATA  'SCORE'
 * <setvar action="Set" varname="SCORE">10</setvar>
 *
 * <!ELEMENT displayfeedback (#PCDATA)>
 * <!ATTLIST displayfeedback  feedbacktype  (Response | Solution | Hint )  'Response'
 *	                        %I_LinkRefId; >
 * mit I_LinkRefId = linkrefid CDATA  #REQUIRED"
 * e.g. <displayfeedback feedbacktype = "Solution" linkrefid = "CorrectSoln"/>
 *
 * ??? should be ? ? <conditionvar><or>...</or><varequal>...</varequal></conditionvar> does not make sense
 * but 	<conditionvar>
 *					<varequal respident = "Word-1">KITTENS</varequal>
 *					<varequal respident = "Word-2">HATS</varequal>
 *				</conditionvar> makes sense, so treat members of conditionvar as children of an "and" element
 * @param node_respcond
 */
public boolean process(Element el_respcond, ItemContext itc, EvalContext ect) {
    // 1. evaluate conditionvar
    // 2. if true, set variables
    // and setCurrentDisplayFeedback (TODO: assuming there is only one displayfeedback in a respcondition)
    Variables vars;
    Element el_condVar = (Element) el_respcond.selectSingleNode("conditionvar");
    String respcondtitle = el_respcond.attributeValue("title");
    QTI_and qtiAnd = QTIHelper.getQTI_and();
    boolean fulfilled = qtiAnd.eval(el_condVar, itc, ect);
    // continue to set variables and display feedback if question was answered correctly
    if (fulfilled) {
        vars = itc.getVariables();
        List setvars = el_respcond.selectNodes("setvar");
        for (Iterator iter = setvars.iterator(); iter.hasNext(); ) {
            Element element = (Element) iter.next();
            String action = element.attributeValue("action");
            String varName = element.attributeValue("varname");
            if (varName == null)
                varName = "SCORE";
            varName.trim();
            String varVal = element.getText();
            Variable var = vars.getVariable(varName);
            if (var == null)
                throw new RuntimeException("var " + varName + " is in setvar, but was not declared ");
            if (action.equals("Set")) {
                var.setValue(varVal);
            } else {
                // Add | Subtract | Multiply | Divide
                if (action.equals("Add")) {
                    var.add(varVal);
                } else if (action.equals("Subtract")) {
                    var.subtract(varVal);
                } else if (action.equals("Multiply")) {
                    var.multiply(varVal);
                } else if (action.equals("Divide")) {
                    var.divide(varVal);
                }
            }
        }
        // set displayfeedback
        // <displayfeedback feedbacktype = "Response" linkrefid = "Correct"/>
        // <!ATTLIST displayfeedback  feedbacktype  (Response | Solution | Hint )  'Response' %I_LinkRefId; >
        Output output = itc.getOutput();
        List fbs = el_respcond.selectNodes("displayfeedback");
        for (Iterator it_fbs = fbs.iterator(); it_fbs.hasNext(); ) {
            Element el_dispfb = (Element) it_fbs.next();
            // must exist (dtd)
            String linkRefId = el_dispfb.attributeValue("linkrefid");
            // must exist (dtd)
            String feedbacktype = el_dispfb.attributeValue("feedbacktype");
            Element el_resolved = (Element) itc.getEl_item().selectSingleNode(".//itemfeedback[@ident='" + linkRefId + "']");
            if (el_resolved == null)
                continue;
            if (feedbacktype.equals("Response")) {
                // additional (olat) rule:
                // we want to render the original answer again in the simple case where the respcondition was generated
                // by the olat export and contains only one varequal.
                /*
					  
					 	 <response_label ident = "2">
			     			<flow_mat>
								<material>
										<mattext texttype="text/html">...</mattext>
								</material>
                  			</flow_mat>
                		</response_label> 
					  ...
					
					 	<respcondition title="_olat_resp_feedback" continue="Yes">
							<conditionvar>
								<varequal respident="Frage6549" case="Yes">2</varequal>
							</conditionvar>
            				<displayfeedback linkrefid="2"/>
						</respcondition>
						
						In this case, it is possible (and wished) to trace the feedback back to the answer which triggered this feedback.
						Such a respcondition is identified by the title which is exactly "_olat_resp_feedback".
						
						
					 */
                Element el_chosenanswer = null;
                if (respcondtitle != null && respcondtitle.equals("_olat_resp_feedback")) {
                    Element el_vareq = (Element) el_respcond.selectSingleNode(".//varequal");
                    String answerident = el_vareq.getText();
                    el_chosenanswer = (Element) itc.getEl_item().selectSingleNode(".//response_label[@ident='" + answerident + "']//material");
                }
                // give the whole itemfeedback to render
                output.addItem_El_response(el_chosenanswer, el_resolved);
            } else if (feedbacktype.equals("Solution")) {
                Element el_solution = (Element) el_resolved.selectSingleNode(".//solution");
                if (el_solution != null)
                    output.setSolution(new Solution(el_solution));
            } else if (feedbacktype.equals("Hint")) {
                // <!ENTITY % I_FeedbackStyle " feedbackstyle  (Complete | Incremental | Multilevel | Proprietary )  'Complete'">
                Element el_hint = (Element) el_resolved.selectSingleNode(".//hint");
                output.setHint(new Hint(el_hint));
            }
        }
    }
    return fulfilled;
}
Also used : Variables(org.olat.ims.qti.container.Variables) Variable(org.olat.ims.qti.container.Variable) Hint(org.olat.ims.qti.container.qtielements.Hint) Element(org.dom4j.Element) Output(org.olat.ims.qti.container.Output) Iterator(java.util.Iterator) List(java.util.List) Solution(org.olat.ims.qti.container.qtielements.Solution)

Example 3 with Variables

use of org.olat.ims.qti.container.Variables in project openolat by klemens.

the class QTIHelper method declareVariables.

/**
 */
public static Variables declareVariables(Element el_outcomes) {
    String varName;
    Variables variables = new Variables();
    if (el_outcomes == null)
        return variables;
    List decvars = el_outcomes.selectNodes("decvar");
    /*
		 * <decvar defaultval = "0" varname = "Var_SumofScores" vartype = "Integer"
		 * minvalue = "-10" maxvalue = "10" cutvalue = "0"/> <decvar minvalue = "0"
		 * maxvalue = "1" defaultval = "0"/>
		 */
    for (Iterator iter = decvars.iterator(); iter.hasNext(); ) {
        Element decvar = (Element) iter.next();
        // dtd CDATA 'SCORE'
        varName = decvar.attributeValue("varname");
        if (varName == null)
            varName = "SCORE";
        String varType = decvar.attributeValue("vartype");
        // default
        if (varType == null)
            varType = "Integer";
        Variable v = null;
        if (varType.equals("Integer") || varType.equals("Decimal")) {
            String def = decvar.attributeValue("defaultval");
            String min = decvar.attributeValue("minvalue");
            String max = decvar.attributeValue("maxvalue");
            String cut = decvar.attributeValue("cutvalue");
            v = new DecimalVariable(varName, max, min, cut, def);
            variables.setVariable(v);
        } else
            throw new RuntimeException("vartype " + varType + " not supported (declaration)");
    }
    return variables;
}
Also used : Variables(org.olat.ims.qti.container.Variables) Variable(org.olat.ims.qti.container.Variable) DecimalVariable(org.olat.ims.qti.container.DecimalVariable) Element(org.dom4j.Element) Iterator(java.util.Iterator) List(java.util.List) DecimalVariable(org.olat.ims.qti.container.DecimalVariable)

Example 4 with Variables

use of org.olat.ims.qti.container.Variables in project openolat by klemens.

the class QTI_respcondition method process.

/**
 * Response Condition
 *
 * ims qti 1.2.1 respcondition
 * <!ELEMENT respcondition (qticomment? , conditionvar , setvar* , displayfeedback*)>
 * <!ELEMENT conditionvar (not | and | or | unanswered | other | varequal | varlt | varlte |
 *      vargt | vargte | varsubset | varinside | varsubstring | durequal | durlt | durlte | durgt | durgte)+>
 *
 * <!ELEMENT setvar (#PCDATA)>
 * <!ATTLIST setvar  %I_VarName; action     (Set | Add | Subtract | Multiply | Divide )  'Set' >
 * mit I_VarName = varname CDATA  'SCORE'
 * <setvar action="Set" varname="SCORE">10</setvar>
 *
 * <!ELEMENT displayfeedback (#PCDATA)>
 * <!ATTLIST displayfeedback  feedbacktype  (Response | Solution | Hint )  'Response'
 *	                        %I_LinkRefId; >
 * mit I_LinkRefId = linkrefid CDATA  #REQUIRED"
 * e.g. <displayfeedback feedbacktype = "Solution" linkrefid = "CorrectSoln"/>
 *
 * ??? should be ? ? <conditionvar><or>...</or><varequal>...</varequal></conditionvar> does not make sense
 * but 	<conditionvar>
 *					<varequal respident = "Word-1">KITTENS</varequal>
 *					<varequal respident = "Word-2">HATS</varequal>
 *				</conditionvar> makes sense, so treat members of conditionvar as children of an "and" element
 * @param node_respcond
 */
public boolean process(Element el_respcond, ItemContext itc, EvalContext ect) {
    // 1. evaluate conditionvar
    // 2. if true, set variables
    // and setCurrentDisplayFeedback (TODO: assuming there is only one displayfeedback in a respcondition)
    Variables vars;
    Element el_condVar = (Element) el_respcond.selectSingleNode("conditionvar");
    String respcondtitle = el_respcond.attributeValue("title");
    QTI_and qtiAnd = QTIHelper.getQTI_and();
    boolean fulfilled = qtiAnd.eval(el_condVar, itc, ect);
    // continue to set variables and display feedback if question was answered correctly
    if (fulfilled) {
        vars = itc.getVariables();
        List setvars = el_respcond.selectNodes("setvar");
        for (Iterator iter = setvars.iterator(); iter.hasNext(); ) {
            Element element = (Element) iter.next();
            String action = element.attributeValue("action");
            String varName = element.attributeValue("varname");
            if (varName == null)
                varName = "SCORE";
            varName.trim();
            String varVal = element.getText();
            Variable var = vars.getVariable(varName);
            if (var == null)
                throw new RuntimeException("var " + varName + " is in setvar, but was not declared ");
            if (action.equals("Set")) {
                var.setValue(varVal);
            } else {
                // Add | Subtract | Multiply | Divide
                if (action.equals("Add")) {
                    var.add(varVal);
                } else if (action.equals("Subtract")) {
                    var.subtract(varVal);
                } else if (action.equals("Multiply")) {
                    var.multiply(varVal);
                } else if (action.equals("Divide")) {
                    var.divide(varVal);
                }
            }
        }
        // set displayfeedback
        // <displayfeedback feedbacktype = "Response" linkrefid = "Correct"/>
        // <!ATTLIST displayfeedback  feedbacktype  (Response | Solution | Hint )  'Response' %I_LinkRefId; >
        Output output = itc.getOutput();
        List fbs = el_respcond.selectNodes("displayfeedback");
        for (Iterator it_fbs = fbs.iterator(); it_fbs.hasNext(); ) {
            Element el_dispfb = (Element) it_fbs.next();
            // must exist (dtd)
            String linkRefId = el_dispfb.attributeValue("linkrefid");
            // must exist (dtd)
            String feedbacktype = el_dispfb.attributeValue("feedbacktype");
            Element el_resolved = (Element) itc.getEl_item().selectSingleNode(".//itemfeedback[@ident='" + linkRefId + "']");
            if (el_resolved == null)
                continue;
            if (feedbacktype.equals("Response")) {
                // additional (olat) rule:
                // we want to render the original answer again in the simple case where the respcondition was generated
                // by the olat export and contains only one varequal.
                /*
					  
					 	 <response_label ident = "2">
			     			<flow_mat>
								<material>
										<mattext texttype="text/html">...</mattext>
								</material>
                  			</flow_mat>
                		</response_label> 
					  ...
					
					 	<respcondition title="_olat_resp_feedback" continue="Yes">
							<conditionvar>
								<varequal respident="Frage6549" case="Yes">2</varequal>
							</conditionvar>
            				<displayfeedback linkrefid="2"/>
						</respcondition>
						
						In this case, it is possible (and wished) to trace the feedback back to the answer which triggered this feedback.
						Such a respcondition is identified by the title which is exactly "_olat_resp_feedback".
						
						
					 */
                Element el_chosenanswer = null;
                if (respcondtitle != null && respcondtitle.equals("_olat_resp_feedback")) {
                    Element el_vareq = (Element) el_respcond.selectSingleNode(".//varequal");
                    String answerident = el_vareq.getText();
                    el_chosenanswer = (Element) itc.getEl_item().selectSingleNode(".//response_label[@ident='" + answerident + "']//material");
                }
                // give the whole itemfeedback to render
                output.addItem_El_response(el_chosenanswer, el_resolved);
            } else if (feedbacktype.equals("Solution")) {
                Element el_solution = (Element) el_resolved.selectSingleNode(".//solution");
                if (el_solution != null)
                    output.setSolution(new Solution(el_solution));
            } else if (feedbacktype.equals("Hint")) {
                // <!ENTITY % I_FeedbackStyle " feedbackstyle  (Complete | Incremental | Multilevel | Proprietary )  'Complete'">
                Element el_hint = (Element) el_resolved.selectSingleNode(".//hint");
                output.setHint(new Hint(el_hint));
            }
        }
    }
    return fulfilled;
}
Also used : Variables(org.olat.ims.qti.container.Variables) Variable(org.olat.ims.qti.container.Variable) Hint(org.olat.ims.qti.container.qtielements.Hint) Element(org.dom4j.Element) Output(org.olat.ims.qti.container.Output) Iterator(java.util.Iterator) List(java.util.List) Solution(org.olat.ims.qti.container.qtielements.Solution)

Aggregations

Iterator (java.util.Iterator)4 List (java.util.List)4 Element (org.dom4j.Element)4 Variable (org.olat.ims.qti.container.Variable)4 Variables (org.olat.ims.qti.container.Variables)4 DecimalVariable (org.olat.ims.qti.container.DecimalVariable)2 Output (org.olat.ims.qti.container.Output)2 Hint (org.olat.ims.qti.container.qtielements.Hint)2 Solution (org.olat.ims.qti.container.qtielements.Solution)2