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

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

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

    ngOnInit() {
    }

}