public async Task <IActionResult> Edit( Guid id, [Bind("ParticipantId,RegistrationDate,OrganizationId,RefParticipantTypeId,RefParticipantCohortId,ParticipantCode,FirstName,MiddleName,LastName,RefSexId,BirthDate,Age,Disability,RefDisabilityTypeId,Phone,Mobile,Email,Facebook,InstantMessenger,RefLocationId,Address")] Participant participant, [Bind("ParticipantId,StudentCode,RefStudentTypeId,RefStudentSpecializationId,RefStudentYearOfStudyId,ParentGuardian")] Student student, [Bind("ParticipantId,RefTeacherTypeId,RefTeacherPositionId,RefTeacherEmploymentTypeId,GradeLevels")] Teacher teacher, [Bind("ParticipantId,RefEducationAdministratorTypeId,RefEducationAdministratorPositionId,RefEducationAdministratorOfficeId")] EducationAdministrator educationAdministrator, string[] RefLocationId, Guid?GroupId ) { if (id != participant.ParticipantId) { return(NotFound()); } // Model Bindings for Student, Teacher, and Education Administrator are null if not included in POST Method. // Recommended to set to null before Model Validation if any property on these entities are required. if (ModelState.IsValid) { try { // *** RefLocation *** // Gets the last non-null item in RefLocationId array, // the array might have null values for locations left empty // and this assigns the last non-null location to RefLocationId if (RefLocationId.Length > 0) { var location = (from r in RefLocationId where !string.IsNullOrEmpty(r) select r) .OrderByDescending(r => r).Count(); participant.RefLocationId = RefLocationId[location - 1]; } _context.Update(participant); // Students if (participant.RefParticipantTypeId == 1) { student.ParticipantId = participant.ParticipantId; _context.Update(student); } // Teachers else if (participant.RefParticipantTypeId == 2) { teacher.ParticipantId = participant.ParticipantId; _context.Update(teacher); } // Education Administrators else if (participant.RefParticipantTypeId == 3) { educationAdministrator.ParticipantId = participant.ParticipantId; _context.Update(educationAdministrator); } await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ParticipantExists(participant.ParticipantId)) { return(NotFound()); } else { throw; } } TempData["messageType"] = "success"; TempData["messageTitle"] = "RECORD UPDATED"; TempData["message"] = "Record successfully updated"; // If Action route includes GroupId redirect to GroupEnrollment if (GroupId != null) { return(RedirectToAction(nameof(Index), "GroupEnrollments", new { id = GroupId })); } // Else redirect to Participants else { return(RedirectToAction(nameof(Index))); } } ViewData["RefLocationId"] = new SelectList(_context.Locations, "RefLocationId", "RefLocationId", participant.RefLocationId); ViewData["OrganizationId"] = new SelectList(_context.Organizations, "OrganizationId", "OrganizationCode", participant.OrganizationId); // List of Participant Types - excluding Student, Teacher, and Education Administrators int[] excludedParticipantTypesIds = { 1, 2, 3 }; ViewData["RefParticipantTypeId"] = new SelectList(_context.ParticipantTypes .Where(p => !excludedParticipantTypesIds.Contains(p.RefParticipantTypeId)), "RefParticipantTypeId", "ParticipantType", participant.RefParticipantTypeId); ViewData["RefParticipantCohortId"] = new SelectList(_context.ParticipantCohorts, "RefParticipantCohortId", "ParticipantCohort", participant.RefParticipantCohortId); ViewData["RefSexId"] = new SelectList(_context.Sex, "RefSexId", "Sex", participant.RefSexId); //ViewData["ParentId"] = participant.ParentId; return(View(participant)); }
public async Task <IActionResult> Create( [Bind("ParticipantId,RegistrationDate,OrganizationId,RefParticipantTypeId,RefParticipantCohortId,ParticipantCode,FirstName,MiddleName,LastName,RefSexId,BirthDate,Age,Disability,RefStudentDisabilityTypeId,Phone,Mobile,Email,Facebook,InstantMessenger,RefLocationId,Address")] Participant participant, [Bind("ParticipantId,StudentCode,RefStudentTypeId,RefStudentSpecializationId,RefStudentYearOfStudyId,ParentGuardian")] Student student, [Bind("ParticipantId,RefTeacherTypeId,RefTeacherPositionId,RefTeacherEmploymentTypeId,GradeLevels")] Teacher teacher, [Bind("ParticipantId,RefEducationAdministratorTypeId,RefEducationAdministratorPositionId,RefEducationAdministratorOfficeId")] EducationAdministrator educationAdministrator, string[] RefLocationId, Guid?GroupId ) { // Model Bindings for Student, Teacher, and Education Administrator are null if not included in POST. // Recommended to set to null before Model Validation if any property on these entities are required. if (ModelState.IsValid) { // *** RefLocation *** // Gets the last non-null item in RefLocationId array, // the array might have null values for locations left empty // and this assigns the last non-null location to RefLocationId if (RefLocationId.Length > 0) { var location = (from r in RefLocationId where !string.IsNullOrEmpty(r) select r) .OrderByDescending(r => r).Count(); participant.RefLocationId = RefLocationId[location - 1]; } // Participant participant.ParticipantId = Guid.NewGuid(); _context.Add(participant); // Students if (participant.RefParticipantTypeId == 1) { student.ParticipantId = participant.ParticipantId; _context.Add(student); } // Teachers else if (participant.RefParticipantTypeId == 2) { teacher.ParticipantId = participant.ParticipantId; _context.Add(teacher); } // Education Administrators else if (participant.RefParticipantTypeId == 3) { educationAdministrator.ParticipantId = participant.ParticipantId; _context.Add(educationAdministrator); } // *** Create Group Enrollment and/or Group Evaluations *** // If Create action route includes GroupId parameter (from GroupEnrollment) // Creates GroupEnrollment record for this Participant if (GroupId != null) { // Get related Group var group = await _context.Groups .Include(x => x.Programs) .Where(x => x.GroupId == GroupId) .FirstOrDefaultAsync(); // Create GroupEnrollment for this Participant GroupEnrollment groupEnrollment = new GroupEnrollment { GroupEnrollmentId = Guid.NewGuid(), GroupId = group.GroupId, ParticipantId = participant.ParticipantId, RefEnrollmentStatusId = 1, //Status Enrolled EnrollmentDate = participant.RegistrationDate }; _context.Add(groupEnrollment); // If the Program selected for this Group has related ProgramAssessments // Creates the GroupEvaluation records for each ProgramAssessment for this Participant if (group.Programs.HasAssessment == true) { List <ProgramAssessment> programAssessments = new List <ProgramAssessment>(); // Get ProgramAssessments list related to this Group/Program programAssessments = await _context.ProgramAssessments .Where(x => x.ProgramId == group.ProgramId) .ToListAsync(); if (programAssessments != null) { foreach (var assessment in programAssessments) { GroupEvaluation groupEvaluation = new GroupEvaluation { GroupEvaluationId = Guid.NewGuid(), GroupEnrollmentId = groupEnrollment.GroupEnrollmentId, RefEvaluationStatusId = 1, // Status Enrolled ProgramAssessmentId = assessment.ProgramAssessmentId, }; _context.GroupEvaluations.Add(groupEvaluation); } } } } await _context.SaveChangesAsync(); TempData["messageType"] = "success"; TempData["messageTitle"] = "RECORD CREATED"; TempData["message"] = "New record successfully created"; // If Action route includes GroupId redirect to GroupEnrollment if (GroupId != null) { return(RedirectToAction(nameof(Index), "GroupEnrollments", new { id = GroupId })); } // Else redirect to Participants else { return(RedirectToAction(nameof(Index))); } } // If Model NotValid return to Create View (Get) // If groupId is not null, Create a new participant and enroll to Group Enrollments if (GroupId != null) { var refOrganizationTypeId = await _context.Groups .Where(g => g.GroupId == GroupId) .Select(g => g.Organizations.RefOrganizationTypeId) .FirstOrDefaultAsync(); // if School return OrganizationId for JSTree to pre-select Organization if (refOrganizationTypeId == 1) { ViewData["OrganizationId"] = participant.OrganizationId; } ViewData["GroupId"] = GroupId; } // If groupId is null, Create new Participant only else { // return related organization Ids } // *** For JSTree *** //Logged User OrganizationId Guid?userOrganizacionId = (await _userManager.GetUserAsync(HttpContext.User))?.OrganizationId; if (userOrganizacionId == null) { return(NotFound()); } //Get Logged User Organization var organization = await _context.Organizations .SingleOrDefaultAsync(o => o.OrganizationId == userOrganizacionId); if (organization == null) { return(NotFound()); } //Returns the top hiearchy organization for JSTree ViewData["ParentOrganizationId"] = organization.OrganizationId; // *** Select Lists *** // RefLocation var locationTypes = _context.LocationTypes; ViewData["RefLocationTypes"] = locationTypes; ViewData["RefLocationTypesCount"] = locationTypes.Count(); ViewData["RefLocationId"] = new SelectList(_context.Locations .Include(x => x.LocationTypes) .Where(x => x.LocationTypes.LocationLevel == 1 && x.ParentLocationId == null) .Select(x => new { x.RefLocationId, x.LocationName }), "RefLocationId", "LocationName"); // Participants - All ViewData["ParentId"] = participant.OrganizationId; ViewData["RefSexId"] = new SelectList(_context.Sex, "RefSexId", "Sex"); ViewData["RefDisabilityTypeId"] = new SelectList(_context.DisabilityTypes, "RefDisabilityTypeId", "DisabilityType"); ViewData["RefParticipantTypeId"] = participant.RefParticipantTypeId; ViewData["RefParticipantCohortId"] = new SelectList(_context.ParticipantCohorts, "RefParticipantCohortId", "ParticipantCohort"); // Students if (participant.RefParticipantTypeId == 1) { ViewData["RefStudentTypeId"] = new SelectList(_context.StudentTypes, "RefStudentTypeId", "StudentType", student.RefStudentTypeId); ViewData["RefStudentSpecializationId"] = new SelectList(_context.StudentSpecializations, "RefStudentSpecializationId", "StudentSpecialization", student.RefStudentSpecializationId); ViewData["RefStudentYearOfStudyId"] = new SelectList(_context.StudentYearOfStudies, "RefStudentYearOfStudyId", "StudentYearOfStudy"); } // Teachers else if (participant.RefParticipantTypeId == 2) { ViewData["RefTeacherTypeId"] = new SelectList(_context.TeacherTypes, "RefTeacherTypeId", "TeacherType", teacher.RefTeacherTypeId); ViewData["RefTeacherPositionId"] = new SelectList(_context.TeacherPositions, "RefTeacherPositionId", "TeacherPosition", teacher.RefTeacherPositionId); ViewData["RefTeacherEmploymentTypeId"] = new SelectList(_context.TeacherEmploymentTypes, "RefTeacherEmploymentTypeId", "TeacherEmploymentType"); } // Education Administrators else if (participant.RefParticipantTypeId == 3) { ViewData["RefEducationAdministratorTypeId"] = new SelectList(_context.EducationAdministratorTypes, "RefEducationAdministratorTypeId", "EducationAdministratorType", educationAdministrator.RefEducationAdministratorTypeId); ViewData["RefEducationAdministratorOfficeId"] = new SelectList(_context.EducationAdministratorOffices, "RefEducationAdministratorOfficeId", "EducationAdministratorOffice", educationAdministrator.RefEducationAdministratorPositionId); ViewData["RefEducationAdministratorPositionId"] = new SelectList(_context.EducationAdministratorPositions, "RefEducationAdministratorPositionId", "EducationAdministratorPosition", educationAdministrator.RefEducationAdministratorPositionId); } return(RedirectToAction(nameof(Create), "Participants", new { participant.OrganizationId, GroupId, participant.RefParticipantTypeId })); }