Search in sources :

Example 71 with JqtiExtensionManager

use of uk.ac.ed.ph.jqtiplus.JqtiExtensionManager in project openolat by klemens.

the class OnyxToQtiWorksAssessementTestsTest method fixAssessmentTest.

@Test
public void fixAssessmentTest() throws IOException, XMLStreamException, SAXException, ParserConfigurationException, URISyntaxException {
    URL xmlUrl = OnyxToQtiWorksAssessementTestsTest.class.getResource("resources/onyx/" + xmlFilename);
    File xmlFile = new File(xmlUrl.toURI());
    File tmpDir = new File(WebappHelper.getTmpDir(), "onyx" + UUID.randomUUID());
    tmpDir.mkdirs();
    File outputFile = new File(tmpDir, "text.xml");
    try (InputStream in = Files.newInputStream(xmlFile.toPath());
        Writer out = Files.newBufferedWriter(outputFile.toPath(), Charset.forName("UTF-8"))) {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xtw = xof.createXMLStreamWriter(out);
        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        DefaultHandler2 myHandler = new Onyx38ToQtiWorksHandler(xtw);
        saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
        saxParser.parse(in, myHandler);
    } catch (Exception e1) {
        log.error("", e1);
        throw e1;
    }
    QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
    ResourceLocator fileResourceLocator = new PathResourceLocator(outputFile.toPath());
    AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
    ResolvedAssessmentTest resolvedAssessmentTest = assessmentObjectXmlLoader.loadAndResolveAssessmentTest(outputFile.toURI());
    Assert.assertNotNull(resolvedAssessmentTest);
    AssessmentTest assessmentTest = resolvedAssessmentTest.getRootNodeLookup().extractIfSuccessful();
    TestValidationResult testResult = assessmentObjectXmlLoader.loadResolveAndValidateTest(outputFile.toURI());
    BadResourceException e = testResult.getResolvedAssessmentTest().getTestLookup().getBadResourceException();
    if (e != null) {
        StringBuilder err = new StringBuilder();
        BadRessourceHelper.extractMessage(e, err);
        log.error(err.toString());
    }
    FileUtils.deleteDirsAndFiles(tmpDir.toPath());
    Assert.assertNotNull(assessmentTest);
    Assert.assertFalse(xmlFilename + " has fatal errors", BadRessourceHelper.hasFatalErrors(e));
}
Also used : AssessmentObjectXmlLoader(uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) DefaultHandler2(org.xml.sax.ext.DefaultHandler2) BadResourceException(uk.ac.ed.ph.jqtiplus.provision.BadResourceException) InputStream(java.io.InputStream) TestValidationResult(uk.ac.ed.ph.jqtiplus.validation.TestValidationResult) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) XMLStreamException(javax.xml.stream.XMLStreamException) BadResourceException(uk.ac.ed.ph.jqtiplus.provision.BadResourceException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) QtiXmlReader(uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader) JqtiExtensionManager(uk.ac.ed.ph.jqtiplus.JqtiExtensionManager) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) ResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) Test(org.junit.Test)

Example 72 with JqtiExtensionManager

use of uk.ac.ed.ph.jqtiplus.JqtiExtensionManager in project openolat by klemens.

the class SingleChoiceAssessmentItemBuilderTest method loadAssessmentItem.

private AssessmentItem loadAssessmentItem(URL itemUrl) throws URISyntaxException {
    QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
    ResourceLocator fileResourceLocator = new PathResourceLocator(Paths.get(itemUrl.toURI()));
    AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
    ResolvedAssessmentItem item = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(itemUrl.toURI());
    return item.getItemLookup().getRootNodeHolder().getRootNode();
}
Also used : AssessmentObjectXmlLoader(uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader) QtiXmlReader(uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader) JqtiExtensionManager(uk.ac.ed.ph.jqtiplus.JqtiExtensionManager) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) ResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator)

Example 73 with JqtiExtensionManager

use of uk.ac.ed.ph.jqtiplus.JqtiExtensionManager in project openolat by klemens.

the class SingleChoiceAssessmentItemBuilderTest method readSingleChoice.

/**
 * Check if a bare bone multiple choice created with our builder make a valid assessmentItem.
 *
 * @throws IOException
 * @throws URISyntaxException
 */
@Test
public void readSingleChoice() throws IOException, URISyntaxException {
    QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
    URL itemUrl = AssessmentItemBuilderTest.class.getResource("resources/openolat/single-choice-feedbacks.xml");
    AssessmentItem assessmentItem = loadAssessmentItem(itemUrl);
    SingleChoiceAssessmentItemBuilder itemBuilder = new SingleChoiceAssessmentItemBuilder(assessmentItem, qtiSerializer);
    // basic check ChoiceInteraction
    AssessmentItem loadedItem = itemBuilder.getAssessmentItem();
    List<Interaction> interactions = loadedItem.getItemBody().findInteractions();
    Assert.assertNotNull(interactions);
    Assert.assertEquals(1, interactions.size());
    Assert.assertTrue(interactions.get(0) instanceof ChoiceInteraction);
    // correct feedback
    ModalFeedbackBuilder correctFeedback = itemBuilder.getCorrectFeedback();
    Assert.assertNotNull(correctFeedback);
    Assert.assertNotNull(correctFeedback.getText());
    Assert.assertTrue(correctFeedback.getText().contains("This is the correct answer"));
    // incorrect
    ModalFeedbackBuilder incorrectFeedback = itemBuilder.getIncorrectFeedback();
    Assert.assertNotNull(incorrectFeedback);
    Assert.assertNotNull(incorrectFeedback.getText());
    Assert.assertTrue(incorrectFeedback.getText().contains("This is the wrong answer"));
    // correct answer
    SimpleChoice correctChoice = itemBuilder.getChoice(Identifier.assumedLegal("id87d42b76-93d7-42fc-bdec-3e2419fa901d"));
    Assert.assertTrue(itemBuilder.isCorrect(correctChoice));
    // score per
    Assert.assertEquals(ScoreEvaluation.allCorrectAnswers, itemBuilder.getScoreEvaluationMode());
}
Also used : SimpleChoice(uk.ac.ed.ph.jqtiplus.node.item.interaction.choice.SimpleChoice) JqtiExtensionManager(uk.ac.ed.ph.jqtiplus.JqtiExtensionManager) SingleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder) QtiSerializer(uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer) Interaction(uk.ac.ed.ph.jqtiplus.node.item.interaction.Interaction) ChoiceInteraction(uk.ac.ed.ph.jqtiplus.node.item.interaction.ChoiceInteraction) ChoiceInteraction(uk.ac.ed.ph.jqtiplus.node.item.interaction.ChoiceInteraction) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) URL(java.net.URL) Test(org.junit.Test)

