Helper class for storing the current attendance for a given kiosk location
Beispiel #1
0
            /// <summary>
            /// Initializes a new instance of the <see cref="AttendanceInfo" /> class
            /// Use this if adding an Attendance record
            /// </summary>
            /// <param name="attendance">The attendance.</param>
            /// <param name="locationId">The location identifier.</param>
            public AttendanceInfo(Attendance attendance, int locationId)
            {
                KioskLocationAttendance kioskLocationAttendance = KioskLocationAttendance.Get(locationId);
                var groupId = attendance.Occurrence?.GroupId;

                if (groupId == null)
                {
                    return;
                }

                // If we can get GroupName from kioskLocationAttendance.Groups, we can avoid lazy loading attendance.Occurrence.Group.
                var groupName = kioskLocationAttendance.Groups.Where(g => g.GroupId == groupId.Value).FirstOrDefault()?.GroupName;

                if (groupName == null)
                {
                    groupName = attendance.Occurrence?.Group?.Name;
                }

                EndDateTime   = attendance.EndDateTime;
                StartDateTime = attendance.StartDateTime;
                CampusId      = attendance.CampusId;
                GroupId       = groupId;
                GroupName     = groupName;
                Schedule      = attendance.Occurrence?.Schedule;
                PersonId      = attendance.PersonAlias?.PersonId;
            }
        /// <summary>
        /// Adds the attendance record.
        /// </summary>
        /// <param name="kioskLocationAttendance">The kiosk location attendance.</param>
        /// <param name="attendance">The attendance.</param>
        private static void AddAttendanceRecord(KioskLocationAttendance kioskLocationAttendance, Attendance attendance)
        {
            if (attendance.GroupId.HasValue && attendance.ScheduleId.HasValue && attendance.PersonId.HasValue)
            {
                var groupAttendance = kioskLocationAttendance.Groups.Where(g => g.GroupId == attendance.GroupId).FirstOrDefault();
                if (groupAttendance == null)
                {
                    groupAttendance           = new KioskGroupAttendance();
                    groupAttendance.GroupId   = attendance.GroupId.Value;
                    groupAttendance.GroupName = attendance.Group.Name;
                    groupAttendance.Schedules = new List <KioskScheduleAttendance>();
                    kioskLocationAttendance.Groups.Add(groupAttendance);
                }

                var scheduleAttendance = groupAttendance.Schedules.Where(s => s.ScheduleId == attendance.ScheduleId).FirstOrDefault();
                if (scheduleAttendance == null)
                {
                    scheduleAttendance              = new KioskScheduleAttendance();
                    scheduleAttendance.ScheduleId   = attendance.ScheduleId.Value;
                    scheduleAttendance.ScheduleName = attendance.Schedule.Name;
                    scheduleAttendance.PersonIds    = new List <int>();
                    groupAttendance.Schedules.Add(scheduleAttendance);
                }

                scheduleAttendance.PersonIds.Add(attendance.PersonId.Value);
            }
        }
 /// <summary>
 /// Adds the attendance.
 /// </summary>
 /// <param name="attendance">The attendance.</param>
 public static void AddAttendance(Attendance attendance)
 {
     if (attendance.LocationId.HasValue)
     {
         var location = KioskLocationAttendance.Read(attendance.LocationId.Value);
         if (location != null)
         {
             AddAttendanceRecord(location, attendance);
         }
     }
 }
        /// <summary>
        /// Reads the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static KioskLocationAttendance Read(int id)
        {
            string cacheKey = KioskLocationAttendance.CacheKey(id);

            ObjectCache             cache = MemoryCache.Default;
            KioskLocationAttendance locationAttendance = cache[cacheKey] as KioskLocationAttendance;

            if (locationAttendance != null)
            {
                return(locationAttendance);
            }
            else
            {
                using (new Rock.Data.UnitOfWorkScope())
                {
                    var location = new LocationService().Get(id);
                    if (location != null)
                    {
                        locationAttendance              = new KioskLocationAttendance();
                        locationAttendance.LocationId   = location.Id;
                        locationAttendance.LocationName = location.Name;
                        locationAttendance.Groups       = new List <KioskGroupAttendance>();

                        var attendanceService = new AttendanceService();
                        foreach (var attendance in attendanceService.GetByDateAndLocation(DateTime.Today, location.Id))
                        {
                            AddAttendanceRecord(locationAttendance, attendance);
                        }

                        var cachePolicy = new CacheItemPolicy();
                        cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(60);
                        cache.Set(cacheKey, locationAttendance, cachePolicy);

                        return(locationAttendance);
                    }
                }
            }

            return(null);
        }
        /// <summary>
        /// Flushes the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        public static void Flush(int id)
        {
            ObjectCache cache = MemoryCache.Default;

            cache.Remove(KioskLocationAttendance.CacheKey(id));
        }
        /// <summary>
        /// Adds the attendance record.
        /// </summary>
        /// <param name="kioskLocationAttendance">The kiosk location attendance.</param>
        /// <param name="attendance">The attendance.</param>
        private static void AddAttendanceRecord( KioskLocationAttendance kioskLocationAttendance, Attendance attendance )
        {
            if ( attendance.GroupId.HasValue && attendance.ScheduleId.HasValue && attendance.PersonId.HasValue )
            {
                var groupAttendance = kioskLocationAttendance.Groups.Where( g => g.GroupId == attendance.GroupId ).FirstOrDefault();
                if ( groupAttendance == null )
                {
                    groupAttendance = new KioskGroupAttendance();
                    groupAttendance.GroupId = attendance.GroupId.Value;
                    groupAttendance.GroupName = attendance.Group.Name;
                    groupAttendance.Schedules = new List<KioskScheduleAttendance>();
                    kioskLocationAttendance.Groups.Add( groupAttendance );
                }

                var scheduleAttendance = groupAttendance.Schedules.Where( s => s.ScheduleId == attendance.ScheduleId ).FirstOrDefault();
                if ( scheduleAttendance == null )
                {
                    scheduleAttendance = new KioskScheduleAttendance();
                    scheduleAttendance.ScheduleId = attendance.ScheduleId.Value;
                    scheduleAttendance.ScheduleName = attendance.Schedule.Name;
                    scheduleAttendance.PersonIds = new List<int>();
                    groupAttendance.Schedules.Add( scheduleAttendance );
                }

                scheduleAttendance.PersonIds.Add( attendance.PersonId.Value );
            }
        }
        /// <summary>
        /// Reads the specified id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static KioskLocationAttendance Read( int id )
        {
            string cacheKey = KioskLocationAttendance.CacheKey( id );

            ObjectCache cache = MemoryCache.Default;
            KioskLocationAttendance locationAttendance = cache[cacheKey] as KioskLocationAttendance;

            if ( locationAttendance != null )
            {
                return locationAttendance;
            }
            else
            {
                var rockContext = new Rock.Data.RockContext();
                var location = new LocationService(rockContext).Get( id );
                if ( location != null )
                {
                    locationAttendance = new KioskLocationAttendance();
                    locationAttendance.LocationId = location.Id;
                    locationAttendance.LocationName = location.Name;
                    locationAttendance.Groups = new List<KioskGroupAttendance>();

                    var attendanceService = new AttendanceService(rockContext);
                    foreach ( var attendance in attendanceService.GetByDateAndLocation( RockDateTime.Today, location.Id ) )
                    {
                        AddAttendanceRecord( locationAttendance, attendance );
                    }

                    var cachePolicy = new CacheItemPolicy();
                    cachePolicy.AbsoluteExpiration = DateTimeOffset.Now.AddSeconds( 60 );
                    cache.Set( cacheKey, locationAttendance, cachePolicy );

                    return locationAttendance;
                }
            }

            return null;
        }