Example #1
0
        private static AttendanceCache LoadByAttendance(Attendance attendance)
        {
            //We don't want to cache attendances created before today.
            if (!attendance.CreatedDateTime.HasValue || attendance.CreatedDateTime < Rock.RockDateTime.Today)
            {
                Remove(attendance.Id);
                return(null);
            }

            Person person;

            if (attendance.PersonAliasId != null && attendance.PersonAlias?.Person == null)
            {
                PersonAliasService personAliasService = new PersonAliasService(new RockContext());
                person = personAliasService.GetPersonNoTracking(attendance.PersonAliasId ?? 0);
            }
            else
            {
                person = attendance.PersonAlias.Person;
            }


            var attendanceCache = new AttendanceCache
            {
                Id = attendance.Id,
                OccurrenceAccessKey = OccurrenceCache.GetByOccurrence(attendance.Occurrence)?.AccessKey ?? "None",
                Code            = attendance.AttendanceCode?.Code,
                CreatedDateTime = attendance.CreatedDateTime,
                StartDateTime   = attendance.StartDateTime,
                EndDateTime     = attendance.EndDateTime,
                PersonId        = person?.Id ?? 0,
                PersonName      = person?.FullName ?? "Unnamed Person",
                GroupId         = attendance.Occurrence.GroupId,
                LocationId      = attendance.Occurrence.LocationId,
                ScheduleId      = attendance.Occurrence.ScheduleId,
                IsVolunteer     = OccurrenceCache.GetVolunteerOccurrences().Select(o => o.GroupId).Contains(attendance.Occurrence.GroupId ?? 0),
                IsChildren      = OccurrenceCache.GetChildrenOccurrences().Select(o => o.GroupId).Contains(attendance.Occurrence.GroupId ?? 0),
                WithParent      = false
            };

            //Sometimes we need to look up the attendance code
            if (attendanceCache.Code.IsNullOrWhiteSpace() && attendance.AttendanceCodeId.HasValue)
            {
                RockContext           rockContext           = new RockContext();
                AttendanceCodeService attendanceCodeService = new AttendanceCodeService(rockContext);
                var code = attendanceCodeService.GetNoTracking(attendance.AttendanceCodeId.Value);
                if (code != null)
                {
                    attendanceCache.Code = code.Code;
                }
            }


            if (attendance.EndDateTime.HasValue)   //End date means checked out
            {
                attendanceCache.AttendanceState = AttendanceState.CheckedOut;
            }
            else //has not been checked out yet
            {
                if (attendance.DidAttend == false && attendance.QualifierValueId.HasValue)
                {
                    attendanceCache.AttendanceState = AttendanceState.MobileReserve;
                }
                else if (attendance.DidAttend == true)
                {
                    attendanceCache.AttendanceState = AttendanceState.InRoom;
                    if (attendance.QualifierValueId == DefinedValueCache.GetId(Constants.DEFINED_VALUE_ATTENDANCE_STATUS_WITH_PARENT.AsGuid()))
                    {
                        attendanceCache.WithParent = true;
                    }
                }
                else
                {
                    attendanceCache.AttendanceState = AttendanceState.EnRoute;
                }
            }


            return(attendanceCache);
        }