Search in sources :

Example 21 with Response

use of org.olat.ims.qti.editor.beecom.objects.Response in project openolat by klemens.

the class QTIEditHelper method getMediaReferences.

/**
 * Retrieves all referenced media by thisItem if filterOut is false,
 * or all referenced media by other items if filterOut is true.
 * <p>
 * Iterates over all sections, items, etc. </br>
 * -> if filterOut is true gets all references except those for thisItem.
 * -> if filterOut is false gets all references for thisItem.
 *
 * @param qtiDocument
 * @param thisItem
 * @param filterOut
 * @return Returns empty set if no reference found.
 */
private static Set<String> getMediaReferences(QTIDocument qtiDocument, Item thisItem, boolean filterOut) {
    HashSet<String> returnSet = new HashSet<String>();
    // sections
    List sectionList = qtiDocument.getAssessment().getSections();
    Iterator sectionIterator = sectionList.iterator();
    while (sectionIterator.hasNext()) {
        // section
        Section section = (Section) sectionIterator.next();
        List itemList = section.getItems();
        Iterator listIterator = itemList.iterator();
        while (listIterator.hasNext()) {
            // item
            Item item = (Item) listIterator.next();
            if ((filterOut && thisItem.getIdent().equals(item.getIdent())) || (!filterOut && !thisItem.getIdent().equals(item.getIdent()))) {
                continue;
            }
            // question
            Material material = item.getQuestion().getQuestion();
            if (material != null) {
                String htmlContent = material.renderAsHtmlForEditor();
                // parse filenames
                returnSet.addAll(getMediaFileNames(htmlContent));
            }
            // responses
            List responseList = item.getQuestion().getResponses();
            Iterator responseIterator = responseList.iterator();
            while (responseIterator.hasNext()) {
                Response response = (Response) responseIterator.next();
                Material responseMat = response.getContent();
                // parse filenames
                if (responseMat != null) {
                    returnSet.addAll(getMediaFileNames(responseMat.renderAsHtmlForEditor()));
                }
                // response-level feedback
                Material responseFeedbackMat = QTIEditHelper.getFeedbackOlatRespMaterial(item, response.getIdent());
                if (responseFeedbackMat != null) {
                    returnSet.addAll(getMediaFileNames(responseFeedbackMat.renderAsHtmlForEditor()));
                }
            }
            // feedback
            Material masteryMat = QTIEditHelper.getFeedbackMasteryMaterial(item);
            if (masteryMat != null) {
                returnSet.addAll(getMediaFileNames(masteryMat.renderAsHtmlForEditor()));
            }
            Material failureMat = QTIEditHelper.getFeedbackFailMaterial(item);
            if (failureMat != null) {
                returnSet.addAll(getMediaFileNames(failureMat.renderAsHtmlForEditor()));
            }
        }
    }
    return returnSet;
}
Also used : ChoiceResponse(org.olat.ims.qti.editor.beecom.objects.ChoiceResponse) FIBResponse(org.olat.ims.qti.editor.beecom.objects.FIBResponse) Response(org.olat.ims.qti.editor.beecom.objects.Response) EssayResponse(org.olat.ims.qti.editor.beecom.objects.EssayResponse) Item(org.olat.ims.qti.editor.beecom.objects.Item) VFSItem(org.olat.core.util.vfs.VFSItem) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Material(org.olat.ims.qti.editor.beecom.objects.Material) Section(org.olat.ims.qti.editor.beecom.objects.Section) HashSet(java.util.HashSet)

Example 22 with Response

use of org.olat.ims.qti.editor.beecom.objects.Response in project openolat by klemens.

the class QTIEditHelper method calculateMaxScore.

/**
 * Calculates the max score for a question (sum of scores)
 * @param question
 * @return max score.
 */
