Search in sources :

Example 1 with AssessmentItemsAndResources

use of org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources in project OpenOLAT by OpenOLAT.

the class QTI21ExportProcessor method exportToQTIEditor.

public ResolvedAssessmentItem exportToQTIEditor(QuestionItemFull fullItem, File editorContainer) throws IOException {
    AssessmentItemsAndResources itemAndMaterials = new AssessmentItemsAndResources();
    collectMaterials(fullItem, itemAndMaterials);
    if (itemAndMaterials.getAssessmentItems().isEmpty()) {
        // nothing found
        return null;
    }
    ResolvedAssessmentItem assessmentItem = itemAndMaterials.getAssessmentItems().get(0);
    // write materials
    for (ItemMaterial material : itemAndMaterials.getMaterials()) {
        String exportPath = material.getExportUri();
        File originalFile = material.getFile();
        File exportFile = new File(editorContainer, exportPath);
        if (!exportFile.getParentFile().exists()) {
            exportFile.getParentFile().mkdirs();
        }
        FileUtils.bcopy(originalFile, exportFile, "Copy material QTI 2.1");
    }
    return assessmentItem;
}
Also used : AssessmentItemsAndResources(org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) ItemMaterial(org.olat.ims.qti21.pool.ImportExportHelper.ItemMaterial) File(java.io.File)

Example 2 with AssessmentItemsAndResources

use of org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources in project OpenOLAT by OpenOLAT.

the class QTI21ExportProcessor method assembleTest.

