/** * <a href="http://www.openolat.org"> * OpenOLAT - Online Learning and Training</a><br> * <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 the * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a> * <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> * Initial code contributed and copyrighted by<br> * frentix GmbH, http://www.frentix.com * <p> */ package org.olat.ims.qti21.model.xml.interactions; import static org.olat.ims.qti21.model.xml.AssessmentItemFactory.appendDefaultItemBody; import static org.olat.ims.qti21.model.xml.AssessmentItemFactory.appendDefaultOutcomeDeclarations; import static org.olat.ims.qti21.model.xml.AssessmentItemFactory.appendDrawingInteraction; import static org.olat.ims.qti21.model.xml.AssessmentItemFactory.createDrawingResponseDeclaration; import static org.olat.ims.qti21.model.xml.AssessmentItemFactory.createResponseProcessing; import java.util.List; import javax.xml.transform.stream.StreamResult; import org.olat.core.gui.render.StringOutput; import org.olat.ims.qti21.model.QTI21QuestionType; import org.olat.ims.qti21.model.xml.AssessmentItemFactory; import uk.ac.ed.ph.jqtiplus.node.content.ItemBody; import uk.ac.ed.ph.jqtiplus.node.content.basic.Block; import uk.ac.ed.ph.jqtiplus.node.content.xhtml.object.Object; import uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem; import uk.ac.ed.ph.jqtiplus.node.item.interaction.DrawingInteraction; import uk.ac.ed.ph.jqtiplus.node.item.response.declaration.ResponseDeclaration; import uk.ac.ed.ph.jqtiplus.node.item.response.processing.ResponseProcessing; import uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer; import uk.ac.ed.ph.jqtiplus.types.Identifier; /** * * Initial date: 15 nov. 2016<br> * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com * */ public class DrawingAssessmentItemBuilder extends LobAssessmentItemBuilder { private DrawingInteraction drawingInteraction; public DrawingAssessmentItemBuilder(String title, QtiSerializer qtiSerializer) { super(createAssessmentItem(title), qtiSerializer); } public DrawingAssessmentItemBuilder(AssessmentItem assessmentItem, QtiSerializer qtiSerializer) { super(assessmentItem, qtiSerializer); } private static AssessmentItem createAssessmentItem(String title) { AssessmentItem assessmentItem = AssessmentItemFactory.createAssessmentItem(QTI21QuestionType.drawing, title); //define the response Identifier responseDeclarationId = Identifier.assumedLegal("RESPONSE_1"); ResponseDeclaration responseDeclaration = createDrawingResponseDeclaration(assessmentItem, responseDeclarationId); assessmentItem.getNodeGroups().getResponseDeclarationGroup().getResponseDeclarations().add(responseDeclaration); //outcomes appendDefaultOutcomeDeclarations(assessmentItem, 1.0d); ItemBody itemBody = appendDefaultItemBody(assessmentItem); appendDrawingInteraction(itemBody, responseDeclarationId); //response processing ResponseProcessing responseProcessing = createResponseProcessing(assessmentItem, responseDeclarationId); assessmentItem.getNodeGroups().getResponseProcessingGroup().setResponseProcessing(responseProcessing); return assessmentItem; } @Override public QTI21QuestionType getQuestionType() { return QTI21QuestionType.drawing; } @Override public void extract() { super.extract(); extractExtendedTextInteraction(); } private void extractExtendedTextInteraction() { StringOutput sb = new StringOutput(); List<Block> blocks = assessmentItem.getItemBody().getBlocks(); for(Block block:blocks) { if(block instanceof DrawingInteraction) { drawingInteraction = (DrawingInteraction)block; responseIdentifier = drawingInteraction.getResponseIdentifier(); break; } else { qtiSerializer.serializeJqtiObject(block, new StreamResult(sb)); } } question = sb.toString(); } public String getBackground() { Object graphichObject = drawingInteraction.getObject(); if(graphichObject != null) { return graphichObject.getData(); } return null; } public void setBackground(String data, String mimeType, int height, int width) { Object graphichObject = drawingInteraction.getObject(); if(graphichObject == null) { graphichObject = new Object(drawingInteraction); drawingInteraction.setObject(graphichObject); } graphichObject.setData(data); graphichObject.setType(mimeType); if(height > 0) { graphichObject.setHeight(Integer.toString(height)); } else { graphichObject.setHeight(null); } if(width > 0) { graphichObject.setWidth(Integer.toString(width)); } else { graphichObject.setWidth(null); } } @Override protected void buildResponseAndOutcomeDeclarations() { ResponseDeclaration responseDeclaration = createDrawingResponseDeclaration(assessmentItem, responseIdentifier); assessmentItem.getResponseDeclarations().add(responseDeclaration); } @Override protected void buildItemBody() { //remove current blocks List<Block> blocks = assessmentItem.getItemBody().getBlocks(); blocks.clear(); //add question getHtmlHelper().appendHtml(assessmentItem.getItemBody(), question); //add interaction blocks.add(drawingInteraction); } }