public static float calculateMaxScore(Question question) {
    float tmpScore = 0;
    if (question.isSingleCorrect())
        return question.getSingleCorrectScore();
    for (Iterator<Response> iter = question.getResponses().iterator(); iter.hasNext(); ) {
        Response resp = iter.next();
        float points = resp.getPoints();
        if (points > 0)
            tmpScore = tmpScore + points;
    }
    return tmpScore;
}
Also used : ChoiceResponse(org.olat.ims.qti.editor.beecom.objects.ChoiceResponse) FIBResponse(org.olat.ims.qti.editor.beecom.objects.FIBResponse) Response(org.olat.ims.qti.editor.beecom.objects.Response) EssayResponse(org.olat.ims.qti.editor.beecom.objects.EssayResponse)

Example 23 with Response

use of org.olat.ims.qti.editor.beecom.objects.Response in project openolat by klemens.

the class QTIEditHelper method fetchChoices.

/**
 * Fetch choices.
 * @param response_labels
 * @return Map of choices.
 */
public static List<Response> fetchChoices(List response_labels) {
    List<Response> choices = new ArrayList<Response>();
    for (Iterator i = response_labels.iterator(); i.hasNext(); ) {
        ChoiceResponse choice = new ChoiceResponse();
        Element response_label = (Element) i.next();
        choice.setIdent(response_label.attributeValue("ident"));
        List materials = response_label.selectNodes(".//material");
        Material content = new Material();
        for (Iterator iter = materials.iterator(); iter.hasNext(); ) {
            Element el_material = (Element) iter.next();
            Material mat = (Material) parserManager.parse(el_material);
            content.getElements().addAll(mat.getElements());
        }
        // assure material always has some content
        if (content.getElements().size() == 0) {
            content.getElements().add(new Mattext(""));
        }
        choice.setContent(content);
        choices.add(choice);
    }
    return choices;
}
Also used : ChoiceResponse(org.olat.ims.qti.editor.beecom.objects.ChoiceResponse) FIBResponse(org.olat.ims.qti.editor.beecom.objects.FIBResponse) Response(org.olat.ims.qti.editor.beecom.objects.Response) EssayResponse(org.olat.ims.qti.editor.beecom.objects.EssayResponse) ChoiceResponse(org.olat.ims.qti.editor.beecom.objects.ChoiceResponse) Mattext(org.olat.ims.qti.editor.beecom.objects.Mattext) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Material(org.olat.ims.qti.editor.beecom.objects.Material)

Example 24 with Response

use of org.olat.ims.qti.editor.beecom.objects.Response in project openolat by klemens.

the class QTIEditorMainController method createChangeMessage.

/**
 * helper method to create the change log message
 *
 * @return
 */
