public void InsertQuestionAnswer(AnsweredQuestion answeredQuestion) { try { var originalQuestion = _database.Questions.FirstOrDefault(x => x.Id == answeredQuestion.Questionid); if (originalQuestion == null) { return; } if (!string.Equals(originalQuestion.Answer.Trim(), answeredQuestion.Useranswer.Trim(), StringComparison.OrdinalIgnoreCase)) { _database.UserMarks.Add(new UserMark { Questionid = originalQuestion.Id, Mark = 0, Testid = originalQuestion.Testid, Userid = Convert.ToInt32(answeredQuestion.Userid) }); } _database.AnsweredQuestions.Add(answeredQuestion); _database.UserMarks.Add(new UserMark { Questionid = originalQuestion.Id, Mark = 5, Testid = originalQuestion.Testid, Userid = Convert.ToInt32(answeredQuestion.Userid) }); _database.SaveChanges(); } catch (Exception e) { throw new Exception(e.Message, e); } }
public void AddNewQuestion(Question question) { try { if (_database.Questions.Any(x => x.Text == question.Text && x.Testid == question.Testid)) { throw new SqlAlreadyFilledException("data already exists"); } _database.Questions.Add(question); _database.SaveChanges(); } catch (Exception e) { throw new NullReferenceException(e.Message, e); } }
public void SaveUserStatus(User user) { try { var dbEntry = _database.Users.FirstOrDefault(x => x.UserType == user.UserType && x.Password == user.Password); if (dbEntry == null) { throw new Exception("could not save status to database"); } dbEntry.Status = "Completed"; _database.SaveChanges(); } catch (SqlException exception) { throw new ApplicationException(exception.Message, exception); } }
private void Button_Click(object sender, RoutedEventArgs e)//new test { using (ExamDatabase ed = new ExamDatabase()) { if (ed.TestsInfo.Where(x => x.Title == TestNameTb.Text).FirstOrDefault() != null) { MessageBox.Show("Test's title have to be unique", "Incorrect test's title", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (String.IsNullOrEmpty(TestNameTb.Text)) { MessageBox.Show("Test's title must not be empty", "Incorrect test's title", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (!Int32.TryParse(TestAttempts.Text, out _attempts) || _attempts <= 0 || _attempts > 100) { MessageBox.Show("Incorrect attempts"); return; } string _testTitle = TestNameTb.Text; NewTestBtn.Visibility = Visibility.Hidden; TestAttempts.Visibility = Visibility.Hidden; TestNameTb.Visibility = Visibility.Hidden; OneQOneAns.Visibility = Visibility.Visible; OneQRb.Visibility = Visibility.Visible; FinishCreatingTestBtn.Visibility = Visibility.Visible; TestsInfo ti = new TestsInfo() { Title = _testTitle, Attempts = _attempts }; ed.TestsInfo.Add(ti); ed.SaveChanges(); _newTestId = ed.TestsInfo.FirstOrDefault(x => x.Title == _testTitle).Id; } AddQuestionBtn.IsEnabled = true; }
private void NewQuestionBtn(object sender, RoutedEventArgs e) { if (String.IsNullOrEmpty(QuestionTb.Text)) { MessageBox.Show("Enter question", "Question", MessageBoxButton.OK, MessageBoxImage.Error); return; } _question = new Questions(); using (ExamDatabase ed = new ExamDatabase()) { int weight = 0; if (!Int32.TryParse(QuestionWeightTb.Text, out weight)) { MessageBox.Show("Enter question's weight", "Incorrect question's weight", MessageBoxButton.OK, MessageBoxImage.Warning); return; } //REGION #region Question field check if (String.IsNullOrEmpty(QuestionTb.Text)) { MessageBox.Show("Question's field must not be empty", "Incorrect question's field", MessageBoxButton.OK, MessageBoxImage.Warning); return; } #endregion _question.Weight = weight; _question.Question = QuestionTb.Text; _question.TestId = _newTestId; if (!(imageForQuestion.Source is null)) { _question.Image = ConvertPicToByte(imageForQuestion.Source as BitmapImage); //load image to database } ed.Questions.Add(_question); List <AnswerVariants> avList = new List <AnswerVariants>(); int answersCount = 0; foreach (var tb in StackPanelWithCB.Children.OfType <CheckBox>().Where(x => !String.IsNullOrEmpty(x.Content.ToString()))) { if (tb.IsChecked == true) { answersCount++; } avList.Add(new AnswerVariants() { Variant = tb.Content.ToString(), IsAnswer = (bool)tb.IsChecked, QuestionId = _question.Id }); } //REGION #region Variants check if (answersCount == 0) { MessageBox.Show("Enter at least 1 answer", "", MessageBoxButton.OK, MessageBoxImage.Warning); return; } if (OneQOneAns.IsChecked == true && !OnlyOneCheckedInCheckBox()) { MessageBox.Show("2 answers have chosen as right. If you think its OK - chose \"One question → many answers\" mode", "too many answers", MessageBoxButton.OK, MessageBoxImage.Warning); return; } for (int i = 0; i < avList.Count - 1; i++) { for (int j = i + 1; j < avList.Count; j++) { if (avList[i].Variant.Equals(avList[j].Variant)) { MessageBox.Show($"U can't have two similar answers ({avList[i].Variant}", "Two similar answers", MessageBoxButton.OK, MessageBoxImage.Error); } } } #endregion foreach (var answers in avList) { ed.AnswerVariants.Add(answers); } ed.SaveChanges(); MessageBox.Show("Question has been added", "Question", MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK); MakeAllFieldsEmpty(); } }
private void button1_Click(object sender, EventArgs e) { if (UserType.SelectedItem != null | UserText.Text != null | PasswordText.Text != null) { try { var user = _database.Users.FirstOrDefault(x => x.Password == PasswordText.Text && x.Name == UserType.Text); if (user != null) { MessageBox.Show("User already exists", "information!", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (UserType.SelectedItem?.ToString() == "Administrator") { _database.Users.Add(new User { Name = UserText.Text, Password = PasswordText.Text, UserType = "Administrator" }); } else { _database.Users.Add(new User { Name = UserText.Text, Password = PasswordText.Text, UserType = UserType?.SelectedItem?.ToString(), Testid = (int)userTest.SelectedValue }); } _database.SaveChanges(); MessageBox.Show($@"Successfully Added {UserText.Text}", "information!", MessageBoxButtons.OK, MessageBoxIcon.Information); UserText.Text = ""; PasswordText.Text = ""; UserType.SelectedIndex = 0; UserType.Text = ""; userTest.Text = ""; } } catch (SqlException exception) { MessageBox.Show(exception.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); UserText.Text = ""; PasswordText.Text = ""; UserType.SelectedItem = ""; } } else { MessageBox.Show("Check All Fields", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); UserText.Text = ""; PasswordText.Text = ""; UserType.SelectedItem = ""; } }
private void FinishTestBtn_Click(object sender, RoutedEventArgs e) { CalculateCurrentMark(); using (ExamDatabase db = new ExamDatabase()) { int mark = (int)_currentMark; //both of these variables are 0 when this method perform, conundrum for me _testId = db.TestsInfo.Where(x => x.Title.Equals(TestTitleTb.Text)).FirstOrDefault().Id; _userId = db.Users.Where(x => x.Login.Equals(_user)).FirstOrDefault().Id; //if this user hasn't already passed this test int lastMark = -1; if (db.Results.Where(x => x.UserId == _userId).FirstOrDefault() != null) { if (db.Results.Where(x => x.UserId == _userId).ToList().Where(x => x.TestId == _testId).FirstOrDefault() != null) { lastMark = db.Results.Where(x => x.UserId == _userId).ToList().Where(x => x.TestId == _testId).FirstOrDefault().Mark; } } if (lastMark == -1) { int attemptsLeft = db.TestsInfo.Where(x => x.Id == _testId).FirstOrDefault().Attempts; Results result = new Results() { UserId = _userId, TestId = _testId, Mark = mark, Attemptsleft = attemptsLeft }; db.Results.Add(result); db.SaveChanges(); } else { int attemptsLeft = (int)db.Results.Where(x => x.UserId == _userId).ToList().Where(x => x.TestId == _testId).First().Attemptsleft; db.Results.Where(x => x.UserId == _userId).ToList().Where(x => x.TestId == _testId).First().Attemptsleft = --attemptsLeft; if (mark > lastMark) { db.Results.Where(x => x.UserId == _userId).ToList().Where(x => x.TestId == _testId).FirstOrDefault().Mark = mark; } db.SaveChanges(); } MessageBox.Show($"U have passed test with mark = {mark} ({String.Format("{0:0.0}", (double)mark / _maxMark * 100)} %)", "Result", MessageBoxButton.OK, MessageBoxImage.Information); TestsTitle.Visibility = Visibility.Visible; FinishTestBtn.Visibility = Visibility.Hidden; foreach (var checkBox in StackPanelWithCB.Children.OfType <CheckBox>()) { checkBox.Content = String.Empty; checkBox.IsEnabled = false; checkBox.IsChecked = false; } QuestionTb.Visibility = Visibility.Hidden; StartTestBtn.Visibility = Visibility.Visible; QuestiobVariantsInfoTb.Visibility = Visibility.Hidden; QuestionImage.Visibility = Visibility.Hidden; _countForQuestion = 0; } }