use of uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer in project OpenOLAT by OpenOLAT.
the class AssessmentHtmlBuilderTest method filter.
@Test
public void filter() throws IOException {
String content = "<html><p>Test \u00EA<strong><span><img src='img.jpg'></span></strong></p><p>Test 2</p></html>";
AssessmentItem item = new AssessmentItem();
ItemBody helper = new ItemBody(item);
new AssessmentHtmlBuilder().appendHtml(helper, content);
List<Block> paragraphs = helper.getBlocks();
Assert.assertNotNull(paragraphs);
Assert.assertEquals(2, paragraphs.size());
// The serializer can throw some exceptions if it doens't like the model
// we want to serialize.
StringOutput sb = new StringOutput();
QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
qtiSerializer.serializeJqtiObject(helper, new StreamResult(sb));
String serializedQti = sb.toString();
Assert.assertTrue(serializedQti.contains("img.jpg"));
sb.close();
}
use of uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer in project OpenOLAT by OpenOLAT.
the class AssessmentHtmlBuilderTest method serializeVideo.
@Test
public void serializeVideo() throws IOException {
String content = "<p><span id=\"olatFlashMovieViewer213060\" class=\"olatFlashMovieViewer\" style=\"display:block;border:solid 1px #000; width:320px; height:240px;\">\n" + "<script src=\"/raw/fx-111111x11/movie/player.js\" type=\"text/javascript\"></script>\n" + "<script type=\"text/javascript\" defer=\"defer\">// <![CDATA[\n" + "BPlayer.insertPlayer(\"demo-video.mp4\",\"olatFlashMovieViewer213060\",320,240,0,0,\"video\",undefined,false,false,true,undefined);\n" + "// ]]></script>\n" + "</span></p>";
AssessmentItem item = new AssessmentItem();
ItemBody helper = new ItemBody(item);
new AssessmentHtmlBuilder().appendHtml(helper, content);
List<Block> paragraphs = helper.getBlocks();
Assert.assertNotNull(paragraphs);
Assert.assertEquals(1, paragraphs.size());
StringOutput sb = new StringOutput();
QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
qtiSerializer.serializeJqtiObject(helper, new StreamResult(sb));
String serializedQti = sb.toString();
Assert.assertNotNull(serializedQti);
Assert.assertTrue(serializedQti.contains("object"));
Assert.assertFalse(serializedQti.contains("span"));
Assert.assertFalse(serializedQti.contains("script"));
sb.close();
}
use of uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer in project OpenOLAT by OpenOLAT.
the class HottextAssessmentItemBuilderTest method readHottextAssessmentItem.
@Test
public void readHottextAssessmentItem() throws URISyntaxException {
QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
URL itemUrl = AssessmentItemBuilderTest.class.getResource("resources/openolat/hottext-per-answer-11-4-0.xml");
AssessmentItem assessmentItem = loadAssessmentItem(itemUrl);
HottextAssessmentItemBuilder itemBuilder = new HottextAssessmentItemBuilder(assessmentItem, qtiSerializer);
// basic check ChoiceInteraction
AssessmentItem loadedItem = itemBuilder.getAssessmentItem();
List<Interaction> interactions = loadedItem.getItemBody().findInteractions();
Assert.assertNotNull(interactions);
Assert.assertEquals(1, interactions.size());
Assert.assertTrue(interactions.get(0) instanceof HottextInteraction);
// hot texts
Choice correct1 = itemBuilder.getChoice(Identifier.assumedLegal("htf9250a96f24bc8873c9cc54dfbaaaa"));
Assert.assertTrue(itemBuilder.isCorrect(correct1));
Choice correct2 = itemBuilder.getChoice(Identifier.assumedLegal("RESPONSE_3"));
Assert.assertTrue(itemBuilder.isCorrect(correct2));
Choice wrong1 = itemBuilder.getChoice(Identifier.assumedLegal("RESPONSE_2"));
Assert.assertFalse(itemBuilder.isCorrect(wrong1));
Choice wrong2 = itemBuilder.getChoice(Identifier.assumedLegal("RESPONSE_4"));
Assert.assertFalse(itemBuilder.isCorrect(wrong2));
// score
Double maxScore = itemBuilder.getMaxScoreBuilder().getScore();
Assert.assertEquals(3.0, maxScore.doubleValue(), 0.0001);
Double minScore = itemBuilder.getMinScoreBuilder().getScore();
Assert.assertEquals(0.0, minScore.doubleValue(), 0.0001);
// per answer
Assert.assertEquals(ScoreEvaluation.perAnswer, itemBuilder.getScoreEvaluationMode());
}
use of uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer in project OpenOLAT by OpenOLAT.
the class HottextAssessmentItemBuilderTest method readHottextAssessmentItem_allCorrectAnswers.
@Test
public void readHottextAssessmentItem_allCorrectAnswers() throws URISyntaxException {
QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
URL itemUrl = AssessmentItemBuilderTest.class.getResource("resources/openolat/hottext-score-all-11-4-0.xml");
AssessmentItem assessmentItem = loadAssessmentItem(itemUrl);
HottextAssessmentItemBuilder itemBuilder = new HottextAssessmentItemBuilder(assessmentItem, qtiSerializer);
// basic check ChoiceInteraction
AssessmentItem loadedItem = itemBuilder.getAssessmentItem();
List<Interaction> interactions = loadedItem.getItemBody().findInteractions();
Assert.assertNotNull(interactions);
Assert.assertEquals(2, interactions.size());
Assert.assertTrue(interactions.get(0) instanceof HottextInteraction);
Assert.assertTrue(interactions.get(1) instanceof EndAttemptInteraction);
// hot texts
Choice correct1 = itemBuilder.getChoice(Identifier.assumedLegal("htebdb40344641dba115e3c8c6ce3926"));
Assert.assertTrue(itemBuilder.isCorrect(correct1));
Choice correct2 = itemBuilder.getChoice(Identifier.assumedLegal("ht103ce53892dea97613005a5ce76be31e"));
Assert.assertTrue(itemBuilder.isCorrect(correct2));
Choice wrong1 = itemBuilder.getChoice(Identifier.assumedLegal("hte11a51c3e3d86a5f7293da19a1a8700e"));
Assert.assertFalse(itemBuilder.isCorrect(wrong1));
// score
Double maxScore = itemBuilder.getMaxScoreBuilder().getScore();
Assert.assertEquals(2.0, maxScore.doubleValue(), 0.0001);
Double minScore = itemBuilder.getMinScoreBuilder().getScore();
Assert.assertEquals(0.0, minScore.doubleValue(), 0.0001);
// per answer
Assert.assertEquals(ScoreEvaluation.allCorrectAnswers, itemBuilder.getScoreEvaluationMode());
// correct feedback
ModalFeedbackBuilder correctFeedback = itemBuilder.getCorrectFeedback();
Assert.assertNotNull(correctFeedback);
Assert.assertNotNull(correctFeedback.getText());
Assert.assertTrue(correctFeedback.getText().contains("You check the right answers"));
// incorrect
ModalFeedbackBuilder incorrectFeedback = itemBuilder.getIncorrectFeedback();
Assert.assertNotNull(incorrectFeedback);
Assert.assertNotNull(incorrectFeedback.getText());
Assert.assertTrue(incorrectFeedback.getText().contains("Some of your anwsers are not the correct one."));
// correct solution feedback
ModalFeedbackBuilder correctSolutionFeedback = itemBuilder.getCorrectSolutionFeedback();
Assert.assertNotNull(correctSolutionFeedback);
Assert.assertNotNull(correctSolutionFeedback.getText());
Assert.assertTrue(correctSolutionFeedback.getText().contains("A little hint towards the correct solution"));
// hint
ModalFeedbackBuilder hint = itemBuilder.getHint();
Assert.assertNotNull(hint);
Assert.assertNotNull(hint.getText());
Assert.assertTrue(hint.getText().contains("This is an endAttemptInteraction"));
}
use of uk.ac.ed.ph.jqtiplus.serialization.QtiSerializer in project OpenOLAT by OpenOLAT.
the class HottextAssessmentItemBuilderTest method createHottextAssessmentItem_perAnswer.
/**
* This is an hottextInteraction with 3 choices, one with 3.0 points and correct,
* one with 0.0 points but correct and one false with -1.0 points.
*
* @throws IOException
*/
@Test
public void createHottextAssessmentItem_perAnswer() throws IOException {
QtiSerializer qtiSerializer = new QtiSerializer(new JqtiExtensionManager());
HottextAssessmentItemBuilder itemBuilder = new HottextAssessmentItemBuilder("Hot texts", "This is a hot ", "text", qtiSerializer);
itemBuilder.setQuestion("<p>This is <hottext identifier=\"RESPONSE_HOT_1\">hot</hottext>, <hottext identifier=\"RESPONSE_HOT_2\">cold</hottext> or <hottext identifier=\"RESPONSE_HOT_3\">freezing</hottext></p>");
itemBuilder.addCorrectAnswer(Identifier.parseString("RESPONSE_HOT_2"));
itemBuilder.addCorrectAnswer(Identifier.parseString("RESPONSE_HOT_3"));
itemBuilder.setMapping(Identifier.parseString("RESPONSE_HOT_1"), -1.0d);
itemBuilder.setMapping(Identifier.parseString("RESPONSE_HOT_2"), 0.0d);
itemBuilder.setMapping(Identifier.parseString("RESPONSE_HOT_3"), 3.0d);
itemBuilder.setMaxScore(3.0d);
itemBuilder.setScoreEvaluationMode(ScoreEvaluation.perAnswer);
itemBuilder.build();
File itemFile = new File(WebappHelper.getTmpDir(), "hottextAssessmentItem" + UUID.randomUUID() + ".xml");
try (FileOutputStream out = new FileOutputStream(itemFile)) {
qtiSerializer.serializeJqtiObject(itemBuilder.getAssessmentItem(), out);
} catch (Exception e) {
log.error("", e);
}
{
// correct answers
Map<Identifier, ResponseData> responseMap = new HashMap<>();
Identifier responseIdentifier = itemBuilder.getInteraction().getResponseIdentifier();
responseMap.put(responseIdentifier, new StringResponseData("RESPONSE_HOT_2", "RESPONSE_HOT_3"));
ItemSessionController itemSessionController = RunningItemHelper.run(itemFile, responseMap);
Value score = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.SCORE_IDENTIFIER);
Assert.assertEquals(new FloatValue(3.0d), score);
Value feedbackBasic = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.FEEDBACKBASIC_IDENTIFIER);
Assert.assertEquals(new IdentifierValue(QTI21Constants.CORRECT_IDENTIFIER), feedbackBasic);
}
{
// max score but not all correct
Map<Identifier, ResponseData> responseMap = new HashMap<>();
Identifier responseIdentifier = itemBuilder.getInteraction().getResponseIdentifier();
responseMap.put(responseIdentifier, new StringResponseData("RESPONSE_HOT_3"));
ItemSessionController itemSessionController = RunningItemHelper.run(itemFile, responseMap);
Value score = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.SCORE_IDENTIFIER);
Assert.assertEquals(new FloatValue(3.0d), score);
Value feedbackBasic = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.FEEDBACKBASIC_IDENTIFIER);
Assert.assertEquals(new IdentifierValue(QTI21Constants.INCORRECT_IDENTIFIER), feedbackBasic);
}
{
// all wrong
Map<Identifier, ResponseData> responseMap = new HashMap<>();
Identifier responseIdentifier = itemBuilder.getInteraction().getResponseIdentifier();
responseMap.put(responseIdentifier, new StringResponseData("RESPONSE_HOT_1"));
ItemSessionController itemSessionController = RunningItemHelper.run(itemFile, responseMap);
Value score = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.SCORE_IDENTIFIER);
Assert.assertEquals(new FloatValue(0.0d), score);
Value feedbackBasic = itemSessionController.getItemSessionState().getOutcomeValue(QTI21Constants.FEEDBACKBASIC_IDENTIFIER);
Assert.assertEquals(new IdentifierValue(QTI21Constants.INCORRECT_IDENTIFIER), feedbackBasic);
}
FileUtils.deleteDirsAndFiles(itemFile.toPath());
}
Aggregations