public ActionResult Questions(int ParentId = 0, int StudentId = 0, int CourseId = 0)
        {
            ViewBag.Title = "Questions";

            if (Helpers.Helpers.IsStaff(User))
            {
                return(RedirectToAction("Index", "Staff"));
            }

            Course oCourse = db.Courses.SingleOrDefault(c => c.CourseId == CourseId);

            if (oCourse == null)//Error. Course DNE.
            {
                throw new ArgumentException("Questions(int ParentId = 0, int StudentId = 0, int CourseId = 0) - Course DNE.");
            }

            Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == StudentId && s.Courses.Any(c => c.CourseId == oCourse.CourseId));

            if (oStudent == null)//Error. Student DNE OR doesn't take this Course.
            {
                throw new ArgumentException("Questions(int ParentId = 0, int StudentId = 0, int CourseId = 0) - Student DNE.");
            }

            Parent oParent = db.Parents.SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId));

            if (oParent == null) //Error. This Student/Child doesn't belong to this Parent.
            {
                throw new ArgumentException("Questions(int ParentId = 0, int StudentId = 0, int CourseId = 0) - Parent DNE.");
            }

            List <QuestionHistory> oQuestionHistorys = db.QuestionHistorys.Include(q => q.Parent).Where(q => q.Course.CourseId == CourseId).OrderByDescending(q => q.DateAnswered).ThenByDescending(q => q.QuestionHistoryId).ToList();//.Include(q => q.Course)

            if (oQuestionHistorys == null)
            {
                oQuestionHistorys = new List <QuestionHistory>();
            }

            ResponseQuestionsViewModel RVM = new ResponseQuestionsViewModel()
            {
                QuestionHistorys  = oQuestionHistorys,
                QuestionHistoryVM = new QuestionHistoryViewModel(),
                Parent            = oParent,
                Student           = oStudent,
                Course            = oCourse
            };

            return(View(RVM));
        }
