use of uk.ac.ed.ph.jqtiplus.node.item.interaction.MatchInteraction in project openolat by klemens.
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.interaction.MatchInteraction in project openolat by klemens.
the class MatchAssessmentItemBuilder method extractMatchInteraction.
private void extractMatchInteraction() {
try (StringOutput sb = new StringOutput()) {
List<Block> blocks = assessmentItem.getItemBody().getBlocks();
for (Block block : blocks) {
if (block instanceof MatchInteraction) {
matchInteraction = (MatchInteraction) block;
responseIdentifier = matchInteraction.getResponseIdentifier();
shuffle = matchInteraction.getShuffle();
break;
} else {
serializeJqtiObject(block, sb);
}
}
question = sb.toString();
} catch (IOException e) {
log.error("", e);
}
}
use of uk.ac.ed.ph.jqtiplus.node.item.interaction.MatchInteraction in project openolat by klemens.
the class AssessmentItemFactory method appendMatchInteractionTrueFalse.
public static MatchInteraction appendMatchInteractionTrueFalse(ItemBody itemBody, String unanswered, String right, String wrong, Identifier responseDeclarationId) {
MatchInteraction matchInteraction = new MatchInteraction(itemBody);
matchInteraction.setResponseIdentifier(responseDeclarationId);
matchInteraction.setMaxAssociations(4);
matchInteraction.setShuffle(false);
itemBody.getBlocks().add(matchInteraction);
PromptGroup prompts = new PromptGroup(matchInteraction);
matchInteraction.getNodeGroups().add(prompts);
SimpleMatchSet sourceMatchSet = new SimpleMatchSet(matchInteraction);
matchInteraction.getSimpleMatchSets().add(sourceMatchSet);
String[] sources = new String[] { "A", "B" };
for (int i = 0; i < sources.length; i++) {
appendSimpleAssociableChoice(sourceMatchSet, sources[i], 1, 1);
}
SimpleMatchSet targetMatchSet = new SimpleMatchSet(matchInteraction);
matchInteraction.getSimpleMatchSets().add(targetMatchSet);
String[] targets = new String[] { unanswered, right, wrong };
for (int i = 0; i < targets.length; i++) {
appendSimpleAssociableChoice(targetMatchSet, targets[i], 0, 0);
}
return matchInteraction;
}
use of uk.ac.ed.ph.jqtiplus.node.item.interaction.MatchInteraction in project openolat by klemens.
the class MatchInteractionArchive method writeHeader2.
@Override
public int writeHeader2(AssessmentItem item, Interaction interaction, int itemNumber, int interactionNumber, Row dataRow, int col, OpenXMLWorkbook workbook) {
MatchInteraction matchInteraction = (MatchInteraction) interaction;
boolean kprim = matchInteraction.getResponseIdentifier().toString().startsWith("KPRIM_") || QTI21QuestionType.hasClass(matchInteraction, QTI21Constants.CSS_MATCH_KPRIM);
String fix = kprim ? "_KP" : "_K";
int numOfChoices = matchInteraction.getSimpleMatchSets().get(0).getSimpleAssociableChoices().size();
if (numOfChoices > 0) {
for (int i = 0; i < numOfChoices; i++) {
String header = (itemNumber + 1) + fix + (i + 1);
dataRow.addCell(col++, header, workbook.getStyles().getHeaderStyle());
}
} else {
col++;
}
return col;
}
use of uk.ac.ed.ph.jqtiplus.node.item.interaction.MatchInteraction in project openolat by klemens.
the class MatchInteractionArchive method writeInteractionData.
@Override
public int writeInteractionData(AssessmentItem item, AssessmentResponse response, Interaction interaction, int itemNumber, Row dataRow, int col, OpenXMLWorkbook workbook) {
MatchInteraction matchInteraction = (MatchInteraction) interaction;
String stringuifiedResponse = response == null ? null : response.getStringuifiedResponse();
if (!StringHelper.containsNonWhitespace(stringuifiedResponse)) {
col += matchInteraction.getSimpleMatchSets().get(0).getSimpleAssociableChoices().size();
} else {
boolean kprim = matchInteraction.getResponseIdentifier().toString().startsWith("KPRIM_") || QTI21QuestionType.hasClass(matchInteraction, QTI21Constants.CSS_MATCH_KPRIM);
Set<String> correctAnswers = CorrectResponsesUtil.getCorrectDirectPairResponses(item, matchInteraction, false);
List<String> responses = CorrectResponsesUtil.parseResponses(stringuifiedResponse);
SimpleMatchSet firstMatchSet = matchInteraction.getSimpleMatchSets().get(0);
SimpleMatchSet secondMatchSet = matchInteraction.getSimpleMatchSets().get(1);
for (SimpleAssociableChoice choice : firstMatchSet.getSimpleAssociableChoices()) {
String choiceIdentifier = choice.getIdentifier().toString();
if (kprim) {
String markerCorrect = choiceIdentifier + " correct";
String markerWrong = choiceIdentifier + " wrong";
boolean isCorrectRight = correctAnswers.contains(markerCorrect);
String rightFlag = isCorrectRight ? markerCorrect : markerWrong;
String wrongFlag = isCorrectRight ? markerWrong : markerCorrect;
String value = null;
if (stringuifiedResponse.contains(markerCorrect)) {
value = "+";
} else if (stringuifiedResponse.contains(markerWrong)) {
value = "-";
}
if (stringuifiedResponse.indexOf(rightFlag) >= 0) {
dataRow.addCell(col++, value, workbook.getStyles().getCorrectStyle());
} else if (stringuifiedResponse.indexOf(wrongFlag) >= 0) {
dataRow.addCell(col++, value, null);
} else {
col++;
}
} else {
String aResponse = null;
for (String r : responses) {
if (r.startsWith(choiceIdentifier)) {
aResponse = r;
}
}
if (StringHelper.containsNonWhitespace(aResponse)) {
boolean correct = correctAnswers.contains(aResponse);
String value = null;
for (SimpleAssociableChoice association : secondMatchSet.getSimpleAssociableChoices()) {
if (aResponse.endsWith(association.getIdentifier().toString())) {
value = getContent(association);
break;
}
}
if (correct) {
dataRow.addCell(col++, value, workbook.getStyles().getCorrectStyle());
} else {
dataRow.addCell(col++, value, null);
}
} else {
col++;
}
}
}
}
return col;
}
Aggregations