Search in sources :

Example 26 with ManifestMetadataBuilder

use of org.olat.ims.qti21.model.xml.ManifestMetadataBuilder in project openolat by klemens.

the class QTI12To21Converter method appendResourceAndMetadata.

private void appendResourceAndMetadata(Item item, AssessmentItemBuilder itemBuilder, File itemFile) {
    manifest.appendAssessmentItem(itemFile.getName());
    ManifestMetadataBuilder metadata = manifest.getResourceBuilderByHref(itemFile.getName());
    metadata.setTechnicalFormat(ManifestBuilder.ASSESSMENTITEM_MIMETYPE);
    metadata.setQtiMetadataInteractionTypes(itemBuilder.getInteractionNames());
    metadata.setOpenOLATMetadataQuestionType(itemBuilder.getQuestionType().getPrefix());
    metadata.setTitle(item.getTitle(), locale.getLanguage());
    metadata.setDescription(item.getObjectives(), locale.getLanguage());
}
Also used : ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)

Example 27 with ManifestMetadataBuilder

use of org.olat.ims.qti21.model.xml.ManifestMetadataBuilder in project openolat by klemens.

the class QTI21ExportProcessor method assembleTest.

public void assembleTest(List<QuestionItemFull> fullItems, ZipOutputStream zout) {
    try {
        QtiSerializer qtiSerializer = qtiService.qtiSerializer();
        // imsmanifest
        ManifestBuilder manifest = ManifestBuilder.createAssessmentTestBuilder();
        // assessment test
        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());
            String itemFilename = itemFile.getName();
            String container = qitem.getKey().toString();
            String containedFilename = container + "/" + itemFilename;
            ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItemForCopy(itemFile.toURI(), resourceDirectory);
            ZipUtil.addFileToZip(containedFilename, itemFile, zout);
            AssessmentTestFactory.appendAssessmentItem(section, containedFilename);
            manifest.appendAssessmentItem(containedFilename);
            ManifestMetadataBuilder metadata = manifest.getResourceBuilderByHref(containedFilename);
            metadata.appendMetadataFrom(qitem, resolvedAssessmentItem, locale);
            // write materials
            try {
                Files.walkFileTree(resourceDirectory.toPath(), new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        String filename = file.getFileName().toString();
                        if (!"imsmanifest.xml".equals(filename) && !filename.startsWith(".") && !itemFilename.equals(filename)) {
                            String relPath = resourceDirectory.toPath().relativize(file).toString();
                            ZipUtil.addFileToZip(container + "/" + relPath, file, zout);
                        }
                        return FileVisitResult.CONTINUE;
                    }
                });
            } catch (IOException e) {
                log.error("", e);
            }
        }
        zout.putNextEntry(new ZipEntry(assessmentTestFilename));
        qtiSerializer.serializeJqtiObject(assessmentTest, new ShieldOutputStream(zout));
        zout.closeEntry();
        zout.putNextEntry(new ZipEntry("imsmanifest.xml"));
        manifest.write(new ShieldOutputStream(zout));
        zout.closeEntry();
    } catch (IOException | URISyntaxException e) {
        log.error("", e);
    }
}
Also used : Path(java.nio.file.Path) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ZipEntry(java.util.zip.ZipEntry) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) ShieldOutputStream(org.olat.core.util.io.ShieldOutputStream) 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) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 28 with ManifestMetadataBuilder

use of org.olat.ims.qti21.model.xml.ManifestMetadataBuilder 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)

Example 29 with ManifestMetadataBuilder

use of org.olat.ims.qti21.model.xml.ManifestMetadataBuilder in project openolat by klemens.

the class QTI21ImportProcessor method process.

