exercise.page.ts 3.54 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 14 15 16 17 18 19 20 21 22 23

@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,
24
                public translateService: TranslateService,
25 26 27 28 29 30 31 32 33 34 35
                public exerciseService: ExerciseService,
                public http: HttpClient,
                public toastCtrl: ToastController,
                public helperService: HelperService,
                public corpusService: CorpusService) {
        this.corpusService.checkAnnisResponse().then(() => {
            this.loadExercise();
        });
    }

    loadExercise(): void {
36
        this.activatedRoute.queryParams.subscribe((params: object) => {
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            if (params['eid']) {
                let url: string = HelperService.config['backendBaseUrl'] + HelperService.config['backendApiExercisePath'];
                const httpParams: HttpParams = new HttpParams().set('eid', params['eid']);
                HelperService.makeGetRequest(this.http, this.toastCtrl, url, httpParams).then((ar: AnnisResponse) => {
                    HelperService.mostRecentSetup.annisResponse = ar;
                    this.helperService.saveMostRecentSetup().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 = `${HelperService.config['backendBaseUrl']}${HelperService.config['backendApiH5pPath']}` +
                        `?eid=${this.corpusService.annisResponse.exercise_id}&lang=${this.translateService.currentLang}`;
                    window.localStorage.setItem(HelperService.config['localStorageKeyH5P'], url);
                    const exerciseTypePath: string = this.corpusService.exercise.type === ExerciseType.markWords ?
                        'mark_words' : 'drag_text';
                    this.exerciseService.initH5P(exerciseTypePath);
                }, () => {
                });
            } 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(HelperService.config['localStorageKeyH5P'],
                    HelperService.baseUrl + '/assets/h5p/' + exerciseType + '/content/' + file + '_' + lang + '.json');
                this.exerciseService.initH5P(exerciseTypePath);
            }
65 66 67 68 69 70 71
        });
    }

    ngOnInit() {
    }

}