use of uk.ac.ed.ph.jqtiplus.value.Value in project OpenOLAT by OpenOLAT.
the class AssessmentRenderFunctions method testFeedbackVisible.
public static final boolean testFeedbackVisible(TestFeedback testFeedback, TestSessionState testSessionState) {
// <xsl:variable name="identifierMatch" select="boolean(qw:value-contains(qw:get-test-outcome-value(@outcomeIdentifier), @identifier))" as="xs:boolean"/>
Identifier outcomeIdentifier = testFeedback.getOutcomeIdentifier();
Value outcomeValue = testSessionState.getOutcomeValue(outcomeIdentifier);
boolean identifierMatch = valueContains(outcomeValue, testFeedback.getOutcomeValue());
// <xsl:if test="($identifierMatch and @showHide='show') or (not($identifierMatch) and @showHide='hide')">
if ((identifierMatch && testFeedback.getVisibilityMode() == VisibilityMode.SHOW_IF_MATCH) || (!identifierMatch && testFeedback.getVisibilityMode() == VisibilityMode.HIDE_IF_MATCH)) {
return true;
}
return false;
}
use of uk.ac.ed.ph.jqtiplus.value.Value in project OpenOLAT by OpenOLAT.
the class AssessmentRenderFunctions method getResponseValueAsBase64.
public static String getResponseValueAsBase64(AssessmentItem assessmentItem, AssessmentTestSession candidateSession, ItemSessionState itemSessionState, Identifier identifier, boolean solutionMode) {
Value val = getResponseValue(assessmentItem, itemSessionState, identifier, solutionMode);
String encodedString = null;
if (val instanceof FileValue) {
FileValue fileValue = (FileValue) val;
File myStore = CoreSpringFactory.getImpl(AssessmentTestSessionDAO.class).getSessionStorage(candidateSession);
File submissionDir = new File(myStore, "submissions");
File submittedFile = new File(submissionDir, fileValue.getFileName());
try (InputStream inStream = new FileInputStream(submittedFile)) {
byte[] binaryData = IOUtils.toByteArray(inStream);
encodedString = new String(Base64.encodeBase64(binaryData), "UTF8");
} catch (Exception e) {
log.error("", e);
}
}
return encodedString;
}
use of uk.ac.ed.ph.jqtiplus.value.Value in project OpenOLAT by OpenOLAT.
the class SingleChoiceAssessmentItemBuilder method extract.
@Override
public void extract() {
super.extract();
if (choiceInteraction != null) {
ResponseDeclaration responseDeclaration = assessmentItem.getResponseDeclaration(choiceInteraction.getResponseIdentifier());
if (responseDeclaration != null && responseDeclaration.getCorrectResponse() != null) {
CorrectResponse correctResponse = responseDeclaration.getCorrectResponse();
List<FieldValue> values = correctResponse.getFieldValues();
Value value = FieldValue.computeValue(Cardinality.SINGLE, values);
if (value instanceof IdentifierValue) {
IdentifierValue identifierValue = (IdentifierValue) value;
correctAnswer = identifierValue.identifierValue();
}
}
}
}
use of uk.ac.ed.ph.jqtiplus.value.Value in project OpenOLAT by OpenOLAT.
the class MaximaOperator method evaluateSelf.
@Override
protected Value evaluateSelf(OpenOLATExtensionPackage jqtiExtensionPackage, ProcessingContext context, Value[] childValues, int depth) {
AttributeList attributes = getAttributes();
Attribute<?> attrValue = attributes.get("value");
String code = (String) attrValue.getValue();
log.debug("Performing scriptRule: code={}, simplify={}" + code);
/* Pass variables to Maxima */
final QtiMaximaProcess qtiMaximaProcess = jqtiExtensionPackage.obtainMaximaSessionForThread();
for (int i = 0; i < childValues.length; i++) {
Value childValue = childValues[i];
String val = getValue(childValue);
code = code.replace("$(" + (i + 1) + ")", val);
}
/* Run code */
log.debug("Executing scriptRule code");
try {
qtiMaximaProcess.executeScriptRule(code, true);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the ScriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
} catch (final RuntimeException e) {
context.fireRuntimeError(this, "An unexpected problem occurred while trying to run the scriptRule logic. Not setting QTI variables and returing FALSE");
return BooleanValue.FALSE;
}
/* Read variables back */
log.debug("Reading variables back from Maxima");
/* Run Maxima code and extract result */
log.info("Running code to determine result of MAXIMA operator");
final Class<? extends ValueWrapper> resultClass = GlueValueBinder.getCasReturnClass(ReturnTypeType.FLOAT);
ValueWrapper maximaResult;
try {
maximaResult = qtiMaximaProcess.executeCasProcess(code, true, resultClass);
} catch (final MaximaTimeoutException e) {
context.fireRuntimeError(this, "A timeout occurred executing the CasCondition logic. Returning NULL");
return NullValue.INSTANCE;
} catch (final MathsContentTooComplexException e) {
context.fireRuntimeError(this, "An unexpected problem occurred querying the result of CasProcess, so returning NULL");
return NullValue.INSTANCE;
} catch (final MathAssessBadCasCodeException e) {
context.fireRuntimeError(this, "Your CasProcess code did not work as expected. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'. The failure reason was: " + e.getReason());
return NullValue.INSTANCE;
} catch (final QtiMaximaTypeConversionException e) {
context.fireRuntimeError(this, "Your CasProcess code did not produce a result that could be converted into the required QTI type. The CAS input was '" + e.getMaximaInput() + "' and the CAS output was '" + e.getMaximaOutput() + "'");
return NullValue.INSTANCE;
} catch (final RuntimeException e) {
log.warn("Unexpected Maxima failure", e);
context.fireRuntimeError(this, "An unexpected problem occurred while executing this CasProcess");
return BooleanValue.FALSE;
}
/* Bind result */
Value result = GlueValueBinder.casToJqti(maximaResult);
if (result == null) {
context.fireRuntimeError(this, "Failed to convert result from Maxima back to a QTI variable - returning NULL");
return NullValue.INSTANCE;
}
return result;
}
use of uk.ac.ed.ph.jqtiplus.value.Value in project openolat by klemens.
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