use of org.olat.ims.qti21.model.xml.ManifestBuilder in project OpenOLAT by OpenOLAT.
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"));
}
use of org.olat.ims.qti21.model.xml.ManifestBuilder in project OpenOLAT by OpenOLAT.
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());
}
use of org.olat.ims.qti21.model.xml.ManifestBuilder 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);
}
}
use of org.olat.ims.qti21.model.xml.ManifestBuilder in project OpenOLAT by OpenOLAT.
the class QTI21ExportProcessor method enrichWithMetadata.
public void enrichWithMetadata(QuestionItemFull qitem, ResolvedAssessmentItem resolvedAssessmentItem, ManifestBuilder manifestBuilder) {
ResourceType resource = manifestBuilder.getResourceTypeByHref(qitem.getRootFilename());
if (resource == null) {
resource = manifestBuilder.appendAssessmentItem(qitem.getRootFilename());
}
ManifestMetadataBuilder metadataBuilder = manifestBuilder.getMetadataBuilder(resource, true);
metadataBuilder.appendMetadataFrom(qitem, resolvedAssessmentItem, locale);
}
use of org.olat.ims.qti21.model.xml.ManifestBuilder 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);
}
}
Aggregations