public ActionResult AddFileGroup(int id) { var didacheDb = new DidacheDb(); // get data var reader = new JsonFx.Json.JsonReader(); dynamic json = reader.Read(HttpUtility.UrlDecode(Request.Form.ToString())); CourseFileGroup group = new CourseFileGroup(); group.CourseID = id; group.SortOrder = 9999; group.Name = json.name; didacheDb.CourseFileGroups.Add(group); didacheDb.SaveChanges(); return Json(new {groupid = group.GroupID, name=json.name, courseid= id}); }
public ActionResult UpdateCourseFileGroup(CourseFileGroup model) { if (model.GroupID > 0) { // EDIT MODE try { model = db.CourseFileGroups.Find(model.GroupID); UpdateModel(model); db.SaveChanges(); return Json(new { success = true, action = "edit", courseFileGroup = serializer.Serialize(model) }); } catch (Exception ex) { ModelState.AddModelError("", "Edit Failure, see inner exception"); Response.StatusCode = 500; return Json(new { success = false, action = "edit", message = ex.ToString(), errors = GetErrors() }); } } else { // ADD MODE if (ModelState.IsValid) { db.CourseFileGroups.Add(model); db.SaveChanges(); return Json(new { success = true, action = "add", courseFileGroup = serializer.Serialize(model) }); } else { Response.StatusCode = 500; return Json(new { success = false, action = "add", message = "Invalid new model", errors = GetErrors() }); } } }
public ActionResult UpdateCourse(Course model) { if (model.CourseID > 0) { // EDIT MODE try { Course course = db.Courses.Find(model.CourseID); //course.IsActive = model.IsActive; TryUpdateModel(course); db.SaveChanges(); return Json(new { success = true, action = "edit", course = serializer.Serialize(model) }); } catch (Exception ex) { ModelState.AddModelError("", "Edit Failure, see inner exception"); Response.StatusCode = 500; return Json(new { success = false, action = "edit", message = ex.ToString(), errors = GetErrors(), course = serializer.Serialize(model) }); } } else { // ADD MODE if (ModelState.IsValid) { db.Courses.Add(model); db.SaveChanges(); // create new stuff CourseFileGroup cfg = new CourseFileGroup(); cfg.CourseID = model.CourseID = model.CourseID; return Json(new { success = true, action = "add", course = serializer.Serialize(model) }); } else { Response.StatusCode = 500; return Json(new { success = false, action = "add", message = "Invalid new model", errors = GetErrors() }); } } }
public static Course CloneCourse(int courseID, int sessionID, DateTime startDate, string courseCode, string section) { DidacheDb db = new DidacheDb(); Course oldCourse = db.Courses .Include("Units.Tasks") .Include("CourseFileGroups.CourseFileAssociations") .SingleOrDefault(c => c.CourseID == courseID); int daysToShift = (startDate - oldCourse.StartDate).Days; Course newCourse = new Course() { SessionID = sessionID, CampusID = oldCourse.CampusID, IsActive = oldCourse.IsActive, CourseCode = !String.IsNullOrWhiteSpace(courseCode) ? courseCode : oldCourse.CourseCode, Name = oldCourse.Name, Section = !String.IsNullOrWhiteSpace(section) ? section : oldCourse.Section, StartDate = oldCourse.StartDate.AddDays(daysToShift), EndDate = oldCourse.EndDate.AddDays(daysToShift), Description = oldCourse.Description }; db.Courses.Add(newCourse); db.SaveChanges(); /* } catch (DbEntityValidationException dbEx) { string errors = ""; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { //System.Web.HttpContext.Current.Trace.Warn("Property: {0} Error: {1}", validationError.PropertyName, dbEx); errors += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + "; "; } } throw new Exception(errors); } */ foreach (Unit oldUnit in oldCourse.Units) { Unit newUnit = new Unit() { CourseID = newCourse.CourseID, IsActive = oldUnit.IsActive, SortOrder = oldUnit.SortOrder, Name = oldUnit.Name, StartDate = oldUnit.StartDate.AddDays(daysToShift), EndDate = oldUnit.EndDate.AddDays(daysToShift), Instructions = oldUnit.Instructions }; db.Units.Add(newUnit); db.SaveChanges(); Dictionary<int, int> taskMap = new Dictionary<int, int>(); List<Task> newTasks = new List<Task>(); foreach (Task oldTask in oldUnit.Tasks) { Task newTask = new Task() { CourseID = newUnit.CourseID, UnitID = newUnit.UnitID, IsActive = oldTask.IsActive, SortOrder = oldTask.SortOrder, Name = oldTask.Name, DueDate = null, FileTypesAllowed = oldTask.FileTypesAllowed, InstructionsAvailableDate = null, IsSkippable = oldTask.IsSkippable, Priority = oldTask.Priority, RelatedTaskID = oldTask.RelatedTaskID, SubmissionAvailableDate = null, TaskID = oldTask.TaskID, TaskTypeName = oldTask.TaskTypeName, Instructions = oldTask.Instructions, CustomAttributes = oldTask.CustomAttributes }; if (oldTask.DueDate.HasValue) newTask.DueDate = oldTask.DueDate.Value.AddDays(daysToShift); if (oldTask.SubmissionAvailableDate.HasValue) newTask.SubmissionAvailableDate = oldTask.SubmissionAvailableDate.Value.AddDays(daysToShift); if (oldTask.InstructionsAvailableDate.HasValue) newTask.InstructionsAvailableDate = oldTask.InstructionsAvailableDate.Value.AddDays(daysToShift); db.Tasks.Add(newTask); db.SaveChanges(); // store to remap the tasks below newTasks.Add(newTask); taskMap.Add(oldTask.TaskID, newTask.TaskID); } // go back and remap the related tasks List<int> newTaskIds = taskMap.Values.ToList(); foreach(Task newTask in newTasks) { if (newTask.RelatedTaskID > 0 && taskMap.ContainsKey(newTask.RelatedTaskID)) { newTask.RelatedTaskID = taskMap[newTask.RelatedTaskID]; } } db.SaveChanges(); } // FILES foreach (CourseFileGroup oldGroup in oldCourse.CourseFileGroups) { CourseFileGroup newGroup = new CourseFileGroup() { CourseID = newCourse.CourseID, Name = oldGroup.Name, SortOrder = oldGroup.SortOrder }; db.CourseFileGroups.Add(newGroup); db.SaveChanges(); foreach (CourseFileAssociation oldFile in oldGroup.CourseFileAssociations) { CourseFileAssociation newFile = new CourseFileAssociation() { GroupID = newGroup.GroupID, FileID = oldFile.FileID, DateAdded = newCourse.StartDate, IsActive = oldFile.IsActive, SortOrder = oldFile.SortOrder }; db.CourseFileAssociations.Add(newFile); } db.SaveChanges(); } return newCourse; }