Search in sources :

Example 6 with ItemInput

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

the class QTI_varlt method eval.

/**
 * var greater than or equal
 * qti ims 1.2.1
 * <!ELEMENT vargte (#PCDATA)>
 * <!ATTLIST vargte  %I_RespIdent;
 *                   %I_Index; >
 * e.g. <vargte respident = "NUM01">3.141</vargte>
 */
public boolean eval(Element boolElement, ItemContext userContext, EvalContext ect) {
    ItemInput iinp = userContext.getItemInput();
    // user has given no answer
    if (iinp.isEmpty())
        return false;
    String respident = boolElement.attributeValue("respident");
    // the answer is tested against content of elem.
    String shouldVal = boolElement.getText();
    String isVal = iinp.getSingle(respident);
    // the isVal and shouldVal must be numeric
    // we use Float so we are on the safe side, even if comparison was only Integer
    shouldVal = shouldVal.trim();
    isVal = isVal.trim();
    float fs = Float.parseFloat(shouldVal);
    float fi;
    try {
        fi = Float.parseFloat(isVal);
    } catch (NumberFormatException e) {
        // try to replace , -> .
        isVal = isVal.replace(',', '.');
        try {
            fi = Float.parseFloat(isVal);
        } catch (NumberFormatException e1) {
            // we try all what we can to understand the input value -> false
            return false;
        }
    }
    boolean ok = (fi < fs);
    return ok;
}
Also used : ItemInput(org.olat.ims.qti.container.ItemInput)

Example 7 with ItemInput

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

the class QTI_varlte method eval.

/**
 * var greater than or equal
 * qti ims 1.2.1
 * <!ELEMENT vargte (#PCDATA)>
 * <!ATTLIST vargte  %I_RespIdent;
 *                   %I_Index; >
 * e.g. <vargte respident = "NUM01">3.141</vargte>
 */
public boolean eval(Element boolElement, ItemContext userContext, EvalContext ect) {
    ItemInput iinp = userContext.getItemInput();
    // user has given no answer
    if (iinp.isEmpty())
        return false;
    String respident = boolElement.attributeValue("respident");
    // the answer is tested against content of elem.
    String shouldVal = boolElement.getText();
    String isVal = iinp.getSingle(respident);
    // the isVal and shouldVal must be numeric
    // we use Float so we are on the safe side, even if comparison was only Integer
    shouldVal = shouldVal.trim();
    isVal = isVal.trim();
    float fs = Float.parseFloat(shouldVal);
    float fi;
    try {
        fi = Float.parseFloat(isVal);
    } catch (NumberFormatException e) {
        // try to replace , -> .
        isVal = isVal.replace(',', '.');
        try {
            fi = Float.parseFloat(isVal);
        } catch (NumberFormatException e1) {
            // we try all what we can to understand the input value -> false
            return false;
        }
    }
    boolean ok = (fi <= fs);
    return ok;
}
Also used : ItemInput(org.olat.ims.qti.container.ItemInput)

Example 8 with ItemInput

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

the class Response_label method render.

/**
 * @see org.olat.ims.qti.container.qtielements.QTIElement#render(StringBuilder,
 *      RenderInstructions)
 */
