public ActionResult Notifications() { if (IsLoggedIn() && IsAuthorized()) { int userID = GetUserID(); var user = db.User.Where(u => u.recordID == userID).FirstOrDefault(); var unreadNotifications = db.Notification.Where(n => n.User.recordID == userID && n.isRead == false).ToList(); var readNotifications = db.Notification.Where(n => n.User.recordID == userID && n.isRead == true).ToList(); var viewModel = new StudentNotificationViewModel { user = user, unreadNotifications = unreadNotifications, readNotifications = readNotifications }; db.Configuration.ValidateOnSaveEnabled = false; foreach (var item in user.Notification) { if (item.isRead == false) { item.isRead = true; } } db.SaveChanges(); return(View(viewModel)); } else { return(RedirectToAction("Login", "User")); } }
public List <StudentNotificationViewModel> GetNotificationData(DateTime afterDate, string userId) { using (ApplicationDbContext context = new ApplicationDbContext()) { List <StudentNotificationViewModel> result = new List <StudentNotificationViewModel>(); //******STUDENT NOTIFICATIONS************** //student - get newly graded submissions per user var updatedSubmissions = context.SubmissionGrades.Where(x => x.GradeAddedOn > afterDate).Include(x => x.Assignment).Include(x => x.User).OrderByDescending(x => x.GradeAddedOn).ToList(); var updatedSubmissionsForUser = updatedSubmissions.Where(x => x.User.Id == userId).ToList(); //student - get newly added assignments per user var currentEnrollmentsWithAssignments = context.Enrollments.Where(x => x.User.Id == userId).Include(x => x.User).Include(x => x.Course).Include("Course.Assignments").ToList(); var allAssignments = currentEnrollmentsWithAssignments.Select(x => x.Course).SelectMany(y => y.Assignments).ToList(); var recentlyAddedAssignments = allAssignments.Where(x => x.AssignmentAddedOn > afterDate).ToList(); //student - add newly graded submissions to the list foreach (SubmissionGrades sg in updatedSubmissionsForUser) { StudentNotificationViewModel newGradedNotification = new StudentNotificationViewModel() { AssignmentTitle = sg.Assignment.AssignmentTitle, Grade = (double)sg.Grade, Points = sg.Assignment.Points, DueDate = null, Department = sg.Assignment.Course.Department, CourseNumber = sg.Assignment.Course.CourseNumber, SubmissionDate = null, isInstructor = false }; result.Add(newGradedNotification); } //student - add newly added assignments to the same list foreach (Assignment a in recentlyAddedAssignments) { StudentNotificationViewModel newAddedAssignment = new StudentNotificationViewModel() { AssignmentTitle = a.AssignmentTitle, Grade = 0, Points = a.Points, DueDate = a.DueDate, Department = a.Course.Department, CourseNumber = a.Course.CourseNumber, SubmissionDate = null, isInstructor = false }; result.Add(newAddedAssignment); } //******INSTRUCTOR NOTIFICATIONS************** //instructor - get newly added submission submitted for instructor var recentStudentSubmissions = context.SubmissionGrades.Where(x => x.SubmissionDate > afterDate).Include(x => x.Assignment).Include(x => x.User).OrderByDescending(x => x.SubmissionDate).ToList(); var getInstructor = context.Users.Where(x => x.Id == userId).FirstOrDefault(); var instructorCourses = context.Courses.Where(x => x.Instructor.Id == getInstructor.Id).ToList(); var instructorCourseSubmission = from submission in recentStudentSubmissions where instructorCourses.Any(x => x.CourseId == submission.Assignment.Course.CourseId) select submission; //instructor add newly graded submissions to the list foreach (SubmissionGrades studentSubmission in instructorCourseSubmission) { StudentNotificationViewModel newAssignmentSubmitted = new StudentNotificationViewModel() { AssignmentTitle = studentSubmission.Assignment.AssignmentTitle, Grade = 0, Points = studentSubmission.Assignment.Points, DueDate = null, Department = studentSubmission.Assignment.Course.Department, CourseNumber = studentSubmission.Assignment.Course.CourseNumber, SubmissionDate = studentSubmission.SubmissionDate, isInstructor = true }; result.Add(newAssignmentSubmitted); } //return the list return(result); } }