import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import 'rxjs/add/operator/map'; @Injectable() export class ResultService { private getResultUrl = '/get-subjects'; private getChartUrl = '/analyze-subject'; private getTextAnswerUrl = '/get-text-answers'; private evaluationId: string; private subjectId: string; resultList = []; textAnswerList = []; http: Http; constructor(http: Http) { this.http = http; } public getResultList(){ return this.resultList; } getTextAnswerList(){ return this.textAnswerList; } updateEvalId(evaluationID){ this.evaluationId = evaluationID; } getEvalId(){ return this.evaluationId; } updateSubjId(subjectId){ this.subjectId = subjectId; } getSubjId(){ return this.subjectId; } getResult(data, callback, errHandler){ this.http.get(this.getResultUrl, data) .map( (response : Response) =>{ return response; } ) .subscribe( (data) => { //this.resultList = data.json().obj; let result = data.json().obj; console.log("Not Sorted: ", result); this.resultList = result.sort((n1,n2) => { if ((n1.name + " " + n1.class) > (n2.name + " " + n2.name)) { return 1; } if ((n1.name + " " + n1.class) < (n2.name + " " + n2.name)) { return -1; } return 0; }) console.log("Sorted: ", this.resultList); callback(); }, (data) =>{ errHandler(); } ); } getChartData(callback){ const data = { evaluationId: this.evaluationId, subjectId: this.subjectId }; this.http.post(this.getChartUrl, data) .map( (response : Response) =>{ return response; } ) .subscribe( (data) => { callback(data); }, (data) =>{ callback(); } ); } getTextAnswers(resultName, callback, errHandler){ const data = { className: resultName, evaluationId: this.evaluationId } console.log("Data: ",data); this.http.post(this.getTextAnswerUrl, data) .map( (response : Response) =>{ return response; } ) .subscribe( (data) => { this.textAnswerList = data.json().obj; callback(); }, (data) =>{ errHandler(); } ); } }