@Override
public void render(StringBuilder buffer, RenderInstructions ri) {
    ItemInput iinput = (ItemInput) ri.get(RenderInstructions.KEY_ITEM_INPUT);
    String responseIdent = (String) ri.get(RenderInstructions.KEY_RESPONSE_IDENT);
    // find parent render_xxx element
    String renderClass = (String) ri.get(RenderInstructions.KEY_RENDER_CLASS);
    if (renderClass == null) {
    // we don't know what to do
    } else if (renderClass.equals("choice")) {
        renderChoice(buffer, ri, iinput);
    } else if (renderClass.equals("kprim")) {
        buffer.append("<tr><td class='o_qti_item_kprim_input'><input id=\"QTI_").append(ri.get(RenderInstructions.KEY_ITEM_IDENT)).append(getQTIIdent()).append("\" type=\"radio\" class=\"radio\" name=\"");
        appendParameterIdent(buffer, ri);
        buffer.append("\" value=\"" + getQTIIdent() + ":correct");
        if (iinput != null && !iinput.isEmpty()) {
            List<String> responses = iinput.getAsList(responseIdent);
            if (responses != null && responses.contains(getQTIIdent() + ":correct"))
                buffer.append("\" checked=\"checked");
        }
        buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\"/>");
        buffer.append("</td><td class='o_qti_item_kprim_input'><input id=\"QTI_").append(ri.get(RenderInstructions.KEY_ITEM_IDENT)).append(getQTIIdent()).append("\" type=\"radio\" class=\"radio\" name=\"");
        appendParameterIdent(buffer, ri);
        buffer.append("\" value=\"" + getQTIIdent() + ":wrong");
        if (iinput != null && !iinput.isEmpty()) {
            List<String> responses = iinput.getAsList(responseIdent);
            if (responses != null && responses.contains(getQTIIdent() + ":wrong"))
                buffer.append("\" checked=\"checked");
        }
        buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\"/>");
        buffer.append("</td><td>");
        super.render(buffer, ri);
        buffer.append("</td></tr>");
        ri.put(RenderInstructions.KEY_FLOW_LABEL, new Integer(RenderInstructions.RENDER_FLOW_BLOCK));
        addBr(ri, buffer);
    } else if (renderClass.equals("fib")) {
        Integer rows = (Integer) ri.get(RenderInstructions.KEY_FIB_ROWS);
        Integer columns = (Integer) ri.get(RenderInstructions.KEY_FIB_COLUMNS);
        Integer maxlength = (Integer) ri.get(RenderInstructions.KEY_FIB_MAXLENGTH);
        if (rows == null || columns == null || maxlength == null)
            throw new AssertException("Rows and/or columns attribute not specified for render_fib.");
        if (rows.intValue() > 1) {
            // render as textarea
            buffer.append("<textarea id=\"QTI_").append(getQTIIdent()).append("\" name=\"");
            appendParameterIdent(buffer, ri);
            buffer.append("\" class='form-control' rows=\"").append(rows).append("\" cols=\"").append(columns).append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\">");
            if (iinput != null && !iinput.isEmpty() && iinput.getSingle(responseIdent) != null) {
                buffer.append(iinput.getSingle(getQTIIdent()));
            }
            buffer.append("</textarea>");
        } else {
            // render as input string
            buffer.append("<span> </span><input id=\"QTI_").append(getQTIIdent()).append("\" name=\"");
            appendParameterIdent(buffer, ri);
            buffer.append("\" type=\"text\" size=\"").append(columns).append("\" maxlength=\"").append(maxlength);
            if (iinput != null && !iinput.isEmpty() && iinput.getSingle(responseIdent) != null) {
                buffer.append("\" value=\"").append(StringHelper.escapeHtml(iinput.getSingle(getQTIIdent())));
            }
            buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\" /><span> </span>");
        }
        addBr(ri, buffer);
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) ItemInput(org.olat.ims.qti.container.ItemInput)

Example 9 with ItemInput

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

the class IQManager method persistResults.

/**
 * Create the QTIResults on the database for a given assessments,
 * self-assessment or survey. These database entries can be used for
 * statistical downloads.
 *
 * @param ai
 * @param resId
 * @param resDetail
 * @param ureq
 */
