All files / src/app/workspace-admin/results results.component.ts

31.37% Statements 16/51
0% Branches 0/11
30% Functions 6/20
31.37% Lines 16/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145                                    1x 1x       1x 1x       1x     1x 1x 1x 1x       1x 1x   1x           1x             1x 1x 1x                                                                                                                                                                                
import {
  Component, OnDestroy, OnInit, ViewChild
} from '@angular/core';
import { SelectionModel } from '@angular/cdk/collections';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatSort } from '@angular/material/sort';
import { MatTableDataSource } from '@angular/material/table';
import { Subscription } from 'rxjs';
import { ConfirmDialogComponent, ConfirmDialogData } from '../../shared/shared.module';
import { BackendService } from '../backend.service';
import { WorkspaceDataService } from '../workspacedata.service';
import { ReportType, ResultData } from '../workspace.interfaces';
 
@Component({
  templateUrl: './results.component.html',
  styleUrls: ['./results.component.css']
})
export class ResultsComponent implements OnInit, OnDestroy {
  displayedColumns: string[] = [
    'selectCheckbox', 'groupName', 'bookletsStarted', 'numUnitsMin', 'numUnitsMax', 'numUnitsAvg', 'lastChange'
  ];
 
  resultDataSource: MatTableDataSource<ResultData> | null = new MatTableDataSource<ResultData>([]);
  tableSelectionCheckbox = new SelectionModel<ResultData>(true, []);
 
  @ViewChild(MatSort, { static: true }) sort!: MatSort;
 
  private wsIdSubscription: Subscription | null = null;
 
  constructor(
    private backendService: BackendService,
    private deleteConfirmDialog: MatDialog,
    public workspaceDataService: WorkspaceDataService,
    public snackBar: MatSnackBar
  ) { }
 
  ngOnInit(): void {
    setTimeout(() => {
      this.wsIdSubscription = this.workspaceDataService.workspaceId$
        .subscribe(() => {
          this.updateTable();
        });
    });
  }
 
  ngOnDestroy(): void {
    Iif (this.wsIdSubscription) {
      this.wsIdSubscription.unsubscribe();
      this.wsIdSubscription = null;
    }
  }
 
  updateTable(): void {
    this.tableSelectionCheckbox.clear();
    this.resultDataSource = null;
    this.backendService.getResults(this.workspaceDataService.workspaceId)
      .subscribe((resultData: ResultData[]) => {
        this.resultDataSource = new MatTableDataSource<ResultData>(resultData);
        this.resultDataSource.sort = this.sort;
      });
  }
 
  isAllSelected(): boolean {
    const numSelected = this.tableSelectionCheckbox.selected.length;
    const numRows = this.resultDataSource?.data.length || 0;
    return numSelected === numRows;
  }
 
  masterToggle(): void {
    this.isAllSelected() ?
      this.tableSelectionCheckbox.clear() :
      this.resultDataSource?.data.forEach(row => this.tableSelectionCheckbox.select(row));
  }
 
  downloadResponsesCSV(): void {
    this.downloadCSVReport(ReportType.RESPONSE, 'iqb-testcenter-responses.csv');
  }
 
  downloadReviewsCSV(): void {
    this.downloadCSVReport(ReportType.REVIEW, 'iqb-testcenter-reviews.csv');
  }
 
  downloadNewReviewsCSV(): void {
    this.downloadCSVReport(ReportType.REVIEW, 'iqb-testcenter-reviews.csv', true);
  }
 
  downloadLogsCSV(): void {
    this.downloadCSVReport(ReportType.LOG, 'iqb-testcenter-logs.csv');
  }
 
  downloadCSVReport(reportType: ReportType, filename: string, useNewVersion: boolean = false): void {
    Iif (this.tableSelectionCheckbox.selected.length > 0) {
      const dataIds: string[] = [];
 
      this.tableSelectionCheckbox.selected.forEach(element => {
        dataIds.push(element.groupName);
      });
 
      this.workspaceDataService.downloadReport(dataIds, reportType, filename, useNewVersion);
 
      this.tableSelectionCheckbox.clear();
    }
  }
 
  deleteData(): void {
    Iif (this.tableSelectionCheckbox.selected.length > 0) {
      const selectedGroups: string[] = [];
      this.tableSelectionCheckbox.selected.forEach(element => {
        selectedGroups.push(element.groupName);
      });
 
      let prompt = 'Es werden alle Antwort- und Logdaten in der Datenbank für diese ';
      if (selectedGroups.length > 1) {
        prompt += `${selectedGroups.length} Gruppen `;
      } else {
        prompt += `Gruppe "${selectedGroups[0]}" `;
      }
 
      const dialogRef = this.deleteConfirmDialog.open(ConfirmDialogComponent, {
        width: '400px',
        data: <ConfirmDialogData>{
          title: 'Löschen von Gruppendaten',
          content: `${prompt}gelöscht. Fortsetzen?`,
          confirmbuttonlabel: 'Gruppendaten löschen',
          showcancel: true
        }
      });
 
      dialogRef.afterClosed()
        .subscribe(result => {
          Iif (result === false) {
            return;
          }
          this.backendService.deleteResponses(this.workspaceDataService.workspaceId, selectedGroups)
            .subscribe(() => {
              this.snackBar.open('Löschen erfolgreich.', 'OK', { duration: 5000 });
              this.tableSelectionCheckbox.clear();
              this.updateTable();
            });
        });
    }
  }
}