/// <summary> /// Gets a JSON array of all the submissions to a certain assignment. /// Each object in the array should have the following fields: /// "fname" - first name /// "lname" - last name /// "uid" - user ID /// "time" - DateTime of the submission /// "score" - The score given to the 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> /// <returns>The JSON array</returns> public IActionResult GetSubmissionsToAssignment(string subject, int num, string season, int year, string category, string asgname) { using (Team94LMSContext db = new Team94LMSContext()) { var submissions = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class join t4 in db.Assignments on t3.CataId equals t4.Catagory join t5 in db.Submissions on t4.AssignId equals t5.Assignment join t6 in db.Students on t5.Student equals t6.UId where t1.Department == subject && t1.Number == num && t2.Semester == season && t2.Year == year && t3.Name == category && t4.Name == asgname select new { fname = t6.FirstName, lname = t6.LastName, uid = t6.UId, time = t5.Time, score = t5.Score }; return(Json(submissions.ToArray())); } }
/// <summary> /// Creates a new assignment category for the specified class. /// </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}, /// false if an assignment category with the same name already exists in the same class.</returns> public IActionResult CreateAssignmentCategory(string subject, int num, string season, int year, string category, int catweight) { using (Team94LMSContext db = new Team94LMSContext()) { try { var classID = from t1 in db.Courses where t1.Department == subject && t1.Number == num join t2 in db.Classes on t1.CourseId equals t2.Course where t2.Semester == season && t2.Year == (uint)year select t2.ClassId; AssignmentCatagories newCat = new AssignmentCatagories(); newCat.Class = classID.Single(); newCat.Name = category; newCat.Weight = (uint)catweight; db.AssignmentCatagories.Add(newCat); db.SaveChanges(); return(Json(new { success = true })); } catch (DbUpdateException e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.InnerException.Message); return(Json(new { success = false })); } } }
/*******Begin code to modify********/ /// <summary> /// Returns a JSON array of all the students in a class. /// Each object in the array should have the following fields: /// "fname" - first name /// "lname" - last name /// "uid" - user ID /// "dob" - date of birth /// "grade" - the student's grade in this class /// </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> /// <returns>The JSON array</returns> public IActionResult GetStudentsInClass(string subject, int num, string season, int year) { using (Team94LMSContext db = new Team94LMSContext()) { var studs = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course join t3 in db.Enrollment on t2.ClassId equals t3.Class join t4 in db.Students on t3.Student equals t4.UId where t1.Department == subject && t1.Number == num && t2.Semester == season && t2.Year == (uint)year select new { fname = t4.FirstName, lname = t4.LastName, uid = t4.UId, dob = t4.Dob, grade = t3.Grade }; return(Json(studs.ToArray())); } }
/// <summary> /// Retreive a JSON array of all departments from the database. /// Each object in the array should have a field called "name" and "subject", /// where "name" is the department name and "subject" is the subject abbreviation. /// </summary> /// <returns>The JSON array</returns> public IActionResult GetDepartments() { using (Team94LMSContext db = new Team94LMSContext()) { var result = from p in db.Departments select new { name = p.Name, subject = p.Abbr }; return(Json(result.ToArray())); } }
/*******Begin code to modify********/ /// <summary> /// Returns a JSON array of all the courses in the given department. /// Each object in the array should have the following fields: /// "number" - The course number (as in 5530) /// "name" - The course name (as in "Database Systems") /// </summary> /// <param name="subject">The department subject abbreviation (as in "CS")</param> /// <returns>The JSON result</returns> public IActionResult GetCourses(string subject) { using (Team94LMSContext db = new Team94LMSContext()) { var courses = from c in db.Courses where c.Department == subject select new { name = c.Name, number = c.Number }; return(Json(courses.ToArray())); } }
/// <summary> /// Returns a JSON array of all the professors working in a given department. /// Each object in the array should have the following fields: /// "lname" - The professor's last name /// "fname" - The professor's first name /// "uid" - The professor's uid /// </summary> /// <param name="subject">The department subject abbreviation</param> /// <returns>The JSON result</returns> public IActionResult GetProfessors(string subject) { using (Team94LMSContext db = new Team94LMSContext()) { var profs = from p in db.Professors where p.Department == subject select new { lname = p.LastName, fname = p.FirstName, uid = p.UId }; return(Json(profs.ToArray())); } }
private string getNewUID() { using (Team94LMSContext db = new Team94LMSContext()) { //Using functional syntax as "take" is not available with the SQL style syntax List <string> maxUIDs = new List <string>(); /* * REFACTOR LATER!! */ //Get the highest uID var uIDStu = db.Students.Select(u => u.UId).OrderByDescending(u => u).Take(1); // Check for a populated value if (uIDStu.Count() == 1) { //Add to the possible max uIDs if one is present maxUIDs.Add(uIDStu.Single()); } var uIDProf = db.Professors.Select(u => u.UId).OrderByDescending(u => u).Take(1); if (uIDProf.Count() == 1) { maxUIDs.Add(uIDProf.Single()); } var uIDAdmin = db.Admins.Select(u => u.UId).OrderByDescending(u => u).Take(1); if (uIDAdmin.Count() == 1) { maxUIDs.Add(uIDAdmin.Single()); } if (maxUIDs.Count() != 0) { //slice off the numerical portion of the greatest uID and add 1 int idNum = int.Parse(maxUIDs.OrderByDescending(u => u).First().Substring(1)) + 1; //add the u back in and pad the number with 0's if necessary return("u" + idNum.ToString().PadLeft(7, '0')); } //First user registered corner case else { return("u0000000"); } } }
/// <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.</returns> public IActionResult CreateClass(string subject, int number, string season, int year, DateTime start, DateTime end, string location, string instructor) { using (Team94LMSContext db = new Team94LMSContext()) { var timeConflict = from b in db.Classes where ((start > b.Start && start < b.End) || (end > b.Start && end < b.End)) && location == b.Loc && season == b.Semester && (uint)year == b.Year select b; if (timeConflict.Count() != 0) { return(Json(new { success = false })); } var course = from b in db.Courses where (b.Department == subject && b.Number == number) select b.CourseId; var courseConflict = from x in db.Classes where course.Contains(x.Course) && season == x.Semester && year == x.Year select x; if (courseConflict.Count() != 0) { return(Json(new { success = false })); } try { Classes c = new Classes(); c.Course = course.Single(); c.Semester = season; c.Year = (uint)year; c.Start = start; c.End = end; c.Loc = location; c.Prof = instructor; db.Classes.Add(c); db.SaveChanges(); } catch (DbUpdateException) { return(Json(new { success = false })); } return(Json(new { success = true })); } }
/// <summary> /// Returns a JSON array with all the assignments in an assignment category for a class. /// If the "category" parameter is null, return all assignments in the class. /// Each object in the array should have the following fields: /// "aname" - The assignment name /// "cname" - The assignment category name. /// "due" - The due DateTime /// "submissions" - The number of submissions to the assignment /// </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, /// or null to return assignments from all categories</param> /// <returns>The JSON array</returns> public IActionResult GetAssignmentsInCategory(string subject, int num, string season, int year, string category) { using (Team94LMSContext db = new Team94LMSContext()) { if (category == null) { var assigns = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class join t4 in db.Assignments on t3.CataId equals t4.Catagory where t1.Department == subject && t1.Number == num && t2.Semester == season && t2.Year == (uint)year select new { aname = t4.Name, cname = t3.Name, due = t4.Due, submissions = t4.Submissions.Count() }; return(Json(assigns.ToArray())); } else { var assigns = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class join t4 in db.Assignments on t3.CataId equals t4.Catagory where t1.Department == subject && t1.Number == num && t2.Semester == season && t2.Year == (uint)year && t3.Name == category select new { aname = t4.Name, cname = t3.Name, due = t4.Due, submissions = t4.Submissions.Count() }; return(Json(assigns.ToArray())); } } }
/// <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.</returns> public IActionResult CreateCourse(string subject, int number, string name) { using (Team94LMSContext db = new Team94LMSContext()) { try { Courses c = new Courses(); c.Department = subject; c.Number = (uint)number; c.Name = name; db.Courses.Add(c); db.SaveChanges(); } catch (DbUpdateException) { return(Json(new { success = false })); } 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) { using (Team94LMSContext db = new Team94LMSContext()) { try { var assignSub = from t1 in db.Courses where t1.Department == subject && t1.Number == num join t2 in db.Classes on t1.CourseId equals t2.Course where t2.Semester == season && t2.Year == (uint)year join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class join t4 in db.Assignments on t3.CataId equals t4.Catagory where t3.Name == category && t4.Name == asgname join t5 in db.Submissions on t4.AssignId equals t5.Assignment where t5.Student == uid select t5.Assignment; var submission = from sub in db.Submissions where sub.Student == uid && sub.Assignment == assignSub.Single() select sub; foreach (Submissions sub in submission) { sub.Score = (uint)score; } db.SaveChanges(); return(Json(new { success = true })); } catch (DbUpdateException e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.InnerException.Message); return(Json(new { success = false })); } } }
/*******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 = getNewUID(); using (Team94LMSContext db = new Team94LMSContext()) { if (role == "Administrator") { Admins newUser = new Admins(); newUser.UId = uID; newUser.FirstName = fName; newUser.LastName = lName; newUser.Dob = DOB; db.Admins.Add(newUser); db.SaveChanges(); } else if (role == "Professor") { Professors newUser = new Professors(); newUser.UId = uID; newUser.FirstName = fName; newUser.LastName = lName; newUser.Dob = DOB; newUser.Department = SubjectAbbrev; db.Professors.Add(newUser); db.SaveChanges(); } else { Students newUser = new Students(); newUser.UId = uID; newUser.FirstName = fName; newUser.LastName = lName; newUser.Dob = DOB; newUser.Major = SubjectAbbrev; db.Students.Add(newUser); db.SaveChanges(); } } return(uID); }
/// <summary> /// Returns a JSON array of the assignment categories for a certain class. /// Each object in the array should have the folling fields: /// "name" - The category name /// "weight" - The category weight /// </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> /// <returns>The JSON array</returns> public IActionResult GetAssignmentCategories(string subject, int num, string season, int year) { using (Team94LMSContext db = new Team94LMSContext()) { var catagory = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class where t1.Department == subject && t1.Number == num && t2.Semester == season && t2.Year == (uint)year select new { name = t3.Name, weight = t3.Weight }; return(Json(catagory.ToArray())); } }
/// <summary> /// Returns a JSON array of the classes taught by the specified professor /// Each object in the array should have the following fields: /// "subject" - The subject abbreviation of the class (such as "CS") /// "number" - The course number (such as 5530) /// "name" - The course name /// "season" - The season part of the semester in which the class is taught /// "year" - The year part of the semester in which the class is taught /// </summary> /// <param name="uid">The professor's uid</param> /// <returns>The JSON array</returns> public IActionResult GetMyClasses(string uid) { using (Team94LMSContext db = new Team94LMSContext()) { var classes = from t1 in db.Courses join t2 in db.Classes on t1.CourseId equals t2.Course where t2.Prof == uid select new { subject = t1.Department, number = t1.Number, name = t1.Name, season = t2.Semester, year = t2.Year }; return(Json(classes.ToArray())); } }
/// <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, /// false if an assignment with the same name already exists in the same assignment category.</returns> public IActionResult CreateAssignment(string subject, int num, string season, int year, string category, string asgname, int asgpoints, DateTime asgdue, string asgcontents) { using (Team94LMSContext db = new Team94LMSContext()) { try { var catIDs = from t1 in db.Courses where t1.Department == subject && t1.Number == num join t2 in db.Classes on t1.CourseId equals t2.Course where t2.Semester == season && t2.Year == (uint)year join t3 in db.AssignmentCatagories on t2.ClassId equals t3.Class join t4 in db.Assignments on t3.CataId equals t4.Catagory where t3.Name == category select t4.Catagory; Assignments newAsign = new Assignments(); newAsign.Points = (uint)asgpoints; newAsign.Due = asgdue; newAsign.Content = asgcontents; newAsign.Catagory = catIDs.Single(); newAsign.Name = asgname; db.Assignments.Add(newAsign); db.SaveChanges(); return(Json(new { success = true })); } catch (DbUpdateException e) { Debug.WriteLine(e.Message); Debug.WriteLine(e.InnerException.Message); return(Json(new { success = false })); } } }
public CommonController() { db = new Team94LMSContext(); }
/* * WARNING: This is the quick and easy way to make the controller * use a different LibraryContext - good enough for our purposes. * The "right" way is through Dependency Injection via the constructor * (look this up if interested). */ public void UseLMSContext(Team94LMSContext ctx) { db = ctx; }