TestChoosingMultipleAnswersViewModel.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System;
  2. using System.Collections.Generic;
  3. using EntranseTesting.Models;
  4. using System.Collections.ObjectModel;
  5. using System.Runtime.InteropServices;
  6. using ReactiveUI;
  7. using System.Linq;
  8. using CommunityToolkit.Mvvm.ComponentModel;
  9. namespace EntranseTesting.ViewModels
  10. {
  11. public class TestChoosingMultipleAnswersViewModel : ReactiveObject
  12. {
  13. EntranceTestingContext baseConnection = new EntranceTestingContext();
  14. int numberTask;
  15. string question;
  16. ObservableCollection<ElementOfChoose> element = new ObservableCollection<ElementOfChoose>();
  17. List<QuestionImage> qImage = new List<QuestionImage>();
  18. public TestChoosingMultipleAnswersViewModel(int numberTask)
  19. {
  20. this.numberTask = numberTask;
  21. Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
  22. QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
  23. int responseIndex = Response.IndexResponse(numberTask);
  24. if (Response.responseUsers[responseIndex].UserResponseChooseAnswers.Count == 0)//åñëè ïîëüçîâàòåëü íå îòâå÷àë
  25. {
  26. List<ElementOfChoose> _list = baseConnection.ElementOfChooses.Where(tb => tb.IdQuestion == numberTask).ToList();
  27. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list));
  28. foreach (ElementOfChoose elem in _list)
  29. Element.Add(elem);
  30. //çàïèñûâàåì â øàáëîí îòâåòà
  31. foreach (ElementOfChoose elem in _list)
  32. Response.responseUsers[responseIndex].UserResponseChooseAnswers.Add(new UserResponseChooseAnswer { IdElement = elem.Id });
  33. }
  34. else//åñëè ïîëüçîâàòåëü îòâå÷àë
  35. {
  36. //çàïîëíÿåì äàííûå
  37. List<ElementOfChoose> _list = baseConnection.ElementOfChooses.Where(tb => tb.IdQuestion == numberTask).ToList();
  38. List<UserResponseChooseAnswer> _response = Response.responseUsers[responseIndex].UserResponseChooseAnswers.ToList();
  39. foreach (UserResponseChooseAnswer item in _response)
  40. {
  41. ElementOfChoose elem = _list.FirstOrDefault(tb => tb.Id == item.IdElement);
  42. elem.UserCorrectly = (item.Usercorrectly == null) ? false : (bool)item.Usercorrectly;
  43. Element.Add(elem);
  44. }
  45. }
  46. }
  47. public string Question { get => question; set => question = value; }
  48. public ObservableCollection<ElementOfChoose> Element { get => element; set => element = value; }
  49. public List<QuestionImage> QImage { get => qImage; set => qImage = value; }
  50. }
  51. }