Search in sources :

Example 26 with ManifestBuilder

use of org.olat.ims.qti21.model.xml.ManifestBuilder 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 27 with ManifestBuilder

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

Example 28 with ManifestBuilder

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

the class QTI21QPoolServiceProvider method createItem.

public QuestionItem createItem(Identity identity, QTI21QuestionType type, String title, Locale locale) {
    AssessmentItemBuilder itemBuilder = null;
    Translator translator = Util.createPackageTranslator(AssessmentTestComposerController.class, locale);
    switch(type) {
        case sc:
            itemBuilder = new SingleChoiceAssessmentItemBuilder(translator.translate("new.sc"), translator.translate("new.answer"), qtiService.qtiSerializer());
            break;
        case mc:
            itemBuilder = new MultipleChoiceAssessmentItemBuilder(translator.translate("new.mc"), translator.translate("new.answer"), qtiService.qtiSerializer());
            break;
        case kprim:
            itemBuilder = new KPrimAssessmentItemBuilder(translator.translate("new.kprim"), translator.translate("new.answer"), qtiService.qtiSerializer());
            break;
        case match:
            itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_MATRIX, qtiService.qtiSerializer());
            break;
        case matchdraganddrop:
            itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_DRAG_AND_DROP, qtiService.qtiSerializer());
            break;
        case matchtruefalse:
            itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_TRUE_FALSE, translator.translate("match.unanswered"), translator.translate("match.true"), translator.translate("match.false"), qtiService.qtiSerializer());
            break;
        case fib:
            itemBuilder = new FIBAssessmentItemBuilder(translator.translate("new.fib"), EntryType.text, qtiService.qtiSerializer());
            break;
        case numerical:
            itemBuilder = new FIBAssessmentItemBuilder(translator.translate("new.fib.numerical"), EntryType.numerical, qtiService.qtiSerializer());
            break;
        case essay:
            itemBuilder = new EssayAssessmentItemBuilder(translator.translate("new.essay"), qtiService.qtiSerializer());
            break;
        case upload:
            itemBuilder = new UploadAssessmentItemBuilder(translator.translate("new.upload"), qtiService.qtiSerializer());
            break;
        case drawing:
            itemBuilder = new DrawingAssessmentItemBuilder(translator.translate("new.drawing"), qtiService.qtiSerializer());
            break;
        case hotspot:
            itemBuilder = new HotspotAssessmentItemBuilder(translator.translate("new.hotspot"), qtiService.qtiSerializer());
            break;
        case hottext:
            itemBuilder = new HottextAssessmentItemBuilder(translator.translate("new.hottext"), translator.translate("new.hottext.start"), translator.translate("new.hottext.text"), qtiService.qtiSerializer());
            break;
        default:
            return null;
    }
    AssessmentItem assessmentItem = itemBuilder.getAssessmentItem();
    assessmentItem.setLabel(title);
    assessmentItem.setTitle(title);
    AssessmentItemMetadata itemMetadata = new AssessmentItemMetadata();
    itemMetadata.setQuestionType(type);
    QTI21ImportProcessor processor = new QTI21ImportProcessor(identity, locale);
    QuestionItemImpl qitem = processor.processItem(assessmentItem, "", null, "OpenOLAT", Settings.getVersion(), itemMetadata);
    VFSContainer baseDir = qpoolFileStorage.getContainer(qitem.getDirectory());
    VFSLeaf leaf = baseDir.createChildLeaf(qitem.getRootFilename());
    File itemFile = ((LocalImpl) leaf).getBasefile();
    qtiService.persistAssessmentObject(itemFile, assessmentItem);
    // create imsmanifest
    ManifestBuilder manifest = ManifestBuilder.createAssessmentItemBuilder();
    manifest.appendAssessmentItem(itemFile.getName());
    manifest.write(new File(itemFile.getParentFile(), "imsmanifest.xml"));
    return qitem;
}
Also used : HottextAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HottextAssessmentItemBuilder) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) SingleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) VFSContainer(org.olat.core.util.vfs.VFSContainer) EssayAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.EssayAssessmentItemBuilder) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) HotspotAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HotspotAssessmentItemBuilder) UploadAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.UploadAssessmentItemBuilder) FIBAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.FIBAssessmentItemBuilder) SingleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder) HottextAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HottextAssessmentItemBuilder) DrawingAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.DrawingAssessmentItemBuilder) EssayAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.EssayAssessmentItemBuilder) MatchAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MatchAssessmentItemBuilder) AssessmentItemBuilder(org.olat.ims.qti21.model.xml.AssessmentItemBuilder) KPrimAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.KPrimAssessmentItemBuilder) MultipleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MultipleChoiceAssessmentItemBuilder) MultipleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MultipleChoiceAssessmentItemBuilder) MatchAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MatchAssessmentItemBuilder) FIBAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.FIBAssessmentItemBuilder) KPrimAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.KPrimAssessmentItemBuilder) Translator(org.olat.core.gui.translator.Translator) AssessmentItemMetadata(org.olat.ims.qti21.model.xml.AssessmentItemMetadata) HotspotAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HotspotAssessmentItemBuilder) UploadAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.UploadAssessmentItemBuilder) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File) DrawingAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.DrawingAssessmentItemBuilder)

