exercise.page.ts 3.75 KB
Newer Older
1 2 3
/* tslint:disable:no-string-literal */
import {Component, OnInit} from '@angular/core';
import {HelperService} from '../helper.service';
4
import {NavController, ToastController} from '@ionic/angular';
5 6
import {ActivatedRoute} from '@angular/router';
import {TranslateService} from '@ngx-translate/core';
7
import {ExerciseService} from 'src/app/exercise.service';
8 9 10 11
import {HttpClient, HttpParams} from '@angular/common/http';
import {AnnisResponse} from 'src/app/models/annisResponse';
import {ExerciseType, MoodleExerciseType} from 'src/app/models/enum';
import {CorpusService} from 'src/app/corpus.service';
12 13
import {ApplicationState} from '../models/applicationState';
import {take} from 'rxjs/operators';
14
import configMC from '../../configMC';
15 16 17 18 19 20 21 22 23 24 25 26

@Component({
    selector: 'app-exercise',
    templateUrl: './exercise.page.html',
    styleUrls: ['./exercise.page.scss'],
})
export class ExercisePage implements OnInit {

    HelperService = HelperService;

    constructor(public navCtrl: NavController,
                public activatedRoute: ActivatedRoute,
27
                public translateService: TranslateService,
28 29 30 31 32 33 34 35 36 37 38
                public exerciseService: ExerciseService,
                public http: HttpClient,
                public toastCtrl: ToastController,
                public helperService: HelperService,
                public corpusService: CorpusService) {
        this.corpusService.checkAnnisResponse().then(() => {
            this.loadExercise();
        });
    }

    loadExercise(): void {
39
        this.activatedRoute.queryParams.subscribe((params: object) => {
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
            if (params['eid']) {
                let url: string = configMC.backendBaseUrl + configMC.backendApiExercisePath;
                const httpParams: HttpParams = new HttpParams().set('eid', params['eid']);
                HelperService.makeGetRequest(this.http, this.toastCtrl, url, httpParams).then((ar: AnnisResponse) => {
                    HelperService.applicationState.pipe(take(1)).subscribe((as: ApplicationState) => {
                        as.mostRecentSetup.annisResponse = ar;
                        this.helperService.saveApplicationState(as).then();
                        this.corpusService.annisResponse = ar;
                        const met: MoodleExerciseType = MoodleExerciseType[ar.exercise_type];
                        this.corpusService.exercise.type = ExerciseType[met.toString()];
                        // this will be called via GET request from the h5p standalone javascript library
                        url = `${configMC.backendBaseUrl}${configMC.backendApiH5pPath}` +
                            `?eid=${this.corpusService.annisResponse.exercise_id}&lang=${this.translateService.currentLang}`;
                        window.localStorage.setItem(configMC.localStorageKeyH5P, url);
                        const exerciseTypePath: string = this.corpusService.exercise.type === ExerciseType.markWords ?
                            'mark_words' : 'drag_text';
                        this.exerciseService.initH5P(exerciseTypePath);
57
                    });
58 59 60 61 62 63 64 65 66 67 68 69
                }, () => {
                });
            } else {
                const exerciseType: string = params['type'];
                const exerciseTypePath: string = exerciseType === this.exerciseService.vocListString ?
                    this.exerciseService.fillBlanksString : exerciseType;
                const file: string = params['file'];
                const lang: string = this.translateService.currentLang;
                window.localStorage.setItem(configMC.localStorageKeyH5P,
                    HelperService.baseUrl + '/assets/h5p/' + exerciseType + '/content/' + file + '_' + lang + '.json');
                this.exerciseService.initH5P(exerciseTypePath);
            }
70 71 72 73 74 75 76
        });
    }

    ngOnInit() {
    }

}