protected Notification CreateNotification(Interview interview, Application application)
 {
     NotificationType type = db.NotificationTypes.Where(t => t.name == "InterviewAssigned").SingleOrDefault();
     NotificationStatus status = db.NotificationStatus.Where(s => s.name == "Pending").SingleOrDefault();
     string body = "";
     string subject = "";
     if (type.NotificationTemplate != null)
     {
         body = type.NotificationTemplate.body;
         subject = type.NotificationTemplate.subject;
     }
     Notification notification = new Notification{
         send_time = DateTime.Now.AddMinutes(30),
         status_id = status.id,
         type_id = type.id,
         body = body,
         subject = subject,
         sender = "*****@*****.**",
         interview_id = interview.id,
         created = DateTime.Now,
         created_by = WebSecurity.CurrentUserName,
         modified = DateTime.Now,
         modified_by = WebSecurity.CurrentUserName
     };
     return notification;
 }
        public ActionResult Create(Interview interview)
        {
            if (ModelState.IsValid)
            {
                db.Interviews.Add(interview);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    Session["FlashMessage"] = "Failed to create interview timeslot." + e.Message;
                    return View(interview);
                }
                return RedirectToAction("Index");
            }

            ViewBag.statusList = new SelectList(db.InterviewStatus, "id", "name", interview.status_id);
            ViewBag.programList = new SelectList(db.Programs.Where(p => p.require_interview), "id", "name");
            ViewBag.venueList = new SelectList(db.InterviewVenues.Where(v => v.status), "id", "name");
            return View(interview);
        }
 protected bool CheckAvoidedSessions(Interview interview, IList<AvoidedSession> sessions, StudentProfile student)
 {
     if (sessions != null)
     {
         foreach (var session in sessions)
         {
             if ((session.academic_organizations == null || session.academic_organizations.ToList().Contains(student.academic_organization)) &&
                 (session.academic_plans == null || session.academic_plans.ToList().Contains(student.academic_plan_primary)) &&
                 (session.academic_levels == null || session.academic_levels.ToList().Contains(student.academic_level)))
             {
                 List<bool> avoidedDayOfWeekList = PrepareAvoidedDayOfWeek(session);
                 if (avoidedDayOfWeekList[Convert.ToInt16(interview.start_time.DayOfWeek)])
                 {
                     if ((interview.start_time.TimeOfDay >= session.start_time.TimeOfDay && interview.start_time.TimeOfDay < session.end_time.TimeOfDay) ||
                         (interview.end_time.TimeOfDay > session.start_time.TimeOfDay && interview.end_time.TimeOfDay <= session.end_time.TimeOfDay) ||
                         (interview.start_time.TimeOfDay <= session.start_time.TimeOfDay && interview.end_time.TimeOfDay >= session.end_time.TimeOfDay))
                     {
                         return true;
                     }
                 }
             }
         }
     }
     return false;
 }
        public Notification CreateNotification(String type, Interview interview, Application application)
        {
            NotificationType notificationtype = db.NotificationTypes.Where(t => t.name == type).SingleOrDefault();
            NotificationStatus notificationstatus = db.NotificationStatus.Where(s => s.name == "Pending").SingleOrDefault();
            if (type == "InterviewAssigned")
            {
                if (notificationtype.NotificationTemplate != null)
                {
                    string body = "";
                    string subject = "";
                    body = notificationtype.NotificationTemplate.body;
                    body = body.Replace("[student id]", application.student_id);
                    body = body.Replace("[student name]", application.StudentProfile.name);
                    body = body.Replace("[application id]", application.id.ToString());
                    body = body.Replace("[program id]", application.program_id.ToString());
                    body = body.Replace("[program name]", application.Program.name);
                    body = body.Replace("[submit date]", String.Format("{0:yyyy-MM-dd HH:mm:ss}", application.submitted));
                    body = body.Replace("[interview date]", String.Format("{0:yyyy-MM-dd}", interview.start_time));
                    body = body.Replace("[interview time]", String.Format("{0:HH:mm}", interview.start_time) + " to " + String.Format("{0:HH:mm}", interview.end_time));
                    body = body.Replace("[interview venue]", interview.InterviewVenue.name);

                    subject = notificationtype.NotificationTemplate.subject;
                    subject = subject.Replace("[student id]", application.student_id);
                    subject = subject.Replace("[student name]", application.StudentProfile.name);
                    subject = subject.Replace("[application id]", application.id.ToString());
                    subject = subject.Replace("[program id]", application.program_id.ToString());
                    subject = subject.Replace("[program name]", application.Program.name);
                    subject = subject.Replace("[submit date]", String.Format("{0:yyyy-MM-dd HH:mm:ss}", application.submitted));
                    subject = subject.Replace("[interview date]", String.Format("{0:yyyy-MM-dd}", interview.start_time));
                    subject = subject.Replace("[interview time]", String.Format("{0:HH:mm}", interview.start_time) + " to " + String.Format("{0:HH:mm}", interview.end_time));
                    subject = subject.Replace("[interview venue]", interview.InterviewVenue.name);

                    Notification notification = new Notification
                    {
                        send_time = DateTime.Now,
                        sender = notificationtype.NotificationTemplate.sender,
                        subject = subject,
                        body = body,
                        status_id = notificationstatus.id,
                        type_id = notificationtype.id,
                        template_id = notificationtype.NotificationTemplate.id,
                        application_id = application.id,
                        created = DateTime.Now,
                        created_by = WebSecurity.CurrentUserName,
                        modified = DateTime.Now,
                        modified_by = WebSecurity.CurrentUserName
                    };
                    //db.Notifications.Add(notification);
                    //db.SaveChanges();

                    notification.NotificationRecipients.Add(new NotificationRecipient
                    {
                        email = application.StudentProfile.email,
                        recipient_type = "to",
                        student_id = application.student_id
                    });

                    if (!String.IsNullOrEmpty(notificationtype.NotificationTemplate.cc))
                    {
                        List<NotificationRecipient> ccList = new List<NotificationRecipient>();
                        notificationtype.NotificationTemplate.cc.Split(',').ToList().ForEach(s => ccList.Add(new NotificationRecipient { email = s.Trim(), recipient_type = "cc" }));
                        notification.NotificationRecipients = notification.NotificationRecipients.Concat(ccList).ToList();
                    }

                    if (!String.IsNullOrEmpty(notificationtype.NotificationTemplate.bcc))
                    {
                        List<NotificationRecipient> bccList = new List<NotificationRecipient>();
                        notificationtype.NotificationTemplate.bcc.Split(',').ToList().ForEach(s => bccList.Add(new NotificationRecipient { email = s.Trim(), recipient_type = "bcc" }));
                        notification.NotificationRecipients = notification.NotificationRecipients.Concat(bccList).ToList();
                    }

                    db.Notifications.Add(notification);
                    try
                    {
                        db.SaveChanges();
                        return notification;
                    }
                    catch (Exception e)
                    {
                        Session["FlashMessage"] += "<br/><br/>Failed to create notification record. <br/><br/>" + e.Message;
                    }
                }
                else
                {
                    Session["FlashMessage"] += "<br/><br/>Notification Template is not correctly configured";
                }
            }
            return null;
        }