Exemple #1
0
        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 => OccurrenceCache.GetVolunteerOccurrences().Select(oc => oc.GroupId).Contains(o.GroupId ?? 0)).FirstOrDefault();

            if (volunteerOccurrences != null)
            {
                return(volunteerOccurrences);
            }
            return(null);
        }
Exemple #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 = OccurrenceCache.GetVolunteerOccurrences().Select(o => o.GroupId).ToList();

            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();
        }