TestsCatalog.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace MyTests.Pages
  16. {
  17. public partial class TestsCatalog : Page
  18. {
  19. public TestsCatalog()
  20. {
  21. InitializeComponent();
  22. TestsListBox.Items.Clear();
  23. if (Session.User.Post == "Преподаватель")
  24. AddTestButton.Visibility = Visibility.Visible;
  25. LoadingTests();
  26. }
  27. private void AddTest_Click(object sender, RoutedEventArgs e)
  28. {
  29. NavigationService.Navigate(new EditTestPage());
  30. }
  31. void LoadingTests()
  32. {
  33. List<Tests> list = cnt.db.Tests.Where(item => item.IsVisible == true && item.Questions.Count > 0).ToList();
  34. if (TestNameBox.Text != "Название теста")
  35. list = list.Where(item => item.Name.StartsWith(TestNameBox.Text)).ToList();
  36. if (AuthorTestBox.Text != "Преподаватель")
  37. list = list.Where(item => item.Users.Login.StartsWith(AuthorTestBox.Text)).ToList();
  38. TestsListBox.ItemsSource = list;
  39. }
  40. private void FindTests_Click(object sender, RoutedEventArgs e)
  41. {
  42. LoadingTests();
  43. }
  44. private void TestNameBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  45. {
  46. TestNameBox.Text = string.Empty;
  47. }
  48. private void TestNameBox_LostFocus(object sender, RoutedEventArgs e)
  49. {
  50. if (TestNameBox.Text.Trim() == "")
  51. TestNameBox.Text = "Название теста";
  52. }
  53. private void AuthorTestBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
  54. {
  55. AuthorTestBox.Text = string.Empty;
  56. }
  57. private void AuthorTestBox_LostFocus(object sender, RoutedEventArgs e)
  58. {
  59. if (AuthorTestBox.Text.Trim() == "")
  60. AuthorTestBox.Text = "Преподаватель";
  61. }
  62. private void TestsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  63. {
  64. try
  65. {
  66. if (((Tests)TestsListBox.SelectedItem) != null)
  67. {
  68. Session.OpenedTest = cnt.db.Tests.Where(item => item.IdTest == ((Tests)TestsListBox.SelectedItem).IdTest).FirstOrDefault();
  69. Session.Points = 0;
  70. Session.CurQuestion = 0;
  71. Session.Quest.Content = cnt.db.Questions.Where(item => item.IdTest == Session.OpenedTest.IdTest).Select(item => item.Content).ToArray();
  72. Session.Quest.Answer = cnt.db.Questions.Where(item => item.IdTest == Session.OpenedTest.IdTest).Select(item => item.Answer).ToArray();
  73. NavigationService.Navigate(new Pages.CurTestPage());
  74. }
  75. }
  76. catch
  77. {
  78. new ErrorWindow("Ошибка открытия теста.").ShowDialog();
  79. }
  80. }
  81. }
  82. }
  83. //拯救我们被关在地下室