TestPageViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. 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().LastOrDefault();
  52. if (SettingTest == null)
  53. {
  54. SettingTest = new AppSetting()
  55. {
  56. Time = new TimeSpan(0, 45, 0),
  57. CountOfQuestions = 30,
  58. HintVisibility = false,
  59. CountOfHints = 0,
  60. Raiting5 = 30,
  61. Raiting4 = 23,
  62. Raiting3 = 16
  63. };
  64. }
  65. CountQuestion = SettingTest.CountOfQuestions;
  66. //ïîëó÷àåì âåñü ñïèñîê âîïðîñîâ èç áàçû
  67. QuestionsCollection = baseConnection.Questions.Include(tb => tb.IdCategoryNavigation).Where(tb => tb.InTest == true).ToList();
  68. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(QuestionsCollection));//ïåðåìåøèâàåì âîïðîñû
  69. QuestionsCollection = QuestionsCollection.Take(CountQuestion).ToList();//áåðåì íóæíîå êîëè÷åñòâî âîïðîñîâ
  70. //çàãðóæàåì ïîäñêàçêè ê âîïðîñàì
  71. Response.loadHints(QuestionsCollection);
  72. //êíîïêè íàâèãàöèè
  73. CountQuestion = (QuestionsCollection.Count == SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : (QuestionsCollection.Count > SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : QuestionsCollection.Count;
  74. ProgressButtons.Clear();
  75. for (int i = 0; i < CountQuestion; i++)
  76. ProgressButtons.Add(new ItemProgressButton(i + 1));
  77. ProgressButtons[0].Active = true;
  78. //çàïîëíÿåì ìàññèâ ñ îòâåòàìè ïîëüçîâàòåëÿ
  79. Response.responseUsers = new List<UserResponse>();
  80. for (int i = 0; i < CountQuestion; i++)
  81. Response.responseUsers.Add(new UserResponse() { IdQuestion = QuestionsCollection[i].Id });
  82. //íà÷èíàåì òåñò
  83. NumQuestion = 1;
  84. TakeItem = InterfaceSettings.take;
  85. changingPage();
  86. }
  87. public List<ItemProgressButton> TakeProgressButtons
  88. {
  89. get => ProgressButtons.Skip(SkipItem).Take(TakeItem).ToList();
  90. }
  91. public int TakeItem { get => takeItem; set { takeItem = value; OnPropertyChanged("TakeItem"); OnPropertyChanged("TakeProgressButtons"); } }
  92. public int SkipItem { get => skipItem; set { skipItem = value; OnPropertyChanged("SkipItem"); OnPropertyChanged("TakeProgressButtons"); } }
  93. public UserControl TestUC { get => testUC; set { testUC = value; OnPropertyChanged("TestUC"); OnPropertyChanged("ProgressText"); OnPropertyChanged("NumQuestion"); } }
  94. public string ProgressText { get => "{0} èç {3}"; }
  95. public int HintCount { get => hintCount; set { hintCount = value; OnPropertyChanged("HintCount"); OnPropertyChanged("HintCountLine"); } }
  96. public string HintCountLine { get => "Êîëè÷åñòâî îñòàâøèõñÿ î÷êîâ " + (SettingTest.CountOfHints - HintCount); }
  97. public List<QuestionHint> Hints { get => hints; set { hints = value; OnPropertyChanged("Hints"); } }
  98. public bool VisibleHint { get => visibleHint; set { visibleHint = value; OnPropertyChanged("VisibleHint"); OnPropertyChanged("NoVisibleHint"); } }
  99. public bool NoVisibleHint { get => !visibleHint; }
  100. public string ButtonValue { get => buttonValue; set { buttonValue = value; OnPropertyChanged("ButtonValue"); } }
  101. public List<ItemProgressButton> ProgressButtons { get => progressButtons; set {progressButtons = value; OnPropertyChanged("ProgressButtons"); } }
  102. public void Next()
  103. {
  104. if (TakeProgressButtons.Last() != ProgressButtons.Last())
  105. SkipItem++;
  106. }
  107. public void Back()
  108. {
  109. if (TakeProgressButtons.First() != ProgressButtons.First())
  110. SkipItem--;
  111. }
  112. public void changingPage()
  113. {
  114. if (QuestionsCollection.Count > 0)
  115. {
  116. List<QuestionHint> _listHints = Response.questionHints.Where(tb => tb.IdQuestion == QuestionsCollection[NumQuestion - 1].Id).ToList();
  117. if (_listHints.Count > 0)
  118. {
  119. VisibleHint = true;
  120. Hints = _listHints.OrderBy(tb => tb.Cost).ToList();
  121. }
  122. else
  123. {
  124. VisibleHint = false;
  125. Hints = new List<QuestionHint>();
  126. }
  127. switch (QuestionsCollection[NumQuestion - 1].IdCategoryNavigation.Name)
  128. {
  129. case "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà":
  130. {
  131. TestC1A = new TestChoosing1AnswerViewModel(QuestionsCollection[NumQuestion - 1].Id);
  132. TestUC = new TestChoosing1Answer();
  133. break;
  134. }
  135. case "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà":
  136. {
  137. TestCMA = new TestChoosingMultipleAnswersViewModel(QuestionsCollection[NumQuestion - 1].Id);
  138. TestUC = new TestChoosingMultipleAnswers();
  139. break;
  140. }
  141. case "Ãîðèçîíòàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  142. {
  143. TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Horizontal);
  144. TestUC = new TestArrangementOfElements();
  145. break;
  146. }
  147. case "Âåðòèêàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  148. {
  149. TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Vertical);
  150. TestUC = new TestArrangementOfElements();
  151. break;
  152. }
  153. case "Ñîîòíîøåíèå âåëè÷èí":
  154. {
  155. TestMV = new TestMatchTheValueViewModel(QuestionsCollection[NumQuestion - 1].Id);
  156. TestUC = new TestMatchTheValue();
  157. break;
  158. }
  159. case "Ñîîòíîøåíèå ýëåìåíòîâ":
  160. {
  161. TestME = new TestMatchTheElementViewModel(QuestionsCollection[NumQuestion - 1].Id);
  162. TestUC = new TestMatchTheElement();
  163. break;
  164. }
  165. case "Ïîäñòàíîâêà îòâåòîâ":
  166. {
  167. TestCAFS = new TestChoosingAnAnswerFromASetViewModel(QuestionsCollection[NumQuestion - 1].Id);
  168. TestUC = new TestChoosingAnAnswerFromASet();
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. public async void BuyAHint(int idQHint)
  175. {
  176. try
  177. {
  178. QuestionHint item = Hints.FirstOrDefault(tb => tb.Id == idQHint);
  179. if (SettingTest.CountOfHints - HintCount < item.Cost)
  180. {
  181. await MessageBoxManager.GetMessageBoxStandard("", "Âàì íå õâàòàåò î÷êîâ äëÿ îòêðûòèÿ ïîäñêàçêè", ButtonEnum.Ok).ShowAsync();
  182. return;
  183. }
  184. var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû äåéñòâèòåëüíî õîòèòå êóïèòü ïîäñêàçêó?\nÑòîèìîñòü ïîäñêàçêè " + item.CostLine + ".\nÏîñëå ïîêóïêè ó âàñ îñòàíåòñÿ " + Response.CostLine(SettingTest.CountOfHints - HintCount - item.Cost), ButtonEnum.YesNo).ShowAsync();
  185. switch (result)
  186. {
  187. case ButtonResult.Yes:
  188. {
  189. HintCount += item.Cost;
  190. int index = Hints.IndexOf(item);
  191. Hints[index].HintPurchased = true;
  192. index = Response.questionHints.IndexOf(item);
  193. Response.questionHints[index].HintPurchased = true;
  194. int responseIndex = Response.IndexResponse(QuestionsCollection[NumQuestion - 1].Id);
  195. Response.responseUsers[responseIndex].HintApply = true;
  196. break;
  197. }
  198. default:
  199. break;
  200. }
  201. }
  202. catch (Exception ex)
  203. {
  204. await MessageBoxManager.GetMessageBoxStandard("", "×òî-òî ïîøëî íå òàê", ButtonEnum.Ok).ShowAsync();
  205. #if DEBUG
  206. Debug.WriteLine(ex.Message);
  207. #endif
  208. }
  209. }
  210. }
  211. }