/*******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)
        {
            string uID = "";

            using (Team9LMSContext db = new Team9LMSContext())
            {
                Users newUser = new Users
                {
                    FirstName = fName,
                    LastName  = lName,
                    Dob       = DOB
                };

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

                var query = from u in db.Users
                            where u.FirstName == fName && u.LastName == lName
                            select new { uID = u.UId };

                foreach (var q in query)
                {
                    string[] sList = Convert.ToString(q.uID).Split(" ");
                    uID = sList[0];
                }

                if (role == "Administrator")
                {
                    Administrators newAd = new Administrators
                    {
                        UId = uID
                    };
                    db.Administrators.Add(newAd);
                    db.SaveChanges();
                }

                if (role == "Professor")
                {
                    Professors newPro = new Professors
                    {
                        UId     = uID,
                        Subject = SubjectAbbrev
                    };
                    db.Professors.Add(newPro);
                    db.SaveChanges();
                }

                if (role == "Student")
                {
                    Students newStu = new Students
                    {
                        UId     = uID,
                        Subject = SubjectAbbrev
                    };
                    db.Students.Add(newStu);
                    db.SaveChanges();
                }
            }
            return(uID);
        }
        /// <summary>
        /// Adds a submission to the given assignment for the given student
        /// The submission should use the current time as its DateTime
        /// You can get the current time with DateTime.Now
        /// The score of the submission should start as 0 until a Professor grades it
        /// If a Student submits to an assignment again, it should replace the submission contents
        /// and the submission time (the score should remain the same).
        /// </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="uid">The student submitting the assignment</param>
        /// <param name="contents">The text contents of the student's submission</param>
        /// <returns>A JSON object containing {success = true/false}</returns>
        public IActionResult SubmitAssignmentText(string subject, int num, string season, int year,
                                                  string category, string asgname, string uid, string contents)
        {
            //sub, num --> catalogID
            // catalogID, year, season --> ClassID
            // classID, category --> acID
            // acID, asgname -- > aID
            // aID, uID, Constents --> add to submission
            using (Team9LMSContext db = new Team9LMSContext())
            {
                var aIDQuery = from acID in (from ci in (from cid in (from c in db.Courses
                                                                      where c.Subject == subject && c.Num == num
                                                                      select new { CatalogId = c.CatalogId })
                                                         join cl in db.Classes
                                                         on cid.CatalogId equals cl.CatalogId
                                                         where cl.Semester == year.ToString() + season
                                                         select new { classID = cl.ClassId })
                                             join ac in db.AssignmentCategories
                                             on ci.classID equals ac.ClassId
                                             where ac.Name == category
                                             select new { acID = ac.AcId })
                               join asg in db.Assignments
                               on acID.acID equals asg.AcId
                               where asg.Name == asgname
                               select new { asg.AId, asg.Points };
                uint aID = aIDQuery.First().AId;

                var subQuery = from s in db.Submission
                               where s.AId == aID && s.UId == uid
                               select s;

                if (subQuery.Count() > 0)
                {
                    subQuery.First().Time     = DateTime.Now;
                    subQuery.First().Contents = contents;
                    try
                    {
                        db.Update(subQuery.First());
                        db.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        System.Diagnostics.Debug.WriteLine(e);
                    }
                }
                else
                {
                    Submission newSub = new Submission();
                    newSub.AId      = aID;
                    newSub.UId      = uid;
                    newSub.Time     = DateTime.Now;
                    newSub.Contents = contents;
                    //newSub.Score = 0;
                    db.Submission.Add(newSub);
                    db.SaveChanges();
                }
            }
            return(Json(new { success = true }));
        }
        /// <summary>
        /// Enrolls a student in a class.
        /// </summary>
        /// <param name="subject">The department subject abbreviation</param>
        /// <param name="num">The course number</param>
        /// <param name="season">The season part of the semester</param>
        /// <param name="year">The year part of the semester</param>
        /// <param name="uid">The uid of the student</param>
        /// <returns>A JSON object containing {success = {true/false}.
        /// false if the student is already enrolled in the class, true otherwise.</returns>
        public IActionResult Enroll(string subject, int num, string season, int year, string uid) //works
        {
            //sub , num --> catalogID
            // catalogID, season, year --> classid
            // classid, uied -- add to Enroll
            var flag = false;

            using (Team9LMSContext db = new Team9LMSContext())
            {
                var classIDQuery = from cid in (from c in db.Courses
                                                where c.Subject == subject && c.Num == num
                                                select new { CatalogId = c.CatalogId })
                                   join cl in db.Classes
                                   on cid.CatalogId equals cl.CatalogId
                                   where cl.Semester == year.ToString() + season
                                   select new { classID = cl.ClassId };
                Enrolled newEn = new Enrolled();
                newEn.UId     = uid;
                newEn.ClassId = (uint)Convert.ToInt32(classIDQuery.First().classID);

                db.Enrolled.Add(newEn);
                db.SaveChanges();

                flag = true;
            }
            return(Json(new { success = flag }));
        }
예제 #4
0
        /// <summary>
        /// Creates a course.
        /// A course is uniquely identified by its number + the subject to which it belongs
        /// </summary>
        /// <param name="subject">The subject abbreviation for the department in which the course will be added</param>
        /// <param name="number">The course number</param>
        /// <param name="name">The course name</param>
        /// <returns>A JSON object containing {success = true/false}.
        /// false if the course already exists, true otherwise.</returns>
        public IActionResult CreateCourse(string subject, int number, string name) //works
        {
            if (number > 9999 || number < 0)
            {
                return(Json(new { success = false }));
            }
            if (name.Length > 100)
            {
                return(Json(new { success = false }));
            }

            using (Team9LMSContext db = new Team9LMSContext())
            {
                //check to void duplicate (sub, num)
                var query = from c in db.Courses
                            where c.Subject == subject && c.Num == (uint)number
                            select c;
                if (query.Count() > 0)
                {
                    return(Json(new { success = false }));
                }

                //add new entry to db
                Courses newCourse = new Courses
                {
                    Name    = name,
                    Num     = (uint)number,
                    Subject = subject
                };

                db.Courses.Add(newCourse);
                db.SaveChanges();
            }
            return(Json(new { success = true }));
        }
        /// <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)  // works
        {
            //subject, num -- catalogId
            //catalogID, semester -- > classID
            //class ID  , Name - ACID
            // asgname, asgcontenst, asgpoint, asgdue, acid  --- add new assignment
            using (Team9LMSContext db = new Team9LMSContext())
            {
                var acIDQuery = from ci in (from cid in (from c in db.Courses
                                                         where c.Subject == subject && c.Num == num
                                                         select new { CatalogId = c.CatalogId })
                                            join cl in db.Classes
                                            on cid.CatalogId equals cl.CatalogId
                                            where cl.Semester == year.ToString() + season
                                            select new { classID = cl.ClassId })
                                join ac in db.AssignmentCategories
                                on ci.classID equals ac.ClassId
                                where ac.Name == category
                                select new { acID = ac.AcId, ci.classID };
                uint acID = (uint)Convert.ToInt32(acIDQuery.First().acID);



                //check to void duiplicate (name , acID )
                var duplicateCheckQuery = from asg in db.Assignments
                                          where asg.Name == asgname && asg.AcId == acID
                                          select asg;
                if (duplicateCheckQuery.Count() > 0)
                {
                    return(Json(new { success = false }));
                }

                if (asgpoints < 0)
                {
                    return(Json(new { success = false }));
                }

                //add new entry to db
                Assignments newAS = new Assignments();
                newAS.AcId     = acID;
                newAS.Name     = asgname;
                newAS.Contents = asgcontents;
                newAS.Points   = (uint)asgpoints;
                newAS.Due      = asgdue;
                db.Assignments.Add(newAS);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    return(Json(new { success = false }));
                }
                //updata all students's grades.
                uint classID = (uint)Convert.ToInt32(acIDQuery.First().classID);
                GPA.ClassGradeUpdate(classID);
            }
            return(Json(new { success = true }));
        }
        /// <summary>
        /// Set the score of an assignment submission
        /// </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 name of the assignment</param>
        /// <param name="uid">The uid of the student who's submission is being graded</param>
        /// <param name="score">The new score for the submission</param>
        /// <returns>A JSON object containing success = true/false</returns>
        public IActionResult GradeSubmission(string subject, int num, string season, int year, string category, string asgname, string uid, int score)
        //works
        {
            uint classID = 0;

            using (Team9LMSContext db = new Team9LMSContext())
            {
                var subQuery = from aid in (from acID in (from ci in (from cid in (from c in db.Courses
                                                                                   where c.Subject == subject && c.Num == num
                                                                                   select new { CatalogId = c.CatalogId })
                                                                      join cl in db.Classes
                                                                      on cid.CatalogId equals cl.CatalogId
                                                                      where cl.Semester == year.ToString() + season
                                                                      select new { classID = cl.ClassId })
                                                          join ac in db.AssignmentCategories
                                                          on ci.classID equals ac.ClassId
                                                          where ac.Name == category
                                                          select new { acID = ac.AcId })
                                            join asg in db.Assignments
                                            on acID.acID equals asg.AcId
                                            where asg.Name == asgname
                                            select new { asg.AId, asg.Points })
                               join s in db.Submission
                               on aid.AId equals s.AId
                               where s.UId == uid
                               select new { s, aid.Points };

                uint points = (uint)subQuery.First().Points;
                if (score < 0 || score > points)
                {
                    return(Json(new { success = false }));
                }

                subQuery.First().s.Score = (uint)score;
                db.Update(subQuery.First().s);
                db.SaveChanges();

                //update student's letter grade.
                var classIDQuery = from cid in (from c in db.Courses
                                                where c.Subject == subject && c.Num == num
                                                select new { CatalogId = c.CatalogId })
                                   join cl in db.Classes
                                   on cid.CatalogId equals cl.CatalogId
                                   where cl.Semester == year.ToString() + season
                                   select new { classID = cl.ClassId };
                classID = (uint)Convert.ToInt32(classIDQuery.First().classID);
                GPA.GradeUpdate(uid, classID);
            }

            return(Json(new { success = true }));
        }
예제 #7
0
        /// <summary>
        /// Creates a class offering of a given course.
        /// </summary>
        /// <param name="subject">The department subject abbreviation</param>
        /// <param name="number">The course number</param>
        /// <param name="season">The season part of the semester</param>
        /// <param name="year">The year part of the semester</param>
        /// <param name="start">The start time</param>
        /// <param name="end">The end time</param>
        /// <param name="location">The location</param>
        /// <param name="instructor">The uid of the professor</param>
        /// <returns>A JSON object containing {success = true/false}.
        /// false if another class occupies the same location during any time
        /// within the start-end range in the same semester, or if there is already
        /// a Class offering of the same Course in the same Semester,
        /// true otherwise.</returns>
        public IActionResult CreateClass(string subject, int number, string season, int year, DateTime start, DateTime end, string location, string instructor) //works
        {
            using (Team9LMSContext db = new Team9LMSContext())
            {
                var catalogIDQuery = from c in db.Courses
                                     where c.Subject == subject && c.Num == number
                                     select new { c.CatalogId };
                uint catalogID = 0;
                foreach (var cq in catalogIDQuery)
                {
                    catalogID = cq.CatalogId;
                }
                // To void Location and time overlay or duplicate class in same semester
                TimeSpan nStart = DT2TimeSpan(start);
                TimeSpan nEnd   = DT2TimeSpan(end);

                var locationTimeQuery = from c in db.Classes
                                        where (c.CatalogId == catalogID && c.Semester == year.ToString() + season) || ((c.Location == location && ((c.StartTime <= nStart && c.EndTime >= nEnd) || (c.StartTime >= nStart && c.StartTime <= nEnd) || (c.EndTime >= nStart && c.EndTime <= nEnd))) && (c.Semester == year.ToString() + season))
                                        select c;
                if (locationTimeQuery.Count() > 0)
                {
                    return(Json(new { success = false }));
                }

                Classes newClass = new Classes
                {
                    Semester  = Convert.ToString(year) + season,
                    CatalogId = catalogID,
                    Location  = location,
                    StartTime = DT2TimeSpan(start),
                    EndTime   = DT2TimeSpan(end),
                    UId       = instructor
                };

                db.Classes.Add(newClass);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception)
                {
                    return(Json(new { success = false }));
                }
            }

            return(Json(new { success = true }));
        }
        /// <summary>
        /// Creates a new assignment category for the specified class.
        /// If a category of the given class 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 new category name</param>
        /// <param name="catweight">The new category weight</param>
        /// <returns>A JSON object containing {success = true/false} </returns>
        public IActionResult CreateAssignmentCategory(string subject, int num, string season, int year, string category, int catweight)  //works
        {
            if (catweight.GetType() != typeof(int) || catweight < 0)
            {
                return(Json(new { success = false }));
            }

            using (Team9LMSContext db = new Team9LMSContext())
            {
                var classIDQuery = from cid in (from c in db.Courses
                                                where c.Subject == subject && c.Num == num
                                                select new { CatalogId = c.CatalogId })
                                   join cl in db.Classes
                                   on cid.CatalogId equals cl.CatalogId
                                   where cl.Semester == year.ToString() + season
                                   select new { classID = cl.ClassId };
                if (classIDQuery.Count() == 0)
                {
                    return(Json(new { success = false }));
                }
                foreach (var cq in classIDQuery)
                {
                    var ClassID = Convert.ToInt32(cq.classID);
                    AssignmentCategories newAC = new AssignmentCategories();
                    newAC.ClassId = (uint)ClassID;
                    newAC.Name    = category;
                    newAC.Weight  = (uint)catweight;

                    db.AssignmentCategories.Add(newAC);
                }
                try
                {
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("CreateAssignmentCategoryCreateAssignmentCategory");
                }
            }
            return(Json(new { success = true }));
        }
예제 #9
0
        public static void GradeUpdate(string uid, uint classID)
        {
            Dictionary <uint, uint> acIDAndWeight      = new Dictionary <uint, uint>();
            Dictionary <uint, uint> acIDAndTotalPoints = new Dictionary <uint, uint>();
            Dictionary <uint, uint> acIDAndTotalScores = new Dictionary <uint, uint>();
            Dictionary <uint, uint> acIDAndTotalAsgs   = new Dictionary <uint, uint>();
            List <uint>             acIDs = new List <uint>();
            uint totalWeight = 0;

            using (Team9LMSContext db = new Team9LMSContext())
            {
                //Get all categories of this class
                var awQuery = from ac in db.AssignmentCategories
                              where ac.ClassId == classID
                              select new { ac.AcId, ac.Weight };
                foreach (var aw in awQuery)
                {
                    acIDAndWeight.Add(aw.AcId, (uint)aw.Weight);
                    acIDAndTotalPoints.Add(aw.AcId, 0);
                    acIDAndTotalScores.Add(aw.AcId, 0);
                    acIDs.Add(aw.AcId);
                }

                //Get all score of each assignment in each assignment category
                var asgsQuery = from asg1 in (from acID in (from ac in db.AssignmentCategories
                                                            where ac.ClassId == classID
                                                            select new { ac.AcId })
                                              join asg in db.Assignments
                                              on acID.AcId equals asg.AcId
                                              select new { asg.AId, asg.Points, asg.AcId })
                                join s in db.Submission
                                on asg1.AId equals s.AId
                                where s.UId == uid
                                select new { s, points = asg1.Points, acID = asg1.AcId };

                foreach (var a1 in asgsQuery)
                {
                    uint acID  = (uint)a1.acID;
                    uint score = (uint?)a1.s.Score ?? 0;
                    acIDAndTotalScores[acID] += score;
                }

                //Get all point of each assignment
                var pointsQuery = from acID in (from ac in db.AssignmentCategories
                                                where ac.ClassId == classID
                                                select new { ac.AcId })
                                  join asg in db.Assignments
                                  on acID.AcId equals asg.AcId
                                  select new { asg.AId, points = asg.Points, acID = asg.AcId };

                foreach (var pq in pointsQuery)
                {
                    uint acID   = (uint)pq.acID;
                    uint points = (uint)pq.points;
                    acIDAndTotalPoints[acID] += points;
                }

                //Get number of assignments for each assignment category
                var asgNumQuery = from ac in db.AssignmentCategories
                                  where ac.ClassId == classID
                                  select new { acID = ac.AcId, numAsg = numAssignments(ac.AcId) };

                foreach (var aNQ in asgNumQuery)
                {
                    acIDAndTotalAsgs.Add(aNQ.acID, aNQ.numAsg);
                }

                //calculation of total weight and scalefactor
                for (int i = 0; i < acIDs.Count(); i++)
                {
                    var tempAcID = acIDs[i];
                    if (acIDAndTotalAsgs[tempAcID] != 0)
                    {
                        totalWeight += acIDAndWeight[tempAcID];
                    }
                }

                double scaleFactor = 100.00 / totalWeight;

                //Calculation for final score for this class
                double finalScore = 0.0;
                for (int i = 0; i < acIDs.Count(); i++)
                {
                    var tempAcID = acIDs[i];
                    if (acIDAndTotalPoints[tempAcID] != 0)  // denominator should not be zero  --- mean no assigments in this category or this assignment will not be counted.
                    {
                        double tempScore = (double)acIDAndTotalScores[tempAcID] / acIDAndTotalPoints[tempAcID] * acIDAndWeight[tempAcID] * scaleFactor;
                        finalScore += tempScore;
                    }
                }

                //convert score to letter grade
                List <int> percentage = new List <int> {
                    93, 90, 87, 83, 80, 77, 73, 70, 67, 63, 60, 0
                };
                List <string> letters = new List <string> {
                    "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "E"
                };
                int    index       = indexOfLetters(percentage, finalScore);
                string letterGrade = letters[index];

                //update letter grade to DB
                var gradeQuery = from e in db.Enrolled
                                 where e.ClassId == classID && e.UId == uid
                                 select e;
                gradeQuery.First().Grade = letterGrade;
                db.Update(gradeQuery.First());
                db.SaveChanges();
            }
        }