public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Attach(ProgramEnrollment).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProgramEnrollmentExists(ProgramEnrollment.LMSProgramID)) { return(NotFound()); } else { throw; } } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync() { Console.WriteLine("ModelState.IsValid: " + ModelState.IsValid); if (!ModelState.IsValid) { Console.WriteLine("ModelState IS NOT valid"); return(Page()); } Console.WriteLine("ModelState IS valid"); //////////////////////////////////////////////////////////// // Retrieve "NONE TO PENDING" StatusTransition //////////////////////////////////////////////////////////// var statusTransition = await _context.StatusTransitions .Where(st => st.TransitionCode == TransitionCodeConstants.NONE_TO_PENDING) .SingleOrDefaultAsync(); //////////////////////////////////////////////////////////// // Create ProgramEnrollment (root class), adding newly // created EnrollmentHistory to the EnrollmentHistory // collection //////////////////////////////////////////////////////////// var programEnrollment = new ProgramEnrollment { LMSProgramID = Int32.Parse(Input.LMSProgramID), StudentUserId = _userManager.GetUserId(User), UserCreated = _userManager.GetUserId(User), DateCreated = DateTime.Now, StatusCode = StatusCodeConstants.PENDING, EnrollmentHistory = new List <EnrollmentHistory> { new EnrollmentHistory { StatusTransitionID = statusTransition.StatusTransitionID, ActorUserId = _userManager.GetUserId(User), ActorRemarks = Input.Remarks, DateCreated = DateTime.Now } } }; ///////////////////////////////////////////////////////////////////// // Persist ProgramEnrollment plus EnrollmentHistory to the database ///////////////////////////////////////////////////////////////////// _context.ProgramEnrollments.Add(programEnrollment); await _context.SaveChangesAsync(); /////////////////////////////////////////////////////////////////// // Log the 'ENROLLMENT_REQUSTED' event /////////////////////////////////////////////////////////////////// ApplicationUser student = await GetCurrentUserAsync(); // _eventLogService.LogEvent(EventTypeCodeConstants.ENROLLMENT_REQUSTED, student, programEnrollment.ProgramEnrollmentID); _eventLogService.LogEvent(EventTypeCodeConstants.ENROLLMENT_REQUSTED, student, programEnrollment); //////////////////////////////////////////////////////////////////////////////////// // Send email notification to approvers who have their EmailNotify flag set to true //////////////////////////////////////////////////////////////////////////////////// IList <ProgramApprover> approverList = await _context.ProgramApprovers .Where(pa => pa.LMSProgramID == programEnrollment.LMSProgramID && pa.EmailNotify == true) .Include(pa => pa.Approver) .Include(pa => pa.LMSProgram) .AsNoTracking() .ToListAsync(); foreach (ProgramApprover approverObj in approverList) { string email = approverObj.Approver.Email; string subject = "Program Enrollment Request (" + approverObj.LMSProgram.LongName + ")"; string message = student.FullName + " has requested to enroll in " + approverObj.LMSProgram.LongName; await _emailSender.SendEmailAsync(email, subject, message); } //////////////////////////////////////////////////////////////////////////////////// // Send email notification to LMS Program's common inbox (if any) //////////////////////////////////////////////////////////////////////////////////// LMSProgram lmsProgram = await _context.LMSPrograms .Where(p => p.LMSProgramID == programEnrollment.LMSProgramID && p.CommonInbox != null) .AsNoTracking() .SingleOrDefaultAsync(); if (lmsProgram != null && String.IsNullOrEmpty(lmsProgram.CommonInbox) == false) { string email = lmsProgram.CommonInbox; string subject = "Program Enrollment Request (" + lmsProgram.LongName + ")"; string message = student.FullName + " has requested to enroll in " + lmsProgram.LongName; await _emailSender.SendEmailAsync(email, subject, message); } ///////////////////////////////////////////////////////////////// // Redirect to Enrollmentl Index Page ///////////////////////////////////////////////////////////////// return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } //////////////////////////////////////////////////////////// // Step #1: // Check to see if records exists //////////////////////////////////////////////////////////// var lvProgramEnrollment = await _context.ProgramEnrollments .Include(p => p.LMSProgram) .SingleOrDefaultAsync(m => m.ProgramEnrollmentID == id); //////////////////////////////////////////////////////////// // Return "Not Found" if record doesn't exist //////////////////////////////////////////////////////////// if (lvProgramEnrollment == null) { return(NotFound()); } //////////////////////////////////////////////////////////// // Step #2: // Now that record exists, make sure that the logged-in user // is authorized to withdraw this program enrollment //////////////////////////////////////////////////////////// var loggedInUserID = _userManager.GetUserId(User); lvProgramEnrollment = null; lvProgramEnrollment = await _context.ProgramEnrollments .Where(pe => pe.StudentUserId == loggedInUserID && pe.ProgramEnrollmentID == id) .Include(p => p.LMSProgram) .SingleOrDefaultAsync(); ///////////////////////////////////////////////////////////// // We already know that record exists from Step #1 so if we // get a "Not Found" in Step #2, we know it's because the // logged-in user is not authorized to withdraw this // program enrollment. ///////////////////////////////////////////////////////////// if (lvProgramEnrollment == null) { return(Unauthorized()); } //////////////////////////////////////////////////////////// // Retrieve the correct StatusTransition //////////////////////////////////////////////////////////// StatusTransition lvStatusTranstion = null; ////////////////////////////////////////////////////////////////////// // STATUS TRANSITION: PENDING TO WITHDRAEN ////////////////////////////////////////////////////////////////////// if (lvProgramEnrollment.StatusCode.Equals(StatusCodeConstants.PENDING)) { lvStatusTranstion = await _context.StatusTransitions .Where(st => st.TransitionCode == TransitionCodeConstants.PENDING_TO_WITHDRAWN) .SingleOrDefaultAsync(); } ////////////////////////////////////////////////////////////////////// // STATUS TRANSITION: APPROVED TO WITHDRAEN ////////////////////////////////////////////////////////////////////// if (lvProgramEnrollment.StatusCode.Equals(StatusCodeConstants.APPROVED)) { lvStatusTranstion = await _context.StatusTransitions .Where(st => st.TransitionCode == TransitionCodeConstants.APPROVED_TO_WITHDRAWN) .SingleOrDefaultAsync(); } //////////////////////////////////////////////////////////////// // Create EnrollmentHistory using the correct StatusTranistion //////////////////////////////////////////////////////////////// var lvEnrollmentHistory = new EnrollmentHistory() { StatusTransitionID = lvStatusTranstion.StatusTransitionID, ActorUserId = _userManager.GetUserId(User), ActorRemarks = Input.Remarks, DateCreated = DateTime.Now }; //////////////////////////////////////////////////////////// // Instantiate EnrollmentHistory Collection, if necessary //////////////////////////////////////////////////////////// if (lvProgramEnrollment.EnrollmentHistory == null) { lvProgramEnrollment.EnrollmentHistory = new List <EnrollmentHistory>(); } /////////////////////////////////////////////////////////////////// // Add newly created EnrollmentHistory with StatusTransition // to ProgramEnrollment's EnrollmentHistory Collection /////////////////////////////////////////////////////////////////// lvProgramEnrollment.EnrollmentHistory.Add(lvEnrollmentHistory); ///////////////////////////////////////////////////////////////// // Update ProgramEnrollment Record with // 1. EnrollmentStatus of "WITHDRAWN" // 2. ApproverUserId (logged-in user) // 3. EnrollmentHistory (PENDING TO WiTHDRAWN) ///////////////////////////////////////////////////////////////// lvProgramEnrollment.StatusCode = StatusCodeConstants.WITHDRAWN; lvProgramEnrollment.ApproverUserId = _userManager.GetUserId(User); _context.ProgramEnrollments.Update(lvProgramEnrollment); await _context.SaveChangesAsync(); //////////////////////////////////////////////////////////////////////////////////// // Send email notification to approvers who have their EmailNotify flag set to true //////////////////////////////////////////////////////////////////////////////////// IList <ProgramApprover> approverList = await _context.ProgramApprovers .Where(pa => pa.LMSProgramID == lvProgramEnrollment.LMSProgramID && pa.EmailNotify == true) .Include(pa => pa.Approver) .Include(pa => pa.LMSProgram) .AsNoTracking() .ToListAsync(); ApplicationUser student = await GetCurrentUserAsync(); foreach (ProgramApprover approverObj in approverList) { string email = approverObj.Approver.Email; string subject = "Program Enrollment Withdrawal (" + approverObj.LMSProgram.LongName + ")"; string message = student.FullName + " has withdrawn his/her enrollment in " + approverObj.LMSProgram.LongName; await _emailSender.SendEmailAsync(email, subject, message); } //////////////////////////////////////////////////////////////////////////////////// // Send email notification to LMS Program's common inbox (if any) //////////////////////////////////////////////////////////////////////////////////// LMSProgram lmsProgram = await _context.LMSPrograms .Where(p => p.LMSProgramID == lvProgramEnrollment.LMSProgramID && p.CommonInbox != null) .AsNoTracking() .SingleOrDefaultAsync(); if (String.IsNullOrEmpty(lmsProgram.CommonInbox) == false) { string email = lmsProgram.CommonInbox; string subject = "Program Enrollment Withdrawal (" + lmsProgram.LongName + ")"; string message = student.FullName + " has withdrawn his/her enrollment in " + lmsProgram.LongName; await _emailSender.SendEmailAsync(email, subject, message); } ///////////////////////////////////////////////////////////////// // Redirect to Student Index Page ///////////////////////////////////////////////////////////////// return(RedirectToPage("./Index")); }