123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- using Avalonia.Controls;
- using Avalonia.Layout;
- using CommunityToolkit.Mvvm.ComponentModel;
- using CommunityToolkit.Mvvm.Input;
- using EntranseTesting.Models;
- using Microsoft.EntityFrameworkCore;
- using MsBox.Avalonia;
- using MsBox.Avalonia.Enums;
- using ReactiveUI;
- namespace EntranseTesting.ViewModels
- {
- public partial class TestPageViewModel : ObservableObject, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected virtual void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- [ObservableProperty] AppSetting settingTest = new AppSetting();
- int hintCount = 0;
- //êîíòåéíåð äëÿ âîïðîñîâ
- UserControl testUC;
- //äàííûå î òåñòå
- string buttonValue = "Ñëåäóþùèé âîïðîñ";
- [ObservableProperty] int numQuestion = 0;
- [ObservableProperty] int countQuestion = 0;
- bool visibleHint = false;
- List<QuestionHint> hints = new List<QuestionHint>();
- [ObservableProperty] List<Question> questionsCollection;
- List<ItemProgressButton> progressButtons = new List<ItemProgressButton>();
- int takeItem = 5;
- int skipItem = 0;
- List<ItemProgressButton> takeProgressButtons = new List<ItemProgressButton>();
- //view model äëÿ ðàáîòû ñ òåñòîì
- [ObservableProperty] TestArrangementOfElementsViewModel testAE;//ïåðåñòàíîâêà ýëåìåíòà
- [ObservableProperty] TestChoosingAnAnswerFromASetViewModel testCAFS;//ïîäñòàíîâêà îòâåòîâ â òåêñò
- [ObservableProperty] TestChoosing1AnswerViewModel testC1A;//âûáîð 1 âàðèàíòà
- [ObservableProperty] TestChoosingMultipleAnswersViewModel testCMA;//âûáîð íåñêîëüêèõ âàðèàíòîâ îòâåòà
- [ObservableProperty] TestMatchTheElementViewModel testME;//ñîîòíîøåíèå ýëåìåíòîâ
- [ObservableProperty] TestMatchTheValueViewModel testMV;//ñîîòíîøåíèå âåëè÷èí
- public TestPageViewModel()
- {
- EntranceTestingContext baseConnection = new EntranceTestingContext();
- SettingTest = baseConnection.AppSettings.ToList().LastOrDefault();
- if (SettingTest == null)
- {
- SettingTest = new AppSetting()
- {
- Time = new TimeSpan(0, 45, 0),
- CountOfQuestions = 30,
- HintVisibility = false,
- CountOfHints = 0,
- Raiting5 = 30,
- Raiting4 = 23,
- Raiting3 = 16
- };
- }
- CountQuestion = SettingTest.CountOfQuestions;
- //ïîëó÷àåì âåñü ñïèñîê âîïðîñîâ èç áàçû
- QuestionsCollection = baseConnection.Questions.Include(tb => tb.IdCategoryNavigation).Where(tb => tb.InTest == true).ToList();
- Random.Shared.Shuffle(CollectionsMarshal.AsSpan(QuestionsCollection));//ïåðåìåøèâàåì âîïðîñû
- QuestionsCollection = QuestionsCollection.Take(CountQuestion).ToList();//áåðåì íóæíîå êîëè÷åñòâî âîïðîñîâ
- //çàãðóæàåì ïîäñêàçêè ê âîïðîñàì
- Response.loadHints(QuestionsCollection);
- //êíîïêè íàâèãàöèè
- CountQuestion = (QuestionsCollection.Count == SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : (QuestionsCollection.Count > SettingTest.CountOfQuestions) ? SettingTest.CountOfQuestions : QuestionsCollection.Count;
- ProgressButtons.Clear();
- for (int i = 0; i < CountQuestion; i++)
- ProgressButtons.Add(new ItemProgressButton(i + 1));
- ProgressButtons[0].Active = true;
- //çàïîëíÿåì ìàññèâ ñ îòâåòàìè ïîëüçîâàòåëÿ
- Response.responseUsers = new List<UserResponse>();
- for (int i = 0; i < CountQuestion; i++)
- Response.responseUsers.Add(new UserResponse() { IdQuestion = QuestionsCollection[i].Id });
- //íà÷èíàåì òåñò
- NumQuestion = 1;
- TakeItem = InterfaceSettings.take;
- changingPage();
- }
- public List<ItemProgressButton> TakeProgressButtons
- {
- get => ProgressButtons.Skip(SkipItem).Take(TakeItem).ToList();
- }
- public int TakeItem { get => takeItem; set { takeItem = value; OnPropertyChanged("TakeItem"); OnPropertyChanged("TakeProgressButtons"); } }
- public int SkipItem { get => skipItem; set { skipItem = value; OnPropertyChanged("SkipItem"); OnPropertyChanged("TakeProgressButtons"); } }
- public UserControl TestUC { get => testUC; set { testUC = value; OnPropertyChanged("TestUC"); OnPropertyChanged("ProgressText"); OnPropertyChanged("NumQuestion"); } }
- public string ProgressText { get => "{0} èç {3}"; }
- public int HintCount { get => hintCount; set { hintCount = value; OnPropertyChanged("HintCount"); OnPropertyChanged("HintCountLine"); } }
- public string HintCountLine { get => "Êîëè÷åñòâî îñòàâøèõñÿ î÷êîâ " + (SettingTest.CountOfHints - HintCount); }
- public List<QuestionHint> Hints { get => hints; set { hints = value; OnPropertyChanged("Hints"); } }
- public bool VisibleHint { get => visibleHint; set { visibleHint = value; OnPropertyChanged("VisibleHint"); OnPropertyChanged("NoVisibleHint"); } }
- public bool NoVisibleHint { get => !visibleHint; }
- public string ButtonValue { get => buttonValue; set { buttonValue = value; OnPropertyChanged("ButtonValue"); } }
- public List<ItemProgressButton> ProgressButtons { get => progressButtons; set {progressButtons = value; OnPropertyChanged("ProgressButtons"); } }
- public void Next()
- {
- if (TakeProgressButtons.Last() != ProgressButtons.Last())
- SkipItem++;
- }
- public void Back()
- {
- if (TakeProgressButtons.First() != ProgressButtons.First())
- SkipItem--;
- }
- public void changingPage()
- {
- if (QuestionsCollection.Count > 0)
- {
- List<QuestionHint> _listHints = Response.questionHints.Where(tb => tb.IdQuestion == QuestionsCollection[NumQuestion - 1].Id).ToList();
- if (_listHints.Count > 0)
- {
- VisibleHint = true;
- Hints = _listHints.OrderBy(tb => tb.Cost).ToList();
- }
- else
- {
- VisibleHint = false;
- Hints = new List<QuestionHint>();
- }
- switch (QuestionsCollection[NumQuestion - 1].IdCategoryNavigation.Name)
- {
- case "Âîïðîñ ñ 1 âàðèàíòîì îòâåòà":
- {
- TestC1A = new TestChoosing1AnswerViewModel(QuestionsCollection[NumQuestion - 1].Id);
- TestUC = new TestChoosing1Answer();
- break;
- }
- case "Âîïðîñ ñ íåñêîëüêèìè âàðèàíòàìè îòâåòà":
- {
- TestCMA = new TestChoosingMultipleAnswersViewModel(QuestionsCollection[NumQuestion - 1].Id);
- TestUC = new TestChoosingMultipleAnswers();
- break;
- }
- case "Ãîðèçîíòàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Horizontal);
- TestUC = new TestArrangementOfElements();
- break;
- }
- case "Âåðòèêàëüíîå óïîðÿäî÷èâàíèå ýëåìåíòîâ":
- {
- TestAE = new TestArrangementOfElementsViewModel(QuestionsCollection[NumQuestion - 1].Id, Orientation.Vertical);
- TestUC = new TestArrangementOfElements();
- break;
- }
- case "Ñîîòíîøåíèå âåëè÷èí":
- {
- TestMV = new TestMatchTheValueViewModel(QuestionsCollection[NumQuestion - 1].Id);
- TestUC = new TestMatchTheValue();
- break;
- }
- case "Ñîîòíîøåíèå ýëåìåíòîâ":
- {
- TestME = new TestMatchTheElementViewModel(QuestionsCollection[NumQuestion - 1].Id);
- TestUC = new TestMatchTheElement();
- break;
- }
- case "Ïîäñòàíîâêà îòâåòîâ":
- {
- TestCAFS = new TestChoosingAnAnswerFromASetViewModel(QuestionsCollection[NumQuestion - 1].Id);
- TestUC = new TestChoosingAnAnswerFromASet();
- break;
- }
- }
- }
- }
- public async void BuyAHint(int idQHint)
- {
- try
- {
- QuestionHint item = Hints.FirstOrDefault(tb => tb.Id == idQHint);
- if (SettingTest.CountOfHints - HintCount < item.Cost)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "Âàì íå õâàòàåò î÷êîâ äëÿ îòêðûòèÿ ïîäñêàçêè", ButtonEnum.Ok).ShowAsync();
- return;
- }
- var result = await MessageBoxManager.GetMessageBoxStandard("", "Âû äåéñòâèòåëüíî õîòèòå êóïèòü ïîäñêàçêó?\nÑòîèìîñòü ïîäñêàçêè " + item.CostLine + ".\nÏîñëå ïîêóïêè ó âàñ îñòàíåòñÿ " + Response.CostLine(SettingTest.CountOfHints - HintCount - item.Cost), ButtonEnum.YesNo).ShowAsync();
- switch (result)
- {
- case ButtonResult.Yes:
- {
- HintCount += item.Cost;
- int index = Hints.IndexOf(item);
- Hints[index].HintPurchased = true;
- index = Response.questionHints.IndexOf(item);
- Response.questionHints[index].HintPurchased = true;
- int responseIndex = Response.IndexResponse(QuestionsCollection[NumQuestion - 1].Id);
- Response.responseUsers[responseIndex].HintApply = true;
- break;
- }
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- await MessageBoxManager.GetMessageBoxStandard("", "×òî-òî ïîøëî íå òàê", ButtonEnum.Ok).ShowAsync();
- #if DEBUG
- Debug.WriteLine(ex.Message);
- #endif
- }
- }
- }
- }
|