public void assembleTest(List<QuestionItemFull> fullItems, File directory) {
    try {
        QtiSerializer qtiSerializer = qtiService.qtiSerializer();
        // imsmanifest
        ManifestBuilder manifest = ManifestBuilder.createAssessmentTestBuilder();
        // assessment test
        DoubleAdder atomicMaxScore = new DoubleAdder();
        AssessmentTest assessmentTest = AssessmentTestFactory.createAssessmentTest("Assessment test from pool", "Section");
        String assessmentTestFilename = assessmentTest.getIdentifier() + ".xml";
        manifest.appendAssessmentTest(assessmentTestFilename);
        // make a section
        AssessmentSection section = assessmentTest.getTestParts().get(0).getAssessmentSections().get(0);
        // assessment items
        for (QuestionItemFull qitem : fullItems) {
            File resourceDirectory = qpoolFileStorage.getDirectory(qitem.getDirectory());
            File itemFile = new File(resourceDirectory, qitem.getRootFilename());
            ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItemForCopy(itemFile.toURI(), resourceDirectory);
            AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
            assessmentItem.setIdentifier(QTI21QuestionType.generateNewIdentifier(assessmentItem.getIdentifier()));
            // save the item in its own container
            String container = qitem.getKey().toString();
            File containerDir = new File(directory, container);
            containerDir.mkdirs();
            File newItemFile = new File(containerDir, assessmentItem.getIdentifier() + ".xml");
            String newItemFilename = container + "/" + newItemFile.getName();
            qtiService.persistAssessmentObject(newItemFile, assessmentItem);
            AssessmentTestFactory.appendAssessmentItem(section, newItemFilename);
            manifest.appendAssessmentItem(newItemFilename);
            ManifestMetadataBuilder metadata = manifest.getResourceBuilderByHref(newItemFilename);
            metadata.appendMetadataFrom(qitem, resolvedAssessmentItem, locale);
            Double maxScore = QtiNodesExtractor.extractMaxScore(assessmentItem);
            if (maxScore != null) {
                atomicMaxScore.add(maxScore.doubleValue());
            }
            // write materials
            AssessmentItemsAndResources materials = new AssessmentItemsAndResources();
            ImportExportHelper.getMaterials(assessmentItem, itemFile, materials);
            for (ItemMaterial material : materials.getMaterials()) {
                String exportPath = material.getExportUri();
                File originalFile = material.getFile();
                File exportFile = new File(containerDir, exportPath);
                if (!exportFile.getParentFile().exists()) {
                    exportFile.getParentFile().mkdirs();
                }
                FileUtils.bcopy(originalFile, exportFile, "Copy material QTI 2.1");
            }
        }
        AssessmentTestBuilder assessmentTestBuilder = new AssessmentTestBuilder(assessmentTest);
        double sumMaxScore = atomicMaxScore.sum();
        if (sumMaxScore > 0.0d) {
            assessmentTestBuilder.setMaxScore(sumMaxScore);
        }
        assessmentTest = assessmentTestBuilder.build();
        try (FileOutputStream out = new FileOutputStream(new File(directory, assessmentTestFilename))) {
            qtiSerializer.serializeJqtiObject(assessmentTest, out);
        } catch (Exception e) {
            log.error("", e);
        }
        manifest.write(new File(directory, "imsmanifest.xml"));
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ItemMaterial(org.olat.ims.qti21.pool.ImportExportHelper.ItemMaterial) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) AssessmentTestBuilder(org.olat.ims.qti21.model.xml.AssessmentTestBuilder) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) AssessmentItemsAndResources(org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources) DoubleAdder(java.util.concurrent.atomic.DoubleAdder) QtiSerializer(uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentSection(uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 3 with AssessmentItemsAndResources

use of org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources in project openolat by klemens.

the class QTI21ExportProcessor method exportToQTIEditor.

public ResolvedAssessmentItem exportToQTIEditor(QuestionItemFull fullItem, File editorContainer) throws IOException {
    AssessmentItemsAndResources itemAndMaterials = new AssessmentItemsAndResources();
    collectMaterials(fullItem, itemAndMaterials);
    if (itemAndMaterials.getAssessmentItems().isEmpty()) {
        // nothing found
        return null;
    }
    ResolvedAssessmentItem assessmentItem = itemAndMaterials.getAssessmentItems().get(0);
    // write materials
    for (ItemMaterial material : itemAndMaterials.getMaterials()) {
        String exportPath = material.getExportUri();
        File originalFile = material.getFile();
        File exportFile = new File(editorContainer, exportPath);
        if (!exportFile.getParentFile().exists()) {
            exportFile.getParentFile().mkdirs();
        }
        FileUtils.bcopy(originalFile, exportFile, "Copy material QTI 2.1");
    }
    return assessmentItem;
}
Also used : AssessmentItemsAndResources(org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) ItemMaterial(org.olat.ims.qti21.pool.ImportExportHelper.ItemMaterial) File(java.io.File)

Example 4 with AssessmentItemsAndResources

use of org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources in project openolat by klemens.

the class QTI21ExportProcessor method assembleTest.

public void assembleTest(List<QuestionItemFull> fullItems, File directory) {
    try {
        QtiSerializer qtiSerializer = qtiService.qtiSerializer();
        // imsmanifest
        ManifestBuilder manifest = ManifestBuilder.createAssessmentTestBuilder();
        // assessment test
        DoubleAdder atomicMaxScore = new DoubleAdder();
        AssessmentTest assessmentTest = AssessmentTestFactory.createAssessmentTest("Assessment test from pool", "Section");
        String assessmentTestFilename = assessmentTest.getIdentifier() + ".xml";
        manifest.appendAssessmentTest(assessmentTestFilename);
        // make a section
        AssessmentSection section = assessmentTest.getTestParts().get(0).getAssessmentSections().get(0);
        // assessment items
        for (QuestionItemFull qitem : fullItems) {
            File resourceDirectory = qpoolFileStorage.getDirectory(qitem.getDirectory());
            File itemFile = new File(resourceDirectory, qitem.getRootFilename());
            ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItemForCopy(itemFile.toURI(), resourceDirectory);
            AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
            assessmentItem.setIdentifier(QTI21QuestionType.generateNewIdentifier(assessmentItem.getIdentifier()));
            // save the item in its own container
            String container = qitem.getKey().toString();
            File containerDir = new File(directory, container);
            containerDir.mkdirs();
            File newItemFile = new File(containerDir, assessmentItem.getIdentifier() + ".xml");
            String newItemFilename = container + "/" + newItemFile.getName();
            qtiService.persistAssessmentObject(newItemFile, assessmentItem);
            AssessmentTestFactory.appendAssessmentItem(section, newItemFilename);
            manifest.appendAssessmentItem(newItemFilename);
            ManifestMetadataBuilder metadata = manifest.getResourceBuilderByHref(newItemFilename);
            metadata.appendMetadataFrom(qitem, resolvedAssessmentItem, locale);
            Double maxScore = QtiNodesExtractor.extractMaxScore(assessmentItem);
            if (maxScore != null) {
                atomicMaxScore.add(maxScore.doubleValue());
            }
            // write materials
            AssessmentItemsAndResources materials = new AssessmentItemsAndResources();
            ImportExportHelper.getMaterials(assessmentItem, itemFile, materials);
            for (ItemMaterial material : materials.getMaterials()) {
                String exportPath = material.getExportUri();
                File originalFile = material.getFile();
                File exportFile = new File(containerDir, exportPath);
                if (!exportFile.getParentFile().exists()) {
                    exportFile.getParentFile().mkdirs();
                }
                FileUtils.bcopy(originalFile, exportFile, "Copy material QTI 2.1");
            }
        }
        AssessmentTestBuilder assessmentTestBuilder = new AssessmentTestBuilder(assessmentTest);
        double sumMaxScore = atomicMaxScore.sum();
        if (sumMaxScore > 0.0d) {
            assessmentTestBuilder.setMaxScore(sumMaxScore);
        }
        assessmentTest = assessmentTestBuilder.build();
        try (FileOutputStream out = new FileOutputStream(new File(directory, assessmentTestFilename))) {
            qtiSerializer.serializeJqtiObject(assessmentTest, out);
        } catch (Exception e) {
            log.error("", e);
        }
        manifest.write(new File(directory, "imsmanifest.xml"));
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ItemMaterial(org.olat.ims.qti21.pool.ImportExportHelper.ItemMaterial) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) AssessmentTestBuilder(org.olat.ims.qti21.model.xml.AssessmentTestBuilder) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) AssessmentItemsAndResources(org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources) DoubleAdder(java.util.concurrent.atomic.DoubleAdder) QtiSerializer(uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentSection(uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

File (java.io.File)4 AssessmentItemsAndResources (org.olat.ims.qti21.pool.ImportExportHelper.AssessmentItemsAndResources)4 ItemMaterial (org.olat.ims.qti21.pool.ImportExportHelper.ItemMaterial)4 ResolvedAssessmentItem (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem)4 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 DoubleAdder (java.util.concurrent.atomic.DoubleAdder)2 AssessmentTestBuilder (org.olat.ims.qti21.model.xml.AssessmentTestBuilder)2 ManifestBuilder (org.olat.ims.qti21.model.xml.ManifestBuilder)2 ManifestMetadataBuilder (org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)2 QuestionItemFull (org.olat.modules.qpool.QuestionItemFull)2 AssessmentItem (uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem)2 AssessmentSection (uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection)2 AssessmentTest (uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest)2 QtiSerializer (uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer)2