/// <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) { using (Team9Context db = new Team9Context()) { if (score < 0) { return(Json(new { success = false })); } var queryAssSubmissions = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId join ac in db.AssignmentCategory on cl.ClassId equals ac.ClassId join ass in db.Assignment on ac.CatId equals ass.Category join sub in db.Submission on ass.AId equals sub.AId join stud in db.Student on sub.UId equals stud.UId where cr.CourseNum == num && cl.Season == season && cl.Year == year && cr.DId == subject && ac.Name == category && ass.Name == asgname && stud.UId == uid select new { sub, aPoints = ass.Points, classID = cl.ClassId }; Submission newSub = null; if (queryAssSubmissions.Count() != 0) { newSub = queryAssSubmissions.ToArray()[0].sub; newSub.Score = score; //db.Submission.Update(newSub); db.SaveChanges(); } //calculate grade and submit to enroll string grade = CalculateGrade(queryAssSubmissions.ToArray()[0].classID, uid); var queryEnroll = from e in db.Enrolled where e.UId == uid && e.ClassId == queryAssSubmissions.ToArray()[0].classID select e; Enrolled enroll = queryEnroll.ToArray()[0]; enroll.Grade = grade; db.Enrolled.Update(enroll); 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.</returns> public IActionResult Enroll(string subject, int num, string season, int year, string uid) { using (Team9Context db = new Team9Context()) { //var queryEnrolled = // from s in db.Student // join e in db.Enrolled on s.UId equals e.UId // join cl in db.Class on e.ClassId equals cl.ClassId // join cr in db.Course on cl.CatId equals cr.CatId // where e.UId == uid && cr.CourseNum == num && cl.Season == season && cl.Year == year && cr.DId == subject // select new { uid = e.UId, classID = cl.ClassId }; var queryEnrolled = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId into join1 from j1 in join1.DefaultIfEmpty() join e in db.Enrolled on j1.ClassId equals e.ClassId into join3 from j3 in join3.DefaultIfEmpty() where cr.CourseNum == num && j1.Season == season && j1.Year == year && cr.DId == subject select new { uid = j3.UId ?? null, classID = j1.ClassId }; bool uidEnrolled = false; for (int i = 0; i < queryEnrolled.Count(); i++) { if (queryEnrolled.ToArray()[0].uid == uid) { uidEnrolled = true; } } if (!uidEnrolled) { //insert into enroll, grade, uid, classID Enrolled enroll = new Enrolled(); enroll.UId = uid; enroll.ClassId = queryEnrolled.ToArray()[0].classID; enroll.Grade = "--"; db.Enrolled.Add(enroll); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } return(Json(new { success = true })); } else { return(Json(new { success = false })); } } }
/// <summary> /// Creates a new assignment category for the specified class. /// A class can not have two categories with the same name. /// 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) { using (Team9Context db = new Team9Context()) { var queryCatagory = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId into join1 from j1 in join1.DefaultIfEmpty() join ac in db.AssignmentCategory on j1.ClassId equals ac.ClassId into join2 from j2 in join2.DefaultIfEmpty() where cr.CourseNum == num && j1.Season == season && j1.Year == year && cr.DId == subject select new { catName = j2.Name ?? null, classID = j1.ClassId }; bool categoryAdded = false; for (int i = 0; i < queryCatagory.Count(); i++) { if (queryCatagory.ToArray()[i].catName != null && queryCatagory.ToArray()[i].catName.Equals(category)) { categoryAdded = true; } } if (!categoryAdded) { //insert into enroll, grade, uid, classID AssignmentCategory catToAdd = new AssignmentCategory(); catToAdd.Name = category; catToAdd.Weight = catweight; catToAdd.ClassId = queryCatagory.ToArray()[0].classID; db.AssignmentCategory.Add(catToAdd); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } return(Json(new { success = true })); } else { return(Json(new { success = false })); } } }
/// <summary> /// Creates a course. /// </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) { using (Team9Context db = new Team9Context()) { var queryAllCourse = from cr in db.Course orderby cr.CatId descending select cr; int maxCorId = queryAllCourse.ToArray()[0].CatId; var queryCourses = from cr in db.Course where cr.CourseNum == number && cr.Name == name && cr.DId == subject select new { courseNumber = cr.CourseNum, courseName = cr.Name, courseSubject = cr.DId }; if (queryCourses.Count() == 0) { Course newCourse = new Course(); newCourse.DId = subject; newCourse.CourseNum = number; newCourse.Name = name; newCourse.CatId = maxCorId + 1; db.Course.Add(newCourse); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } return(Json(new { success = true })); } else { return(Json(new { success = false })); } } }
/// <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.</returns> public IActionResult CreateClass(string subject, int number, string season, int year, DateTime start, DateTime end, string location, string instructor) { using (Team9Context db = new Team9Context()) { var queryAllClasses = from cl in db.Class orderby cl.ClassId descending select cl; int maxClassID = queryAllClasses.ToArray()[0].ClassId; var queryClass = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId ////////////////////////////////////////////////////////// fix timing for class time range where cr.DId == subject && cr.CourseNum == number && cl.Season == season && cl.Year == year && cl.Location == location && ((cl.StartTime <= start.TimeOfDay && cl.EndTime >= end.TimeOfDay) //overlap fully //|| (cl.StartTime >= start.TimeOfDay && cl.EndTime <= end.TimeOfDay) // inside fully || (cl.EndTime > start.TimeOfDay && cl.EndTime <= end.TimeOfDay) || // end in middle (cl.StartTime > start.TimeOfDay && cl.StartTime < end.TimeOfDay) // start in middle ) select cl; var queryCourse = from cr in db.Course where cr.DId == subject && cr.CourseNum == number select cr.CatId; if (queryClass.Count() == 0) { Class newClass = new Class(); newClass.Season = season; newClass.Year = year; newClass.StartTime = start.TimeOfDay; newClass.EndTime = end.TimeOfDay; newClass.Instructor = instructor; newClass.Location = location; newClass.ClassId = maxClassID + 1; newClass.CatId = queryCourse.ToArray()[0]; db.Class.Add(newClass); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } return(Json(new { success = true })); } else { return(Json(new { success = false })); } } }
/// <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 /// </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) { using (Team9Context db = new Team9Context()) { if (contents == null) { return(Json(new { success = false })); } var queryAssignments = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId into join1 from j1 in join1.DefaultIfEmpty() join ac in db.AssignmentCategory on j1.ClassId equals ac.ClassId into join2 from j2 in join2.DefaultIfEmpty() join ass in db.Assignment on j2.CatId equals ass.Category into join3 from j3 in join3.DefaultIfEmpty() where cr.CourseNum == num && j1.Season == season && j1.Year == year && cr.DId == subject && j2.Name == category && j3.Name == asgname select new { aID = j3.AId }; int assID = queryAssignments.ToArray()[0].aID; var querySubmission = from sub in db.Submission where sub.AId == assID && sub.UId == uid select sub; Submission newSub = null; if (querySubmission.Count() != 0) { newSub = querySubmission.ToArray()[0]; newSub.Contents = contents; newSub.Time = DateTime.Now; db.Submission.Update(newSub); } else { newSub = new Submission(); newSub.Time = DateTime.Now; newSub.Contents = contents; newSub.Score = 0; newSub.UId = uid; newSub.AId = assID; //no need to add score value into the table here db.Submission.Add(newSub); } try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } return(Json(new { success = true })); } }
/// <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) { using (Team9Context db = new Team9Context()) { var queryAssignment = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId into join1 from j1 in join1.DefaultIfEmpty() join ac in db.AssignmentCategory on j1.ClassId equals ac.ClassId into join2 from j2 in join2.DefaultIfEmpty() join ass in db.Assignment on j2.CatId equals ass.Category into join3 from j3 in join3.DefaultIfEmpty() where cr.CourseNum == num && j1.Season == season && j1.Year == year && cr.DId == subject && j2.Name == category && j3.Name == asgname select new { assignName = j3.Name ?? null, catID = j2.CatId, points = j3.Points, classID = j1.ClassId }; var queryCatagory = from cr in db.Course join cl in db.Class on cr.CatId equals cl.CatId into join1 from j1 in join1.DefaultIfEmpty() join ac in db.AssignmentCategory on j1.ClassId equals ac.ClassId into join2 from j2 in join2.DefaultIfEmpty() where cr.CourseNum == num && j1.Season == season && j1.Year == year && cr.DId == subject && j2.Name == category select new { catName = j2.Name ?? null, classID = j1.ClassId, catID = j2.CatId }; var queryStudents = from s in db.Student join e in db.Enrolled on s.UId equals e.UId where e.ClassId == queryAssignment.ToArray()[0].classID select s; bool assAdded = false; for (int i = 0; i < queryAssignment.Count(); i++) { if (queryAssignment.ToArray()[i].assignName.Equals(asgname)) { assAdded = true; } } if (!assAdded) { Assignment assToAdd = new Assignment(); assToAdd.Name = asgname; assToAdd.DueDateTime = asgdue; assToAdd.Contents = asgcontents; assToAdd.Points = asgpoints; assToAdd.Category = queryCatagory.ToArray()[0].catID; db.Assignment.Add(assToAdd); db.SaveChanges(); string grade = ""; for (int i = 0; i < queryStudents.Count(); i++) { //calculate grade and submit to enroll grade = CalculateGrade(queryCatagory.ToArray()[0].classID, queryStudents.ToArray()[i].UId); var queryEnroll = from e in db.Enrolled where e.ClassId == queryAssignment.ToArray()[0].classID && e.UId == queryStudents.ToArray()[i].UId select e; Enrolled enroll = queryEnroll.ToArray()[0]; enroll.Grade = grade; db.SaveChanges(); } return(Json(new { success = true })); } else { return(Json(new { success = false })); } } }
/// <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. This uID must not be used by anyone else</returns> public string CreateNewUser(string fName, string lName, DateTime DOB, string SubjectAbbrev, string role) { string uid = generateUID(); using (Team9Context db = new Team9Context()) { if (role == "Administrator") { Administrator admin = new Administrator(); admin.UId = uid; admin.FirstName = fName; admin.LastName = lName; admin.DOB = DOB; admin.Password = ""; db.Administrator.Add(admin); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } } if (role == "Professor") { Professor prof = new Professor(); prof.UId = uid; prof.FirstName = fName; prof.LastName = lName; prof.DOB = DOB; prof.Department = SubjectAbbrev; prof.Password = ""; db.Professor.Add(prof); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } } if (role == "Student") { Student student = new Student(); student.UId = uid; student.FirstName = fName; student.LastName = lName; student.DOB = DOB; student.Major = SubjectAbbrev; student.Password = ""; db.Student.Add(student); try { db.SaveChanges(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine("Problem saving changes: " + e); } } } return(uid); }