/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The workflow action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public override bool Execute(RockContext rockContext, Rock.Model.WorkflowAction action, Object entity, out List <string> errorMessages) { var checkInState = GetCheckInState(entity, out errorMessages); if (checkInState == null) { return(false); } var groupIdAttributeKey = string.Empty; var groupIdAttributeGuid = GetAttributeValue(action, "GroupMembershipGroupAttribute").AsGuid(); if (groupIdAttributeGuid != Guid.Empty) { groupIdAttributeKey = AttributeCache.Get(groupIdAttributeGuid).Key; } var family = checkInState.CheckIn.CurrentFamily; if (family != null) { GroupMemberService groupMemberService = new GroupMemberService(rockContext); var remove = GetAttributeValue(action, "Remove").AsBoolean(); foreach (var person in family.People) { if (person.Person.Attributes == null) { person.Person.LoadAttributes(rockContext); } foreach (var groupType in person.GroupTypes.ToList()) { foreach (var group in groupType.Groups.ToList()) { var groupGuid = group.Group.GetAttributeValue(groupIdAttributeKey).AsGuidOrNull(); if (groupGuid != null) { if (!groupMemberService.GetByGroupGuid(groupGuid ?? new Guid()) .Where(gm => gm.PersonId == person.Person.Id && (gm.GroupMemberStatus == GroupMemberStatus.Active || !checkInState.CheckInType.PreventInactivePeople)).Any()) { if (remove) { groupType.Groups.Remove(group); } else { group.ExcludedByFilter = true; } } } } } } } return(true); }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The workflow action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public override bool Execute(RockContext rockContext, Rock.Model.WorkflowAction action, Object entity, out List <string> errorMessages) { var checkInState = GetCheckInState(entity, out errorMessages); if (checkInState == null) { errorMessages.Add($"Attempted to run {this.GetType().GetFriendlyTypeName()} in check-in, but the check-in state was null."); return(false); } var groupIdAttributeKey = string.Empty; var groupIdAttributeGuid = GetAttributeValue(action, "GroupMembershipGroupAttribute").AsGuid(); if (groupIdAttributeGuid != Guid.Empty) { groupIdAttributeKey = AttributeCache.Get(groupIdAttributeGuid).Key; } var family = checkInState.CheckIn.CurrentFamily; if (family != null) { GroupMemberService groupMemberService = new GroupMemberService(rockContext); var remove = GetAttributeValue(action, "Remove").AsBoolean(); foreach (var person in family.People) { if (person.Person.Attributes == null) { person.Person.LoadAttributes(rockContext); } foreach (var groupType in person.GroupTypes.ToList()) { foreach (var group in groupType.Groups.ToList()) { var groupGuid = group.Group.GetAttributeValue(groupIdAttributeKey).AsGuidOrNull(); if (groupGuid.HasValue) { bool allowInactive = false; if (checkInState.CheckInType != null) { allowInactive = !checkInState.CheckInType.PreventInactivePeople; } var groupmembers = groupMemberService.GetByGroupGuid(groupGuid.Value) .Where(gm => gm.PersonId == person.Person.Id && (gm.GroupMemberStatus == GroupMemberStatus.Active || allowInactive)); var state = GetAttributeValue(action, "CheckRequirements"); switch (state) { case "0": break; case "1": groupmembers = groupmembers.Where( gm => !gm.GroupMemberRequirements.Where( r => r.RequirementFailDateTime != null) .Any() ); break; case "2": groupmembers = groupmembers.Where( gm => !gm.GroupMemberRequirements.Where( r => r.RequirementFailDateTime != null || r.RequirementWarningDateTime != null) .Any()); break; } if (!groupmembers.Any()) { if (remove) { groupType.Groups.Remove(group); } else { group.ExcludedByFilter = true; } } } } } } } return(true); }
/// <summary> /// Executes the specified workflow. /// </summary> /// <param name="rockContext">The rock context.</param> /// <param name="action">The workflow action.</param> /// <param name="entity">The entity.</param> /// <param name="errorMessages">The error messages.</param> /// <returns></returns> /// <exception cref="System.NotImplementedException"></exception> public override bool Execute(RockContext rockContext, Rock.Model.WorkflowAction action, Object entity, out List <string> errorMessages) { var checkInState = GetCheckInState(entity, out errorMessages); if (checkInState == null) { errorMessages.Add($"Attempted to run {this.GetType().GetFriendlyTypeName()} in check-in, but the check-in state was null."); return(false); } var groupAttributeKey = AttributeCache.Get(Constants.GROUP_ATTRIBUTE_MEMBERSHIP_GROUP).Key; var checkRequirementsKey = AttributeCache.Get(Constants.GROUP_ATTRIBUTE_CHECK_REQUIREMENTS).Key; var memberRoleAttributeKey = AttributeCache.Get(Constants.GROUP_ATTRIBUTE_MEMBER_ROLE).Key; bool allowInactive = false; if (checkInState.CheckInType != null) { allowInactive = !checkInState.CheckInType.PreventInactivePeople; } var family = checkInState.CheckIn.CurrentFamily; if (family != null) { GroupService groupService = new GroupService(rockContext); GroupMemberService groupMemberService = new GroupMemberService(rockContext); GroupTypeRoleService groupTypeRoleService = new GroupTypeRoleService(rockContext); var remove = GetAttributeValue(action, "Remove").AsBoolean(); foreach (var person in family.People) { if (person.Person.Attributes == null) { person.Person.LoadAttributes(rockContext); } foreach (var groupType in person.GroupTypes.ToList()) { var groupsToCheck = groupType.Groups .Where(g => g.Group.AttributeValues.ContainsKey(groupAttributeKey)) .Select(g => g.Group.AttributeValues[groupAttributeKey].Value) .Where(s => s.IsNotNullOrWhiteSpace()) .Select(s => s.AsGuid()); if (!groupsToCheck.Any()) { //There are no groups to search here. Don't remove any and move on continue; } var groupMemberQry = groupMemberService.Queryable().AsNoTracking() .Where(gm => gm.PersonId == person.Person.Id); if (!allowInactive) { groupMemberQry = groupMemberQry.Where(gm => gm.GroupMemberStatus == GroupMemberStatus.Active); } //Check all the groups at once. This turns many little requests into one medium request. var qry = groupService.Queryable().AsNoTracking() .Where(g => groupsToCheck.Contains(g.Guid)) .Join(groupMemberQry, g => g.Id, gm => gm.GroupId, (g, gm) => new { g.Guid, gm.GroupRole.IsLeader }); var validGroups = qry.ToList(); foreach (var group in groupType.Groups.ToList()) { var groupGuid = group.Group.GetAttributeValue(groupAttributeKey).AsGuid(); if (groupGuid == Guid.Empty) { //There is no group to check... do not remove check-in group continue; } var cannotCheckin = false; var role = group.Group.GetAttributeValue(memberRoleAttributeKey); switch (role) { case "0": // Any if (!validGroups.Where(g => g.Guid == groupGuid).Any()) { cannotCheckin = true; } break; case "1": //Leaders Only if (!validGroups.Where(g => g.Guid == groupGuid && g.IsLeader).Any()) { cannotCheckin = true; } break; case "2": //Non Leaders if (!validGroups.Where(g => g.Guid == groupGuid && !g.IsLeader).Any()) { cannotCheckin = true; } break; } if (!cannotCheckin) { //Check the group requirements to see if this person passes var requirement = group.Group.GetAttributeValue(checkRequirementsKey); switch (requirement) { case "0": //No requirement break; case "1": //Required Only cannotCheckin = !groupMemberService.GetByGroupGuid(groupGuid) .Where(gm => gm.PersonId == person.Person.Id && (gm.GroupMemberStatus == GroupMemberStatus.Active || allowInactive)) .Where(gm => !gm.GroupMemberRequirements.Where(r => r.RequirementFailDateTime != null) .Any()) .Any(); break; case "2": //Required And Warning cannotCheckin = !groupMemberService.GetByGroupGuid(groupGuid) .Where(gm => gm.PersonId == person.Person.Id && (gm.GroupMemberStatus == GroupMemberStatus.Active || allowInactive)) .Where(gm => !gm.GroupMemberRequirements.Where(r => r.RequirementFailDateTime != null || r.RequirementWarningDateTime != null) .Any()) .Any(); break; } } if (cannotCheckin) { if (remove) { groupType.Groups.Remove(group); } else { group.ExcludedByFilter = true; } } } } } } return(true); }