예제 #1
0
        // GET: Enrollment/Create
        public ActionResult Create()
        {
            var viewModel = new EnrollmentViewModel();

            // Teacher can enroll to his course only
            if (User.IsInRole("Teacher"))
            {
                string currentUserId = User.Identity.GetUserId();
                var courses = db.Courses.Where(c => c.Teacher.User.Id == currentUserId).ToList();
                viewModel.Courses = courses;
            }

            // Student can enroll to courses he has not joined
            if (User.IsInRole("Student"))
            {
                string currentUserId = User.Identity.GetUserId();

                Student student = db.Students.FirstOrDefault(s => s.UserId == currentUserId);
                if (student == null) // Check student is existed or not
                {
                    return HttpNotFound();
                }
                // Select Passed course from Enrollment table
                var enrollments = db.Enrollments.Where(e => e.StudentId == student.Id).ToList();
                var courses = db.Courses.ToList();

                if (enrollments == null)
                {
                    return View(courses);
                }

                foreach (Enrollment enrollment in enrollments)
                {
                    Course course = db.Courses.FirstOrDefault(c => c.Id == enrollment.CourseId);
                    if (course == null)
                    {
                        return HttpNotFound();
                    }
                    courses.Remove(course);
                }
                viewModel.Courses = courses;
                viewModel.UserName = User.Identity.GetUserName();
           }

            if (User.IsInRole("Admin"))
            {
                var courses = db.Courses.Include(c => c.Semester).ToList();
                viewModel.Courses = courses;
            }
            //viewModel.Courses = db.Courses.ToList();
            ViewData["Error"] = null;
            ViewData["Success"] = null;
            return View(viewModel);
        }
예제 #2
0
        public ActionResult Create([Bind(Include = "UserName,SelectedCourses")] EnrollmentViewModel enrollModel)
        {
            if (ModelState.IsValid)
            {
                System.Diagnostics.Debug.WriteLine(enrollModel.UserName);

                ApplicationUser user = db.Users.FirstOrDefault(u => u.UserName == enrollModel.UserName);
                if (user != null) //check user is existed or not
                {
                    Student student = db.Students.FirstOrDefault(s => s.UserId == user.Id);
                    if (student != null) //check student is existed or not
                    {
                        EnrollmentViewModel enrollment = new EnrollmentViewModel();
                        enrollment.SelectedCourses = enrollModel.SelectedCourses;
                        foreach (var item in enrollModel.SelectedCourses)
                        {
                            //check Enrollment is exited or not
                            Enrollment enroll = db.Enrollments.FirstOrDefault(e => e.StudentId == student.Id && e.CourseId == item);
                            if (enroll == null)
                            {
                                enroll = new Enrollment { StudentId = student.Id, CourseId = item };
                                db.Enrollments.Add(enroll);
                                db.SaveChanges();
                            }
                            else {
                                ViewData["Error"] += db.Courses.Find(item).Name + ", ";
                                enrollModel.Courses = db.Courses.ToList();
                                return View(enrollModel);
                            }
                        }
                    }
                    else {
                        ViewData["Error"] = "Student is not existed! Please input other!";
                        enrollModel.Courses = db.Courses.ToList();
                        return View(enrollModel);
                    }
                }
                else {
                    ViewData["Error"] = "User is not existed! Please input other!";
                    enrollModel.Courses = db.Courses.ToList();
                    return View(enrollModel);
                }               

            }

            //Enroll successfully!
            TempData["Success"] = "Enrollment successfully!";
            return RedirectToAction("Create");
        }