protected XMLTree GET(String resource) { string xml = GETxml(resource); XMLTree tree = new XMLTree(xml); String next = ""; do { try { next = tree.ChildTrees.Where(t => t.TagName.Equals("links")).Single().ChildNodes.Where(n => n.TagName.Equals("next")).Single().Value; next = next.Replace("amp%3Blimit=20&", ""); tree.ChildTrees.Remove(tree.ChildTrees.Where(t => t.TagName.Equals("links")).Single()); string nxml = ""; using (SchoologyAPICall newCall = new SchoologyAPICall()) { string rscr = next.Substring(next.IndexOf("v1")); nxml = newCall.GETxml(rscr); } tree = tree.MergeWith(new XMLTree(nxml)); } catch { next = ""; } } while (!next.Equals("")); return(tree); }
/// <summary> /// Download Attendance Data for a given section from schoology. /// </summary> /// <param name="sectionId">Schoology Section Id</param> /// <param name="period"></param> /// <returns></returns> public static List <SchoologyAttendance> Download(int sectionId, DateRange period = null) { XMLTree tree; using (SchoologyAPICall call = new SchoologyAPICall()) { tree = period == null?call.GetAttendances(sectionId) : call.GetAttendances(sectionId, period); } List <SchoologyAttendance> list = new List <SchoologyAttendance>(); tree = tree.Search("attendance"); foreach (XMLTree attendanceTag in tree.ChildTrees) { list.Add(new SchoologyAttendance(sectionId, attendanceTag)); } return(list); }
/// <summary> /// Get a Schoology Enrollment and map it to a Webhost User Id. /// </summary> /// <param name="section_id">Schoology Section Id</param> /// <param name="enrollment_id">Schoology Enrollment Id</param> public SchoologyEnrollment(int section_id, int enrollment_id) { XMLTree tree; using (SchoologyAPICall call = new SchoologyAPICall()) { try { tree = call.GetEnrollmentXML(section_id, enrollment_id); } catch (XMLException) { this.enrollment_id = enrollment_id; user_id = -1; return; } } this.enrollment_id = enrollment_id; user_id = Convert.ToInt32(tree.ChildNodes.Where(node => node.TagName.Equals("school_uid")).Single().Value); }
/// <summary> /// Get Course Data from Schoology to populate the SchoologyId Field. /// </summary> /// <returns>Report suitable for printing to logs.</returns> public static String GetCoursesFromSchoology() { XMLTree tree; String report = "Getting courses from schoology...\r\n"; WebhostEventLog.SchoologyLog.LogInformation("Getting courses from Schoology..."); using (SchoologyAPICall call = new SchoologyAPICall()) { tree = call.GetCourses(); } List <SchoologyCourseResponse> schoologyCourses = new List <SchoologyCourseResponse>(); foreach (XMLTree courseTree in tree.ChildTrees.Where(tr => tr.TagName.Equals("course"))) { SchoologyCourseResponse resp = new SchoologyCourseResponse(courseTree); if (resp.WebhostId != -1) { schoologyCourses.Add(new SchoologyCourseResponse(courseTree)); } else { report += String.Format("Disregarding Schoology Course with no Webhost Course Code:\r\n{0}\r\n", courseTree.ToString()); WebhostEventLog.SchoologyLog.LogWarning("Disregarding Schoology Course with no Webhost Course Code:\r\n{0}", courseTree.ToString()); } } using (WebhostEntities db = new WebhostEntities()) { foreach (SchoologyCourseResponse sc in schoologyCourses) { int cid = sc.WebhostId; if (db.Courses.Where(c => c.id == cid).Count() <= 0) { // put a placeholder in WebhostDB so to prevent conflicts. report += String.Format("Creating Placholder Course to prevent conflicts with \r\n{0}\r\n", sc.xml); WebhostEventLog.SchoologyLog.LogWarning("Creating Placholder Course to prevent conflicts with \r\n{0}", sc.xml); Course newCourse = new Course() { id = cid, BlackBaudID = "DEL", AcademicYearID = 2014, DepartmentID = sc.Department.Equals("") ? 0 : db.Departments.Where(d => d.Name.Equals(sc.Department)).Single().id, goesToSchoology = true, Name = sc.CourseName, SchoologyId = sc.SchoologyId, LengthInTerms = 0 }; db.Courses.Add(newCourse); } else { Course course = db.Courses.Where(c => c.id == cid).Single(); report += String.Format("Updating reference to {0} in Webhost with Schoology UID: {1}\r\n", course.Name, sc.SchoologyId); WebhostEventLog.SchoologyLog.LogInformation("Updating reference to {0} in Webhost with Schoology UID: {1}", course.Name, sc.SchoologyId); course.goesToSchoology = true; course.SchoologyId = sc.SchoologyId; } } try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { throw e; } catch (System.Data.Entity.Infrastructure.DbUpdateException e) { throw e; } } report += "Done!"; return(report); }
public static String GetSchoologySectionIdsForTerm(int TermId) { String report = "Getting Section Ides for term " + TermId + "\r\n"; WebhostEventLog.SchoologyLog.LogInformation("Getting Section Ides for term {0}", TermId); using (WebhostEntities db = new WebhostEntities()) { Term term = db.Terms.Where(t => t.id == TermId).Single(); List <Section> sections = term.Sections.ToList(); report += String.Format("Found {0} sections for {1} {2}\r\n", sections.Count, term.Name, term.StartDate.Year); WebhostEventLog.SchoologyLog.LogInformation("Found {0} sections for {1} {2}", sections.Count, term.Name, term.StartDate.Year); List <Course> courses = new List <Course>(); foreach (Section section in sections) { if (section.Course.goesToSchoology && !courses.Contains(section.Course)) { courses.Add(section.Course); } } report += String.Format("Found {0} courses for {1} {2}\r\n", courses.Count, term.Name, term.StartDate.Year); WebhostEventLog.SchoologyLog.LogInformation("Found {0} courses for {1} {2}", courses.Count, term.Name, term.StartDate.Year); foreach (Course course in courses) { if (course.SchoologyId <= 0) { report += String.Format("Course {0} webhost_id={1} schoology_id={2} cannot be querried.\r\n", course.Name, course.id, course.SchoologyId); WebhostEventLog.SchoologyLog.LogWarning("Course {0} webhost_id={1} schoology_id={2} cannot be querried.", course.Name, course.id, course.SchoologyId); continue; } report += String.Format("Querrying Schoology for course_id={0}\r\n", course.SchoologyId); WebhostEventLog.SchoologyLog.LogInformation("Querrying Schoology for course_id={0}", course.SchoologyId); XMLTree sectionsTree; using (SchoologyAPICall call = new SchoologyAPICall()) { sectionsTree = call.GetSectionsOf(course.SchoologyId); } foreach (XMLTree sectionTree in sectionsTree.ChildTrees.Where(t => t.TagName.Equals("section"))) { SchoologySectionResponse resp = new SchoologySectionResponse(sectionTree); if (resp.WebhostId == -1) { report += String.Format("Section does not have a Webhost ID:\r\n{0}\r\n", sectionTree.ToString()); WebhostEventLog.SchoologyLog.LogError("Section does not have a Webhost ID:\r\n{0}", sectionTree.ToString()); continue; } try { Section section = db.Sections.Where(sec => sec.id == resp.WebhostId).Single(); section.SchoologyId = resp.SchoologyId; report += String.Format("Updated [{0}] {1} with schoology_id={2}\r\n", section.Block.LongName, section.Course.Name, resp.SchoologyId); WebhostEventLog.SchoologyLog.LogInformation("Updated [{0}] {1} with schoology_id={2}", section.Block.LongName, section.Course.Name, resp.SchoologyId); } catch (Exception e) { report += String.Format("Failed to locate section with Id {0}. Error: {1}\r\n", resp.WebhostId, e.Message); WebhostEventLog.SchoologyLog.LogError("Failed to locate section with Id {0}. Error: {1}\r\n", resp.WebhostId, e.Message); } } } db.SaveChanges(); } return(report); }