Example 74 with JqtiExtensionManager

use of uk.ac.ed.ph.jqtiplus.JqtiExtensionManager in project openolat by klemens.

the class QTI12To21ConverterTest method validateAssessmentTest.

private boolean validateAssessmentTest(File assessmentTestFile) {
    QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
    ResourceLocator fileResourceLocator = new PathResourceLocator(assessmentTestFile.toPath());
    AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
    TestValidationResult test = assessmentObjectXmlLoader.loadResolveAndValidateTest(assessmentTestFile.toURI());
    for (Notification notification : test.getModelValidationErrors()) {
        log.error(notification.getQtiNode() + " : " + notification.getMessage());
    }
    BadResourceException e = test.getResolvedAssessmentTest().getTestLookup().getBadResourceException();
    if (e != null) {
        StringBuilder err = new StringBuilder();
        BadRessourceHelper.extractMessage(e, err);
        log.error(err.toString());
    }
    return test.getModelValidationErrors().isEmpty();
}
Also used : AssessmentObjectXmlLoader(uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader) QtiXmlReader(uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader) BadResourceException(uk.ac.ed.ph.jqtiplus.provision.BadResourceException) JqtiExtensionManager(uk.ac.ed.ph.jqtiplus.JqtiExtensionManager) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator) TestValidationResult(uk.ac.ed.ph.jqtiplus.validation.TestValidationResult) Notification(uk.ac.ed.ph.jqtiplus.notification.Notification) ResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator) PathResourceLocator(org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator)

Example 75 with JqtiExtensionManager

use of uk.ac.ed.ph.jqtiplus.JqtiExtensionManager in project openolat by klemens.

the class AssessmentHtmlBuilderTest method filter.

@Test
public void filter() throws IOException {
    String content = "<html><p>Test \u00EA<strong><span><img src='img.jpg'></span></strong></p><p>Test 2</p></html>";
    AssessmentItem item = new AssessmentItem();
    ItemBody helper = new ItemBody(item);
    new AssessmentHtmlBuilder().appendHtml(helper, content);
    List<Block> paragraphs = helper.getBlocks();
    Assert.assertNotNull(paragraphs);
    Assert.assertEquals(2, paragraphs.size());
    // The serializer can throw some exceptions if it doens't like the model
    // we want to serialize.
    StringOutput sb = new StringOutput();
    QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
    qtiSerializer.serializeJqtiObject(helper, new StreamResult(sb));
    String serializedQti = sb.toString();
    Assert.assertTrue(serializedQti.contains("img.jpg"));
    sb.close();
}
Also used : ItemBody(uk.ac.ed.ph.jqtiplus.node.content.ItemBody) JqtiExtensionManager(uk.ac.ed.ph.jqtiplus.JqtiExtensionManager) StreamResult(javax.xml.transform.stream.StreamResult) QtiSerializer(uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer) Block(uk.ac.ed.ph.jqtiplus.node.content.basic.Block) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) StringOutput(org.olat.core.gui.render.StringOutput) Test(org.junit.Test)

Aggregations

JqtiExtensionManager (uk.ac.ed.ph.jqtiplus.JqtiExtensionManager)78 Test (org.junit.Test)52 ResolvedAssessmentItem (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem)52 QtiSerializer (uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer)46 AssessmentItem (uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem)42 PathResourceLocator (org.olat.fileresource.types.ImsQTI21Resource.PathResourceLocator)34 AssessmentObjectXmlLoader (uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader)34 QtiXmlReader (uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader)34 ResourceLocator (uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator)34 URL (java.net.URL)30 Interaction (uk.ac.ed.ph.jqtiplus.node.item.interaction.Interaction)26 ItemValidationResult (uk.ac.ed.ph.jqtiplus.validation.ItemValidationResult)26 ChoiceInteraction (uk.ac.ed.ph.jqtiplus.node.item.interaction.ChoiceInteraction)24 BadResourceException (uk.ac.ed.ph.jqtiplus.provision.BadResourceException)20 File (java.io.File)16 URISyntaxException (java.net.URISyntaxException)16 IOException (java.io.IOException)14 SimpleChoice (uk.ac.ed.ph.jqtiplus.node.item.interaction.choice.SimpleChoice)14 SingleChoiceAssessmentItemBuilder (org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder)12 EndAttemptInteraction (uk.ac.ed.ph.jqtiplus.node.item.interaction.EndAttemptInteraction)12