public GroupOfTypeResult GetInGroupOfType(int personId, Guid groupTypeId)
        {
            GroupOfTypeResult result = new GroupOfTypeResult();
            result.PersonId = personId;
            result.PersonInGroup = false;
            result.GroupList = new List<GroupSummary>();

            // get person info
            Person person = new PersonService( (Rock.Data.RockContext)Service.Context ).Get( personId );

            if (person != null)
            {
                result.NickName = person.NickName;
                result.LastName = person.LastName;
            }

            // get group type info
            GroupType groupType = new GroupTypeService( (Rock.Data.RockContext)Service.Context ).Get( groupTypeId );

            if (groupType != null)
            {
                result.GroupTypeName = groupType.Name;
                result.GroupTypeIconCss = groupType.IconCssClass;
                result.GroupTypeId = groupType.Id;
            }

            // determine if person is in this type of group
            GroupMemberService groupMemberService = new GroupMemberService( (Rock.Data.RockContext)Service.Context );
            IQueryable<GroupMember> groupMembershipsQuery = groupMemberService.Queryable("Person,GroupRole,Group")
                                        .Where(t => t.Group.GroupType.Guid == groupTypeId && t.PersonId == personId )
                                        .OrderBy(g => g.GroupRole.Order);

            foreach (GroupMember member in groupMembershipsQuery)
            {
                result.PersonInGroup = true;
                GroupSummary group = new GroupSummary();
                group.GroupName = member.Group.Name;
                group.GroupId = member.Group.Id;
                group.RoleName = member.GroupRole.Name;

                result.GroupList.Add(group);
            }

            return result;
        }
Example #2
0
        public GroupWithPurposeResult GetGroupWithPurpose( int personId, Guid definedValueGuid )
        {
            GroupWithPurposeResult result = new GroupWithPurposeResult();
            result.PersonId = personId;
            result.PersonInGroup = false;
            result.GroupList = new List<GroupSummary>();

            // get person info
            Person person = new PersonService( ( Rock.Data.RockContext ) Service.Context ).Get( personId );

            if ( person != null )
            {
                result.NickName = person.NickName;
                result.LastName = person.LastName;
            }

            var purposeValue = DefinedValueCache.Read( definedValueGuid );
            result.Purpose = purposeValue.Value;

            // determine if person is in a group with this purpose
            GroupMemberService groupMemberService = new GroupMemberService( ( Rock.Data.RockContext ) Service.Context );

            IQueryable<GroupMember> groupMembershipsQuery = groupMemberService.Queryable( "Person,GroupRole,Group" )
                                        .Where( t => t.Group.GroupType.GroupTypePurposeValueId == purposeValue.Id
                                                 && t.PersonId == personId
                                                 && t.GroupMemberStatus == GroupMemberStatus.Active
                                                 && t.Group.IsActive )
                                        .OrderBy( g => g.GroupRole.Order );

            foreach ( GroupMember member in groupMembershipsQuery )
            {
                result.PersonInGroup = true;
                GroupSummary group = new GroupSummary();
                group.GroupName = member.Group.Name;
                group.GroupId = member.Group.Id;
                group.RoleName = member.GroupRole.Name;

                result.GroupList.Add( group );
            }

            return result;
        }