internal static AttendanceOccurrence GetOccurrenceForLocation(RockContext rockContext, Location location)
        {
            if (IsSubRoom(location))
            {
                location = location.ParentLocation;
            }

            AttendanceOccurrenceService attendanceOccurrenceService = new AttendanceOccurrenceService(rockContext);
            var occurrences = attendanceOccurrenceService.Queryable()
                              .Where(o => o.OccurrenceDate == RockDateTime.Now.Date && o.LocationId == location.Id).ToList();
            var volunteerOccurrences = occurrences.Where(o => KioskCountUtility.GetVolunteerGroupIds().Contains(o.GroupId ?? 0)).FirstOrDefault();

            if (volunteerOccurrences != null)
            {
                return(volunteerOccurrences);
            }
            return(null);
        }
Example #2
0
        private void BindGrid()
        {
            gReport.Visible        = false;
            nbNotification.Visible = false;
            RockContext rockContext = new RockContext();
            AttendanceOccurrenceService attendanceOccurrenceService = new AttendanceOccurrenceService(rockContext);
            AttendanceService           attendanceService           = new AttendanceService(rockContext);
            LocationService             locationService             = new LocationService(rockContext);
            ScheduleService             scheduleService             = new ScheduleService(rockContext);

            var location = lpLocation.Location;

            if (location == null || dpDate.SelectedDate == null)
            {
                return;
            }

            //Set the name of the export
            List <string> locationNameList  = new List <string>();
            var           locationForExport = location;

            while (true)
            {
                if (locationForExport == null)
                {
                    break;
                }
                locationNameList.Add(locationForExport.Name);
                locationForExport = locationForExport.ParentLocation;
            }

            locationNameList.Reverse();
            gReport.ExportTitleName = dpDate.SelectedDate.Value.ToString("MM/dd/yyyy") +
                                      "  -- ";

            if (tgChildLocations.Checked)
            {
                gReport.ExportTitleName += " Child Locations of: ";
            }
            gReport.ExportTitleName += string.Join(" > ", locationNameList);
            var schedule = scheduleService.Get(spSchedule.SelectedValueAsId() ?? 0);

            if (schedule != null)
            {
                gReport.ExportTitleName += " (" + schedule.Name + ")";
            }

            var fileName = dpDate.SelectedDate.Value.ToString("MM.dd.yyyy") +
                           (tgChildLocations.Checked ? "_ChildLocationsOf_" : "_") +
                           location.Name;

            if (schedule != null)
            {
                fileName += "_" + schedule.Name;
            }

            gReport.ExportFilename = fileName;



            //Get child locations if needed
            var locationIds = new List <int> {
                location.Id
            };

            if (tgChildLocations.Checked)
            {
                List <Location> childLocations = GetChildLocations(location);
                locationIds.AddRange(childLocations.Select(l => l.Id));
            }

            var attendanceOccurrencesQry = attendanceOccurrenceService
                                           .Queryable()
                                           .Where(ao => ao.OccurrenceDate == (dpDate.SelectedDate ?? RockDateTime.Now))
                                           .Where(ao => locationIds.Contains(ao.LocationId ?? 0))
                                           .Where(ao => ao.Group != null && ao.Schedule != null);


            var scheduleId = spSchedule.SelectedValueAsId();

            if (scheduleId != null)
            {
                attendanceOccurrencesQry = attendanceOccurrencesQry.Where(ao => ao.ScheduleId == scheduleId);
            }

            var attendanceOccurrences = attendanceOccurrencesQry
                                        .Select(ao => ao.Id)
                                        .ToList();

            var attendances = attendanceService.Queryable("PersonAlias, PersonAlias.Person, Occurrence, Occurrence.Group, Occurrence.Location, Occurrence.Schedule, Device")
                              .Where(a => attendanceOccurrences.Contains(a.OccurrenceId))
                              .ToList();

            if (!attendances.Any())
            {
                nbNotification.Visible = true;
                nbNotification.Text    = "Could not find any attendances with these parameters";
                return;
            }

            var records           = new List <AttendanceRecord>();
            var volunteerGroupIds = KioskCountUtility.GetVolunteerGroupIds();

            foreach (var attendance in attendances)
            {
                var record = new AttendanceRecord
                {
                    PersonId    = attendance.PersonAlias.Person.Id,
                    PersonName  = attendance.PersonAlias.Person.FullNameReversed,
                    Age         = attendance.PersonAlias.Person.Age,
                    Schedule    = attendance.Occurrence.Schedule.Name,
                    Location    = attendance.Occurrence.Location.Name,
                    Group       = attendance.Occurrence.Group.Name,
                    CheckInTime = attendance.CreatedDateTime,
                    EntryTime   = attendance.StartDateTime,
                    ExitTime    = attendance.EndDateTime,
                    DidAttend   = attendance.DidAttend,
                    IsVolunteer = volunteerGroupIds.Contains(attendance.Occurrence.GroupId ?? 0),
                    Device      = attendance.Device != null ? attendance.Device.Name : "Room Scanner"
                };

                if (attendance.ForeignId != null)
                {
                    var subLocation = locationService.Get(attendance.ForeignId ?? 0);
                    if (subLocation != null)
                    {
                        record.SubLocation = subLocation.Name;
                    }
                }

                if (record.CheckInTime >= record.EntryTime && record.Device != "Room Scanner")
                {
                    record.EntryTime = null;
                }

                records.Add(record);
            }

            records = records
                      .OrderBy(r => r.CheckInTime)
                      .ToList();

            gReport.Visible    = true;
            gReport.DataSource = records;
            gReport.DataBind();
        }
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            var filterAttendanceSchedules = GetActionAttributeValue(action, "FilterAttendanceSchedules").AsBoolean();

            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.Families.Where(f => f.Selected).FirstOrDefault();
                if (family != null)
                {
                    KioskCountUtility kioskCountUtility = new KioskCountUtility(checkInState.ConfiguredGroupTypes);
                    List <int>        volunteerGroupIds = kioskCountUtility.VolunteerGroupIds;

                    var locationService   = new LocationService(rockContext);
                    var attendanceService = new AttendanceService(rockContext).Queryable();
                    foreach (var person in family.People)
                    {
                        List <int> filteredScheduleIds = new List <int>();
                        foreach (var groupType in person.GroupTypes)
                        {
                            foreach (var group in groupType.Groups)
                            {
                                foreach (var location in group.Locations)
                                {
                                    //Get a new copy of the entity because otherwise the threshold data may be stale
                                    var locationEntity = locationService.Get(location.Location.Id);

                                    foreach (var schedule in location.Schedules.ToList())
                                    {
                                        if (filterAttendanceSchedules && filteredScheduleIds.Contains(schedule.Schedule.Id))
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var attendanceQry = attendanceService.Where(a =>
                                                                                    a.EndDateTime == null &&
                                                                                    a.Occurrence.ScheduleId == schedule.Schedule.Id &&
                                                                                    a.StartDateTime >= Rock.RockDateTime.Today);


                                        if (filterAttendanceSchedules && attendanceQry.Where(a => a.PersonAlias.PersonId == person.Person.Id).Any())
                                        {
                                            filteredScheduleIds.Add(schedule.Schedule.Id);
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var threshold = locationEntity.FirmRoomThreshold ?? 0;

                                        LocationScheduleCount locationScheduleCount = kioskCountUtility.GetLocationScheduleCount(location.Location.Id, schedule.Schedule.Id);
                                        if (locationScheduleCount.TotalCount >= threshold)
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        if (!volunteerGroupIds.Contains(group.Group.Id))
                                        {
                                            threshold = Math.Min(locationEntity.FirmRoomThreshold ?? 0, locationEntity.SoftRoomThreshold ?? 0);

                                            if (locationScheduleCount.ChildCount >= threshold)
                                            {
                                                location.Schedules.Remove(schedule);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }
Example #4
0
        /// <summary>
        /// Executes the specified workflow.
        /// </summary>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="action">The workflow action.</param>
        /// <param name="entity">The entity.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public override bool Execute(RockContext rockContext, WorkflowAction action, object entity, out List <string> errorMessages)
        {
            var filterAttendanceSchedules = GetActionAttributeValue(action, "FilterAttendanceSchedules").AsBoolean();

            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                var family = checkInState.CheckIn.Families.Where(f => f.Selected).FirstOrDefault();
                if (family != null)
                {
                    var volAttributeGuid = Constants.VOLUNTEER_ATTRIBUTE_GUID;

                    KioskCountUtility kioskCountUtility = new KioskCountUtility(checkInState.ConfiguredGroupTypes);

                    ObjectCache cache             = Rock.Web.Cache.RockMemoryCache.Default;
                    List <int>  volunteerGroupIds = cache[volAttributeGuid + "CachedVolunteerGroups"] as List <int>;
                    if (volunteerGroupIds == null)   //cache miss load from DB
                    {
                        var volAttribute = AttributeCache.Read(volAttributeGuid.AsGuid());
                        AttributeValueService attributeValueService = new AttributeValueService(rockContext);
                        volunteerGroupIds = attributeValueService.Queryable().Where(av => av.AttributeId == volAttribute.Id && av.Value == "True").Select(av => av.EntityId.Value).ToList();
                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(10);
                        cache.Set(volAttributeGuid + "CachedVolunteerGroups", volAttributeGuid + "CachedVolunteerGroups", cachePolicy);
                    }

                    var locationService   = new LocationService(rockContext);
                    var attendanceService = new AttendanceService(rockContext).Queryable();
                    foreach (var person in family.People)
                    {
                        List <int> filteredScheduleIds = new List <int>();
                        foreach (var groupType in person.GroupTypes)
                        {
                            foreach (var group in groupType.Groups)
                            {
                                foreach (var location in group.Locations)
                                {
                                    //Get a new copy of the entity because otherwise the threshold data may be stale
                                    var locationEntity = locationService.Get(location.Location.Id);

                                    foreach (var schedule in location.Schedules.ToList())
                                    {
                                        if (filterAttendanceSchedules && filteredScheduleIds.Contains(schedule.Schedule.Id))
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var attendanceQry = attendanceService.Where(a =>
                                                                                    a.EndDateTime == null &&
                                                                                    a.ScheduleId == schedule.Schedule.Id &&
                                                                                    a.StartDateTime >= Rock.RockDateTime.Today);


                                        if (filterAttendanceSchedules && attendanceQry.Where(a => a.PersonAlias.PersonId == person.Person.Id).Any())
                                        {
                                            filteredScheduleIds.Add(schedule.Schedule.Id);
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        var threshold = locationEntity.FirmRoomThreshold ?? 0;

                                        LocationScheduleCount locationScheduleCount = kioskCountUtility.GetLocationScheduleCount(location.Location.Id, schedule.Schedule.Id);
                                        if (locationScheduleCount.TotalCount >= threshold)
                                        {
                                            location.Schedules.Remove(schedule);
                                            continue;
                                        }

                                        if (!volunteerGroupIds.Contains(group.Group.Id))
                                        {
                                            threshold = Math.Min(locationEntity.FirmRoomThreshold ?? 0, locationEntity.SoftRoomThreshold ?? 0);

                                            if (locationScheduleCount.ChildCount >= threshold)
                                            {
                                                location.Schedules.Remove(schedule);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    return(true);
                }
                else
                {
                    errorMessages.Add("There is not a family that is selected");
                }

                return(false);
            }

            return(false);
        }