/// <summary>
        /// 9 April 2014
        /// Jonathan Sanborn
        /// 
        /// Constructor for an Assignment Session
        /// </summary>
        /// <param name="mmControl">The Control Object</param>
        /// <param name="assign">The Assignment for this session</param>
        public AssignmentSession(MMControl mmControl, ref Assignment assign)
        {
            init();
            MMControl = mmControl;
            assignment = assign;
            problemSetGenerator = new ProblemSetGenerator(assignment.ProblemSet);
            assignmentAttempt = new AssignmentAttempt();
            problemList = problemSetGenerator.GetProblemList;

            attemptsRemaining = (int)Assignment.ProblemSet.NumberOfAttempts;
            currentProblem = problemList.First();
            problemList.Remove(CurrentProblem);
        }
 /// <summary>
 /// Jonathan Sanborn & Harvey Mercado
 /// 
 /// Creates a new assignment for each sudent in the passed in List foreach problemSet in
 /// the passed in List
 /// </summary>
 /// <param name="students">The list of Students to create assignments for</param>
 /// <param name="problemSetList">The List of Problemsets to create assignmetns from</param>
 internal void AddNewAssignment(List<Student> students, List<ProblemSet> problemSetList) 
 {
     foreach (Student student in students)
     {
         Student s = StudentList.Where(w => student.ID == w.ID).First();
         foreach (ProblemSet ps in problemSetList)
         {
             Assignment assign = new Assignment(this, Guid.NewGuid().ToString(), s, ps);
             s.Assignments.Add(assign);
             FileHandler.SaveNewAssignment(assign);
         }
     }
 }
            /// <summary>
            /// Jonathan Sanborn
            /// 
            /// Deletes the passed in assignment from the XML
            /// </summary>
            /// <param name="assignment">The assignment to delete</param>
            public void DeleteAssignment(Assignment assignment)
            {
                string fileName = Path.Combine(filePath, Properties.Settings.Default.assignmentFilename);
                try
                {
                    XDocument assignmentFile = OpenFile(fileName, MMFileType.Assignment);

                    if (assignmentFile != null)
                    {
                        XElement elem = assignmentFile.Descendants("Assignment").First(s => s.Element("ID").Value == assignment.ID);
                        
                        if (elem != null)
                        {elem.Remove();}

                        assignmentFile.Save(fileName);
                    }

                }
                catch (FileNotFoundException ex)
                {
                    System.Diagnostics.Debug.Write(ex.Message);
                }
            }
            //public List<AssignmentAttempt> GetAllAssignmentAttempts()
            //{
            //    List<AssignmentAttempt> assignmentAttempsList = new List<AssignmentAttempt>();
            //    string fileName = Path.Combine(filePath, Properties.Settings.Default.assignmentAttempsFilename);
            //    XDocument assignmentAttempsDocument;

            //    try
            //    {
            //        assignmentAttempsDocument = OpenFile(fileName, MMFileType.AssignmentAttempt);
            //        if (assignmentAttempsDocument != null)
            //        {
            //            assignmentAttempsList = assignmentAttempsDocument.Descendants("AssignmentAttempt").Select(d => new AssignmentAttempt(MMControl, d)).ToList();
            //        }
            //    }
            //    catch (System.ArgumentOutOfRangeException ex)
            //    {
            //        System.Diagnostics.Debug.Write(ex.Message);
            //    }

            //    return assignmentAttempsList;
            //}

            //public AssignmentAttempt GetAssignmentAttemptByID(string id)
            //{
            //    var assignmentAttempts = from assignmentAttempt in GetAllAssignmentAttempts()
            //                             where assignmentAttempt.ID == id
            //                             select assignmentAttempt;
            //    List<AssignmentAttempt> assignmentAttemptList = new List<AssignmentAttempt>(assignmentAttempts);
            //    return assignmentAttemptList.Count > 0 ? assignmentAttemptList[0] : null;
            //}

            public List<AssignmentAttempt> GetAssignmentAttemptsByAssignment(Assignment assign)
            {
                List<AssignmentAttempt> assignmentAttempsList = new List<AssignmentAttempt>();
                string fileName = Path.Combine(filePath, Properties.Settings.Default.assignmentAttempsFilename);
                XDocument assignmentAttempsDocument;


                 try
                 {
                     assignmentAttempsDocument = OpenFile(fileName, MMFileType.AssignmentAttempt);
                     if (assignmentAttempsDocument != null)
                     {
                         assignmentAttempsList = assignmentAttempsDocument.Descendants("AssignmentAttempt").Where(w => assign.ID == w.Element("AssignmentID").Value).Select(d => new AssignmentAttempt(assign, d)).ToList();
                     }
                 }
                 catch (System.ArgumentOutOfRangeException ex)
                 {
                     System.Diagnostics.Debug.Write(ex.Message);
                 }

                 return assignmentAttempsList;
            }
            /// <summary>
            /// 31 March 2014
            /// Jonathan Sanborn
            /// Decoupled Assignments and FileHandler
            /// 
            /// </summary>
            /// <param name="assignment">The Assignment to be saved in XMl file</param>
            public void SaveNewAssignment(Assignment assignment)
            {
                string fileName = Path.Combine(filePath, Properties.Settings.Default.assignmentFilename);

                try
                {
                    XDocument assignmentFile = OpenFile(fileName, MMFileType.Assignment);

                    if (assignmentFile != null)
                    {                        
                        assignmentFile.Element("Assignments").Add(assignment.GetXMLNode());
                        assignmentFile.Save(fileName);
                    }
                }
                catch (FileNotFoundException ex)
                {
                    System.Diagnostics.Debug.Write(ex.Message);
                }
            }
  /// <summary>
 /// Jonathan Sanborn & Harvey Mercado
  /// 
  /// Creates a Assignment Attempt from the passed in XML node
  /// </summary>
  /// <param name="assign">The Assignment that was attempted</param>
  /// <param name="d">A assignment Attempt XElement</param>
 public AssignmentAttempt(Assignment assign, XElement d)
 {
     ID = d.Element("ID").Value;
     Date = DateTime.Parse(d.Element("Date").Value);
     Grade = Double.Parse(d.Element("Grade").Value);
     TimeSpent = System.Xml.XmlConvert.ToTimeSpan(d.Element("TimeSpent").Value);
     Assignment = assign;
 }
         /// <summary>
        ///  22 March 2014
        ///  Jonathan Sanborn & Harvey Mercado
        ///  Parameterized Constructor
         /// </summary>
         /// <param name="id">A Unique ID to identify the assignment attempt</param>
         /// <param name="assignment">The assignment that was attempted</param>
         /// <param name="date">The Date and time the assignment was attempted</param>
         /// <param name="grade">The percent correct on this attempt as a value 0.0-1.0</param>
         /// <param name="timeSpent">The amount of time the user spent working on this assignment attempt</param>
        public AssignmentAttempt(string id, Assignment assignment, DateTime date, double grade, TimeSpan timeSpent)
        {
            init();

            this.ID = id;
            this.Assignment = assignment;
            this.Date = date;
            this.Grade = grade;
            this.TimeSpent = timeSpent;
        }
        /// <summary>
        /// Jonathan Sanborn
        /// 
        /// Updates the XML of the passed in assignment
        /// </summary>
        /// <param name="assignment">The assignment to update</param>
        internal void UpdateAssignment(Assignment assignment)
        {
            string fileName = Path.Combine(filePath, Properties.Settings.Default.assignmentFilename);

            try
            {
                XDocument assignmentFile = OpenFile(fileName, MMFileType.Assignment);

                if (assignmentFile != null)
                {
                    assignmentFile.Descendants("Assignment").First(s => s.Element("ID").Value == assignment.ID).ReplaceWith(assignment.GetXMLNode());
                    assignmentFile.Save(fileName);
                }

            }
            catch (FileNotFoundException ex)
            {
                System.Diagnostics.Debug.Write(ex.Message);
            }

        }
        /// <summary> 
        /// Be handed control by CtrlStudnt object
        /// </summary>
        /// <author> Jeff Bunce </author>
        internal void TakeControl()
        {
            assignedDrill = loggedInStudent.Assignments.Where(w => !w.IsCompleted).First();

            problemSetGenerator = new ProblemSetGenerator(assignedDrill.ProblemSet);
            problemSet = assignedDrill.ProblemSet;
            problemList = problemSetGenerator.GetProblemList;
            attemptsRemaining = (int)assignedDrill.ProblemSet.NumberOfAttempts;
            currentProblem = problemList.First();
            problemList.Remove(CurrentProblem);
            ProblemNumber = 1;
            ProblemsCorrect = 0;
            ProblemsIncorrect = 0;
            sessionRunning = false;

            

            switch (problemSet.Operation.ToString())
            {
                case "Addition":
                    operationIndex = 1;
                    break;
                case "Subtraction":
                    operationIndex = 2;
                    break;
                case "Multiplication":
                    operationIndex = 3;
                    break;
                case "Division":
                    operationIndex = 4;
                    break;
                default:
                    operationIndex = 0;
                    break;
            }

            studentDrillForm.DisplayProblem(problemSet.NumberOfProblems.ToString(), (problemNumber).ToString(), operationIndex,
                currentProblem.Operand1.ToString(), currentProblem.Operand2.ToString());
            studentDrillForm.DisplayFeedback(String.Empty); // feedback given upon giving an answer

            studentDrillForm.ShowDialog();
        }