diff --git a/src/app/components/shared/list-statistic/list-statistic.component.ts b/src/app/components/shared/list-statistic/list-statistic.component.ts index 1f41c36a080a47e3c90767c4ddcd7d2307e19018..4d569abc98791f06e72a9223b01707a4a2f6ffa4 100644 --- a/src/app/components/shared/list-statistic/list-statistic.component.ts +++ b/src/app/components/shared/list-statistic/list-statistic.component.ts @@ -1,4 +1,3 @@ - import { Component, Input, OnInit } from '@angular/core'; import { ContentGroup } from '../../../models/content-group'; import { ContentService } from '../../../services/http/content.service'; @@ -6,6 +5,7 @@ import { Content } from '../../../models/content'; import { ContentType } from '../../../models/content-type.enum'; import { AnswerOption } from '../../../models/answer-option'; import { ContentChoice } from '../../../models/content-choice'; + export class ContentPercents { content: Content; percent: number; @@ -19,7 +19,9 @@ export class ContentPercents { templateUrl: './list-statistic.component.html', styleUrls: ['./list-statistic.component.scss'] }) + export class ListStatisticComponent implements OnInit { + @Input() contentGroup: ContentGroup; contents: Content[] = []; percents: number[] = []; @@ -29,33 +31,41 @@ export class ListStatisticComponent implements OnInit { totalP = 0; correctCounts = 0; totalCounts = 0; + contentCounter = 0; + constructor(private contentService: ContentService) { } + ngOnInit() { this.percents = [73, 87, 69, 92, 77]; this.contentService.getContentChoiceByIds(this.contentGroup.contentIds).subscribe(contents => { this.getContents(contents); }); } + getContents(contents: ContentChoice[]) { this.contents = contents; const length = contents.length; this.dataSource = new Array<ContentPercents>(length); for (let i = 0; i < length; i++) { - this.dataSource[i] = new ContentPercents(null, null ); + this.dataSource[i] = new ContentPercents(null, 0 ); this.dataSource[i].content = this.contents[i]; if (contents[i].format === ContentType.CHOICE) { this.contentService.getAnswer(contents[i].id).subscribe(answer => { - let percent = this.getCountCorrect(contents[i].options, answer.roundStatistics[0].independentCounts); + const percent = this.getCountCorrect(contents[i].options, answer.roundStatistics[0].independentCounts); this.dataSource[i].percent = percent; - this.totalP += percent; - this.total = this.totalP / length; + if (percent >= 0) { + console.log(percent); + this.totalP += percent; + this.total = this.totalP / this.contentCounter; + } }); } else { this.dataSource[i].percent = -1; } } } + getCountCorrect(options: AnswerOption[], indCounts: number[]): number { let correctIndex; this.correctCounts = 0; @@ -73,7 +83,12 @@ export class ListStatisticComponent implements OnInit { } this.totalCounts += indCounts[i]; } - res = ((this.correctCounts / this.totalCounts) * 100); + if (this.totalCounts) { + res = ((this.correctCounts / this.totalCounts) * 100); + this.contentCounter++; + } else { + res = -1; + } return res; } }