public ActionResult UpdateUserGroupSorting(int id) { var didacheDb = new DidacheDb(); string data = HttpUtility.UrlDecode(Request.Form.ToString()); //dynamic newValue = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Dynamic.>(data); var reader = new JsonFx.Json.JsonReader(); dynamic output = reader.Read(data); // get groups List<CourseUserGroup> groups = db.CourseUserGroups.Where(cfg => cfg.CourseID == id).ToList(); List<int> groupIDs = groups.Select(cfg => cfg.GroupID).ToList(); // get files List<CourseUser> courseUsers = db.CourseUsers.Where(cu => groupIDs.Contains(cu.GroupID)).ToList(); foreach (var groupInfo in output) { CourseUserGroup userGroup = groups.SingleOrDefault(cug => cug.GroupID == groupInfo.groupid); foreach (var userInfo in groupInfo.users) { CourseUser courseUser = courseUsers.SingleOrDefault(cu => cu.UserID == userInfo.userid); // has the group changed? if (courseUser.GroupID != userGroup.GroupID) { // create a new one with the new group CourseUser newCourseFile = new CourseUser() { UserID = courseUser.UserID, GroupID = userGroup.GroupID, RoleID = (int)CourseUserRole.Student }; db.CourseUsers.Remove(courseUser); db.CourseUsers.Add(newCourseFile); } else { // do nothing since we don't care about the order } } } db.SaveChanges(); /* foreach (var groupInfo in output) { // get and update the group CourseFileGroup fileGroup = didacheDb.CourseFileGroups.Find(groupInfo.groupid); fileGroup.SortOrder = groupInfo.sortorder; //fileGroup.Name = groupInfo.name; // TEST 2: get all existing, update, remove missing // get exsiting files List<CourseFileAssociation> courseFiles = didacheDb.CourseFileAssociations.Where(cfa => cfa.GroupID == fileGroup.GroupID).ToList(); foreach (var fileInfo in groupInfo.files) { // find the file CourseFileAssociation cfa = courseFiles.Find(c => c.FileID == fileInfo.fileid); if (cfa != null) { // update cfa.SortOrder = fileInfo.sortorder; // add to change list //changedFiles.Add(cfa); courseFiles.Remove(cfa); } else { cfa = new CourseFileAssociation(); cfa.GroupID = fileGroup.GroupID; cfa.FileID = fileInfo.fileid; cfa.SortOrder = fileInfo.sortorder; didacheDb.CourseFileAssociations.Add(cfa); } } // remove all remaining files foreach (CourseFileAssociation notUpdated in courseFiles) { didacheDb.CourseFileAssociations.Remove(notUpdated); } } */ didacheDb.SaveChanges(); // need to deserialize this and update all the groups and files return Json(new { success = true }); }
/// <summary> /// /// </summary> /// <param name="courseID"></param> /// <param name="userID"></param> /// <param name="groupID"></param> /// <param name="userRole"></param> /// <returns>Whether or not the user was added (false = just updated)</returns> public static bool AddUserToCourse(int courseID, int userID, int groupID, CourseUserRole userRole) { DidacheDb db = new DidacheDb(); CourseUser courseUser = db.CourseUsers.SingleOrDefault(cu => cu.UserID == userID && cu.CourseID == courseID && cu.RoleID == (int) userRole); if (courseUser != null) { // update the group if (groupID > 0) { courseUser.GroupID = groupID; db.SaveChanges(); } return false; } else { courseUser = new CourseUser() { UserID = userID, CourseID = courseID, RoleID = (int) userRole, GroupID = groupID }; db.CourseUsers.Add(courseUser); if (userRole == CourseUserRole.Student) { // check for assignments bool hasTasks = db.UserTasks.Count(ut => ut.UserID == userID && ut.Task.CourseID == courseID) > 0; if (!hasTasks) { // create assignments List<Task> tasks = db.Tasks.Where(t => t.CourseID == courseID).ToList(); foreach (Task task in tasks) { UserTaskData utd = new UserTaskData() { TaskID = task.TaskID, CourseID = task.CourseID, UnitID = task.UnitID, GraderComments = "", GraderSubmitDate = null, GraderUserID = 0, GradeStatus = 0, LetterGrade = "", NumericGrade = null, StudentComments = "", StudentSubmitDate = null, TaskCompletionStatus = TaskCompletionStatus.NotStarted, TaskData = "", GraderFileID = 0, PostID = 0, StudentFileID = 0, UserID = userID }; db.UserTasks.Add(utd); } } } db.SaveChanges(); return true; } }