Search in sources :

Example 21 with ManifestBuilder

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

the class QTI21QPoolServiceProvider method importAssessmentItemRef.

/**
 * Very important, the ManifestMetadataBuilder will be changed, it need to be a clone
 *
 * @param owner The future owner of the question
 * @param assessmentItem The assessment item to convert
 * @param itemFile The file where the assessment item is saved
 * @param clonedMetadataBuilder The metadata builder need to be a clone!
 * @param fUnzippedDirRoot The directory of the assessment item or the assessment test.
 * @param defaultLocale The locale used by some translation
 * @return
 */
public QuestionItem importAssessmentItemRef(Identity owner, AssessmentItem assessmentItem, File itemFile, ManifestMetadataBuilder clonedMetadataBuilder, Locale defaultLocale) {
    QTI21ImportProcessor processor = new QTI21ImportProcessor(owner, defaultLocale);
    AssessmentItemMetadata metadata = new AssessmentItemMetadata(clonedMetadataBuilder);
    String editor = null;
    String editorVersion = null;
    if (StringHelper.containsNonWhitespace(assessmentItem.getToolName())) {
        editor = assessmentItem.getToolName();
    }
    if (StringHelper.containsNonWhitespace(assessmentItem.getToolVersion())) {
        editorVersion = assessmentItem.getToolVersion();
    }
    String originalItemFilename = itemFile.getName();
    QuestionItemImpl qitem = processor.processItem(assessmentItem, null, originalItemFilename, editor, editorVersion, metadata);
    // storage
    File itemStorage = qpoolFileStorage.getDirectory(qitem.getDirectory());
    FileUtils.copyDirContentsToDir(itemFile, itemStorage, false, "QTI21 import item xml in pool");
    // create manifest
    ManifestBuilder manifest = ManifestBuilder.createAssessmentItemBuilder();
    ResourceType resource = manifest.appendAssessmentItem(UUID.randomUUID().toString(), originalItemFilename);
    ManifestMetadataBuilder exportedMetadataBuilder = manifest.getMetadataBuilder(resource, true);
    exportedMetadataBuilder.setMetadata(clonedMetadataBuilder.getMetadata());
    manifest.write(new File(itemStorage, "imsmanifest.xml"));
    // process material
    File materialDirRoot = itemFile.getParentFile();
    List<String> materials = ImportExportHelper.getMaterials(assessmentItem);
    for (String material : materials) {
        if (material.indexOf("://") < 0) {
            // material can be an external URL
            try {
                File materialFile = new File(materialDirRoot, material);
                if (materialFile.isFile() && materialFile.exists()) {
                    File itemMaterialFile = new File(itemStorage, material);
                    if (!itemMaterialFile.getParentFile().exists()) {
                        itemMaterialFile.getParentFile().mkdirs();
                    }
                    Files.copy(materialFile.toPath(), itemMaterialFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                }
            } catch (IOException e) {
                log.error("", e);
            }
        }
    }
    return qitem;
}
Also used : ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) AssessmentItemMetadata(org.olat.ims.qti21.model.xml.AssessmentItemMetadata) ResourceType(org.olat.imscp.xml.manifest.ResourceType) IOException(java.io.IOException) File(java.io.File) ManifestMetadataBuilder(org.olat.ims.qti21.model.xml.ManifestMetadataBuilder)

Example 22 with ManifestBuilder

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

the class BigAssessmentTestPackageBuilder method createAssessmentTest.

