public ActionResult EditExam(string id, FormCollection collection)//---> gets exam id, delete the row of it and add new row with changes from the faculty
        {
            ExamsDal dal = new ExamsDal();

            Exams ex = dal.exams.Find(id);

            dal.exams.Remove(ex);
            dal.SaveChanges();

            Exams exam = new Exams()
            {
                CourseName = collection["CourseName"],
                CourseId   = collection["CourseId"],
                MoedADate  = collection["MoedADate"],
                ClassA     = collection["ClassA"],
                HoursA     = collection["HoursA"],
                MoedBDate  = collection["MoedBDate"],
                ClassB     = collection["ClassB"],
                HoursB     = collection["HoursB"]
            };

            if (ModelState.IsValid)
            {
                try
                {
                    (from c in dal.exams
                     where c.CourseId == id
                     select c).ToList();
                    dal.exams.Add(exam);
                    dal.SaveChanges();
                }
                catch (Exception)
                {
                    TempData["error"] = "The course already exist!\n"; // print error message
                    return(View());
                }
            }
            return(View("Home"));
        }
Esempio n. 2
0
        public ActionResult SubmitExam(Exam exam)
        {
            if (ModelState.IsValid)
            {
                /* change or add exam */
                using (CoursesDal coursesDb = new CoursesDal())
                    using (ExamsDal exmaDb = new ExamsDal())
                        using (CourseParticipantsDal courseParticipantDb = new CourseParticipantsDal())
                        {
                            /* check course exist */
                            var courseId = coursesDb.Courses.Where(Course => Course.courseId.Equals(exam.courseId)).FirstOrDefault();
                            if (courseId == null)
                            {
                                TempData["msg"] = "Invalid course ID";
                                return(View("ManageExams", exam));
                            }

                            /* check course name is the same in courses */
                            var courseName = coursesDb.Courses.Where(Course => Course.courseId.Equals(exam.courseId) && Course.courseName.Equals(exam.courseName)).FirstOrDefault();
                            if (courseName == null)
                            {
                                TempData["msg"] = "Invalid course Name";
                                return(View("ManageExams", exam));
                            }

                            /* check start time is before end time */
                            if (exam.startTime >= exam.endTime)
                            {
                                TempData["msg"] = "Exam end time must be bigger than Exam start time";
                                return(View("ManageExams", exam));
                            }

                            /* check date isnt in the past */
                            if (DateTime.Compare(exam.date + exam.startTime, DateTime.Now) < 0)
                            {
                                TempData["msg"] = "Exam cant be in the past";
                                return(View("ManageExams", exam));
                            }


                            /* get all exams with same class and check time doesnt clash */
                            var exams = (from row in exmaDb.Exams
                                         where row.className.Equals(exam.className)
                                         select row).ToList();

                            foreach (Exam x in exams)
                            {
                                if (x.startTime < exam.endTime && exam.startTime < x.endTime && x.date.Equals(exam.date))
                                {
                                    TempData["msg"] = "Exam class clashes with other Exams";
                                    return(View("ManageExams", exam));
                                }
                            }

                            /* check moed a is before b */
                            if (exam.moed == "A")
                            {
                                /* check if moed B exam exist */
                                var moedBExam =
                                    (from row in exmaDb.Exams
                                     where row.courseId.Equals(exam.courseId) && row.moed.Equals("B")
                                     select row).FirstOrDefault();

                                if (moedBExam != null)
                                {
                                    if (moedBExam.date <= exam.date)
                                    {
                                        TempData["msg"] = "Moed A needs to be before Moed B";
                                        return(View("ManageExams", exam));
                                    }
                                }
                            }

                            if (exam.moed == "B")
                            {
                                /* check if moed A exam exist */
                                var moedAExam =
                                    (from row in exmaDb.Exams
                                     where row.courseId.Equals(exam.courseId) && row.moed.Equals("A")
                                     select row).FirstOrDefault();

                                if (moedAExam != null)
                                {
                                    if (moedAExam.date >= exam.date)
                                    {
                                        TempData["msg"] = "Moed A needs to be before Moed B";
                                        return(View("ManageExams", exam));
                                    }
                                }
                            }


                            /* check if exam exist */
                            var examInDb =
                                (from row in exmaDb.Exams
                                 where row.courseId.Equals(exam.courseId) && row.moed.Equals(exam.moed)
                                 select row).FirstOrDefault();

                            /* if exam exsist update */
                            if (examInDb != null)
                            {
                                examInDb.courseId   = exam.courseId;
                                examInDb.courseName = exam.courseName;
                                examInDb.moed       = exam.moed;
                                examInDb.date       = exam.date;
                                examInDb.startTime  = exam.startTime;
                                examInDb.endTime    = exam.endTime;
                                examInDb.className  = exam.className;

                                exmaDb.SaveChanges();
                            }
                            /*else insert new exam */
                            else
                            {
                                exmaDb.Exams.Add(exam);
                                exmaDb.SaveChanges();
                            }

                            TempData["goodMsg"] = "Inserted\\updated exam";
                            /* redirect with succsees message */
                            return(View("ManageExams", new Exam()));
                        }
            }
            else
            {
                return(View("ManageExams", exam));
            }
        }