Search in sources :

Example 6 with QuestionItemFull

use of org.olat.modules.qpool.QuestionItemFull in project OpenOLAT by OpenOLAT.

the class QuestionDAOTest method resetAllStatesToDraft.

@Test
public void resetAllStatesToDraft() {
    QItemType mcType = qItemTypeDao.loadByType(QuestionType.MC.name());
    QuestionItemImpl item1 = questionDao.createAndPersist(null, "RES DRAFT 1", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    item1.setQuestionStatus(QuestionStatus.endOfLife);
    questionDao.loadForUpdate(item1);
    QuestionItemImpl item2 = questionDao.createAndPersist(null, "RES DRAFT 2", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    item2.setQuestionStatus(QuestionStatus.review);
    questionDao.loadForUpdate(item2);
    QuestionItemImpl item3 = questionDao.createAndPersist(null, "RES DRAFT 3", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    item3.setQuestionStatus(QuestionStatus.revised);
    questionDao.loadForUpdate(item3);
    questionDao.createAndPersist(null, "RES DRAFT 4", QTIConstants.QTI_12_FORMAT, Locale.ENGLISH.getLanguage(), null, null, null, mcType);
    dbInstance.commitAndCloseSession();
    questionDao.resetAllStatesToDraft();
    dbInstance.commitAndCloseSession();
    List<QuestionItemFull> allItems = questionDao.getAllItems(0, -1);
    for (QuestionItem item : allItems) {
        Assert.assertEquals(QuestionStatus.draft, item.getQuestionStatus());
    }
}
Also used : QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) QuestionItem(org.olat.modules.qpool.QuestionItem) QItemType(org.olat.modules.qpool.model.QItemType) Test(org.junit.Test)

Example 7 with QuestionItemFull

use of org.olat.modules.qpool.QuestionItemFull 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 8 with QuestionItemFull

use of org.olat.modules.qpool.QuestionItemFull in project OpenOLAT by OpenOLAT.

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 9 with QuestionItemFull

use of org.olat.modules.qpool.QuestionItemFull in project OpenOLAT by OpenOLAT.

the class QTI12And21PoolWordExport method prepare.

@Override
public void prepare(HttpServletResponse hres) {
    try {
        hres.setCharacterEncoding(encoding);
    } catch (Exception e) {
        log.error("", e);
    }
    try (ZipOutputStream zout = new ZipOutputStream(hres.getOutputStream())) {
        String label = "Items_Export";
        String secureLabel = StringHelper.transformDisplayNameToFileSystemName(label);
        List<Long> itemKeys = new ArrayList<>();
        for (QuestionItemShort item : items) {
            itemKeys.add(item.getKey());
        }
        List<QuestionItemFull> fullItems = questionItemDao.loadByIds(itemKeys);
        String file = secureLabel + ".zip";
        hres.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + StringHelper.urlEncodeUTF8(file));
        hres.setHeader("Content-Description", StringHelper.urlEncodeUTF8(label));
        zout.setLevel(9);
        ZipEntry test = new ZipEntry(secureLabel + ".docx");
        zout.putNextEntry(test);
        exportItems(fullItems, zout, false);
        zout.closeEntry();
        ZipEntry responses = new ZipEntry(secureLabel + "_responses.docx");
        zout.putNextEntry(responses);
        exportItems(fullItems, zout, true);
        zout.closeEntry();
    } catch (Exception e) {
        log.error("", e);
    }
}
Also used : QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) ZipOutputStream(java.util.zip.ZipOutputStream) QuestionItemShort(org.olat.modules.qpool.QuestionItemShort) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 10 with QuestionItemFull

use of org.olat.modules.qpool.QuestionItemFull in project openolat by klemens.

the class QTI12And21PoolWordExport method exportItems.