Example 29 with ManifestBuilder

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

the class QTI21AssessmentDetailsController method doCorrection.

private void doCorrection(UserRequest ureq, AssessmentTestSession session) {
    File unzippedDirRoot = FileResourceManager.getInstance().unzipFileResource(testEntry.getOlatResource());
    ResolvedAssessmentTest resolvedAssessmentTest = qtiService.loadAndResolveAssessmentTest(unzippedDirRoot, false, false);
    ManifestBuilder manifestBuilder = ManifestBuilder.read(new File(unzippedDirRoot, "imsmanifest.xml"));
    TestSessionState testSessionState = qtiService.loadTestSessionState(session);
    // use mutable maps to allow updates
    Map<Identity, AssessmentTestSession> lastSessions = new HashMap<>();
    lastSessions.put(assessedIdentity, session);
    Map<Identity, TestSessionState> testSessionStates = new HashMap<>();
    testSessionStates.put(assessedIdentity, testSessionState);
    CorrectionOverviewModel model = new CorrectionOverviewModel(entry, subIdent, testEntry, resolvedAssessmentTest, manifestBuilder, lastSessions, testSessionStates);
    correctionCtrl = new CorrectionIdentityAssessmentItemListController(ureq, getWindowControl(), stackPanel, model, session, assessedIdentity);
    listenTo(correctionCtrl);
    stackPanel.pushController(translate("correction"), correctionCtrl);
}
Also used : CorrectionOverviewModel(org.olat.ims.qti21.ui.assessment.CorrectionOverviewModel) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) AssessmentTestSession(org.olat.ims.qti21.AssessmentTestSession) TestSessionState(uk.ac.ed.ph.jqtiplus.state.TestSessionState) CorrectionIdentityAssessmentItemListController(org.olat.ims.qti21.ui.assessment.CorrectionIdentityAssessmentItemListController) HashMap(java.util.HashMap) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) Identity(org.olat.core.id.Identity) File(java.io.File)

Example 30 with ManifestBuilder

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

the class AssessmentTestComposerController method doExportPool.

private void doExportPool(AssessmentItemRef itemRef) {
    ResolvedAssessmentItem resolvedAssessmentItem = resolvedAssessmentTest.getResolvedAssessmentItem(itemRef);
    RootNodeLookup<AssessmentItem> rootNode = resolvedAssessmentItem.getItemLookup();
    AssessmentItem assessmentItem = rootNode.extractIfSuccessful();
    ManifestBuilder clonedManifestBuilder = ManifestBuilder.read(new File(unzippedDirRoot, "imsmanifest.xml"));
    ResourceType resource = getResourceType(clonedManifestBuilder, itemRef);
    ManifestMetadataBuilder metadata = clonedManifestBuilder.getMetadataBuilder(resource, true);
    if (metadata == null) {
        // not in imsmanifest.xml?
        metadata = new ManifestMetadataBuilder();
    }
    File itemFile = new File(rootNode.getSystemId());
    qti21QPoolServiceProvider.importAssessmentItemRef(getIdentity(), assessmentItem, itemFile, metadata, getLocale());
}
Also used : ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) ResourceType(org.olat.imscp.xml.manifest.ResourceType) File(java.io.File) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)

Aggregations

ManifestBuilder (org.olat.ims.qti21.model.xml.ManifestBuilder)28 File (java.io.File)26 ManifestMetadataBuilder (org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)18 ResolvedAssessmentItem (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem)18 AssessmentItem (uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem)16 IOException (java.io.IOException)14 ResourceType (org.olat.imscp.xml.manifest.ResourceType)14 URISyntaxException (java.net.URISyntaxException)10 Path (java.nio.file.Path)8 QuestionItemImpl (org.olat.modules.qpool.model.QuestionItemImpl)8 AssessmentSection (uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection)8 AssessmentTest (uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest)8 ResolvedAssessmentTest (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest)8 FileOutputStream (java.io.FileOutputStream)6 URI (java.net.URI)6 AssessmentItemMetadata (org.olat.ims.qti21.model.xml.AssessmentItemMetadata)6 QtiSerializer (uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer)6 FileVisitResult (java.nio.file.FileVisitResult)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4 ArrayList (java.util.ArrayList)4