public async Task <StudentGetNextQuestionQueryResult> Handle(StudentGetNextQuestionQuery request, CancellationToken cancellationToken)
        {
            Test currentTest = await _testRepository.GetSingleTestByIdWithIncludes(request.TestId);

            PossibleStatesWithPossibilities existingpossibleStatesWithPossibilities = _possibleStatesWithPossibilitiesRepository.getPossibleStatesWithPossibilitiesForStudent(request.StudentId);
            Enrolement studentEnrolment = await _enrolementRepository.GetByStudentIdAndTestId(request.StudentId, request.TestId);

            if (request.previousAnsweredQuestion.Answers == null)
            {
                studentEnrolment           = new Enrolement();
                studentEnrolment.StudentId = request.StudentId;
                studentEnrolment.TestId    = request.TestId;
                studentEnrolment.Completed = false;
                studentEnrolment           = await _enrolementRepository.CreateEnrolement(studentEnrolment);

                PossibleStatesWithPossibilities possibleStatesWithPossibilities = _possibleStatesWithPossibilitiesRepository.getPossibleStatesWithPossibilities(currentTest.KnowledgeSpaceId);
                existingpossibleStatesWithPossibilities = new PossibleStatesWithPossibilities();
                //existingpossibleStatesWithPossibilities.states = possibleStatesWithPossibilities.states;
                //foreach (KeyValuePair<int, float> entry in possibleStatesWithPossibilities.statePosibilities)
                //{
                //    existingpossibleStatesWithPossibilities.statePosibilities.Add(entry.Key, entry.Value);
                //}
                existingpossibleStatesWithPossibilities.KnowledgeSpaceId = null;
                existingpossibleStatesWithPossibilities.StudentId        = request.StudentId;
                existingpossibleStatesWithPossibilities.Title            = "Maintaining probabilities on KS for student: " + request.StudentId;
                existingpossibleStatesWithPossibilities.states           = new List <Problem>();
                foreach (Problem problem in possibleStatesWithPossibilities.states)
                {
                    Problem newProblem = new Problem();
                    newProblem.ProblemId        = new int();
                    newProblem.KnowledgeSpaceId = problem.KnowledgeSpaceId;
                    newProblem.X               = problem.X;
                    newProblem.Y               = problem.Y;
                    newProblem.Title           = problem.Title;
                    newProblem.statePosibility = problem.statePosibility;
                    var savedProblem = await _knowledgeSpaceRepository.addProblem(newProblem);

                    existingpossibleStatesWithPossibilities.states.Add(savedProblem);
                }
                _possibleStatesWithPossibilitiesRepository.createPossibleStatesWithPossibilities(existingpossibleStatesWithPossibilities);
            }
            if (request.previousAnsweredQuestion.Answers != null)
            {
                updateProbabilities(existingpossibleStatesWithPossibilities, request.previousAnsweredQuestion);
                saveAnsweredQuestion(request.previousAnsweredQuestion, studentEnrolment.EnrolementId);
                _possibleStatesWithPossibilitiesRepository.updatePossibleStatesWithPossibilities(existingpossibleStatesWithPossibilities);
            }

            TestView retVal = new TestView();

            retVal = await getNextQuestion(existingpossibleStatesWithPossibilities, request.StudentId, request.TestId, currentTest);

            return(new StudentGetNextQuestionQueryResult()
            {
                TestToReturn = retVal
            });
        }
        public async Task <StudentGetOneTestQueryResult> Handle(StudentGetOneTestQuery request, CancellationToken cancellationToken)
        {
            var test = await _testRepository.GetSingleTestByIdWithIncludes(request.TestId);

            var testView = new StudentGetOneTestQueryResult.TestView();

            _mapper.Map(test, testView);

            // TODO: Change when (if) we add temporary save
            if (await _enrolementRepository.GetByStudentIdAndTestId(request.UserId.Value, test.TestId) != null)
            {
                testView.Completed = true;
                Enrolement enrolement = await _enrolementRepository.GetByStudentIdAndTestIdWithAnswrs(request.UserId.Value, request.TestId);

                foreach (EnrolementAnswer studentAnswer in enrolement.EnrolementAnswers)
                {
                    System.Diagnostics.Debug.WriteLine(studentAnswer.AnswerId);
                    var question = testView.Questions
                                   .Where(q => q.QuestionId == studentAnswer.QuestionId)
                                   .FirstOrDefault();
                    if (question.SelectedAnswers == null)
                    {
                        question.SelectedAnswers = new List <StudentGetOneTestQueryResult.Answer>();
                    }
                    if (!studentAnswer.Skipped)
                    {
                        question.SelectedAnswers.Add(_mapper.Map(studentAnswer.Answer, new StudentGetOneTestQueryResult.Answer()));
                        question.SelectedAnswers.ToList().ForEach(a => a.Text = "");
                    }
                }
            }
            else
            {
                testView.Questions
                .ToList()
                .ForEach(q =>
                {
                    q.SelectedAnswers = new List <StudentGetOneTestQueryResult.Answer>();
                    q.Answers.ToList().ForEach(a => a.Correct = false);
                });
            }

            var sorted = await SortQuestions(testView);

            var isolated = testView.Questions.Where(q => !sorted.Contains(q)).ToList();

            sorted.AddRange(isolated);

            testView.Questions = sorted;

            return(new StudentGetOneTestQueryResult
            {
                Test = testView
            });
        }
        public async Task <Enrolement> CreateEnrolement(Enrolement enrolement)
        {
            EntityEntry <Enrolement> result = await _context.Enrolements.AddAsync(enrolement);

            _context.SaveChanges();
            if (result != null)
            {
                return(result.Entity);
            }
            return(null);
        }
        protected async Task <TestView> getNextQuestion(PossibleStatesWithPossibilities existingpossibleStatesWithPossibilities, int student_id, int test_id, Test test)
        {
            Enrolement enrolement = await _enrolementRepository.GetByStudentIdAndTestId(student_id, test_id);

            Dictionary <int, float>  questionPossibility = new Dictionary <int, float>();
            List <Entities.Question> forItteration       = enrolement.EnrolementAnswers != null?
                                                           test.Questions.Where(x => enrolement.EnrolementAnswers.Where(t => t.QuestionId.Equals(x.QuestionId)).Count() == 0).ToList()
                                                               :
                                                               test.Questions.ToList();

            foreach (Entities.Question q in forItteration)
            {
                foreach (Problem problem in existingpossibleStatesWithPossibilities.states)
                {
                    List <string> splitedTitle = problem.Title.Split(" ").ToList();
                    if (splitedTitle.Contains(q.ProblemId.ToString()))
                    {
                        float temp = questionPossibility.ContainsKey(q.QuestionId) ? questionPossibility[q.QuestionId] : 0;
                        questionPossibility[q.QuestionId] = (float)(temp + problem.statePosibility);
                    }
                }
            }

            if (forItteration.Count != 0)
            {
                int      maxKeyQuestion = questionPossibility.Aggregate((x, y) => x.Value > y.Value ? x : y).Key;
                TestView retVal         = new TestView();
                _mapper.Map(test, retVal);
                retVal.Questions = new List <StudentGetOneTestQueryResult.Question>();
                retVal.Questions.Add(_mapper.Map <Entities.Question, StudentGetOneTestQueryResult.Question>(forItteration.Find(x => x.QuestionId == maxKeyQuestion)));
                foreach (StudentGetOneTestQueryResult.Question q in retVal.Questions)
                {
                    q.SelectedAnswers = new List <StudentGetOneTestQueryResult.Answer>();
                }

                return(retVal);
            }
            else
            {
                enrolement.Completed = true;
                _enrolementRepository.UpdateEnrolement(enrolement);
                TestView retVal = new TestView();
                _mapper.Map(test, retVal);
                retVal.Questions = new List <StudentGetOneTestQueryResult.Question>();
                foreach (StudentGetOneTestQueryResult.Question q in retVal.Questions)
                {
                    q.SelectedAnswers = new List <StudentGetOneTestQueryResult.Answer>();
                }
                return(retVal);
            }
        }
        public async Task <SubmitTestCommandResult> Handle(SubmitTestCommand request, CancellationToken cancellationToken)
        {
            Enrolement enr = new Enrolement();

            enr.StudentId = request.UserId.Value;
            enr.TestId    = request.Test.TestId;
            enr.Completed = true;
            Enrolement enrolement = await _enrolementRepository.CreateEnrolement(enr);


            foreach (SubmitTestQuestion q in request.Test.Questions)
            {
                if (q.SelectedAnswers?.Count > 0)
                {
                    foreach (SubmitTestAnswer a in q.SelectedAnswers)
                    {
                        EnrolementAnswer enrAnsw = new EnrolementAnswer();
                        enrAnsw.EnrolementId = enrolement.EnrolementId;
                        enrAnsw.QuestionId   = q.QuestionId;
                        enrAnsw.AnswerId     = a.AnswerId;
                        await _enrolementAnswerRepository.CreateEnrolementAnswer(enrAnsw);
                    }
                }
                else
                {
                    EnrolementAnswer enrAnsw = new EnrolementAnswer();
                    enrAnsw.EnrolementId = enrolement.EnrolementId;
                    enrAnsw.QuestionId   = q.QuestionId;
                    enrAnsw.Skipped      = true;
                    await _enrolementAnswerRepository.CreateEnrolementAnswer(enrAnsw);
                }
            }

            return(new SubmitTestCommandResult
            {
                EnrolementId = enrolement.EnrolementId,
                Success = true,
            });
        }
 public void UpdateEnrolement(Enrolement enrolement)
 {
     _context.Enrolements.Update(enrolement);
     _context.SaveChanges();
 }