/// <summary>
        /// Creates a new assignment for the given class and category.
        /// An assignment category (which belongs to a class) can not have two assignments with
        /// the same name.
        /// If an assignment of the given category with the given name already exists, return success = false.
        /// </summary>
        /// <param name="subject">The course subject abbreviation</param>
        /// <param name="num">The course number</param>
        /// <param name="season">The season part of the semester for the class the assignment belongs to</param>
        /// <param name="year">The year part of the semester for the class the assignment belongs to</param>
        /// <param name="category">The name of the assignment category in the class</param>
        /// <param name="asgname">The new assignment name</param>
        /// <param name="asgpoints">The max point value for the new assignment</param>
        /// <param name="asgdue">The due DateTime for the new assignment</param>
        /// <param name="asgcontents">The contents of the new assignment</param>
        /// <returns>A JSON object containing success = true/false</returns>
        public IActionResult CreateAssignment(string subject, int num, string season, int year, string category, string asgname, int asgpoints, DateTime asgdue, string asgcontents)
        {
            // Check if assignment name already exists in the category.
            var query =
                from co in db.Courses // COURSES to CLASSES
                join cl in db.Classes on co.CatalogId equals cl.Offering into join1

                from j1 in join1 // CLASSES to ASSIGNMENT CATEGORIES
                join ac in db.AssignmentCategories on j1.ClassId equals ac.Class into join2

                from j2 in join2 // ASSIGNMENT CATEGORIES to ASSIGNMENTS
                join agn in db.Assignments on j2.CategoryId equals agn.Category into join3
                from j3 in join3

                where co.Department == subject &&
                co.Number == num &&
                j1.Season == season &&
                j1.Year == year &&
                j2.Name == category &&
                j3.Name == asgname
                select j3.Name;

            // Can't add if the assignment already exists
            if (query.Count() > 0)
            {
                return(Json(new { success = false }));
            }

            Models.LMSModels.Assignments assignment = new Models.LMSModels.Assignments();
            assignment.Name           = asgname;
            assignment.Due            = asgdue;
            assignment.Points         = asgpoints;
            assignment.Contents       = asgcontents;
            assignment.SubmissionType = false;
            // Get category
            var category_query =
                from co in db.Courses         // COURSES to CLASSES
                join cl in db.Classes on co.CatalogId equals cl.Offering into join1

                from j1 in join1         // CLASSES to ASSIGNMENT CATEGORIES
                join ac in db.AssignmentCategories on j1.ClassId equals ac.Class into join2
                from j2 in join2
                where co.Department == subject &&
                co.Number == num &&
                j1.Season == season &&
                j1.Year == year &&
                j2.Name == category
                select j2.CategoryId;

            assignment.Category = category_query.First();

            // Insert the assignment to the database
            db.Assignments.Add(assignment);
            try
            {
                db.SaveChanges();

                var update_query =
                    from co in db.Courses // COURSES to CLASSES
                    join cl in db.Classes on co.CatalogId equals cl.Offering into join1
                    from j1 in join1
                    join E in db.Enrolled on j1.ClassId equals E.Class into join2
                    from j2 in join2
                    where co.Department == subject &&
                    co.Number == num &&
                    j1.Season == season &&
                    j1.Year == year
                    select j2;

                foreach (var j2 in update_query)
                {
                    updateGrade(j2.Student, j2.Class);
                }
                return(Json(new { success = true }));
            }
            catch // If inserting changes to database fails
            {
                return(Json(new { success = false }));
            }
        }
        /// <summary>
        /// Creates a new assignment for the given class and category.
        /// </summary>
        /// <param name="subject">The course subject abbreviation</param>
        /// <param name="num">The course number</param>
        /// <param name="season">The season part of the semester for the class the assignment belongs to</param>
        /// <param name="year">The year part of the semester for the class the assignment belongs to</param>
        /// <param name="category">The name of the assignment category in the class</param>
        /// <param name="asgname">The new assignment name</param>
        /// <param name="asgpoints">The max point value for the new assignment</param>
        /// <param name="asgdue">The due DateTime for the new assignment</param>
        /// <param name="asgcontents">The contents of the new assignment</param>
        /// <returns>A JSON object containing success = true/false</returns>
        public IActionResult CreateAssignment(string subject, int num, string season, int year, string category, string asgname, int asgpoints, DateTime asgdue, string asgcontents)
        {
            var chars       = "0123456789";
            var stringChars = new char[5];
            var random      = new Random();

            for (int i = 0; i < stringChars.Length; i++)
            {
                stringChars[i] = chars[random.Next(chars.Length)];
            }

            var AssignmentID = new String(stringChars);

            using (Models.LMSModels.Team13LMSContext db = new Models.LMSModels.Team13LMSContext())
            {
                var query =
                    from c in db.Classes
                    join course in db.Courses
                    on c.CourseNumber equals course.CourseNumber

                    where course.SubjectAbbreviation == subject && course.CourseNumber == num && c.SemesterSeason == season && c.SemesterYear == year
                    select c.ClassId;

                var query1 =
                    from ac in db.AssignmentCategories
                    where ac.Name == category && ac.ClassId == query.ToArray()[0]
                    select ac.AssignmentCategoryId;

                var query2 =
                    from a in db.Assignments
                    where a.AssignmentCategoryId == query1.ToArray()[0] && a.Name == asgname
                    select a;

                if (query2.Count() != 0)
                {
                    return(Json(new { success = false }));
                }

                Models.LMSModels.Assignments a1 = new Models.LMSModels.Assignments();
                a1.AssignmentId         = AssignmentID;
                a1.DueDate              = asgdue;
                a1.Name                 = asgname;
                a1.Contents             = asgcontents;
                a1.MaxPointValue        = asgpoints;
                a1.AssignmentCategoryId = query1.ToArray()[0];
                db.Assignments.Add(a1);
                db.SaveChanges();

                var allStudents =
                    from se in db.StudentEnrollment
                    where se.ClassId == query.ToArray()[0]
                    select se;

                foreach (var s in allStudents)
                {
                    updateStudentGradeForClass(s.ClassId, s.UId);
                }

                return(Json(new { success = true }));
            }
        }