use of org.olat.ims.qti21.AssessmentResponse in project OpenOLAT by OpenOLAT.
the class AssessmentItemDisplayController method handleResponses.
public void handleResponses(UserRequest ureq, Map<Identifier, ResponseInput> stringResponseMap, Map<Identifier, ResponseInput> fileResponseMap, String candidateComment) {
/* Retrieve current JQTI state and set up JQTI controller */
NotificationRecorder notificationRecorder = new NotificationRecorder(NotificationLevel.INFO);
ItemSessionState itemSessionState = itemSessionController.getItemSessionState();
/* Make sure an attempt is allowed */
if (itemSessionState.isEnded()) {
candidateAuditLogger.logAndThrowCandidateException(candidateSession, CandidateExceptionReason.RESPONSES_NOT_EXPECTED, null);
logError("RESPONSES_NOT_EXPECTED", null);
return;
}
/* Build response map in required format for JQTI+.
* NB: The following doesn't test for duplicate keys in the two maps. I'm not sure
* it's worth the effort.
*/
final Map<Identifier, ResponseData> responseDataMap = new HashMap<>();
// final Map<Identifier, CandidateFileSubmission> fileSubmissionMap = new HashMap<>();
final Map<Identifier, AssessmentResponse> assessmentResponseDataMap = new HashMap<>();
if (stringResponseMap != null) {
for (final Entry<Identifier, ResponseInput> stringResponseEntry : stringResponseMap.entrySet()) {
Identifier identifier = stringResponseEntry.getKey();
ResponseInput responseData = stringResponseEntry.getValue();
if (responseData instanceof StringInput) {
responseDataMap.put(identifier, new StringResponseData(((StringInput) responseData).getResponseData()));
} else if (responseData instanceof Base64Input) {
// TODO
} else if (responseData instanceof FileInput) {
}
}
}
if (fileResponseMap != null) {
for (final Entry<Identifier, ResponseInput> fileResponseEntry : fileResponseMap.entrySet()) {
final Identifier identifier = fileResponseEntry.getKey();
final FileInput multipartFile = (FileInput) fileResponseEntry.getValue();
if (!multipartFile.isEmpty()) {
// final CandidateFileSubmission fileSubmission = candidateUploadService.importFileSubmission(candidateSession, multipartFile);
File storedFile = qtiService.importFileSubmission(candidateSession, multipartFile.getMultipartFileInfos());
final FileResponseData fileResponseData = new FileResponseData(storedFile, multipartFile.getContentType(), multipartFile.getFileName());
responseDataMap.put(identifier, fileResponseData);
assessmentResponseDataMap.put(identifier, new AssessmentResponseData(identifier, fileResponseData));
// fileSubmissionMap.put(identifier, fileSubmission);
}
}
}
/* Submit comment (if provided)
* NB: Do this first in case next actions end the item session.
*/
final Date timestamp = ureq.getRequestTimestamp();
if (candidateComment != null) {
try {
itemSessionController.setCandidateComment(timestamp, candidateComment);
} catch (final QtiCandidateStateException e) {
candidateAuditLogger.logAndThrowCandidateException(candidateSession, CandidateExceptionReason.CANDIDATE_COMMENT_FORBIDDEN, e);
logError("CANDIDATE_COMMENT_FORBIDDEN", null);
return;
} catch (final RuntimeException e) {
logError("", e);
// handleExplosion(e, candidateSession);
return;
}
}
/* Attempt to bind responses */
boolean allResponsesValid = false, allResponsesBound = false;
try {
itemSessionController.bindResponses(timestamp, responseDataMap);
/* Note any responses that failed to bind */
final Set<Identifier> badResponseIdentifiers = itemSessionState.getUnboundResponseIdentifiers();
allResponsesBound = badResponseIdentifiers.isEmpty();
/* Now validate the responses according to any constraints specified by the interactions */
if (allResponsesBound) {
final Set<Identifier> invalidResponseIdentifiers = itemSessionState.getInvalidResponseIdentifiers();
allResponsesValid = invalidResponseIdentifiers.isEmpty();
}
/* (We commit responses immediately here) */
itemSessionController.commitResponses(timestamp);
/* Invoke response processing (only if responses are valid) */
if (allResponsesValid) {
itemSessionController.performResponseProcessing(timestamp);
}
} catch (final QtiCandidateStateException e) {
candidateAuditLogger.logAndThrowCandidateException(candidateSession, CandidateExceptionReason.RESPONSES_NOT_EXPECTED, null);
logError("RESPONSES_NOT_EXPECTED", e);
return;
} catch (final RuntimeException e) {
logError("", e);
// handleExplosion(e, candidateSession);
return;
}
/* Record resulting attempt and event */
final CandidateItemEventType eventType = allResponsesBound ? (allResponsesValid ? CandidateItemEventType.ATTEMPT_VALID : CandidateItemEventType.RESPONSE_INVALID) : CandidateItemEventType.RESPONSE_BAD;
final CandidateEvent candidateEvent = qtiService.recordCandidateItemEvent(candidateSession, null, entry, eventType, itemSessionState, notificationRecorder);
candidateAuditLogger.logCandidateEvent(candidateEvent, assessmentResponseDataMap);
lastEvent = candidateEvent;
/* Record current result state, or finish session */
updateSessionFinishedStatus(ureq);
}
use of org.olat.ims.qti21.AssessmentResponse in project OpenOLAT by OpenOLAT.
the class AssessmentTestDisplayController method handleResponse.
// public CandidateSession handleResponses(final CandidateSessionContext candidateSessionContext,
// final Map<Identifier, StringResponseData> stringResponseMap,
// final Map<Identifier, MultipartFile> fileResponseMap,
// final String candidateComment)
private void handleResponse(UserRequest ureq, Map<Identifier, ResponseInput> stringResponseMap, Map<Identifier, ResponseInput> fileResponseMap, String candidateComment) {
NotificationRecorder notificationRecorder = new NotificationRecorder(NotificationLevel.INFO);
TestSessionState testSessionState = testSessionController.getTestSessionState();
TestPlanNodeKey currentItemKey = testSessionState.getCurrentItemKey();
if (currentItemKey == null && getLastEvent() != null && getLastEvent().getTestEventType() == CandidateTestEventType.REVIEW_ITEM) {
// someone try to send the form in review with tab / return
return;
}
if (!qtiWorksCtrl.validatePresentedItem(currentItemKey)) {
logError("Response send by browser doesn't match current item key", null);
ServletUtil.printOutRequestParameters(ureq.getHttpReq());
if (candidateSession != null && candidateSession.getFinishTime() != null) {
showWarning("error.test.closed");
} else {
showWarning("error.reload.question");
}
// this is not the right node in the plan
return;
}
final Map<Identifier, File> fileSubmissionMap = new HashMap<>();
final Map<Identifier, ResponseData> responseDataMap = new HashMap<>();
if (stringResponseMap != null) {
for (final Entry<Identifier, ResponseInput> responseEntry : stringResponseMap.entrySet()) {
final Identifier identifier = responseEntry.getKey();
final ResponseInput responseData = responseEntry.getValue();
if (responseData instanceof StringInput) {
responseDataMap.put(identifier, new StringResponseData(((StringInput) responseData).getResponseData()));
} else if (responseData instanceof Base64Input) {
// only used from drawing interaction
Base64Input fileInput = (Base64Input) responseData;
String filename = "submitted_image.png";
File storedFile = qtiService.importFileSubmission(candidateSession, filename, fileInput.getResponseData());
responseDataMap.put(identifier, new FileResponseData(storedFile, fileInput.getContentType(), storedFile.getName()));
fileSubmissionMap.put(identifier, storedFile);
} else if (responseData instanceof FileInput) {
FileInput fileInput = (FileInput) responseData;
File storedFile = qtiService.importFileSubmission(candidateSession, fileInput.getMultipartFileInfos());
responseDataMap.put(identifier, new FileResponseData(storedFile, fileInput.getContentType(), storedFile.getName()));
fileSubmissionMap.put(identifier, storedFile);
}
}
}
ParentPartItemRefs parentParts = getParentSection(currentItemKey);
String assessmentItemIdentifier = currentItemKey.getIdentifier().toString();
AssessmentItemSession itemSession = qtiService.getOrCreateAssessmentItemSession(candidateSession, parentParts, assessmentItemIdentifier);
if (fileResponseMap != null) {
for (Entry<Identifier, ResponseInput> fileResponseEntry : fileResponseMap.entrySet()) {
Identifier identifier = fileResponseEntry.getKey();
FileInput multipartFile = (FileInput) fileResponseEntry.getValue();
if (!multipartFile.isEmpty()) {
File storedFile = qtiService.importFileSubmission(candidateSession, multipartFile.getMultipartFileInfos());
responseDataMap.put(identifier, new FileResponseData(storedFile, multipartFile.getContentType(), storedFile.getName()));
fileSubmissionMap.put(identifier, storedFile);
}
}
}
Map<Identifier, AssessmentResponse> candidateResponseMap = qtiService.getAssessmentResponses(itemSession);
for (Entry<Identifier, ResponseData> responseEntry : responseDataMap.entrySet()) {
Identifier responseIdentifier = responseEntry.getKey();
ResponseData responseData = responseEntry.getValue();
AssessmentResponse candidateItemResponse;
if (candidateResponseMap.containsKey(responseIdentifier)) {
candidateItemResponse = candidateResponseMap.get(responseIdentifier);
} else {
candidateItemResponse = qtiService.createAssessmentResponse(candidateSession, itemSession, responseIdentifier.toString(), ResponseLegality.VALID, responseData.getType());
}
switch(responseData.getType()) {
case STRING:
{
List<String> data = ((StringResponseData) responseData).getResponseData();
String stringuifiedResponse = ResponseFormater.format(data);
candidateItemResponse.setStringuifiedResponse(stringuifiedResponse);
break;
}
case FILE:
{
if (fileSubmissionMap.get(responseIdentifier) != null) {
File storedFile = fileSubmissionMap.get(responseIdentifier);
candidateItemResponse.setStringuifiedResponse(storedFile.getName());
}
break;
}
default:
throw new OLATRuntimeException("Unexpected switch case: " + responseData.getType());
}
candidateResponseMap.put(responseIdentifier, candidateItemResponse);
}
boolean allResponsesValid = true;
boolean allResponsesBound = true;
final Date timestamp = ureq.getRequestTimestamp();
if (candidateComment != null) {
testSessionController.setCandidateCommentForCurrentItem(timestamp, candidateComment);
}
/* Attempt to bind responses (and maybe perform RP & OP) */
testSessionController.handleResponsesToCurrentItem(timestamp, responseDataMap);
/* Classify this event */
final SubmissionMode submissionMode = testSessionController.getCurrentTestPart().getSubmissionMode();
final CandidateItemEventType candidateItemEventType;
if (allResponsesValid) {
candidateItemEventType = submissionMode == SubmissionMode.INDIVIDUAL ? CandidateItemEventType.ATTEMPT_VALID : CandidateItemEventType.RESPONSE_VALID;
} else {
candidateItemEventType = allResponsesBound ? CandidateItemEventType.RESPONSE_INVALID : CandidateItemEventType.RESPONSE_BAD;
}
/* Record resulting event */
final CandidateEvent candidateEvent = qtiService.recordCandidateTestEvent(candidateSession, testEntry, entry, CandidateTestEventType.ITEM_EVENT, candidateItemEventType, currentItemKey, testSessionState, notificationRecorder);
candidateAuditLogger.logCandidateEvent(candidateEvent, candidateResponseMap);
this.lastEvent = candidateEvent;
/* Record current result state */
AssessmentResult assessmentResult = computeAndRecordTestAssessmentResult(timestamp, testSessionState, false);
ItemSessionState itemSessionState = testSessionState.getCurrentItemSessionState();
long itemDuration = itemSessionState.getDurationAccumulated();
itemSession.setDuration(itemDuration);
ItemResult itemResult = assessmentResult.getItemResult(assessmentItemIdentifier);
collectOutcomeVariablesForItemSession(itemResult, itemSession);
/* Persist CandidateResponse entities */
qtiService.recordTestAssessmentResponses(itemSession, candidateResponseMap.values());
/* Save any change to session state */
candidateSession = qtiService.updateAssessmentTestSession(candidateSession);
addToHistory(ureq, this);
checkConcurrentExit(ureq);
}
use of org.olat.ims.qti21.AssessmentResponse in project openolat by klemens.
the class QTI21ArchiveFormat method exportWorkbook.
public void exportWorkbook(OutputStream exportStream) {
RepositoryEntry testEntry = searchParams.getTestEntry();
FileResourceManager frm = FileResourceManager.getInstance();
File unzippedDirRoot = frm.unzipFileResource(testEntry.getOlatResource());
resolvedAssessmentTest = qtiService.loadAndResolveAssessmentTest(unzippedDirRoot, false, false);
// content
final List<AssessmentTestSession> sessions = testSessionDao.getTestSessionsOfResponse(searchParams);
final List<AssessmentResponse> responses = responseDao.getResponse(searchParams);
try (OpenXMLWorkbook workbook = new OpenXMLWorkbook(exportStream, 1)) {
// headers
OpenXMLWorksheet exportSheet = workbook.nextWorksheet();
exportSheet.setHeaderRows(2);
writeHeaders_1(exportSheet, workbook);
writeHeaders_2(exportSheet, workbook);
writeData(sessions, responses, exportSheet, workbook);
} catch (Exception e) {
log.error("", e);
}
}
use of org.olat.ims.qti21.AssessmentResponse in project openolat by klemens.
the class QTI21ArchiveFormat method writeDataRow.
private void writeDataRow(int num, SessionResponses responses, OpenXMLWorksheet exportSheet, OpenXMLWorkbook workbook) {
int col = 0;
Row dataRow = exportSheet.newRow();
// sequence number
dataRow.addCell(col++, num, null);
AssessmentTestSession testSession = responses.getTestSession();
AssessmentEntry entry = testSession.getAssessmentEntry();
Identity assessedIdentity = entry.getIdentity();
// user properties
if (assessedIdentity == null) {
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
if (userPropertyHandler != null) {
if (userPropertyHandlers.get(0) == userPropertyHandler) {
dataRow.addCell(col++, translator.translate("anonym.user"), null);
} else {
col++;
}
}
}
} else if (anonymizerCallback != null) {
String anonymizedName = anonymizerCallback.getAnonymizedUserName(assessedIdentity);
dataRow.addCell(col++, anonymizedName, null);
} else {
User assessedUser = assessedIdentity.getUser();
for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
if (userPropertyHandler != null) {
String property = userPropertyHandler.getUserProperty(assessedUser, translator.getLocale());
dataRow.addCell(col++, property, null);
}
}
}
// homepage
if (anonymizerCallback == null) {
String homepage;
if (entry.getIdentity() == null) {
homepage = "";
} else {
ContextEntry ce = BusinessControlFactory.getInstance().createContextEntry(entry.getIdentity());
homepage = BusinessControlFactory.getInstance().getAsURIString(Collections.singletonList(ce), false);
}
dataRow.addCell(col++, homepage, null);
}
// course node points and passed
if (courseNode instanceof AssessableCourseNode) {
AssessableCourseNode assessableCourseNode = (AssessableCourseNode) courseNode;
if (assessableCourseNode.hasScoreConfigured()) {
if (entry.getScore() != null) {
dataRow.addCell(col++, entry.getScore(), null);
} else {
col++;
}
}
if (assessableCourseNode.hasPassedConfigured()) {
if (entry.getPassed() != null) {
dataRow.addCell(col++, entry.getPassed().toString(), null);
} else {
col++;
}
}
}
// assesspoints, passed, ipaddress, date, duration
if (testSession.getScore() != null) {
dataRow.addCell(col++, testSession.getScore(), null);
} else {
col++;
}
if (testSession.getManualScore() != null) {
dataRow.addCell(col++, testSession.getManualScore(), null);
} else {
col++;
}
if (testSession.getFinalScore() != null) {
dataRow.addCell(col++, testSession.getFinalScore(), null);
} else {
col++;
}
if (testSession.getPassed() != null) {
dataRow.addCell(col++, testSession.getPassed().toString(), null);
} else {
col++;
}
if (anonymizerCallback == null) {
dataRow.addCell(col++, testSession.getCreationDate(), workbook.getStyles().getDateStyle());
}
dataRow.addCell(col++, toDurationInMilliseconds(testSession.getDuration()), null);
List<AbstractInfos> infos = getItemInfos();
for (int i = 0; i < infos.size(); i++) {
AbstractInfos info = infos.get(i);
if (info instanceof ItemInfos) {
ItemInfos item = (ItemInfos) info;
AssessmentItemRef itemRef = item.getAssessmentItemRef();
String itemRefIdentifier = itemRef.getIdentifier().toString();
AssessmentItemSession itemSession = responses.getItemSession(itemRefIdentifier);
if (exportConfig.isResponseCols()) {
List<Interaction> interactions = item.getInteractions();
for (int j = 0; j < interactions.size(); j++) {
Interaction interaction = interactions.get(j);
AssessmentResponse response = responses.getResponse(itemRefIdentifier, interaction.getResponseIdentifier());
col = interactionArchiveMap.get(interaction.getQtiClassName()).writeInteractionData(item.getAssessmentItem(), response, interaction, j, dataRow, col, workbook);
}
}
// score, start, duration
if (itemSession == null) {
if (exportConfig.isPointCol()) {
col++;
}
if (exportConfig.isCommentCol()) {
col++;
}
if (exportConfig.isTimeCols()) {
col += anonymizerCallback != null ? 1 : 2;
}
} else {
if (exportConfig.isPointCol()) {
if (itemSession.getManualScore() != null) {
dataRow.addCell(col++, itemSession.getManualScore(), null);
} else {
dataRow.addCell(col++, itemSession.getScore(), null);
}
}
if (exportConfig.isCommentCol()) {
dataRow.addCell(col++, itemSession.getCoachComment(), null);
}
if (exportConfig.isTimeCols()) {
if (anonymizerCallback == null) {
dataRow.addCell(col++, itemSession.getCreationDate(), workbook.getStyles().getTimeStyle());
}
dataRow.addCell(col++, toDurationInMilliseconds(itemSession.getDuration()), null);
}
}
} else if (numOfSections > 1 && info instanceof SectionInfos) {
SectionInfos section = (SectionInfos) info;
if (!section.getItemInfos().isEmpty()) {
BigDecimal score = calculateSectionScore(responses, section);
if (score != null) {
dataRow.addCell(col++, score, workbook.getStyles().getLightGrayStyle());
} else {
col++;
}
}
}
}
}
use of org.olat.ims.qti21.AssessmentResponse in project openolat by klemens.
the class AuditLogFormatter method log.
protected static void log(CandidateEvent candidateEvent, Map<Identifier, AssessmentResponse> candidateResponseMap, Writer writer) throws IOException {
writer.append("QTI21 audit [");
AssessmentTestSession testSession = candidateEvent.getCandidateSession();
if (testSession != null) {
RepositoryEntryRef testEntry = candidateEvent.getTestEntry();
RepositoryEntryRef courseEntry = candidateEvent.getRepositoryEntry();
if (courseEntry != null && !testEntry.getKey().equals(courseEntry.getKey())) {
writer.write(courseEntry.getKey().toString());
writer.write(":");
if (testSession.getSubIdent() == null) {
writer.write("NULL:");
} else {
writer.write(testSession.getSubIdent());
writer.write(":");
}
}
if (testEntry != null) {
writer.write(testEntry.getKey().toString());
}
}
writer.write("] ");
if (candidateEvent.getTestEventType() != null) {
writer.append("TestEvent:");
writer.append(candidateEvent.getTestEventType().toString());
writer.write(" ");
}
if (candidateEvent.getItemEventType() != null) {
writer.append("ItemEvent:");
writer.append(candidateEvent.getItemEventType().toString());
writer.write(" ");
}
if (candidateEvent.getTestItemKey() != null) {
writer.append("TestItemKey[");
writer.append(candidateEvent.getTestItemKey());
writer.write("] ");
}
if (candidateResponseMap != null) {
writer.write("params=");
for (Map.Entry<Identifier, AssessmentResponse> responseEntry : candidateResponseMap.entrySet()) {
Identifier identifier = responseEntry.getKey();
AssessmentResponse response = responseEntry.getValue();
writer.append("|");
writer.append(identifier.toString());
writer.append("=");
String stringuifiedResponse = response.getStringuifiedResponse();
if (stringuifiedResponse == null) {
writer.append("NULL");
} else {
writer.append(stringuifiedResponse);
}
}
}
}
Aggregations