use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class QTIQPoolServiceProvider method importBeecomItem.
public QuestionItemImpl importBeecomItem(Identity owner, ItemAndMetadata itemAndMetadata, VFSContainer sourceDir, Locale defaultLocale) {
QTIImportProcessor processor = new QTIImportProcessor(owner, defaultLocale);
String editor = null;
String editorVersion = null;
Item item = itemAndMetadata.getItem();
if (!item.isAlient()) {
editor = "OpenOLAT";
editorVersion = Settings.getVersion();
}
Document doc = QTIEditHelper.itemToXml(item);
Element itemEl = (Element) doc.selectSingleNode("questestinterop/item");
QuestionItemImpl qitem = processor.processItem(itemEl, "", null, editor, editorVersion, null, itemAndMetadata);
// save to file System
VFSContainer baseDir = qpoolFileStorage.getContainer(qitem.getDirectory());
VFSLeaf leaf = baseDir.createChildLeaf(qitem.getRootFilename());
QTIEditHelper.serialiazeDoc(doc, leaf);
if (sourceDir != null) {
List<String> materials = processor.getMaterials(itemEl);
// copy materials
for (String material : materials) {
VFSItem sourceItem = sourceDir.resolve(material);
if (sourceItem instanceof VFSLeaf) {
VFSLeaf targetItem = baseDir.createChildLeaf(material);
VFSManager.copyContent((VFSLeaf) sourceItem, targetItem);
}
}
}
return qitem;
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class QTIQPoolServiceProvider method extractTextContent.
@Override
public String extractTextContent(QuestionItemFull item) {
String content = null;
if (item.getRootFilename() != null) {
String dir = item.getDirectory();
VFSContainer container = qpoolFileStorage.getContainer(dir);
VFSItem file = container.resolve(item.getRootFilename());
if (file instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) file;
InputStream is = leaf.getInputStream();
QTI12SAXHandler handler = new QTI12SAXHandler();
try {
XMLReader parser = XMLReaderFactory.createXMLReader();
parser.setContentHandler(handler);
parser.setEntityResolver(new IMSEntityResolver());
parser.setFeature("http://xml.org/sax/features/validation", false);
parser.parse(new InputSource(is));
} catch (Exception e) {
log.error("", e);
} finally {
FileUtils.closeSafely(is);
}
return handler.toString();
}
}
return content;
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class QTIQPoolServiceProvider method exportToEditorPackage.
public void exportToEditorPackage(QTIEditorPackageImpl editorPackage, List<QuestionItemShort> items, boolean newTest) {
VFSContainer editorContainer = editorPackage.getBaseDir();
List<Long> itemKeys = toKeys(items);
List<QuestionItemFull> fullItems = questionItemDao.loadByIds(itemKeys);
Section section = editorPackage.getQTIDocument().getAssessment().getSections().get(0);
if (newTest) {
// remove autogenerated question
section.getItems().clear();
}
QTIExportProcessor processor = new QTIExportProcessor(qpoolFileStorage);
for (QuestionItemFull fullItem : fullItems) {
Element itemEl = processor.exportToQTIEditor(fullItem, editorContainer);
Item item = (Item) new ParserManager().parse(itemEl);
item.setIdent(QTIEditHelper.generateNewIdent(item.getIdent()));
section.getItems().add(item);
}
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class QTIQPoolServiceProvider method createItem.
public QuestionItem createItem(Identity owner, QTI12ItemFactory.Type type, String title, Locale defaultLocale) {
Translator trans = Util.createPackageTranslator(QTIEditorMainController.class, defaultLocale);
Item item;
switch(type) {
case sc:
item = QTIEditHelper.createSCItem(trans);
break;
case mc:
item = QTIEditHelper.createMCItem(trans);
break;
case kprim:
item = QTIEditHelper.createKPRIMItem(trans);
break;
case fib:
item = QTIEditHelper.createFIBItem(trans);
break;
case essay:
item = QTIEditHelper.createEssayItem(trans);
break;
default:
return null;
}
item.setLabel(title);
item.setTitle(title);
QTIImportProcessor processor = new QTIImportProcessor(owner, defaultLocale);
Document doc = QTIEditHelper.itemToXml(item);
Element itemEl = (Element) doc.selectSingleNode("questestinterop/item");
QuestionItemImpl qitem = processor.processItem(itemEl, "", null, "OpenOLAT", Settings.getVersion(), null, null);
// save to file System
VFSContainer baseDir = qpoolFileStorage.getContainer(qitem.getDirectory());
VFSLeaf leaf = baseDir.createChildLeaf(qitem.getRootFilename());
QTIEditHelper.serialiazeDoc(doc, leaf);
return qitem;
}
use of org.olat.core.util.vfs.VFSContainer in project OpenOLAT by OpenOLAT.
the class QTIImportProcessor method processItemFiles.
/**
* Process the file of an item's package
* @param item
* @param itemInfos
*/
protected void processItemFiles(QuestionItemImpl item, DocInfos docInfos) {
// a package with an item
String dir = item.getDirectory();
String rootFilename = item.getRootFilename();
VFSContainer container = qpoolFileStorage.getContainer(dir);
if (docInfos != null && docInfos.getRoot() != null) {
try {
Path destDir = ((LocalImpl) container).getBasefile().toPath();
// unzip to container
Path path = docInfos.getRoot();
Files.walkFileTree(path, new CopyVisitor(path, destDir, new YesMatcher()));
} catch (IOException e) {
log.error("", e);
}
} else if (importedFilename.toLowerCase().endsWith(".zip")) {
ZipUtil.unzipStrict(importedFile, container);
} else {
VFSLeaf endFile = container.createChildLeaf(rootFilename);
OutputStream out = null;
FileInputStream in = null;
try {
out = endFile.getOutputStream(false);
in = new FileInputStream(importedFile);
IOUtils.copy(in, out);
} catch (IOException e) {
log.error("", e);
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
}
}
Aggregations