public void AddAttendanceNoPerson() { int groupId = 111; int locationId = 14; int scheduleId = 19; DateTime occurrenceDate = new DateTime(2019, 8, 5); int? personId = null; int? personAliasId = null; var attendancesController = new AttendancesController(); Rock.Model.Attendance attendance = new Rock.Model.Attendance(); System.Web.Http.HttpResponseException exception = null; try { attendance = attendancesController.AddAttendance(groupId, locationId, scheduleId, occurrenceDate, personId, personAliasId); } catch (System.Web.Http.HttpResponseException ex) { exception = ex; } finally { Assert.IsTrue(exception.Response.StatusCode == System.Net.HttpStatusCode.BadRequest); } }
protected void btnRecordAttendance_Click(object sender, EventArgs e) { if (!dpAttendedDate.SelectedDateTimeIsBlank) { var attendendPeopleIds = new List <int>(); foreach (RepeaterItem item in rptAttendees.Items) { var cb = item.FindControl("didAttend") as CheckBox; if (cb.Checked) { var personId = item.FindControl("personId") as HiddenField; attendendPeopleIds.Add(Int32.Parse(personId.Value)); } } if (attendendPeopleIds.Count > 0) { var attendanceService = new AttendanceService(ctx); var peopleService = new PersonService(ctx); var people = peopleService.GetByIds(attendendPeopleIds); var attendances = new List <Rock.Model.Attendance>(); foreach (Person person in people) { var attendance = new Rock.Model.Attendance(); attendance.PersonAlias = person.PrimaryAlias; attendance.Group = _group; attendance.DidAttend = true; // ADD GROUP LOCATION ? attendance.StartDateTime = (DateTime)dpAttendedDate.SelectedDateTime; if (attendance.IsValid) { attendanceService.Add(attendance); attendances.Add(attendance); } } ctx.SaveChanges(); FlashMessage(string.Format( "Attendance Recorded for {1} people on {0}", dpAttendedDate.SelectedDateTime.Value.ToShortTimeString(), attendendPeopleIds.Count ), NotificationBoxType.Success); resetCheckBoxes(); BindGrid(); } else { FlashMessage("Please select at least one Attendee", NotificationBoxType.Danger); } } else { FlashMessage("Attended Date is required", NotificationBoxType.Danger); } }
/// <summary> /// Maps the attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> private void MapAttendance( IQueryable<Row> tableData ) { foreach ( var row in tableData ) { DateTime? startTime = row["Start_Date_time"] as DateTime?; if ( startTime != null ) { var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.StartDateTime = (DateTime)startTime; attendance.DidAttend = true; string position = row["CheckedInAs"] as string; string jobTitle = row["Job_Title"] as string; string machineName = row["Checkin_Machine_Name"] as string; int? rlcId = row["RLC_ID"] as int?; // look up location, schedule, group, and device int? individualId = row["Individual_ID"] as int?; if ( individualId != null ) { attendance.PersonId = GetPersonId( individualId ); } DateTime? checkInTime = row["Check_In_Time"] as DateTime?; if ( checkInTime != null ) { // set the start time to the time they actually checked in attendance.StartDateTime = (DateTime)checkInTime; } DateTime? checkOutTime = row["Check_Out_Time"] as DateTime?; if ( checkOutTime != null ) { attendance.EndDateTime = (DateTime)checkOutTime; } string f1AttendanceCode = row["Tag_Code"] as string; if ( f1AttendanceCode != null ) { attendance.AttendanceCode = new AttendanceCode(); attendance.AttendanceCode.Code = f1AttendanceCode; } // Other Attributes to create: // Tag_Comment // BreakoutGroup_Name // Pager_Code RockTransactionScope.WrapTransaction( () => { var attendanceService = new AttendanceService(); attendanceService.Add( attendance, ImportPersonAlias ); attendanceService.Save( attendance, ImportPersonAlias ); } ); } } }
/// <summary> /// Maps the attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> private void MapAttendance(IQueryable <Row> tableData) { var lookupContext = new RockContext(); foreach (var row in tableData) { DateTime?startTime = row["Start_Date_time"] as DateTime?; if (startTime != null) { var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.StartDateTime = (DateTime)startTime; attendance.DidAttend = true; string position = row["CheckedInAs"] as string; string jobTitle = row["Job_Title"] as string; string machineName = row["Checkin_Machine_Name"] as string; int? rlcId = row["RLC_ID"] as int?; // look up location, schedule, group, and device int?individualId = row["Individual_ID"] as int?; if (individualId != null) { attendance.PersonAliasId = GetPersonAliasId(individualId); } DateTime?checkInTime = row["Check_In_Time"] as DateTime?; if (checkInTime != null) { // set the start time to the time they actually checked in attendance.StartDateTime = (DateTime)checkInTime; } DateTime?checkOutTime = row["Check_Out_Time"] as DateTime?; if (checkOutTime != null) { attendance.EndDateTime = (DateTime)checkOutTime; } string f1AttendanceCode = row["Tag_Code"] as string; if (!string.IsNullOrWhiteSpace(f1AttendanceCode)) { attendance.AttendanceCode = new AttendanceCode(); attendance.AttendanceCode.Code = f1AttendanceCode; } // Other Attributes to create: // Tag_Comment // BreakoutGroup_Name // Pager_Code SaveAttendance(attendance); } } }
/// <summary> /// Saves the attendance. /// </summary> /// <param name="attendance">The attendance.</param> private static void SaveAttendance(Rock.Model.Attendance attendance) { var rockContext = new RockContext(); rockContext.WrapTransaction(() => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attendances.Add(attendance); rockContext.SaveChanges(DisableAudit); }); }
/// <summary> /// Maps the attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> //private DateTime? StartDateTime { get; set;} private void MapAttendance( IQueryable<Row> tableData ) { var lookupContext = new RockContext(); int completed = 0; int totalRows = tableData.Count(); int percentage = ( totalRows - 1 ) / 100 + 1; ReportProgress( 0, string.Format( "Verifying Attendance import ({0:N0} found).", totalRows ) ); var attendanceList = new List<Rock.Model.Attendance>(); var groupService = new GroupService( lookupContext ); var existingGroupList = new List<Group>(); existingGroupList = groupService.Queryable().ToList(); foreach ( var row in tableData ) { DateTime? startTime = row["Start_Date_Time"] as DateTime?; if ( startTime != null && startTime != DateTime.MinValue) { DateTime startDateTime = (DateTime)startTime; if ( startDateTime.Year == 2014 && startDateTime.Month >= 1 && startDateTime.Month <= 8 ) { //startDateTime = BruteForceDateTime(startTime); var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.ModifiedByPersonAliasId = ImportPersonAlias.Id; attendance.CreatedDateTime = DateTime.Today; attendance.ModifiedDateTime = DateTime.Today; attendance.StartDateTime = startDateTime; //(DateTime)startTime; attendance.DidAttend = true; attendance.CampusId = 1; //Campus is needed for attendance to show in attendance analysis. //string position = row["CheckedInAs"] as string; //string jobTitle = row["Job_Title"] as string; //string machineName = row["Checkin_Machine_Name"] as string; int? rlcId = row["RLC_ID"] as int?; int? individualId = row["Individual_ID"] as int?; if ( individualId != null ) { attendance.PersonAliasId = GetPersonAliasId( individualId ); } DateTime? checkInTime = row["Check_In_Time"] as DateTime?; if ( checkInTime != null ) { // set the start time to the time they actually checked in. If null it maintains Start_Date_Time attendance.StartDateTime = (DateTime)checkInTime; //BruteForceDateTime( checkInTime ); } DateTime? checkOutTime = row["Check_Out_Time"] as DateTime?; if ( checkOutTime != null ) { attendance.EndDateTime = (DateTime)checkOutTime; //BruteForceDateTime( checkOutTime ); } //string f1AttendanceCode = row["Tag_Code"] as string; //if ( f1AttendanceCode != null ) //{ // attendance.AttendanceCode = new Rock.Model.AttendanceCode(); // attendance.AttendanceCode.Code = f1AttendanceCode; //} string f1AttendanceCheckedInAs = row["CheckedInAs"] as string; if ( f1AttendanceCheckedInAs != null ) { attendance.Note = f1AttendanceCheckedInAs; } // look up location, schedule, and device -- all of these fields can be null if need be attendance.LocationId = GetLocationId( Convert.ToInt32( rlcId ) ); //look up Group Group rlcGroup = existingGroupList.Where( g => g.ForeignId == ( rlcId.ToString() ) ).FirstOrDefault(); if ( rlcGroup != null ) { attendance.GroupId = rlcGroup.Id; } var dvService = new DefinedValueService( lookupContext ); attendance.SearchTypeValueId = dvService.Queryable().Where( dv => dv.Value == "Phone Number" ).FirstOrDefault().Id; //ReportProgress( 0, string.Format( "{0},{1},{2},{3},{4},{5},{6},{7},{8}", individualId,rlcId,rlcGroup.Name,attendance.CreatedByPersonAliasId,attendance.ModifiedByPersonAliasId,attendance.StartDateTime,attendance.DidAttend,attendance.AttendanceCode,attendance.LocationId ) ); //look into creating DeviceIds and Locations (Generic) // Other Attributes to create: // Tag_Comment // BreakoutGroup_Name // Pager_Code //attendanceList.Add( attendance ); completed++; if ( completed % percentage < 1 ) { int percentComplete = completed / percentage; ReportProgress( percentComplete, string.Format( "Completed: {0:N0} Percent Completed: {0:N0} ", completed, percentComplete ) ); } // else if ( completed % ReportingNumber < 1 ) // { // var rockContext = new RockContext(); // rockContext.WrapTransaction( () => //{ // rockContext.Configuration.AutoDetectChangesEnabled = false; // rockContext.Attendances.AddRange( attendanceList ); // rockContext.SaveChanges( DisableAudit ); //} ); // ReportPartialProgress(); // } var rockContext = new RockContext(); rockContext.WrapTransaction( () => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attendances.Add( attendance ); rockContext.SaveChanges( DisableAudit ); } ); ReportPartialProgress(); } } } }
/// <summary> /// Maps the group attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> //private DateTime? StartDateTime { get; set;} private void MapGroupsAttendance(IQueryable <Row> tableData) { var lookupContext = new RockContext(); int completed = 0; int totalRows = tableData.Count(); int percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, string.Format("Verifying Attendance import ({0:N0} found).", totalRows)); var attendanceList = new List <Rock.Model.Attendance>(); var groupService = new GroupService(lookupContext); var existingGroupList = new List <Group>(); existingGroupList = groupService.Queryable().Where(g => g.ForeignId != null).ToList(); foreach (var row in tableData) { DateTime?startTime = row["StartDateTime"] as DateTime?; if (startTime != null && startTime != DateTime.MinValue) { int?groupId = row["GroupID"] as int?; if (existingGroupList.Find(g => g.ForeignId == groupId.ToString()) != null) //making sure the group has been created. { DateTime startDateTime = (DateTime)startTime; DateTime?endTime = row["EndDateTime"] as DateTime?; int? met = row["Met"] as int?; bool didAttend = true; if (met == 0) { didAttend = false; } string comments = row["Comments"] as string; var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.ModifiedByPersonAliasId = ImportPersonAlias.Id; attendance.CreatedDateTime = DateTime.Today; attendance.ModifiedDateTime = DateTime.Today; attendance.StartDateTime = startDateTime; attendance.EndDateTime = endTime; attendance.DidAttend = didAttend; attendance.Note = comments; attendance.CampusId = 1; //Campus is needed for attendance to show in attendance analysis. attendance.GroupId = existingGroupList.Find(g => g.ForeignId == groupId.ToString()).Id; int?individualId = row["IndividualID"] as int?; if (individualId != null) { attendance.PersonAliasId = GetPersonAliasId(individualId); } ////look up Group //int? groupId = row["GroupID"] as int?; //Group group = existingGroupList.Where( g => g.ForeignId == ( groupId.ToString() ) ).FirstOrDefault(); //if ( group != null ) //{ // attendance.GroupId = group.Id; //} //no other search type right now; small group attendance not currently set up in Rock. var dvService = new DefinedValueService(lookupContext); attendance.SearchTypeValueId = dvService.Queryable().Where(dv => dv.Value == "Phone Number").FirstOrDefault().Id; completed++; if (completed % percentage < 1) { int percentComplete = completed / percentage; ReportProgress(percentComplete, string.Format("Completed: {0:N0} Percent Completed: {0:N0} ", completed, percentComplete)); } var rockContext = new RockContext(); rockContext.WrapTransaction(() => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attendances.Add(attendance); rockContext.SaveChanges(DisableAudit); }); ReportPartialProgress(); } } } }
/// <summary> /// Maps the group attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> //private DateTime? StartDateTime { get; set;} private void MapGroupsAttendance( IQueryable<Row> tableData ) { var lookupContext = new RockContext(); int completed = 0; int totalRows = tableData.Count(); int percentage = ( totalRows - 1 ) / 100 + 1; ReportProgress( 0, string.Format( "Verifying Attendance import ({0:N0} found).", totalRows ) ); var attendanceList = new List<Rock.Model.Attendance>(); var groupService = new GroupService( lookupContext ); var existingGroupList = new List<Group>(); existingGroupList = groupService.Queryable().Where(g => g.ForeignId != null).ToList(); foreach ( var row in tableData ) { DateTime? startTime = row["StartDateTime"] as DateTime?; if ( startTime != null && startTime != DateTime.MinValue ) { int? groupId = row["GroupID"] as int?; if ( existingGroupList.Find(g => g.ForeignId == groupId.ToString()) != null ) //making sure the group has been created. { DateTime startDateTime = (DateTime)startTime; DateTime? endTime = row["EndDateTime"] as DateTime?; int? met = row["Met"] as int?; bool didAttend = true; if ( met == 0 ) { didAttend = false; } string comments = row["Comments"] as string; var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.ModifiedByPersonAliasId = ImportPersonAlias.Id; attendance.CreatedDateTime = DateTime.Today; attendance.ModifiedDateTime = DateTime.Today; attendance.StartDateTime = startDateTime; attendance.EndDateTime = endTime; attendance.DidAttend = didAttend; attendance.Note = comments; attendance.CampusId = 1; //Campus is needed for attendance to show in attendance analysis. attendance.GroupId = existingGroupList.Find( g => g.ForeignId == groupId.ToString() ).Id; int? individualId = row["IndividualID"] as int?; if ( individualId != null ) { attendance.PersonAliasId = GetPersonAliasId( individualId ); } ////look up Group //int? groupId = row["GroupID"] as int?; //Group group = existingGroupList.Where( g => g.ForeignId == ( groupId.ToString() ) ).FirstOrDefault(); //if ( group != null ) //{ // attendance.GroupId = group.Id; //} //no other search type right now; small group attendance not currently set up in Rock. var dvService = new DefinedValueService( lookupContext ); attendance.SearchTypeValueId = dvService.Queryable().Where( dv => dv.Value == "Phone Number" ).FirstOrDefault().Id; completed++; if ( completed % percentage < 1 ) { int percentComplete = completed / percentage; ReportProgress( percentComplete, string.Format( "Completed: {0:N0} Percent Completed: {0:N0} ", completed, percentComplete ) ); } var rockContext = new RockContext(); rockContext.WrapTransaction( () => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attendances.Add( attendance ); rockContext.SaveChanges( DisableAudit ); } ); ReportPartialProgress(); } } } }
/// <summary> /// Maps the attendance. /// </summary> /// <param name="tableData">The table data.</param> /// <returns></returns> //private DateTime? StartDateTime { get; set;} private void MapAttendance(IQueryable <Row> tableData) { var lookupContext = new RockContext(); int completed = 0; int totalRows = tableData.Count(); int percentage = (totalRows - 1) / 100 + 1; ReportProgress(0, string.Format("Verifying Attendance import ({0:N0} found).", totalRows)); var attendanceList = new List <Rock.Model.Attendance>(); var groupService = new GroupService(lookupContext); var existingGroupList = new List <Group>(); existingGroupList = groupService.Queryable().ToList(); foreach (var row in tableData) { DateTime?startTime = row["Start_Date_Time"] as DateTime?; if (startTime != null && startTime != DateTime.MinValue) { DateTime startDateTime = (DateTime)startTime; if (startDateTime.Year == 2014 && startDateTime.Month >= 1 && startDateTime.Month <= 8) { //startDateTime = BruteForceDateTime(startTime); var attendance = new Rock.Model.Attendance(); attendance.CreatedByPersonAliasId = ImportPersonAlias.Id; attendance.ModifiedByPersonAliasId = ImportPersonAlias.Id; attendance.CreatedDateTime = DateTime.Today; attendance.ModifiedDateTime = DateTime.Today; attendance.StartDateTime = startDateTime; //(DateTime)startTime; attendance.DidAttend = true; attendance.CampusId = 1; //Campus is needed for attendance to show in attendance analysis. //string position = row["CheckedInAs"] as string; //string jobTitle = row["Job_Title"] as string; //string machineName = row["Checkin_Machine_Name"] as string; int?rlcId = row["RLC_ID"] as int?; int?individualId = row["Individual_ID"] as int?; if (individualId != null) { attendance.PersonAliasId = GetPersonAliasId(individualId); } DateTime?checkInTime = row["Check_In_Time"] as DateTime?; if (checkInTime != null) { // set the start time to the time they actually checked in. If null it maintains Start_Date_Time attendance.StartDateTime = (DateTime)checkInTime; //BruteForceDateTime( checkInTime ); } DateTime?checkOutTime = row["Check_Out_Time"] as DateTime?; if (checkOutTime != null) { attendance.EndDateTime = (DateTime)checkOutTime; //BruteForceDateTime( checkOutTime ); } //string f1AttendanceCode = row["Tag_Code"] as string; //if ( f1AttendanceCode != null ) //{ // attendance.AttendanceCode = new Rock.Model.AttendanceCode(); // attendance.AttendanceCode.Code = f1AttendanceCode; //} string f1AttendanceCheckedInAs = row["CheckedInAs"] as string; if (f1AttendanceCheckedInAs != null) { attendance.Note = f1AttendanceCheckedInAs; } // look up location, schedule, and device -- all of these fields can be null if need be attendance.LocationId = GetLocationId(Convert.ToInt32(rlcId)); //look up Group Group rlcGroup = existingGroupList.Where(g => g.ForeignId == (rlcId.ToString())).FirstOrDefault(); if (rlcGroup != null) { attendance.GroupId = rlcGroup.Id; } var dvService = new DefinedValueService(lookupContext); attendance.SearchTypeValueId = dvService.Queryable().Where(dv => dv.Value == "Phone Number").FirstOrDefault().Id; //ReportProgress( 0, string.Format( "{0},{1},{2},{3},{4},{5},{6},{7},{8}", individualId,rlcId,rlcGroup.Name,attendance.CreatedByPersonAliasId,attendance.ModifiedByPersonAliasId,attendance.StartDateTime,attendance.DidAttend,attendance.AttendanceCode,attendance.LocationId ) ); //look into creating DeviceIds and Locations (Generic) // Other Attributes to create: // Tag_Comment // BreakoutGroup_Name // Pager_Code //attendanceList.Add( attendance ); completed++; if (completed % percentage < 1) { int percentComplete = completed / percentage; ReportProgress(percentComplete, string.Format("Completed: {0:N0} Percent Completed: {0:N0} ", completed, percentComplete)); } // else if ( completed % ReportingNumber < 1 ) // { // var rockContext = new RockContext(); // rockContext.WrapTransaction( () => //{ // rockContext.Configuration.AutoDetectChangesEnabled = false; // rockContext.Attendances.AddRange( attendanceList ); // rockContext.SaveChanges( DisableAudit ); //} ); // ReportPartialProgress(); // } var rockContext = new RockContext(); rockContext.WrapTransaction(() => { rockContext.Configuration.AutoDetectChangesEnabled = false; rockContext.Attendances.Add(attendance); rockContext.SaveChanges(DisableAudit); }); ReportPartialProgress(); } } } }
/// <summary> /// Adds or updates an attendance record and will create the occurrence if needed /// </summary> /// <param name="personAliasId">The person alias identifier.</param> /// <param name="checkinDateTime">The checkin date time.</param> /// <param name="groupId">The group identifier.</param> /// <param name="locationId">The location identifier.</param> /// <param name="scheduleId">The schedule identifier.</param> /// <param name="campusId">The campus identifier.</param> /// <param name="deviceId">The device identifier.</param> /// <param name="searchTypeValueId">The search type value identifier.</param> /// <param name="searchValue">The search value.</param> /// <param name="searchResultGroupId">The search result group identifier.</param> /// <param name="attendanceCodeId">The attendance code identifier.</param> /// <param name="checkedInByPersonAliasId">The checked in by person alias identifier.</param> /// <returns></returns> public Attendance AddOrUpdate(int?personAliasId, DateTime checkinDateTime, int?groupId, int?locationId, int?scheduleId, int?campusId, int?deviceId, int?searchTypeValueId, string searchValue, int?searchResultGroupId, int?attendanceCodeId, int?checkedInByPersonAliasId) { // Check to see if an occurrence exists already var occurrenceService = new AttendanceOccurrenceService((RockContext)Context); var occurrence = occurrenceService.Get(checkinDateTime.Date, groupId, locationId, scheduleId); if (occurrence == null) { // If occurrence does not yet exists, use a new context and create it using (var newContext = new RockContext()) { occurrence = new AttendanceOccurrence { OccurrenceDate = checkinDateTime.Date, GroupId = groupId, LocationId = locationId, ScheduleId = scheduleId, }; var newOccurrenceService = new AttendanceOccurrenceService(newContext); newOccurrenceService.Add(occurrence); newContext.SaveChanges(); // Query for the new occurrence using original context. occurrence = occurrenceService.Get(occurrence.Id); } } // If we still don't have an occurrence record (i.e. validation failed) return null if (occurrence == null) { return(null); } // Query for existing attendance record Attendance attendance = null; if (personAliasId.HasValue) { attendance = occurrence.Attendees .FirstOrDefault(a => a.PersonAliasId.HasValue && a.PersonAliasId.Value == personAliasId.Value); } // If an attendance record doesn't exist for the occurrence, add a new record if (attendance == null) { attendance = ((RockContext)Context).Attendances.Create(); { attendance.Occurrence = occurrence; attendance.OccurrenceId = occurrence.Id; attendance.PersonAliasId = personAliasId; }; Add(attendance); } // Update details of the attendance (do not overwrite an existing value with an empty value) if (campusId.HasValue) { attendance.CampusId = campusId.Value; } if (deviceId.HasValue) { attendance.DeviceId = deviceId.Value; } if (searchTypeValueId.HasValue) { attendance.SearchTypeValueId = searchTypeValueId; } if (searchValue.IsNotNullOrWhiteSpace()) { attendance.SearchValue = searchValue; } if (checkedInByPersonAliasId.HasValue) { attendance.CheckedInByPersonAliasId = checkedInByPersonAliasId.Value; } if (searchResultGroupId.HasValue) { attendance.SearchResultGroupId = searchResultGroupId; } if (attendanceCodeId.HasValue) { attendance.AttendanceCodeId = attendanceCodeId; } attendance.StartDateTime = checkinDateTime; attendance.DidAttend = true; return(attendance); }