/*******Begin code to modify********/

        /// <summary>
        /// Create a new user of the LMS with the specified information.
        /// Assigns the user a unique uID consisting of a 'u' followed by 7 digits.
        /// </summary>
        /// <param name="fName">First Name</param>
        /// <param name="lName">Last Name</param>
        /// <param name="DOB">Date of Birth</param>
        /// <param name="SubjectAbbrev">The department the user belongs to (professors and students only)</param>
        /// <param name="SubjectAbbrev">The user's role: one of "Administrator", "Professor", "Student"</param>
        /// <returns>A unique uID that is not be used by anyone else</returns>
        public string CreateNewUser(string fName, string lName, DateTime DOB, string SubjectAbbrev, string role)
        {
            // Added a new row to the table CheckedOut
            using (Team11LMSContext db = new Team11LMSContext())
            {
                //// Start a transaction
                //var transaction = db.Database.BeginTransaction();
                //db.Database.AutoTransactionsEnabled = false;

                // Create a new user object.
                Users user = new Users {
                };

                db.Users.Add(user);
                db.SaveChanges();

                // Get max user id
                uint userId = (from u in db.Users select u.UId).Max <uint>();

                // Create a type of user based on role
                if (role.Equals("Administrator"))
                {
                    Administrators admin = new Administrators
                    {
                        FirstName = fName,
                        LastName  = lName,
                        Dob       = DOB,
                        UId       = userId
                    };
                    db.Administrators.Add(admin);
                    db.SaveChanges();
                }
                else if (role.Equals("Professor"))
                {
                    Professors prof = new Professors
                    {
                        FirstName  = fName,
                        LastName   = lName,
                        Dob        = DOB,
                        UId        = userId,
                        Department = SubjectAbbrev
                    };
                    db.Professors.Add(prof);
                    db.SaveChanges();
                }
                else
                {
                    Students student = new Students
                    {
                        FirstName = fName,
                        LastName  = lName,
                        Dob       = DOB,
                        UId       = userId,
                        Major     = SubjectAbbrev
                    };
                    db.Students.Add(student);
                    db.SaveChanges();
                }

                //transaction.Commit();

                string fmt = "0000000";

                return("u" + userId.ToString(fmt));
            }
        }
        public static void updateClassGrade(Team11LMSContext db, uint classId, uint uId)
        {
            // Create twos dictionary for each assignment category to possible points and earned points
            Dictionary <string, uint> possiblePoints = new Dictionary <string, uint>();
            Dictionary <string, uint> earnedPoints   = new Dictionary <string, uint>();

            // Create a dictionary for each assignment category weight
            Dictionary <string, uint> categoryWeights = new Dictionary <string, uint>();

            // Write a query to return "Assignment Category", "Category Weight", "Possible Points", and "EarnedPoint" (zero if empty)
            var query =
                (from ac in db.AssignmentCategories
                 join cl in db.Classes
                 on ac.ClassId equals cl.ClassId
                 join a in db.Assignments
                 on ac.AssignmentCategoryId equals a.AssignmentCategoryId
                 where cl.ClassId == classId
                 select new
            {
                assignmentName = a.Name,
                categoryName = ac.Name,
                AssignmentCategoryId = ac.AssignmentCategoryId,
                categoryWeight = ac.Weight,
                possiblePoints = a.Points
            }).Distinct();

            // Iterate over each row in query to sum total points and possible points to
            foreach (var row in query)
            {
                if (!possiblePoints.ContainsKey(row.categoryName))
                {
                    possiblePoints.Add(row.categoryName, row.possiblePoints);
                    categoryWeights.Add(row.categoryName, row.categoryWeight);
                    earnedPoints.Add(row.categoryName, getCategoryEarnedPoints(db, row.AssignmentCategoryId, uId));
                }
                else
                {
                    possiblePoints[row.categoryName] = possiblePoints[row.categoryName] + row.possiblePoints;
                }

                //System.Diagnostics.Debug.WriteLine("");
                //System.Diagnostics.Debug.WriteLine(row.assignmentName);
                //System.Diagnostics.Debug.WriteLine(row.categoryName);
                //System.Diagnostics.Debug.WriteLine(row.categoryWeight);
                //System.Diagnostics.Debug.WriteLine(row.possiblePoints);
                //System.Diagnostics.Debug.WriteLine("");
            }

            //System.Diagnostics.Debug.WriteLine("------------------------------");

            double weightedEarnedPercentage = 0;

            double categoryWeightSum = 0;

            double percentage;

            foreach (string key in possiblePoints.Keys)
            {
                percentage = (double)earnedPoints[key] / (double)possiblePoints[key];
                weightedEarnedPercentage = weightedEarnedPercentage + (percentage * categoryWeights[key]);

                categoryWeightSum = categoryWeightSum + categoryWeights[key];
                //System.Diagnostics.Debug.WriteLine(key);
                //System.Diagnostics.Debug.WriteLine(possiblePoints[key]);
                //System.Diagnostics.Debug.WriteLine(categoryWeights[key]);
                //System.Diagnostics.Debug.WriteLine(earnedPoints[key]);
                //System.Diagnostics.Debug.WriteLine("");
            }

            var enrolledQuery =
                from e in db.Enrolled
                where e.ClassId == classId &&
                e.UId == uId
                select e;

            try
            {
                foreach (Enrolled row in enrolledQuery)
                {
                    row.Grade = getLetterGradeFromPercentage(weightedEarnedPercentage * (100 / categoryWeightSum));
                }

                System.Diagnostics.Debug.WriteLine(weightedEarnedPercentage * (100 / categoryWeightSum));


                db.SaveChanges();
            }
            catch (Exception e)
            {
                // Actually do this in a loop and continue until a success happens
            }
        }