private String createChangeMessage() {
    // FIXME:pb:break down into smaller pieces
    final StringBuilder result = new StringBuilder();
    if (isRestrictedEdit()) {
        Visitor v = new Visitor() {

            /*
				 * a history key is built as follows
				 * sectionkey+"/"+itemkey+"/"+questionkey+"/"+responsekey
				 */
            String sectionKey = null;

            Map<String, String> itemMap = new HashMap<>();

            public void visit(INode node) {
                if (node instanceof AssessmentNode) {
                    AssessmentNode an = (AssessmentNode) node;
                    String key = "null/null/null/null";
                    if (history.containsKey(key)) {
                        // some assessment top level data changed
                        Memento mem = history.get(key);
                        result.append("---+ Changes in test " + formatVariable(startedWithTitle) + ":");
                        result.append(an.createChangeMessage(mem));
                    }
                } else if (node instanceof SectionNode) {
                    SectionNode sn = (SectionNode) node;
                    String tmpKey = ((Section) sn.getUnderlyingQTIObject()).getIdent();
                    String key = tmpKey + "/null/null/null";
                    if (history.containsKey(key)) {
                        // some section only data changed
                        Memento mem = history.get(key);
                        result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
                        result.append(sn.createChangeMessage(mem));
                    }
                } else if (node instanceof ItemNode) {
                    ItemNode in = (ItemNode) node;
                    SectionNode sn = (SectionNode) in.getParent();
                    String parentSectkey = ((Section) ((SectionNode) in.getParent()).getUnderlyingQTIObject()).getIdent();
                    Item item = (Item) in.getUnderlyingQTIObject();
                    Question question = item.getQuestion();
                    String itemKey = item.getIdent();
                    String prefixKey = "null/" + itemKey;
                    String questionIdent = question != null ? question.getQuestion().getId() : "null";
                    String key = prefixKey + "/" + questionIdent + "/null";
                    StringBuilder changeMessage = new StringBuilder();
                    boolean hasChanges = false;
                    if (!itemMap.containsKey(itemKey)) {
                        Memento questMem = null;
                        Memento respMem = null;
                        if (history.containsKey(key)) {
                            // question changed!
                            questMem = history.get(key);
                            hasChanges = true;
                        }
                        // if(!hasChanges){
                        // check if a response changed
                        // new prefix for responses
                        prefixKey += "/null/";
                        // list contains org.olat.ims.qti.editor.beecom.objects.Response
                        List<Response> responses = question != null ? question.getResponses() : null;
                        if (responses != null && responses.size() > 0) {
                            // check for changes in each response
                            for (Iterator<Response> iter = responses.iterator(); iter.hasNext(); ) {
                                Response resp = iter.next();
                                if (history.containsKey(prefixKey + resp.getIdent())) {
                                    // this response changed!
                                    Memento tmpMem = history.get(prefixKey + resp.getIdent());
                                    if (respMem != null) {
                                        respMem = respMem.getTimestamp() > tmpMem.getTimestamp() ? tmpMem : respMem;
                                    } else {
                                        hasChanges = true;
                                        respMem = tmpMem;
                                    }
                                }
                            }
                        }
                        // output message
                        if (hasChanges) {
                            Memento mem = null;
                            if (questMem != null && respMem != null) {
                                // use the earlier memento
                                mem = questMem.getTimestamp() > respMem.getTimestamp() ? respMem : questMem;
                            } else if (questMem != null) {
                                mem = questMem;
                            } else if (respMem != null) {
                                mem = respMem;
                            }
                            changeMessage.append(in.createChangeMessage(mem));
                            itemMap.put(itemKey, itemKey);
                            if (!parentSectkey.equals(sectionKey)) {
                                // either this item belongs to a new section or no section
                                // is active
                                result.append("\n---++ Section " + formatVariable(sn.getAltText()) + " changes:");
                                result.append("\n").append(changeMessage);
                                sectionKey = parentSectkey;
                            } else {
                                result.append("\n").append(changeMessage);
                            }
                        }
                    }
                }
            }

            private String formatVariable(String var) {
                if (StringHelper.containsNonWhitespace(var)) {
                    return var;
                }
                return "[no entry]";
            }
        };
        TreeVisitor tv = new TreeVisitor(v, menuTreeModel.getRootNode(), false);
        tv.visitAll();
    }
    /*
		 * 
		 */
    return result.toString();
}
Also used : INode(org.olat.core.util.nodes.INode) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Visitor(org.olat.core.util.tree.Visitor) AssessmentNode(org.olat.ims.qti.editor.tree.AssessmentNode) SectionNode(org.olat.ims.qti.editor.tree.SectionNode) Section(org.olat.ims.qti.editor.beecom.objects.Section) Response(org.olat.ims.qti.editor.beecom.objects.Response) TreeVisitor(org.olat.core.util.tree.TreeVisitor) Memento(org.olat.core.util.memento.Memento) Item(org.olat.ims.qti.editor.beecom.objects.Item) ItemNode(org.olat.ims.qti.editor.tree.ItemNode) ChoiceQuestion(org.olat.ims.qti.editor.beecom.objects.ChoiceQuestion) Question(org.olat.ims.qti.editor.beecom.objects.Question) Map(java.util.Map) HashMap(java.util.HashMap)

