Skip to content
Snippets Groups Projects
statistics.component.ts 3.37 KiB
Newer Older
import { Component, Input, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { RoomService } from '../../../services/http/room.service';
import { Content } from '../../../models/content';
import { ContentService } from '../../../services/http/content.service';
import { ContentAnswerService } from '../../../services/http/content-answer.service';
import { AnswerText } from '../../../models/answer-text';
import { AnswerChoice } from '../../../models/answer-choice';
import { ContentType } from '../../../models/content-type.enum';
@Component({
  selector: 'app-statistics',
  templateUrl: './statistics.component.html',
  styleUrls: ['./statistics.component.scss']
export class StatisticsComponent implements OnInit {
  @Input() content: Content[];
  @Input() textAnswers: AnswerText[] = [];
  @Input() choiceAnswers: AnswerChoice[] = [];
  statistics: any = null;
Hagen Dreßler's avatar
Hagen Dreßler committed
  selectedContent: any =
    { name: 'HOW TO MAKE CONTENT GREAT AGAIN', index: '1', length: '1' };
    { name: 'Skill', percent: 10, correct: false, answers: 1, },
    { name: 'Knowledge', percent: 10, correct: false, answers: 1, },
    { name: '???', percent: 30, correct: true, answers: 3, },
    { name: 'Not at all', percent: 50, correct: true, answers: 5, }
    { value: '1', viewValue: 'Text answers' },
    { value: '2', viewValue: 'Choice answers' }
  selected: number = null;
  constructor(
    private route: ActivatedRoute,
    private roomService: RoomService,
    private contentService: ContentService,
    private contentAnswerService: ContentAnswerService ) { }
  ngOnInit(): void {
    this.route.params.subscribe(params => {
      this.getContent(params['roomId']);
    });
  }

  getContent(roomId: string): void {
    this.contentService.getContents(roomId).subscribe(content => {
      this.content = content;
      this.getAnswers();
    });
  }

  getAnswers(): void {
Tom Käsler's avatar
Tom Käsler committed
    for (const c of this.content) {
      this.contentAnswerService.getAnswers(c.id).subscribe( answer => {
        [].push.apply(this.textAnswers, answer);
      });
Tom Käsler's avatar
Tom Käsler committed
      this.contentAnswerService.getAnswers(c.id).subscribe( answer => {
        [].push.apply(this.choiceAnswers, answer);
Hagen Dreßler's avatar
Hagen Dreßler committed
  showStatistic(value) {  // refactor answer class structure for less code and more abstraction
    this.statistics = [];
Tom Käsler's avatar
Tom Käsler committed
    for (const c of this.content) {
Tom Käsler's avatar
Tom Käsler committed
        if (c.format === ContentType.TEXT) {
          const count = this.countTextAnswers(c.id);
Tom Käsler's avatar
Tom Käsler committed
            name: c.subject, answers: count, percent: count * 100 / this.textAnswers.length,
Tom Käsler's avatar
Tom Käsler committed
        if (c.format === ContentType.CHOICE) {
          const count = this.countChoiceAnswers(c.id);
Tom Käsler's avatar
Tom Käsler committed
            name: c.subject, answers: count, percent: count * 100 / this.choiceAnswers.length,
    this.selected = value;
  }
  countTextAnswers(contentId: string): number {
    return this.textAnswers.filter(answer => answer.contentId === contentId).length;
  }

  countChoiceAnswers(contentId: string): number {
    return this.choiceAnswers.filter(answer => answer.contentId === contentId).length;

  showEvaluation(index: number) {
Hagen Dreßler's avatar
Hagen Dreßler committed
    // coming with api connection, logic doesnt make sense without knowledge about api