/// <summary>
        /// Shows the connectionOpportunity requirements statuses.
        /// </summary>
        private void ShowConnectionOpportunityRequirementsStatuses()
        {
            using ( var rockContext = new RockContext() )
            {
                int connectionRequestId = hfConnectionRequestId.Value.AsInteger();
                var connectionOpportunityId = hfConnectionOpportunityId.Value.AsInteger();

                IEnumerable<GroupRequirementStatus> requirementsResults = new List<PersonGroupRequirementStatus>();
                bool passedAllRequirements = true;

                var connectionRequest = new ConnectionRequestService( rockContext ).Get( connectionRequestId );
                if ( connectionRequest != null && connectionRequest.PersonAlias != null )
                {
                    var group = new GroupService( rockContext ).Get( connectionRequest.AssignedGroupId.Value );
                    if ( group != null )
                    {
                        requirementsResults = group.PersonMeetsGroupRequirements(
                            connectionRequest.PersonAlias.PersonId,
                            connectionRequest.AssignedGroupMemberRoleId );

                        if ( requirementsResults != null )
                        {
                            // Ignore notapplicable requirements
                            requirementsResults = requirementsResults.Where( r => r.MeetsGroupRequirement != MeetsGroupRequirement.NotApplicable ).ToList();
                        }

                        // Clear results
                        cblManualRequirements.Items.Clear();
                        lRequirementsLabels.Text = string.Empty;

                        rcwRequirements.Visible = requirementsResults.Any();

                        foreach ( var requirementResult in requirementsResults )
                        {
                            if ( requirementResult.GroupRequirement.GroupRequirementType.RequirementCheckType == RequirementCheckType.Manual )
                            {
                                var checkboxItem = new ListItem( requirementResult.GroupRequirement.GroupRequirementType.CheckboxLabel, requirementResult.GroupRequirement.Id.ToString() );
                                if ( string.IsNullOrEmpty( requirementResult.GroupRequirement.GroupRequirementType.CheckboxLabel ) )
                                {
                                    checkboxItem.Text = requirementResult.GroupRequirement.GroupRequirementType.Name;
                                }
                                checkboxItem.Selected = requirementResult.MeetsGroupRequirement == MeetsGroupRequirement.Meets;
                                cblManualRequirements.Items.Add( checkboxItem );
                            }
                            else
                            {
                                string labelText;
                                string labelType;
                                string labelTooltip;
                                if ( requirementResult.MeetsGroupRequirement == MeetsGroupRequirement.Meets )
                                {
                                    labelText = requirementResult.GroupRequirement.GroupRequirementType.PositiveLabel;
                                    labelType = "success";
                                }
                                else if ( requirementResult.MeetsGroupRequirement == MeetsGroupRequirement.MeetsWithWarning )
                                {
                                    labelText = requirementResult.GroupRequirement.GroupRequirementType.WarningLabel;
                                    labelType = "warning";
                                }
                                else
                                {
                                    passedAllRequirements = false;
                                    labelText = requirementResult.GroupRequirement.GroupRequirementType.NegativeLabel;
                                    labelType = "danger";
                                }

                                if ( string.IsNullOrEmpty( labelText ) )
                                {
                                    labelText = requirementResult.GroupRequirement.GroupRequirementType.Name;
                                }

                                if ( requirementResult.MeetsGroupRequirement == MeetsGroupRequirement.MeetsWithWarning )
                                {
                                    labelTooltip = requirementResult.RequirementWarningDateTime.HasValue
                                        ? "Last Checked: " + requirementResult.RequirementWarningDateTime.Value.ToString( "g" )
                                        : "Not calculated yet";
                                }
                                else
                                {
                                    labelTooltip = requirementResult.LastRequirementCheckDateTime.HasValue
                                        ? "Last Checked: " + requirementResult.LastRequirementCheckDateTime.Value.ToString( "g" )
                                        : "Not calculated yet";
                                }

                                lRequirementsLabels.Text += string.Format(
                                    @"<span class='label label-{1}' title='{2}'>{0}</span>
                        ",
                                    labelText,
                                    labelType,
                                    labelTooltip );
                            }
                        }

                        var requirementsWithErrors = requirementsResults.Where( a => a.MeetsGroupRequirement == MeetsGroupRequirement.Error ).ToList();
                        if ( requirementsWithErrors.Any() )
                        {
                            nbRequirementsErrors.Text = string.Format(
                                "An error occurred in one or more of the requirement calculations: <br /> {0}",
                                requirementsWithErrors.AsDelimited( "<br />" ) );
                            nbRequirementsErrors.Visible = true;
                        }

                        if ( passedAllRequirements || ( group.MustMeetRequirementsToAddMember.HasValue && !group.MustMeetRequirementsToAddMember.Value ) )
                        {
                            if ( passedAllRequirements )
                            {
                                lbConnect.RemoveCssClass( "js-confirm-connect" );
                            }
                            else
                            {
                                lbConnect.AddCssClass( "js-confirm-connect" );
                            }

                            lbConnect.Enabled = true;
                        }
                        else
                        {
                            lbConnect.Enabled = false;
                        }
                    }
                }
            }
        }
