public IActionResult Index(int id) { var taskData = _context.TaskVariants .Where(taskVariant => taskVariant.Id == id) .Select(taskVariant => new { CourseId = taskVariant.TaskGroup.CourseId, CourseName = taskVariant.TaskGroup.Course.Name, TaskGroupId = taskVariant.TaskGroupId, TaskGroupName = taskVariant.TaskGroup.Name, TaskVariantId = taskVariant.Id, TaskVariantName = taskVariant.Name }).FirstOrDefault(); try { var assignments = _assignmentLogic.GetAssignmentsForTaskVariant(id); return(View(new IndexViewModel() { CourseId = taskData.CourseId, CourseName = taskData.CourseName, TaskGroupId = taskData.TaskGroupId, TaskGroupName = taskData.TaskGroupName, TaskVariantId = taskData.TaskVariantId, TaskVariantName = taskData.TaskVariantName, Assignments = assignments, IsCourseTeacher = KaCakeUtils.IsCourseTeacher(_context, taskData.CourseId, _userManager.GetUserId(HttpContext.User)) })); } catch (NotFoundException) { return(NotFound()); } }
public TaskGroupViewModel GetTaskGroup(string userId, int taskGroupId) { TaskGroup taskGroup = _context.TaskGroups .Include(tg => tg.Course) .Include(tg => tg.Variants) .FirstOrDefault(tg => tg.Id == taskGroupId); if (taskGroup == null) { throw new NotFoundException(); } return(new TaskGroupViewModel { Id = taskGroupId, CourseId = taskGroup.CourseId, CourseName = taskGroup.Course.Name, Name = taskGroup.Name, Description = taskGroup.Description, IsCourseTeacher = KaCakeUtils.IsCourseTeacher(_context, taskGroup.CourseId, userId), Variants = taskGroup.Variants.Select(variant => new TaskVariantViewModel() { Id = variant.Id, Name = variant.Name, Description = variant.Description }).ToList() }); }
public IActionResult Create(int id, int?taskVariantId) { string userId = _userManager.GetUserId(User); TaskGroup taskGroup = _context.TaskGroups.Find(id); if (!KaCakeUtils.IsCourseTeacher(_context, taskGroup.CourseId, userId)) { return(Challenge()); } TaskVariant editingTaskVariant; if (taskVariantId.HasValue && (editingTaskVariant = _context.TaskVariants.Find(taskVariantId.Value)) != null) { return(View(new TaskVariantViewModel() { TaskGroupId = id, Id = taskVariantId.Value, Name = editingTaskVariant.Name, Description = editingTaskVariant.Description, IsUserTeacher = KaCakeUtils.IsCourseTeacher(_context, editingTaskVariant.TaskGroup.CourseId, userId) })); } return(View(new TaskVariantViewModel { TaskGroupId = id, IsUserTeacher = KaCakeUtils.IsCourseTeacher(_context, _context.TaskGroups.Find(id).CourseId, userId) })); }
public CourseViewModel GetCourse(string callerId, int courseId) { var viewingCourse = _context.Courses .Include(course => course.TaskGroups) .Include(course => course.Teachers) .Include(course => course.Students) .FirstOrDefault(course => course.Id == courseId); if (viewingCourse == null) { throw new NotFoundException(); } return(new CourseViewModel() { Id = viewingCourse.Id, Name = viewingCourse.Name, Description = viewingCourse.Description, TaskGroups = viewingCourse.TaskGroups.Select(taskGroup => new TaskGroupViewModel() { Id = taskGroup.Id, Name = taskGroup.Name }).ToList(), Students = viewingCourse.Students.Select( student => KaCakeUtils.createUserInfoViewModel(_context, student.UserId)) .ToList(), Teachers = viewingCourse.Teachers.Select( teacher => KaCakeUtils.createUserInfoViewModel(_context, teacher.TeacherId)) .ToList(), IsUserATeacher = viewingCourse.Teachers.Any(teacher => teacher.TeacherId == callerId), CanDeleteThisCourse = CanDeleteCourse(callerId, courseId) }); }
public IndexViewModel GetProject(string userId, int submissionId) { var submission = _context.Submissions .Include(sub => sub.Assignment) .Include(sub => sub.Assignment.User) .Include(sub => sub.Assignment.TaskVariant) .Include(sub => sub.Assignment.TaskVariant.TaskGroup) .Where(sub => sub.Id == submissionId && (sub.Assignment.UserId == userId || sub.Assignment.ReviewerId == userId)) .FirstOrDefault(); if (submission == null) { throw new NotFoundException(); } var viewModel = new { RootPath = submission.Path, Model = new IndexViewModel() { UserName = submission.Assignment.User.FullName, TaskGroupName = submission.Assignment.TaskVariant.TaskGroup.Name, TaskVariantName = submission.Assignment.TaskVariant.Name, SubmissionTime = submission.Time, SubmissionId = submission.Id, IsCourseTeacher = KaCakeUtils.IsCourseTeacher(_context, submission.Assignment.TaskVariant.TaskGroup.CourseId, userId) } }; viewModel.Model.Root = GetEntires(new DirectoryInfo(viewModel.RootPath)); return(viewModel.Model); }
public IActionResult AddAssignments(int id) { string userId = _userManager.GetUserId(User); var taskVariant = _context.TaskVariants .Include(tv => tv.TaskGroup) .Include(tv => tv.Assignments) .FirstOrDefault(tv => tv.Id == id); var taskVariantAssignments = _context.Assignments .Include(ass => ass.User) .Join(taskVariant.Assignments, ass => ass.TaskVariantId, tvAss => tvAss.TaskVariantId, (ass, tvAss) => ass); if (taskVariant == null) { return(NotFound()); } if (!KaCakeUtils.IsCourseTeacher(_context, taskVariant.TaskGroup.CourseId, userId)) { return(Challenge()); } var editingVariant = new { taskVariant.Id, taskVariant.Name, UsersToRemove = taskVariantAssignments .Select(assignment => new { assignment.UserId, Name = assignment.User.FullName }).ToList() }; PopulateAddAssignmentData(taskVariant.TaskGroup.CourseId, taskVariant.Id); return(View(new AddAsignmentsViewModel() { TaskVariantId = editingVariant.Id, TaskVariantName = editingVariant.Name, DeadlineUtc = DateTime.Now, })); }
public TaskVariantViewModel CreateTaskVariant(string userId, TaskVariantViewModel taskVariant) { if (!KaCakeUtils.IsCourseTeacher(_context, _context.TaskGroups.Find(taskVariant.TaskGroupId).CourseId, userId)) { throw new IllegalAccessException(); } var entity = _context.TaskVariants.Add(new TaskVariant() { TaskGroupId = taskVariant.TaskGroupId, Name = taskVariant.Name, Description = taskVariant.Description }); _context.SaveChanges(); return(GetTaskVariant(userId, entity.Entity.Id)); }
public TaskGroupViewModel EditTaskGroup(string userId, int taskGroupId, TaskGroupViewModel taskGroup) { TaskGroup editingGroup = _context.TaskGroups.Find(taskGroupId); if (editingGroup == null) { throw new NotFoundException(); } if (!KaCakeUtils.IsCourseTeacher(_context, editingGroup.CourseId, userId)) { throw new IllegalAccessException(); } editingGroup.Name = taskGroup.Name; editingGroup.Description = taskGroup.Description; _context.SaveChanges(); return(GetTaskGroup(userId, editingGroup.Id)); }
public TaskGroupViewModel CreateTaskGroup(string userId, TaskGroupViewModel taskGroup) { if (!KaCakeUtils.IsCourseTeacher(_context, taskGroup.CourseId, userId)) { throw new IllegalAccessException(); } var entry = _context.TaskGroups.Add(new TaskGroup() { CourseId = taskGroup.CourseId, Name = taskGroup.Name, Description = taskGroup.Description }); _context.SaveChanges(); taskGroup.Id = entry.Entity.Id; return(GetTaskGroup(userId, entry.Entity.Id)); }
public TaskVariantViewModel EditTaskVariant(string userId, int taskVariantId, TaskVariantViewModel taskVariant) { var editingTaskVariant = _context.TaskVariants .Include(tv => tv.TaskGroup) .FirstOrDefault(tv => tv.Id == taskVariantId); if (editingTaskVariant == null) { throw new NotFoundException(); } if (!KaCakeUtils.IsCourseTeacher(_context, editingTaskVariant.TaskGroup.CourseId, userId)) { throw new IllegalAccessException(); } editingTaskVariant.Name = taskVariant.Name; editingTaskVariant.Description = taskVariant.Description; _context.SaveChanges(); return(GetTaskVariant(userId, editingTaskVariant.Id)); }
public IList <UserInfoViewModel> GetTeachersCouldBeRemoved(string callerId, int courseId) { var editingCourse = _context.Courses.Include(c => c.Teachers) .FirstOrDefault(c => c.Id == courseId); if (editingCourse == null) { throw new NotFoundException(); } return(editingCourse.Teachers .Where(teacher => KaCakeUtils.isAppointer(editingCourse, callerId, teacher)) .Select(teacher => new UserInfoViewModel { FullName = _context.Users.Find(teacher.TeacherId).FullName, UserName = _context.Users.Find(teacher.TeacherId).UserName, UserId = teacher.TeacherId }) .ToList()); }
public bool AddAssignments(string userId, int taskVariantId, AddAsignmentsViewModel viewModel) { var taskVariant = _context.TaskVariants .Include(tv => tv.TaskGroup) .FirstOrDefault(tv => tv.Id == taskVariantId); if (taskVariant == null) { throw new NotFoundException(); } if (!KaCakeUtils.IsCourseTeacher(_context, taskVariant.TaskGroup.CourseId, userId)) { throw new IllegalAccessException(); } if (viewModel.UsersToRemove != null) { _context.Assignments.RemoveRange( _context.Assignments.Where( assignment => viewModel.UsersToRemove.Contains(assignment.UserId) ) ); } if (viewModel.UsersToAdd != null) { _context.Assignments.AddRange(viewModel.UsersToAdd.Select(user => new Assignment() { TaskVariantId = viewModel.TaskVariantId, DeadlineUtc = viewModel.DeadlineUtc, UserId = user, ReviewerId = viewModel.ReviewerId })); } _context.SaveChanges(); return(true); }
public TaskVariantViewModel GetTaskVariant(string userId, int taskVariantId) { TaskVariant taskVariant = _context.TaskVariants .Include(tv => tv.TaskGroup) .Include(tv => tv.TaskGroup.Course) .Include(tv => tv.Assignments) .FirstOrDefault(tv => tv.Id == taskVariantId); if (taskVariant == null) { throw new NotFoundException(); } return(new TaskVariantViewModel() { Id = taskVariantId, Name = taskVariant.Name, Description = taskVariant.Description, TaskGroupId = taskVariant.TaskGroupId, TaskGroupName = taskVariant.TaskGroup.Name, CourseId = taskVariant.TaskGroup.CourseId, CourseName = taskVariant.TaskGroup.Course.Name, IsAssigned = taskVariant.Assignments.Any(assignment => assignment.UserId == userId), IsNeedingReview = taskVariant.Assignments.Any(assignment => assignment.ReviewerId == userId), AssignmentsCount = taskVariant.Assignments.Count, Assignments = taskVariant.Assignments .Select(assignment => new AssignmentViewModel { TaskVariantId = taskVariant.Id, Status = assignment.Status, Score = assignment.Score, TaskName = taskVariant.Name, UserId = assignment.UserId }).ToList(), IsUserTeacher = KaCakeUtils.IsCourseTeacher(_context, taskVariant.TaskGroup.CourseId, userId) }); }
public CourseViewModel Edit(string callerId, int courseId, CreateViewModel course) { Course editingCourse; if ((editingCourse = _context.Courses .Include(c => c.Teachers) .Include(c => c.Students) .FirstOrDefault(c => c.Id == courseId)) != null) { // The course could be edited only by a teacher of that course if (editingCourse.Teachers.Any(teacher => teacher.TeacherId == callerId)) { editingCourse.Name = course.Name; editingCourse.Description = course.Description; if (course.TeachersToAdd != null) { course.TeachersToAdd.Select( teacherId => new CourseTeacher2 { CourseId = course.Id.GetValueOrDefault(), TeacherId = teacherId, AppointerId = callerId } ).ToList().ForEach(teacher => editingCourse.Teachers.Add(teacher)); } if (course.TeachersToRemove != null) { var teachersToRemove = course.TeachersToRemove .Where(userId => editingCourse.Teachers.Any(teacher => teacher.TeacherId.Equals(userId))) .Select(teacherId => editingCourse.Teachers.First(teacher => teacher.TeacherId.Equals(teacherId))); foreach (var teacherToRemove in teachersToRemove) { // Only appointer can remove teachers appointed by him if (KaCakeUtils.isAppointer(editingCourse, callerId, teacherToRemove)) { editingCourse.Teachers.Remove(teacherToRemove); } else { throw new IllegalAccessException(); } } } if (course.StudentsToAdd != null) { foreach (var student in course.StudentsToAdd) { editingCourse.Students.Add(new CourseEnrollment { CourseId = editingCourse.Id, UserId = student }); } } if (course.StudentsToRemove != null) { var studentsToRemove = course.StudentsToRemove .Where(userId => editingCourse.Students.Any(student => student.UserId == userId)) .Select(studentId => editingCourse.Students.First(student => student.UserId == studentId)); foreach (var student in studentsToRemove) { editingCourse.Students.Remove(student); } } _context.SaveChanges(); return(GetCourse(callerId, courseId)); } else { throw new IllegalAccessException(); } } else { throw new NotFoundException(); } }