public static String SyncAttendanceForSection(int webhost_section_id, DateRange period = null) { String report = ""; using (WebhostEntities db = new WebhostEntities()) { Section section = db.Sections.Where(sec => sec.id == webhost_section_id).Single(); int schoology_section_id = section.SchoologyId; int attmarkid = db.AttendanceMarkings.Count() > 0 ? db.AttendanceMarkings.OrderBy(att => att.id).ToList().Last().id : 0; List <SchoologyAttendance> schoologyAttendances = SchoologyAttendance.Download(schoology_section_id, period); report += String.Format("Found {0} Attendance Markings{1}", schoologyAttendances.Count, Environment.NewLine); foreach (SchoologyAttendance sch_att in schoologyAttendances) { AttendanceMarking marking; if (section.AttendanceMarkings.Where(mk => mk.AttendanceDate.Date.Equals(sch_att.date) && mk.StudentID == sch_att.enrollment.user_id).Count() <= 0) { marking = new AttendanceMarking() { id = ++attmarkid, AttendanceDate = sch_att.date, StudentID = sch_att.enrollment.user_id, SectionIndex = webhost_section_id, MarkingIndex = AttendanceControl.LookupAttendanceIdByName(sch_att.AttendanceMarking), Notes = sch_att.Notes, SubmissionTime = DateTime.Now, SubmittedBy = 9997 }; db.AttendanceMarkings.Add(marking); Student student = db.Students.Where(s => s.ID == sch_att.enrollment.user_id).Single(); GradeTableEntry entry = db.GradeTableEntries.Where(e => e.id == marking.MarkingIndex).Single(); report += String.Format("Created new attendance [{3}] for {0} {1} on {2}{4}", student.FirstName, student.LastName, marking.AttendanceDate.ToShortDateString(), entry.Name, Environment.NewLine); } else { marking = section.AttendanceMarkings.Where(mk => mk.AttendanceDate.Date.Equals(sch_att.date) && mk.StudentID == sch_att.enrollment.user_id).Single(); if (marking.MarkingIndex == AttendanceControl.LookupAttendanceIdByName("Excused")) { report += String.Format("Skipping previously excused absence for {0} {1} on {3}{2}", marking.Student.FirstName, marking.Student.LastName, Environment.NewLine, marking.AttendanceDate.ToShortDateString()); continue; // Do not overwrite an excused absence. } marking.MarkingIndex = AttendanceControl.LookupAttendanceIdByName(sch_att.AttendanceMarking); marking.Notes = sch_att.Notes; marking.SubmissionTime = DateTime.Now; report += String.Format("Updated attendance [{4}] for {0} {1} on {3}{2}", marking.Student.FirstName, marking.Student.LastName, Environment.NewLine, marking.AttendanceDate.ToShortDateString(), marking.Marking.Name); } } db.SaveChanges(); } return(report); }