// Add a new course control to a course. Adds a new CourseControl referencing controlId into courseId. The place to insert is // given by courseControl1 and courseControl2. These control should have been gotten by calling FindControlInsertionPoint. public static Id<CourseControl> AddCourseControl(EventDB eventDB, Id<ControlPoint> controlId, Id<Course> courseId, Id<CourseControl> courseControl1, Id<CourseControl> courseControl2) { CourseControl newCourseControl; Id<CourseControl> newCourseControlId; // When adding a new course controls, they fit into variations fine because we are never adding or changing an split, just // fitting into existing splits. if (courseControl1.IsNone) { // Adding at start. Course course = (Course) eventDB.GetCourse(courseId).Clone(); Debug.Assert(courseControl2 == course.firstCourseControl); newCourseControl = new CourseControl(controlId, course.firstCourseControl); newCourseControlId = eventDB.AddCourseControl(newCourseControl); course.firstCourseControl = newCourseControlId; eventDB.ReplaceCourse(courseId, course); } else { // Adding after courseControl1. CourseControl before = (CourseControl) eventDB.GetCourseControl(courseControl1).Clone(); Debug.Assert(courseControl2 == before.nextCourseControl); newCourseControl = new CourseControl(controlId, before.nextCourseControl); newCourseControlId = eventDB.AddCourseControl(newCourseControl); before.nextCourseControl = newCourseControlId; eventDB.ReplaceCourseControl(courseControl1, before); } return newCourseControlId; }
public static bool AddVariation(EventDB eventDB, CourseDesignator courseDesignator, Id<CourseControl> variationCourseControlId, bool loop, int numberOfForks) { if (!QueryEvent.CanAddVariation(eventDB, courseDesignator, variationCourseControlId)) return false; CourseControl variationCourseControl = eventDB.GetCourseControl(variationCourseControlId); CourseControl newVariationCourseControl = (CourseControl)variationCourseControl.Clone(); newVariationCourseControl.split = true; newVariationCourseControl.loop = loop; newVariationCourseControl.splitEnd = loop ? variationCourseControlId : variationCourseControl.nextCourseControl; // If its a loop, we add a new course control for each loop. For a fork, we already have one leg of the fork. int newCourseControls = loop ? numberOfForks : numberOfForks - 1; CourseControl[] splitCourseControls = new CourseControl[newCourseControls + 1]; Id<CourseControl>[] splitCourseControlIds = new Id<CourseControl>[newCourseControls + 1]; splitCourseControls[0] = newVariationCourseControl; splitCourseControlIds[0] = variationCourseControlId; for (int i = 1; i <= newCourseControls; ++i) { splitCourseControls[i] = (CourseControl) newVariationCourseControl.Clone(); if (loop) splitCourseControls[i].nextCourseControl = variationCourseControlId; splitCourseControlIds[i] = eventDB.AddCourseControl(splitCourseControls[i]); } for (int i = 0; i <= newCourseControls; ++i) { splitCourseControls[i].splitCourseControls = splitCourseControlIds; eventDB.ReplaceCourseControl(splitCourseControlIds[i], splitCourseControls[i]); } return true; }
public void ReplaceCourseControl(Id<CourseControl> id, CourseControl courseControl) { courseControlStore.Replace(id, courseControl); }
// Duplicate a course with a new name, new course controls (since course controls can't be shared), // but all other attributes the same. public static Id<Course> DuplicateCourse(EventDB eventDB, Id<Course> oldCourseId, string newName) { Course oldCourse = eventDB.GetCourse(oldCourseId); int newSortOrder = oldCourse.sortOrder + 1; // Update existing sort orders after by adding one to existing course orders after the new one. foreach (Id<Course> courseToChangeId in eventDB.AllCourseIds.ToList()) { int sortOrder = eventDB.GetCourse(courseToChangeId).sortOrder; if (sortOrder >= newSortOrder) ChangeCourseSortOrder(eventDB, courseToChangeId, sortOrder + 1); } // Duplicate the course controls with blank course controls, and record the mapping. Dictionary<Id<CourseControl>, Id<CourseControl>> mapCourseControl = new Dictionary<Id<CourseControl>, Id<CourseControl>>(); foreach (Id<CourseControl> oldCourseControlId in QueryEvent.EnumCourseControlIds(eventDB, new CourseDesignator(oldCourseId))) { CourseControl newCourseControl = new CourseControl(); Id<CourseControl> newCourseControlId = eventDB.AddCourseControl(newCourseControl); mapCourseControl[oldCourseControlId] = newCourseControlId; } // Create a new course with no course controls in it and the new name, sort order. Course newCourse = (Course)oldCourse.Clone(); if (oldCourse.firstCourseControl.IsNotNone) newCourse.firstCourseControl = mapCourseControl[oldCourse.firstCourseControl]; newCourse.name = newName; newCourse.sortOrder = newSortOrder; Id<Course> newCourseId = eventDB.AddCourse(newCourse); // Now copy all the old course control, updating all the linking fields. foreach (Id<CourseControl> oldCourseControlId in QueryEvent.EnumCourseControlIds(eventDB, new CourseDesignator(oldCourseId))) { // Add a new course control to the new course. CourseControl oldCourseControl = eventDB.GetCourseControl(oldCourseControlId); CourseControl newCourseControl = (CourseControl) oldCourseControl.Clone(); if (newCourseControl.nextCourseControl.IsNotNone) newCourseControl.nextCourseControl = mapCourseControl[newCourseControl.nextCourseControl]; if (newCourseControl.splitEnd.IsNotNone) newCourseControl.splitEnd = mapCourseControl[newCourseControl.splitEnd]; if (newCourseControl.splitCourseControls != null) { for (int i = 0; i < newCourseControl.splitCourseControls.Length; ++i) { newCourseControl.splitCourseControls[i] = mapCourseControl[newCourseControl.splitCourseControls[i]]; } } eventDB.ReplaceCourseControl(mapCourseControl[oldCourseControlId], newCourseControl); } // Duplicate any specials from the old course. foreach (Id<Special> specialId in eventDB.AllSpecialIds.ToList()) { Special special = eventDB.GetSpecial(specialId); if (!special.allCourses) { List<CourseDesignator> addedCourseDesignators = new List<CourseDesignator>(); foreach (CourseDesignator designatorWithSpecial in special.courses) { if (designatorWithSpecial.CourseId == oldCourseId) { if (designatorWithSpecial.AllParts) addedCourseDesignators.Add(new CourseDesignator(newCourseId)); else addedCourseDesignators.Add(new CourseDesignator(newCourseId, designatorWithSpecial.Part)); } } if (addedCourseDesignators.Count > 0) { ChangeDisplayedCourses(eventDB, specialId, special.courses.Concat(addedCourseDesignators).ToArray()); } } } return newCourseId; }
public Id<CourseControl> AddCourseControl(CourseControl courseControl) { return courseControlStore.Add(courseControl); }