/// <summary> /// Get an assignment by the assignment's ID /// </summary> public TeacherCreateAssignmentViewModel GetAssignmentById(int assignmentId) { var assignment = _db.Assignments.SingleOrDefault(x => x.Id == assignmentId); if (assignment != null) { var model = new TeacherCreateAssignmentViewModel { Name = assignment.Name, Description = assignment.Description, CourseInstanceId = assignment.CourseInstanceId, EndTime = assignment.EndTime.ToString(), StartTime = assignment.StartTime.ToString(), MaxCollaborators = assignment.MaxCollaborators, Problems = new List <TeacherAssignmentProblemViewModel>() }; var problems = _db.AssignmentProblems.Where(x => x.AssignmentId == assignmentId); foreach (var assignmentProblem in problems) { model.Problems.Add(new TeacherAssignmentProblemViewModel { MaxSubmissions = assignmentProblem.MaxSubmissions, ProblemId = assignmentProblem.ProblemId, Weight = assignmentProblem.Weight }); } return(model); } return(null); }
/// <summary> /// Update assignment information via TeacherCreateAssignmentViewModel /// </summary> public bool UpdateAssignment(TeacherCreateAssignmentViewModel assignment) { var dbAssignment = _db.Assignments.SingleOrDefault(x => x.Id == assignment.Id); if (dbAssignment != null) { dbAssignment.Description = assignment.Description; dbAssignment.Name = assignment.Name; dbAssignment.EndTime = DateTime.Parse(assignment.EndTime); dbAssignment.StartTime = DateTime.Parse(assignment.StartTime); dbAssignment.MaxCollaborators = assignment.MaxCollaborators; // Remove problems foreach (var assignmentProblem in _db.AssignmentProblems.Where(x => x.AssignmentId == assignment.Id)) { var found = assignment.Problems.Any(problem => assignmentProblem.ProblemId == problem.ProblemId); if (!found) { RemoveProblemFromAssignmentByIds(assignmentProblem.AssignmentId, assignmentProblem.ProblemId); } } // Add/Edit existing problems foreach (var problem in assignment.Problems) { var dbAssignmentProblem = _db.AssignmentProblems.SingleOrDefault(x => x.AssignmentId == assignment.Id && x.ProblemId == problem.ProblemId); if (dbAssignmentProblem != null) { dbAssignmentProblem.MaxSubmissions = problem.MaxSubmissions; dbAssignmentProblem.Weight = (byte)problem.Weight; } else { var newAssignmentProblem = new AssignmentProblem { ProblemId = problem.ProblemId, AssignmentId = assignment.Id, Weight = (byte)problem.Weight, MaxSubmissions = problem.MaxSubmissions }; _db.AssignmentProblems.Add(newAssignmentProblem); } } try { _db.SaveChanges(); return(true); } catch (Exception e) { return(false); } } return(false); }
/// <summary> /// Create a new assignment via TeacherCreateAssignmentViewModel /// </summary> public bool CreateNewAssignment(TeacherCreateAssignmentViewModel assignment) { var newAssignment = new Assignment { CourseInstanceId = assignment.CourseInstanceId, Description = assignment.Description, EndTime = DateTime.Parse(assignment.EndTime), StartTime = DateTime.Parse(assignment.StartTime), Name = assignment.Name, MaxCollaborators = assignment.MaxCollaborators, IsGraded = false }; newAssignment = _db.Assignments.Add(newAssignment); foreach (var assignmentProblem in assignment.Problems) { var problem = _db.Problems.SingleOrDefault(x => x.Id == assignmentProblem.ProblemId); if (problem == null) { continue; } var newAssignmentProblem = new AssignmentProblem { ProblemId = problem.Id, AssignmentId = newAssignment.Id, Weight = (byte)assignmentProblem.Weight, MaxSubmissions = assignmentProblem.MaxSubmissions }; _db.AssignmentProblems.Add(newAssignmentProblem); } var students = _courseService.GetAllStudentsInCourseInstance(newAssignment.CourseInstanceId); // Create groups for students var count = 1; foreach (var student in students) { _db.AssignmentGroups.Add(new AssignmentGroup { UserId = student.Id, AssignmentId = newAssignment.Id, GroupNumber = count }); count++; } try { _db.SaveChanges(); return(true); } catch (Exception e) { return(false); } }
/// <summary> /// Edit an assignment's information and linked problems /// </summary> public ActionResult EditAssignmentInformation(TeacherCreateAssignmentViewModel assignment) { return(Json(_teacherService.UpdateAssignment(assignment))); }
public ActionResult NewAssignment(TeacherCreateAssignmentViewModel assignment) { return(Json(_teacherService.CreateNewAssignment(assignment))); }