/// <summary> /// function that checks the logic of a test that the user requested to add or update to the database. /// /// </summary> /// <param name="test">the test the user added or updated</param> /// <param name="update">if the function was called for updating, this will be true</param> public void AddTest(Test test, bool update = false) { string ErrorString = ""; if (!update) { test.TestNumber = dal.GetTestCode().ToString().PadLeft(8, '0'); } var testTrainee = dal.GetTrainees().FirstOrDefault(T => T.ID == test.TraineeID); var testTester = dal.GetTesters().FirstOrDefault(T => T.ID == test.TesterID); if (testTrainee == null) { throw new InvalidOperationException("The trainee does not exist" + "\n"); } if (testTester == null) { throw new InvalidOperationException("The tester does not exist" + "\n"); } // Thread thread = new Thread(() =>TestersInRange(testTester, test.BeginLocation)); //thread.Start(); Test test2 = dal.GetTests().FirstOrDefault(t => t.TraineeID == test.TraineeID && t.TestDateTime.AddDays(Configuration.TimeBetweenTests) > test.TestDateTime); //var otherTests = TestGroupsAccordingToTrainee(false).FirstOrDefault(item => item.Key.ID == test.TraineeID); if (test2 != null && update == false) { ErrorString += string.Format("The trainee must wait {0} days before he can appoint the test", Configuration.TimeBetweenTests) + "\n"; } if (testTrainee.CurrentCarType != testTester.testingCarType) { ErrorString += "The tester does not teach on the vehicle type that the trainee learned with" + "\n"; } else { test.TestingCarType = testTrainee.CurrentCarType; } if (testTrainee.carTypeStats[testTrainee.CurrentCarType].numOfLessons < 20) { ErrorString += "The trainee is not yet ready for a test" + "\n"; } if (!update && testTrainee.carTypeStats[testTrainee.CurrentCarType].passed) { ErrorString += "The student has already passed a test on that vehicle" + "\n"; } if (Functions.IsHoliday(test.TestDateTime, out Holiday? holiday)) { ErrorString += "The chosen date falls out on " + Functions.InsertSpacesBeforeUpper(holiday.ToString()) + "\n"; } DayOfWeek day = test.TestDateTime.DayOfWeek; int time = test.TestDateTime.Hour; var tests_by_tester = from test1 in dal.GetTests() where test1.TesterID == testTester.ID select test1; ///if the tester is unavailable if (!testTester.schedule[day, time] || tests_by_tester.Any(T => T.TestDateTime.Date == test.TestDateTime.Date && T.TestDateTime.Hour == test.TestDateTime.Hour && T.TestNumber != test.TestNumber)) { ErrorString += "The tester is unavailable" + "\n"; } var tests_by_tester_same_week = from test1 in tests_by_tester where Functions.DatesAreInTheSameWeek(test.TestDateTime, test1.TestDateTime) select test1; if (tests_by_tester_same_week.Count() >= testTester.MaxWeeklyTests) { ErrorString += "The tester has signed up for too many tests"; } //if(TestersInRange(test.BeginLocation).FirstOrDefault(T => T.ID == test.TesterID) == null) //if (TestersInRange(testTester, test.BeginLocation) == false) // throw new InvalidOperationException("The location is too far for the tester"); if (ErrorString.Length != 0) { throw new InvalidOperationException(ErrorString); } if (test.TestDateTime < DateTime.Now) { if (test.testProperties.passed) { testTrainee.carTypeStats[testTrainee.CurrentCarType].passed = true; string message = string.Format("Congradulations! you've passed the test on {0}, at {1}!", test.TestDateTime.ToShortDateString(), test.TestDateTime.ToShortTimeString()); testTrainee.AddNotification(message, MessageIcon.Information); Functions.SendEmail(testTrainee, "You Passed the Test!!!", message); } else { string message = string.Format("We're sorry, but you failed the test on {0}, at {1}", test.TestDateTime.ToShortDateString(), test.TestDateTime.ToShortTimeString()); testTrainee.AddNotification(message, MessageIcon.Error); Functions.SendEmail(testTrainee, "You failed :(", message); } } if (!update) { testTrainee.carTypeStats[testTrainee.CurrentCarType].numOfTest++; string testerMessage = string.Format("A test was appointed to you with the trainee {0} at {1}, {2}. See test for details.", testTrainee.Name, test.TestDateTime.ToShortDateString(), test.TestDateTime.ToShortTimeString()); string traineeMessage = string.Format("A test was appointed to you with the tester {0} at {1}, {2}. See test for details.", testTester.Name, test.TestDateTime.ToShortDateString(), test.TestDateTime.ToShortTimeString()); testTrainee.AddNotification(traineeMessage, MessageIcon.Warning); testTester.AddNotification(testerMessage, MessageIcon.Warning); if (test.TestDateTime > DateTime.Now) { Functions.SendEmail(testTrainee, "New Test Appointment", traineeMessage); } Functions.SendEmail(testTester, "New Test Appointment", testerMessage); } else { string message = string.Format("Your test on {0} at {1} has been updated. See for details.", test.TestDateTime.ToShortDateString(), test.TestDateTime.ToShortTimeString()); if (!testTrainee.Equals(GlobalSettings.User)) { testTrainee.AddNotification(message, MessageIcon.Information); if (test.TestDateTime > DateTime.Now) { Functions.SendEmail(testTrainee, "Test Update", message); } } if (!testTester.Equals(GlobalSettings.User)) { testTester.AddNotification(message, MessageIcon.Information); if (test.TestDateTime > DateTime.Now) { Functions.SendEmail(testTester, "Test Update", message); } } } UpdateTrainee(testTrainee); UpdateTester(testTester); if (!update) { dal.AddTestCode(); } else { RemoveTest(test.TestNumber); } test.TestingCarType = testTester.testingCarType; dal.AddTest(test); }