/// <summary>
 /// Контруктор для прохождения всех тестов
 /// </summary>
 /// <param name="forTesting">список тестов для прохождения</param>
 public TestingService(string SubjectTitle, List<Question> forTesting)
 {
     if (Collections.IsNullOrEmpty(forTesting))
     {
         throw new Exception("Массив тестов не должен быть null или пустым");
     }
     questions = new List<Question>(forTesting);
     Collections.Shuffle(questions);
     questionCount = questions.Count;
     IDX = 0;
     userResult = new TestResult(userAuth.AuthorizedUser, SubjectTitle);
 }
 /// <summary>
 /// Конструктор для прохождения определенных тестов
 /// </summary>
 /// <param name="forTesting">список всех тестов</param>
 /// <param name="count">количество тестов, которое необходимо пройти</param>
 public TestingService(string SubjectTitle, List<Question> forTesting, int count)
 {
     if (Collections.IsNullOrEmpty(forTesting))
     {
         throw new Exception("Массив тестов не должен быть null или пустым");
     }
     if (count >= forTesting.Count)
     {
         throw new Exception("Количество тестов для прохождение не должно превышать размер массива тестов");
     }
     questions = new List<Question>(forTesting);
     questionCount = count;
     IDX = 0;
     Collections.Shuffle(questions);
     userResult = new TestResult(userAuth.AuthorizedUser, SubjectTitle);
 }
 public void Delete(TestResult testResult)
 {
     using (var conn = sqlConnection.Connection)
     {
         conn.Open();
         if (testResult.Questions != null)
         {
             foreach (var question in testResult.Questions)
             {
                 if (question.UserAnswers != null)
                 {
                     foreach (var answer in question.UserAnswers)
                     {
                         answerDao.Delete(conn, answer.ID);
                     }
                 }
                 questionDao.Delete(conn, question.ID);
             }
         }
         testResultDao.Delete(conn, testResult.ID);
     }
 }
 public void SaveOrUpdate(TestResult testResult)
 {
     using (var conn = sqlConnection.Connection)
     {
         conn.Open();
         testResultDao.SaveOrUpdate(conn, testResult);
         if (testResult.Questions != null)
         {
             foreach (var question in testResult.Questions)
             {
                 questionDao.SaveOrUpdate(conn, question);
                 if (question.UserAnswers != null)
                 {
                     foreach (var answer in question.UserAnswers)
                     {
                         answerDao.SaveOrUpdate(conn, answer);
                     }
                 }
             }
         }
     }
 }
 public void SaveOrUpdate(SQLiteConnection conn, TestResult testResult)
 {
     if (testResult.ID == 0)
     {
         int ID = indexService.NextID(TABLE_NAME);
         using (SQLiteCommand command = new SQLiteCommand("insert into USER_TEST_RESULT (ID, SubjectTitle, USER_FIRST_NAME, USER_LAST_NAME, USER_GROUP, StartTime, EndTime, RightAnswerCount, TOTAL_QUESTION) values (@ID, @SubjectTitle, @USER_FIRST_NAME, @USER_LAST_NAME, @USER_GROUP, @StartTime, @EndTime, @RightAnswerCount, @TOTAL_QUESTION)", conn))
         {
             command.Parameters.AddWithValue("@ID", ID);
             command.Parameters.AddWithValue("@USER_FIRST_NAME", testResult.UserName.FirstName);
             command.Parameters.AddWithValue("@USER_LAST_NAME", testResult.UserName.LastName);
             command.Parameters.AddWithValue("@USER_GROUP", testResult.UserName.Group);
             command.Parameters.AddWithValue("@StartTime", DateTimeSQLite(testResult.StartTime));
             command.Parameters.AddWithValue("@EndTime", DateTimeSQLite(testResult.EndTime));
             command.Parameters.AddWithValue("@RightAnswerCount", testResult.RightAnswerCount);
             command.Parameters.AddWithValue("@TOTAL_QUESTION", testResult.Questions.Count);
             command.Parameters.AddWithValue("@SubjectTitle", testResult.SubjectTitle);
             command.ExecuteNonQuery();
             testResult.ID = ID;
         }
     }
     else
     {
         using (SQLiteCommand command = new SQLiteCommand("UPDATE USER_TEST_RESULT SET USER_FIRST_NAME = @USER_FIRST_NAME, USER_LAST_NAME = @USER_LAST_NAME, USER_GROUP = @USER_GROUP, StartTime = @StartTime, EndTime = @EndTime, RightAnswerCount = RightAnswerCount, TOTAL_QUESTION = @TOTAL_QUESTION, SubjectTitle = @SubjectTitle WHERE ID = @ID", conn))
         {
             command.Parameters.AddWithValue("@ID", testResult.ID);
             command.Parameters.AddWithValue("@USER_FIRST_NAME", testResult.UserName.FirstName);
             command.Parameters.AddWithValue("@USER_LAST_NAME", testResult.UserName.LastName);
             command.Parameters.AddWithValue("@USER_GROUP", testResult.UserName.Group);
             command.Parameters.AddWithValue("@StartTime", DateTimeSQLite(testResult.StartTime));
             command.Parameters.AddWithValue("@EndTime", DateTimeSQLite(testResult.EndTime));
             command.Parameters.AddWithValue("@RightAnswerCount", testResult.RightAnswerCount);
             command.Parameters.AddWithValue("@TOTAL_QUESTION", testResult.Questions.Count);
             command.Parameters.AddWithValue("@SubjectTitle", testResult.SubjectTitle);
             command.ExecuteNonQuery();
         }
     }
 }
 private void FindAllQuestionBySubject(SQLiteConnection conn, TestResult testResult)
 {
     testResult.Questions = questionDao.FindAllByResultID(conn, testResult.ID);
     foreach (var question in testResult.Questions)
     {
         question.testResult = testResult;
         question.UserAnswers = answerDao.FindAllByQuestionID(conn, question.ID);
         foreach (var answer in question.UserAnswers)
         {
             answer.Question = question;
         }
     }
 }
        private TestResult ToObject(SQLiteDataReader reader)
        {
            TestResult testResult = new TestResult();
            testResult.ID = Convert.ToInt32(reader["ID"]);
            testResult.UserName = new User();
            testResult.UserName.FirstName = Convert.ToString(reader["USER_FIRST_NAME"]);
            testResult.UserName.LastName = Convert.ToString(reader["USER_LAST_NAME"]);
            testResult.UserName.Group = Convert.ToString(reader["USER_GROUP"]);

            testResult.StartTime = Convert.ToDateTime(reader["StartTime"]);
            testResult.EndTime = Convert.ToDateTime(reader["EndTime"]);
            testResult.RightAnswerCount = Convert.ToInt32(reader["RightAnswerCount"]);
            testResult.TotalQuestion = Convert.ToInt32(reader["TOTAL_QUESTION"]);
            testResult.SubjectTitle = Convert.ToString(reader["SubjectTitle"]);
            return testResult;
        }