123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using Avalonia.Controls;
- using Avalonia.Platform.Storage;
- using CommunityToolkit.Mvvm.ComponentModel;
- using EntranseTesting.Models;
- using EntranseTesting.Models.customClass;
- using Microsoft.EntityFrameworkCore;
- using MsBox.Avalonia;
- using MsBox.Avalonia.Enums;
- using ReactiveUI;
- namespace EntranseTesting.ViewModels
- {
- public partial class TaskEditorViewModel : ObservableObject, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- //ýëåìåíòû ñòðàíèöû
- [ObservableProperty] UserControl? taskUC = null;
- [ObservableProperty] bool editing = false;
- [ObservableProperty] bool editingCategory = true;
- [ObservableProperty] string header = "";
- [ObservableProperty] List<string> category = ["--", "Âûáîð îòâåòà", "Óïîðÿäî÷èâàíèå ýëåìåíòîâ", "Ñîîòíîøåíèå", "Ïîäñòàíîâêà îòâåòîâ"];
- [ObservableProperty] List<string> _choseCategory = ["1 ïðàâèëüíîå çíà÷åíèå", "íåñêîëüêî ïðàâèëüíûõ çíà÷åíèé"];
- [ObservableProperty] List<string> _arrangmentCategory = ["ãîðèçîíòàëüíî", "âåðòèêàëüíî"];
- [ObservableProperty] List<string> _matchCategory = ["ãðóïïû ýëåìåíòîâ", "ðàâíûå âåëè÷èíû"];
- string selectedCategory = "--";
- string selectedMatchCategory = "ãðóïïû ýëåìåíòîâ";
- [ObservableProperty] bool selectedGroup = true;
- [ObservableProperty] string selectedChoseCategory = "1 ïðàâèëüíîå çíà÷åíèå";
- [ObservableProperty] string selectedArrangmentCategory = "ãîðèçîíòàëüíî";
- //ýëåìåíòû âîïðîñà
- [ObservableProperty] Question q;
- List<QuestionImage> qImage;
- List<QuestionHint> qHint;
- ObservableCollection<ElementOfChoose> chooseElement = new ObservableCollection<ElementOfChoose>();
- [ObservableProperty] Group matchesGroup1 = new Group();
- [ObservableProperty] Group matchesGroup2 = new Group();
- ObservableCollection<ItemMatch> matchesElement = new ObservableCollection<ItemMatch>();
- ObservableCollection<ItemCAFS> matchesMultiplyElement = new ObservableCollection<ItemCAFS>();
- ObservableCollection<ElementOfArrangement> arrangementElement = new ObservableCollection<ElementOfArrangement>();
- public TaskEditorViewModel()
- {
- Header = "Äîáàâëåíèå âîïðîñà";
- Q = new Question() { IdCategory = 1 };
- QImage = new List<QuestionImage>();
- QHint = new List<QuestionHint>();
- EntranceTestingContext connection = new EntranceTestingContext();
- connection.Questions.Add(Q);
- connection.SaveChanges();
- }
- public TaskEditorViewModel(int idQuestion)
- {
- EntranceTestingContext baseConnection = new EntranceTestingContext();
- Header = "Ðåäàêòèðîâàíèå âîïðîñà";
- Editing = true;
- OnPropertyChanged("NoEditing");
- Q = baseConnection.Questions.Include(tb => tb.IdCategoryNavigation)
- .Include(tb => tb.ElementOfChooses)
- .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement1Navigations)
- .Include(tb => tb.ElementOfEqualities).ThenInclude(tb => tb.RatioOfElementEqualityIdElement2Navigations)
- .Include(tb => tb.ElementOfArrangements)
- .Include(tb => tb.Groups).ThenInclude(tb => tb.ElementOfGroups)
- .Include(tb => tb.TextOfPuttings).ThenInclude(tb => tb.ElementOfPuttings)
- .FirstOrDefault(tb => tb.Id == idQuestion);
- //èçîáðàæåíèÿ
- QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == Q.Id).ToList();
- if (QImage.Count == 0) QImage = new List<QuestionImage>();
- //ïîäñêàçêè
- QHint = baseConnection.QuestionHints
- .Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages)
- .Where(tb => tb.IdQuestion == Q.Id).ToList();
- if (QHint.Count == 0) QHint = new List<QuestionHint>();
- loadSettingCategory();
- }
- public string SelectedCategory
- {
- get => selectedCategory;
- set
- {
- selectedCategory = value;
- loadUC();
- OnPropertyChanged("TaskUC");
- }
- }
- public string SelectedMatchCategory
- {
- get => selectedMatchCategory;
- set
- {
- selectedMatchCategory = value;
- if (value == "ãðóïïû ýëåìåíòîâ")
- SelectedGroup = true;
- else
- SelectedGroup = false;
- OnPropertyChanged("SelectedGroup");
- }
- }
- public List<QuestionImage> QImage { get => qImage; set { qImage = value; OnPropertyChanged("QImage"); } }
- public List<QuestionHint> QHint { get => qHint; set { qHint = value; OnPropertyChanged("QHint"); } }
- public bool NoEditing { get => !Editing; }
- public ObservableCollection<ElementOfChoose> ChooseElement { get => chooseElement; set { chooseElement = value; OnPropertyChanged("ChooseElement"); } }
- public ObservableCollection<ElementOfArrangement> ArrangementElement { get => arrangementElement; set { arrangementElement = value; OnPropertyChanged("ArrangementElement"); } }
- public ObservableCollection<ItemMatch> MatchesElement { get => matchesElement; set { matchesElement = value; OnPropertyChanged("MatchesElement"); } }
- public ObservableCollection<ItemCAFS> MatchesMultiplyElement { get => matchesMultiplyElement; set { matchesMultiplyElement = value; OnPropertyChanged("MatchesMultiplyElement"); } }
- private async void loadSettingCategory()
- {
- try
- {
- switch (Q.IdCategoryNavigation.Name)
- {
- case "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà":
- {
- ChooseElement.Clear();
- foreach (var item in Q.ElementOfChooses)
- ChooseElement.Add(item);
- SelectedChoseCategory = "1 ïðàâèëüíîå çíà÷åíèå";
- SelectedCategory = "Âûáîð îòâåòà";
- break;
- }
- case "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà":
- {
- ChooseElement.Clear();
- foreach (var item in Q.ElementOfChooses)
- ChooseElement.Add(item);
- SelectedChoseCategory = "íåñêîëüêî ïðàâèëüíûõ çíà÷åíèé";
- SelectedCategory = "Âûáîð îòâåòà";
- break;
- }
- case "Ãîðèçîíòàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- ArrangementElement.Clear();
- foreach (var item in Q.ElementOfArrangements.OrderBy(tb => tb.Position))
- ArrangementElement.Add(item);
- SelectedArrangmentCategory = "ãîðèçîíòàëüíî";
- SelectedCategory = "Óïîðÿäî÷èâàíèå ýëåìåíòîâ";
- break;
- }
- case "Âåðòèêàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- ArrangementElement.Clear();
- foreach (var item in Q.ElementOfArrangements.OrderBy(tb => tb.Position))
- ArrangementElement.Add(item);
- SelectedArrangmentCategory = "âåðòèêàëüíî";
- SelectedCategory = "Óïîðÿäî÷èâàíèå ýëåìåíòîâ";
- break;
- }
- case "Ñîîòíîøåíèå ýëåìåíòîâ":
- {
- MatchesElement.Clear();
- MatchesGroup1 = Q.Groups.ToList()[0];
- MatchesGroup2 = Q.Groups.ToList()[1];
- List<ElementOfGroup> e1 = MatchesGroup1.ElementOfGroups.OrderBy(tb => tb.RatioNumeri).ToList();
- List<ElementOfGroup> e2 = MatchesGroup2.ElementOfGroups.OrderBy(tb => tb.RatioNumeri).ToList();
- for (int i = 0; i < e1.Count; i++)
- MatchesElement.Add(new ItemMatch(e1[i], e2[i]));
- SelectedMatchCategory = "ãðóïïû ýëåìåíòîâ";
- SelectedCategory = "Ñîîòíîøåíèå";
- EditingCategory = false;
- break;
- }
- case "Ñîîòíîøåíèå âåëè÷èí":
- {
- MatchesElement.Clear();
- List<ElementOfEquality> e = Q.ElementOfEqualities.ToList();
- for (int i = 0; i < e.Count; i++)
- {
- RatioOfElementEquality elem = e[i].RatioOfElementEqualityIdElement1Navigations.FirstOrDefault();
- if (elem != null)
- {
- ItemMatch item = new ItemMatch(elem.IdElement1Navigation, elem.IdElement2Navigation);
- MatchesElement.Add(item);
- }
- }
- SelectedMatchCategory = "ðàâíûå âåëè÷èíû";
- SelectedCategory = "Ñîîòíîøåíèå";
- EditingCategory = false;
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- MatchesMultiplyElement.Clear();
- List<TextOfPutting> tp = Q.TextOfPuttings.ToList();
- for (int i = 0; i < tp.Count; i++)
- {
- List<ElementOfPutting> ep = tp[i].ElementOfPuttings.ToList();
- MatchesMultiplyElement.Add(new ItemCAFS(tp[i], ep));
- }
- SelectedCategory = "Ïîäñòàíîâêà îòâåòîâ";
- break;
- }
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà", "Âî âðåìÿ çàãðóçêè äàííûõ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- private void loadUC()
- {
- switch (SelectedCategory)
- {
- case "Âûáîð îòâåòà":
- {
- TaskUC = new EditorChoosing();
- break;
- }
- case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- TaskUC = new EditorArrangement();
- break;
- }
- case "Ñîîòíîøåíèå":
- {
- TaskUC = new EditorMatches();
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- TaskUC = new EditorMatchesMultiply();
- break;
- }
- default:
- {
- TaskUC = null;
- break;
- }
- }
- }
- public async void AddItem()
- {
- switch (SelectedCategory)
- {
- case "Âûáîð îòâåòà":
- {
- ChooseElement.Add(new ElementOfChoose());
- break;
- }
- case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- ArrangementElement.Add(new ElementOfArrangement());
- break;
- }
- case "Ñîîòíîøåíèå":
- {
- MatchesElement.Add(new ItemMatch());
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- MatchesMultiplyElement.Add(new ItemCAFS());
- break;
- }
- default:
- {
- break;
- }
- }
- }
- public async void AddItemCAFS(ItemCAFS elem)
- {
- int index = MatchesMultiplyElement.IndexOf(elem);
- MatchesMultiplyElement[index].ElementEditor.Add(new ElementOfPutting());
- }
- public async void DeleteItemCAFS(object? collection)
- {
- try
- {
- var values = (IList<object?>)collection;
- ItemCAFS item = (ItemCAFS)values[0];
- ElementOfPutting elem = (ElementOfPutting)values[1];
- int index = MatchesMultiplyElement.IndexOf(item);
- MatchesMultiplyElement[index].ElementEditor.Remove(elem);
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- public async void DeleteItem(object? elem)
- {
- try
- {
- switch (SelectedCategory)
- {
- case "Âûáîð îòâåòà":
- {
- ChooseElement.Remove((ElementOfChoose)elem);
- break;
- }
- case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- ArrangementElement.Remove((ElementOfArrangement)elem);
- break;
- }
- case "Ñîîòíîøåíèå":
- {
- MatchesElement.Remove((ItemMatch)elem);
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- MatchesMultiplyElement.Remove((ItemCAFS)elem);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- public async void AddHint()
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- bool countFlag = QHint.Count == 0;
- List<QuestionHint> list = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
- QuestionHint? qh = (list.Count > 0) ? list.Last() : null;
- //åñëè ïîäñêàçîê íåò è ïîäñêàçêà ñîäåðæèò òåêñò èëè èçîáðàæåíèÿ
- if (qh == null || countFlag || !string.IsNullOrWhiteSpace(qh.IdHintNavigation.Text) || qh.IdHintNavigation.HintImages.Count > 0)
- {
- HintText hint = new HintText();
- connection.HintTexts.Add(hint);
- connection.SaveChanges();
- QuestionHint qhint = new QuestionHint() { IdQuestion = Q.Id, IdHint = hint.Id };
- connection.QuestionHints.Add(qhint);
- connection.SaveChanges();
- QHint = connection.QuestionHints
- .Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages)
- .Where(tb => tb.IdQuestion == Q.Id).ToList();
- }
- else
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà äîáàâëåíèÿ", "Ó âàñ óæå åñòü ïóñòàÿ ïîäñêàçêà, çàïîëíèòå ñíà÷àëà åå", ButtonEnum.Ok).ShowAsync();
- }
- public async void DeleteHint(int idHint)
- {
- try
- {
- var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü ïîäñêàçêó? Âîññòàíîâèòü åå â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- int countHint = connection.QuestionHints.Where(tb => tb.IdHint == idHint).Count();
- QuestionHint qh = connection.QuestionHints.FirstOrDefault(tb => tb.IdHint == idHint && tb.IdQuestion == Q.Id);
- HintText hint = connection.HintTexts.FirstOrDefault(tb => tb.Id == idHint);
- if (countHint > 1)
- {
- connection.QuestionHints.Remove(qh);
- }
- else
- {
- connection.HintTexts.Remove(hint);
- }
- connection.SaveChanges();
- QHint = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
- await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå ïîäñêàçêè", "Ïîäñêàçêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
- break;
- }
- case ButtonResult.No:
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- public async void DeleteHintImage(int idImage)
- {
- try
- {
- var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü èçîáðàæåíèå? Âîññòàíîâèòü åãî â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- HintImage hImage = connection.HintImages.FirstOrDefault(tb => tb.Id == idImage);
- connection.HintImages.Remove(hImage);
- connection.SaveChanges();
- QHint = connection.QuestionHints.Include(tb => tb.IdHintNavigation).ThenInclude(tb => tb.HintImages).Where(tb => tb.IdQuestion == Q.Id).ToList();
- await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Êàðòèíêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
- break;
- }
- case ButtonResult.No:
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- public async void DeleteQuestionImage(int idImage)
- {
- try
- {
- var result = await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Âû äåéñòâèòåëüíî õîòèòå óäàëèòü èçîáðàæåíèå? Âîññòàíîâèòü åãî â äàëüíåéøåì áóäåò íåëüçÿ", ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- QuestionImage qImage = connection.QuestionImages.FirstOrDefault(tb => tb.Id == idImage);
- connection.QuestionImages.Remove(qImage);
- connection.SaveChanges();
- QImage = connection.QuestionImages.Where(tb => tb.IdQuestion == Q.Id).ToList();
- await MessageBoxManager.GetMessageBoxStandard("Óäàëåíèå êàðòèíêè", "Êàðòèíêà óñïåøíî óäàëåíà", ButtonEnum.Ok).ShowAsync();
- break;
- }
- case ButtonResult.No:
- {
- break;
- }
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("Îøèáêà óäàëåíèÿ", "Âî âðåìÿ óäàëåíèÿ âîçíèêëà îøèáêà", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- public async void QSaveChanges()
- {
- if (Editing)//åñëè ðåäàêòèðîâàíèå
- {
- editSaveChanges();
- }
- else//åñëè äîáàâëåíèå
- {
- bool flag = SelectedCategory == "Ñîîòíîøåíèå";
- string mess = "Âû óâåðåíû ÷òî õîòèòå ñîõðàíèòü âîïðîñ? Íåêîòîðûå èçìåíåíèÿ ïîñëå ñîõðàíåíèÿ áóäóò íåäîñòóïíû.\n Íàïðèìåð: \n\t - Èçìåíåíèå êàòåãîðèè âîïðîñà;\n\t - Äîáàâëåíèå è óäàëåíèå ýëåìåíòîâ âîïðîñà" + ((flag) ? ";\n\t - Èçìåíåíèå ñîîòíîøåíèÿ." : ".");
- var result = await MessageBoxManager.GetMessageBoxStandard("", mess, ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- createSaveChanges();
- break;
- }
- default:
- break;
- }
- }
- if (QHint.Count > 0)
- hintSaveChanges();
- }
- private async void hintSaveChanges()
- {
- try
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- int index = 0;
- while(true)
- {
- bool flag = QHint[index].Cost > 0 && (!string.IsNullOrEmpty(QHint[index].IdHintNavigation.Text) || QHint[index].IdHintNavigation.HintImages.Count > 0);
- if (flag)
- {
- connection.QuestionHints.Update(QHint[index]);
- index++;
- }
- if (index == QHint.Count)
- break;
- }
- connection.SaveChanges();
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ ñîõðàíåíèÿ ïîäñêàçîê ïðîèçîøëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- private async void editSaveChanges()
- {
- try
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- int idCategory = CategoryIdFromSelectedComboBoxItem();
- if (idCategory != Q.IdCategory)
- Q.IdCategory = idCategory;
- connection.Update(Q);
- switch (idCategory)
- {
- case 1:
- case 2:
- {
- connection.ElementOfChooses.UpdateRange(ChooseElement);
- break;
- }
- case 3:
- case 4:
- {
- connection.ElementOfArrangements.UpdateRange(ArrangementElement);
- break;
- }
- case 5:
- {
- foreach (var item in MatchesElement)
- {
- item.Elem1_e.Name = item.Name1;
- item.Elem2_e.Name = item.Name2;
- connection.ElementOfEqualities.Update(item.Elem1_e);
- connection.ElementOfEqualities.Update(item.Elem2_e);
- }
- break;
- }
- case 6:
- {
- connection.Groups.Update(MatchesGroup1);
- connection.Groups.Update(MatchesGroup2);
- foreach (var item in MatchesElement)
- {
- item.Elem1.Name = item.Name1;
- item.Elem2.Name = item.Name2;
- connection.ElementOfGroups.Update(item.Elem1);
- connection.ElementOfGroups.Update(item.Elem2);
- }
- break;
- }
- case 7:
- {
- foreach (var item in MatchesMultiplyElement)
- {
- item.TP.Name = item.Text;
- connection.TextOfPuttings.Update(item.TP);
- connection.ElementOfPuttings.UpdateRange(item.ElementEditor);
- }
- break;
- }
- }
- connection.SaveChanges();
- await MessageBoxManager.GetMessageBoxStandard("", "Âîïðîñ óñïåøíî îáíîâëåí", ButtonEnum.Ok).ShowAsync();
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ îáíîâëåíèÿ âîïðîñà âîçíèêëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- private async void createSaveChanges()
- {
- try
- {
- EntranceTestingContext connection = new EntranceTestingContext();
- int idCategory = CategoryIdFromSelectedComboBoxItem();
- if (idCategory == -1)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè êàòåãîðèþ âîïðîñà, äàëüíåéøåå ñîõðàíåíèå íåâîçìîæíî", ButtonEnum.Ok, Icon.Error).ShowAsync();
- return;
- }
- Q.IdCategory = idCategory;
- bool flag = true; //ïóñòûå ýëåìåíòû
- switch (idCategory)
- {
- case 1:
- case 2:
- {
- Q.ElementOfChooses.Clear();
- if (string.IsNullOrWhiteSpace(Q.Name))
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (ChooseElement.Count() == 0 || ChooseElement.Where(tb => tb.Name == "").Count() == ChooseElement.Count())
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè ýëåìåíòû. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- bool correctly = ChooseElement.Where(tb => tb.Correctly == true).Count() > 0;
- if (!correctly)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè íè îäèí ïðàâèëüíûé îòâåò. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- foreach (var item in ChooseElement)
- {
- if (!string.IsNullOrWhiteSpace(item.Name))
- Q.ElementOfChooses.Add(item);
- else
- flag = false;
- }
- break;
- }
- case 3:
- case 4:
- {
- Q.ElementOfArrangements.Clear();
- if (string.IsNullOrWhiteSpace(Q.Name))
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (ArrangementElement.Count() == 0 || ArrangementElement.Where(tb => tb.Name == "").Count() == ArrangementElement.Count())
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè ýëåìåíòû. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- //ïðîâåðêà ïîðÿäêà
- bool positionFlag = true;
- int index = 1;
- foreach (var item in ArrangementElement.OrderBy(tb => tb.Position))
- {
- if (item.Position != index)
- positionFlag = false;
- index++;
- }
- if (!positionFlag)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ïðîñòàâèëè ïîçèöèè ýëåìåíòîâ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- //äîáàâëåíèå ýëåìåíòîâ
- Random.Shared.Shuffle(CollectionsMarshal.AsSpan(ArrangementElement.ToList()));
- foreach (var item in ArrangementElement)
- {
- if (!string.IsNullOrWhiteSpace(item.Name))
- Q.ElementOfArrangements.Add(item);
- else
- flag = false;
- }
- break;
- }
- case 5:
- {
- if (string.IsNullOrWhiteSpace(Q.Name))
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (MatchesElement.Count() == 0 || MatchesElement.Where(tb => tb.Name1 == "" || tb.Name2 == "").Count() == MatchesElement.Count())
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- //òîëüêî ïðîâåðêà, çàïèñü ïîñëå ïîäòâåðæäåíèÿ òàê êàê áä êîðÿâàÿ è çàïèñûâàòü íåóäîáíî
- foreach (var item in MatchesElement)
- {
- if (string.IsNullOrWhiteSpace(item.Name1) || string.IsNullOrWhiteSpace(item.Name2))
- flag = false;
- }
- break;
- }
- case 6:
- {
- Q.Groups.Clear();
- if (string.IsNullOrWhiteSpace(Q.Name))
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå ââåëè âîïðîñ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (MatchesElement.Count() == 0 || MatchesElement.Where(tb => tb.Name1 == "" || tb.Name2 == "").Count() == MatchesElement.Count())
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (string.IsNullOrWhiteSpace(MatchesGroup1.Name) || string.IsNullOrWhiteSpace(MatchesGroup2.Name))
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå çàïîëíèëè íàçâàíèå ãðóïï. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- Group group1 = new Group() { Name = MatchesGroup1.Name };
- Group group2 = new Group() { Name = MatchesGroup2.Name };
- int index = 1;
- foreach (var item in MatchesElement)
- {
- if (!string.IsNullOrWhiteSpace(item.Name1) && !string.IsNullOrWhiteSpace(item.Name2))
- {
- group1.ElementOfGroups.Add(new ElementOfGroup() { Name = item.Name1, RatioNumeri = index });
- group2.ElementOfGroups.Add(new ElementOfGroup() { Name = item.Name2, RatioNumeri = index });
- index++;
- }
- else
- flag = false;
- }
- Q.Groups.Add(group1);
- Q.Groups.Add(group2);
- break;
- }
- case 7:
- {
- Q.TextOfPuttings.Clear();
- if (MatchesMultiplyElement.Count() == 0)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå äîáàâèëè âàðèàíòû ñîîòíîøåíèÿ. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- bool firstFlag = true;
- foreach (var item in MatchesMultiplyElement)
- {
- if (string.IsNullOrEmpty(item.Text))
- {
- flag = (firstFlag) ? true : false;
- }
- TextOfPutting tp = new TextOfPutting() { Name = item.Text };
- bool correctly = item.ElementEditor.Where(tb => tb.Correctly == true).Count() > 0;
- if (!correctly)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âû íå âûáðàëè íè îäèí ïðàâèëüíûé îòâåò. Ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- foreach (var elem in item.ElementEditor)
- {
- if (!string.IsNullOrWhiteSpace(elem.Name))
- tp.ElementOfPuttings.Add(new ElementOfPutting() { Name = elem.Name, Correctly = elem.Correctly });
- else
- flag = false;
- }
- Q.TextOfPuttings.Add(tp);
- firstFlag = false;
- }
- break;
- }
- }
- if (!flag)
- {
- var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû íå çàïîëíèëè âñå ýëåìåíòû, ïîýòîìó ñîõðàíåíèå ïðåðâàíî", ButtonEnum.Ok).ShowAsync();
- return;
- }
- if (idCategory == 5)
- {
- Q.ElementOfEqualities.Clear();
- foreach (var item in MatchesElement)
- {
- if (!string.IsNullOrWhiteSpace(item.Name1) && !string.IsNullOrWhiteSpace(item.Name2))
- {
- ElementOfEquality elem1 = new ElementOfEquality() { Name = item.Name1 };
- ElementOfEquality elem2 = new ElementOfEquality() { Name = item.Name2 };
- Q.ElementOfEqualities.Add(elem1);
- Q.ElementOfEqualities.Add(elem2);
- connection.Questions.Update(Q);
- connection.SaveChanges();
- RatioOfElementEquality ratio = new RatioOfElementEquality() { IdElement1 = elem1.Id, IdElement2 = elem2.Id };
- connection.RatioOfElementEqualities.Add(ratio);
- }
- }
- }
- else
- connection.Questions.Update(Q);
- connection.SaveChanges();
- await MessageBoxManager.GetMessageBoxStandard("", "Âîïðîñ óñïåøíî ñîõðàíåí", ButtonEnum.Ok).ShowAsync();
- Editing = true;
- Header = "Ðåäàêòèðîâàíèå âîïðîñà";
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âî âðåìÿ äîáàâëåíèÿ âîïðîñà âîçíèêëà îøèáêà.", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.Write(ex.Message);
- #endif
- }
- }
- /// <summary>
- /// Ìåòîä äëÿ ïîëó÷åíèÿ Id êàòåãîðèè âîïðîñà â çàâèñèìîñòè îò âûáðàííûõ çíà÷åíèé ComboBox
- /// </summary>
- /// <returns></returns>
- private int CategoryIdFromSelectedComboBoxItem()
- {
- switch (SelectedCategory)
- {
- case "Âûáîð îòâåòà":
- {
- if (SelectedChoseCategory == "1 ïðàâèëüíîå çíà÷åíèå")
- return 1;
- else
- return 2;
- }
- case "Óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- if (SelectedArrangmentCategory == "ãîðèçîíòàëüíî")
- return 3;
- else
- return 4;
- break;
- }
- case "Ñîîòíîøåíèå":
- {
- if (SelectedMatchCategory == "ãðóïïû ýëåìåíòîâ")
- return 6;
- else
- return 5;
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- return 7;
- }
- default:
- return -1;
- }
- }
- }
- }
|