private void exportItems(List<QuestionItemFull> fullItems, OutputStream out, boolean withResponses) {
    ZipOutputStream zout = null;
    try {
        OpenXMLDocument document = new OpenXMLDocument();
        document.setDocumentHeader("");
        Translator qti12Translator = Util.createPackageTranslator(QTIWordExport.class, locale);
        Translator translator = Util.createPackageTranslator(AssessmentTestDisplayController.class, locale, Util.createPackageTranslator(AssessmentTestComposerController.class, locale, qti12Translator));
        AssessmentHtmlBuilder htmlBuilder = new AssessmentHtmlBuilder();
        for (Iterator<QuestionItemFull> itemIt = fullItems.iterator(); itemIt.hasNext(); ) {
            QuestionItemFull fullItem = itemIt.next();
            if (QTIConstants.QTI_12_FORMAT.equals(fullItem.getFormat())) {
                String dir = fullItem.getDirectory();
                VFSContainer container = qpoolFileStorage.getContainer(dir);
                document.setMediaContainer(container);
                VFSItem rootItem = container.resolve(fullItem.getRootFilename());
                Item item = QTIEditHelper.readItemXml((VFSLeaf) rootItem);
                if (item.isAlient()) {
                    QTIWordExport.renderAlienItem(item, document, translator);
                } else {
                    QTIWordExport.renderItem(item, document, withResponses, translator);
                }
            } else {
                File resourceDirectory = qpoolService.getRootDirectory(fullItem);
                VFSContainer resourceContainer = qpoolService.getRootContainer(fullItem);
                document.setMediaContainer(resourceContainer);
                File resourceFile = qpoolService.getRootFile(fullItem);
                URI assessmentItemUri = resourceFile.toURI();
                ResolvedAssessmentItem resolvedAssessmentItem = qtiService.loadAndResolveAssessmentItem(assessmentItemUri, resourceDirectory);
                AssessmentItem item = resolvedAssessmentItem.getItemLookup().extractIfSuccessful();
                QTI21WordExport.renderAssessmentItem(item, resourceFile, resourceDirectory, document, withResponses, translator, htmlBuilder);
            }
            if (itemIt.hasNext()) {
                document.appendPageBreak();
            }
        }
        zout = new ZipOutputStream(out);
        zout.setLevel(9);
        OpenXMLDocumentWriter writer = new OpenXMLDocumentWriter();
        writer.createDocument(zout, document);
    } catch (Exception e) {
        log.error("", e);
    } finally {
        if (zout != null) {
            try {
                zout.finish();
            } catch (IOException e) {
                log.error("", e);
            }
        }
    }
}
Also used : VFSContainer(org.olat.core.util.vfs.VFSContainer) OpenXMLDocument(org.olat.core.util.openxml.OpenXMLDocument) AssessmentHtmlBuilder(org.olat.ims.qti21.model.xml.AssessmentHtmlBuilder) VFSItem(org.olat.core.util.vfs.VFSItem) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) IOException(java.io.IOException) URI(java.net.URI) IOException(java.io.IOException) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) Item(org.olat.ims.qti.editor.beecom.objects.Item) VFSItem(org.olat.core.util.vfs.VFSItem) QuestionItemFull(org.olat.modules.qpool.QuestionItemFull) OpenXMLDocumentWriter(org.olat.core.util.openxml.OpenXMLDocumentWriter) Translator(org.olat.core.gui.translator.Translator) ZipOutputStream(java.util.zip.ZipOutputStream) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentTestComposerController(org.olat.ims.qti21.ui.editor.AssessmentTestComposerController) File(java.io.File)

Aggregations

QuestionItemFull (org.olat.modules.qpool.QuestionItemFull)44 File (java.io.File)18 QuestionItem (org.olat.modules.qpool.QuestionItem)18 Test (org.junit.Test)16 VFSItem (org.olat.core.util.vfs.VFSItem)16 IOException (java.io.IOException)14 VFSContainer (org.olat.core.util.vfs.VFSContainer)14 URL (java.net.URL)12 InputStream (java.io.InputStream)10 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)10 ZipOutputStream (java.util.zip.ZipOutputStream)8 Document (org.dom4j.Document)8 Node (org.dom4j.Node)8 XMLParser (org.olat.core.util.xml.XMLParser)8 IMSEntityResolver (org.olat.ims.resources.IMSEntityResolver)8 ResolvedAssessmentItem (uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem)8 URISyntaxException (java.net.URISyntaxException)6 ArrayList (java.util.ArrayList)6 ZipEntry (java.util.zip.ZipEntry)6 Element (org.dom4j.Element)6