Response.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using Avalonia.Threading;
  2. using DynamicData.Binding;
  3. using EntranseTesting.Models.customClass;
  4. using Microsoft.EntityFrameworkCore;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace EntranseTesting.Models
  13. {
  14. public class Response
  15. {
  16. public static UserSession userSession = new UserSession();
  17. public static List<UserResponse> responseUsers = new List<UserResponse>();
  18. public static List<QuestionHint> questionHints = new List<QuestionHint>();
  19. public static bool change = false;
  20. public static DispatcherTimer timer = new DispatcherTimer();
  21. public static void loadHints(List<Question> questionsCollection)
  22. {
  23. EntranceTestingContext connection = new EntranceTestingContext();
  24. questionHints.Clear();
  25. foreach (var item in questionsCollection)
  26. {
  27. List<QuestionHint> list = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == item.Id).ToList();
  28. questionHints.AddRange(list);
  29. }
  30. }
  31. public static string CostLine(int Cost)
  32. {
  33. switch (Cost % 10)
  34. {
  35. case 1:
  36. {
  37. return Cost + " очко";
  38. }
  39. case 2:
  40. case 3:
  41. case 4:
  42. {
  43. return Cost + " очка";
  44. }
  45. default:
  46. {
  47. return Cost + " очков";
  48. }
  49. }
  50. }
  51. public static int IndexResponse(int idQuestion)
  52. {
  53. UserResponse item = responseUsers.FirstOrDefault(tb => tb.IdQuestion == idQuestion);
  54. if (item == null)
  55. return -1;
  56. return responseUsers.IndexOf(item);
  57. }
  58. public static void SaveChangesArrangement(int idQuestion, ObservableCollection<ElementOfArrangement> element)
  59. {
  60. int responseIndex = IndexResponse(idQuestion);
  61. List<UserResponseArrangement> list = responseUsers[responseIndex].UserResponseArrangements.ToList();
  62. responseUsers[responseIndex].UserResponseArrangements.Clear();
  63. int i = 1;
  64. foreach (var item in element)
  65. {
  66. int index = list.IndexOf(list.FirstOrDefault(tb => tb.IdElement == item.Id));
  67. list[index].Position = i++;
  68. }
  69. foreach (var item in list)
  70. responseUsers[responseIndex].UserResponseArrangements.Add(item);
  71. change = true;
  72. //проверка задания
  73. bool correctly = true;
  74. List<ElementOfArrangement> elem = element.OrderBy(tb => tb.Position).ToList();
  75. list = responseUsers[responseIndex].UserResponseArrangements.OrderBy(tb => tb.Position).ToList();
  76. for (int j = 0; i < elem.Count; j++)
  77. {
  78. if (elem[j].Id != list[j].IdElement)
  79. correctly = false;
  80. }
  81. responseUsers[responseIndex].Correctly = correctly;
  82. }
  83. public static void SaveChangesChoosing(int idQuestion, bool manyAnswer, ObservableCollection<ElementOfChoose> element)
  84. {
  85. change = false;
  86. int responseIndex = IndexResponse(idQuestion);
  87. List<UserResponseChooseAnswer> list = responseUsers[responseIndex].UserResponseChooseAnswers.ToList();
  88. responseUsers[responseIndex].UserResponseChooseAnswers.Clear();
  89. if (list.Count > 0)
  90. {
  91. foreach (var item in element)
  92. {
  93. int index = list.IndexOf(list.FirstOrDefault(tb => tb.IdElement == item.Id));
  94. list[index].Usercorrectly = (item.UserCorrectly == null || item.UserCorrectly == false) ? false : item.UserCorrectly;
  95. if ((bool)list[index].Usercorrectly) change = true;
  96. }
  97. }
  98. //проверка задания
  99. if (change)
  100. {
  101. foreach (var item in list) responseUsers[responseIndex].UserResponseChooseAnswers.Add(item);
  102. bool correctly = true;
  103. if (manyAnswer)
  104. {
  105. foreach (var item in list)
  106. {
  107. var correctItem = element.FirstOrDefault(tb => tb.Id == item.IdElement);
  108. if (correctItem.Correctly != item.Usercorrectly)
  109. correctly = false;
  110. }
  111. }
  112. else
  113. {
  114. var userCorrectly = list.FirstOrDefault(tb => tb.Usercorrectly == true);
  115. var elem = element.FirstOrDefault(tb => tb.Id == userCorrectly.IdElement);
  116. if (elem.Correctly != true)
  117. correctly = false;
  118. }
  119. responseUsers[responseIndex].Correctly = correctly;
  120. }
  121. else
  122. responseUsers[responseIndex].Correctly = false;
  123. }
  124. public static void SaveChangesCAFS(int idQuestion, ObservableCollection<ItemCAFS> element)
  125. {
  126. change = true;
  127. EntranceTestingContext connection = new EntranceTestingContext();
  128. int responseIndex = IndexResponse(idQuestion);
  129. responseUsers[responseIndex].UserResponseMultiplyAnswers.Clear();
  130. foreach (var item in element)
  131. {
  132. if (item.SelectedItem != "--")
  133. {
  134. TextOfPutting t = connection.TextOfPuttings.FirstOrDefault(tb => tb.Name == item.Text && tb.IdQuestion == idQuestion);
  135. ElementOfPutting e = connection.ElementOfPuttings.FirstOrDefault(tb => tb.Name == item.SelectedItem && tb.IdText == t.Id);
  136. responseUsers[responseIndex].UserResponseMultiplyAnswers.Add(new UserResponseMultiplyAnswer() { IdElement = e.Id, IdText = t.Id });
  137. }
  138. else
  139. {
  140. change = false;
  141. }
  142. }
  143. //проверка задания
  144. if (change)
  145. {
  146. bool correctly = true;
  147. List<UserResponseMultiplyAnswer> _list = responseUsers[responseIndex].UserResponseMultiplyAnswers.ToList();
  148. for (int i = 0; i < _list.Count; i++)
  149. {
  150. ElementOfPutting e = connection.ElementOfPuttings.FirstOrDefault(tb => tb.Id == _list[i].IdElement);
  151. if (e.Correctly != true)
  152. correctly = false;
  153. }
  154. responseUsers[responseIndex].Correctly = correctly;
  155. }
  156. else
  157. responseUsers[responseIndex].Correctly = false;
  158. }
  159. public static void SaveChangesMatchTheElement(int idQuestion, List<ItemMatch> element)
  160. {
  161. EntranceTestingContext connection = new EntranceTestingContext();
  162. int countMatch = connection.Groups.Include(tb => tb.ElementOfGroups).FirstOrDefault(tb => tb.IdQuestion == idQuestion).ElementOfGroups.Count();
  163. change = countMatch == element.Count;
  164. int responseIndex = IndexResponse(idQuestion);
  165. responseUsers[responseIndex].UserResponseMatchTheElements.Clear();
  166. if (element.Count > 0)
  167. {
  168. element = element.OrderBy(tb => tb.Num).ToList();
  169. foreach (var item in element)
  170. {
  171. UserResponseMatchTheElement ume = new UserResponseMatchTheElement()
  172. {
  173. IdElement1 = item.Elem1.Id,
  174. IdElement2 = item.Elem2.Id
  175. };
  176. responseUsers[responseIndex].UserResponseMatchTheElements.Add(ume);
  177. }
  178. }
  179. //проверка задания
  180. if (change)
  181. {
  182. bool correctly = true;
  183. List<UserResponseMatchTheElement> list = responseUsers[responseIndex].UserResponseMatchTheElements.ToList();
  184. var group1 = connection.Groups.Where(tb => tb.IdQuestion == idQuestion).Include(tb => tb.ElementOfGroups).ToList()[0];
  185. var group2 = connection.Groups.Where(tb => tb.IdQuestion == idQuestion).Include(tb => tb.ElementOfGroups).ToList()[1];
  186. for (int i = 0; i < countMatch; i++)
  187. {
  188. var elem1 = group1.ElementOfGroups.FirstOrDefault(tb => tb.Id == list[i].IdElement1 || tb.Id == list[i].IdElement2);
  189. var elem2 = group2.ElementOfGroups.FirstOrDefault(tb => tb.Id == list[i].IdElement1 || tb.Id == list[i].IdElement2);
  190. if (elem1.RatioNumeri != elem2.RatioNumeri)
  191. correctly = false;
  192. }
  193. responseUsers[responseIndex].Correctly = correctly;
  194. }
  195. else
  196. responseUsers[responseIndex].Correctly = false;
  197. }
  198. public static void SaveChangesMatchTheValue(int idQuestion, List<ItemMatchTheValue> element)
  199. {
  200. EntranceTestingContext connection = new EntranceTestingContext();
  201. List<ElementOfEquality> _list = connection.ElementOfEqualities.Include(tb => tb.RatioOfElementEqualityIdElement1Navigations).Include(tb => tb.RatioOfElementEqualityIdElement2Navigations).Where(tb => tb.IdQuestion == idQuestion).ToList();
  202. int countMatch = _list.Count() / 2;
  203. change = true;
  204. int responseIndex = IndexResponse(idQuestion);
  205. responseUsers[responseIndex].UserResponseMatchTheValues.Clear();
  206. if (element.Count > 0)
  207. {
  208. foreach (var item in element)
  209. {
  210. if (item.Elem1 != "--" && item.Elem2 != "--")
  211. {
  212. var elem1 = _list.FirstOrDefault(tb => tb.Name == item.Elem1);
  213. var elem2 = _list.FirstOrDefault(tb => tb.Name == item.Elem2);
  214. UserResponseMatchTheValue umv = new UserResponseMatchTheValue()
  215. {
  216. IdElement1 = elem1.Id,
  217. IdElement2 = elem2.Id
  218. };
  219. responseUsers[responseIndex].UserResponseMatchTheValues.Add(umv);
  220. }
  221. else
  222. {
  223. change = false;
  224. }
  225. }
  226. }
  227. //проверка задания
  228. if (change)
  229. {
  230. bool correctly = true;
  231. List<UserResponseMatchTheValue> list = responseUsers[responseIndex].UserResponseMatchTheValues.ToList();
  232. for (int i = 0; i < list.Count; i++)
  233. {
  234. var item = _list.FirstOrDefault(tb => tb.Id == list[i].IdElement1);
  235. var elem1 = item.RatioOfElementEqualityIdElement1Navigations.FirstOrDefault();
  236. var elem2 = item.RatioOfElementEqualityIdElement2Navigations.FirstOrDefault();
  237. if (elem1 != null)
  238. {
  239. correctly = (elem1.IdElement1 == list[i].IdElement1 && elem1.IdElement2 == list[i].IdElement2) || (elem1.IdElement2 == list[i].IdElement1 && elem1.IdElement1 == list[i].IdElement2);
  240. }
  241. else if (elem2 != null)
  242. {
  243. correctly = (elem2.IdElement1 == list[i].IdElement1 && elem2.IdElement2 == list[i].IdElement2) || (elem2.IdElement2 == list[i].IdElement1 && elem2.IdElement1 == list[i].IdElement2);
  244. }
  245. }
  246. responseUsers[responseIndex].Correctly = correctly;
  247. }
  248. else
  249. responseUsers[responseIndex].Correctly = false;
  250. }
  251. }
  252. }