Exemple #2
0
            /// <summary>
            /// Called after the save operation has been executed
            /// </summary>
            /// <remarks>
            /// This method is only called if <see cref="M:Rock.Data.EntitySaveHook`1.PreSave" /> returns
            /// without error.
            /// </remarks>
            protected override void PostSave()
            {
                // Get the current person's alias ID from the current context.
                var currentPersonAliasId = DbContext.GetCurrentPersonAlias()?.Id;
                var connectionRequest    = this.Entity as ConnectionRequest;

                // Create and send the change notification message now that the connection request has been saved.
                var processConnectionRequestChangeMessage = GetProcessConnectionRequestChangeMessage(Entry, connectionRequest, currentPersonAliasId);

                processConnectionRequestChangeMessage.SendWhen(this.DbContext.WrappedTransactionCompletedTask);

                var rockContext = ( RockContext )this.RockContext;

                if (Entity.ConnectionStatus == null)
                {
                    Entity.ConnectionStatus = new ConnectionStatusService(rockContext).Get(Entity.ConnectionStatusId);
                }

                if (Entity.ConnectionStatus != null && Entity.ConnectionStatus.AutoInactivateState && Entity.ConnectionState != ConnectionState.Inactive)
                {
                    Entity.ConnectionState = ConnectionState.Inactive;
                    rockContext.SaveChanges();
                }

                var connectionStatusAutomationsQuery = new ConnectionStatusAutomationService(rockContext).Queryable().Where(a => a.SourceStatusId == Entity.ConnectionStatusId);

                if (this.Entity._runAutomationsInPostSaveChanges && connectionStatusAutomationsQuery.Any())
                {
                    var connectionStatusAutomationsList = connectionStatusAutomationsQuery.AsNoTracking().OrderBy(a => a.AutomationName).ToList();
                    var connectionStatusAutomations     = connectionStatusAutomationsList;
                    int changedStatusCount = 0;
                    foreach (var connectionStatusAutomation in connectionStatusAutomations)
                    {
                        if (this.Entity.processedConnectionStatusAutomations.Contains(connectionStatusAutomation.Id))
                        {
                            // to avoid recursion, skip over automations that have already been processed in this thread.
                            continue;
                        }

                        if (Entity.ConnectionStatusId == connectionStatusAutomation.DestinationStatusId)
                        {
                            // If already have this status, no need to figure out if it needs to be set to this status,
                            // or to set the status.
                            this.Entity.processedConnectionStatusAutomations.Add(connectionStatusAutomation.Id);
                            continue;
                        }

                        bool isAutomationValid = true;
                        if (connectionStatusAutomation.DataViewId.HasValue)
                        {
                            // Get the dataview configured for the connection request
                            var dataViewService = new DataViewService(rockContext);
                            var dataview        = dataViewService.Get(connectionStatusAutomation.DataViewId.Value);

                            if (dataview != null)
                            {
                                var dataViewQuery = new ConnectionRequestService(rockContext).GetQueryUsingDataView(dataview);
                                isAutomationValid = dataViewQuery.Any(a => a.Id == Entity.Id);
                            }
                        }

                        if (isAutomationValid && connectionStatusAutomation.GroupRequirementsFilter != GroupRequirementsFilter.Ignore)
                        {
                            // Group Requirement can't be meet when either placement group or placement group role id is missing
                            if (!Entity.AssignedGroupId.HasValue || !Entity.AssignedGroupMemberRoleId.HasValue)
                            {
                                isAutomationValid = false;
                            }
                            else
                            {
                                var isRequirementMeet   = true;
                                var group               = new GroupService(rockContext).Get(Entity.AssignedGroupId.Value);
                                var hasGroupRequirement = new GroupRequirementService(rockContext).Queryable().Where(a => (a.GroupId.HasValue && a.GroupId == group.Id) || (a.GroupTypeId.HasValue && a.GroupTypeId == group.GroupTypeId)).Any();
                                if (hasGroupRequirement)
                                {
                                    var requirementsResults = group.PersonMeetsGroupRequirements(
                                        rockContext,
                                        Entity.PersonAlias.PersonId,
                                        Entity.AssignedGroupMemberRoleId.Value);

                                    if (requirementsResults != null && requirementsResults
                                        .Where(a => a.MeetsGroupRequirement != MeetsGroupRequirement.NotApplicable)
                                        .Any(r =>
                                             r.MeetsGroupRequirement != MeetsGroupRequirement.Meets && r.MeetsGroupRequirement != MeetsGroupRequirement.MeetsWithWarning)
                                        )
                                    {
                                        isRequirementMeet = false;
                                    }
                                }

                                // connection request based on if group requirement is meet or not is added to list for status update
                                isAutomationValid = (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.DoesNotMeet && !isRequirementMeet) ||
                                                    (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.MustMeet && isRequirementMeet);
                            }
                        }

                        if (isAutomationValid)
                        {
                            if (Entity.SetConnectionStatusFromAutomationLoop(connectionStatusAutomation))
                            {
                                changedStatusCount++;
                                rockContext.SaveChanges();
                            }
                        }
                    }
                }

                var hasHistoryChanges       = HistoryChangeList?.Any() == true;
                var hasPersonHistoryChanges = PersonHistoryChangeList?.Any() == true;

                if (hasHistoryChanges || hasPersonHistoryChanges)
                {
                    using (var historyRockContext = new RockContext())
                    {
                        if (hasHistoryChanges)
                        {
                            HistoryService.SaveChanges(historyRockContext, typeof(ConnectionRequest), Rock.SystemGuid.Category.HISTORY_CONNECTION_REQUEST.AsGuid(), Entity.Id, HistoryChangeList, false, Entity.ModifiedByPersonAliasId);
                        }

                        if (hasPersonHistoryChanges)
                        {
                            var personId = Entity.PersonAlias?.PersonId ?? new PersonAliasService(rockContext).GetPersonId(Entity.PersonAliasId);
                            if (personId.HasValue)
                            {
                                HistoryService.SaveChanges(
                                    historyRockContext,
                                    typeof(Person),
                                    Rock.SystemGuid.Category.HISTORY_PERSON_CONNECTION_REQUEST.AsGuid(),
                                    personId.Value,
                                    PersonHistoryChangeList,
                                    "Request",
                                    typeof(ConnectionRequest),
                                    Entity.Id,
                                    false,
                                    Entity.ModifiedByPersonAliasId,
                                    rockContext.SourceOfChange);
                            }
                        }

                        historyRockContext.SaveChanges(false);
                    }
                }

                base.PostSave();
            }
        private void CheckGroupRequirement()
        {
            nbRequirementsWarning.Visible = false;

            int? personId = ppRequestor.PersonId;
            int? groupId = ddlPlacementGroup.SelectedValueAsInt();
            int? roleId = ddlPlacementGroupRole.SelectedValueAsInt();

            if ( personId.HasValue && groupId.HasValue && roleId.HasValue )
            {
                using ( var rockContext = new RockContext() )
                {
                    var group = new GroupService( rockContext ).Get( groupId.Value );
                    if ( group != null && group.MustMeetRequirementsToAddMember.HasValue && group.MustMeetRequirementsToAddMember.Value )
                    {
                        var requirementsResults = group.PersonMeetsGroupRequirements(
                            personId.Value,
                            roleId.Value );

                        if ( requirementsResults != null && requirementsResults
                            .Any( r =>
                                r.MeetsGroupRequirement == MeetsGroupRequirement.NotMet &&
                                r.GroupRequirement.GroupRequirementType.RequirementCheckType != RequirementCheckType.Manual )
                            )
                        {
                            var person = new PersonService( rockContext ).Get( personId.Value );
                            if ( person != null )
                            {
                                nbRequirementsWarning.Text = string.Format( "{0} does not currently meet the requirements for the selected group/role and will not be able to be placed.", person.NickName );
                            }
                            else
                            {
                                nbRequirementsWarning.Text = "This person does not currently meet the requirements for this group and will not be able to be placed.";
                            }
                            nbRequirementsWarning.Visible = true;
                        }
                    }
                }
            }
        }
            /// <summary>
            /// Called after the save operation has been executed
            /// </summary>
            /// <remarks>
            /// This method is only called if <see cref="M:Rock.Data.EntitySaveHook`1.PreSave" /> returns
            /// without error.
            /// </remarks>
            protected override void PostSave()
            {
                var rockContext = ( RockContext )this.RockContext;

                if (Entity.ConnectionStatus == null)
                {
                    Entity.ConnectionStatus = new ConnectionStatusService(rockContext).Get(Entity.ConnectionStatusId);
                }

                if (Entity.ConnectionStatus != null && Entity.ConnectionStatus.AutoInactivateState && Entity.ConnectionState != ConnectionState.Inactive)
                {
                    Entity.ConnectionState = ConnectionState.Inactive;
                    rockContext.SaveChanges();
                }

                if (Entity.ConnectionStatus.ConnectionStatusAutomations.Any())
                {
                    foreach (var connectionStatusAutomation in Entity.ConnectionStatus.ConnectionStatusAutomations)
                    {
                        bool isAutomationValid = true;
                        if (connectionStatusAutomation.DataViewId.HasValue)
                        {
                            // Get the dataview configured for the connection request
                            var dataViewService = new DataViewService(rockContext);
                            var dataview        = dataViewService.Get(connectionStatusAutomation.DataViewId.Value);
                            if (dataview != null)
                            {
                                var dataViewGetQueryArgs = new DataViewGetQueryArgs {
                                    DbContext = rockContext
                                };
                                isAutomationValid = dataview.GetQuery(dataViewGetQueryArgs).Any(a => a.Id == Entity.Id);
                            }
                        }

                        if (isAutomationValid && connectionStatusAutomation.GroupRequirementsFilter != GroupRequirementsFilter.Ignore)
                        {
                            // Group Requirement can't be meet when either placement group or placement group role id is missing
                            if (!Entity.AssignedGroupId.HasValue || !Entity.AssignedGroupMemberRoleId.HasValue)
                            {
                                isAutomationValid = false;
                            }
                            else
                            {
                                var isRequirementMeet   = true;
                                var group               = new GroupService(rockContext).Get(Entity.AssignedGroupId.Value);
                                var hasGroupRequirement = new GroupRequirementService(rockContext).Queryable().Where(a => (a.GroupId.HasValue && a.GroupId == group.Id) || (a.GroupTypeId.HasValue && a.GroupTypeId == group.GroupTypeId)).Any();
                                if (hasGroupRequirement)
                                {
                                    var requirementsResults = group.PersonMeetsGroupRequirements(
                                        rockContext,
                                        Entity.PersonAlias.PersonId,
                                        Entity.AssignedGroupMemberRoleId.Value);

                                    if (requirementsResults != null && requirementsResults
                                        .Where(a => a.MeetsGroupRequirement != MeetsGroupRequirement.NotApplicable)
                                        .Any(r =>
                                             r.MeetsGroupRequirement != MeetsGroupRequirement.Meets && r.MeetsGroupRequirement != MeetsGroupRequirement.MeetsWithWarning)
                                        )
                                    {
                                        isRequirementMeet = false;
                                    }
                                }

                                // connection request based on if group requirement is meet or not is added to list for status update
                                isAutomationValid = (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.DoesNotMeet && !isRequirementMeet) ||
                                                    (connectionStatusAutomation.GroupRequirementsFilter == GroupRequirementsFilter.MustMeet && isRequirementMeet);
                            }
                        }

                        if (isAutomationValid)
                        {
                            Entity.ConnectionStatusId = connectionStatusAutomation.DestinationStatusId;

                            // disabled pre post processing in order to prevent circular loop that may arise due to status change.
                            rockContext.SaveChanges(true);
                        }
                    }
                }

                if (HistoryChangeList?.Any() == true)
                {
                    HistoryService.SaveChanges(rockContext, typeof(ConnectionRequest), Rock.SystemGuid.Category.HISTORY_CONNECTION_REQUEST.AsGuid(), Entity.Id, HistoryChangeList, true, Entity.ModifiedByPersonAliasId);
                }

                if (PersonHistoryChangeList?.Any() == true)
                {
                    var personAlias = Entity.PersonAlias ?? new PersonAliasService(rockContext).Get(Entity.PersonAliasId);
                    HistoryService.SaveChanges(
                        rockContext,
                        typeof(Person),
                        Rock.SystemGuid.Category.HISTORY_PERSON_CONNECTION_REQUEST.AsGuid(),
                        personAlias.PersonId,
                        PersonHistoryChangeList,
                        "Request",
                        typeof(ConnectionRequest),
                        Entity.Id,
                        true,
                        Entity.ModifiedByPersonAliasId,
                        rockContext.SourceOfChange);
                }

                base.PostSave();
            }