Example #1
0
        public static void CloneAttendance(Attendance attendance, bool isSubroom, Location location, AttendanceService attendanceService, Request req)
        {
            Attendance newAttendance = ( Attendance )attendance.Clone();

            newAttendance.Id               = 0;
            newAttendance.Guid             = new Guid();
            newAttendance.StartDateTime    = Rock.RockDateTime.Now;
            newAttendance.EndDateTime      = null;
            newAttendance.DidAttend        = true;
            newAttendance.Device           = null;
            newAttendance.SearchTypeValue  = null;
            newAttendance.LocationId       = req.LocationId;
            newAttendance.AttendanceCode   = null;
            newAttendance.AttendanceCodeId = attendance.AttendanceCodeId;
            if (isSubroom)
            {
                newAttendance.ForeignId = location.Id;
            }
            else
            {
                newAttendance.ForeignId = null;
            }
            attendanceService.Add(newAttendance);
            var stayedFifteenMinutes = (Rock.RockDateTime.Now - attendance.StartDateTime) > new TimeSpan(0, 15, 0);

            attendance.DidAttend   = stayedFifteenMinutes;
            attendance.EndDateTime = Rock.RockDateTime.Now;
            InMemoryPersonStatus.RemoveFromWorship(attendance.PersonAlias.PersonId);
            InMemoryPersonStatus.RemoveFromWithParent(attendance.PersonAlias.PersonId);
            CheckInCountCache.AddAttendance(newAttendance);
            CheckInCountCache.RemoveAttendance(attendance);
        }
        public Response Insert([FromBody] Request req)
        {
            try
            {
                RockContext       rockContext       = new RockContext();
                AttendanceService attendanceService = new AttendanceService(rockContext);
                LocationService   locationService   = new LocationService(rockContext);

                var location = locationService.Get(req.LocationId);
                if (location == null)
                {
                    return(new Response(false, "Could not find location", false));
                }

                var isSubroom = ValidationHelper.IsSubRoom(location);

                var person = ValidationHelper.TestPin(rockContext, req.PIN);

                if (person == null)
                {
                    return(new Response(false, "Person not authorized", false));
                }
                var newAttendance = new Attendance
                {
                    PersonAlias   = person.PrimaryAlias,
                    LocationId    = location.Id,
                    StartDateTime = Rock.RockDateTime.Now,
                    DidAttend     = true,
                };
                if (isSubroom)
                {
                    newAttendance.LocationId = location.ParentLocationId;
                    newAttendance.ForeignId  = location.Id;
                }
                attendanceService.Add(newAttendance);
                DataHelper.CloseActiveAttendances(rockContext, newAttendance, location, isSubroom);
                DataHelper.AddEntranceHistory(rockContext, location, newAttendance, isSubroom);
                CheckInCountCache.AddAttendance(newAttendance);
                rockContext.SaveChanges();

                return(DataHelper.GetEntryResponse(rockContext, person, location));
            }
            catch (Exception e)
            {
                ExceptionLogService.LogException(e, System.Web.HttpContext.Current);
                return(new Response(false, "An error occured", false));
            }
        }
        public static void CloneAttendance(Attendance attendance, bool isSubroom, Location location, AttendanceService attendanceService, Request req)
        {
            Attendance newAttendance = null;

            if (!isSubroom)
            {
                newAttendance = attendanceService.AddOrUpdate(attendance.PersonAliasId, attendance.StartDateTime.Date, attendance.Occurrence.GroupId,
                                                              location.Id, attendance.Occurrence.ScheduleId, location.CampusId,
                                                              null, null, null, null, null, null);
            }
            else
            {
                newAttendance = attendanceService.AddOrUpdate(attendance.PersonAliasId, attendance.StartDateTime.Date, attendance.Occurrence.GroupId,
                                                              location.ParentLocationId, attendance.Occurrence.ScheduleId, location.CampusId,
                                                              null, null, null, null, null, null);
            }
            newAttendance.StartDateTime    = Rock.RockDateTime.Now;
            newAttendance.EndDateTime      = null;
            newAttendance.DidAttend        = true;
            newAttendance.AttendanceCodeId = attendance.AttendanceCodeId;
            if (isSubroom)
            {
                newAttendance.ForeignId = location.Id;
            }
            else
            {
                newAttendance.ForeignId = null;
            }
            attendanceService.Add(newAttendance);
            var stayedFifteenMinutes = (Rock.RockDateTime.Now - attendance.StartDateTime) > new TimeSpan(0, 15, 0);

            attendance.DidAttend   = stayedFifteenMinutes;
            attendance.EndDateTime = Rock.RockDateTime.Now;
            InMemoryPersonStatus.RemoveFromWorship(attendance.PersonAlias.PersonId);
            InMemoryPersonStatus.RemoveFromWithParent(attendance.PersonAlias.PersonId);
            CheckInCountCache.AddAttendance(newAttendance);
            CheckInCountCache.RemoveAttendance(attendance);
        }
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, Rock.Model.WorkflowAction action, Object entity, out List <string> errorMessages)
        {
            var checkInState = GetCheckInState(entity, out errorMessages);

            if (checkInState != null)
            {
                AttendanceCode attendanceCode = null;
                DateTime       startDateTime  = Rock.RockDateTime.Now;
                DateTime       today          = startDateTime.Date;
                DateTime       tomorrow       = startDateTime.AddDays(1);

                bool reuseCodeForFamily = checkInState.CheckInType != null && checkInState.CheckInType.ReuseSameCode;
                int  securityCodeLength = checkInState.CheckInType != null ? checkInState.CheckInType.SecurityCodeAlphaNumericLength : 3;

                var attendanceCodeService = new AttendanceCodeService(rockContext);
                var attendanceService     = new AttendanceService(rockContext);
                var groupMemberService    = new GroupMemberService(rockContext);
                var personAliasService    = new PersonAliasService(rockContext);

                var family = checkInState.CheckIn.CurrentFamily;
                if (family != null)
                {
                    foreach (var person in family.GetPeople(true))
                    {
                        if (reuseCodeForFamily && attendanceCode != null)
                        {
                            person.SecurityCode = attendanceCode.Code;
                        }
                        else
                        {
                            attendanceCode      = AttendanceCodeService.GetNew(securityCodeLength);
                            person.SecurityCode = attendanceCode.Code;
                        }

                        foreach (var groupType in person.GetGroupTypes(true))
                        {
                            foreach (var group in groupType.GetGroups(true))
                            {
                                if (groupType.GroupType.AttendanceRule == AttendanceRule.AddOnCheckIn &&
                                    groupType.GroupType.DefaultGroupRoleId.HasValue &&
                                    !groupMemberService.GetByGroupIdAndPersonId(group.Group.Id, person.Person.Id, true).Any())
                                {
                                    var groupMember = new GroupMember();
                                    groupMember.GroupId     = group.Group.Id;
                                    groupMember.PersonId    = person.Person.Id;
                                    groupMember.GroupRoleId = groupType.GroupType.DefaultGroupRoleId.Value;
                                    groupMemberService.Add(groupMember);
                                }

                                foreach (var location in group.GetLocations(true))
                                {
                                    foreach (var schedule in location.GetSchedules(true))
                                    {
                                        var primaryAlias = personAliasService.GetPrimaryAlias(person.Person.Id);
                                        if (primaryAlias != null)
                                        {
                                            // If a like attendance service exists close it before creating another one.
                                            var oldAttendance = attendanceService.Queryable()
                                                                .Where(a =>
                                                                       a.StartDateTime >= today &&
                                                                       a.StartDateTime < tomorrow &&
                                                                       a.Occurrence.LocationId == location.Location.Id &&
                                                                       a.Occurrence.ScheduleId == schedule.Schedule.Id &&
                                                                       a.Occurrence.GroupId == group.Group.Id &&
                                                                       a.PersonAlias.PersonId == person.Person.Id)
                                                                .FirstOrDefault();

                                            if (oldAttendance != null)
                                            {
                                                oldAttendance.EndDateTime = Rock.RockDateTime.Now;
                                                oldAttendance.DidAttend   = false;
                                            }
                                            var attendance = attendanceService.AddOrUpdate(primaryAlias.Id, startDateTime.Date, group.Group.Id,
                                                                                           location.Location.Id, schedule.Schedule.Id, location.CampusId,
                                                                                           checkInState.Kiosk.Device.Id, checkInState.CheckIn.SearchType.Id,
                                                                                           checkInState.CheckIn.SearchValue, family.Group.Id, attendanceCode.Id);

                                            attendance.DeviceId                 = checkInState.Kiosk.Device.Id;
                                            attendance.SearchTypeValueId        = checkInState.CheckIn.SearchType.Id;
                                            attendance.SearchValue              = checkInState.CheckIn.SearchValue;
                                            attendance.CheckedInByPersonAliasId = checkInState.CheckIn.CheckedInByPersonAliasId;
                                            attendance.SearchResultGroupId      = family.Group.Id;
                                            attendance.AttendanceCodeId         = attendanceCode.Id;
                                            attendance.StartDateTime            = startDateTime;
                                            attendance.EndDateTime              = null;
                                            attendance.Note      = group.Notes;
                                            attendance.DidAttend = groupType.GroupType.GetAttributeValue("SetDidAttend").AsBoolean();

                                            attendanceService.Add(attendance);
                                            CheckInCountCache.AddAttendance(attendance);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                rockContext.SaveChanges();
                return(true);
            }

            return(false);
        }