Example 25 with Response

use of org.olat.ims.qti.editor.beecom.objects.Response in project openolat by klemens.

the class FIBItemController method event.

/**
 * @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 == main) {
        // olat::: as: improve easy fix since almost all operations change the main vc.
        main.setDirty(true);
        String cmd = event.getCommand();
        String sPosid = ureq.getParameter("posid");
        int posid = 0;
        if (sPosid != null)
            posid = Integer.parseInt(sPosid);
        if (cmd.equals("up")) {
            List<Response> elements = item.getQuestion().getResponses();
            if (posid > 0 && posid < item.getQuestion().getResponses().size()) {
                Response obj = elements.remove(posid);
                elements.add(posid - 1, obj);
            } else {
                logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
            }
        } else if (cmd.equals("down")) {
            List<Response> elements = item.getQuestion().getResponses();
            if (posid >= 0 && posid < elements.size() - 1) {
                Response obj = elements.remove(posid);
                elements.add(posid + 1, obj);
            } else {
                logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
            }
        } else if (cmd.equals("editq")) {
            editQuestion = item.getQuestion().getQuestion();
            displayMaterialFormController(ureq, editQuestion, restrictedEdit);
        } else if (cmd.equals("editr")) {
            List<Response> elements = item.getQuestion().getResponses();
            if (posid >= 0 && posid < elements.size()) {
                editResponse = elements.get(posid);
                Material responseMat = elements.get(posid).getContent();
                displayMaterialFormController(ureq, responseMat, restrictedEdit);
            } else {
                logError("posid doesn't match responses length: " + posid + "/" + elements.size(), null);
            }
        } else if (cmd.equals("addtext")) {
            FIBQuestion fib = (FIBQuestion) item.getQuestion();
            FIBResponse response = new FIBResponse();
            response.setType(FIBResponse.TYPE_CONTENT);
            Material mat = new Material();
            mat.add(new Mattext(translate("newtextelement")));
            response.setContent(mat);
            fib.getResponses().add(response);
        } else if (cmd.equals("addblank")) {
            FIBQuestion fib = (FIBQuestion) item.getQuestion();
            FIBResponse response = new FIBResponse();
            response.setType(FIBResponse.TYPE_BLANK);
            response.setCorrectBlank("");
            // default value
            response.setPoints(1f);
            fib.getResponses().add(response);
        } else if (cmd.equals("del")) {
            delYesNoCtrl = DialogBoxUIFactory.createYesNoDialog(ureq, getWindowControl(), null, translate("confirm.delete.element"));
            listenTo(delYesNoCtrl);
            delYesNoCtrl.setUserObject(new Integer(posid));
            delYesNoCtrl.activate();
        } else if (cmd.equals("sfib")) {
            // submit fib
            FIBQuestion question = (FIBQuestion) item.getQuestion();
            // Survey specific variables
            if (surveyMode) {
                List<Response> responses = question.getResponses();
                for (int i = 0; i < responses.size(); i++) {
                    FIBResponse response = (FIBResponse) responses.get(i);
                    if (FIBResponse.TYPE_BLANK.equals(response.getType())) {
                        // Set size of input field
                        String size = ureq.getParameter("size_q" + i);
                        if (size != null)
                            response.setSizeFromString(size);
                        String maxLength = ureq.getParameter("maxl_q" + i);
                        if (maxLength != null)
                            response.setMaxLengthFromString(maxLength);
                    }
                }
            } else {
                // set min/max values before single_correct !!
                if (!restrictedEdit) {
                    // only in full edit mode the following fields are available:
                    // min_value, max_value, valuation_method
                    question.setMinValue(ureq.getParameter("min_value"));
                    question.setMaxValue(ureq.getParameter("max_value"));
                    question.setSingleCorrect("single".equals(ureq.getParameter("valuation_method")));
                    if (question.isSingleCorrect()) {
                        question.setSingleCorrectScore(ureq.getParameter("single_score"));
                    } else {
                        question.setSingleCorrectScore(0.0f);
                    }
                }
                NodeBeforeChangeEvent nce = new NodeBeforeChangeEvent();
                nce.setItemIdent(item.getIdent());
                List<Response> responses = question.getResponses();
                for (int i = 0; i < responses.size(); i++) {
                    FIBResponse response = (FIBResponse) responses.get(i);
                    nce.setResponseIdent(response.getIdent());
                    fireEvent(ureq, nce);
                    response.setPoints(ureq.getParameter("points_q" + i));
                    if (FIBResponse.TYPE_BLANK.equals(response.getType())) {
                        response.setCorrectBlank(ureq.getParameter("content_q" + i));
                        // Set case sensitiveness
                        String caseSensitive = ureq.getParameter("case_q" + i);
                        if (caseSensitive == null)
                            caseSensitive = "No";
                        response.setCaseSensitive(caseSensitive);
                        // Set size of input field
                        String size = ureq.getParameter("size_q" + i);
                        if (size != null)
                            response.setSizeFromString(size);
                        String maxLength = ureq.getParameter("maxl_q" + i);
                        if (maxLength != null)
                            response.setMaxLengthFromString(maxLength);
                        // find longest correct blank in all synonyms of
                        // correct answers, fix max lenght if a longer value
                        // is found
                        String[] allCorrect = response.getCorrectBlank().split(";");
                        int longestCorrect = 0;
                        for (int j = 0; j < allCorrect.length; j++) {
                            String singleCorrect = allCorrect[j];
                            if (singleCorrect.length() > longestCorrect) {
                                longestCorrect = singleCorrect.length();
                            }
                        }
                        if (longestCorrect > response.getMaxLength())
                            response.setMaxLength(longestCorrect);
                    }
                }
            }
        }
        qtiPackage.serializeQTIDocument();
    }
}
Also used : FIBResponse(org.olat.ims.qti.editor.beecom.objects.FIBResponse) Response(org.olat.ims.qti.editor.beecom.objects.Response) FIBResponse(org.olat.ims.qti.editor.beecom.objects.FIBResponse) Mattext(org.olat.ims.qti.editor.beecom.objects.Mattext) List(java.util.List) Material(org.olat.ims.qti.editor.beecom.objects.Material) FIBQuestion(org.olat.ims.qti.editor.beecom.objects.FIBQuestion)

Aggregations

Response (org.olat.ims.qti.editor.beecom.objects.Response)60 FIBResponse (org.olat.ims.qti.editor.beecom.objects.FIBResponse)44 ArrayList (java.util.ArrayList)32 ChoiceResponse (org.olat.ims.qti.editor.beecom.objects.ChoiceResponse)26 ChoiceQuestion (org.olat.ims.qti.editor.beecom.objects.ChoiceQuestion)22 Question (org.olat.ims.qti.editor.beecom.objects.Question)22 EssayResponse (org.olat.ims.qti.editor.beecom.objects.EssayResponse)20 Material (org.olat.ims.qti.editor.beecom.objects.Material)20 HashMap (java.util.HashMap)14 Item (org.olat.ims.qti.editor.beecom.objects.Item)14 EssayQuestion (org.olat.ims.qti.editor.beecom.objects.EssayQuestion)12 List (java.util.List)10 FIBQuestion (org.olat.ims.qti.editor.beecom.objects.FIBQuestion)10 StatisticSurveyItemResponse (org.olat.ims.qti.statistics.model.StatisticSurveyItemResponse)10 Mattext (org.olat.ims.qti.editor.beecom.objects.Mattext)8 StatisticAnswerOption (org.olat.ims.qti.statistics.model.StatisticAnswerOption)8 StatisticChoiceOption (org.olat.ims.qti.statistics.model.StatisticChoiceOption)8 File (java.io.File)6 URL (java.net.URL)6 Test (org.junit.Test)6