/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] options = selection.Split( '|' ); if ( options.Length < 4 ) { return null; } var groupGuidList = options[0].Split( ',' ).AsGuidList(); ComparisonType comparisonType = options[1].ConvertToEnum<ComparisonType>( ComparisonType.GreaterThanOrEqualTo ); int? attended = options[2].AsIntegerOrNull(); string slidingDelimitedValues = options[3].Replace( ',', '|' ); var dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( slidingDelimitedValues ); bool includeChildGroups = options.Length >= 5 ? options[4].AsBooleanOrNull() ?? false : false; var groupService = new GroupService( new RockContext() ); var groups = groupService.GetByGuids( groupGuidList ); List<int> groupIds = new List<int>(); foreach ( var group in groups ) { groupIds.Add( group.Id ); if ( includeChildGroups ) { var childGroups = groupService.GetAllDescendents( group.Id ); if ( childGroups.Any() ) { groupIds.AddRange( childGroups.Select( a => a.Id ) ); // get rid of any duplicates groupIds = groupIds.Distinct().ToList(); } } } var rockContext = serviceInstance.Context as RockContext; var attendanceQry = new AttendanceService( rockContext ).Queryable().Where( a => a.DidAttend.HasValue && a.DidAttend.Value ); if ( dateRange.Start.HasValue ) { var startDate = dateRange.Start.Value; attendanceQry = attendanceQry.Where( a => a.StartDateTime >= startDate ); } if ( dateRange.End.HasValue ) { var endDate = dateRange.End.Value; attendanceQry = attendanceQry.Where( a => a.StartDateTime < endDate ); } if ( groupIds.Count == 1 ) { int groupId = groupIds[0]; attendanceQry = attendanceQry.Where( a => a.GroupId.HasValue && a.GroupId.Value == groupId ); } else if ( groupIds.Count > 1 ) { attendanceQry = attendanceQry.Where( a => a.GroupId.HasValue && groupIds.Contains( a.GroupId.Value ) ); } else { // no groups selected, so return nothing return Expression.Constant( false ); } var qry = new PersonService( rockContext ).Queryable() .Where( p => attendanceQry.Where( xx => xx.PersonAlias.PersonId == p.Id ).Count() == attended ); BinaryExpression compareEqualExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ) as BinaryExpression; BinaryExpression result = FilterExpressionExtractor.AlterComparisonType( comparisonType, compareEqualExpression, null ); return result; }
/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] selectionValues = selection.Split( '|' ); if ( selectionValues.Length >= 2 ) { GroupMemberService groupMemberService = new GroupMemberService( (RockContext)serviceInstance.Context ); List<Guid> groupGuids = selectionValues[0].Split( ',' ).AsGuidList(); var groupService = new GroupService( (RockContext)serviceInstance.Context ); var groupIds = groupService.GetByGuids( groupGuids ).Select( a => a.Id ).Distinct().ToList(); bool includeChildGroups = false; bool includeChildGroupsIncludeSelected = false; bool includeChildGroupsPlusDescendants = false; bool includeInactiveGroups = false; if ( selectionValues.Length >= 3 ) { includeChildGroups = selectionValues[2].AsBooleanOrNull() ?? false; } if ( selectionValues.Length >= 6 ) { includeChildGroupsIncludeSelected = selectionValues[4].AsBooleanOrNull() ?? false; includeChildGroupsPlusDescendants = selectionValues[5].AsBooleanOrNull() ?? false; } else if ( includeChildGroups ) { // in case the selection was saved before these options where added includeChildGroupsIncludeSelected = true; includeChildGroupsPlusDescendants = true; } if ( selectionValues.Length >= 7 ) { includeInactiveGroups = selectionValues[6].AsBooleanOrNull() ?? true; ; } else { // if options where saved before this option was added, set to false, even though it would have included inactive before includeInactiveGroups = false; } GroupMemberStatus? groupMemberStatus = null; if ( selectionValues.Length >= 4 ) { groupMemberStatus = selectionValues[3].ConvertToEnumOrNull<GroupMemberStatus>(); } var groupMemberServiceQry = groupMemberService.Queryable(); if ( includeChildGroups ) { List<int> childGroupIds = new List<int>(); foreach ( var groupId in groupIds ) { if ( includeChildGroupsPlusDescendants ) { // get all children and descendants of the selected group(s) var descendants = groupService.GetAllDescendents( groupId ); if ( !includeInactiveGroups ) { descendants = descendants.Where( a => a.IsActive == true ); } childGroupIds.AddRange( descendants.Select( a => a.Id ).Distinct().ToList() ); } else { // get only immediate children of the selected group(s) var childGroups = groupService.Queryable().Where( a => a.ParentGroupId == groupId ); if ( !includeInactiveGroups ) { childGroups = childGroups.Where( a => a.IsActive == true ); } childGroupIds.AddRange( childGroups.Select( a => a.Id ) ); } } if ( includeChildGroupsIncludeSelected ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) || childGroupIds.Contains( xx.GroupId ) ); } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => childGroupIds.Contains( xx.GroupId ) ); } } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) ); } if ( groupMemberStatus.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.GroupMemberStatus == groupMemberStatus.Value ); } var groupRoleGuids = selectionValues[1].Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( n => n.AsGuid() ).ToList(); if ( groupRoleGuids.Count() > 0 ) { var groupRoleIds = new GroupTypeRoleService( (RockContext)serviceInstance.Context ).Queryable().Where( a => groupRoleGuids.Contains( a.Guid ) ).Select( a => a.Id ).ToList(); groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupRoleIds.Contains( xx.GroupRoleId ) ); } if ( selectionValues.Length >= 8 ) { string slidingDelimitedValues = selectionValues[7].Replace( ',', '|' ); DateRange dateRange = SlidingDateRangePicker.CalculateDateRangeFromDelimitedValues( slidingDelimitedValues ); if ( dateRange.Start.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.DateTimeAdded >= dateRange.Start.Value ); } if ( dateRange.End.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.DateTimeAdded < dateRange.End.Value ); } } var qry = new PersonService( (RockContext)serviceInstance.Context ).Queryable() .Where( p => groupMemberServiceQry.Any( xx => xx.PersonId == p.Id ) ); Expression extractedFilterExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ); return extractedFilterExpression; } return null; }
/// <summary> /// Gets the expression. /// </summary> /// <param name="entityType">Type of the entity.</param> /// <param name="serviceInstance">The service instance.</param> /// <param name="parameterExpression">The parameter expression.</param> /// <param name="selection">The selection.</param> /// <returns></returns> public override Expression GetExpression( Type entityType, IService serviceInstance, ParameterExpression parameterExpression, string selection ) { string[] selectionValues = selection.Split( '|' ); if ( selectionValues.Length >= 2 ) { GroupMemberService groupMemberService = new GroupMemberService( (RockContext)serviceInstance.Context ); List<Guid> groupGuids = selectionValues[0].Split( ',' ).AsGuidList(); var groupService = new GroupService( (RockContext)serviceInstance.Context ); var groupIds = groupService.GetByGuids( groupGuids ).Select( a => a.Id ).Distinct().ToList(); bool includeChildGroups = false; bool includeChildGroupsIncludeSelected = false; bool includeChildGroupsPlusDescendants = false; if ( selectionValues.Length >= 3 ) { includeChildGroups = selectionValues[2].AsBooleanOrNull() ?? false; } if ( selectionValues.Length >= 6 ) { includeChildGroupsIncludeSelected = selectionValues[4].AsBooleanOrNull() ?? false; includeChildGroupsPlusDescendants = selectionValues[5].AsBooleanOrNull() ?? false; } else if ( includeChildGroups ) { // in case the selection was saved before these options where added includeChildGroupsIncludeSelected = true; includeChildGroupsPlusDescendants = true; } GroupMemberStatus? groupMemberStatus = null; if ( selectionValues.Length >= 4 ) { groupMemberStatus = selectionValues[3].ConvertToEnumOrNull<GroupMemberStatus>(); } var groupMemberServiceQry = groupMemberService.Queryable(); if ( includeChildGroups ) { List<int> childGroupIds = new List<int>(); foreach ( var groupId in groupIds ) { if ( includeChildGroupsPlusDescendants ) { // get all children and descendants of the selected group(s) childGroupIds.AddRange( groupService.GetAllDescendents( groupId ).Select( a => a.Id ).Distinct().ToList() ); } else { // get only immediate children of the selected group(s) childGroupIds.AddRange( groupService.Queryable().Where( a => a.ParentGroupId == groupId ).Select( a => a.Id ) ); } } if ( includeChildGroupsIncludeSelected ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) || childGroupIds.Contains( xx.GroupId ) ); } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => childGroupIds.Contains( xx.GroupId ) ); } } else { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupIds.Contains( xx.GroupId ) ); } if ( groupMemberStatus.HasValue ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => xx.GroupMemberStatus == groupMemberStatus.Value ); } var groupRoleGuids = selectionValues[1].Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ).Select( n => n.AsGuid() ).ToList(); if ( groupRoleGuids.Count() > 0 ) { groupMemberServiceQry = groupMemberServiceQry.Where( xx => groupRoleGuids.Contains( xx.GroupRole.Guid ) ); } var qry = new PersonService( (RockContext)serviceInstance.Context ).Queryable() .Where( p => groupMemberServiceQry.Any( xx => xx.PersonId == p.Id ) ); Expression extractedFilterExpression = FilterExpressionExtractor.Extract<Rock.Model.Person>( qry, parameterExpression, "p" ); return extractedFilterExpression; } return null; }