/// <summary> /// Constructor. /// </summary> public AssignmentGrade( User user, Assignment assignment, double oldScore, double newScore) { User = user; Assignment = assignment; OldScore = oldScore; NewScore = newScore; }
public async Task<IActionResult> Edit(string assignmentName, Assignment assignment) { if (ModelState.IsValid && await AssignmentService.UpdateAssignmentAsync(ClassroomName, assignment, ModelErrors)) { return RedirectToAction("Index"); } else { await PopulateDropDownsAsync(); return View("CreateEdit", assignment); } }
/// <summary> /// Creates a new assignment with the given data. /// </summary> public TestDatabaseBuilder AddAssignment( string classroomName, string assignmentGroupName, string assignmentName, IDictionary<string, DateTime> sectionDueDates, IDictionary<string, string[]> questionsByCategory) { var classroom = _buildContext.Classrooms .Include(c => c.Sections) .Single(c => c.Name == classroomName); var sections = classroom.Sections .ToDictionary(s => s.Name, s => s.Id); var allQuestions = _buildContext.Questions .Include(q => q.QuestionCategory) .ToList() .GroupBy(q => q.QuestionCategory.Name) .ToDictionary(group => group.Key, group => group.ToDictionary(q => q.Name, q => q.Id)); var assignment = new Assignment() { ClassroomId = classroom.Id, Name = assignmentName, GroupName = assignmentGroupName, DueDates = sectionDueDates.Select ( kvp => new AssignmentDueDate() { SectionId = sections[kvp.Key], DueDate = kvp.Value } ).ToList(), Questions = questionsByCategory.SelectMany ( qs => qs.Value, (kvp, questionName) => new AssignmentQuestion() { QuestionId = allQuestions[kvp.Key][questionName], Points = 1.0 } ).ToList() }; _buildContext.Assignments.Add(assignment); _buildContext.SaveChanges(); return this; }
/// <summary> /// Validates that an assignment is completely retrieved from the database. /// </summary> private bool ValidateAssignment( Assignment assignment, string assignmentName, string assignmentGroupName) { return assignment.Name == assignmentName && assignment.GroupName == assignmentGroupName && assignment.DueDates.Count > 0 && assignment.Questions.Count > 0 && assignment.Questions.All(q => q.Question != null); }
/// <summary> /// Updates a assignment. /// </summary> private bool UpdateAssignment(Assignment assignment, IModelErrorCollection modelErrors) { if (assignment.DueDates != null) { var sections = assignment.DueDates.Select(d => d.SectionId).ToList(); if (sections.Distinct().Count() != sections.Count) { modelErrors.AddError("DueDates", "You may only have one due date per section."); return false; } } UpdateQuestionOrder(assignment.Questions); _dbContext.RemoveUnwantedObjects ( _dbContext.AssignmentQuestions, question => question.Id, question => question.AssignmentId == assignment.Id, assignment.Questions ); _dbContext.RemoveUnwantedObjects ( _dbContext.AssignmentDueDates, dueDate => dueDate.Id, dueDate => dueDate.AssignmentId == assignment.Id, assignment.DueDates ); return true; }
/// <summary> /// Updates an assignment. /// </summary> public async Task<bool> UpdateAssignmentAsync( string classroomName, Assignment assignment, IModelErrorCollection modelErrors) { var classroom = await LoadClassroomAsync(classroomName); if (!UpdateAssignment(assignment, modelErrors)) return false; assignment.ClassroomId = classroom.Id; _dbContext.Update(assignment); await _dbContext.SaveChangesAsync(); return true; }