Commit 5abb07b5 authored by Konstantin Schulz's avatar Konstantin Schulz
Browse files

single exercise results (in preview) are now sent to the backend server

parent 8d54de0c
{
"name": "mc_frontend",
"version": "1.0.9",
"version": "1.1.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
......
......@@ -17,6 +17,7 @@ window.onresize = () => {
export class ExerciseService {
public excludeOOV = false;
public fillBlanksString = 'fill_blanks';
public h5pIframeString = '#h5p-iframe-1';
public kwicGraphs: string;
public vocListString = 'voc_list';
......
......@@ -5,10 +5,12 @@ import {HelperService} from 'src/app/helper.service';
import {NavController} from '@ionic/angular';
import {ExerciseService} from 'src/app/exercise.service';
import {CorpusService} from 'src/app/corpus.service';
import {Component, OnDestroy} from '@angular/core';
import {Component, OnDestroy, OnInit} from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {Solution} from 'src/app/models/solution';
import {HttpClient} from '@angular/common/http';
import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {XAPIevent} from 'src/app/models/xAPIevent';
import {TestResultMC} from 'src/app/models/testResultMC';
declare var H5P: any;
......@@ -17,7 +19,7 @@ declare var H5P: any;
templateUrl: './preview.page.html',
styleUrls: ['./preview.page.scss'],
})
export class PreviewPage implements OnDestroy {
export class PreviewPage implements OnDestroy, OnInit {
HelperService = HelperService;
public ExerciseType = ExerciseType;
public FileType = FileType;
......@@ -65,6 +67,24 @@ export class PreviewPage implements OnDestroy {
H5P.externalDispatcher.off('xAPI');
}
ngOnInit(): void {
H5P.externalDispatcher.on('xAPI', (event: XAPIevent) => {
// results are only available when a task has been completed/answered, not in the "attempted" or "interacted" stages
if (event.data.statement.verb.id === HelperService.config['xAPIverbIDanswered'] && event.data.statement.result) {
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
if (iframe) {
const iframeDoc: Document = iframe.contentWindow.document;
const inner: string = iframeDoc.documentElement.innerHTML;
const result: TestResultMC = new TestResultMC({
statement: event.data.statement,
innerHTML: inner
});
this.sendData(result);
}
}
});
}
processAnnisResponse(ar: AnnisResponse) {
this.corpusService.annisResponse.solutions = ar.solutions;
this.processSolutions(ar.solutions);
......@@ -95,6 +115,21 @@ export class PreviewPage implements OnDestroy {
this.currentSolutions = newSolutions;
}
sendData(result: TestResultMC) {
const fileUrl: string = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiFilePath'];
HelperService.currentError = null;
HelperService.isLoading = true;
const formData = new FormData();
formData.append('learning_result', JSON.stringify(result.statement));
this.http.post(fileUrl, formData).subscribe(async () => {
HelperService.isLoading = false;
}, async (error: HttpErrorResponse) => {
HelperService.isLoading = false;
HelperService.currentError = error;
console.log('ERROR: COULD NOT SEND EXERCISE RESULTS TO SERVER.');
});
}
switchOOV() {
this.currentSolutions = [];
this.processSolutions(this.corpusService.annisResponse.solutions);
......
......@@ -84,7 +84,6 @@ export class TestPage implements OnDestroy, OnInit {
public h5pBlanksString = 'H5P.Blanks';
public h5pCheckButtonClassString = '.h5p-question-check-answer';
public h5pDragTextString = 'H5P.DragText';
public h5pIframeString = '#h5p-iframe-1';
public h5pMultiChoiceString = 'H5P.MultiChoice';
public h5pRetryClassString = '.h5p-question-try-again';
public h5pRowIDstring = '#h5p-row';
......@@ -241,7 +240,7 @@ export class TestPage implements OnDestroy, OnInit {
}
hideRetryButton() {
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
const iframeDoc: Document = iframe.contentWindow.document;
// hide the retry button during review
const retryButton: HTMLButtonElement = iframeDoc.documentElement.querySelector(this.h5pRetryClassString);
......@@ -270,7 +269,7 @@ export class TestPage implements OnDestroy, OnInit {
// If the count down is over, write some text
if (distance < 0) {
this.removeTimer(false);
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
if (iframe) {
const checkButton: HTMLButtonElement = iframe.contentWindow.document.body.querySelector(this.h5pCheckButtonClassString);
if (checkButton) {
......@@ -334,7 +333,7 @@ export class TestPage implements OnDestroy, OnInit {
}
saveResult(showSolutions: boolean, event: XAPIevent) {
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
if (iframe) {
const iframeDoc: Document = iframe.contentWindow.document;
const inner: string = iframeDoc.documentElement.innerHTML;
......@@ -437,7 +436,7 @@ export class TestPage implements OnDestroy, OnInit {
const id = this.vocService.currentTestResults[this.currentExerciseIndex].statement.context.contextActivities.category[0].id;
// handle the drag text exercise solutions
if (id.indexOf(this.h5pDragTextString) > -1) {
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
const iframeDoc: Document = iframe.contentWindow.document;
iframeDoc.documentElement.innerHTML = this.vocService.currentTestResults[this.currentExerciseIndex].innerHTML;
this.hideRetryButton();
......@@ -455,7 +454,7 @@ export class TestPage implements OnDestroy, OnInit {
}
triggerInputEventHandler() {
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
if (iframe) {
const inputs: NodeList = iframe.contentWindow.document.querySelectorAll(this.h5pTextInputClassString);
inputs.forEach((input: HTMLInputElement) => {
......@@ -476,7 +475,7 @@ export class TestPage implements OnDestroy, OnInit {
}
triggerSolutionsEventHandler() {
const iframe: HTMLIFrameElement = document.querySelector(this.h5pIframeString);
const iframe: HTMLIFrameElement = document.querySelector(this.exerciseService.h5pIframeString);
if (iframe) {
if (this.vocService.currentTestResults[this.currentExerciseIndex]) {
const oldActivity: Activity = this.vocService.currentTestResults[this.currentExerciseIndex].statement.object as Activity;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment