Rock.Model.GroupMemberService.GetByGroupIdAndPersonId C# (CSharp) Method

GetByGroupIdAndPersonId() public method

Returns a collection of Rock.Model.GroupMember">GroupMembers by the Id of the Rock.Model.Person.
public GetByGroupIdAndPersonId ( int groupId, int personId, bool includeDeceased = false ) : IQueryable
groupId int An representing the Id of the to search by.
personId int An representing the Id of the to search by.
includeDeceased bool A value indicating if deceased GroupMembers should be included. If true /// deceased group members will be included, if false deceased group members will not be included. This parameter defaults to false.
return IQueryable
        public IQueryable<GroupMember> GetByGroupIdAndPersonId( int groupId, int personId, bool includeDeceased = false )
        {
            return GetByGroupId(groupId, includeDeceased).Where( g => g.PersonId == personId );
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Validates the group membership.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="errorMessage">The error message.</param>
        /// <returns></returns>
        private bool ValidateGroupMembership(RockContext rockContext, out string errorMessage)
        {
            errorMessage = string.Empty;
            GroupMemberService groupMemberService = new GroupMemberService(rockContext);
            var groupRole = this.GroupRole ?? new GroupTypeRoleService(rockContext).Get(this.GroupRoleId);

            // check to see if the person is alread a member of the gorup/role
            var existingGroupMembership = groupMemberService.GetByGroupIdAndPersonId(this.GroupId, this.PersonId);

            if (existingGroupMembership.Any(a => a.GroupRoleId == this.GroupRoleId && a.Id != this.Id))
            {
                var person = this.Person ?? new PersonService(rockContext).Get(this.PersonId);

                errorMessage = string.Format(
                    "{0} already belongs to the {1} role for this {2}, and cannot be added again with the same role",
                    person,
                    groupRole.Name,
                    groupRole.GroupType.GroupTerm);

                return(false);
            }

            var databaseRecord = existingGroupMembership.FirstOrDefault(a => a.Id == this.Id);

            int memberCountInRole = new GroupMemberService(rockContext).Queryable()
                                    .Where(m =>
                                           m.GroupId == this.GroupId &&
                                           m.GroupRoleId == this.GroupRoleId &&
                                           m.GroupMemberStatus == GroupMemberStatus.Active)
                                    .Count();

            bool roleMembershipAboveMax = false;

            // if adding new active group member..
            if (this.Id.Equals(0) && this.GroupMemberStatus == GroupMemberStatus.Active)
            {
                // verify that active count has not exceeded the max
                if (groupRole.MaxCount != null && (memberCountInRole + 1) > groupRole.MaxCount)
                {
                    roleMembershipAboveMax = true;
                }
            }
            else if (this.Id > 0 && (this.GroupRoleId != databaseRecord.GroupRoleId || this.GroupMemberStatus != databaseRecord.GroupMemberStatus) &&
                     this.GroupMemberStatus == GroupMemberStatus.Active)
            {
                // if existing group member changing role or status..
                // verify that active count has not exceeded the max
                if (groupRole.MaxCount != null && (memberCountInRole + 1) > groupRole.MaxCount)
                {
                    roleMembershipAboveMax = true;
                }
            }

            // throw error if above max.. do not proceed
            if (roleMembershipAboveMax)
            {
                errorMessage = string.Format(
                    "The number of {0} for this {1} is at or above its maximum allowed limit of {2:N0} active {3}.",
                    groupRole.Name.Pluralize(),
                    groupRole.GroupType.GroupTerm,
                    groupRole.MaxCount,
                    groupRole.GroupType.GroupMemberTerm.Pluralize(groupRole.MaxCount == 1));
                return(false);
            }

            return(true);
        }
All Usage Examples Of Rock.Model.GroupMemberService::GetByGroupIdAndPersonId