Exemple #2
0
        public ActionResult Parent(int queryvalues)
        {
            ViewBag.Title = "Dashboard - Staff";

            int id = queryvalues;

            Parent oParent = db.Parents.SingleOrDefault(p => p.ParentId == id);
            List <QuestionHistory> oQuestionHistorysResponded = db.QuestionHistorys.Include(qh => qh.Student).Include(qh => qh.Course).Where(qh => qh.Parent.ParentId == id && !(qh.Answer == null || qh.Answer.Length == 0)).OrderBy(q => q.Parent.LastName).ThenBy(q => q.Parent.FirstName).ThenBy(q => q.Student.FirstName).ThenBy(q => q.Course.Name).ThenByDescending(q => q.QuestionHistoryId).ToList();
            List <QuestionHistory> oQuestionHistorysPending   = db.QuestionHistorys.Include(qh => qh.Student).Include(qh => qh.Course).Where(qh => qh.Parent.ParentId == id && (qh.Answer == null || qh.Answer.Length == 0)).OrderBy(q => q.Parent.LastName).ThenBy(q => q.Parent.FirstName).ThenBy(q => q.Student.FirstName).ThenBy(q => q.Course.Name).ThenByDescending(q => q.QuestionHistoryId).ToList();

            ResponseQuestionsViewModel oRVM = new ResponseQuestionsViewModel()
            {
                Parent                  = oParent,
                QuestionHistoryVM       = new QuestionHistoryViewModel(),
                QuestionHistorys        = oQuestionHistorysResponded,
                QuestionHistorysPending = oQuestionHistorysPending
            };

            return(View(oRVM));
        }
        public async Task <ActionResult> CreateQuestionHistory(ResponseQuestionsViewModel oRVM)
        {
            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = false, Title = "Error!", Message = "Something went wrong."
            };

            if (Helpers.Helpers.IsStaff(User))
            {
                return(RedirectToAction("Index", "Staff"));
            }

            if (!ModelState.IsValid)
            {
                return(View(oRVM));
            }

            QuestionHistoryViewModel oQuestionHistoryVM = oRVM.QuestionHistoryVM;

            Course oCourse = db.Courses.Include(c => c.QuestionHistorys).SingleOrDefault(c => c.CourseId == oQuestionHistoryVM.CourseId);

            if (oCourse == null)//Error. Course DNE.
            {
                throw new ArgumentException("CreateQuestionHistory(ResponseQuestionsViewModel oRVM) - Course DNE.");
            }

            Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == oQuestionHistoryVM.StudentId && s.Courses.Any(c => c.CourseId == oCourse.CourseId));

            if (oStudent == null)//Error. Student DNE OR doesn't take this Course.
            {
                throw new ArgumentException("CreateQuestionHistory(ResponseQuestionsViewModel oRVM) - Student DNE.");
            }

            Parent oParent = db.Parents.SingleOrDefault(p => p.ParentId == SessionSingleton.Current.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId));

            if (oParent == null) //Error. This Student/Child doesn't belong to this Parent.
            {
                throw new ArgumentException("CreateQuestionHistory(ResponseQuestionsViewModel oRVM) - Parent DNE.");
            }

            QuestionHistory oQuestionHistory = new QuestionHistory()
            {
                //-- Default values --
                //-- end of Default values --

                //-- VM to Model --
                QuestionTxt = oQuestionHistoryVM.QuestionTxt,
                Parent      = oParent,
                Student     = oStudent,
                Course      = oCourse
                              //-- end of VM to Model --
            };

            //oParent.QuestionHistorys.Add(oQuestionHistory);
            //oStudent.QuestionHistorys.Add(oQuestionHistory);
            oCourse.QuestionHistorys.Add(oQuestionHistory);
            db.QuestionHistorys.Add(oQuestionHistory);

            try
            {
                if (db.SaveChanges() <= 0)
                {
                    return(View(oQuestionHistoryVM));
                }
            }
            catch (Exception e)
            {
                return(View(oQuestionHistoryVM));
            }

            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = true, Title = "Success!", Message = "Your question for this course has been successfully added."
            };

            return(RedirectToAction("Questions", new { ParentId = oQuestionHistoryVM.ParentId, StudentId = oQuestionHistoryVM.StudentId, CourseId = oQuestionHistoryVM.CourseId }));
        }
        public async Task <ActionResult> EditQuestionHistory(ResponseQuestionsViewModel oRVM)
        {
            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = false, Title = "Error!", Message = "Something went wrong."
            };

            //This is for Staffer use also so commented out:
            //if (Helpers.Helpers.IsStaff(User))
            //    return RedirectToAction("Index", "Staff");

            if (!ModelState.IsValid)
            {
                return(View(oRVM));
            }

            QuestionHistoryViewModel oQuestionHistoryVM = oRVM.QuestionHistoryVM;

            Course oCourse = db.Courses.Include(c => c.QuestionHistorys).SingleOrDefault(c => c.CourseId == oQuestionHistoryVM.CourseId);

            if (oCourse == null)//Error. Course DNE.
            {
                throw new ArgumentException("EditQuestionHistory(ResponseQuestionsViewModel oRVM) - Course DNE.");
            }

            Student oStudent = db.Students.SingleOrDefault(s => s.StudentId == oQuestionHistoryVM.StudentId && s.Courses.Any(c => c.CourseId == oCourse.CourseId));

            if (oStudent == null)//Error. Student DNE OR doesn't take this Course.
            {
                throw new ArgumentException("EditQuestionHistory(ResponseQuestionsViewModel oRVM) - Student DNE.");
            }

            Parent oParent = db.Parents.SingleOrDefault(p => p.ParentId == oQuestionHistoryVM.ParentId && p.Children.Any(s => s.StudentId == oStudent.StudentId));

            if (oParent == null) //Error. This Student/Child doesn't belong to this Parent.
            {
                throw new ArgumentException("EditQuestionHistory(ResponseQuestionsViewModel oRVM) - Parent DNE.");
            }

            //-- QuestionHistory --
            QuestionHistory oQuestionHistory = db.QuestionHistorys.SingleOrDefault(qh => qh.QuestionHistoryId == oQuestionHistoryVM.QuestionHistoryId);

            if (oQuestionHistory == null) //Error. This Question DNE.
            {
                throw new ArgumentException("EditQuestionHistory(ResponseQuestionsViewModel oRVM) - QuestionHistory DNE.");
            }

            oQuestionHistory.QuestionTxt = oQuestionHistoryVM.QuestionTxt;
            //-- end of QuestionHistory --

            try
            {
                if (db.SaveChanges() <= 0)
                {
                    return(View(oQuestionHistoryVM));
                }
            }
            catch (Exception e)
            {
                return(View(oQuestionHistoryVM));
            }

            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = true, Title = "Success!", Message = "Your question for this course has been successfully updated."
            };

            return(RedirectToAction("Parent", "Staff", new { queryvalues = oQuestionHistoryVM.ParentId }));//, StudentId = oQuestionHistoryVM.StudentId, CourseId = oQuestionHistoryVM.CourseId
        }
