TaskEditorViewModel.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.ComponentModel;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using Avalonia.Controls;
  10. using Avalonia.Platform.Storage;
  11. using CommunityToolkit.Mvvm.ComponentModel;
  12. using EntranseTesting.Models;
  13. using EntranseTesting.Models.customClass;
  14. using Microsoft.EntityFrameworkCore;
  15. using MsBox.Avalonia;
  16. using MsBox.Avalonia.Enums;
  17. using ReactiveUI;
  18. namespace EntranseTesting.ViewModels
  19. {
  20. public partial class TaskEditorViewModel : ObservableObject, INotifyPropertyChanged
  21. {
  22. public event PropertyChangedEventHandler PropertyChanged;
  23. protected virtual void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  24. //ýëåìåíòû ñòðàíèöû
  25. [ObservableProperty] UserControl? taskUC = null;
  26. [ObservableProperty] bool editing = false;
  27. [ObservableProperty] bool editingCategory = true;
  28. [ObservableProperty] string header = "";
  29. [ObservableProperty] List<string> category = ["--", "Âûáîð îòâåòà", "Óïîðÿäî÷èâàíèå ýëåìåíòîâ", "Ñîîòíîøåíèå", "Ïîäñòàíîâêà îòâåòîâ"];
  30. [ObservableProperty] List<string> _choseCategory = ["1 ïðàâèëüíîå çíà÷åíèå", "íåñêîëüêî ïðàâèëüíûõ çíà÷åíèé"];
  31. [ObservableProperty] List<string> _arrangmentCategory = ["ãîðèçîíòàëüíî", "âåðòèêàëüíî"];
  32. [ObservableProperty] List<string> _matchCategory = ["ãðóïïû ýëåìåíòîâ", "ðàâíûå âåëè÷èíû"];
  33. string selectedCategory = "--";
  34. string selectedMatchCategory = "ãðóïïû ýëåìåíòîâ";
  35. [ObservableProperty] bool selectedGroup = true;
  36. [ObservableProperty] string selectedChoseCategory = "1 ïðàâèëüíîå çíà÷åíèå";
  37. [ObservableProperty] string selectedArrangmentCategory = "ãîðèçîíòàëüíî";
  38. //ýëåìåíòû âîïðîñà
  39. [ObservableProperty] Question q;
  40. List<QuestionImage> qImage;
  41. List<QuestionHint> qHint;
  42. ObservableCollection<ElementOfChoose> chooseElement = new ObservableCollection<ElementOfChoose>();
  43. [ObservableProperty] Group matchesGroup1 = new Group();
  44. [ObservableProperty] Group matchesGroup2 = new Group();
  45. ObservableCollection<ItemMatch> matchesElement = new ObservableCollection<ItemMatch>();
  46. ObservableCollection<ItemCAFS> matchesMultiplyElement = new ObservableCollection<ItemCAFS>();
  47. ObservableCollection<ElementOfArrangement> arrangementElement = new ObservableCollection<ElementOfArrangement>();
  48. public TaskEditorViewModel()
  49. {
  50. Header = "Äîáàâëåíèå âîïðîñà";
  51. Q = new Question() { IdCategory = 1 };
  52. QImage = new List<QuestionImage>();
  53. QHint = new List<QuestionHint>();
  54. EntranceTestingContext connection = new EntranceTestingContext();
  55. connection.Questions.Add(Q);
  56. connection.SaveChanges();
  57. }
  58. public TaskEditorViewModel(int idQuestion)
  59. {
  60. EntranceTestingContext baseConnection = new EntranceTestingContext();
  61. Header = "Ðåäàêòèðîâàíèå âîïðîñà";
  62. Editing = true;
  63. OnPropertyChanged("NoEditing");
  64. Q = baseConnection.Questions.Include(tb => tb.IdCategoryNavigation)
  65. .Include(tb => tb.ElementOfChooses)
  66. .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement1Navigations)
  67. .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement2Navigations)
  68. .Include(tb => tb.ElementOfArrangements)
  69. .Include(tb => tb.Groups).ThenInclude(tb => tb.ElementOfGroups)
  70. .Include(tb => tb.TextOfPuttings).ThenInclude(tb => tb.ElementOfPuttings)
  71. .FirstOrDefault(tb => tb.Id == idQuestion);
  72. //èçîáðàæåíèÿ
  73. QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == Q.Id).ToList();
  74. if (QImage.Count == 0) QImage = new List<QuestionImage>();
  75. //ïîäñêàçêè
  76. QHint = baseConnection.QuestionHints
  77. .Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages)
  78. .Where(tb => tb.IdQuestion == Q.Id).ToList();
  79. if (QHint.Count == 0) QHint = new List<QuestionHint>();
  80. loadSettingCategory();
  81. }
  82. public string SelectedCategory
  83. {
  84. get => selectedCategory;
  85. set
  86. {
  87. selectedCategory = value;
  88. loadUC();
  89. OnPropertyChanged("TaskUC");
  90. }
  91. }
  92. public string SelectedMatchCategory
  93. {
  94. get => selectedMatchCategory;
  95. set
  96. {
  97. selectedMatchCategory = value;
  98. if (value == "ãðóïïû ýëåìåíòîâ")
  99. SelectedGroup = true;
  100. else
  101. SelectedGroup = false;
  102. OnPropertyChanged("SelectedGroup");
  103. }
  104. }
  105. public List<QuestionImage> QImage { get => qImage; set { qImage = value; OnPropertyChanged("QImage"); } }
  106. public List<QuestionHint> QHint { get => qHint; set { qHint = value; OnPropertyChanged("QHint"); } }
  107. public bool NoEditing { get => !Editing; }
  108. public ObservableCollection<ElementOfChoose> ChooseElement { get => chooseElement; set { chooseElement = value; OnPropertyChanged("ChooseElement"); } }
  109. public ObservableCollection<ElementOfArrangement> ArrangementElement { get => arrangementElement; set { arrangementElement = value; OnPropertyChanged("ArrangementElement"); } }
  110. public ObservableCollection<ItemMatch> MatchesElement { get => matchesElement; set { matchesElement = value; OnPropertyChanged("MatchesElement"); } }
  111. public ObservableCollection<ItemCAFS> MatchesMultiplyElement { get => matchesMultiplyElement; set { matchesMultiplyElement = value; OnPropertyChanged("MatchesMultiplyElement"); } }
  112. private async void loadSettingCategory()
  113. {
  114. try
  115. {
  116. switch (Q.IdCategoryNavigation.Name)
  117. {
  118. case "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà":
  119. {
  120. ChooseElement.Clear();
  121. foreach (var item in Q.ElementOfChooses)
  122. ChooseElement.Add(item);
  123. SelectedChoseCategory = "1 ïðàâèëüíîå çíà÷åíèå";
  124. SelectedCategory = "Âûáîð îòâåòà";
  125. break;
  126. }
  127. case "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà":
  128. {
  129. ChooseElement.Clear();
  130. foreach (var item in Q.ElementOfChooses)
  131. ChooseElement.Add(item);
  132. SelectedChoseCategory = "íåñêîëüêî ïðàâèëüíûõ çíà÷åíèé";
  133. SelectedCategory = "Âûáîð îòâåòà";
  134. break;
  135. }
  136. case "Ãîðèçîíòàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  137. {
  138. ArrangementElement.Clear();
  139. foreach (var item in Q.ElementOfArrangements.OrderBy(tb => tb.Position))
  140. ArrangementElement.Add(item);
  141. SelectedArrangmentCategory = "ãîðèçîíòàëüíî";
  142. SelectedCategory = "Óïîðÿäî÷èâàíèå ýëåìåíòîâ";
  143. break;
  144. }
  145. case "Âåðòèêàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  146. {
  147. ArrangementElement.Clear();
  148. foreach (var item in Q.ElementOfArrangements.OrderBy(tb => tb.Position))
  149. ArrangementElement.Add(item);
  150. SelectedArrangmentCategory = "âåðòèêàëüíî";
  151. SelectedCategory = "Óïîðÿäî÷èâàíèå ýëåìåíòîâ";
  152. break;
  153. }
  154. case "Ñîîòíîøåíèå ýëåìåíòîâ":
  155. {
  156. MatchesElement.Clear();
  157. MatchesGroup1 = Q.Groups.ToList()[0];
  158. MatchesGroup2 = Q.Groups.ToList()[1];
  159. List<ElementOfGroup> e1 = MatchesGroup1.ElementOfGroups.OrderBy(tb => tb.RatioNumeri).ToList();
  160. List<ElementOfGroup> e2 = MatchesGroup2.ElementOfGroups.OrderBy(tb => tb.RatioNumeri).ToList();
  161. for (int i = 0; i < e1.Count; i++)
  162. MatchesElement.Add(new ItemMatch(e1[i], e2[i]));
  163. SelectedMatchCategory = "ãðóïïû ýëåìåíòîâ";
  164. SelectedCategory = "Ñîîòíîøåíèå";
  165. EditingCategory = false;
  166. break;
  167. }
  168. case "Ñîîòíîøåíèå âåëè÷èí":
  169. {
  170. MatchesElement.Clear();
  171. List<ElementOfEquality> e = Q.ElementOfEqualities.ToList();
  172. for (int i = 0; i < e.Count; i++)
  173. {
  174. RatioOfElementEquality elem = e[i].RatioOfElementEqualityIdElement1Navigations.FirstOrDefault();
  175. if (elem != null)
  176. {
  177. ItemMatch item = new ItemMatch(elem.IdElement1Navigation, elem.IdElement2Navigation);
  178. MatchesElement.Add(item);
  179. }
  180. }
  181. SelectedMatchCategory = "ðàâíûå âåëè÷èíû";
  182. SelectedCategory = "Ñîîòíîøåíèå";
  183. EditingCategory = false;
  184. break;
  185. }
  186. case "Ïîäñòàíîâêà îòâåòîâ":
  187. {
  188. MatchesMultiplyElement.Clear();
  189. List<TextOfPutting> tp = Q.TextOfPuttings.ToList();
  190. for (int i = 0; i < tp.Count; i++)
  191. {
  192. List<ElementOfPutting> ep = tp[i].ElementOfPuttings.ToList();
  193. MatchesMultiplyElement.Add(new ItemCAFS(tp[i], ep));
  194. }
  195. SelectedCategory = "Ïîäñòàíîâêà îòâåòîâ";
  196. break;
  197. }
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà", "Âî âðåìÿ çàãðóçêè äàííûõ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  203. #if DEBUG
  204. Debug.Write(ex.Message);
  205. #endif
  206. }
  207. }
  208. private void loadUC()
  209. {
  210. switch (SelectedCategory)
  211. {
  212. case "Âûáîð îòâåòà":
  213. {
  214. TaskUC = new EditorChoosing();
  215. break;
  216. }
  217. case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  218. {
  219. TaskUC = new EditorArrangement();
  220. break;
  221. }
  222. case "Ñîîòíîøåíèå":
  223. {
  224. TaskUC = new EditorMatches();
  225. break;
  226. }
  227. case "Ïîäñòàíîâêà îòâåòîâ":
  228. {
  229. TaskUC = new EditorMatchesMultiply();
  230. break;
  231. }
  232. default:
  233. {
  234. TaskUC = null;
  235. break;
  236. }
  237. }
  238. }
  239. public async void AddItem()
  240. {
  241. switch (SelectedCategory)
  242. {
  243. case "Âûáîð îòâåòà":
  244. {
  245. ChooseElement.Add(new ElementOfChoose());
  246. break;
  247. }
  248. case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  249. {
  250. ArrangementElement.Add(new ElementOfArrangement());
  251. break;
  252. }
  253. case "Ñîîòíîøåíèå":
  254. {
  255. MatchesElement.Add(new ItemMatch());
  256. break;
  257. }
  258. case "Ïîäñòàíîâêà îòâåòîâ":
  259. {
  260. MatchesMultiplyElement.Add(new ItemCAFS());
  261. break;
  262. }
  263. default:
  264. {
  265. break;
  266. }
  267. }
  268. }
  269. public async void AddItemCAFS(ItemCAFS elem)
  270. {
  271. int index = MatchesMultiplyElement.IndexOf(elem);
  272. MatchesMultiplyElement[index].ElementEditor.Add(new ElementOfPutting());
  273. }
  274. public async void DeleteItemCAFS(object? collection)
  275. {
  276. try
  277. {
  278. var values = (IList<object?>)collection;
  279. ItemCAFS item = (ItemCAFS)values[0];
  280. ElementOfPutting elem = (ElementOfPutting)values[1];
  281. int index = MatchesMultiplyElement.IndexOf(item);
  282. MatchesMultiplyElement[index].ElementEditor.Remove(elem);
  283. }
  284. catch (Exception ex)
  285. {
  286. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  287. #if DEBUG
  288. Debug.Write(ex.Message);
  289. #endif
  290. }
  291. }
  292. public async void DeleteItem(object? elem)
  293. {
  294. try
  295. {
  296. switch (SelectedCategory)
  297. {
  298. case "Âûáîð îòâåòà":
  299. {
  300. ChooseElement.Remove((ElementOfChoose)elem);
  301. break;
  302. }
  303. case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  304. {
  305. ArrangementElement.Remove((ElementOfArrangement)elem);
  306. break;
  307. }
  308. case "Ñîîòíîøåíèå":
  309. {
  310. MatchesElement.Remove((ItemMatch)elem);
  311. break;
  312. }
  313. case "Ïîäñòàíîâêà îòâåòîâ":
  314. {
  315. MatchesMultiplyElement.Remove((ItemCAFS)elem);
  316. break;
  317. }
  318. default:
  319. {
  320. break;
  321. }
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  327. #if DEBUG
  328. Debug.Write(ex.Message);
  329. #endif
  330. }
  331. }
  332. public async void AddHint()
  333. {
  334. EntranceTestingContext connection = new EntranceTestingContext();
  335. bool countFlag = QHint.Count == 0;
  336. List<QuestionHint> list = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
  337. QuestionHint? qh = (list.Count > 0) ? list.Last() : null;
  338. //åñëè ïîäñêàçîê íåò è ïîäñêàçêà ñîäåðæèò òåêñò èëè èçîáðàæåíèÿ
  339. if (qh == null || countFlag || !string.IsNullOrWhiteSpace(qh.IdHintNavigation.Text) || qh.IdHintNavigation.HintImages.Count > 0)
  340. {
  341. HintText hint = new HintText();
  342. connection.HintTexts.Add(hint);
  343. connection.SaveChanges();
  344. QuestionHint qhint = new QuestionHint() { IdQuestion = Q.Id, IdHint = hint.Id };
  345. connection.QuestionHints.Add(qhint);
  346. connection.SaveChanges();
  347. QHint = connection.QuestionHints
  348. .Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages)
  349. .Where(tb => tb.IdQuestion == Q.Id).ToList();
  350. }
  351. else
  352. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà äîáàâëåíèÿ", "Ó âàñ óæå åñòü ïóñòàÿ ïîäñêàçêà, çàïîëíèòå ñíà÷àëà åå", ButtonEnum.Ok).ShowAsync();
  353. }
  354. public async void DeleteHint(int idHint)
  355. {
  356. try
  357. {
  358. var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü ïîäñêàçêó? Âîññòàíîâèòü åå â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
  359. switch (result)
  360. {
  361. case ButtonResult.Yes:
  362. {
  363. EntranceTestingContext connection = new EntranceTestingContext();
  364. int countHint = connection.QuestionHints.Where(tb => tb.IdHint == idHint).Count();
  365. QuestionHint qh = connection.QuestionHints.FirstOrDefault(tb => tb.IdHint == idHint && tb.IdQuestion == Q.Id);
  366. HintText hint = connection.HintTexts.FirstOrDefault(tb => tb.Id == idHint);
  367. if (countHint > 1)
  368. {
  369. connection.QuestionHints.Remove(qh);
  370. }
  371. else
  372. {
  373. connection.HintTexts.Remove(hint);
  374. }
  375. connection.SaveChanges();
  376. QHint = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
  377. await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå ïîäñêàçêè", "Ïîäñêàçêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
  378. break;
  379. }
  380. case ButtonResult.No:
  381. {
  382. break;
  383. }
  384. }
  385. }
  386. catch (Exception ex)
  387. {
  388. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  389. #if DEBUG
  390. Debug.Write(ex.Message);
  391. #endif
  392. }
  393. }
  394. public async void DeleteHintImage(int idImage)
  395. {
  396. try
  397. {
  398. var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü èçîáðàæåíèå? Âîññòàíîâèòü åãî â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
  399. switch (result)
  400. {
  401. case ButtonResult.Yes:
  402. {
  403. EntranceTestingContext connection = new EntranceTestingContext();
  404. HintImage hImage = connection.HintImages.FirstOrDefault(tb => tb.Id == idImage);
  405. connection.HintImages.Remove(hImage);
  406. connection.SaveChanges();
  407. QHint = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
  408. await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Êàðòèíêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
  409. break;
  410. }
  411. case ButtonResult.No:
  412. {
  413. break;
  414. }
  415. }
  416. }
  417. catch (Exception ex)
  418. {
  419. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  420. #if DEBUG
  421. Debug.Write(ex.Message);
  422. #endif
  423. }
  424. }
  425. public async void DeleteQuestionImage(int idImage)
  426. {
  427. try
  428. {
  429. var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü èçîáðàæåíèå? Âîññòàíîâèòü åãî â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
  430. switch (result)
  431. {
  432. case ButtonResult.Yes:
  433. {
  434. EntranceTestingContext connection = new EntranceTestingContext();
  435. QuestionImage qImage = connection.QuestionImages.FirstOrDefault(tb => tb.Id == idImage);
  436. connection.QuestionImages.Remove(qImage);
  437. connection.SaveChanges();
  438. QImage = connection.QuestionImages.Where(tb => tb.IdQuestion == Q.Id).ToList();
  439. await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Êàðòèíêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
  440. break;
  441. }
  442. case ButtonResult.No:
  443. {
  444. break;
  445. }
  446. }
  447. }
  448. catch (Exception ex)
  449. {
  450. await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
  451. #if DEBUG
  452. Debug.Write(ex.Message);
  453. #endif
  454. }
  455. }
  456. public async void QSaveChanges()
  457. {
  458. if (Editing)//åñëè ðåäàêòèðîâàíèå
  459. {
  460. editSaveChanges();
  461. }
  462. else//åñëè äîáàâëåíèå
  463. {
  464. bool flag = SelectedCategory == "Ñîîòíîøåíèå";
  465. string mess = "Âû óâåðåíû ÷òî õîòèòå ñîõðàíèòü âîïðîñ? Íåêîòîðûå èçìåíåíèÿ ïîñëå ñîõðàíåíèÿ áóäóò íåäîñòóïíû.\n Íàïðèìåð: \n\t - Èçìåíåíèå êàòåãîðèè âîïðîñà;\n\t - Äîáàâëåíèå è óäàëåíèå ýëåìåíòîâ âîïðîñà" + ((flag) ? ";\n\t - Èçìåíåíèå ñîîòíîøåíèÿ." : ".");
  466. var result = await MessageBoxManager.GetMessageBoxStandard("", mess, ButtonEnum.YesNo).ShowAsync();
  467. switch (result)
  468. {
  469. case ButtonResult.Yes:
  470. {
  471. createSaveChanges();
  472. break;
  473. }
  474. default:
  475. break;
  476. }
  477. }
  478. if (QHint.Count > 0)
  479. hintSaveChanges();
  480. }
  481. private async void hintSaveChanges()
  482. {
  483. try
  484. {
  485. EntranceTestingContext connection = new EntranceTestingContext();
  486. int index = 0;
  487. while(true)
  488. {
  489. bool flag = QHint[index].Cost > 0 && (!string.IsNullOrEmpty(QHint[index].IdHintNavigation.Text) || QHint[index].IdHintNavigation.HintImages.Count > 0);
  490. if (flag)
  491. {
  492. connection.QuestionHints.Update(QHint[index]);
  493. index++;
  494. }
  495. if (index == QHint.Count)
  496. break;
  497. }
  498. connection.SaveChanges();
  499. }
  500. catch (Exception ex)
  501. {
  502. await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ ñîõðàíåíèÿ ïîäñêàçîê ïðîèçîøëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
  503. #if DEBUG
  504. Debug.Write(ex.Message);
  505. #endif
  506. }
  507. }
  508. private async void editSaveChanges()
  509. {
  510. try
  511. {
  512. EntranceTestingContext connection = new EntranceTestingContext();
  513. int idCategory = CategoryIdFromSelectedComboBoxItem();
  514. if (idCategory != Q.IdCategory)
  515. Q.IdCategory = idCategory;
  516. connection.Update(Q);
  517. switch (idCategory)
  518. {
  519. case 1:
  520. case 2:
  521. {
  522. connection.ElementOfChooses.UpdateRange(ChooseElement);
  523. break;
  524. }
  525. case 3:
  526. case 4:
  527. {
  528. connection.ElementOfArrangements.UpdateRange(ArrangementElement);
  529. break;
  530. }
  531. case 5:
  532. {
  533. foreach (var item in MatchesElement)
  534. {
  535. item.Elem1_e.Name = item.Name1;
  536. item.Elem2_e.Name = item.Name2;
  537. connection.ElementOfEqualities.Update(item.Elem1_e);
  538. connection.ElementOfEqualities.Update(item.Elem2_e);
  539. }
  540. break;
  541. }
  542. case 6:
  543. {
  544. connection.Groups.Update(MatchesGroup1);
  545. connection.Groups.Update(MatchesGroup2);
  546. foreach (var item in MatchesElement)
  547. {
  548. item.Elem1.Name = item.Name1;
  549. item.Elem2.Name = item.Name2;
  550. connection.ElementOfGroups.Update(item.Elem1);
  551. connection.ElementOfGroups.Update(item.Elem2);
  552. }
  553. break;
  554. }
  555. case 7:
  556. {
  557. foreach (var item in MatchesMultiplyElement)
  558. {
  559. item.TP.Name = item.Text;
  560. connection.TextOfPuttings.Update(item.TP);
  561. connection.ElementOfPuttings.UpdateRange(item.ElementEditor);
  562. }
  563. break;
  564. }
  565. }
  566. connection.SaveChanges();
  567. await MessageBoxManager.GetMessageBoxStandard("", "Âîïðîñ óñïåøíî îáíîâëåí", ButtonEnum.Ok).ShowAsync();
  568. }
  569. catch (Exception ex)
  570. {
  571. await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ îáíîâëåíèÿ âîïðîñà âîçíèêëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
  572. #if DEBUG
  573. Debug.Write(ex.Message);
  574. #endif
  575. }
  576. }
  577. private async void createSaveChanges()
  578. {
  579. try
  580. {
  581. EntranceTestingContext connection = new EntranceTestingContext();
  582. int idCategory = CategoryIdFromSelectedComboBoxItem();
  583. if (idCategory == -1)
  584. {
  585. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè êàòåãîðèþ âîïðîñà, äàëüíåéøåå ñîõðàíåíèå íåâîçìîæíî", ButtonEnum.Ok, Icon.Error).ShowAsync();
  586. return;
  587. }
  588. Q.IdCategory = idCategory;
  589. bool flag = true; //ïóñòûå ýëåìåíòû
  590. switch (idCategory)
  591. {
  592. case 1:
  593. case 2:
  594. {
  595. Q.ElementOfChooses.Clear();
  596. if (string.IsNullOrWhiteSpace(Q.Name))
  597. {
  598. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  599. return;
  600. }
  601. if (ChooseElement.Count() == 0 || ChooseElement.Where(tb => tb.Name == "").Count() == ChooseElement.Count())
  602. {
  603. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè ýëåìåíòû. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  604. return;
  605. }
  606. bool correctly = ChooseElement.Where(tb => tb.Correctly == true).Count() > 0;
  607. if (!correctly)
  608. {
  609. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè íè îäèí ïðàâèëüíûé îòâåò. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  610. return;
  611. }
  612. foreach (var item in ChooseElement)
  613. {
  614. if (!string.IsNullOrWhiteSpace(item.Name))
  615. Q.ElementOfChooses.Add(item);
  616. else
  617. flag = false;
  618. }
  619. break;
  620. }
  621. case 3:
  622. case 4:
  623. {
  624. Q.ElementOfArrangements.Clear();
  625. if (string.IsNullOrWhiteSpace(Q.Name))
  626. {
  627. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  628. return;
  629. }
  630. if (ArrangementElement.Count() == 0 || ArrangementElement.Where(tb => tb.Name == "").Count() == ArrangementElement.Count())
  631. {
  632. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè ýëåìåíòû. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  633. return;
  634. }
  635. //ïðîâåðêà ïîðÿäêà
  636. bool positionFlag = true;
  637. int index = 1;
  638. foreach (var item in ArrangementElement.OrderBy(tb => tb.Position))
  639. {
  640. if (item.Position != index)
  641. positionFlag = false;
  642. index++;
  643. }
  644. if (!positionFlag)
  645. {
  646. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ïðîñòàâèëè ïîçèöèè ýëåìåíòîâ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  647. return;
  648. }
  649. //äîáàâëåíèå ýëåìåíòîâ
  650. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(ArrangementElement.ToList()));
  651. foreach (var item in ArrangementElement)
  652. {
  653. if (!string.IsNullOrWhiteSpace(item.Name))
  654. Q.ElementOfArrangements.Add(item);
  655. else
  656. flag = false;
  657. }
  658. break;
  659. }
  660. case 5:
  661. {
  662. if (string.IsNullOrWhiteSpace(Q.Name))
  663. {
  664. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  665. return;
  666. }
  667. if (MatchesElement.Count() == 0 || MatchesElement.Where(tb => tb.Name1 == "" || tb.Name2 == "").Count() == MatchesElement.Count())
  668. {
  669. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  670. return;
  671. }
  672. //òîëüêî ïðîâåðêà, çàïèñü ïîñëå ïîäòâåðæäåíèÿ òàê êàê áä êîðÿâàÿ è çàïèñûâàòü íåóäîáíî
  673. foreach (var item in MatchesElement)
  674. {
  675. if (string.IsNullOrWhiteSpace(item.Name1) || string.IsNullOrWhiteSpace(item.Name2))
  676. flag = false;
  677. }
  678. break;
  679. }
  680. case 6:
  681. {
  682. Q.Groups.Clear();
  683. if (string.IsNullOrWhiteSpace(Q.Name))
  684. {
  685. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  686. return;
  687. }
  688. if (MatchesElement.Count() == 0 || MatchesElement.Where(tb => tb.Name1 == "" || tb.Name2 == "").Count() == MatchesElement.Count())
  689. {
  690. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  691. return;
  692. }
  693. if (string.IsNullOrWhiteSpace(MatchesGroup1.Name) || string.IsNullOrWhiteSpace(MatchesGroup2.Name))
  694. {
  695. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå çàïîëíèëè íàçâàíèå ãðóïï. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  696. return;
  697. }
  698. Group group1 = new Group() { Name = MatchesGroup1.Name };
  699. Group group2 = new Group() { Name = MatchesGroup2.Name };
  700. int index = 1;
  701. foreach (var item in MatchesElement)
  702. {
  703. if (!string.IsNullOrWhiteSpace(item.Name1) && !string.IsNullOrWhiteSpace(item.Name2))
  704. {
  705. group1.ElementOfGroups.Add(new ElementOfGroup() { Name = item.Name1, RatioNumeri = index });
  706. group2.ElementOfGroups.Add(new ElementOfGroup() { Name = item.Name2, RatioNumeri = index });
  707. index++;
  708. }
  709. else
  710. flag = false;
  711. }
  712. Q.Groups.Add(group1);
  713. Q.Groups.Add(group2);
  714. break;
  715. }
  716. case 7:
  717. {
  718. Q.TextOfPuttings.Clear();
  719. if (MatchesMultiplyElement.Count() == 0)
  720. {
  721. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  722. return;
  723. }
  724. bool firstFlag = true;
  725. foreach (var item in MatchesMultiplyElement)
  726. {
  727. if (string.IsNullOrEmpty(item.Text))
  728. {
  729. flag = (firstFlag) ? true : false;
  730. }
  731. TextOfPutting tp = new TextOfPutting() { Name = item.Text };
  732. bool correctly = item.ElementEditor.Where(tb => tb.Correctly == true).Count() > 0;
  733. if (!correctly)
  734. {
  735. await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè íè îäèí ïðàâèëüíûé îòâåò. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  736. return;
  737. }
  738. foreach (var elem in item.ElementEditor)
  739. {
  740. if (!string.IsNullOrWhiteSpace(elem.Name))
  741. tp.ElementOfPuttings.Add(new ElementOfPutting() { Name = elem.Name, Correctly = elem.Correctly });
  742. else
  743. flag = false;
  744. }
  745. Q.TextOfPuttings.Add(tp);
  746. firstFlag = false;
  747. }
  748. break;
  749. }
  750. }
  751. if (!flag)
  752. {
  753. var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû íå çàïîëíèëè âñå ýëåìåíòû, ïîýòîìó ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
  754. return;
  755. }
  756. if (idCategory == 5)
  757. {
  758. Q.ElementOfEqualities.Clear();
  759. foreach (var item in MatchesElement)
  760. {
  761. if (!string.IsNullOrWhiteSpace(item.Name1) && !string.IsNullOrWhiteSpace(item.Name2))
  762. {
  763. ElementOfEquality elem1 = new ElementOfEquality() { Name = item.Name1 };
  764. ElementOfEquality elem2 = new ElementOfEquality() { Name = item.Name2 };
  765. Q.ElementOfEqualities.Add(elem1);
  766. Q.ElementOfEqualities.Add(elem2);
  767. connection.Questions.Update(Q);
  768. connection.SaveChanges();
  769. RatioOfElementEquality ratio = new RatioOfElementEquality() { IdElement1 = elem1.Id, IdElement2 = elem2.Id };
  770. connection.RatioOfElementEqualities.Add(ratio);
  771. }
  772. }
  773. }
  774. else
  775. connection.Questions.Update(Q);
  776. connection.SaveChanges();
  777. await MessageBoxManager.GetMessageBoxStandard("", "Âîïðîñ óñïåøíî ñîõðàíåí", ButtonEnum.Ok).ShowAsync();
  778. Editing = true;
  779. Header = "Ðåäàêòèðîâàíèå âîïðîñà";
  780. }
  781. catch (Exception ex)
  782. {
  783. await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ äîáàâëåíèÿ âîïðîñà âîçíèêëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
  784. #if DEBUG
  785. Debug.Write(ex.Message);
  786. #endif
  787. }
  788. }
  789. /// <summary>
  790. /// Ìåòîä äëÿ ïîëó÷åíèÿ Id êàòåãîðèè âîïðîñà â çàâèñèìîñòè îò âûáðàííûõ çíà÷åíèé ComboBox
  791. /// </summary>
  792. /// <returns></returns>
  793. private int CategoryIdFromSelectedComboBoxItem()
  794. {
  795. switch (SelectedCategory)
  796. {
  797. case "Âûáîð îòâåòà":
  798. {
  799. if (SelectedChoseCategory == "1 ïðàâèëüíîå çíà÷åíèå")
  800. return 1;
  801. else
  802. return 2;
  803. }
  804. case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
  805. {
  806. if (SelectedArrangmentCategory == "ãîðèçîíòàëüíî")
  807. return 3;
  808. else
  809. return 4;
  810. break;
  811. }
  812. case "Ñîîòíîøåíèå":
  813. {
  814. if (SelectedMatchCategory == "ãðóïïû ýëåìåíòîâ")
  815. return 6;
  816. else
  817. return 5;
  818. break;
  819. }
  820. case "Ïîäñòàíîâêà îòâåòîâ":
  821. {
  822. return 7;
  823. }
  824. default:
  825. return -1;
  826. }
  827. }
  828. }
  829. }