import { Component , Input , OnChanges , SimpleChanges } from "@angular/core" ; import { OnInitDynamicComponent } from "../../../core/view/dynamic-components/on-init-dynamic-component.interface" ; import { RecurringActivity } from "../model/recurring-activity" ; import { AttendanceDetailsComponent } from "../attendance-details/attendance-details.component" ; import { AttendanceService } from "../attendance.service" ; import { PercentPipe } from "@angular/common" ; import { ActivityAttendance } from "../model/activity-attendance" ; import { Note } from "../../notes/model/note" ; import moment from "moment" ; import { FormFieldConfig } from "../../../core/entity-components/entity-form/entity-form/FormConfig" ; import { FormDialogService } from "../../../core/form-dialog/form-dialog.service" ; @ Component ( { selector : "app-activity-attendance-section" , templateUrl : "./activity-attendance-section.component.html" , styleUrls : [ "./activity-attendance-section.component.scss" ] , } ) export class ActivityAttendanceSectionComponent implements OnChanges , OnInitDynamicComponent { @ Input ( ) activity : RecurringActivity ; @ Input ( ) forChild ? : string ; records : ActivityAttendance [ ] = [ ] ; allRecords : ActivityAttendance [ ] = [ ] ; displayedEvents : Note [ ] = [ ] ; columns : FormFieldConfig [ ] = [ { id : "periodFrom" , label : $localize `:The month something took place:Month` , view : "DisplayDate" , additional : "YYYY-MM" , } , { id : "presentEvents" , label : $localize `:How many children are present at a meeting:Present` , view : "ReadonlyFunction" , additional : ( e ) => this . forChild ? e . countEventsPresent ( this . forChild ) : e . countEventsPresentAverage ( true ) , } , { id : "totalEvents" , label : $localize `:Events of an attendance:Events` , view : "ReadonlyFunction" , additional : ( e ) => e . countEventsTotal ( ) , } , { id : "attendancePercentage" , label : $localize `:Percentage of people that attended an event:Attended` , view : "ReadonlyFunction" , additional : ( e ) => this . percentPipe . transform ( this . forChild ? e . getAttendancePercentage ( this . forChild ) : e . getAttendancePercentageAverage ( ) , "1.0-0" ) , } , ] ; constructor ( private attendanceService , private percentPipe , private formDialog ) { } async ngOnChanges ( changes ) { if ( changes . hasOwnProperty ( "activity" ) || changes . hasOwnProperty ( "forChild" ) ) { await this . init ( ) ; } } async onInitFromDynamicConfig ( config ) { this . activity = config . entity as RecurringActivity ; await this . init ( ) ; } async init ( loadAll = false ) { if ( loadAll ) { this . allRecords = await this . attendanceService . getActivityAttendances ( this . activity ) ; } else { this . allRecords = await this . attendanceService . getActivityAttendances ( this . activity , moment ( ) . startOf ( "month" ) . subtract ( 6 , "months" ) . toDate ( ) ) ; } this . updateDisplayedRecords ( false ) ; } updateDisplayedRecords ( includeRecordsWithoutParticipation ) { if ( includeRecordsWithoutParticipation || ! this . forChild ) { this . records = this . allRecords ; } else { this . records = this . allRecords . filter ( ( r ) => r . countEventsAbsent ( this . forChild ) + r . countEventsPresent ( this . forChild ) > 0 ) ; } if ( this . records ?. length > 0 ) { this . records . sort ( ( a , b ) => b . periodFrom . getTime ( ) - a . periodFrom . getTime ( ) ) ; this . displayedEvents = this . records [ 0 ] . events ; } } showDetails ( activity ) { this . formDialog . openDialog ( AttendanceDetailsComponent , activity , { forChild : this . forChild , } ) ; } }