/// <summary> /// Update command for when a student enrolls in a course based on the course ID and if the user is logged in under the correct role of student. /// /// If there are validation exceptions, add them to validation exception list. /// </summary> /// <param name="courseID"></param> /// <param name="userIndentity"></param> public void RegisterCourse(int courseID) { using (LearningManagementContext context = new LearningManagementContext()) { Course enrollCourse = context.Courses.Where(x => x.ID == courseID).SingleOrDefault(); if (User.Identity.Name == null) { exception.ValidationExceptions.Add(new Exception("Please sign-in to enroll")); } else { User userRoll = context.Users.Where(x => x.Role == 3 && x.ID == int.Parse(User.Identity.Name)).SingleOrDefault(); if (userRoll == null) { exception.ValidationExceptions.Add(new Exception("You must be a student to enroll")); } else { if (context.Students.Any(x => x.CourseID == courseID && x.UserID == int.Parse(User.Identity.Name))) { exception.ValidationExceptions.Add(new Exception("You have already enrolled for this course.")); } else { if (enrollCourse.CurrentCapacity >= enrollCourse.MaxCapacity) { exception.ValidationExceptions.Add(new Exception("Sorry, the course you are registering for is already full")); } else { if (enrollCourse.EndDate <= DateTime.Today) { exception.ValidationExceptions.Add(new Exception("Sorry, that course has already ended.")); } } } } } if (exception.ValidationExceptions.Count > 0) { throw exception; } context.Students.Add(new Student() { UserID = int.Parse(User.Identity.Name), CourseID = courseID, }); enrollCourse.CurrentCapacity += 1; context.SaveChanges(); } }
/// <summary> /// Function to enter the Answer of an assignment in the database by Student /// </summary> /// <param name="id"></param> /// <param name="answer"></param> /// <param name="courseID"></param> public void SubmitAssignment(string id, string answer, string courseID) { ValidationException exception = new ValidationException(); bool flag = false; int parsedId; // Trim the values id = id?.Trim(); answer = answer?.Trim(); courseID = courseID?.Trim(); // Validation for Assignment ID if (id == null || courseID == null) { exception.ValidationExceptions.Add(new Exception("ID not found, Go back to details page and try again")); flag = true; } else { if (!int.TryParse(id, out parsedId)) { exception.ValidationExceptions.Add(new Exception("Invalid Course ID : Go back to main instructor dsahboard and select course again")); flag = true; } } // Validation for answer if (answer == null) { exception.ValidationExceptions.Add(new Exception("Invalid Value : Answer Required")); flag = true; } else { if (answer.Length > 2000) { exception.ValidationExceptions.Add(new Exception("Invalid value : cannot exceed character count of 2000, please rephrase")); flag = true; } } if (exception.ValidationExceptions.Count > 0) { throw exception; } if (flag == false) { using (LearningManagementContext context = new LearningManagementContext()) { // Add Values in assignment Table if all validations are passed context.Submissions.Add(new Submit() { AssignmentID = int.Parse(id), StudentID = GetStudentId(courseID, User.Identity.Name), DateSubmitted = DateTime.Now, Answer = answer }); context.SaveChanges(); } } }
/// <summary> /// Function to update the Score and remarks in submit, table these fields are added by instructor /// </summary> /// <param name="submitId"></param> /// <param name="remarks"></param> /// <param name="scoreObtained"></param> /// <param name="totalScore"></param> public void SubmitAssignmentScoreAndRemarks(string submitId, string remarks, string scoreObtained, string totalScore) { ValidationException exception = new ValidationException(); Submit updateSubmission = null; submitId = submitId?.Trim(); remarks = remarks?.Trim(); scoreObtained = scoreObtained?.Trim(); totalScore = totalScore?.Trim(); bool flag = false; int parsedId; int parsedScoreObtained; int parsedTotalScore; // Validation for submitID if (!int.TryParse(submitId, out parsedId)) { exception.ValidationExceptions.Add(new Exception("Invalid submission ID : go back to course list and try again")); flag = true; } // Validation for Total Score if (!int.TryParse(totalScore, out parsedTotalScore)) { exception.ValidationExceptions.Add(new Exception("Invalid value: total score required")); flag = true; } // Validation for remarks if (remarks != null) { if (remarks.Length > 500) { exception.ValidationExceptions.Add(new Exception("Invalid value : cannot exceed character count of 500, please rephrase")); flag = true; } } // Validation for Score obtained if (!int.TryParse(scoreObtained, out parsedScoreObtained)) { exception.ValidationExceptions.Add(new Exception("Invalud value : numeric Value required for score obtained")); flag = true; } else { if (!((parsedScoreObtained > -1) && (parsedScoreObtained < parsedTotalScore + 1))) { exception.ValidationExceptions.Add(new Exception($"Invalid value : enter total score between 0 and {parsedTotalScore}")); flag = true; } } if (flag == false) { using (LearningManagementContext context = new LearningManagementContext()) { updateSubmission = context.Submissions.Where(x => x.ID == int.Parse(submitId)).SingleOrDefault(); updateSubmission.ScoreObtained = int.Parse(scoreObtained); updateSubmission.Remarks = remarks; context.SaveChanges(); } } if (exception.ValidationExceptions.Count > 0) { throw exception; } }
/// <summary> /// Function to insert assignment values by instructor into assignment table /// Validation # 1 : Due Date for assignment Cannot be before Course start date /// Validation # 2 : Due date for assignment cannot be set before today /// Validation # 3 : Due date cannot be after Course end date /// </summary> /// <param name="question"></param> /// <param name="dueDate"></param> /// <param name="totalScore"></param> /// <param name="id"></param> public void CreateNewAssignment(string question, string dueDate, string totalScore, string id) { ValidationException exception = new ValidationException(); // Trim the values question = question?.Trim(); dueDate = dueDate?.Trim(); totalScore = totalScore?.Trim(); id = id?.Trim(); bool flag = false; int parsedId; int parsedTotalScore; // Validation for courseID if (id == null) { exception.ValidationExceptions.Add(new Exception("Invalid ID : Go back to details page and try again")); flag = true; } else { if (!int.TryParse(id, out parsedId)) { exception.ValidationExceptions.Add(new Exception("Invalid Course ID : Go back to main Instructor Dsahboard and select course again")); flag = true; } } // Validation for question if (question == null) { exception.ValidationExceptions.Add(new Exception("Invalid Value : Question Required")); flag = true; } else { if (question.Length > 500) { exception.ValidationExceptions.Add(new Exception("Invalid character count : cannot exceed 500 characters, please rephrase")); flag = true; } } // Validation for dueDate if (dueDate == null) { exception.ValidationExceptions.Add(new Exception("Invalid Date : Due Date Required")); flag = true; } else { if (DateTime.Parse(dueDate) < DateTime.Now) { exception.ValidationExceptions.Add(new Exception("Invalid Date : Due Date Can not be set prior to today")); flag = true; } } // Validation for TotalScore if (totalScore == null) { exception.ValidationExceptions.Add(new Exception("Invalid Score : total score for assignment required")); flag = true; } else { if (!int.TryParse(totalScore, out parsedTotalScore)) { exception.ValidationExceptions.Add(new Exception("Invalid Value : Numeric Value required for total score")); flag = true; } else { if (!((parsedTotalScore > -1) && (parsedTotalScore < 101))) { exception.ValidationExceptions.Add(new Exception("Invalid value : enter total score between 0 and 100")); flag = true; } } } if (flag == false) { using (LearningManagementContext context = new LearningManagementContext()) { Course course = GetCourseDetailsByID(id); if (DateTime.Parse(dueDate) < course.StartDate) { exception.ValidationExceptions.Add(new Exception("Invalid Due date : due date for an assignment cannot be before course start date")); flag = true; } else if (DateTime.Parse(dueDate) > course.EndDate) { exception.ValidationExceptions.Add(new Exception($"Invalid Due date : due date for an assignment cannot be later than course end date {course.EndDate}")); flag = true; } if (flag == false) { // Add Values in assignment Table if all validations are passed context.Assignments.Add(new Assignment() { CourseID = int.Parse(id), Question = question, DueDate = DateTime.Parse(dueDate), TotalScore = int.Parse(totalScore) }); context.SaveChanges(); } } } if (exception.ValidationExceptions.Count > 0) { throw exception; } }
/* ----------------------------------------------- Data ------------------------------------------*/ /// <summary> /// Function To add Validated user credentials in User database /// </summary> /// <param name="firstName"></param> /// <param name="lastName"></param> /// <param name="email"></param> /// <param name="password"></param> /// <param name="role"></param> public void Register(string firstName, string lastName, string email, string password, string role) { ValidationException exception = new ValidationException(); // Trim the values firstName = firstName?.Trim(); lastName = lastName?.Trim(); email = email?.Trim(); password = password?.Trim(); role = role?.Trim(); bool flag = false; // Validation for First Name if (string.IsNullOrWhiteSpace(firstName)) { exception.ValidationExceptions.Add(new Exception("Invalid value : First Name Not Provided")); flag = true; } else if (firstName.Length > 50) { exception.ValidationExceptions.Add(new Exception("Invalid value : First Name Cannot exceed 50 characters")); flag = true; } // Validation for Last Name if (string.IsNullOrWhiteSpace(lastName)) { exception.ValidationExceptions.Add(new Exception("Invalid value : Last Name Not Provided")); flag = true; } else if (lastName.Length > 50) { exception.ValidationExceptions.Add(new Exception("Invalid value : Last Name Cannot exceed 50 characters")); flag = true; } // Validation for Email if (string.IsNullOrWhiteSpace(email)) { exception.ValidationExceptions.Add(new Exception("Invalid value : Email Not Provided")); flag = true; } else if (email.Length > 50) { exception.ValidationExceptions.Add(new Exception("Invalid value : Email Cannot exceed 50 characters")); flag = true; } else if (!Regex.IsMatch(email, @"^[\w-!$*%^\.]+@([\w-]+\.)+[\w-]{2,4}$")) { exception.ValidationExceptions.Add(new Exception("Invalid value : Incorrect Email Address ")); flag = true; } // Validation for Password if (string.IsNullOrWhiteSpace(password)) { exception.ValidationExceptions.Add(new Exception("Invalid value : Password Not Provided")); flag = true; } else if (password.Length > 50) { exception.ValidationExceptions.Add(new Exception(" Invalid value : Password Cannot exceed 50 characters")); flag = true; } if (string.IsNullOrWhiteSpace(role)) { exception.ValidationExceptions.Add(new Exception("Please Select a Role")); flag = true; } if (flag == false) { using (LearningManagementContext context = new LearningManagementContext()) { // Checking for Email duplication if ((context.Users.Where(x => (x.EMail.Trim().ToUpper()) == email.ToUpper()).Count()) > 0) { exception.ValidationExceptions.Add(new Exception("Email already exist, Try again with a new one")); flag = true; } if (flag == false) { // Add Values in user Table if all validations are passed context.Users.Add(new User() { FirstName = firstName, LastName = lastName, EMail = email.ToUpper(), Password = SignInController.HashAndSaltPassowrd(password, email.ToUpper()), Role = int.Parse(role), JoinDate = DateTime.Now }); context.SaveChanges(); } } } if (exception.ValidationExceptions.Count > 0) { throw exception; } }