PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/acarter-wizard-mods/web/OSBLE/Areas/AssignmentWizard/Controllers/TeamController.cs

#
C# | 276 lines | 196 code | 30 blank | 50 comment | 16 complexity | 11d90f8c5e42b5bf499adbc0a4dad8af MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using OSBLE.Models.Assignments;
  7. using OSBLE.Models.Courses;
  8. namespace OSBLE.Areas.AssignmentWizard.Controllers
  9. {
  10. public class TeamController : WizardBaseController
  11. {
  12. public override string ControllerName
  13. {
  14. get { return "Team"; }
  15. }
  16. public override string PrettyName
  17. {
  18. get
  19. {
  20. return "Team Settings";
  21. }
  22. }
  23. public override string ControllerDescription
  24. {
  25. get { return "The assignment is team-based"; }
  26. }
  27. public override ICollection<WizardBaseController> Prerequisites
  28. {
  29. get
  30. {
  31. List<WizardBaseController> prereqs = new List<WizardBaseController>();
  32. prereqs.Add(new BasicsController());
  33. return prereqs;
  34. }
  35. }
  36. public override ICollection<AssignmentTypes> ValidAssignmentTypes
  37. {
  38. get
  39. {
  40. return base.AllAssignmentTypes;
  41. }
  42. }
  43. public override bool IsRequired
  44. {
  45. get
  46. {
  47. return true;
  48. }
  49. }
  50. /// <summary>
  51. /// Sets up the viewbag for various controller actions.
  52. /// </summary>
  53. /// <param name="assignmentWithTeams">Supply the assignment that you want pull the teams from.
  54. /// This is used for pulling team configurations from other assignments
  55. /// </param>
  56. protected void SetUpViewBag(List<IAssignmentTeam> teams)
  57. {
  58. //Guaranteed to pull all people enrolled in the course that can submit files
  59. //(probably students).
  60. List<CourseUser> users = (from cu in db.CourseUsers
  61. where cu.AbstractCourseID == activeCourse.AbstractCourseID
  62. && cu.AbstractRole.CanSubmit
  63. select cu).ToList();
  64. List<CourseUser> allUsers = users.ToList();
  65. //remove students currently on the team list from our complete user list
  66. foreach (IAssignmentTeam team in teams)
  67. {
  68. foreach (TeamMember member in team.Team.TeamMembers)
  69. {
  70. //If we're in a postback condition, our list of teams will include little more than the CourseUserId
  71. //As such, we can't access member.CourseUser
  72. CourseUser user = users.Find(u => u.ID == member.CourseUserID);
  73. users.Remove(user);
  74. //add the more detailed CourseUser info to the member
  75. member.CourseUser = user;
  76. }
  77. }
  78. //pull previous team configurations
  79. List<Assignment> previousTeamAssignments = (from assignment in db.Assignments
  80. where assignment.Category.Course.ID == activeCourse.AbstractCourseID
  81. where assignment.AssignmentTeams.Count > 0
  82. select assignment).ToList();
  83. //place items into the viewbag
  84. ViewBag.AllUsers = allUsers;
  85. ViewBag.UnassignedUsers = users;
  86. ViewBag.Teams = teams;
  87. ViewBag.PreviousTeamAssignments = previousTeamAssignments;
  88. }
  89. /// <summary>
  90. /// Sets up the viewbag for various controller actions. Will pull team information from
  91. /// the assignment currently being edited.
  92. /// </summary>
  93. protected new void SetUpViewBag()
  94. {
  95. SetUpViewBag(Assignment.AssignmentTeams.Cast<IAssignmentTeam>().ToList());
  96. }
  97. public override ActionResult Index()
  98. {
  99. base.Index();
  100. SetUpViewBag();
  101. return View(Assignment);
  102. }
  103. [HttpPost]
  104. public virtual ActionResult Index(Assignment model)
  105. {
  106. //reset our assignment
  107. Assignment = db.Assignments.Find(model.ID);
  108. //two postback options:
  109. // Load a prior team configuraiton. This will be donoted by the presence of the
  110. // "AutoGenFromPastButton" key in postback.
  111. // Save team configuration. If we don't have the above key, then we must be
  112. // wanting to do that.
  113. if (Request.Form.AllKeys.Contains("AutoGenFromPastButton"))
  114. {
  115. //we don't want to continue so force success to be false
  116. WasUpdateSuccessful = false;
  117. int assignmentId = Assignment.ID;
  118. Int32.TryParse(Request.Form["AutoGenFromPastSelect"].ToString(), out assignmentId);
  119. Assignment otherAssignment = db.Assignments.Find(assignmentId);
  120. SetUpViewBag(otherAssignment.AssignmentTeams.Cast<IAssignmentTeam>().ToList());
  121. }
  122. else
  123. {
  124. List<IAssignmentTeam> teams = Assignment.AssignmentTeams.Cast<IAssignmentTeam>().ToList();
  125. ParseFormValues(teams);
  126. IList<IAssignmentTeam> castedTeams = CastTeamAsConcreteType(teams, typeof(AssignmentTeam));
  127. Assignment.AssignmentTeams = castedTeams.Cast<AssignmentTeam>().ToList();
  128. db.SaveChanges();
  129. //We need to force the update as our model validation fails by default because
  130. //we're not guaranteeing that the Assignment will be fully represented in our view.
  131. WasUpdateSuccessful = true;
  132. SetUpViewBag();
  133. }
  134. return base.PostBack(Assignment);
  135. }
  136. /// <summary>
  137. /// AC: As of 2012-02-27, we have two different team uses (AssignmentTeam, DiscussionTeam). Both inherit from
  138. /// IAssignmentTeam, but when creating new objects, I need to instantiate a concrete class. This method
  139. /// allows the programmer to convert between concrete implementations of IAssignmentTeam.
  140. /// </summary>
  141. /// <param name="genericTeam"></param>
  142. /// <param name="teamType"></param>
  143. /// <returns></returns>
  144. protected IList<IAssignmentTeam> CastTeamAsConcreteType(IList<IAssignmentTeam> genericTeam, Type teamType)
  145. {
  146. List<IAssignmentTeam> castedTeams = new List<IAssignmentTeam>();
  147. foreach (IAssignmentTeam team in genericTeam)
  148. {
  149. IAssignmentTeam t = Activator.CreateInstance(teamType) as IAssignmentTeam;
  150. t.AssignmentID = team.AssignmentID;
  151. t.Assignment = team.Assignment;
  152. t.TeamID = team.TeamID;
  153. t.Team = team.Team;
  154. castedTeams.Add(t);
  155. }
  156. return castedTeams;
  157. }
  158. protected void ParseFormValues(IList<IAssignmentTeam> previousTeams)
  159. {
  160. //our new list of teams
  161. List<Team> teams = previousTeams.Select(at => at.Team).ToList();
  162. //Now that we've captured any existing teams, wipe out the old association
  163. //as well as the old team members
  164. previousTeams.Clear();
  165. foreach (Team team in teams)
  166. {
  167. team.TeamMembers.Clear();
  168. }
  169. db.SaveChanges();
  170. //update all prior teams (those already stored in the DB)
  171. string[] teamKeys = Request.Form.AllKeys.Where(k => k.Contains("team_")).ToArray();
  172. foreach (string key in teamKeys)
  173. {
  174. string teamName = Request.Form[key];
  175. int teamId = 0;
  176. //skip any bad apples
  177. if (!Int32.TryParse(key.Split('_')[1], out teamId))
  178. {
  179. continue;
  180. }
  181. //update the team name
  182. Team team = teams.Find(t => t.ID == teamId);
  183. if (team == null)
  184. {
  185. continue;
  186. }
  187. team.Name = teamName;
  188. //clear any existing team members
  189. team.TeamMembers.Clear();
  190. db.Entry(team).State = System.Data.EntityState.Modified;
  191. }
  192. //get all relevant form keys
  193. string[] keys = Request.Form.AllKeys.Where(k => k.Contains("student_")).ToArray();
  194. foreach (string key in keys)
  195. {
  196. string TeamName = Request.Form[key];
  197. int courseUserId = 0;
  198. if(!Int32.TryParse(key.Split('_')[1], out courseUserId))
  199. {
  200. continue;
  201. }
  202. //if the team doesn't exist, create it before continuing
  203. if (teams.Count(t => t.Name.CompareTo(TeamName) == 0) == 0)
  204. {
  205. Team newTeam = new Team()
  206. {
  207. Name = TeamName
  208. };
  209. teams.Add(newTeam);
  210. }
  211. Team team = teams.Find(t => t.Name.CompareTo(TeamName) == 0);
  212. TeamMember tm = new TeamMember()
  213. {
  214. CourseUserID = courseUserId,
  215. Team = team
  216. };
  217. team.TeamMembers.Add(tm);
  218. }
  219. //Remove any empty teams. This is a possibility when a team was loaded from
  220. //the database and then removed using the team creation tool. Because we
  221. //retrieved it from the DB and added it to our list of teams, it will exist
  222. //but it won't have anyone assigned to it.
  223. Team[] emptyTeams = teams.Where(tm => tm.TeamMembers.Count == 0).ToArray();
  224. foreach (Team team in emptyTeams)
  225. {
  226. teams.Remove(team);
  227. //remove from the db
  228. if (team.ID > 0)
  229. {
  230. db.Teams.Remove(team);
  231. }
  232. }
  233. //attach the new teams to the assignment
  234. foreach (Team team in teams)
  235. {
  236. previousTeams.Add(new AssignmentTeam()
  237. {
  238. Assignment = Assignment,
  239. AssignmentID = Assignment.ID,
  240. Team = team,
  241. TeamID = team.ID
  242. });
  243. }
  244. }
  245. }
  246. }