Esempio n. 1
0
        private void GetCorrectAnswersFromQuestions()
        {
            uint numberQuestion = 1;

            foreach (Question question in Questions)
            {
                List <string> answers = new List <string>()
                {
                    question.Answer1, question.Answer2, question.Answer3, question.Answer4,
                    question.Answer5, question.Answer6, question.Answer7, question.Answer8
                };
                List <bool> answersBool = new List <bool>()
                {
                    question.Answer1Bool == true?true:false, question.Answer2Bool == true?true:false, question.Answer3Bool == true?true:false, question.Answer4Bool == true?true:false,
                    question.Answer5Bool == true?true:false, question.Answer6Bool == true?true:false, question.Answer7Bool == true?true:false, question.Answer8Bool == true?true:false
                };
                List <string> ansCor = new List <string>();
                for (int i = 0; i < answersBool.Count; i++)
                {
                    if (answersBool[i])
                    {
                        ansCor.Add(answers[i]);
                    }
                }

                AllAnswers.Add(numberQuestion, answers);
                CorrectAnswers.Add(numberQuestion, answersBool);
                AnswersCorrect.Add(numberQuestion, ansCor);
                numberQuestion++;
            }
        }
        public async Task SetCurrentTestResults(int?id)
        {
            try
            {
                // I. Clear controller properties for processing a test period.
                //TestPeriod = 0;
                //StartTestTime = null;

                // II. Check.
                OperationDetails operationDetails;
                string           currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId();
                if (currentUserId == null || AllAnswers.Count == 0 || UserAnswers.Count == 0)
                {
                    return;
                }

                // III.Get the test result.
                operationDetails = PLRepository.CalculateTestResults(AllAnswers, UserAnswers, TestQuetions, out TestResultViewModel testResult);
                if (!operationDetails.Succedeed)
                {
                    return;
                }
                // Is the test passed?
                int       firstQuestionId = AllAnswers.FirstOrDefault().Key;
                CourseDTO courseDTO       = (await QuestionService.GetAsync(firstQuestionId)).Topic.Course;
                testResult.IsPassedTest = testResult.Result > testResult.MaxScore * courseDTO.PassingScore / 100;

                // IV. Write the test results to DB.
                operationDetails = await SaveCurrentTestResults(id, currentUserId, testResult);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Esempio n. 3
0
    // Use this for initialization
    void Start()
    {
        injector = this.GetComponent <QuizInjector> ();
        qea      = this.GetComponent <AllAnswers> ();
        var idLevel = PlayerPrefs.GetInt("idLevel");

        var quiz = injector.quizes [idLevel - 1 < 0 ? 0 : idLevel - 1];

        foreach (var question in quiz.questions)
        {
            qea.questions.Add(question.text);
            qea.alternativeA.Add(question.answerA);
            qea.alternativeB.Add(question.answerB);
            qea.alternativeC.Add(question.answerC);
            qea.alternativeD.Add(question.answerD);
            qea.corrects.Add(question.textFromCorrectAnswer());
        }
    }
        // Get test results.
        public async Task <ActionResult> GetCurrentTestResults(int?id)
        {
            try
            {
                // I. Clear controller properties for processing a test period.
                TestPeriod    = 0;
                StartTestTime = null;

                // II. Check.
                if (AllAnswers.Count == 0 || UserAnswers.Count == 0)
                {
                    return(RedirectToAction("Index"));
                }

                // III.Get the test result.
                OperationDetails operationDetails = PLRepository.CalculateTestResults(AllAnswers, UserAnswers, TestQuetions, out TestResultViewModel testResult);
                if (!operationDetails.Succedeed)
                {
                    return(View("Report", operationDetails));
                }
                // Is the test passed?
                int       firstQuestionId = AllAnswers.FirstOrDefault().Key;
                CourseDTO courseDTO       = (await QuestionService.GetAsync(firstQuestionId)).Topic.Course;
                testResult.IsPassedTest = testResult.Result > testResult.MaxScore * courseDTO.PassingScore / 100;
                // Set ViewBag property for a View.
                ViewBag.CourseName = courseDTO.CourseTitle;

                // IV.Set testResult properies (Result, MaxScore and TestResultDetails).
                testResult.Result   = testResult.Result * 1000 / testResult.MaxScore;
                testResult.MaxScore = 1000;
                foreach (var item in testResult.TestResultDetails)
                {
                    item.Topic    = (await QuestionService.GetAsync(item.QuestionId)).Topic.TopicTitle;
                    item.Question = (await QuestionService.GetAsync(item.QuestionId)).QuestionText;
                }

                // V.
                return(View(testResult));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        // Start a topic test.
        public async Task <ActionResult> TopicTest(int?id)
        {
            try
            {
                // I.Checks.
                // Check id.
                if (!int.TryParse(id.ToString(), out int intId))
                {
                    return(RedirectToAction("Index"));
                }
                // Get courseDTO object.
                TopicDTO topicDTO = await TopicService.GetAsync(intId);

                if (topicDTO == null)
                {
                    return(RedirectToAction("Index"));
                }
                //Get courseDTO object.
                CourseDTO courseDTO = await CourseService.GetAsync(topicDTO.CourseId);

                if (courseDTO == null)
                {
                    return(RedirectToAction("Index"));
                }

                // II. Set ViewBag properties.
                ViewBag.CourseName = courseDTO.CourseTitle;
                ViewBag.TopicName  = topicDTO.TopicTitle;
                ViewBag.TopicId    = intId;

                // III. AutoMapper Setup.
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <QuestionDTO, UserQuestionAnswersViewModel>()
                    .ForMember("Question", opt => opt.MapFrom(obj => obj.QuestionText))
                    .ForMember("AnswerType", opt => opt.MapFrom(obj => obj.AnswerType.AnswerTypeDescription))
                    .ForMember("AvailableUserAnswers", opt => opt.MapFrom(obj => obj.Answers))
                    .ForMember("QuestionWeight", opt => opt.MapFrom(obj => obj.QuestionWeight))
                    .ForMember(q => q.SelectedUserAnswers, option => option.Ignore())
                    .ForMember(q => q.PostedUserAnswers, option => option.Ignore());
                    cfg.CreateMap <AnswerDTO, UserAnswer>()
                    .ForMember("Id", opt => opt.MapFrom(obj => obj.AnswerId))
                    .ForMember("Answer", opt => opt.MapFrom(obj => obj.AnswerText))
                    .ForMember("IsSelected", opt => opt.MapFrom(obj => obj.IsProper));
                });
                IMapper iMapper = config.CreateMapper();

                // IV. Get data for a view.
                IList <QuestionDTO> source = await TestingService.GetRandomQuestionsForTopic(intId);

                IEnumerable <UserQuestionAnswersViewModel> topicTestQuestionList = iMapper.Map <IEnumerable <QuestionDTO>, IEnumerable <UserQuestionAnswersViewModel> >(source);

                // V. Set properties: TestQuetions, AllAnswers and UserAnswers.
                TestQuetions = topicTestQuestionList.ToList();
                AllAnswers.Clear();
                UserAnswers.Clear();
                foreach (var item in topicTestQuestionList)
                {
                    AllAnswers[item.QuestionId]  = item.AvailableUserAnswers.ToList();
                    UserAnswers[item.QuestionId] = new List <UserAnswer>();
                }

                // VI. Set Timer.
                TestPeriod    = courseDTO.TimeToAnswerOneQuestion * topicTestQuestionList.Count();
                StartTestTime = DateTime.Now;

                // VII.
                return(View(topicTestQuestionList));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }