Exemplo n.º 1
0
        public ActionResult <TestOutputModel> GetTestAllInfoById(int testId)
        {
            Mapper           mapper       = new Mapper();
            AuthorDataAccess tests        = new AuthorDataAccess();
            var            test           = tests.GetTestById(testId);
            TestStatistics testStatistics = new TestStatistics(testId);

            if (test == null)
            {
                return(BadRequest("Теста не существует"));
            }
            TestOutputModel   model = mapper.ConvertTestDTOToTestOutputModel(tests.GetTestById(testId));
            PassedFailedModel pfs   = testStatistics.GetPassedFailedStats(testId);

            model.Questions     = mapper.ConvertQuestionDTOToQuestionModelList(tests.GetQuestionsByTestID(testId));
            model.Tags          = mapper.ConvertTagDTOToTagModelList(tests.GetTagsInTest(testId));
            model.AverageResult = testStatistics.GetAverageResults(testId);
            model.Passed        = pfs.Passed;
            model.Failed        = pfs.Failed;
            model.SuccessRate   = pfs.SuccessRate;
            foreach (QuestionOutputModel qModel in model.Questions)
            {
                qModel.Answers = mapper.ConvertAnswerDTOToAnswerModelList(tests.GetAnswerByQuestionId(qModel.ID));
                QuestionStatistics statistics = new QuestionStatistics(qModel.ID);
                qModel.PercentageOfCorrectlyAnswered = statistics.GetPercentageOfCorrectlyAnswered(qModel.ID);
                Dictionary <int, double> answersPercent = new Dictionary <int, double>();
                answersPercent = statistics.GetPercentageOfPeopleChoosingAnswer(qModel.ID);
                foreach (var answer in qModel.Answers)
                {
                    foreach (var i in answersPercent)
                    {
                        if (answer.ID == i.Key)
                        {
                            answer.PercentageOfPeopleChoosingAnswer = i.Value;
                        }
                        else
                        {
                            answer.PercentageOfPeopleChoosingAnswer = 0;
                        }
                    }
                }
            }
            return(Ok(model));
        }