Esempio n. 1
0
 //Data manipulations on TESTS
 /// <summary>
 /// Adds a test to the tests list in the DataSource
 /// </summary>
 /// <param name="test">Test to be added</param>
 /// <returns></returns>
 public string AddTest(Test test)
 {
     if (TestPropertiesValidation(test) == true)
     {
         m_xamlImp.AddTest(test);
         return(null);
     }
     else
     {
         return("Couldn't add the test because of 1 or more errors");
     }
 }
Esempio n. 2
0
        public void AddTest(Test test)
        {
            // each Test contains a boolean field that sign if the test is a test
            // that was offered to the student by the system, if it is indeed -
            // simply add it to the DS , because we already checked the logic terms
            // for it before sending
            if (test.IsReturning)
            {
                ImmediateAddTest(test);
                return;
            }

            Trainee trainee = dal.FindTrainee(test.TraineeId); //finding the trainee for this test

            if (test.Date < DateTime.Now)
            {
                throw new MyExceptions("לא ניתן לקבוע טסט בזמן עבר!");
            }

            if (trainee == null)
            {
                throw new MyExceptions("התלמיד " + test.TraineeId + " לא נמצא");
            }

            // checking in all the tests this trainee has done , to check if already passed
            // a test on the same car type
            foreach (Test temp in dal.GetTestsForSpecTrainee(trainee.Id))
            {
                if (temp.CarType == test.CarType && temp.Passed)
                {
                    throw new MyExceptions("התלמיד:" + trainee.Id + " כבר עבר טסט על סוג רכב: " + trainee.CarType + "!");
                }
                if (temp.Date == test.Date)
                {
                    throw new MyExceptions("כבר נקבע עבורך טסט באותה השעה והיום בדיוק");
                }
            }
            //// הוספה ביום המבחן - אם יש בעיה להוריד מכאן
            //if(DateTime.Now.AddMonths(3) < test.Date.AddMonths(3))
            //    throw new MyExceptions("לא ניתן לקבוע מראש יותר מטסט אחד בשלושה חודשים ");
            //// לבדוק אם עובד
            // if trainee didn't reach min required lessons - throwing
            if (trainee.NumLessons < Configuration.MinLessons || trainee.NumLessons == null)
            {
                throw new MyExceptions("התלמיד:" + trainee.FullName + " לא הגיע ל" + Configuration.MinLessons.ToString() + " שיעורים עדיין");
            }

            if (trainee.LastTest != DateTime.MinValue) // check if trainee did a test in last 7 days
            {
                TimeSpan timeSpan = test.Date - trainee.LastTest;
                if (timeSpan.Days < Configuration.MinDaysBetweenTests)
                {
                    throw new MyExceptions("לתלמיד זה כבר נקבע מבחן  " + Configuration.MinDaysBetweenTests.ToString() + " ימים לפני המועד המבוקש");
                }
            }

            // getting testers with same car_type as trainee's
            var TestersWithCarType = dal.GetTestersWithCarType(test.CarType);

            // if there isn't any tester that match with trainee's car type - throw
            // because there won't be created any test at all!!
            if (!TestersWithCarType.Any())
            {
                throw new MyExceptions("No available testers with this car type!");//chek
            }
            IEnumerable <Tester> AvailableTesters;
            DateTime             OriginalTestDate = test.Date; // saving the original date and time, because we might change it, so we should count 3 month from the original time
            bool flag = false;

            do
            {
                // trying to find an available tester
                AvailableTesters = FindAvilableTesters(TestersWithCarType, test.Date).ToList();
                if (AvailableTesters != null && AvailableTesters.Any())
                {
                    // AvailableTesters = AvailableTesters.ToList();
                    try
                    {
                        //foreach( var item in AvailableTesters)
                        //{
                        //    if (item.MaxDistance < MapRequest.MapRequestLoop(item, test.BeginAddressString))
                        //        AvailableTesters.Remove(item);
                        //}
                        AvailableTesters = AvailableTesters.Where(item => item.MaxDistance >= MapRequest.MapRequestLoop(item, test.BeginAddressString));
                    }
                    catch (MyExceptions a)
                    { throw a; }

                    if (AvailableTesters.Any())
                    {
                        test.TesterId = AvailableTesters.First().Id;
                    }
                    else
                    {
                        throw new MyExceptions("לא נמצאו טסטרים בטווח");
                    }

                    if (flag)                    // flag is raised if we change the time for the date
                    {
                        test.IsReturning = true; // so we mark the test as "returning" (from UI), and offering it to user
                        throw new MyExceptions("לא ניתן לקבוע טסט בזמן המבוקש, אך המערכת מצאה טסט זמין ב: " + test.Date + " האם אתה מעוניין?", test);
                    }
                    else // if it is in the time user asked - simply sending it to the dal
                    {
                        dal.AddTest(test);
                        throw new MyExceptions("נרשם טסט עבור: " + test.TraineeId + " ב: " + test.Date);
                    }
                }
                flag      = true;                                                        // if a test wasn't signed until first iteration here - it means we are changing the original time he asked
                test.Date = test.Date.AddHours(1);                                       // now adding an hour to the time of test, and re-checking for avalability
                if (test.Date.Hour == Configuration.WorkHours + Configuration.StartHour) // if the time is beyond work schedule of the office-
                {
                    test.Date = test.Date.AddDays(1);                                    // adding another day
                    test.Date = test.Date.AddHours(-Configuration.WorkHours);            // decreasing the hour by the number of working hours
                    if (test.Date.DayOfWeek == DayOfWeek.Friday)                         // if friday - jumping forward to sunday
                    {
                        test.Date = test.Date.AddDays(2);
                    }
                }

                if (test.Date.Month > OriginalTestDate.AddMonths(3).Month) // if we already checked 3 months forward - throwing
                {
                    throw new MyExceptions("אין טסטים זמינים בשלושת החודשים הקרובים!");
                }
            } while (!AvailableTesters.Any()); // returning as long as there wasn't found an available tester
        }
        /// <summary>
        ///     Add a Test
        /// </summary>
        /// <param name="newTest">The Test to add</param>
        public void AddTest(Test newTest)
        {
            //check if the test is ok
            var testMissingDate = newTest.TestTime == DateTime.MinValue;

            var testerExist = AllTesters.Any(tester => tester.Id == newTest.TesterId);

            var traineeExist = AllTrainees.Any(trainee => trainee.Id == newTest.TraineeId);

            var twoTestesTooClose = AllTests.Any(test =>
                                                 test.TraineeId == newTest.TraineeId && test.LicenseType == newTest.LicenseType &&
                                                 Math.Abs((newTest.TestTime - test.TestTime).TotalDays) < Configuration.MinTimeBetweenTests);

            // the trainee didn't the minimum number of lessens before test
            var lessThenMinLessons = AllTrainees.Any(trainee =>
                                                     trainee.Id == newTest.TraineeId && trainee.LicenseTypeLearning.Any(l =>
                                                                                                                        l.License == newTest.LicenseType && l.NumberOfLessons < Configuration.MinLessons));

            var traineeIsLearningLicense = AllTrainees.Any(trainee =>
                                                           trainee.Id == newTest.TraineeId &&
                                                           trainee.LicenseTypeLearning.Any(l => l.License == newTest.LicenseType));

            var testerIsTeachingLicense = AllTesters.Any(tester =>
                                                         tester.Id == newTest.TesterId && tester.LicenseTypeTeaching.Any(l => l == newTest.LicenseType));

            var tooManyTestInWeek =
                AllTests.Count(test =>
                               test.TesterId == newTest.TesterId && Tools.DatesAreInTheSameWeek(newTest.TestTime, test.TestTime)) +
                1 >
                AllTesters.First(tester => tester.Id == newTest.TesterId).MaxWeekExams;

            var traineeHasTestInSameTime = AllTests.Any(test =>
                                                        test.TraineeId == newTest.TraineeId && newTest.TestTime == test.TestTime);
            var testerHasTestInSameTime =
                AllTests.Any(test => test.TesterId == newTest.TesterId && newTest.TestTime == test.TestTime);

            var traineeHasLicenseAlready = AllTests.Any(y =>
                                                        y.TraineeId == newTest.TraineeId && y.Passed == true && y.LicenseType == newTest.LicenseType);

            var traineePassedTestAlready = AllTests.Any(test =>
                                                        test.TraineeId == newTest.TraineeId && test.LicenseType == newTest.LicenseType && test.Passed == true);

            if (testMissingDate)
            {
                throw new Exception("Enter a valid date");
            }
            if (tooManyTestInWeek)
            {
                throw new Exception("To many tests for tester");
            }
            if (!traineeExist)
            {
                throw new Exception("This trainee doesn't exist");
            }
            if (!testerExist)
            {
                throw new Exception("This tester doesn't exist");
            }
            if (twoTestesTooClose)
            {
                throw new Exception(
                          "The trainee has a already a test in " + Configuration.MinTimeBetweenTests + " days");
            }
            if (lessThenMinLessons)
            {
                throw new Exception("The trainee learned less then " + Configuration.MinLessons +
                                    " lessons which is the minimum");
            }
            if (!traineeIsLearningLicense)
            {
                throw new Exception("The trainee is not learning for this license");
            }
            if (!testerIsTeachingLicense)
            {
                throw new Exception("Tester is not qualified for this license type");
            }
            if (traineeHasTestInSameTime)
            {
                throw new Exception("The trainee has already another test in the same time");
            }
            if (testerHasTestInSameTime)
            {
                throw new Exception("The tester has already another test in the same time");
            }
            if (traineeHasLicenseAlready)
            {
                throw new Exception("The trainee has already a license with same type");
            }
            if (traineePassedTestAlready)
            {
                throw new Exception("The trainee already passed the test");
            }


            //add the test
            _dalImp.AddTest(newTest);
        }