public ActionResult Index() { // Step 1 - Get current user var currentUser = TempData[TempDataValues.CurrentUser] as UserModel; TempData.Keep(TempDataValues.CurrentUser); // Step 2 - Get the number of courses registered/teaching/offered if (currentUser.Role == "student") { ViewBag.NumCourses = StudentCourseAssocService.ListAssocs(new StudentCourseAssocModel { IdStudent = currentUser.IdUser ?? 0 }).Count(); } else if (currentUser.Role == "instructor") { ViewBag.NumCourses = InstructorCourseAssocService.ListAssocs(new InstructorCourseAssocModel { IdInstructor = currentUser.IdUser ?? 0 }).Count(); } else if (currentUser.Role == "admin") { // Get current semester value from config var currentSemester = System.Configuration.ConfigurationManager.AppSettings["Semester"]; ViewBag.NumCourses = CoursesService.ListCourses().Where(c => c.Semester.Equals(currentSemester, StringComparison.InvariantCultureIgnoreCase)).ToList().Count(); } return(View()); }
public ActionResult MyCourses() { var viewString = ViewStrings.My_Courses; // List to store courses var courses = new List <CourseInfoModel>(); // Get current semester value from config var currentSemester = System.Configuration.ConfigurationManager.AppSettings["Semester"]; try { // Step 1 - Get current user var currentUser = TempData[TempDataValues.CurrentUser] as UserModel; TempData.Keep(TempDataValues.CurrentUser); // Step 2 - Get list of all courses registered/teaching if (currentUser.Role == "student") { // For each registration record, pull in the course info and add it to the list foreach (var course in StudentCourseAssocService.ListAssocs(new StudentCourseAssocModel { IdStudent = currentUser.IdUser ?? 0 })) { var courseInfo = CoursesService.GetCourse(new CourseModel { IdCourse = course.IdCourse }); var instructorInfo = UsersService.GetUser(courseInfo.CourseInstructorId.ToString()); courses.Add(new CourseInfoModel { Course = courseInfo, InstructorFirstName = instructorInfo.FirstName, InstructorLastName = instructorInfo.LastName }); } } else if (currentUser.Role == "instructor") { // For each instructor record, pull in the course info and add it to the list foreach (var course in InstructorCourseAssocService.ListAssocs(new InstructorCourseAssocModel { IdInstructor = currentUser.IdUser ?? 0 })) { var courseInfo = CoursesService.GetCourse(new CourseModel { IdCourse = course.IdCourse }); var instructorInfo = UsersService.GetUser(courseInfo.CourseInstructorId.ToString()); courses.Add(new CourseInfoModel { Course = courseInfo, InstructorFirstName = instructorInfo.FirstName, InstructorLastName = instructorInfo.LastName }); } } // Step 3 - Filter courses by current semester courses = courses.Where(c => c.Course.Semester.Equals(currentSemester, StringComparison.InvariantCultureIgnoreCase)).ToList(); // Step 4 - Go to Catalogue view with list return(View(viewString, courses)); } catch (Exception e) { Console.Write(e.Message); return(View(viewString)); } }