public ActionResult Create(Mail mail, IEnumerable <HttpPostedFileBase> files = null) { if (ModelState.IsValid) { string recipient_string = Request.Params["recipientlist"]; string[] recipients; string currentCourse = Request.Form["CurrentlySelectedCourse"]; //gets selected FROM courseid string mailReply = Request.Form["mailReply"]; string forwardedAttachmentsContextId = Request.Form["forwarded_contextId"]; //get forwarded attachments context id string forwardedAttachmentsThreadId = Request.Form["forwarded_threadId"]; //get forwarded attachments thread id bool forwardedAttachments = false; if (!String.IsNullOrEmpty(forwardedAttachmentsContextId) && !String.IsNullOrEmpty(forwardedAttachmentsThreadId)) { forwardedAttachments = true; } if (mailReply == "" || mailReply == null) { mail.ContextID = Convert.ToInt16(currentCourse); } // AJ: Keep the ContextID the same if this is a reply to avoid confusion mail.Context = db.Courses.Where(b => b.ID == mail.ContextID).FirstOrDefault(); if (recipient_string != null) { recipients = recipient_string.Split(','); int count = 0; int threadID = 0; int dummyOut = 0; bool attachmentsSaved = false; foreach (string id in recipients) { if (Int32.TryParse(id, out dummyOut)) { Mail newMail = new Mail(); newMail.FromUserProfileID = CurrentUser.ID; newMail.Read = false; newMail.ToUserProfileID = Convert.ToInt32(id); newMail.Subject = mail.Subject; newMail.Message = mail.Message; newMail.ThreadID = threadID; newMail.ContextID = mail.ContextID; newMail.DeleteFromInbox = false; newMail.DeleteFromOutbox = false; if (newMail.ContextID <= 0) { newMail.ContextID = ActiveCourseUser.AbstractCourseID; } //need to create the mail before we can send the notification and set the threadID db.Mails.Add(newMail); db.SaveChanges(); // need to have an email created to get a valid id to set the thread ids to. if (count == 0) { threadID = newMail.ID; newMail.ThreadID = newMail.ID; db.SaveChanges(); } //now that the message is saved, set up attachments. //handle file attachments if ((null != files && !attachmentsSaved) || forwardedAttachments) { MailAttachmentFilePath mfp = Directories.GetMailAttachment(mail.ContextID, newMail.ThreadID); if (forwardedAttachments) { //we are also adding attachments from the forwarded mail MailAttachmentFilePath forwardedMfp = Directories.GetMailAttachment(Int32.Parse(forwardedAttachmentsContextId), Int32.Parse(forwardedAttachmentsThreadId)); foreach (var item in forwardedMfp.AllFiles()) { mfp.AddFile(Path.GetFileName(item), forwardedMfp.OpenFileRead(Path.GetFileName(item))); } } if (null != files && !attachmentsSaved) { foreach (var file in files) { if (null != file && file.ContentLength > 0) { Regex illegalInFileName = new Regex(@"[\\/:*?""<>|+#]"); string fileName = illegalInFileName.Replace(Path.GetFileName(file.FileName), ""); mfp.AddFile(fileName, file.InputStream); } } } attachmentsSaved = true; //we only need to save attachments once for each mail thread } using (NotificationController nc = new NotificationController()) { nc.SendMailNotification(newMail); } ++count; } } return(RedirectToAction("Index")); } } return(View(mail)); }
public ActionResult Create(Event e) { // Set to current user and poster e.Poster = ActiveCourseUser; // Default to not Approved. e.Approved = false; if (!Request.Form.AllKeys.Contains("IncludeEndDate")) { e.EndDate = null; } else { //make sure that the end date happens after the start if ((DateTime)e.EndDate < e.StartDate) { ModelState.AddModelError("badDates", "The starting time must occur before the ending time"); } } // Approve if instructor/leader, course is community, or approval is not required. if (ActiveCourseUser.AbstractRole.CanModify || ((ActiveCourseUser.AbstractCourse is Course) && !(ActiveCourseUser.AbstractCourse as Course).RequireInstructorApprovalForEventPosting) || (ActiveCourseUser.AbstractCourse is Community) ) { e.Approved = true; } if (ModelState.IsValid) { //locate timezone offset //int courseOffset = (ActiveCourseUser.AbstractCourse).GetType() != typeof(Community) ? ((Course)ActiveCourseUser.AbstractCourse).TimeZoneOffset : 0; //CourseController cc = new CourseController(); //TimeZoneInfo tz = cc.getTimeZone(courseOffset); //now convert the time to utc //if (e.EndDate != null) // e.EndDate = TimeZoneInfo.ConvertTimeToUtc((DateTime)e.EndDate, tz); //e.StartDate = TimeZoneInfo.ConvertTimeToUtc(e.StartDate, tz); if (e.EndDate != null) { e.EndDate = ((DateTime)e.EndDate).CourseToUTC(ActiveCourseUser.AbstractCourseID); } e.StartDate = e.StartDate.CourseToUTC(ActiveCourseUser.AbstractCourseID); db.Events.Add(e); db.SaveChanges(); if (!e.Approved) { using (NotificationController nc = new NotificationController()) { nc.SendEventApprovalNotification(e); } return(RedirectToAction("NeedsApproval")); } //rebuilds course calendar file upon creating of a new event ICAL using (iCalendarController ical = new iCalendarController()) { ical.CreateCourseCalendar(ActiveCourseUser.AbstractCourseID); } return(RedirectToAction("Index")); } return(View(e)); }
public ActionResult NewReply(DashboardReply dr) { if (ModelState.IsValid) { dr.CourseUser = ActiveCourseUser; dr.Posted = DateTime.UtcNow; int replyTo = 0; if (Request.Form["reply_to"] != null) { replyTo = Convert.ToInt32(Request.Form["reply_to"]); } int latestReply = 0; if (Request.Form["latest_reply"] != null) { latestReply = Convert.ToInt32(Request.Form["latest_reply"]); } DashboardPost replyToPost = db.DashboardPosts.Find(replyTo); if (replyToPost != null) { // Does the post we're replying to exist? // Are we a member of the course we're replying to? CourseUser cu = (from c in currentCourses where c.AbstractCourseID == replyToPost.CourseUser.AbstractCourseID select c).FirstOrDefault(); AbstractCourse ac = null; if (cu != null) { ac = cu.AbstractCourse; } if ((cu != null) && (cu.AbstractRole.CanGrade || ((ac != null) && (ac.AllowDashboardPosts)))) { replyToPost.Replies.Add(dr); db.SaveChanges(); //construct the subject & body string subject = ""; string body = ""; List <MailAddress> addresses = new List <MailAddress>(); ViewBag.dp = replyToPost; List <DashboardReply> replys = replyToPost.Replies.Where(r => r.ID > latestReply).ToList(); //slightly different messages depending on course type if (ac is Course && (ac as Course).AllowDashboardReplies) { Course course = (Course)ac; subject = "[" + course.Prefix + " " + course.Number + "] Reply from " + CurrentUser.FirstName + " " + CurrentUser.LastName; body = CurrentUser.FirstName + " " + CurrentUser.LastName + " sent the following reply to the Dashboard post " + replyToPost.DisplayTitle + " at " + dr.Posted.UTCToCourse(ActiveCourseUser.AbstractCourseID).ToString() + ":"; } else if (ac is Community) { Community community = ac as Community; subject = "[" + community.Nickname + "] Reply from " + CurrentUser.FirstName + " " + CurrentUser.LastName; body = CurrentUser.FirstName + " " + CurrentUser.LastName + " sent the following reply to the Dashboard post " + replyToPost.DisplayTitle + " at " + dr.Posted.UTCToCourse(ActiveCourseUser.AbstractCourseID).ToString() + ":"; } else { //this should never execute, but just in case... subject = "OSBLE Activity Post"; body = CurrentUser.FirstName + " " + CurrentUser.LastName + " sent the following message at " + dr.Posted.UTCToCourse(ActiveCourseUser.AbstractCourseID).ToString() + ":"; } body += "<br /><br />"; body += dr.Content.Replace("\n", "<br />"); body += string.Format("<br /><br /><a href=\"http://plus.osble.org/Home/Course?courseId={0}&postId={1}\">View and reply to post in OSBLE</a>", dr.CourseUser.AbstractCourseID, dr.ID ); //List<CoursesUsers> courseUsers = db.CoursesUsers.Where(c => (c.AbstractCourseID == ac.ID && c.UserProfile.EmailAllActivityPosts)).ToList(); List <CourseUser> courseUsers = (from c in db.CourseUsers where c.AbstractCourseID == ac.ID && c.UserProfile.EmailAllActivityPosts && c.UserProfileID != CurrentUser.ID select c).ToList(); foreach (CourseUser member in courseUsers) { if (member.UserProfile.UserName != null) // Ignore pending users { addresses.Add(new MailAddress(member.UserProfile.UserName, member.UserProfile.FirstName + " " + member.UserProfile.LastName)); } } //Send the message Email.Send(subject, body, addresses); foreach (DashboardReply r in replys) { SetupPostDisplay(r); } replys.Clear(); replys.Add(dr); ViewBag.DashboardReplies = replys; // Post notification to other thread participants using (NotificationController nc = new NotificationController()) { nc.SendDashboardNotification(dr.Parent, dr.CourseUser); } } else { Response.StatusCode = 403; } } else { Response.StatusCode = 403; } } return(View("_SubDashboardReply")); }