Exemple #1
0
        public ActionResult TestQuestion(PerQuestionView view)
        {
            //saving session
            var testSave = (TestSave)Session["save"];

            view.Question = cx.Questions.Find(view.Question.Id);
            testSave.Update(view);
            Session["save"] = testSave;

            //choosing action
            helper.ChooseAction(
                view.Question,
                Request.Form["btnSubmit"],
                testSave,
                out bool isFinish, out Question nextQuestion);

            //finish view
            if (isFinish)
            {
                var finishView = new FinishView()
                {
                    Quiz = view.Question.Quiz,
                };

                var usedSections = new List <Section>();

                foreach (var question in testSave.QuestionSaves.
                         Select(x => cx.Questions.Find(x.Key)).
                         ToList())
                {
                    if (usedSections.Any(x => x.Id == question.Section.Id))
                    {
                        continue;
                    }
                    usedSections.Add(question.Section);
                }

                var sectionAnswersList = new List <SectionAnswersView>();

                foreach (var section in usedSections)
                {
                    var sectionAnswers = new SectionAnswersView()
                    {
                        Section = section,
                        QuestionIndex_IsInit = new Dictionary <int, bool>()
                    };

                    int index = 1;

                    var sectionIds = cx.Questions.
                                     Where(x => x.Section.Id == section.Id).
                                     Select(y => y.Id).ToList();

                    foreach (var questionAnswer in testSave.QuestionSaves.
                             Where(x => sectionIds.Contains(x.Key)).ToList())
                    {
                        sectionAnswers.QuestionIndex_IsInit.Add(
                            index,
                            ((IParseAnswer)questionAnswer.Value.Answer).IsValid()
                            );
                        index++;
                    }

                    sectionAnswersList.Add(sectionAnswers);
                }

                finishView.SectionAnswers = sectionAnswersList;

                ModelState.Clear();

                bool isEnded = false;

                if (Request.Form["btnSubmit"] == "QuizTime" ||
                    Request.Form["btnSubmit"] == "QuestionTime")
                {
                    isEnded = true;
                }

                ViewBag.isEnded = isEnded;

                return(PartialView("Finish", finishView));
            }

            //create model
            PerQuestionView model = null;

            if (testSave.QuestionSaves.ContainsKey(nextQuestion.Id))
            {
                model = testSave.QuestionSaves[nextQuestion.Id].View;
            }
            else
            {
                model = new PerQuestionView()
                {
                    Question = nextQuestion,
                };

                model.Test = helper.CreateTestView(nextQuestion);
            }

            model = helper.SetNavigation(model, nextQuestion, testSave);

            ModelState.Clear();

            return(PartialView("TestQuestion", model));
        }
Exemple #2
0
        /// <summary>
        /// For perSection testing
        /// Saving sections in Session
        /// </summary>
        public ActionResult TestSection(SectionView view)
        {
            //saving session

            var testSave = (TestSave)Session["save"];

            view.Section = cx.Sections.Find(view.SectionId);

            testSave.Update(view);

            Session["save"] = testSave;

            //choosing action

            var quiz = cx.Quizzes.Find(view.QuizId);

            var prevSection = cx.Sections.Find(view.SectionId);

            helper.ChooseAction(prevSection, Request.Form["btnSubmit"], testSave,
                                out bool isFinish, out Section section);

            //finish view
            if (isFinish)
            {
                var finishView = new FinishView()
                {
                    Quiz = quiz,
                };

                var sectionAnswersList = new List <SectionAnswersView>();

                foreach (var item in testSave.Saves)
                {
                    var sectionAnswers = new SectionAnswersView()
                    {
                        Section = cx.Sections.Find(item.Key),
                        QuestionIndex_IsInit = new Dictionary <int, bool>()
                    };

                    int index = 1;

                    foreach (var questionAnswer in item.Value.Answers)
                    {
                        sectionAnswers.QuestionIndex_IsInit.Add(
                            index,
                            ((IParseAnswer)questionAnswer.Value).IsValid()
                            );
                        index++;
                    }

                    sectionAnswersList.Add(sectionAnswers);
                }

                finishView.SectionAnswers = sectionAnswersList;

                ModelState.Clear();

                bool isEnded = false;

                if (Request.Form["btnSubmit"] == "QuizTime" || Request.Form["btnSubmit"] == "SectionTime")
                {
                    isEnded = true;
                }

                ViewBag.isEnded = isEnded;

                return(PartialView("Finish", finishView));
            }

            //creating model

            SectionView model = null;

            if (testSave.Saves.ContainsKey(section.Id))
            {
                model = testSave.Saves[section.Id].View;
            }
            else
            {
                model = new SectionView()
                {
                    QuizId    = quiz.Id,
                    SectionId = section.Id,
                    Section   = section
                };

                var questions = helper.GetRandomSectionQuestions(section.Id);

                model.Tests = helper.CreateTestViews(questions, quiz);
            }

            model = helper.SetNavigation(model, section, quiz);

            ModelState.Clear();

            return(PartialView("TestSection", model));
        }