TestPageViewModel.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using Avalonia.Controls;
  9. using Avalonia.Layout;
  10. using CommunityToolkit.Mvvm.ComponentModel;
  11. using CommunityToolkit.Mvvm.Input;
  12. using EntranseTesting.Models;
  13. using Microsoft.EntityFrameworkCore;
  14. using MsBox.Avalonia;
  15. using MsBox.Avalonia.Enums;
  16. using ReactiveUI;
  17. namespace EntranseTesting.ViewModels
  18. {
  19. public partial class TestPageViewModel : ObservableObject, INotifyPropertyChanged
  20. {
  21. public event PropertyChangedEventHandler PropertyChanged;
  22. protected virtual void OnPropertyChanged(string propertyName)
  23. {
  24. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  25. }
  26. [ObservableProperty] AppSetting settingTest = new AppSetting();
  27. int hintCount = 0;
  28. //êîíòåéíåð äëÿ âîïðîñîâ
  29. UserControl testUC;
  30. //äàííûå î òåñòå
  31. string buttonValue = "Ñëåäóþùèé âîïðîñ";
  32. [ObservableProperty] int numQuestion = 0;
  33. [ObservableProperty] int countQuestion = 0;
  34. bool visibleHint = false;
  35. List<QuestionHint> hints = new List<QuestionHint>();
  36. [ObservableProperty] List<Question> questionsCollection;
  37. [ObservableProperty] List<ItemProgressButton> progressButtons = new List<ItemProgressButton>();
  38. int takeItem = 5;
  39. int skipItem = 0;
  40. List<ItemProgressButton> takeProgressButtons = new List<ItemProgressButton>();
  41. //view model äëÿ ðàáîòû ñ òåñòîì
  42. [ObservableProperty] TestArrangementOfElementsViewModel testAE;//ïåðåñòàíîâêà ýëåìåíòà
  43. [ObservableProperty] TestChoosingAnAnswerFromASetViewModel testCAFS;//ïîäñòàíîâêà îòâåòîâ â òåêñò
  44. [ObservableProperty] TestChoosing1AnswerViewModel testC1A;//âûáîð 1 âàðèàíòà
  45. [ObservableProperty] TestChoosingMultipleAnswersViewModel testCMA;//âûáîð íåñêîëüêèõ âàðèàíòîâ îòâåòà
  46. [ObservableProperty] TestMatchTheElementViewModel testME;//ñîîòíîøåíèå ýëåìåíòîâ
  47. [ObservableProperty] TestMatchTheValueViewModel testMV;//ñîîòíîøåíèå âåëè÷èí
  48. public TestPageViewModel()
  49. {
  50. EntranceTestingContext baseConnection = new EntranceTestingContext();
  51. SettingTest = baseConnection.AppSettings.ToList().Last();
  52. CountQuestion = SettingTest.CountOfQuestions;
  53. //ïîëó÷àåì âåñü ñïèñîê âîïðîñîâ èç áàçû
  54. QuestionsCollection = baseConnection.Questions.Include(tb => tb.IdCategoryNavigation).Where(tb => tb.InTest == true).ToList();
  55. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(QuestionsCollection));//ïåðåìåøèâàåì âîïðîñû
  56. QuestionsCollection = QuestionsCollection.Take(CountQuestion).ToList();//áåðåì íóæíîå êîëè÷åñòâî âîïðîñîâ
  57. //çàãðóæàåì ïîäñêàçêè ê âîïðîñàì
  58. Response.loadHints(QuestionsCollection);
  59. //êíîïêè íàâèãàöèè
  60. CountQuestion = (QuestionsCollection.Count == SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : (QuestionsCollection.Count > SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : QuestionsCollection.Count;
  61. for (int i = 0; i < CountQuestion; i++)
  62. ProgressButtons.Add(new ItemProgressButton(i + 1));
  63. ProgressButtons[0].Active = true;
  64. //çàïîëíÿåì ìàññèâ ñ îòâåòàìè ïîëüçîâàòåëÿ
  65. Response.responseUsers = new List<UserResponse>();
  66. for (int i = 0; i < CountQuestion; i++)
  67. Response.responseUsers.Add(new UserResponse() { IdQuestion = QuestionsCollection[i].Id });
  68. //íà÷èíàåì òåñò
  69. NumQuestion = 1;
  70. TakeItem = InterfaceSettings.take;
  71. changingPage();
  72. }
  73. public List<ItemProgressButton> TakeProgressButtons
  74. {
  75. get => ProgressButtons.Skip(SkipItem).Take(TakeItem).ToList();
  76. }
  77. public int TakeItem { get => takeItem; set { takeItem = value; OnPropertyChanged("TakeItem"); OnPropertyChanged("TakeProgressButtons"); } }
  78. public int SkipItem { get => skipItem; set { skipItem = value; OnPropertyChanged("SkipItem"); OnPropertyChanged("TakeProgressButtons"); } }
  79. public UserControl TestUC { get => testUC; set { testUC = value; OnPropertyChanged("TestUC"); OnPropertyChanged("ProgressText"); OnPropertyChanged("NumQuestion"); } }
  80. public string ProgressText { get => "{0} èç {3}"; }
  81. public int HintCount { get => hintCount; set { hintCount = value; OnPropertyChanged("HintCount"); OnPropertyChanged("HintCountLine"); } }
  82. public string HintCountLine { get => "Êîëè÷åñòâî îñòàâøèõñÿ î÷êîâ " + (SettingTest.CountOfHints - HintCount); }
  83. public List<QuestionHint> Hints { get => hints; set { hints = value; OnPropertyChanged("Hints"); } }
  84. public bool VisibleHint { get => visibleHint; set { visibleHint = value; OnPropertyChanged("VisibleHint"); OnPropertyChanged("NoVisibleHint"); } }
  85. public bool NoVisibleHint { get => !visibleHint; }
  86. public string ButtonValue { get => buttonValue; set { buttonValue = value; OnPropertyChanged("ButtonValue"); } }
  87. public void Next()
  88. {
  89. if (TakeProgressButtons.Last() != ProgressButtons.Last())
  90. SkipItem++;
  91. }
  92. public void Back()
  93. {
  94. if (TakeProgressButtons.First() != ProgressButtons.First())
  95. SkipItem--;
  96. }
  97. public void changingPage()
  98. {
  99. if (QuestionsCollection.Count > 0)
  100. {
  101. List<QuestionHint> _listHints = Response.questionHints.Where(tb => tb.IdQuestion == QuestionsCollection[NumQuestion - 1].Id).ToList();
  102. if (_listHints.Count > 0)
  103. {
  104. VisibleHint = true;
  105. Hints = _listHints.OrderBy(tb => tb.Cost).ToList();
  106. }
  107. else
  108. {
  109. VisibleHint = false;
  110. Hints = new List<QuestionHint>();
  111. }
  112. switch (QuestionsCollection[NumQuestion - 1].IdCategoryNavigation.Name)
  113. {
  114. case "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà":
  115. {
  116. TestC1A = new TestChoosing1AnswerViewModel(QuestionsCollection[NumQuestion - 1].Id);
  117. TestUC = new TestChoosing1Answer();
  118. break;
  119. }
  120. case "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà":
  121. {
  122. TestCMA = new TestChoosingMultipleAnswersViewModel(QuestionsCollection[NumQuestion - 1].Id);
  123. TestUC = new TestChoosingMultipleAnswers();
  124. break;
  125. }
  126. case "Ãîðèçîíòàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  127. {
  128. TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Horizontal);
  129. TestUC = new TestArrangementOfElements();
  130. break;
  131. }
  132. case "Âåðòèêàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  133. {
  134. TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Vertical);
  135. TestUC = new TestArrangementOfElements();
  136. break;
  137. }
  138. case "Ñîîòíîøåíèå âåëè÷èí":
  139. {
  140. TestMV = new TestMatchTheValueViewModel(QuestionsCollection[NumQuestion - 1].Id);
  141. TestUC = new TestMatchTheValue();
  142. break;
  143. }
  144. case "Ñîîòíîøåíèå ýëåìåíòîâ":
  145. {
  146. TestME = new TestMatchTheElementViewModel(QuestionsCollection[NumQuestion - 1].Id);
  147. TestUC = new TestMatchTheElement();
  148. break;
  149. }
  150. case "Ïîäñòàíîâêà îòâåòîâ":
  151. {
  152. TestCAFS = new TestChoosingAnAnswerFromASetViewModel(QuestionsCollection[NumQuestion - 1].Id);
  153. TestUC = new TestChoosingAnAnswerFromASet();
  154. break;
  155. }
  156. }
  157. }
  158. }
  159. public async void BuyAHint(int idQHint)
  160. {
  161. try
  162. {
  163. QuestionHint item = Hints.FirstOrDefault(tb => tb.Id == idQHint);
  164. if (SettingTest.CountOfHints - HintCount < item.Cost)
  165. {
  166. await MessageBoxManager.GetMessageBoxStandard("", "Âàì íå õâàòàåò î÷êîâ äëÿ îòêðûòèÿ ïîäñêàçêè", ButtonEnum.Ok).ShowAsync();
  167. return;
  168. }
  169. var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû äåéñòâèòåëüíî õîòèòå êóïèòü ïîäñêàçêó?\nÑòîèìîñòü ïîäñêàçêè " + item.CostLine + ".\nÏîñëå ïîêóïêè ó âàñ îñòàíåòñÿ " + Response.CostLine(SettingTest.CountOfHints - HintCount - item.Cost), ButtonEnum.YesNo).ShowAsync();
  170. switch (result)
  171. {
  172. case ButtonResult.Yes:
  173. {
  174. HintCount += item.Cost;
  175. int index = Hints.IndexOf(item);
  176. Hints[index].HintPurchased = true;
  177. index = Response.questionHints.IndexOf(item);
  178. Response.questionHints[index].HintPurchased = true;
  179. int responseIndex = Response.IndexResponse(QuestionsCollection[NumQuestion - 1].Id);
  180. Response.responseUsers[responseIndex].HintApply = true;
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. }
  187. catch (Exception ex)
  188. {
  189. await MessageBoxManager.GetMessageBoxStandard("", "×òî-òî ïîøëî íå òàê", ButtonEnum.Ok).ShowAsync();
  190. #if DEBUG
  191. Debug.WriteLine(ex.Message);
  192. #endif
  193. }
  194. }
  195. }
  196. }