public static Enrollment CreateNewEnrollment(Course course, ApplicationUser user, ApplicationDbContext context) { var passedDate = DateTime.Parse("1900-1-1"); var newEnrollment = new Enrollment { ApplicationUser = user, PassedDate=passedDate, Course= course, EnrollmentDate = DateTime.UtcNow }; context.Enrollments.Add(newEnrollment); try { context.SaveChanges(); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Debug.Print("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Debug.Print("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } CreateScoRecordsForCourse(newEnrollment, context); return newEnrollment; }
public void SaveNoteToInstructorEnrollmentRecord(CourseViewModel form, Enrollment enrollment, ModelStateDictionary modelState, ApplicationUser instructor) { if (enrollment != null) { if (modelState.IsValid) { if (HttpUtility.HtmlDecode(form.EnrollmentNote) != null) { enrollment.Note = HttpUtility.HtmlDecode(form.EnrollmentNote); enrollment.ApplicationUser = instructor; _context.Entry(enrollment).State = EntityState.Modified; try { _context.SaveChanges(); } catch (DbEntityValidationException ex) { var sb = new StringBuilder(); foreach (DbEntityValidationResult failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); foreach (DbValidationError error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } throw new DbEntityValidationException( "Entity Validation Failed - errors follow:\n" + sb, ex ); // Add the original exception as the innerException } } } } }
public static void CreateScoRecordsForCourse(Enrollment enrollment, ApplicationDbContext context) { var courseTemplateId = enrollment.Course.CourseTemplateId; var courseScos = context.CourseScos.Include(c => c.Sco).Where(cm => cm.CourseTemplateId == courseTemplateId); foreach (var item in courseScos) { var scoRecord = new ScoRecord { Title = item.Title, EnrollmentId = enrollment.Id, CatalogueNumber = item.CatalogueNumber, ScoId = item.Sco.Id, ScormName = "", ScormValue = "", LessonStatus = "", LastClosed = DateTime.Parse("1900-1-1"), Stored = DateTime.Parse("1900-1-1"), TimeStamp = DateTime.Parse("1900-1-1"), CourseTemplateId = enrollment.Course.CourseTemplateId, CourseId = enrollment.CourseId, ActorId = enrollment.ApplicationUserId, //IsEnabled = item.RequiredCourseScoId == 0, IsFinalExam = item.IsFinalExam }; //var requiredRecord = context.CourseScos.Find(item.RequiredCourseScoId); //if (requiredRecord != null) // scoRecord.RequiredScoId = requiredRecord.ScoId; context.ScoRecords.Add(scoRecord); } context.SaveChanges(); }
public ActionResult NewEnrollment(CourseEnrollmentViewModel form) { if (!ModelState.IsValid) { return View(form); } var user = _context.Users.Single(u => u.Id == form.UserId); var passedDate = form.PassedDate ?? DateTime.Parse("1900-1-1"); var cohortId = Convert.ToInt32(form.CourseId); var course = _context.Courses.Single(c => c.Id == cohortId); var newEnrollment = new Enrollment { ApplicationUser=user, PassedDate=passedDate, Course = course, Note = course.Note, }; _context.Enrollments.Add(newEnrollment); _context.SaveChanges(); var enrollmentId = newEnrollment.Id; var enrollment = _context.Enrollments.Include(c=>c.Course).FirstOrDefault(e=>e.Id==enrollmentId); EnrollmentServices.CreateScoRecordsForCourse(enrollment, _context); var id = cohortId; return RedirectToAction("Edit", "ManageCourses", new { id }); }
public ActionResult NewEnrollment(CohortEnrollmentViewModel form) { if (!ModelState.IsValid) { return System.Web.UI.WebControls.View(form); } var user = _context.Users.Single(u => u.Id == form.UserId); var passedDate = form.PassedDate ?? DateTime.Parse("1900-1-1"); var cohortId = Convert.ToInt32(form.CohortId); var cohort = _context.Courses.Single(c => c.Id == cohortId); var newEnrollment = new Enrollment(user, passedDate, cohort); _context.Enrollments.Add(newEnrollment); _context.SaveChanges(); var enrollmentId = newEnrollment.Id; var enr = _context.Enrollments.Find(enrollmentId); _context.Courses.Find(enr.Course.Id); EnrollmentServices.CreateScoRecordsForCourse(enr, _context); _context.SaveChanges(); var id = cohortId; return RedirectToAction("Enrollments", "Courses", new { id }); }