Exemplo n.º 1
0
        private NavigationData GetNavigationData( CampusCache campus )
        {
            using ( var rockContext = new RockContext() )
            {
                var validLocationids = new List<int>();
                if ( campus.LocationId.HasValue )
                {
                    // Get all the child locations
                    validLocationids.Add( campus.LocationId.Value );
                    new LocationService( rockContext )
                        .GetAllDescendents( campus.LocationId.Value )
                        .Select( l => l.Id )
                        .ToList()
                        .ForEach( l => validLocationids.Add( l ) );
                }

                var groupTypeTemplateGuid = PageParameter( "Area" ).AsGuidOrNull();
                if ( !groupTypeTemplateGuid.HasValue )
                {
                    groupTypeTemplateGuid = this.GetAttributeValue( "GroupTypeTemplate" ).AsGuidOrNull();
                }

                if ( !groupTypeTemplateGuid.HasValue )
                {
                    // Check to see if a specific group was specified
                    Guid? guid = Rock.SystemGuid.DefinedValue.GROUPTYPE_PURPOSE_CHECKIN_TEMPLATE.AsGuid();
                    int? groupId = PageParameter( "Group" ).AsIntegerOrNull();
                    if ( groupId.HasValue && guid.HasValue )
                    {
                        var group = new GroupService( rockContext ).Get( groupId.Value );
                        if ( group != null && group.GroupType != null )
                        {
                            var groupType = GetParentPurposeGroupType( group.GroupType, guid.Value );
                            if ( groupType != null )
                            {
                                groupTypeTemplateGuid = groupType.Guid;
                            }
                        }
                    }
                }

                if ( groupTypeTemplateGuid.HasValue )
                {
                    pnlContent.Visible = true;

                    NavData = new NavigationData();

                    var chartTimes = GetChartTimes();

                    // Get the group types
                    var parentGroupType = GroupTypeCache.Read( groupTypeTemplateGuid.Value );
                    if ( parentGroupType != null )
                    {
                        foreach ( var childGroupType in parentGroupType.ChildGroupTypes )
                        {
                            AddGroupType( childGroupType, chartTimes );
                        }
                    }

                    // Get the groups
                    var groupTypeIds = NavData.GroupTypes.Select( t => t.Id ).ToList();

                    var groups = new GroupService( rockContext )
                        .Queryable( "GroupLocations" ).AsNoTracking()
                        .Where( g =>
                            groupTypeIds.Contains( g.GroupTypeId ) &&
                            g.IsActive )
                        .ToList();
                    var groupIds = groups.Select( g => g.Id ).ToList();

                    foreach( var group in groups )
                    {
                        var childGroupIds = groups
                            .Where( g =>
                                g.ParentGroupId.HasValue &&
                                g.ParentGroupId.Value == group.Id )
                            .Select( g => g.Id )
                            .ToList();

                        var childLocationIds = group.GroupLocations
                            .Where( l => validLocationids.Contains( l.LocationId ) )
                            .Select( l => l.LocationId )
                            .ToList();

                        if ( childLocationIds.Any() || childGroupIds.Any() )
                        {
                            var navGroup = new NavigationGroup( group, chartTimes );
                            navGroup.ChildLocationIds = childLocationIds;
                            navGroup.ChildGroupIds = childGroupIds;
                            NavData.Groups.Add( navGroup );

                            if ( !group.ParentGroupId.HasValue || groupIds.Contains( group.ParentGroupId.Value ) )
                            {
                                NavData.GroupTypes.Where( t => t.Id == group.GroupTypeId ).ToList()
                                    .ForEach( t => t.ChildGroupIds.Add( group.Id ) );
                            }
                        }
                    }

                    // Remove any groups without child locations
                    var emptyGroupIds = NavData.Groups
                        .Where( g => !g.ChildGroupIds.Any() && !g.ChildLocationIds.Any() )
                        .Select( g => g.Id )
                        .ToList();
                    while ( emptyGroupIds.Any() )
                    {
                        NavData.Groups = NavData.Groups.Where( g => !emptyGroupIds.Contains( g.Id ) ).ToList();
                        NavData.Groups.ForEach( g =>
                            g.ChildGroupIds = g.ChildGroupIds.Where( c => !emptyGroupIds.Contains( c ) ).ToList() );
                        emptyGroupIds = NavData.Groups
                            .Where( g => !g.ChildGroupIds.Any() && !g.ChildLocationIds.Any() )
                            .Select( g => g.Id )
                            .ToList();
                    }

                    // Remove any grouptype without groups
                    var emptyGroupTypeIds = NavData.GroupTypes
                        .Where( t => !t.ChildGroupIds.Any() && !t.ChildGroupTypeIds.Any() )
                        .Select( t => t.Id )
                        .ToList();
                    while ( emptyGroupTypeIds.Any() )
                    {
                        NavData.GroupTypes = NavData.GroupTypes.Where( t => !emptyGroupTypeIds.Contains( t.Id ) ).ToList();
                        NavData.GroupTypes.ForEach( t =>
                            t.ChildGroupTypeIds = t.ChildGroupTypeIds.Where( c => !emptyGroupTypeIds.Contains( c ) ).ToList() );
                        emptyGroupTypeIds = NavData.GroupTypes
                            .Where( t => !t.ChildGroupIds.Any() && !t.ChildGroupTypeIds.Any() )
                            .Select( t => t.Id )
                            .ToList();
                    }

                    // If not group types left, redirect to area select page
                    if ( NavData.GroupTypes.Count == 0 )
                    {
                        NavigateToLinkedPage( "AreaSelectPage" );
                    }

                    // Get the locations
                    var locationIds = NavData.Groups.SelectMany( g => g.ChildLocationIds ).Distinct().ToList();
                    foreach ( var location in new LocationService( rockContext )
                        .Queryable( "ParentLocation" ).AsNoTracking()
                        .Where( l => locationIds.Contains( l.Id ) ) )
                    {
                        var navLocation = AddLocation( location, chartTimes );
                        navLocation.HasGroups = true;
                    }

                    // Get the attendance counts
                    var dayStart = RockDateTime.Today;
                    var now = RockDateTime.Now;
                    groupIds = NavData.Groups.Select( g => g.Id ).ToList();

                    var attendances = new AttendanceService( rockContext ).Queryable()
                        .Where( a =>
                            a.ScheduleId.HasValue &&
                            a.GroupId.HasValue &&
                            a.LocationId.HasValue &&
                            a.StartDateTime > dayStart &&
                            a.StartDateTime < now &&
                            a.DidAttend.HasValue &&
                            a.DidAttend.Value &&
                            groupIds.Contains( a.GroupId.Value ) &&
                            locationIds.Contains( a.LocationId.Value ) )
                        .ToList();

                    var schedules = new ScheduleService( rockContext ).Queryable()
                            .Where( s => s.CheckInStartOffsetMinutes.HasValue )
                            .ToList();

                    foreach ( DateTime chartTime in chartTimes )
                    {
                        // Get the active schedules
                        var activeSchedules = new List<int>();
                        foreach ( var schedule in schedules )
                        {
                            if ( schedule.WasScheduleOrCheckInActive( chartTime ) )
                            {
                                activeSchedules.Add( schedule.Id );
                            }
                        }

                        bool current = chartTime.Equals( chartTimes.Max() );

                        foreach ( var groupLocSched in attendances
                            .Where( a =>
                                a.StartDateTime < chartTime &&
                                a.PersonAlias != null &&
                                activeSchedules.Contains( a.ScheduleId.Value ) )
                            .GroupBy( a => new
                            {
                                ScheduleId = a.ScheduleId.Value,
                                GroupId = a.GroupId.Value,
                                LocationId = a.LocationId.Value
                            } )
                            .Select( g => new
                            {
                                ScheduleId = g.Key.ScheduleId,
                                GroupId = g.Key.GroupId,
                                LocationId = g.Key.LocationId,
                                PersonIds = g.Select( a => a.PersonAlias.PersonId ).Distinct().ToList()
                            } ) )
                        {
                            AddGroupCount( chartTime, groupLocSched.GroupId, groupLocSched.PersonIds, current );
                            AddLocationCount( chartTime, groupLocSched.LocationId, groupLocSched.PersonIds, current );
                        }
                    }
                    return NavData;
                }
                else
                {
                    if ( string.IsNullOrWhiteSpace( PageParameter( "Area" ) ) )
                    {
                        // If could not determine area and did not come from are select, redirect to area select page
                        NavigateToLinkedPage( "AreaSelectPage" );
                    }

                    nbWarning.Text = "Please select a valid Check-in type in the block settings.";
                    nbWarning.Visible = true;
                    pnlContent.Visible = false;
                }
            }

            return null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        public override bool Execute(RockContext rockContext, WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            var mergeFields = GetMergeFields(action);

            var homePhoneValueId   = DefinedValueCache.Read(SystemGuid.DefinedValue.PERSON_PHONE_TYPE_HOME).Id;
            var mobilePhoneValueId = DefinedValueCache.Read(SystemGuid.DefinedValue.PERSON_PHONE_TYPE_MOBILE).Id;
            var workPhoneValueId   = DefinedValueCache.Read(SystemGuid.DefinedValue.PERSON_PHONE_TYPE_WORK).Id;

            // get requester
            var requestPerson = new PersonAliasService(rockContext).Get(GetAttributeValue(action, "Person", true).AsGuid()).Person;

            if (requestPerson == null)
            {
                var errorMessage = "Could not determine the person for the request.";
                errorMessages.Add(errorMessage);
                action.AddLogEntry(errorMessage, true);
                return(false);
            }

            // get case worker
            var caseWorker = new PersonAliasService(rockContext).Get(GetAttributeValue(action, "CaseWorker", true).AsGuid())?.Person;

            // get request status
            var statusValue = DefinedValueCache.Read(GetAttributeValue(action, "RequestStatus"));

            if (statusValue == null)
            {
                var errorMessage = "Invalid request status provided.";
                errorMessages.Add(errorMessage);
                action.AddLogEntry(errorMessage, true);
                return(false);
            }

            // get request description
            var requestDescription = GetAttributeValue(action, "RequestDescription", true).ResolveMergeFields(mergeFields);

            if (string.IsNullOrWhiteSpace(requestDescription))
            {
                var errorMessage = "Request description is requried.";
                errorMessages.Add(errorMessage);
                action.AddLogEntry(errorMessage, true);
                return(false);
            }

            // get government id
            var governmentId = GetAttributeValue(action, "GovernmentId", true).ResolveMergeFields(mergeFields);

            // get campus
            int?campusId = CampusCache.Read(GetAttributeValue(action, "Campus").AsGuid())?.Id;

            // create benevolence request
            BenevolenceRequestService benevolenceRequestService = new BenevolenceRequestService(rockContext);

            BenevolenceRequest request = new BenevolenceRequest();

            benevolenceRequestService.Add(request);

            request.RequestDateTime          = RockDateTime.Now;
            request.RequestText              = requestDescription;
            request.RequestedByPersonAliasId = requestPerson.PrimaryAliasId;
            request.FirstName    = requestPerson.NickName;
            request.LastName     = requestPerson.LastName;
            request.Email        = requestPerson.Email;
            request.LocationId   = requestPerson.GetHomeLocation()?.Id;
            request.GovernmentId = governmentId;

            if (campusId.HasValue)
            {
                request.CampusId = campusId.Value;
            }
            else
            {
                request.CampusId = requestPerson.GetCampus()?.Id;
            }

            var requestorPhoneNumbers = requestPerson.PhoneNumbers;

            if (requestorPhoneNumbers != null)
            {
                request.HomePhoneNumber = requestorPhoneNumbers.Where(p => p.NumberTypeValueId == homePhoneValueId).FirstOrDefault()?.NumberFormatted;
                request.CellPhoneNumber = requestorPhoneNumbers.Where(p => p.NumberTypeValueId == mobilePhoneValueId).FirstOrDefault()?.NumberFormatted;
                request.WorkPhoneNumber = requestorPhoneNumbers.Where(p => p.NumberTypeValueId == workPhoneValueId).FirstOrDefault()?.NumberFormatted;
            }

            if (caseWorker != null)
            {
                request.CaseWorkerPersonAliasId = caseWorker.PrimaryAliasId;
            }

            request.ConnectionStatusValueId = requestPerson.ConnectionStatusValueId;
            request.RequestStatusValueId    = statusValue.Id;

            rockContext.SaveChanges();

            SetWorkflowAttributeValue(action, "BenevolenceRequest", request.Guid);

            action.AddLogEntry($"Set 'Benevolence Request' attribute to '{request.Guid}'.");
            return(true);
        }