public ActionResult Edit(int id = 0)
 {
     ICourseService courseService = CourseService.GetInstance<CourseRepository>();
     CourseReport course = courseService.GetCourseReport(id);
     CourseViewModel model = TempData["CourseModel"] as CourseViewModel ?? new CourseViewModel();
     if (course != null) {
         model = new CourseViewModel(course);
         ICourseClassService courseClassService = CourseClassService.GetInstance<CourseClassRepository>();
         IEnumerable<CourseClass> courseClasses = courseClassService.GetClassesByCourseId(course.Id);
         List<CourseClassViewModel> courseClassModels = new List<CourseClassViewModel>();
         foreach (CourseClass cc in courseClasses) {
             courseClassModels.Add(new CourseClassViewModel() {
                 Id = cc.Id,
                 Description = cc.Description,
                 BeginTime = cc.BeginTime,
                 EndTime = cc.EndTime,
                 Finished = cc.Finished,
                 Broadcasting = cc.Broadcasting
             });
         }
         model.CourseClasses = courseClassModels;
     }
     ViewData["Errors"] = TempData["Errors"] ?? new List<ModelError>();
     return View(model);
 }
        public ActionResult Index(int id = 0)
        {
            ICourseService courseService = CourseService.GetInstance<CourseRepository>();
            Course course = courseService.Get(id);
            if (id == 0 || course == null) {
                // If no id specified, redirect to profile's dashboard
                return RedirectToHash("Dashboard", "Index", "dashboard");
            }
            CourseViewModel model = new CourseViewModel(course);

            Session["CurrentCourseId"] =  model.Id;

            ICourseClassService ccService = CourseClassService.GetInstance<CourseClassRepository>();
            IEnumerable<CourseClass> classes = ccService.GetClassesByCourseId(course.Id);
            List<CourseClassViewModel> ccModels = new List<CourseClassViewModel>();
            foreach (CourseClass cc in classes) {
                ccModels.Add(new CourseClassViewModel(cc));
            }
            ViewBag.Classes = ccModels;

            if (IsInRole(Role.ROLE_TEACHER)) {
                bool res = courseService.IsTeacherOfCourse(model.Id, CurrentProfile.Id);
                ViewBag.IsTeacherOfClass = res;
                if (res) {
                    // This gets all students in a given course
                    ICourseStudentService service = CourseStudentService.GetInstance<CourseStudentRepository>();
                    IEnumerable<CourseStudent.Report> students = service.GetCourseStudentsByCourseId(model.Id);
                    ViewBag.CourseStudents = students;

                    // This gets all classes for a given course
                    ICourseClassService courseClassService = CourseClassService.GetInstance<CourseClassRepository>();
                    IEnumerable<CourseClass> courseClasses = courseClassService.GetClassesByCourseId(course.Id);
                    List<CourseClassViewModel> courseClassModels = new List<CourseClassViewModel>();
                    foreach (CourseClass cc in courseClasses) {
                        courseClassModels.Add(new CourseClassViewModel() {
                            Id = cc.Id,
                            Description = cc.Description,
                            BeginTime = cc.BeginTime,
                            EndTime = cc.EndTime,
                            Finished = cc.Finished,
                            Broadcasting = cc.Broadcasting
                        });
                    }
                    ViewBag.CourseClasses = courseClassModels;
                }
            } else {
                ViewBag.IsTeacherOfClass = false;
            }

            return View(model);
        }
        public ActionResult Edit(CourseViewModel model, HttpPostedFileBase file)
        {
            if (!ModelState.IsValid || model.TeacherId == 0) {
                TempData["CourseModel"] = model;
                if (model.TeacherId == 0) {
                    ModelState.AddModelError("NoTeacher", "You must specify a teacher.");
                }
                TempData["Errors"] = ModelState.Values.SelectMany(v => v.Errors).ToList();
                if (model.Id > 0) {
                    return RedirectToAction("Edit", "Course", new { id = model.Id });
                } else {
                    return RedirectToAction("Edit", "Course");
                }
            }
            ICourseService courseService = CourseService.GetInstance<CourseRepository>();
            ICourseTeacherService courseTeacherService = CourseTeacherService.GetInstance<CourseTeacherRepository>();
            Course course = model.Id == 0 ? new Course() : courseService.Get(model.Id);
            course.Title = model.Title;
            course.Description = model.Description;
            course.AboutCourse = model.AboutCourse;
            course.Syllabus = model.Syllabus;
            course.Lectures = model.Lectures;
            course.IsPublic = model.IsPublic;
            if (course.SchoolId == 0) {
                course.SchoolId = CurrentProfile.Id;
            }

            string filename;
            if (file != null && file.ContentLength > 0) {
                filename = Guid.NewGuid().ToString() + "." + Path.GetExtension(file.FileName);
                string path = Path.Combine(Server.MapPath(Constants.UPLOADS_PATH), filename);
                FileHelper.CreateFile(path, file.InputStream, true);

            } else {
                filename = "class_default.jpg";
            }

            course.PictureUrl = filename;

            if (model.Id == 0) {
                course = courseService.Insert(course);
            } else {
                courseService.Update(course);
            }

            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.CourseId = course.Id;
            courseTeacher.TeacherId = model.TeacherId;
            courseTeacherService.Insert(courseTeacher);

            return RedirectToAction("Edit", "Course", new { id = course.Id });
        }