use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project openolat by klemens.
the class QTI21ServiceImpl method loadAndResolveAssessmentItemForCopy.
@Override
public ResolvedAssessmentItem loadAndResolveAssessmentItemForCopy(URI assessmentObjectSystemId, File resourceDirectory) {
QtiXmlReader qtiXmlReader = new QtiXmlReader(jqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(resourceDirectory.toPath());
ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
return assessmentObjectXmlLoader.loadAndResolveAssessmentItem(assessmentObjectSystemId);
}
use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader 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;
}
}
use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project openolat by klemens.
the class CopyAndConvertVisitor method checkAssessmentItem.
private void checkAssessmentItem(Path outputFile) {
QTI21Service qtiService = CoreSpringFactory.getImpl(QTI21Service.class);
QtiXmlReader qtiXmlReader = new QtiXmlReader(qtiService.jqtiExtensionManager());
ResourceLocator fileResourceLocator = new FileResourceLocator();
ResourceLocator inputResourceLocator = ImsQTI21Resource.createResolvingResourceLocator(fileResourceLocator);
URI assessmentObjectSystemId = outputFile.toFile().toURI();
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, inputResourceLocator);
ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(assessmentObjectSystemId);
AssessmentItem assessmentItem = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
if (!AssessmentItemChecker.checkAndCorrect(assessmentItem)) {
try (FileOutputStream out = new FileOutputStream(outputFile.toFile())) {
qtiService.qtiSerializer().serializeJqtiObject(assessmentItem, out);
} catch (Exception e) {
log.error("", e);
}
}
}
use of uk.ac.ed.ph.jqtiplus.reading.AssessmentObjectXmlLoader in project openolat by klemens.
the class CopyAndConvertVisitor method validate.
private boolean validate(Path inputFile, InputType type, boolean verbose) {
try {
QtiXmlReader qtiXmlReader = new QtiXmlReader(new JqtiExtensionManager());
ResourceLocator fileResourceLocator = new PathResourceLocator(inputFile.getParent());
AssessmentObjectXmlLoader assessmentObjectXmlLoader = new AssessmentObjectXmlLoader(qtiXmlReader, fileResourceLocator);
RootNode rootNode = null;
BadResourceException e = null;
URI uri = new URI("zip", inputFile.getFileName().toString(), null);
if (type == InputType.assessmentItem) {
ItemValidationResult itemResult = assessmentObjectXmlLoader.loadResolveAndValidateItem(uri);
e = itemResult.getResolvedAssessmentItem().getItemLookup().getBadResourceException();
ResolvedAssessmentItem resolvedAssessmentItem = assessmentObjectXmlLoader.loadAndResolveAssessmentItem(uri);
rootNode = resolvedAssessmentItem.getRootNodeLookup().extractIfSuccessful();
} else if (type == InputType.assessmentTest) {
TestValidationResult testResult = assessmentObjectXmlLoader.loadResolveAndValidateTest(uri);
e = testResult.getResolvedAssessmentTest().getTestLookup().getBadResourceException();
ResolvedAssessmentTest resolvedAssessmentTest = assessmentObjectXmlLoader.loadAndResolveAssessmentTest(uri);
rootNode = resolvedAssessmentTest.getRootNodeLookup().extractIfSuccessful();
}
if (e != null && verbose) {
StringBuilder err = new StringBuilder();
BadRessourceHelper.extractMessage(e, err);
log.warn(err.toString());
}
return (rootNode != null) && (e == null || (e instanceof QtiXmlInterpretationException && ((QtiXmlInterpretationException) e).getXmlParseResult().getFatalErrors().isEmpty()));
} catch (URISyntaxException e) {
log.error("", e);
return false;
}
}
Aggregations