public IHttpActionResult PutUpdatedCommentHeader(int section_id, int term_id, [FromBody] String content) { using (WebhostEntities db = new WebhostEntities()) { int header = -1; try { header = db.CommentHeaders.Where(h => h.SectionIndex == section_id && h.TermIndex == term_id).Single().id; } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e))); } CommentHeader headerParagraph = db.CommentHeaders.Find(header); content = CommentLetter.ConvertFromBase64String(content); headerParagraph.HTML = CommentLetter.CleanTags(content); try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new CommentHeaderInfo(header)))); } }
public IHttpActionResult CreateNewCommentHeader(int section_id, [FromBody] String content) { using (WebhostEntities db = new WebhostEntities()) { Section section = db.Sections.Find(section_id); if (section == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Section Id.")))); } WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; Faculty self = db.Faculties.Find(id); if (!self.Sections.Contains(section)) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException("You are not a teacher for this class.")))); } int termId = DateRange.GetCurrentOrLastTerm(); if (section.CommentHeaders.Where(h => h.TermIndex == termId).Count() > 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Header Already Exists. Use PUT to Update.")))); } CommentHeader newHeader = new CommentHeader() { id = db.CommentHeaders.OrderBy(h => h.id).ToList().Last().id + 1, SectionIndex = section_id, TermIndex = termId, HTML = "" }; content = CommentLetter.ConvertFromBase64String(content); newHeader.HTML = CommentLetter.CleanTags(content); try { db.CommentHeaders.Add(newHeader); db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Created, new CommentHeaderInfo(newHeader.id), "text/json"))); } }
public IHttpActionResult GetPdfComment(int id) { CommentLetter comment = new CommentLetter(id); byte[] pdfData = comment.Publish().Save(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(pdfData); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline") { FileName = "comment.pdf" }; response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/pdf"); return(ResponseMessage(response)); }
/// <summary> /// Initialize Info object. /// </summary> /// <param name="id"></param> /// <param name="includeStudentCommentData"></param> public CommentHeaderInfo(int id, bool includeStudentCommentData = false) { using (WebhostEntities db = new WebhostEntities()) { CommentHeader header = db.CommentHeaders.Find(id); if (header == null) { throw new ArgumentException("Invalid Comment Header Id."); } Id = id; SectionId = header.SectionIndex; TermId = header.TermIndex; Term = string.Format("{0} {1}", header.Term.Name, header.Term.StartDate.Year); SectionTitle = String.Format("[{0}] {1}", header.Section.Block.LongName, header.Section.Course.Name); // Convert HTML to Base64String. if (header.RTF.Length <= 0) { HtmlContent = CommentLetter.ConvertToBase64String(header.HTML); } else { RtfContnet = header.RTF; } StudentCommentIds = header.StudentComments.Select(com => com.id).ToList(); if (includeStudentCommentData) { StudentComments = new List <StudentCommentInfo>(); foreach (int comId in StudentCommentIds) { StudentComments.Add(new StudentCommentInfo(comId)); } } } }
/// <summary> /// Initialize a student comment. /// </summary> /// <param name="id"></param> /// <param name="includeHeader"></param> public StudentCommentInfo(int id, bool includeHeader = false) { using (WebhostEntities db = new WebhostEntities()) { StudentComment comment = db.StudentComments.Find(id); if (comment == null) { throw new ArgumentException("Invalid Student Comment."); } Grades = new GradeInfo() { EngagementGrade = comment.EngagementGrade.Name, ExamGrade = comment.ExamGrade.Name, FinalGrade = comment.FinalGrade.Name, TrimesterGrade = comment.TermGrade.Name }; Id = id; Student = new StudentInfo(comment.StudentID); HeaderId = comment.HeaderIndex; if (comment.RTF.Length <= 0) { HtmlContent = CommentLetter.ConvertToBase64String(comment.HTML); } else { RtfContent = comment.RTF; } if (includeHeader) { Header = new CommentHeaderInfo(HeaderId); } } }
public async Task <IHttpActionResult> PostNewCommentHeader(int section_id, int term_id) { using (WebhostEntities db = new WebhostEntities()) { if (db.CommentHeaders.Where(h => h.SectionIndex == section_id && h.TermIndex == term_id).Count() > 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, new ArgumentException("Comment Header already Exists.")))); } CommentHeader headerParagraph = new CommentHeader() { id = db.CommentHeaders.OrderBy(h => h.id).ToList().Last().id + 1, SectionIndex = section_id, TermIndex = term_id }; String content = await Request.Content.ReadAsStringAsync(); content = CommentLetter.ConvertFromBase64String(content); headerParagraph.HTML = CommentLetter.CleanTags(content); db.CommentHeaders.Add(headerParagraph); try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new CommentHeaderInfo(headerParagraph.id)))); } }
public IHttpActionResult PutUpdatedStudentComment(int section_id, int student_id, [FromBody] StudentCommentInfo info, [FromUri] String format = "rtf") { using (WebhostEntities db = new WebhostEntities()) { Section section = db.Sections.Find(section_id); if (section == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Section Id.")))); } if (section.Students.Where(s => s.ID == student_id).Count() <= 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Student Id.")))); } WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; Faculty self = db.Faculties.Find(id); if (!self.Sections.Contains(section)) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException("You are not a teacher for this class.")))); } int termId = DateRange.GetCurrentOrLastTerm(); if (section.CommentHeaders.Where(h => h.TermIndex == termId).Count() <= 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NoContent, new ArgumentException("No Comment Header for this Term is saved.")))); } CommentHeader header = section.CommentHeaders.Where(h => h.TermIndex == termId).Single(); if (header.StudentComments.Where(com => com.StudentID == student_id).Count() <= 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, new ArgumentException("No Comment for this student saved.")))); } StudentComment comment = header.StudentComments.Where(com => com.StudentID == student_id).Single(); Dictionary <String, int> GradeEntries = CommentLetter.GetGradeTableEntryData(); comment.EffortGradeID = GradeEntries[info.Grades.EngagementGrade]; comment.ExamGradeID = GradeEntries[info.Grades.ExamGrade]; comment.TermGradeID = GradeEntries[info.Grades.TrimesterGrade]; comment.FinalGradeID = GradeEntries[info.Grades.FinalGrade]; if (format.Equals("html")) { comment.HTML = CommentLetter.ConvertFromBase64String(info.HtmlContent); } else { comment.RTF = info.RtfContent; } try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new StudentCommentInfo(header.StudentComments.Where(com => com.StudentID == student_id).Single().id)))); } }
public IHttpActionResult PutUpdatedCommentHeader(int section_id, [FromBody] String content, [FromUri] String format = "rtf") { using (WebhostEntities db = new WebhostEntities()) { Section section = db.Sections.Find(section_id); if (section == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Section Id.")))); } WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; Faculty self = db.Faculties.Find(id); if (!self.Sections.Contains(section)) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException("You are not a teacher for this class.")))); } int termId = DateRange.GetCurrentOrLastTerm(); CommentHeader header; if (section.CommentHeaders.Where(h => h.TermIndex == termId).Count() <= 0) { header = new CommentHeader() { id = db.CommentHeaders.OrderBy(h => h.id).ToList().Last().id + 1, HTML = "", SectionIndex = section_id, TermIndex = termId }; db.CommentHeaders.Add(header); } else { header = section.CommentHeaders.Where(h => h.TermIndex == termId).Single(); } content = CommentLetter.ConvertFromBase64String(content); if (format.Equals("html")) { header.HTML = CommentLetter.ConvertFromBase64String(content); } else { header.RTF = Convert.FromBase64String(content); } try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new CommentHeaderInfo(header.id), "text/json"))); } }
protected void SaveBtn_Click(object sender, EventArgs e) { if (SelectedSectionId == -1) { State.log.WriteLine("{0} {1}: Cannot Save with no section selected.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); LogWarning("Cannot save with no section selected."); return; } if (Saving) { State.log.WriteLine("{1} {0}: Aborted multiple Saving attempts.", DateTime.Now.ToLongTimeString(), DateTime.Today.ToShortDateString()); LogWarning("Aborted multiple Saving Attempts."); return; } using (WebhostEntities db = new WebhostEntities()) { Saving = true; if (HeaderPanel.Visible) { CommentHeader header = db.CommentHeaders.Where(h => h.id == LoadedHeaderId).Single(); State.log.WriteLine("{0} {1}: Saving Header Paragraph.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); // Save the Header! if (HeaderEditor.Content.Length < header.HTML.Length) { State.log.WriteLine("{0} {1}: Warning--suspicious overwrite:{2}Original:{2}{3}{2}Replacement:{2}{4}", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString(), Environment.NewLine, header.HTML, HeaderEditor.Content); WebhostEventLog.CommentLog.LogWarning("Suspicious Overwrite:{0}Original:{0}{1}{0}========================================================{0}Replacement:{0}{2}", Environment.NewLine, header.HTML, HeaderEditor.Content); } String html = HeaderEditor.Content; String temp = html; html = CommentLetter.CleanTags(html); if (!temp.Equals(html)) { WebhostEventLog.CommentLog.LogWarning("CleanTags method changed:{0}{1}{0}============================================================={0}Into:{0}{2}", Environment.NewLine, temp, html); } WebhostEventLog.CommentLog.LogInformation("Saving Header:{0}{1}", Environment.NewLine, html); HeaderEditor.Content = html; header.HTML = HeaderEditor.Content; HeaderHTML = HeaderEditor.Content; } if (LoadedCommentId != -1) { State.log.WriteLine("{0} {1}: Saving Student Comment.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); StudentComment comment = db.StudentComments.Where(com => com.id == LoadedCommentId).Single(); if (StudentEditor.Content.Length < comment.HTML.Length) { State.log.WriteLine("{0} {1}: Warning--suspicious overwrite:{2}Original:{2}{3}{2}Replacement:{2}{4}", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString(), Environment.NewLine, comment.HTML, StudentEditor.Content); WebhostEventLog.CommentLog.LogWarning("Suspicious Overwrite:{0}Original:{0}{1}{0}========================================================{0}Replacement:{0}{2}", Environment.NewLine, comment.HTML, StudentEditor.Content); } String html = StudentEditor.Content; String temp = html; html = CommentLetter.CleanTags(html); if (!temp.Equals(html)) { WebhostEventLog.CommentLog.LogWarning("CleanTags method changed:{0}{1}{0}============================================================={0}Into:{0}{2}", Environment.NewLine, temp, html); } WebhostEventLog.CommentLog.LogInformation("Saving Comment for {2} {3}:{0}{1}", Environment.NewLine, html, comment.Student.FirstName, comment.Student.LastName); StudentEditor.Content = html; comment.HTML = StudentEditor.Content; comment.EffortGradeID = CommentGrades.EffortGradeID; comment.FinalGradeID = CommentGrades.FinalGradeID; comment.TermGradeID = CommentGrades.TrimesterGradeID; comment.ExamGradeID = CommentGrades.ExamGradeID; StudentCommentHTML = StudentEditor.Content; } db.SaveChanges(); State.log.WriteLine("{0} {1}: Changes saved to the Database.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); WebhostEventLog.CommentLog.LogInformation("Save Complete."); } Saving = false; }
public IHttpActionResult GetMyComments(string term, int year) { if (!(new List <String>() { "Fall", "Winter", "Spring" }).Contains(term)) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, new ArgumentOutOfRangeException(nameof(term))))); } using (WebhostEntities db = new WebhostAPI.WebhostEntities()) { Term theTerm = null; try { theTerm = db.Terms.Where(t => t.Name.Equals(term) && t.StartDate.Year == year).Single(); } catch (Exception e) { // Invalid term. return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e))); } WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; byte[] zipData = null; using (MemoryStream ms = new MemoryStream()) { using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Create, true)) { List <int> CommentIds = new List <int>(); if (((WebhostIdentity)(principal.Identity)).User.IsTeacher()) { Faculty teacher = db.Faculties.Find(((WebhostIdentity)(principal.Identity)).User.Id); foreach (Section section in teacher.Sections.Where(s => s.CommentHeaders.Where(ch => ch.TermIndex == theTerm.id).Count() > 0).ToList()) { CommentHeader header = section.CommentHeaders.Where(ch => ch.TermIndex == theTerm.id).Single(); CommentIds.AddRange(header.StudentComments.Select(c => c.id).ToList()); } } else { Student student = db.Students.Find(((WebhostIdentity)(principal.Identity)).User.Id); CommentIds.AddRange(student.StudentComments.Where(com => com.CommentHeader.TermIndex == theTerm.id).Select(com => com.id).ToList()); } foreach (int cid in CommentIds) { CommentLetter letter = new CommentLetter(cid); byte[] pdfData = letter.Publish().Save(); ZipArchiveEntry entry = archive.CreateEntry(CommentLetter.EncodeSafeFileName(letter.Title) + ".pdf"); using (Stream stream = entry.Open()) { stream.Write(pdfData, 0, pdfData.Length); } } } ms.Seek(0, SeekOrigin.Begin); zipData = new byte[ms.Length]; for (long i = 0; i < ms.Length; i++) { zipData[i] = (byte)ms.ReadByte(); } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new ByteArrayContent(zipData); response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline") { FileName = "comments.zip" }; response.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/octet-stream"); return(ResponseMessage(response)); } }