public IActionResult QuestionAnswer(string questionAnswer, int questionId)
        {
            Console.WriteLine(questionId);
            Console.WriteLine(questionAnswer);
            var user         = User.GetUserId();
            var thisQuestion = _db.Questions.Include(questions => questions.Quiz).FirstOrDefault(questions => questions.QuestionId == questionId);
            var thisQuiz     = _db.CompletedQuizzes.FirstOrDefault(quizzes => quizzes.UserId == user && quizzes.QuizId == thisQuestion.QuizId);
            CompletedQuestion newCompletedQuestion = new CompletedQuestion(thisQuiz.CompletedQuizId, user, thisQuestion.QuestionId, questionAnswer, thisQuestion.CorrectAnswer);

            _db.CompletedQuestions.Add(newCompletedQuestion);
            _db.SaveChanges();
            return(Json(newCompletedQuestion.QuestionAnswer));
        }
        public ICollection <CompletedQuestion> MapQuestionsFromDto(IEnumerable <CompletedQuestionDto> questionsDto, SurveyDbContext context)
        {
            var completedQuestions = new List <CompletedQuestion>();

            foreach (var q in questionsDto)
            {
                var question = context.Questions.Where(m => m.QuestionId == q.QuestionId)
                               .Include(m => m.QuestionType)
                               .Include(m => m.QuestionGroup)
                               .First();
                var completedQuestion = new CompletedQuestion(question, q.Answer);
                completedQuestions.Add(completedQuestion);
            }

            return(completedQuestions);
        }
        public void TestAddCompletedQuestionToCompletedSurvey()
        {
            var options = SqliteInMemory.CreateOptions <SurveyDbContext>();

            using (var context = new SurveyDbContext(options))
            {
                context.Database.EnsureCreated();
                context.SeedDataBaseWithSurveys();

                var completedSurvey   = context.CompletedSurveys.First();
                var type              = new QuestionType("Text");
                var question          = new Question("How has your sleep pattern been?", type);
                var completedQuestion = new CompletedQuestion(question, "It's not been great.", completedSurvey);

                var status = completedSurvey.AddQuestion(completedQuestion, context);
                status.Errors.ShouldBeEmpty();
                status.HasErrors.ShouldBeFalse();
                status.Message.ShouldEqual("Success");
                context.SaveChanges();

                completedSurvey.CompletedQuestions.Count().ShouldEqual(2);
            }
        }
        private void StartQuizButton_Click(object sender, EventArgs e)
        {
            //Hides the main form
            this.Hide();

            CalculateDifficulty cd = new CalculateDifficulty(); //Claculated difficulty is initilised

            //Trys to decide which option the user wants.
            //Adaptive question order: Questions answered incorrectly are presented more often than the correct ones
            //Standard: Questions are presented normally
            //If the user hasn`t selected an option a null exception is thrown
            try
            {
                if (SelectModeComboBox.SelectedItem.ToString() == "Adaptive Questions Order")
                {
                    foreach (CompletedQuestion cq in completedQuestion)
                    {
                        if ((cq.CalculatedDifficulty > 80) && (cq.XCompleted > 5))
                        {
                            //This removes the question from the quiz so that the user doesn`t answer this question that they have already answered correctly the majority of times
                            storedQuizQuestions.Remove(storedQuizQuestions.Find(x => x.QuestionId == cq.QuestionId));
                        }
                    }
                }
            }
            catch (System.NullReferenceException)
            {
                //The null excpetion is caught and this message is displayed
                MessageBox.Show("Please Select A Question Mode", "Error", MessageBoxButtons.OK);
                return;
            }

            //The stored quiz question need to be shuffled so this line of code does that
            List <StoredQuizQuestions> ShuffledQuizQuestions = storedQuizQuestions.OrderBy(x => Guid.NewGuid()).ToList();

            foreach (StoredQuizQuestions QuizQuestion in ShuffledQuizQuestions)
            {
                StoredQuestions CurrentQuestion = (storedQuestions.Find(x => x.QuestionId == QuizQuestion.QuestionId)); //The current question is saved to the varaible called current question

                CompletedQuestion CurrentCompletedQuestion = (completedQuestion.Find(x => x.QuestionId == QuizQuestion.QuestionId));

                if (CurrentQuestion.PictureUrl == "")                  //If there is no picture URL then it doesn`t have a picture
                {
                    var page2 = new TextQuestionForm(CurrentQuestion); //The next form is created

                    page2.Answered += (source, Correct) =>             //This event is used to return the answer that the user selects and treats it accordingly based upon if the correct bool is true or false
                    {
                        //Question is removed from both storedquestion and completedquestion
                        storedQuestions.Remove(CurrentQuestion);
                        completedQuestion.Remove(CurrentCompletedQuestion);

                        //If the user has ansewred correctly then the current quiz question`s XAnsweredCorrect will increase by one
                        if (Correct == true)
                        {
                            CurrentQuestion.XAnsweredCorrectly++;
                            CurrentCompletedQuestion.XCorrect++;
                        }

                        //Regardless of if the answer was answered correctly the times answered counter must also increment by one
                        CurrentQuestion.XAnswered++;
                        CurrentCompletedQuestion.XCompleted++;

                        //The difficulty rating must be recalculated
                        CurrentQuestion.CalculatedDifficulty          = cd.CalcDifficulty(CurrentQuestion.XAnswered, CurrentQuestion.XAnsweredCorrectly);
                        CurrentCompletedQuestion.CalculatedDifficulty = cd.CalcDifficulty(CurrentCompletedQuestion.XCompleted, CurrentCompletedQuestion.XCorrect);

                        //The question is added back to the storedquestion and complted question lists
                        storedQuestions.Add(CurrentQuestion);
                        completedQuestion.Add(CurrentCompletedQuestion);
                    };

                    //Displays the question answering form
                    page2.ShowDialog();
                }
                else
                {
                    var page2 = new PictureQuestionForm(CurrentQuestion);

                    page2.Answered += (source, Correct) =>
                    {
                        //Question is removed from both storedquestion and completedquestion
                        storedQuestions.Remove(CurrentQuestion);
                        completedQuestion.Remove(CurrentCompletedQuestion);

                        //If the user has ansewred correctly then the current quiz question`s XAnsweredCorrect will increase by one
                        if (Correct == true)
                        {
                            CurrentQuestion.XAnsweredCorrectly++;
                            CurrentCompletedQuestion.XCorrect++;
                        }

                        //Regardless of if the answer was answered correctly the times answered counter must also increment by one
                        CurrentQuestion.XAnswered++;
                        CurrentCompletedQuestion.XCompleted++;

                        //The difficulty rating must be recalculated
                        CurrentQuestion.CalculatedDifficulty          = cd.CalcDifficulty(CurrentQuestion.XAnswered, CurrentQuestion.XAnsweredCorrectly);
                        CurrentCompletedQuestion.CalculatedDifficulty = cd.CalcDifficulty(CurrentCompletedQuestion.XCompleted, CurrentCompletedQuestion.XCorrect);

                        //The question is added back to the storedquestion and complted question lists
                        storedQuestions.Add(CurrentQuestion);
                        completedQuestion.Add(CurrentCompletedQuestion);
                    };

                    //Displays the question answering form
                    page2.ShowDialog();
                }
            }
            //Question class is initilized
            QuestionClass qc = new QuestionClass();

            //The method update scored is called in order to refresh the scored that are in the database
            qc.UpdateScores(storedQuestions, completedQuestion);

            this.Show();
        }