1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using EntranseTesting.Models;
- using System.Collections.ObjectModel;
- using System.Runtime.InteropServices;
- using ReactiveUI;
- using System.Linq;
- using CommunityToolkit.Mvvm.ComponentModel;
- namespace EntranseTesting.ViewModels
- {
- public class TestChoosingMultipleAnswersViewModel : ReactiveObject
- {
- EntranceTestingContext baseConnection = new EntranceTestingContext();
- int numberTask;
- string question;
- ObservableCollection<ElementOfChoose> element = new ObservableCollection<ElementOfChoose>();
- List<QuestionImage> qImage = new List<QuestionImage>();
- public TestChoosingMultipleAnswersViewModel(int numberTask)
- {
- this.numberTask = numberTask;
- Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
- QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
- int responseIndex = Response.IndexResponse(numberTask);
- if (Response.responseUsers[responseIndex].UserResponseChooseAnswers.Count == 0)//åñëè ïîëüçîâàòåëü íå îòâå÷àë
- {
- List<ElementOfChoose> _list = baseConnection.ElementOfChooses.Where(tb => tb.IdQuestion == numberTask).ToList();
- Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list));
- foreach (ElementOfChoose elem in _list)
- Element.Add(elem);
- //çàïèñûâàåì â øàáëîí îòâåòà
- foreach (ElementOfChoose elem in _list)
- Response.responseUsers[responseIndex].UserResponseChooseAnswers.Add(new UserResponseChooseAnswer { IdElement = elem.Id });
- }
- else//åñëè ïîëüçîâàòåëü îòâå÷àë
- {
- //çàïîëíÿåì äàííûå
- List<ElementOfChoose> _list = baseConnection.ElementOfChooses.Where(tb => tb.IdQuestion == numberTask).ToList();
- List<UserResponseChooseAnswer> _response = Response.responseUsers[responseIndex].UserResponseChooseAnswers.ToList();
- foreach (UserResponseChooseAnswer item in _response)
- {
- ElementOfChoose elem = _list.FirstOrDefault(tb => tb.Id == item.IdElement);
- elem.UserCorrectly = (item.Usercorrectly == null) ? false : (bool)item.Usercorrectly;
- Element.Add(elem);
- }
- }
- }
- public string Question { get => question; set => question = value; }
- public ObservableCollection<ElementOfChoose> Element { get => element; set => element = value; }
- public List<QuestionImage> QImage { get => qImage; set => qImage = value; }
- }
- }
|