public void persistResults(AssessmentInstance ai) {
    AssessmentContext ac = ai.getAssessmentContext();
    QTIResultSet qtiResultSet = new QTIResultSet();
    qtiResultSet.setLastModified(new Date(System.currentTimeMillis()));
    qtiResultSet.setOlatResource(ai.getCallingResId());
    qtiResultSet.setOlatResourceDetail(ai.getCallingResDetail());
    qtiResultSet.setRepositoryRef(ai.getRepositoryEntryKey());
    qtiResultSet.setIdentity(ai.getAssessedIdentity());
    qtiResultSet.setQtiType(ai.getType());
    qtiResultSet.setAssessmentID(ai.getAssessID());
    qtiResultSet.setDuration(new Long(ai.getAssessmentContext().getDuration()));
    if (ai.isSurvey()) {
        qtiResultSet.setScore(0);
        qtiResultSet.setIsPassed(true);
    } else {
        qtiResultSet.setScore(ac.getScore());
        qtiResultSet.setIsPassed(ac.isPassed());
    }
    dbInstance.getCurrentEntityManager().persist(qtiResultSet);
    // Loop over all sections in this assessment
    int sccnt = ac.getSectionContextCount();
    for (int i = 0; i < sccnt; i++) {
        // Loop over all items in this section
        SectionContext sc = ac.getSectionContext(i);
        int iccnt = sc.getItemContextCount();
        for (int j = 0; j < iccnt; j++) {
            ItemContext ic = sc.getItemContext(j);
            // Create new result item for this item
            QTIResult qtiResult = new QTIResult();
            qtiResult.setResultSet(qtiResultSet);
            qtiResult.setItemIdent(ic.getIdent());
            qtiResult.setDuration(new Long(ic.getTimeSpent()));
            if (ai.isSurvey())
                qtiResult.setScore(0);
            else
                qtiResult.setScore(ic.getScore());
            qtiResult.setTstamp(new Date(ic.getLatestAnswerTime()));
            qtiResult.setLastModified(new Date(System.currentTimeMillis()));
            qtiResult.setIp(ai.getRemoteAddr());
            // Get user answers for this item
            StringBuilder sb = new StringBuilder();
            if (ic.getItemInput() == null) {
            } else {
                ItemInput inp = ic.getItemInput();
                if (inp.isEmpty()) {
                    sb.append("[]");
                } else {
                    Map<String, List<String>> im = inp.getInputMap();
                    // Create answer block
                    Set<String> keys = im.keySet();
                    Iterator<String> iter = keys.iterator();
                    while (iter.hasNext()) {
                        String ident = iter.next();
                        // response_lid ident
                        sb.append(ident);
                        sb.append("[");
                        List<String> answers = inp.getAsList(ident);
                        for (int y = 0; y < answers.size(); y++) {
                            sb.append("[");
                            String answer = answers.get(y);
                            // answer is referenced to response_label ident, if
                            // render_choice
                            // answer is userinput, if render_fib
                            answer = quoteSpecialQTIResultCharacters(answer);
                            sb.append(answer);
                            sb.append("]");
                        }
                        sb.append("]");
                    }
                }
            }
            qtiResult.setAnswer(sb.toString());
            // Persist result data in database
            dbInstance.getCurrentEntityManager().persist(qtiResult);
        }
    }
}
Also used : SectionContext(org.olat.ims.qti.container.SectionContext) QTIResult(org.olat.ims.qti.QTIResult) HttpItemInput(org.olat.ims.qti.container.HttpItemInput) ItemInput(org.olat.ims.qti.container.ItemInput) Date(java.util.Date) QTIResultSet(org.olat.ims.qti.QTIResultSet) AssessmentContext(org.olat.ims.qti.container.AssessmentContext) ItemContext(org.olat.ims.qti.container.ItemContext) List(java.util.List) ArrayList(java.util.ArrayList)

Example 10 with ItemInput

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

the class Response_label method render.

/**
 * @see org.olat.ims.qti.container.qtielements.QTIElement#render(StringBuilder,
 *      RenderInstructions)
 */
