static void PullSchoologyUserIds() { using (WebhostEntities db = new WebhostEntities()) { using (SchoologyAPICall call = new SchoologyAPICall()) { XMLTree userList = call.ListUsers(); foreach (XMLTree userTree in userList.ChildTrees.Where(u => u.TagName.Equals("user")).ToList()) { int webhost_id; try { webhost_id = Convert.ToInt32(userTree.ChildNodes.Where(t => t.TagName.Equals("school_uid")).Single().Value); } catch { WebhostEventLog.SchoologyLog.LogWarning("Skipping user with no school_uid. schoology id = {0}", userTree.ChildNodes.Where(t => t.TagName.Equals("id")).Single().Value); Console.WriteLine("Skipping user with no school_uid. email: {0}", userTree.ChildNodes.Where(t => t.TagName.Equals("primary_email")).Single().Value); continue; } int schoology_id = Convert.ToInt32(userTree.ChildNodes.Where(t => t.TagName.Equals("id")).Single().Value); Faculty faculty = db.Faculties.Find(webhost_id); if (faculty != null) { if (faculty.SchoologyId != schoology_id) { WebhostEventLog.SchoologyLog.LogInformation("Updating Schoology User Id for {0} {1} [{2}, {3}]", faculty.FirstName, faculty.LastName, faculty.ID, schoology_id); Console.WriteLine("Updating Schoology User Id for {0} {1} [{2}, {3}]", faculty.FirstName, faculty.LastName, faculty.ID, schoology_id); faculty.SchoologyId = schoology_id; } else { Console.WriteLine("{0} {1} has their correct Schoology Id already. schoology id: {2}", faculty.FirstName, faculty.LastName, schoology_id); } } else { Student student = db.Students.Find(webhost_id); if (student == null) // unknown webhost_id? { WebhostEventLog.SchoologyLog.LogWarning("No Webhost user corresponds to email: {0}", userTree.ChildNodes.Where(t => t.TagName.Equals("primary_email")).Single().Value); Console.WriteLine("No Webhost user corresponds to email: {0}", userTree.ChildNodes.Where(t => t.TagName.Equals("primary_email")).Single().Value); continue; } if (student.SchoologyId != schoology_id) { WebhostEventLog.SchoologyLog.LogInformation("Updating Schoology User Id for {0} {1} [{2}, {3}]", student.FirstName, student.LastName, student.ID, schoology_id); student.SchoologyId = schoology_id; } else { Console.WriteLine("{0} {1} already has their correct schoology id: {2}", student.FirstName, student.LastName, schoology_id); } } //Thread.Sleep(250); } db.SaveChanges(); } } }
static void CheckSectionRosters(int ay = 2017) { using (WebhostEntities db = new WebhostEntities()) { foreach (Section section in db.Sections.Where(s => s.Course.AcademicYearID == ay && s.Course.goesToSchoology).ToList()) { Console.WriteLine("[{0}] {1}:", section.Block.LongName, section.Course.Name); if (section.Course.SchoologyId <= 0) { // create course using (SchoologyAPICall call = new SchoologyAPICall()) { XMLTree response; try { response = call.CreateItem(new SchoologyCourse(section.Course.id)); } catch (XMLException e) { Console.WriteLine(e.Message); continue; } section.Course.SchoologyId = Convert.ToInt32(response.ChildNodes.Where(n => n.TagName.Equals("id")).Single().Value); WebhostEventLog.SchoologyLog.LogInformation("Created new Schoology Course {0} : {1}", section.Course.Name, section.Course.SchoologyId); Console.WriteLine("Created new Schoology Course {0} : {1}", section.Course.Name, section.Course.SchoologyId); db.SaveChanges(); } } if (section.SchoologyId <= 0) { if (section.Terms.Count == 0) { Console.WriteLine("This section has no Terms assigned.... I'm deleting it."); WebhostEventLog.Syslog.LogInformation("Deleting section with no terms assigned [{0}] {1}", section.Block.LongName, section.Course.Name); section.Students.Clear(); section.Teachers.Clear(); section.AttendanceSubmissionStatuses.Clear(); db.Sections.Remove(section); try { db.SaveChanges(); } catch (Exception e) { String message = e.Message; while (e.InnerException != null) { e = e.InnerException; message = e.Message; } Console.WriteLine("Failed to save database changes. {0}", message); } continue; } using (SchoologyAPICall call = new SchoologyAPICall()) { XMLTree response; try { response = call.CreateItem(new SchoologySection(section.id)); } catch (XMLException e) { Console.WriteLine(e.Message); continue; } section.SchoologyId = Convert.ToInt32(response.ChildNodes.Where(n => n.TagName.Equals("id")).Single().Value); WebhostEventLog.SchoologyLog.LogInformation("Created new Schoology Section [{0}] {1} : {2}", section.Block.LongName, section.Course.Name, section.SchoologyId); Console.WriteLine("Created new Schoology Section [{0}] {1} : {2}", section.Block.LongName, section.Course.Name, section.SchoologyId); db.SaveChanges(); } } if (section.Students.Count <= 0 || section.Teachers.Count <= 0) { Console.WriteLine("This section has no students or no teachers..."); section.Students.Clear(); section.Teachers.Clear(); section.Terms.Clear(); using (SchoologyAPICall call = new SchoologyAPICall()) { call.DeleteSection(section.SchoologyId); WebhostEventLog.SchoologyLog.LogInformation("Deleted superfluous section: [{0}] {1}", section.Block.LongName, section.Course.Name); Console.WriteLine("Deleted Section from Schoology."); } section.SchoologyId = -1; try { db.SaveChanges(); } catch { Console.WriteLine("Failed to save changes to database."); } continue; } List <int> schoologyIds; List <SchoologyEnrollment> enrollments; try { using (SchoologyAPICall call = new SchoologyAPICall()) enrollments = call.GetSectionEnrollments(section.SchoologyId).ToList(); schoologyIds = enrollments.Select(u => u.user_id).ToList(); } catch (SchoologyAPICall.SchoologyAPIException ex) { Console.WriteLine(ex.Message); continue; } List <int> webhostIds = section.Students.Select(s => s.ID).ToList(); webhostIds.AddRange(section.Teachers.Select(t => t.ID).ToList()); foreach (int id in schoologyIds) { if (!webhostIds.Contains(id)) { String name; Student student = db.Students.Find(id); if (student == null) { Faculty teacher = db.Faculties.Find(id); name = String.Format("{0} {1}", teacher.FirstName, teacher.LastName); } else { name = String.Format("{0} {1} [2]", student.FirstName, student.LastName); } Console.WriteLine("Schoology Section has extra enrollment for {0}.", name); using (SchoologyAPICall call = new SchoologyAPICall()) { call.DeleteEnrollment(section.SchoologyId, enrollments.Where(enr => enr.user_id == id).Select(enr => enr.enrollment_id).Single()); } } } foreach (int id in webhostIds) { if (!schoologyIds.Contains(id)) { String name; Student student = db.Students.Find(id); int schoologyId = 0; if (student == null) { Faculty teacher = db.Faculties.Find(id); schoologyId = teacher.SchoologyId; name = String.Format("{0} {1}", teacher.FirstName, teacher.LastName); } else { name = String.Format("{0} {1} [2]", student.FirstName, student.LastName, student.GraduationYear); schoologyId = student.SchoologyId; } Console.WriteLine("Schoology Section is missing enrollment for {0}.", name); using (SchoologyAPICall call = new SchoologyAPICall()) { try { XMLTree tree = call.CreateEnrollment(section.SchoologyId, schoologyId, student == null); } catch (XMLException e) { Console.WriteLine(e.Message); } } } } } } }