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
            });
        }
        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);
            }
        }
        protected async Task <int> determinePosibilitiesForAllStates(KnowledgeSpace ksWithAllStates, int realKsId, Dictionary <int, int> reverseMap, HttpClient httpClient)
        {
            int count = 0;
            PossibleStatesWithPossibilities possibleStatesWithPossibilities = new PossibleStatesWithPossibilities();

            foreach (var state in ksWithAllStates.Problems)
            {
                List <int> oneStateList = new List <int>();
                foreach (string oneValueInState in state.Title.Split(' '))
                {
                    int index;
                    if (int.TryParse(oneValueInState, out index))
                    {
                        oneStateList.Add(reverseMap[int.Parse(oneValueInState)]);
                    }
                }
                var json    = JsonConvert.SerializeObject(oneStateList);
                var content = new StringContent(json, Encoding.UTF8, "application/json");

                var result = await httpClient.PostAsync("http://localhost:8000/kst/probability_for_state", content);

                var a = await result.Content.ReadAsStringAsync();

                dynamic someObject = JsonConvert.DeserializeObject <dynamic>(a);
                var     numberOfStudentsInState = JsonConvert.DeserializeObject <int>(someObject);
                possibleStatesWithPossibilities.statePosibilities.Add(state.ProblemId, (float)numberOfStudentsInState);
                count += numberOfStudentsInState;
            }
            foreach (KeyValuePair <int, float> entry in possibleStatesWithPossibilities.statePosibilities)
            {
                possibleStatesWithPossibilities.statePosibilities[entry.Key] = entry.Value / count;
            }
            possibleStatesWithPossibilities.states           = ksWithAllStates.Problems;
            possibleStatesWithPossibilities.KnowledgeSpaceId = realKsId;
            possibleStatesWithPossibilities.StudentId        = null;
            possibleStatesWithPossibilities.Title            = "Mapping knowlegde states to possibilities for knowledge states: " + ksWithAllStates.KnowledgeSpaceId;
            foreach (Problem p in possibleStatesWithPossibilities.states)
            {
                p.statePosibility = possibleStatesWithPossibilities.statePosibilities[p.ProblemId];
                _knowledgeSpaceRepository.updateProblem(p);
            }
            _possibleStatesWithPossibilitiesRepository.createPossibleStatesWithPossibilities(possibleStatesWithPossibilities);
            return(0);
        }
        protected void updateProbabilities(PossibleStatesWithPossibilities existingpossibleStatesWithPossibilities, StudentGetOneTestQueryResult.Question answeredQuestion)
        {
            List <int> realAnswersCorrect = new List <int>();

            foreach (StudentGetOneTestQueryResult.Answer a in answeredQuestion.Answers)
            {
                Entities.Answer realAnswer = _answerRepository.getAnswerById(a.AnswerId);
                if (realAnswer.Correct)
                {
                    realAnswersCorrect.Add(realAnswer.AnswerId);
                }
            }
            int flag = 0;

            foreach (StudentGetOneTestQueryResult.Answer studentsAnswer in answeredQuestion.SelectedAnswers)
            {
                if (realAnswersCorrect.Contains(studentsAnswer.AnswerId))
                {
                    flag += 1;
                }
            }
            if (realAnswersCorrect.Count() == flag)
            {
                foreach (Problem state in existingpossibleStatesWithPossibilities.states)
                {
                    List <string> splitedTitle = state.Title.Split(" ").ToList();
                    if (splitedTitle.Contains(answeredQuestion.ProblemId.ToString()))
                    {
                        state.statePosibility = (float)(state.statePosibility * 1.3);
                    }
                }
            }
            else
            {
                foreach (Problem state in existingpossibleStatesWithPossibilities.states)
                {
                    List <string> splitedTitle = state.Title.Split(" ").ToList();
                    if (splitedTitle.Contains(answeredQuestion.ProblemId.ToString()))
                    {
                        state.statePosibility = (float)(state.statePosibility * 0.6);
                    }
                }
            }
        }
 public void updatePossibleStatesWithPossibilities(PossibleStatesWithPossibilities possibleStatesWithPossibilities)
 {
     _context.PossibleStatesWithPossibilities.Update(possibleStatesWithPossibilities);
     _context.SaveChanges();
 }
 public void createPossibleStatesWithPossibilities(PossibleStatesWithPossibilities possibleStatesWithPossibilities)
 {
     _context.PossibleStatesWithPossibilities.Add(possibleStatesWithPossibilities);
     _context.SaveChanges();
 }