public List<QuestionItem> process(File file) {
    // export zip file
    List<QuestionItem> items = new ArrayList<>();
    try (FileSystem fs = FileSystems.newFileSystem(file.toPath(), null)) {
        Path fPath = fs.getPath("/");
        if (fPath != null) {
            ImsManifestVisitor visitor = new ImsManifestVisitor();
            Files.walkFileTree(fPath, visitor);
            List<Path> imsmanifests = visitor.getImsmanifestFiles();
            for (Path imsmanifest : imsmanifests) {
                InputStream in = Files.newInputStream(imsmanifest);
                ManifestBuilder manifestBuilder = ManifestBuilder.read(new ShieldInputStream(in));
                List<ResourceType> resources = manifestBuilder.getResourceList();
                for (ResourceType resource : resources) {
                    ManifestMetadataBuilder metadataBuilder = manifestBuilder.getMetadataBuilder(resource, true);
                    QuestionItem qitem = processResource(resource, imsmanifest, metadataBuilder);
                    if (qitem != null) {
                        items.add(qitem);
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error("", e);
    }
    return items;
}
Also used : Path(java.nio.file.Path) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ResourceType(org.olat.imscp.xml.manifest.ResourceType) IOException(java.io.IOException) ShieldInputStream(org.olat.core.util.io.ShieldInputStream) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) FileSystem(java.nio.file.FileSystem) QuestionItem(org.olat.modules.qpool.QuestionItem)

Example 30 with ManifestMetadataBuilder

use of org.olat.ims.qti21.model.xml.ManifestMetadataBuilder in project openolat by klemens.

the class QTI21ImportProcessor method processResource.

private QuestionItem processResource(ResourceType resource, Path imsmanifestPath, ManifestMetadataBuilder metadataBuilder) {
    try {
        String href = resource.getHref();
        Path parentPath = imsmanifestPath.getParent();
        Path assessmentItemPath = parentPath.resolve(href);
        if (Files.notExists(assessmentItemPath)) {
            return null;
        }
        String dir = qpoolFileStorage.generateDir();
        // storage
        File itemStorage = qpoolFileStorage.getDirectory(dir);
        File outputFile = new File(itemStorage, href);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        QTI21Infos infos = getInfos(imsmanifestPath);
        convertXmlFile(assessmentItemPath, outputFile.toPath(), infos);
        QtiXmlReader qtiXmlReader = new QtiXmlReader(qtiService.jqtiExtensionManager());
        ResourceLocator fileResourceLocator = new FileResourceLocator();
        ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
        URI assessmentObjectSystemId = outputFile.toURI();
        AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
        ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(assessmentObjectSystemId);
        AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
        if (!AssessmentItemChecker.checkAndCorrect(assessmentItem)) {
            qtiService.persistAssessmentObject(outputFile, assessmentItem);
        }
        AssessmentItemMetadata metadata = new AssessmentItemMetadata(metadataBuilder);
        String editor = null;
        String editorVersion = null;
        if (StringHelper.containsNonWhitespace(assessmentItem.getToolName())) {
            editor = assessmentItem.getToolName();
        }
        if (StringHelper.containsNonWhitespace(assessmentItem.getToolVersion())) {
            editorVersion = assessmentItem.getToolVersion();
        }
        QuestionItemImpl qitem = processItem(assessmentItem, null, href, editor, editorVersion, dir, metadata);
        // create manifest
        ManifestBuilder manifest = ManifestBuilder.createAssessmentItemBuilder();
        String itemId = IdentifierGenerator.newAsIdentifier("item").toString();
        ResourceType importedResource = manifest.appendAssessmentItem(itemId, href);
        ManifestMetadataBuilder importedMetadataBuilder = manifest.getMetadataBuilder(importedResource, true);
        importedMetadataBuilder.setMetadata(metadataBuilder.getMetadata());
        manifest.write(new File(itemStorage, "imsmanifest.xml"));
        // process material
        List<String> materials = ImportExportHelper.getMaterials(assessmentItem);
        for (String material : materials) {
            if (material.indexOf("://") < 0) {
                // material can be an external URL
                Path materialFile = assessmentItemPath.getParent().resolve(material);
                PathUtils.copyFileToDir(materialFile, outputFile.getParentFile(), material);
            }
        }
        return qitem;
    } catch (Exception e) {
        log.error("", e);
        return null;
    }
}
Also used : Path(java.nio.file.Path) AssessmentObjectXmlLoader(uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) ResourceType(org.olat.imscp.xml.manifest.ResourceType) URI(java.net.URI) IOException(java.io.IOException) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder) QtiXmlReader(uk.ac.ed.ph.jqtiplus.reading.QtiXmlReader) QTI21Infos(org.olat.ims.qti21.model.xml.QTI21Infos) FileResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.FileResourceLocator) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItemMetadata(org.olat.ims.qti21.model.xml.AssessmentItemMetadata) File(java.io.File) FileResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.FileResourceLocator) ResourceLocator(uk.ac.ed.ph.jqtiplus.xmlutils.locators.ResourceLocator)

Aggregations

ManifestMetadataBuilder (org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)32 ResolvedAssessmentItem (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem)22 AssessmentItem (uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem)18 File (java.io.File)16 ManifestBuilder (org.olat.ims.qti21.model.xml.ManifestBuilder)16 ResourceType (org.olat.imscp.xml.manifest.ResourceType)14 IOException (java.io.IOException)12 AssessmentItemRef (uk.ac.ed.ph.jqtiplus.node.test.AssessmentItemRef)12 AssessmentSection (uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection)10 URISyntaxException (java.net.URISyntaxException)8 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 ResolvedAssessmentTest (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest)8 URI (java.net.URI)6 Path (java.nio.file.Path)6 QuestionItemFull (org.olat.modules.qpool.QuestionItemFull)6 QuestionItemImpl (org.olat.modules.qpool.model.QuestionItemImpl)6 AssessmentTest (uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest)6 GenericTreeNode (org.olat.core.gui.components.tree.GenericTreeNode)4 TreeNode (org.olat.core.gui.components.tree.TreeNode)4