TestChoosingAnAnswerFromASetViewModel.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 DynamicData;
  9. using Microsoft.EntityFrameworkCore;
  10. using CommunityToolkit.Mvvm.ComponentModel;
  11. namespace EntranseTesting.ViewModels
  12. {
  13. public class TestChoosingAnAnswerFromASetViewModel : ReactiveObject
  14. {
  15. EntranceTestingContext baseConnection = new EntranceTestingContext();
  16. int numberTask;
  17. string question;
  18. ObservableCollection<ItemCAFS> element = new ObservableCollection<ItemCAFS>();
  19. List<QuestionImage> qImage = new List<QuestionImage>();
  20. public TestChoosingAnAnswerFromASetViewModel(int numberTask)
  21. {
  22. this.numberTask = numberTask;
  23. Question = baseConnection.Questions.FirstOrDefault(tb => tb.Id == numberTask).Name;
  24. QImage = baseConnection.QuestionImages.Where(tb => tb.IdQuestion == numberTask).ToList();
  25. List<TextOfPutting> _text = baseConnection.TextOfPuttings.Where(tb => tb.IdQuestion == numberTask).ToList();
  26. foreach (TextOfPutting text in _text)
  27. {
  28. List<string> _list = baseConnection.ElementOfPuttings.Where(tb => tb.IdText == text.Id).Select(tb => tb.Name).ToList();
  29. Random.Shared.Shuffle(CollectionsMarshal.AsSpan(_list));
  30. Element.Add(new ItemCAFS(text.Name,_list));
  31. }
  32. int responseIndex = Response.IndexResponse(numberTask);
  33. if (Response.responseUsers[responseIndex].UserResponseMultiplyAnswers.Count() > 0)//åñëè ïîëüçîâàòåëü îòâå÷àë
  34. {
  35. //çàïîëíÿåì äàííûå
  36. List<TextOfPutting> _list = baseConnection.TextOfPuttings.Include(tb => tb.ElementOfPuttings).Where(tb => tb.IdQuestion == numberTask).ToList();
  37. List<UserResponseMultiplyAnswer> _response = Response.responseUsers[responseIndex].UserResponseMultiplyAnswers.ToList();
  38. for(int i = 0; i < _response.Count; i++)
  39. {
  40. TextOfPutting t = _list.FirstOrDefault(tb => tb.Id == _response[i].IdText);
  41. ElementOfPutting e = baseConnection.ElementOfPuttings.FirstOrDefault(tb => tb.Id == _response[i].IdElement);
  42. int index = Element.IndexOf(Element.FirstOrDefault(tb => tb.Text == t.Name));
  43. Element[index].SelectedItem = e.Name;
  44. }
  45. }
  46. }
  47. public bool QuestionVisible
  48. {
  49. get
  50. {
  51. if (string.IsNullOrWhiteSpace(Question))
  52. return false;
  53. return true;
  54. }
  55. }
  56. public string? Question { get => question; set => question = value; }
  57. public ObservableCollection<ItemCAFS> Element { get => element; set => element = value; }
  58. public List<QuestionImage> QImage { get => qImage; set => qImage = value; }
  59. }
  60. }