/// <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) { using (db) { int cid = 0; var query = from p in db.Courses where p.Subject == subject && p.Number == num.ToString() join c in db.Classes on p.CatalogId equals c.CatalogId where c.Semester == (season + year.ToString()) select c.CId; cid = query.ToArray()[0]; /* Generate assignments for the enrolled student */ var query2 = from p in db.AssignmentCategories where p.CId == cid join a in db.Assignments on p.AcId equals a.AcId select a.AId; if (query.ToArray().Count() != 0) { /* update enroll table */ EnrollmentGrade en = new EnrollmentGrade(); en.UId = uid; en.Grade = null; en.CId = query.ToArray()[0]; db.Add(en); /* update submission table */ foreach (int aid in query2.ToArray()) { Submission sb = new Submission(); sb.AId = aid; sb.UId = uid; sb.Score = -1; sb.Time = DateTime.Now; sb.Contents = ""; db.Add(sb); } try { db.SaveChanges(); return(Json(new { success = true })); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); return(Json(new { success = false })); } } } return(Json(new { success = false })); }
/// <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) { string semester = season + " " + year; var query = from course in db.Courses where course.Department == subject && course.Number == num join cls in db.Classes on course.CatalogId equals cls.CatalogId into classes from cls2 in classes where cls2.Semester == semester join enroll in db.EnrollmentGrade on cls2.ClassId equals enroll.CId into enrollments from enroll2 in enrollments where enroll2.UId == uid select new { uID = enroll2.UId }; if (query.Any()) { return(Json(new { success = false })); } else { var query2 = from course in db.Courses where course.Department == subject && course.Number == num join cls in db.Classes on course.CatalogId equals cls.CatalogId into classes from cls2 in classes where cls2.Semester == semester select new { cID = cls2.ClassId }; if (query2.Any()) { EnrollmentGrade enroll = new EnrollmentGrade { UId = uid, CId = query2.First().cID, Grade = "--" }; db.EnrollmentGrade.Add(enroll); try { db.SaveChanges(); return(Json(new { success = true })); } catch (Exception e) { Console.WriteLine(e.Message); } } } return(Json(new { success = false })); }