Ejemplo n.º 1
0
        public ActionResult CreateCourse()
        {
            CoursesCreateVM model = new CoursesCreateVM();
            model.Subjects = unitOfWork.SubjectRepository.GetAll(true);

            return View("CreateEditCourse", model);
        }
Ejemplo n.º 2
0
        public ActionResult CreateEditCourse(CoursesCreateVM model)
        {
            if (ModelState.IsValid)
            {
                Course course;

                if (model.Id == 0)
                {
                    course = new Course();
                }
                else
                {
                    course = unitOfWork.CourseRepository.GetById(model.Id);
                }

                if (unitOfWork.CourseRepository.CheckIfCourseCodeExists(model.CourseCode))
                {
                    if (model.CourseCode != course.Code)
                    {
                        model.Subjects = unitOfWork.SubjectRepository.GetAll(true);

                        TempData.FlashMessage("The course code you are trying to create is already in use", null, FlashMessageTypeEnum.Red);
                        return View(model);
                    }
                }

                int oldCourseCode = course.Code;

                course.Name = model.Name;

                model.Subjects = unitOfWork.SubjectRepository.GetAll();

                List<Subject> subjects = new List<Subject>();

                foreach (var s in model.Subjects)
                {
                    if ((Request.Form[s.Id.ToString()] != null) && (Request.Form[s.Id.ToString()] == "on"))
                    {
                        subjects.Add(s);
                    }
                }

                if (model.Id == 0)
                {
                    course.Code = model.CourseCode;
                    unitOfWork.CourseRepository.Insert(course);
                    TempData.FlashMessage("Course Created");
                }
                else
                {
                    if (course.Code != model.CourseCode)
                    {
                        course.Code = model.CourseCode;

                        List<Student> students = new List<Student>();
                        foreach (var s in unitOfWork.StudentRepository.GetAll(null, false))
                        {
                            if (s.FacultyNumber.Substring(4, 2) == oldCourseCode.ToString("00"))
                            {
                                students.Add(s);
                            }
                        }

                        GenerateFacultyNumber.ResetFacultyNumbersbyCourseId(course.Id, students);

                        unitOfWork.StudentRepository.Save();

                        foreach (var s in students)
                        {
                            s.FacultyNumber = GenerateFacultyNumber.Generate(course, unitOfWork);
                        }
                    }

                    course.Code = model.CourseCode;
                    unitOfWork.CourseRepository.Update(course);
                    TempData.FlashMessage("Course Edited");
                }
                unitOfWork.Save();

                unitOfWork.CoursesSubjectsRepository.UpdateTable(course, subjects);

                return RedirectToAction("ManageCourses", "Admin");
            }

            model.Subjects = unitOfWork.SubjectRepository.GetAll(true);
            return View(model);
        }
Ejemplo n.º 3
0
        public ActionResult EditCourse(int? id)
        {
            if (!id.HasValue)
            {
                return RedirectToAction("ManageCourses", "Admin");
            }

            Course course = unitOfWork.CourseRepository.GetById(id.Value);

            if (course == null)
            {
                return RedirectToAction("ManageCourses", "Admin");
            }

            CoursesCreateVM model = new CoursesCreateVM();
            model.Id = course.Id;
            model.Name = course.Name;
            model.CourseCode = course.Code;
            model.Subjects = unitOfWork.SubjectRepository.GetAll(true);

            return View("CreateEditCourse", model);
        }