@Override
public void render(StringBuilder buffer, RenderInstructions ri) {
    ItemInput iinput = (ItemInput) ri.get(RenderInstructions.KEY_ITEM_INPUT);
    String responseIdent = (String) ri.get(RenderInstructions.KEY_RESPONSE_IDENT);
    // find parent render_xxx element
    String renderClass = (String) ri.get(RenderInstructions.KEY_RENDER_CLASS);
    if (renderClass == null) {
    // we don't know what to do
    } else if (renderClass.equals("choice")) {
        renderChoice(buffer, ri, iinput);
    } else if (renderClass.equals("kprim")) {
        buffer.append("<tr><td class='o_qti_item_kprim_input'><input id=\"QTI_").append(ri.get(RenderInstructions.KEY_ITEM_IDENT)).append(getQTIIdent()).append("\" type=\"radio\" class=\"radio\" name=\"");
        appendParameterIdent(buffer, ri);
        buffer.append("\" value=\"" + getQTIIdent() + ":correct");
        if (iinput != null && !iinput.isEmpty()) {
            List<String> responses = iinput.getAsList(responseIdent);
            if (responses != null && responses.contains(getQTIIdent() + ":correct"))
                buffer.append("\" checked=\"checked");
        }
        buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\"/>");
        buffer.append("</td><td class='o_qti_item_kprim_input'><input id=\"QTI_").append(ri.get(RenderInstructions.KEY_ITEM_IDENT)).append(getQTIIdent()).append("\" type=\"radio\" class=\"radio\" name=\"");
        appendParameterIdent(buffer, ri);
        buffer.append("\" value=\"" + getQTIIdent() + ":wrong");
        if (iinput != null && !iinput.isEmpty()) {
            List<String> responses = iinput.getAsList(responseIdent);
            if (responses != null && responses.contains(getQTIIdent() + ":wrong"))
                buffer.append("\" checked=\"checked");
        }
        buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\"/>");
        buffer.append("</td><td>");
        super.render(buffer, ri);
        buffer.append("</td></tr>");
        ri.put(RenderInstructions.KEY_FLOW_LABEL, new Integer(RenderInstructions.RENDER_FLOW_BLOCK));
        addBr(ri, buffer);
    } else if (renderClass.equals("fib")) {
        Integer rows = (Integer) ri.get(RenderInstructions.KEY_FIB_ROWS);
        Integer columns = (Integer) ri.get(RenderInstructions.KEY_FIB_COLUMNS);
        Integer maxlength = (Integer) ri.get(RenderInstructions.KEY_FIB_MAXLENGTH);
        if (rows == null || columns == null || maxlength == null)
            throw new AssertException("Rows and/or columns attribute not specified for render_fib.");
        if (rows.intValue() > 1) {
            // render as textarea
            buffer.append("<textarea id=\"QTI_").append(getQTIIdent()).append("\" name=\"");
            appendParameterIdent(buffer, ri);
            buffer.append("\" class='form-control' rows=\"").append(rows).append("\" cols=\"").append(columns).append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\">");
            if (iinput != null && !iinput.isEmpty() && iinput.getSingle(responseIdent) != null) {
                buffer.append(iinput.getSingle(getQTIIdent()));
            }
            buffer.append("</textarea>");
        } else {
            // render as input string
            buffer.append("<span> </span><input id=\"QTI_").append(getQTIIdent()).append("\" name=\"");
            appendParameterIdent(buffer, ri);
            buffer.append("\" type=\"text\" size=\"").append(columns).append("\" maxlength=\"").append(maxlength);
            if (iinput != null && !iinput.isEmpty() && iinput.getSingle(responseIdent) != null) {
                buffer.append("\" value=\"").append(StringHelper.escapeHtml(iinput.getSingle(getQTIIdent())));
            }
            buffer.append("\" onchange=\"return setFormDirty('ofo_iq_item')\" onclick=\"return setFormDirty('ofo_iq_item')\" /><span> </span>");
        }
        addBr(ri, buffer);
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) ItemInput(org.olat.ims.qti.container.ItemInput)

Aggregations

ItemInput (org.olat.ims.qti.container.ItemInput)24 AssessmentContext (org.olat.ims.qti.container.AssessmentContext)10 ItemContext (org.olat.ims.qti.container.ItemContext)10 SectionContext (org.olat.ims.qti.container.SectionContext)10 HttpItemInput (org.olat.ims.qti.container.HttpItemInput)6 Date (java.util.Date)4 HashMap (java.util.HashMap)4 List (java.util.List)4 QTIResultSet (org.olat.ims.qti.QTIResultSet)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 Set (java.util.Set)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Document (org.dom4j.Document)2 DocumentFactory (org.dom4j.DocumentFactory)2 Element (org.dom4j.Element)2 XPath (org.dom4j.XPath)2 Translator (org.olat.core.gui.translator.Translator)2 IdentityEnvironment (org.olat.core.id.IdentityEnvironment)2