Exemple #5
0
        public async Task <ActionResult> SendEmailsToTeachers(ResponseQuestionsViewModel oRVM)
        {
            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = false, Title = "Error!", Message = "Something went wrong."
            };

            if (!ModelState.IsValid)
            {
                return(View(oRVM));
            }

            //-- Parent --
            Parent oParent = db.Parents
                             .Include(p => p.Children.Select(s => s.Courses.Select(c => c.Teacher)))
                             .Include(p => p.Children.Select(s => s.Courses.Select(c => c.QuestionHistorys)))
                             .SingleOrDefault(p => p.ParentId == oRVM.QuestionHistoryVM.ParentId);

            //Parent oParent = db.Parents.Include(p => p.Children.Select(s => s.Courses.Select(c => c.Teacher))).SingleOrDefault(p => p.ParentId == oRVM.QuestionHistoryVM.ParentId);
            //Parent oParent = db.Parents.Include(p => p.Children.Select(s => s.Courses)).Select(p => new { Parent = p, Children = p.Children, Teacher = p.Children.Select(s => s.Courses.Select(c => c.Teacher)), QuestionHistorys = p.Children.Select(s => s.Courses.Select(c => c.QuestionHistorys)) }).SingleOrDefault(p => p.ParentId == oRVM.QuestionHistoryVM.ParentId);
            //Parent oParent = db.Parents.Include(p => p.Children.Select(s => s.Courses)).SingleOrDefault(p => p.ParentId == oRVM.Parent.ParentId);

            if (oParent == null) //Error. Parent DNE.
            {
                throw new ArgumentException("SendEmailsToTeachers(ResponseQuestionsViewModel oRVM) - Parent DNE.");
            }
            //-- end of Parent --

            try
            {
                string questions;
                int    questionNumber;
                foreach (Student iStudent in oParent.Children)
                {
                    foreach (Course iCourse in iStudent.Courses)
                    {
                        if (iCourse.QuestionHistorys.Count > 0)
                        {
                            questions      = "";
                            questionNumber = 1;

                            foreach (QuestionHistory iQuestionHistory in iCourse.QuestionHistorys)
                            {
                                questions += questionNumber++ + ". " + iQuestionHistory.QuestionTxt + "<br /><i>your answer here</i><br /><br />";
                            }

                            if (!SendEmail(oParent, iStudent, iCourse, questions))
                            {
                                return(RedirectToAction("Parent", new { queryvalues = oParent.ParentId }));
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                return(RedirectToAction("Parent", new { queryvalues = oParent.ParentId }));
            }

            TempData["UserMessage"] = new MessageVM()
            {
                IsSuccessful = true, Title = "Success!", Message = "Your question(s) for this parent have been successfully sent."
            };

            return(RedirectToAction("Parent", new { queryvalues = oParent.ParentId }));
        }