public BlockActionResult DeleteScheduledUnavailability(Guid attendanceGuid)
        {
            var rockContext = new RockContext();

            // The schedule exclusion service.
            var scheduleExclusionService = new PersonScheduleExclusionService(rockContext);

            // Get the specific scheduled exclusion from the Guid.
            var scheduleExclusion = scheduleExclusionService.GetNoTracking(attendanceGuid);

            if (scheduleExclusion == null)
            {
                return(ActionNotFound());
            }


            // Get the person schedule exclusion.
            var personScheduleExclusion = scheduleExclusionService.Get(scheduleExclusion.Id);

            if (personScheduleExclusion == null)
            {
                return(ActionNotFound());
            }


            // Find all of the children.
            var scheduleExclusionChildren = scheduleExclusionService.Queryable().Where(x => x.ParentPersonScheduleExclusionId == personScheduleExclusion.Id);

            foreach (var scheduleExclusionChild in scheduleExclusionChildren)
            {
                scheduleExclusionChild.ParentPersonScheduleExclusionId = null;
            }

            // Delete the exclusion.
            scheduleExclusionService.Delete(personScheduleExclusion);

            rockContext.SaveChanges();


            return(ActionOk());
        }
        /// <summary>
        /// Gets the content view model for ScheduleUnavailability mobile block.
        /// </summary>
        /// <returns>A <see cref="ContentBag"/></returns>
        private ContentBag GetScheduleContent()
        {
            var rockContext = new RockContext();

            // The dictionary of merge fields.
            var mergeFields = RequestContext.GetCommonMergeFields();

            // Get the current user's family members.
            var familyMemberAliasIds = new PersonService(rockContext)
                                       .GetFamilyMembers(this.CurrentPersonId, true)
                                       .SelectMany(m => m.Person.Aliases)
                                       .Select(a => a.Id).ToList();

            var currentDateTime = RockDateTime.Now.Date;

            var personScheduleExclusionService = new PersonScheduleExclusionService(rockContext);

            // Get the schedule exclusions.
            var personScheduleExclusions = personScheduleExclusionService
                                           .Queryable("PersonAlias.Person")
                                           .AsNoTracking()
                                           .Where(e => familyMemberAliasIds.Contains(e.PersonAliasId.Value))
                                           .Where(e => e.StartDate >= currentDateTime || e.EndDate >= currentDateTime)
                                           .OrderBy(e => e.StartDate)
                                           .ThenBy(e => e.EndDate)
                                           .Select(e => new GroupScheduleRowInfo
            {
                Title = e.Title,
                Guid  = e.Guid,
                Id    = e.Id,
                OccurrenceStartDate = DbFunctions.TruncateTime(e.StartDate).Value,
                OccurrenceEndDate   = DbFunctions.TruncateTime(e.EndDate).Value,
                Group             = e.Group,
                PersonAlias       = e.PersonAlias,
                GroupScheduleType = GroupScheduleType.Unavailable
            });

            var groupService = new GroupService(rockContext);

            // get groups that the selected person is an active member of and have SchedulingEnabled and have at least one location with a schedule.
            var groups = groupService
                         .Queryable()
                         .AsNoTracking()
                         .Where(x => x.Members.Any(m => m.PersonId == this.CurrentPersonId && m.IsArchived == false && m.GroupMemberStatus == GroupMemberStatus.Active))
                         .Where(x => x.IsActive == true && x.IsArchived == false &&
                                x.GroupType.IsSchedulingEnabled == true &&
                                x.DisableScheduling == false &&
                                x.DisableScheduleToolboxAccess == false)
                         .Where(x => x.GroupLocations.Any(gl => gl.Schedules.Any()))
                         .OrderBy(x => new { x.Order, x.Name })
                         .AsNoTracking()
                         .ToList();

            // A list of the group names and guids.
            var groupsList = new List <ListItemViewModel>();

            // Every result from our query.
            foreach (var group in groups)
            {
                // Add it to the groups list with the necessary information.
                var groupInformation = new ListItemViewModel
                {
                    Text  = group.Name,
                    Value = group.Guid.ToStringSafe()
                };

                groupsList.Add(groupInformation);
            }

            // Getting the family members.
            var personService = new PersonService(rockContext);

            var familyMembersQuery = personService.GetFamilyMembers(this.CurrentPersonId, false)
                                     .AsNoTracking()
                                     .ToList();

            // A list of family member names and guids.
            var familyMembersList = new List <ListItemViewModel>();

            foreach (var familyMember in familyMembersQuery)
            {
                // Add it to the family members list with the necessary information.
                var familyMemberInformation = new ListItemViewModel
                {
                    Text  = familyMember.Person.FullName,
                    Value = familyMember.Guid.ToStringSafe()
                };

                familyMembersList.Add(familyMemberInformation);
            }

            // Add in our list of schedule exclusions.
            mergeFields.AddOrReplace("ScheduleExclusionsList", personScheduleExclusions);

            // Pass those in as content.
            var content = TypeTemplate.ResolveMergeFields(mergeFields);

            // Return all of the necessary information.
            return(new ContentBag
            {
                Content = content,
                GroupInformation = groupsList,
                FamilyMemberInformation = familyMembersList,
                IsDescriptionRequired = DescriptionRequired
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a list of available schedules for the group the selected sign-up person belongs to.
        /// </summary>
        /// <param name="groupGuid"></param>
        /// <returns></returns>
        private PersonScheduleSignupBag GetScheduleSignupData(Guid groupGuid)
        {
            List <PersonScheduleSignupDataBag> personScheduleSignups = new List <PersonScheduleSignupDataBag>();
            int numOfWeeks = FutureWeeksToShow;
            var startDate  = RockDateTime.Now.AddDays(1).Date;
            var endDate    = RockDateTime.Now.AddDays(numOfWeeks * 7);

            using (var rockContext = new RockContext())
            {
                var scheduleService                = new ScheduleService(rockContext);
                var attendanceService              = new AttendanceService(rockContext);
                var groupLocationService           = new GroupLocationService(rockContext);
                var groupService                   = new GroupService(rockContext);
                var personScheduleExclusionService = new PersonScheduleExclusionService(rockContext);

                var group = groupService.Get(groupGuid);
                var personGroupLocationList = GetApplicableGroupLocations(group, groupLocationService);

                var groupsThatHaveSchedulingRequirements          = personGroupLocationList.Where(a => a.Group.SchedulingMustMeetRequirements).Select(a => a.Group).Distinct().ToList();
                var personDoesntMeetSchedulingRequirementGroupIds = new HashSet <int>();

                // If the person does not meet the scheduling requirements for the current group.
                var personDoesntMeetSchedulingRequirements = groupService.GroupMembersNotMeetingRequirements(group, false, false)
                                                             .Where(a => a.Key.PersonId == CurrentPersonId)
                                                             .Any();

                if (personDoesntMeetSchedulingRequirements)
                {
                    personDoesntMeetSchedulingRequirementGroupIds.Add(group.Id);
                }

                // For every location in the location list.
                foreach (var personGroupLocation in personGroupLocationList)
                {
                    // Loop through each particular scheduling opportunity
                    foreach (var schedule in personGroupLocation.Schedules)
                    {
                        // Find if this has max volunteers here.
                        int maximumCapacitySetting = 0;
                        int desiredCapacitySetting = 0;
                        int minimumCapacitySetting = 0;
                        int desiredOrMinimumNeeded = 0;

                        if (personGroupLocation.GroupLocationScheduleConfigs.Any())
                        {
                            var groupConfigs = personGroupLocationList.Where(x => x.GroupId == personGroupLocation.GroupId).Select(x => x.GroupLocationScheduleConfigs);
                            foreach (var groupConfig in groupConfigs)
                            {
                                foreach (var config in groupConfig)
                                {
                                    if (config.ScheduleId == schedule.Id)
                                    {
                                        maximumCapacitySetting += config.MaximumCapacity ?? 0;
                                        desiredCapacitySetting += config.DesiredCapacity ?? 0;
                                        minimumCapacitySetting += config.MinimumCapacity ?? 0;
                                    }
                                }
                            }

                            desiredOrMinimumNeeded = Math.Max(desiredCapacitySetting, minimumCapacitySetting);
                        }

                        var startDateTimeList = schedule.GetScheduledStartTimes(startDate, endDate);

                        // For every start date time in the schedule, loop through to check if it is applicable to the current person. If so, we can add it to the master list.
                        foreach (var startDateTime in startDateTimeList)
                        {
                            var  occurrenceDate   = startDateTime.Date;
                            bool alreadyScheduled = attendanceService.IsScheduled(occurrenceDate, schedule.Id, CurrentPersonId);
                            if (alreadyScheduled)
                            {
                                continue;
                            }

                            // Don't show dates they have blacked out.
                            if (personScheduleExclusionService.IsExclusionDate(RequestContext.CurrentPerson.PrimaryAlias.PersonId, personGroupLocation.GroupId, occurrenceDate))
                            {
                                continue;
                            }

                            // Don't show groups that have scheduling requirements that the person hasn't met.
                            if (personDoesntMeetSchedulingRequirementGroupIds.Contains(personGroupLocation.GroupId))
                            {
                                continue;
                            }

                            // Get count of scheduled Occurrences with RSVP "Yes" for the group/schedule.
                            int currentScheduled = attendanceService
                                                   .Queryable()
                                                   .Where(a => a.Occurrence.OccurrenceDate == startDateTime.Date &&
                                                          a.Occurrence.ScheduleId == schedule.Id &&
                                                          a.RSVP == RSVP.Yes &&
                                                          a.Occurrence.GroupId == personGroupLocation.GroupId)
                                                   .Count();

                            bool maxScheduled = maximumCapacitySetting != 0 && currentScheduled >= maximumCapacitySetting;
                            int  peopleNeeded = desiredOrMinimumNeeded != 0 ? desiredOrMinimumNeeded - currentScheduled : 0;

                            // Add to master list personScheduleSignups.
                            personScheduleSignups.Add(new PersonScheduleSignupDataBag
                            {
                                GroupGuid         = personGroupLocation.Group.Guid,
                                GroupOrder        = personGroupLocation.Group.Order,
                                GroupName         = personGroupLocation.Group.Name,
                                LocationGuid      = personGroupLocation.Location.Guid,
                                LocationName      = personGroupLocation.Location.Name,
                                LocationOrder     = personGroupLocation.Order,
                                ScheduleGuid      = schedule.Guid,
                                ScheduleName      = schedule.Name,
                                ScheduledDateTime = startDateTime.ToRockDateTimeOffset(),
                                MaxScheduled      = maxScheduled,
                                PeopleNeeded      = peopleNeeded < 0 ? 0 : peopleNeeded
                            });
                        }
                    }
                }

                return(new PersonScheduleSignupBag
                {
                    GroupName = group.Name,
                    PersonScheduleSignups = personScheduleSignups
                });
            }
        }