use of uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem in project OpenOLAT by OpenOLAT.
the class MatchAssessmentItemBuilder method createAssessmentItem.
private static AssessmentItem createAssessmentItem(String title, String matchCssClass, String unanswered, String right, String wrong) {
AssessmentItem assessmentItem = AssessmentItemFactory.createAssessmentItem(QTI21QuestionType.match, title);
NodeGroupList nodeGroups = assessmentItem.getNodeGroups();
double maxScore = 1.0d;
Identifier responseDeclarationId = Identifier.assumedLegal("RESPONSE_1");
// define correct answer
ResponseDeclaration responseDeclaration = createMatchResponseDeclaration(assessmentItem, responseDeclarationId, new HashMap<>());
nodeGroups.getResponseDeclarationGroup().getResponseDeclarations().add(responseDeclaration);
appendDefaultOutcomeDeclarations(assessmentItem, maxScore);
// the single choice interaction
ItemBody itemBody = appendDefaultItemBody(assessmentItem);
Map<Identifier, List<Identifier>> associations = new HashMap<>();
MatchInteraction interaction;
if (QTI21Constants.CSS_MATCH_TRUE_FALSE.equals(matchCssClass)) {
interaction = appendMatchInteractionTrueFalse(itemBody, unanswered, right, wrong, responseDeclarationId);
// default correct answers set to "right"
SimpleAssociableChoice rightChoice = interaction.getSimpleMatchSets().get(1).getSimpleAssociableChoices().get(1);
List<SimpleAssociableChoice> sourceChoices = interaction.getSimpleMatchSets().get(0).getSimpleAssociableChoices();
for (SimpleAssociableChoice sourceChoice : sourceChoices) {
List<Identifier> targetIdentifiers = new ArrayList<>();
targetIdentifiers.add(rightChoice.getIdentifier());
associations.put(sourceChoice.getIdentifier(), targetIdentifiers);
}
} else {
interaction = appendMatchInteraction(itemBody, responseDeclarationId);
}
List<String> cssClasses = new ArrayList<>();
cssClasses.add(matchCssClass);
interaction.setClassAttr(cssClasses);
appendAssociationMatchResponseDeclaration(responseDeclaration, associations);
// response processing
ResponseProcessing responseProcessing = createResponseProcessing(assessmentItem, responseDeclarationId);
assessmentItem.getNodeGroups().getResponseProcessingGroup().setResponseProcessing(responseProcessing);
return assessmentItem;
}
use of uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem in project OpenOLAT by OpenOLAT.
the class UploadAssessmentItemBuilder method createAssessmentItem.
private static AssessmentItem createAssessmentItem(String title) {
AssessmentItem assessmentItem = AssessmentItemFactory.createAssessmentItem(QTI21QuestionType.upload, title);
// define the response
Identifier responseDeclarationId = Identifier.assumedLegal("RESPONSE_1");
ResponseDeclaration responseDeclaration = createUploadResponseDeclaration(assessmentItem, responseDeclarationId);
assessmentItem.getNodeGroups().getResponseDeclarationGroup().getResponseDeclarations().add(responseDeclaration);
// outcomes
appendDefaultOutcomeDeclarations(assessmentItem, 1.0d);
ItemBody itemBody = appendDefaultItemBody(assessmentItem);
appendUploadInteraction(itemBody, responseDeclarationId);
// response processing
ResponseProcessing responseProcessing = createResponseProcessing(assessmentItem, responseDeclarationId);
assessmentItem.getNodeGroups().getResponseProcessingGroup().setResponseProcessing(responseProcessing);
return assessmentItem;
}
use of uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem in project OpenOLAT by OpenOLAT.
the class QTI12To21Converter method convertItemBasics.
private void convertItemBasics(Item item, AssessmentItemBuilder itemBuilder) {
AssessmentItem assessmentItem = itemBuilder.getAssessmentItem();
if (StringHelper.containsNonWhitespace(item.getTitle())) {
assessmentItem.setTitle(item.getTitle());
}
if (StringHelper.containsNonWhitespace(item.getLabel())) {
assessmentItem.setLabel(item.getLabel());
}
Question question = item.getQuestion();
String questionText = question.getQuestion().renderAsHtmlForEditor();
questionText = blockedHtml(questionText);
if (StringHelper.isHtml(questionText)) {
itemBuilder.setQuestion(questionText);
} else {
itemBuilder.setQuestion("<p>" + questionText + "</p>");
}
String hintText = question.getHintText();
if (StringHelper.containsNonWhitespace(hintText)) {
ModalFeedbackBuilder hint = itemBuilder.createHint();
Translator translator = Util.createPackageTranslator(QTIModule.class, locale);
hint.setTitle(translator.translate("render.hint"));
hint.setText(hintText);
}
String solutionText = question.getSolutionText();
if (StringHelper.containsNonWhitespace(solutionText)) {
ModalFeedbackBuilder solution = itemBuilder.createCorrectSolutionFeedback();
solutionText = blockedHtml(solutionText);
solution.setText(solutionText);
}
String feedbackMastery = QTIEditHelper.getFeedbackMasteryText(item);
if (StringHelper.containsNonWhitespace(feedbackMastery)) {
ModalFeedbackBuilder feedback = itemBuilder.createCorrectFeedback();
feedbackMastery = blockedHtml(feedbackMastery);
feedback.setText(feedbackMastery);
}
String feedbackFail = QTIEditHelper.getFeedbackFailText(item);
if (StringHelper.containsNonWhitespace(feedbackFail)) {
ModalFeedbackBuilder feedback = itemBuilder.createIncorrectFeedback();
feedbackFail = blockedHtml(feedbackFail);
feedback.setText(feedbackFail);
}
}
use of uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem in project OpenOLAT by OpenOLAT.
the class QTI12To21Converter method convert.
private void convert(Section section, TestPart testPart) throws URISyntaxException {
AssessmentSection assessmentSection = AssessmentTestFactory.appendAssessmentSection("Section", testPart);
assessmentSection.setTitle(section.getTitle());
convertDuration(section.getDuration(), assessmentSection);
RubricBlock rubricBlock = assessmentSection.getRubricBlocks().get(0);
rubricBlock.getBlocks().clear();
String objectives = section.getObjectives();
htmlBuilder.appendHtml(rubricBlock, blockedHtml(objectives));
boolean shuffle = SelectionOrdering.RANDOM.equals(section.getSelection_ordering().getOrderType());
assessmentSection.getOrdering().setShuffle(shuffle);
int selectionNum = section.getSelection_ordering().getSelectionNumber();
if (selectionNum > 0) {
Selection selection = new Selection(assessmentSection);
selection.setSelect(selectionNum);
assessmentSection.setSelection(selection);
}
List<Item> items = section.getItems();
for (Item item : items) {
AssessmentItemBuilder itemBuilder = null;
if (item != null && item.getQuestion() != null) {
int questionType = item.getQuestion().getType();
switch(questionType) {
case Question.TYPE_SC:
itemBuilder = convertSingleChoice(item);
break;
case Question.TYPE_MC:
itemBuilder = convertMultipleChoice(item);
break;
case Question.TYPE_KPRIM:
itemBuilder = convertKPrim(item);
break;
case Question.TYPE_FIB:
itemBuilder = convertFIB(item);
break;
case Question.TYPE_ESSAY:
itemBuilder = convertEssay(item);
break;
}
} else {
errors.add(item.getTitle());
log.error("Item without question: " + item);
}
if (itemBuilder != null) {
itemBuilder.build();
AssessmentItem assessmentItem = itemBuilder.getAssessmentItem();
AssessmentItemRef itemRef = new AssessmentItemRef(assessmentSection);
String itemId = IdentifierGenerator.newAsString(itemBuilder.getQuestionType().getPrefix());
itemRef.setIdentifier(Identifier.parseString(itemId));
convertItemBasics(item, itemRef);
File itemFile = new File(unzippedDirRoot, itemId + ".xml");
itemRef.setHref(new URI(itemFile.getName()));
assessmentSection.getSectionParts().add(itemRef);
persistAssessmentObject(itemFile, assessmentItem);
appendResourceAndMetadata(item, itemBuilder, itemFile);
// collect max score
Double maxScore = QtiNodesExtractor.extractMaxScore(assessmentItem);
if (maxScore != null && maxScore.doubleValue() > 0.0d) {
atomicMaxScore.add(maxScore.doubleValue());
}
}
}
}
use of uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem 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);
}
}
Aggregations