/// <summary> /// Get details of the current application in progress. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <Application> GetCurrentApplication(StudentPortalContext _ctx, int?applicationId = null) { // Get the current application ID from the session. int appId = -1; if (applicationId.HasValue) { appId = applicationId.Value; } else if (Session["ApplicationId"] != null) { appId = Convert.ToInt32(Session["ApplicationId"]); } // Find details of the application. Application application = await _ctx.Applications.FindAsync(appId); // Ensure the application exists. if (application == null) { HttpContext.Current.Response.Redirect("~/"); } return(application); }
/// <summary> /// Handle updating an application's details once it has been submitted. /// </summary> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task CompleteApplication(Application application, StudentPortalContext _ctx) { application.Submitted = true; application.Complete = true; application.Finished = DateTime.Now; application.State = "Completed - awaiting review"; await _ctx.SaveChangesAsync(); }
/// <summary> /// Breakdown of application numbers by status code /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <ApplicationProgress, int> ApplicationsByStatus(StudentPortalContext _ctx) { Dictionary <ApplicationProgress, int> mapping = new Dictionary <ApplicationProgress, int>(); foreach (ApplicationProgress progess in _ctx.ApplicationProgress.ToList()) { int count = ( from a in _ctx.Applications where a.Submitted && a.Progress == progess.Id select a.Progress ).Count(); mapping.Add(progess, count); } return(mapping); }
/// <summary> /// Breakdown of the number of completed applications by the available ethnicities. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <Ethnicity, int> ApplicationsByEthnicity(StudentPortalContext _ctx) { Dictionary <Ethnicity, int> mapping = new Dictionary <Ethnicity, int>(); foreach (Ethnicity ethnicity in _ctx.Ethnicity.ToList()) { int count = ( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join e in _ctx.Ethnicity on pd.Ethnicity equals e.Id.ToString() where a.Submitted && ethnicity.Id == e.Id select e.Name ).Count(); mapping.Add(ethnicity, count); } return(mapping); }
/// <summary> /// Breakdown of the number of completed applications by the available schools. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <School, int> ApplicationsBySchool(StudentPortalContext _ctx) { Dictionary <School, int> mapping = new Dictionary <School, int>(); foreach (School school in _ctx.Schools.ToList()) { int count = ( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join eh in _ctx.Education on a.Id equals eh.Application.Id where a.Submitted && eh.SchoolOrCollege == school.Id.ToString() select eh.SchoolOrCollege ).Count(); mapping.Add(school, count); } return(mapping); }
/// <summary> /// Breakdown of applications by course group offering. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <CourseGroup, int> ApplicationsByCourseGroup(StudentPortalContext _ctx) { Dictionary <CourseGroup, int> mapping = new Dictionary <CourseGroup, int>(); foreach (CourseGroup courseGroup in _ctx.CourseGroups.ToList()) { int count = ( from a in _ctx.Applications join cs in _ctx.CourseSelections on a.Id equals cs.Application.Id join c in _ctx.Courses on cs.FirstChoice equals c.Id where a.Submitted && c.Type == courseGroup.Id select c.Id ).Count(); mapping.Add(courseGroup, count); } return(mapping); }
/// <summary> /// Breakdown of the number of completed applications by the available genders. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <Gender, int> ApplicationsByGender(StudentPortalContext _ctx) { Dictionary <Gender, int> mapping = new Dictionary <Gender, int>(); foreach (Gender gender in _ctx.Genders.ToList()) { int count = ( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join g in _ctx.Genders on pd.Gender equals g.Id.ToString() where a.Submitted && pd.Gender == gender.Id.ToString() select g.Name ).Count(); mapping.Add(gender, count); } return(mapping); }
/// <summary> /// Breakdown of application numbers by disabilities identified /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public Dictionary <Disability, int> ApplicationsByDisability(StudentPortalContext _ctx) { Dictionary <Disability, int> mapping = new Dictionary <Disability, int>(); foreach (Disability disability in _ctx.Disabilities.ToList()) { int count = ( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join ud in _ctx.UserDisabilities on a.Id equals ud.Application.Id join d in _ctx.Disabilities on ud.Disability equals d.Id where a.Submitted && d.Id == disability.Id select d.Id ).Count(); mapping.Add(disability, count); } return(mapping); }
/// <summary> /// Check if the provided user is the owner of the provided application. /// Optionally, send them back to the home screen if they do not own the application. /// </summary> /// <param name="applicationId"></param> /// <param name="userName"></param> /// <param name="_ctx"></param> /// <param name="forceRedirect"></param> /// <returns></returns> public async Task <bool> OwnsApplication(int applicationId, string userName, StudentPortalContext _ctx, bool forceRedirect = false) { // Find application and verify it exists Application application = await _ctx.Applications.FindAsync(applicationId); if (application != null) { // Check user owns application if (application.User.UserName == userName) { return(true); } } // Optionally, send back to home screen as they aren't the application owner. if (forceRedirect) { Response.Redirect("~/"); } return(false); }
/// <summary> /// Extract data from the database of applicants who have not finished their full application form yet. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <ApplicantDetails> > IncompleteApplications(StudentPortalContext _ctx) { List <ApplicantDetails> applications = await( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join g in _ctx.Genders on pd.Gender equals g.Id.ToString() where !a.Submitted && !a.Complete orderby a.Started descending select new ApplicantDetails { Id = a.Id, Started = a.Started, Gender = g.Name, Forename = pd.Forename, Surname = pd.Surname, Email = pd.EmailAddress, DateOfBirth = pd.DateOfBirth, MobileNumber = pd.MobileNumber, TelephoneNumber = pd.TelephoneNumber, } ).ToListAsync(); return(applications); }
/// <summary> /// Get the current education history details for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <EducationHistory> GetEducationHistory(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.Education.SingleOrDefaultAsync(e => e.Application.Id == application.Id)); }
/// <summary> /// Get the current qualifications entered for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <Qualification> > GetQualifications(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.Qualifications.Where(q => q.Application.Id == application.Id).ToListAsync()); }
/// <summary> /// Get the current course selection details for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <CourseSelection> GetCourses(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.CourseSelections.SingleOrDefaultAsync(c => c.Application.Id == application.Id)); }
/// <summary> /// Pre-select any disabilities the user has identified in their application. /// </summary> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <Disability> > SelectDisabilities(Application application, StudentPortalContext _ctx) { // List of available disabilities List <Disability> disabilities = await _ctx.Disabilities.ToListAsync(); // List of disabilities identified in the user's application List <UserDisability> userDisabilities = await _ctx.UserDisabilities .Where(u => u.Application.Id == application.Id) .ToListAsync(); // Flag any disabilities that the user has identified foreach (var disability in userDisabilities) { Disability dis = disabilities.FirstOrDefault(d => d.Id == disability.Disability); if (dis != null) { dis.HasDisability = true; } } return(disabilities); }
/// <summary> /// Remove any old disbilities tied to the current user's application. /// </summary> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task RemoveOldDisabilities(Application application, StudentPortalContext _ctx) { _ctx.UserDisabilities.RemoveRange(await GetDisabilities(application, _ctx)); }
/// <summary> /// List of applications made for a particular course or course group. /// </summary> /// <param name="courseCode"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <ApplicantDetails> > ApplicationsForCourse(string courseCode, string courseGroupCode, StudentPortalContext _ctx) { List <ApplicantDetails> applications = await( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join g in _ctx.Genders on pd.Gender equals g.Id.ToString() join cs in _ctx.CourseSelections on a.Id equals cs.Application.Id join c in _ctx.Courses on cs.FirstChoice equals c.Id join cg in _ctx.CourseGroups on c.Type equals cg.Id where a.Submitted && // Only complete applications ( (c.Code == courseCode || c.Id.ToString() == courseCode) || // For the provided course (cg.Id.ToString() == courseGroupCode) // For the provided course group ) orderby a.Finished descending select new ApplicantDetails { Id = a.Id, Started = a.Started, Finished = a.Finished.Value, Gender = g.Name, Forename = pd.Forename, Surname = pd.Surname, Email = pd.EmailAddress, DateOfBirth = pd.DateOfBirth, MobileNumber = pd.MobileNumber, TelephoneNumber = pd.TelephoneNumber, } ).ToListAsync(); return(applications); }
/// <summary> /// Get the current learning support details for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <LearningSupport> GetLearningSupport(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.LearningSupport.SingleOrDefaultAsync(l => l.Application.Id == application.Id)); }
/// <summary> /// List of applicant details for students who's age falls between a provided range, e.g 16 to 20. /// </summary> /// <param name="startAge"></param> /// <param name="endAge"></param> /// <param name="_ctx"></param> /// <returns></returns> public IEnumerable <ApplicantDetails> ApplicationsByAge(string startAge, string endAge, StudentPortalContext _ctx) { // If no ages are passed, lookup applicants born today - e.g show no results. DateTime startDate = DateTime.Today; DateTime endDate = DateTime.Today; if (!string.IsNullOrEmpty(startAge)) { startDate = DateTime.Today.AddYears(-Convert.ToInt32(startAge)); } if (!string.IsNullOrEmpty(endAge)) { endDate = DateTime.Today.AddYears(-Convert.ToInt32(endAge)); } List <ApplicantDetails> applications = ( from a in _ctx.Applications join pd in _ctx.PersonalDetails on a.Id equals pd.Application.Id join g in _ctx.Genders on pd.Gender equals g.Id.ToString() where a.Submitted // Only complete applications orderby a.Finished descending select new ApplicantDetails { Id = a.Id, Started = a.Started, Finished = a.Finished.Value, Gender = g.Name, Forename = pd.Forename, Surname = pd.Surname, Email = pd.EmailAddress, DateOfBirth = pd.DateOfBirth, MobileNumber = pd.MobileNumber, TelephoneNumber = pd.TelephoneNumber, } ).ToList(); foreach (ApplicantDetails applicant in applications) { DateTime dob = DateTime.Parse(applicant.DateOfBirth); // Born within the date range if (dob >= endDate && dob <= startDate) { yield return(applicant); } } }
/// <summary> /// Comma seperate format each of the disabilities identified in the provided application. /// </summary> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <string> ConvertDisabilitiesToString(Application application, StudentPortalContext _ctx) { List <Disability> disabilities = await SelectDisabilities(application, _ctx); List <string> disabilityNames = disabilities.Where(d => d.HasDisability).Select(d => d.Name).ToList(); if (disabilityNames == null || !disabilityNames.Any()) { return("None"); } return(disabilityNames.Aggregate((x, y) => string.Format("{0}, {1}", x, y))); }
/// <summary> /// Save learning support details related to the current application. /// </summary> /// <param name="learningSupport"></param> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task SaveLearningSupport(LearningSupport learningSupport, Application application, StudentPortalContext _ctx) { learningSupport.Application = application; LearningSupport currentSupport = await _applicationService.GetLearningSupport(_ctx); if (currentSupport != null) { learningSupport.Id = currentSupport.Id; } _ctx.LearningSupport.AddOrUpdate(l => l.Id, learningSupport); await _ctx.SaveChangesAsync(); }
/// <summary> /// Get a list of all the applications that belong to the provided user. /// </summary> /// <param name="userName"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <Application> > GetUsersApplications(string userName, StudentPortalContext _ctx) { List <Application> applications = await _ctx.Applications .Where(a => a.User.UserName == userName) .OrderBy(a => a.Started) .ToListAsync(); return(applications); }
/// <summary> /// Get a list of disabilities that have been identified in the current application. /// </summary> /// <param name="application"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <List <UserDisability> > GetDisabilities(Application application, StudentPortalContext _ctx) { List <UserDisability> disabilities = await _ctx.UserDisabilities .Where(u => u.Application.Id == application.Id) .ToListAsync(); return(disabilities); }
/// <summary> /// Get the current personal details for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <PersonalDetails> GetPersonalDetails(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.PersonalDetails.SingleOrDefaultAsync(p => p.Application.Id == application.Id)); }
/// <summary> /// Save the disabilities/support needs of the user for their application. /// </summary> /// <param name="application"></param> /// <param name="disabilities"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task SaveDisabilities(Application application, List <Disability> disabilities, StudentPortalContext _ctx) { await RemoveOldDisabilities(application, _ctx); foreach (var disability in disabilities) { if (disability.HasDisability) { Disability dis = await _ctx.Disabilities .FirstOrDefaultAsync(d => d.Name == disability.Name); _ctx.UserDisabilities.Add(new UserDisability { Disability = dis.Id, Application = application }); } } }
/// <summary> /// Get the current address details for an application. /// </summary> /// <param name="_ctx"></param> /// <returns></returns> public async Task <Address> GetAddress(StudentPortalContext _ctx, int?applicationId = null) { Application application = await GetCurrentApplication(_ctx, applicationId); return(await _ctx.Addressses.SingleOrDefaultAsync(a => a.Application.Id == application.Id)); }
/// <summary> /// Get the user details for the currently logged in user. /// </summary> /// <param name="userName"></param> /// <param name="_ctx"></param> /// <returns></returns> public async Task <ApplicationUser> GetUser(string userName, StudentPortalContext _ctx) { return(await _ctx.Users.SingleOrDefaultAsync(u => u.UserName == userName)); }
public PortalRoleManager(StudentPortalContext _ctx) { this._roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(_ctx)); this._userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_ctx)); }