From 3352ff8b3a15bb6df95d73a7a1014b0a9b3cede3 Mon Sep 17 00:00:00 2001 From: croske Date: Thu, 20 Apr 2023 13:30:58 -0700 Subject: [PATCH] + (Group) Fixed issue with Group Requirement applying age classification. (Fixes #5393) --- .../GroupRequirement.Logic.cs | 74 ++++++++++++++++--- 1 file changed, 62 insertions(+), 12 deletions(-) diff --git a/Rock/Model/Group/GroupRequirement/GroupRequirement.Logic.cs b/Rock/Model/Group/GroupRequirement/GroupRequirement.Logic.cs index 884c5b3b7de..bb01be994cd 100644 --- a/Rock/Model/Group/GroupRequirement/GroupRequirement.Logic.cs +++ b/Rock/Model/Group/GroupRequirement/GroupRequirement.Logic.cs @@ -80,7 +80,7 @@ public IEnumerable PersonQueryableMeetsGroupRequir var result = personQry.Select( p => p.Id ).ToList().Select( a => new PersonGroupRequirementStatus { - PersonId = Id, + PersonId = a, GroupRequirement = this, MeetsGroupRequirement = MeetsGroupRequirement.NotApplicable } ); @@ -88,9 +88,28 @@ public IEnumerable PersonQueryableMeetsGroupRequir return result; } + // Create a list of statuses so that not applicable status results can be included. + List statusResults = new List(); + + // If this GroupRequirement is not for 'All' age classifications, then calculate those statuses. if ( this.AppliesToAgeClassification != AppliesToAgeClassification.All ) { - // If the requirement's Applies To Age Classification is not "All", filter the person query to the corresponding Age Classification. + // If the person's age classification we are comparing is not the one applied to this requirement, + // then mark that person's requirement status as 'Not Applicable'. + var notApplicablePersonQry = personQry.Where( p => ( int ) p.AgeClassification != ( int ) this.AppliesToAgeClassification ); + var results = notApplicablePersonQry.Select( p => p.Id ).ToList().Select( a => + new PersonGroupRequirementStatus + { + PersonId = a, + GroupRequirement = this, + MeetsGroupRequirement = MeetsGroupRequirement.NotApplicable + } ); + + if ( results != null ) + { + statusResults.AddRange( results ); + } + personQry = personQry.Where( p => ( int ) p.AgeClassification == ( int ) this.AppliesToAgeClassification ); } @@ -102,20 +121,26 @@ public IEnumerable PersonQueryableMeetsGroupRequir var appliesToDataViewWhereExpression = this.AppliesToDataView.GetExpression( appliesToDataViewPersonService, appliesToDataViewParamExpression ); var appliesToDataViewPersonIds = appliesToDataViewPersonService.Get( appliesToDataViewParamExpression, appliesToDataViewWhereExpression ).Select( p => p.Id ); - // If the dataview does not contain anyone in the person query, return a "not applicable" status. - if ( !personQry.Where( p => appliesToDataViewPersonIds.Contains( p.Id ) ).Any() ) + // If the dataview does not contain anyone in the person query, give the member a "not applicable" status. + var notApplicablePersonQry = personQry.Where( p => !appliesToDataViewPersonIds.Contains( p.Id ) ); { - var result = personQry.Select( p => p.Id ).ToList().Select( a => + var results = notApplicablePersonQry.Select( p => p.Id ).ToList().Select( a => new PersonGroupRequirementStatus { - PersonId = Id, + PersonId = a, GroupRequirement = this, MeetsGroupRequirement = MeetsGroupRequirement.NotApplicable } ); - return result; + if ( results != null ) + { + statusResults.AddRange( results ); + } + + personQry = personQry.Where( p => appliesToDataViewPersonIds.Contains( p.Id ) ); } } + var attributeValueService = new AttributeValueService( rockContext ); if ( this.GroupRequirementType.RequirementCheckType == RequirementCheckType.Dataview ) @@ -193,7 +218,12 @@ public IEnumerable PersonQueryableMeetsGroupRequir return personGroupRequirementStatus; } ); - return result; + if ( result != null ) + { + statusResults.AddRange( result ); + } + + return statusResults; } } else @@ -213,7 +243,12 @@ public IEnumerable PersonQueryableMeetsGroupRequir return personGroupRequirementStatus; } ); - return result; + if ( result != null ) + { + statusResults.AddRange( result ); + } + + return statusResults; } } else if ( this.GroupRequirementType.RequirementCheckType == RequirementCheckType.Sql ) @@ -271,7 +306,12 @@ public IEnumerable PersonQueryableMeetsGroupRequir return personGroupRequirementStatus; } ); - return result; + if ( result != null ) + { + statusResults.AddRange( result ); + } + + return statusResults; } } catch ( Exception ex ) @@ -287,7 +327,12 @@ public IEnumerable PersonQueryableMeetsGroupRequir CalculationException = ex } ); - return result; + if ( result != null ) + { + statusResults.AddRange( result ); + } + + return statusResults; } } else @@ -318,7 +363,12 @@ public IEnumerable PersonQueryableMeetsGroupRequir }; } ); - return result; + if ( result != null ) + { + statusResults.AddRange( result ); + } + + return statusResults; } // This shouldn't happen, since a requirement must have a dataview, SQL or manual origin.