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;
        }
Exemple #2
0
        public void AddToCart(Course cohort)
        {
            // Get the matching cart and Product instances
            var cartItem = _db.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId
                && c.CourseId == cohort.Id);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    CourseId = cohort.Id,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
                };
                _db.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart, 
                // then add one to the quantity
                cartItem.Count++;
            }
            // Save changes
            _db.SaveChanges();
        }
        public async Task<ActionResult> CreateCourse([Bind(Include = "Id,Title,MaxEnrollments,CourseTemplateId,Note,OverView,Price,IsLive")] CreateCohortViewModel form)
        {
            if (ModelState.IsValid)
            {
                var courseTemplate = await _context.CourseTemplates.FindAsync(form.CourseTemplateId);
                var course = new Course
                {
                    Title = courseTemplate.Title,
                    MaxEnrollments = form.MaxEnrollments,
                    CourseTemplateId = form.CourseTemplateId,
                    OverView = form.OverView,
                    Price = form.Price,
                    IsLive = form.IsLive,
                };
                _context.Courses.Add(course);
                await _context.SaveChangesAsync();

                var newCourseId = course.Id;
                var newCourseTemplateId = course.CourseTemplateId;

                var courseTemplateResources =
                    _context.CourseTemplateResources.Where(r => r.CourseId == newCourseTemplateId);
               

                //Add the resources associated with the template
                foreach (var resource in courseTemplateResources)
                {
                    var courseResource = new CourseResource
                    {
                        CourseId = newCourseId,
                        ResourceId = resource.ResourceId
                        
                    };
                    _context.CourseResources.Add(courseResource);
                }

                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.Courseid = new SelectList(_context.Products, "Id", "Title");
            return View(form);
        }
        public async Task<ActionResult> CreateCohort([Bind(Include = "Id,Title,MaxEnrollments,CourseTemplateId,Note,OverView,Price,IsLive")] CreateCohortViewModel form)
        {
            //form.Title = "";
            if (ModelState.IsValid)
            {
                var cohort = new Course
                {
                    Title = form.Title,
                    MaxEnrollments = form.MaxEnrollments,
                    CourseTemplateId = form.CourseTemplateId,
                    OverView = form.OverView,
                    Price = form.Price,
                    IsLive = form.IsLive,


                };
                _context.Courses.Add(cohort);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.Courseid = new SelectList(_context.Products, "Id", "Title");
            return View(form);
        }
        public async Task<ActionResult> CreateCourse([Bind(Include = "Id,Title,MaxEnrollments,CourseTemplateId,Note,OverView,Price,IsLive")] CreateCohortViewModel form)
        {
            if (ModelState.IsValid)
            {
                var courseTemplate = await _context.CourseTemplates.FindAsync(form.CourseTemplateId);
                var course = new Course
                {
                    Title = courseTemplate.Title,
                    OverView = courseTemplate.OverView,
                    Note = courseTemplate.Note,

                    MaxEnrollments = form.MaxEnrollments,
                    CourseTemplateId = form.CourseTemplateId,
                    Price = form.Price,
                    IsLive = form.IsLive,
                    
                };
                _context.Courses.Add(course);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string errorMessage = string.Format("{0}:{1}",
                                validationErrors.Entry.Entity.ToString(),
                                validationError.ErrorMessage);
                            // raise a new exception nesting
                            // the current instance as InnerException
                            raise = new InvalidOperationException(errorMessage, raise);
                        }
                    }
                    throw raise;
                }

                

                var newCourseId = course.Id;
                var newCourseTemplateId = course.CourseTemplateId;

                var courseTemplateResources =
                    _context.CourseTemplateResources.Where(r => r.CourseTemplateId == newCourseTemplateId);
               

                //Add the resources associated with the template
                foreach (var resource in courseTemplateResources)
                {
                    var courseResource = new CourseResource
                    {
                        CourseId = newCourseId,
                        ResourceId = resource.ResourceId
                        
                    };
                    _context.CourseResources.Add(courseResource);
                }

                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.Courseid = new SelectList(_context.Products, "Id", "Title");
            return View(form);
        }
        public static string GetCourseTitle(Course course)
        {
            string courseTitle;

            if (course != null)
            {
                const int abbreviationLength = 25;
                var titleLength = course.Title.Length;
                if (titleLength > abbreviationLength)
                {
                    courseTitle = course.Title.Substring(0, abbreviationLength) + "...";
                }
                else
                {
                    courseTitle = course.Title;
                }
            }

            else
            {
                courseTitle = "Unspecified Course";
            }
            return courseTitle;
        }
        public static void SendMeetingNotifications(Course course, Meeting meetingBooked, IEmailService emailService,
            ApplicationDbContext context, ApplicationUser instructor)
        {
            var enrollments = context.Enrollments.Include("ApplicationUser").Where(e => e.CourseId == course.Id);
            foreach (var enrollment in enrollments)
            {
                try
                {
                    var bodyText = GenerateBodyText(meetingBooked, course.Title, enrollment.ApplicationUser, instructor);

                    var confirmationEmailMessage = BuildEmailMessage(enrollment.ApplicationUser, meetingBooked.Title,
                        bodyText);
                    emailService.SendAsync(confirmationEmailMessage);
                    var logMessage = "Notice of meeting for " + course.Title + " - " + meetingBooked.Title + " sent to " +
                                     enrollment.ApplicationUser.FullName;
                    Log4NetHelper.Log("logMessage", LogLevel.INFO, "MeetingService", 133,
                        meetingBooked.Instructor.FullName, null);
                }
                catch (Exception ex)
                {
                    Log4NetHelper.Log("Send Student Message Failed - ", LogLevel.ERROR, "ManageMeetings", 342, "Tester",
                        ex);
                }
            }
        }