/// <summary> /// Gets list of all assignment categories of classes attached to studentid /// </summary> /// <param name="studentid">ID of student to get assignment categories for</param> /// <returns>List(AssignmentCategory) of all assignment categories attached to studentid</returns> public List <AssignmentCategory> GetAssignmentCategoriesInStudentSchedule(int studentid) { var categoriesQuery = (from assignmentcategories in _context.AssignmentCategories join studentclasses in _context.StudentsClasses on assignmentcategories.ClassId equals studentclasses.ClassId join classes in _context.Classes on assignmentcategories.ClassId equals classes.ClassId select new { assignmentcategories.CategoryId, assignmentcategories.ClassId, assignmentcategories.CategoryName, assignmentcategories.CategoryWeight, classes.Period, studentclasses.StudentId }).Where(c => c.StudentId == studentid).OrderBy(c => c.Period); List <AssignmentCategory> categories = new List <AssignmentCategory>(); foreach (var c in categoriesQuery) { AssignmentCategory newCategory = new AssignmentCategory() { CategoryId = c.CategoryId, ClassId = c.ClassId, CategoryName = c.CategoryName, CategoryWeight = c.CategoryWeight }; categories.Add(newCategory); } return(categories); }
/// <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) { int classId = lookupClassId(subject, num, season, year); var query = from co in db.Course join cl in db.Class on co.CourseId equals cl.CourseId into Courses from c in Courses.DefaultIfEmpty() join cat in db.AssignmentCategory on c.ClassId equals cat.ClassId select new { name = cat.Name, weight = cat.Weight }; if (query.Any()) { foreach (var result in query) { if (result.name.Equals(category)) { return(Json(new { success = false })); } } } AssignmentCategory ac = new AssignmentCategory(); ac.Name = category; ac.Weight = catweight; ac.ClassId = classId; db.AssignmentCategory.Add(ac); db.SaveChanges(); return(Json(new { success = true })); }
/// <summary> /// Deletes all assignment categories attached to classid. /// </summary> /// <param name="classid">ID of class to delete assignment categories of</param> public async Task DeleteClassAssignmentCategories(int classid) { var assignmentCategoriesQuery = (from assignmentcategories in _context.AssignmentCategories select new { assignmentcategories.CategoryId, assignmentcategories.ClassId }).Where(ac => ac.ClassId == classid); List <AssignmentCategory> categoriesToDelete = new List <AssignmentCategory>(); foreach (var ac in assignmentCategoriesQuery) { AssignmentCategory category = new AssignmentCategory() { CategoryId = ac.CategoryId, ClassId = ac.ClassId }; categoriesToDelete.Add(category); } _context.AssignmentCategories.RemoveRange(categoriesToDelete); await _context.SaveChangesAsync(); }
private static Assignment AddAssignment(AssignmentsDbContext db, AssignmentCategory category, string title, string body, int dayTillDue) { var assignment = new Assignment() { Id = (title + "-" + category.OwnerId).ToUpper().Replace(' ', '-'), OwnerId = category.OwnerId, OwnerLevel = OwnerLevel.User, CreatedUTC = DateTime.UtcNow, Status = AssignmentStatus.Draft, Title = title, AssignmentBody = body, AllowComments = false, DueUTC = DateTime.Now.AddDays(dayTillDue), TimeZoneId = TimeZoneHelper.DefaultTimeZoneId, SendNotification = false, CreatedBy = category.OwnerId }; if (!db.Assignments.Any(a => a.Id == assignment.Id)) { db.Assignments.Add(assignment); } if (!db.AssignmentCategoryLinks.Any(l => l.AssignmentId == assignment.Id && l.CategoryId == category.Id)) { db.AssignmentCategoryLinks.Add(new AssignmentCategoryLink() { AssignmentId = assignment.Id, CategoryId = category.Id }); } return(assignment); }
/// <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) { var classID = getClassID(subject, num, season, year); // To see if the category of the given class already exists var query1 = from ac in db.AssignmentCategory where ac.ClassId == classID && ac.Name == category select ac; if (query1.Count() > 0) { return(Json(new { success = false })); } AssignmentCategory a = new AssignmentCategory(); a.ClassId = classID; a.Weight = catweight; a.Name = category; db.AssignmentCategory.Add(a); db.SaveChanges(); return(Json(new { success = true })); }
public async Task DeleteAssignmentCategory(int categoryid) { AssignmentCategory category = _context.AssignmentCategories.FirstOrDefault(ac => ac.CategoryId == categoryid); _context.AssignmentCategories.Remove(category); await _context.SaveChangesAsync(); }
/// <summary> /// Gets list of AssignmentCategories attached to classid /// </summary> /// <param name="classid">ID of class to get AssignmentCategories for</param> /// <returns>List(AssignmentCategory) of categories attached to classid</returns> public List <AssignmentCategory> GetAssignmentCategories(int classid) { List <AssignmentCategory> assignmentCategories = new List <AssignmentCategory>(); var assignmentCategoriesQuery = (from assignmentcategories in _context.AssignmentCategories select new { assignmentcategories.CategoryId, assignmentcategories.ClassId, assignmentcategories.CategoryName, assignmentcategories.CategoryWeight }).Where(a => a.ClassId == classid).ToList(); foreach (var category in assignmentCategoriesQuery) { AssignmentCategory newCategory = new AssignmentCategory { CategoryId = category.CategoryId, ClassId = category.ClassId, CategoryName = category.CategoryName, CategoryWeight = category.CategoryWeight }; assignmentCategories.Add(newCategory); } return(assignmentCategories); }
/// <summary> /// Deletes all assignment categories of classes attached to teacherid /// </summary> /// <param name="teacherid">ID of teacher to delete assignment categories from classes</param> public async Task DeleteClassesAssignmentCategories(int teacherid) { var categoriesQuery = (from categories in _context.AssignmentCategories join classes in _context.Classes on categories.ClassId equals classes.ClassId select new { categories.CategoryId, classes.ClassId, classes.TeacherId }).Where(c => c.TeacherId == teacherid); List <AssignmentCategory> categoriesToDelete = new List <AssignmentCategory>(); foreach (var c in categoriesQuery) { AssignmentCategory ac = new AssignmentCategory() { CategoryId = c.CategoryId, ClassId = c.ClassId, }; categoriesToDelete.Add(ac); } _context.AssignmentCategories.AttachRange(categoriesToDelete); _context.AssignmentCategories.RemoveRange(categoriesToDelete); await _context.SaveChangesAsync(); }
public async Task InsertCategoryAsync(AssignmentCategory category) { if (string.IsNullOrEmpty(category.Id)) { category.Id = Guid.NewGuid().ToString("N"); } _assignmentsDb.AssignmentCategories.Add(category); await _assignmentsDb.SaveChangesAsync(); }
/// <summary> /// Adds assignment category to class. /// </summary> /// <param name="classid">ID of class to add category to</param> /// <param name="categoryname">Name of new category</param> /// <param name="categoryweight">Weight percent of new category</param> /// <returns></returns> public async Task AddAssignmentCategory(int classid, string categoryname, int categoryweight) { AssignmentCategory categoryToAdd = new AssignmentCategory() { ClassId = classid, CategoryName = categoryname, CategoryWeight = categoryweight }; _context.AssignmentCategories.Add(categoryToAdd); await _context.SaveChangesAsync(); }
public async Task UpdateCategoryAsync(AssignmentCategory category) { var oldCategory = await _assignmentsDb.AssignmentCategories.Where(x => x.Id == category.Id).FirstOrDefaultAsync(); if (oldCategory == null) { throw new Exception($"Unable to find Assignment Category (Id: {category.Id})."); } oldCategory.OwnerLevel = category.OwnerLevel; oldCategory.OwnerId = category.OwnerId ?? oldCategory.OwnerId; oldCategory.Title = category.Title; await _assignmentsDb.SaveChangesAsync(); }
/// <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) { // Query table to check if assignment category with the same name already exists in the same class. var query = from cl in db.Class join cr in db.Course on cl.CourseId equals cr.CourseId join ac in db.AssignmentCategory on cl.ClassId equals ac.ClassId join sm in db.Semester on cl.SemesterId equals sm.SemesterId where cr.DeptAbbr == subject && cr.Number == num && sm.Season == season && sm.Year == year && ac.Name == category select ac.Name; // If the above query found any results, then there already exists an AssignmentCategory // with the same name in the same class. if (query.ToArray().Length > 0) { return(Json(new { success = false })); } // Grab the class ID for the class specified by the input parameters. var queryForClassID = from cl in db.Class join cr in db.Course on cl.CourseId equals cr.CourseId join sm in db.Semester on cl.SemesterId equals sm.SemesterId where cr.DeptAbbr == subject && cr.Number == num && sm.Season == season && sm.Year == year select cl.ClassId; uint classID = queryForClassID.First(); // Create the new Assignment category with the specified name/weight and the ClassID from above. AssignmentCategory newAssnCat = new AssignmentCategory(); newAssnCat.Name = category; newAssnCat.Weight = (uint)catweight; newAssnCat.ClassId = classID; // only return true if the addition saves successfully. try { db.AssignmentCategory.Add(newAssnCat); db.SaveChanges(); return(Json(new { success = true })); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); } // return false if the try failed. Changes were not saved successfully to db. 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 })); } } }
private static AssignmentCategory AddAssignmentCategory(AssignmentsDbContext db, string title, string userId) { var ac = new AssignmentCategory() { Id = (title + "-" + userId).ToUpper().Replace(' ', '-'), OwnerLevel = OwnerLevel.User, OwnerId = userId, Title = title }; if (!db.AssignmentCategories.Any(c => c.Id == ac.Id)) { db.AssignmentCategories.Add(ac); } return(ac); }
/// <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) { // grab the class id var query = from d in db.Department where d.Abbrv == subject join co in db.Course on d.DepartmentId equals co.DepartmentId where co.Number == num join cl in db.Class on co.CourseId equals cl.CourseId where cl.Season == season && cl.Year == year select new { cl.ClassId }; uint theID = 0; // parse the id foreach (var item in query) { theID = item.ClassId; break; } // make sure the category doesn't already exist if (db.AssignmentCategory.Any(checkCat => checkCat.Name == category && checkCat.ClassId == theID)) { return(Json(new { success = false })); } // make our new category and save it AssignmentCategory newCat = new AssignmentCategory(); newCat.Name = category; newCat.Weight = (sbyte)catweight; newCat.ClassId = theID; db.AssignmentCategory.Add(newCat); db.SaveChanges(); return(Json(new { success = true })); }
public async Task<ActionResult> SaveAssignmentCategory(AssignmentCategoryViewModel model) { //ModelState.AddModelError("EmailSubject", "Test Error."); if (ModelState.IsValid) { var category = new AssignmentCategory() { Id = model.Id, OwnerLevel = model.OwnerLevel, OwnerId = model.OwnerId, Title = model.Title }; if (string.IsNullOrEmpty(model.Id)) await _assignmentManager.InsertCategoryAsync(category); else await _assignmentManager.UpdateCategoryAsync(category); return Ok(category); } return BadRequest(ModelState); }
/// <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) { var query1 = from p in db.Department join g in db.Courses on p.DId equals g.DId join h in db.Classes on g.CId equals h.CId join w in db.AssignmentCategory on h.ClassId equals w.ClassId where h.SemesterSeason.Equals(season) && h.SemesterYear == year && g.Number.Equals(num) && p.Subject.Equals(subject) && w.Name.Equals(category) select h.ClassId; if (query1.Count() == 1) { return(Json(new { success = false })); } var query = from p in db.Department join g in db.Courses on p.DId equals g.DId join h in db.Classes on g.CId equals h.CId where h.SemesterSeason.Equals(season) && h.SemesterYear == year && g.Number.Equals(num) && p.Subject.Equals(subject) select h.ClassId; if (query.Count() == 1) { AssignmentCategory assignCat = new AssignmentCategory(); assignCat.ClassId = query.ToArray()[0]; assignCat.Name = category; assignCat.Weight = (uint?)catweight; db.AssignmentCategory.Add(assignCat); int success = db.SaveChanges(); return(Json(new { success = true })); } return(Json(new { success = false })); }