public JsonResult LookUpSISPeriods(int crsuniq, int funiq) { //calculate the school year int schoolYear = Convert.ToInt32(SISdb.schools.Max(s => s.schyear)); List <SISPeriodModel> sisPeriods = new List <SISPeriodModel>(); var periods = SISdb.mstscheds.Where(m => m.funiq == funiq && m.trkcr.crsuniq == crsuniq && m.trkcr.track.schyear == schoolYear); if (periods.Any()) { foreach (mstsched period in periods) { //check to see if it has a canvas section CanvasSectionModel section = GetSectionBySISId(schoolYear + ":" + period.mstuniq + ":" + funiq); SISPeriodModel sisPeriod = new SISPeriodModel { mstuniq = period.mstuniq, period = Convert.ToInt32(period.mstmeets.First().periodn), termc = period.mstmeets.First().termc, funiq = Convert.ToInt32(period.funiq), section = section }; sisPeriods.Add(sisPeriod); } } return(Json(new { success = "true", periods = sisPeriods })); }
private CanvasSectionModel GetSectionBySISId(string sectionId) { CanvasSectionModel section = new CanvasSectionModel(); try { using (var client = new WebClient()) { string getCourseURL = CanvasUrl + "sections/sis_section_id:" + sectionId + "?access_token=" + OAuthKey; var json = client.DownloadString(getCourseURL); var serializer = new JavaScriptSerializer(); section = serializer.Deserialize <CanvasSectionModel>(json); return(section); } } catch (Exception e) { //EmailSender.EmailSender.sendImportantMessage("*****@*****.**", Globals.EMAIL_ADDRESSES,"Error getting section: " + sectionId, "Exception: " + e.Message); return(null); } }
public JsonResult CreateCourseSection(string courseId, string mstUniq) { mstsched period = SISdb.mstscheds.Find(Convert.ToInt32(mstUniq)); //calculate the school year int schoolYear = Convert.ToInt32(SISdb.schools.Max(s => s.schyear)); if (period != null) { //check to see that the section isn't already created CanvasSectionModel section = GetSectionBySISId(schoolYear + ":" + period.mstuniq + ":" + period.funiq); if (section != null) { return(Json(new { success = "true", sectionId = section.id })); } //get the course CanvasCourseModel course = GetCourseById(courseId); if (course != null) { string sectionJson = "{ \"course_section\": {" + "\"name\": \"" + period.trkcr.course.descript + " : " + period.facdemo.lastname.Trim() + " " + period.facdemo.firstname.Trim() + " : " + period.mstmeets.First().periodn + "\"," + "\"sis_section_id\": \"" + schoolYear + ":" + period.mstuniq + ":" + period.funiq + "\"}}"; string createSectionUrl = CanvasUrl + "courses/" + courseId + "/sections?access_token=" + OAuthKey; var httpWebRequest = (HttpWebRequest)WebRequest.Create(createSectionUrl); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; CanvasSectionModel newSection = new CanvasSectionModel(); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(sectionJson); streamWriter.Flush(); streamWriter.Close(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var serializer = new JsonSerializer(); using (var jsonTextReader = new JsonTextReader(streamReader)) { newSection = serializer.Deserialize <CanvasSectionModel>(jsonTextReader); } } } return(Json(new { success = "true", sectionId = newSection.id, mstUniq = mstUniq })); } return(Json(new { success = "false", message = "Could not get course!" })); } return(Json(new { success = "false", message = "Could not get mstsched!" })); }
public JsonResult UpdateStudentsInSection(string mstuniq) { int usersAdded = 0; int usersRemoved = 0; int usersLeftAlone = 0; int usersWithErrors = 0; List <studemo> errorUsers = new List <studemo>(); //calculate the school year int schoolYear = Convert.ToInt32(SISdb.schools.Max(s => s.schyear)); mstsched period = SISdb.mstscheds.Find(Convert.ToInt32(mstuniq)); if (period == null) { return(Json(new { success = "false", message = "Period does not exist in SIS!" })); } CanvasSectionModel section = GetSectionBySISId(schoolYear + ":" + mstuniq + ":" + period.facdemo.funiq); if (section == null) { return(Json(new { success = "false", message = "Section does not exist in Canvas!" })); } //get the students in the section in canvas List <CanvasUserModel> users = GetUsersInSection(section.id); List <CanvasUserModel> usersNotInSIS = GetUsersInSection(section.id); //get the students in the period in sis DateTime tomorrow = DateTime.Now.AddDays(-365); var vStuSched = SISdb.stuscheds.Where(s => s.mstmeet.mstuniq == period.mstuniq && (s.xdate >= tomorrow || s.xdate == null)); foreach (stusched student in vStuSched) { //check to see if they are in the section CanvasUserModel canvasStudent = users.Find(u => u.sis_user_id == student.suniq.ToString()); if (canvasStudent == null) { //add the student //see if student exists in canvas canvasStudent = GetUserBySISId(student.suniq.ToString()); if (canvasStudent == null) { //create the user school sisSchool = SISdb.schools.First(s => s.schoolc == period.trkcr.track.schoolc); canvasStudent = CreateCanvasUser(student.suniq, sisSchool.schuniq); } if (canvasStudent != null) { //enroll the current user EnrollStudentInSection(canvasStudent.id, section.id); usersAdded++; } else { errorUsers.Add(student.studemo); usersWithErrors++; } } else { //student is in sis and the course in canvas do nothing with them. usersLeftAlone++; } if (canvasStudent != null) { usersNotInSIS.RemoveAll(c => c.id == canvasStudent.id); } } foreach (CanvasUserModel canvasStudent in usersNotInSIS) { UnEnrollStudentFromSection(canvasStudent.id, section.course_id); usersRemoved++; } if (errorUsers.Any()) { string message = "These users failed to enroll, likely because they are deleted in canvas: <br /><ul>"; foreach (studemo errorUser in errorUsers) { message += "<li>" + errorUser.FirstName + " " + errorUser.LastName + " Email Address:" + errorUser.emailaddr + "</li>"; } message += "</ul>"; EmailSender.EmailSender.sendTemplatedMessage("*****@*****.**", Globals.EMAIL_ADDRESSES, "Failed to enroll students in class", message, "High"); } return(Json(new { success = "true", studentsAdded = usersAdded, studentsRemoved = usersRemoved, studentsLeftAlone = usersLeftAlone, studentsCurrentlyEnrolled = vStuSched.Count(), studentsWithErrors = usersWithErrors })); }