private async Task<float> GetAverageForType(ApplicationUser student, IEnumerable<WorkItem> workItems, WorkItemType type, GradeDistribution distribution) { var scoreUnitManager = new ScoreUnitManager(_db); var workItemsForType = workItems.Where(wi => wi.Type == type); var PointsForType = MaxPointsForType(type, distribution); // If no WorkItem for the type, return total for that type if(workItemsForType.Count() == 0) { return PointsForType; } bool hasGradedScoreUnit = false; float studentsScores = 0.0f; float total = 0.0f; foreach(var workItem in workItemsForType) { var scoreUnit = await scoreUnitManager.GetStudentScoreUnit(workItem, student); if(scoreUnit != null && scoreUnit.Grade.HasValue) { hasGradedScoreUnit = true; studentsScores += scoreUnit.Grade.Value; total += workItem.MaxPoints; } } // If no ScoreUnit has been graded, return the total for that type if(!hasGradedScoreUnit) { return PointsForType; } return (studentsScores / total) * PointsForType; }
/// <summary> /// Create a UserModel for an ApplicationUser /// </summary> /// <param name="user"></param> public UserViewModel(ApplicationUser user) { if(user != null) { this.FirstName = user.FirstName; this.LastName = user.LastName; this.UserName = user.UserName; } }
/// <summary> /// Returns a list of classes for the user /// </summary> /// <param name="user"></param> /// <remarks>If the user is a teacher, returns classes taught by the teacher. /// If the user is a student, returns classes enrolled in by the student /// (both pending and accepted).</remarks> public async Task<IEnumerable<Class>> GetUserClasses(ApplicationUser user) { IQueryable<Class> classes = null; if(await _userManager.IsInRoleAsync(user.Id, studentRole)) { classes = _db.Enrollments.Where(e => e.Student.Email == user.Email).Select(e => e.Class).Include(c => c.Teacher); } else if(await _userManager.IsInRoleAsync(user.Id, teacherRole)) { classes = _db.Classes.Where(@class => @class.Teacher.Email == user.Email).Include(c => c.Teacher); } return classes; }
/// <summary> /// Calculates and returns a students grade in a class /// </summary> /// <param name="student">To student to calculate a grade for</param> /// <param name="class">The class that the student is enrolled in</param> /// <returns></returns> public async Task<float> GetStudentGradeAsync(ApplicationUser student, Class @class) { WorkItemManager workItemManager = new WorkItemManager(_db); var workItems = await workItemManager.GetClassWorkItems(@class); var total = 0.0f; total += await GetAverageForType(student, workItems, WorkItemType.Exam, @class.GradeDistribution); total += await GetAverageForType(student, workItems, WorkItemType.Homework, @class.GradeDistribution); total += await GetAverageForType(student, workItems, WorkItemType.Other, @class.GradeDistribution); total += await GetAverageForType(student, workItems, WorkItemType.Project, @class.GradeDistribution); total += await GetAverageForType(student, workItems, WorkItemType.Quiz, @class.GradeDistribution); return total; }
/// <summary> /// Deletes a students enrollment in a class /// </summary> /// <param name="class">The class the student is enrolled in or has applied for enrollment</param> /// <param name="student">The student to unenroll from the class</param> /// <returns>Null if the student is not enrolled in class. Otherwise, returns the /// enrollment that was removed</returns> /// <remarks>This clears all data associated with the student in this class and /// cannot be undone</remarks> public async Task<Enrollment> DropStudent(Class @class, ApplicationUser student) { // Check that the student is actually enrolled var status = await _db.Enrollments.Where(e => e.Class.Id == @class.Id && e.Student.Id == student.Id).FirstOrDefaultAsync(); if(status == null) { return null; } // Delete all student related data var scoreUnits = await _db.ScoreUnits.Where(sc => sc.Student.Id == student.Id).ToListAsync(); _db.ScoreUnits.RemoveRange(scoreUnits); _db.Enrollments.Remove(status); await _db.SaveChangesAsync(); return status; }
/// <summary> /// Enrolls a new student into the class /// </summary> /// <param name="class">Class to enroll the student in</param> /// <param name="student">Student to enroll in a class</param> /// <returns>Null if the an enrollment has already been created. Otherwise, the /// enrollment that was just created.</returns> public async Task<Enrollment> RequestEnrollment(Class @class, ApplicationUser student) { var status = await (from enrollment in _db.Enrollments where enrollment.Student.Id == student.Id && enrollment.Class.Id == @class.Id select enrollment).FirstOrDefaultAsync(); if(status != null) { return null; } // Create the enrollment as pending Enrollment newEnroll = new Enrollment { Class = @class, Student = student, Pending = true }; _db.Enrollments.Add(newEnroll); await _db.SaveChangesAsync(); return newEnroll; }
/// <summary> /// Returns a students ScoreUnit for a WorkItem /// </summary> /// <param name="workItem">WorkItem to return the ScoreUnit for</param> /// <param name="student">Student to return the ScorreUnit for</param> /// <returns></returns> public async Task<ScoreUnit> GetStudentScoreUnit(WorkItem workItem, ApplicationUser student) { return await _db.ScoreUnits.Where(su => su.WorkItem.Id == workItem.Id && su.Student.Id == student.Id).FirstOrDefaultAsync(); }
/// <summary> /// Creates a StudentViewModel for an ApplicationUser /// </summary> /// <param name="user">ApplicationUser to construct view-model from</param> /// <param name="class">Class to calculate the students in</param> /// <param name="manager">Grademanager to calculate student's grade</param> public StudentViewModel(ApplicationUser user) : base(user) { }
/// <summary> /// Returns of enrollments which apply to the given student /// </summary> /// <param name="student"></param> /// <returns></returns> public async Task<IEnumerable<Enrollment>> GetStudentEnrollments(ApplicationUser student) { return await _db.Enrollments.Where(e => e.Student.Id == student.Id).Include(e => e.Class).Include(e => e.Student).ToListAsync(); }
public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model) { if(!ModelState.IsValid) { return BadRequest(ModelState); } var info = await Authentication.GetExternalLoginInfoAsync(); if(info == null) { return InternalServerError(); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email }; IdentityResult result = await UserManager.CreateAsync(user); if(!result.Succeeded) { return GetErrorResult(result); } result = await UserManager.AddLoginAsync(user.Id, info.Login); if(!result.Succeeded) { return GetErrorResult(result); } return Ok(); }
public async Task<IHttpActionResult> Register(RegisterBindingModel model) { if(!ModelState.IsValid) { return BadRequest(ModelState); } // Check that role exists var db = new ApplicationDbContext(); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db)); if(!(await roleManager.RoleExistsAsync(model.Role))) { return BadRequest(string.Format("No such role: {0}", model.Role)); } var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName }; IdentityResult result = await UserManager.CreateAsync(user, model.Password); if(!result.Succeeded) { return GetErrorResult(result); } await UserManager.AddToRoleAsync(user.Id, model.Role); return Ok(); }