@Test
public void createAssessmentTest() throws URISyntaxException {
    String date = format.format(new Date());
    File directory = new File("/HotCoffee/qti/" + date + "/");
    directory.mkdirs();
    ManifestBuilder manifest = ManifestBuilder.createAssessmentTestBuilder();
    System.out.println(directory);
    // test
    File testFile = new File(directory, IdentifierGenerator.newAssessmentTestFilename());
    AssessmentTest assessmentTest = AssessmentTestFactory.createAssessmentTest("Big test " + date, "Section");
    manifest.appendAssessmentTest(testFile.getName());
    TestPart part = assessmentTest.getTestParts().get(0);
    part.getAssessmentSections().clear();
    // section
    for (int i = 0; i < numOfSections; i++) {
        AssessmentSection section = new AssessmentSection(part);
        section.setFixed(Boolean.TRUE);
        section.setVisible(Boolean.TRUE);
        section.setTitle((i + 1) + ". Section");
        section.setIdentifier(IdentifierGenerator.newAsIdentifier("sec"));
        part.getAssessmentSections().add(section);
        Ordering ordering = new Ordering(section);
        ordering.setShuffle(true);
        section.setOrdering(ordering);
        Selection selection = new Selection(section);
        selection.setSelect(4);
        section.setSelection(selection);
        ItemSessionControl itemSessionControl = new ItemSessionControl(section);
        itemSessionControl.setAllowSkipping(Boolean.TRUE);
        itemSessionControl.setAllowComment(Boolean.FALSE);
        itemSessionControl.setShowFeedback(Boolean.FALSE);
        section.setItemSessionControl(itemSessionControl);
        RubricBlock rubrickBlock = new RubricBlock(section);
        rubrickBlock.setViews(Collections.singletonList(View.CANDIDATE));
        section.getRubricBlocks().add(rubrickBlock);
        for (int j = 0; j < numOfQuestions; j++) {
            // single choice
            String itemId = IdentifierGenerator.newAsString(QTI21QuestionType.sc.getPrefix());
            File itemFile = new File(directory, itemId + ".xml");
            AssessmentItem assessmentItem = AssessmentItemFactory.createSingleChoice("Single choice", "New answer");
            assessmentItem.setTitle((i + 1) + "." + (j + 1) + ". Question SC");
            AssessmentTestFactory.appendAssessmentItem(section, itemFile.getName());
            manifest.appendAssessmentItem(itemFile.getName());
            try (FileOutputStream out = new FileOutputStream(itemFile)) {
                qtiSerializer.serializeJqtiObject(assessmentItem, out);
            } catch (Exception e) {
                log.error("", e);
            }
        }
    }
    try (FileOutputStream out = new FileOutputStream(testFile)) {
        qtiSerializer.serializeJqtiObject(assessmentTest, out);
    } catch (Exception e) {
        log.error("", e);
    }
    manifest.write(new File(directory, "imsmanifest.xml"));
}
Also used : ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) Selection(uk.ac.ed.ph.jqtiplus.node.test.Selection) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) ItemSessionControl(uk.ac.ed.ph.jqtiplus.node.test.ItemSessionControl) AssessmentSection(uk.ac.ed.ph.jqtiplus.node.test.AssessmentSection) FileOutputStream(java.io.FileOutputStream) TestPart(uk.ac.ed.ph.jqtiplus.node.test.TestPart) Ordering(uk.ac.ed.ph.jqtiplus.node.test.Ordering) File(java.io.File) RubricBlock(uk.ac.ed.ph.jqtiplus.node.content.variable.RubricBlock) ResolvedAssessmentTest(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentTest) AssessmentTest(uk.ac.ed.ph.jqtiplus.node.test.AssessmentTest) Test(org.junit.Test)

Example 23 with ManifestBuilder

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

the class QTI21IdentityListCourseNodeToolsController method doOpenCorrection.

private void doOpenCorrection(UserRequest ureq) {
    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(lastSession);
    Map<Identity, AssessmentTestSession> lastSessionMap = new HashMap<>();
    lastSessionMap.put(assessedIdentity, lastSession);
    Map<Identity, TestSessionState> testSessionStates = new HashMap<>();
    testSessionStates.put(assessedIdentity, testSessionState);
    CorrectionOverviewModel model = new CorrectionOverviewModel(courseEntry, testCourseNode.getIdent(), testEntry, resolvedAssessmentTest, manifestBuilder, lastSessionMap, testSessionStates);
    correctionCtrl = new CorrectionIdentityAssessmentItemListController(ureq, getWindowControl(), stackPanel, model, lastSession, assessedIdentity);
    listenTo(correctionCtrl);
    stackPanel.pushController(translate("tool.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 24 with ManifestBuilder

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

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

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