public static SurveyReponseDetailViewModel Create(IRepository repository, SurveyResponse surveyResponse)
        {
            Check.Require(repository != null, "Repository must be supplied");
            Check.Require(surveyResponse != null);

            var viewModel = new SurveyReponseDetailViewModel {SurveyResponse = surveyResponse};

            //Get all the related categories that had answers.
            var relatedCategoryIds = viewModel.SurveyResponse.Answers.Select(x => x.Category.Id).Distinct().ToList();
            var bypassedAnswers = viewModel.SurveyResponse.Answers.Where(a => a.BypassScore);
            viewModel.Scores = new List<Scores>();
            foreach (var category in viewModel.SurveyResponse.Survey.Categories.Where(a => !a.DoNotUseForCalculations && relatedCategoryIds.Contains(a.Id)))
            {
                var score = new Scores();
                score.Category = category;
                var totalMax = repository.OfType<CategoryTotalMaxScore>().GetNullableById(category.Id);
                if (totalMax == null) //No Questions most likely
                {
                    continue;
                }
                score.MaxScore = totalMax.TotalMaxScore;
                foreach (var bypassedAnswer in bypassedAnswers.Where(a => a.Category == category))
                {
                    score.MaxScore = score.MaxScore - bypassedAnswer.Question.Responses.Where(a => a.IsActive).Max(a => a.Score);
                }
                //score.MaxScore = repository.OfType<CategoryTotalMaxScore>().GetNullableById(category.Id).TotalMaxScore;
                score.TotalScore = viewModel.SurveyResponse.Answers.Where(a => a.Category == category).Sum(b => b.Score);
                if (score.MaxScore == 0)
                {
                    score.Percent = 100;
                }
                else
                {
                    score.Percent = (score.TotalScore / score.MaxScore) * 100m;
                }
                score.Rank = category.Rank;
                viewModel.Scores.Add(score);
            }

            return viewModel;
        }
Example #2
0
        public void CalculateScores(IRepository repository, SurveyResponse surveyResponse)
        {
            var bypassedAnswers = surveyResponse.Answers.Where(a => a.BypassScore);

            var scores = new List<Scores>();
            foreach (var category in surveyResponse.Survey.Categories.Where(a => !a.DoNotUseForCalculations && a.IsActive && a.IsCurrentVersion))
            {
                var score = new Scores();
                score.Category = category;
                var totalMax = repository.OfType<CategoryTotalMaxScore>().GetNullableById(category.Id);
                if (totalMax == null) //No Questions most likely
                {
                    continue;
                }
                score.MaxScore = totalMax.TotalMaxScore;
                Category category1 = category;
                foreach (var bypassedAnswer in bypassedAnswers.Where(a => a.Category == category1))
                {
                    score.MaxScore = score.MaxScore - bypassedAnswer.Question.Responses.Where(a => a.IsActive).Max(a => a.Score);
                }
                Category category2 = category;
                score.TotalScore =
                    surveyResponse.Answers.Where(a => a.Category == category2).Sum(b => b.Score);
                if (score.MaxScore == 0)
                {
                    //?Don't score it?
                    continue;
                }
                score.Percent = (score.TotalScore / score.MaxScore) * 100m;
                score.Rank = category.Rank;
                scores.Add(score);

            }

            if (scores.OrderByDescending(a => a.Percent)
                .FirstOrDefault() != null)
            {
            surveyResponse.PositiveCategory = scores
                .OrderByDescending(a => a.Percent)
                .ThenBy(a => a.Rank)
                .FirstOrDefault().Category;
            }
            if (scores.Where(a => a.Category != surveyResponse.PositiveCategory)
                .FirstOrDefault() != null)
            {
                surveyResponse.NegativeCategory1 = scores
                .Where(a => a.Category != surveyResponse.PositiveCategory)
                .OrderBy(a => a.Percent)
                .ThenBy(a => a.Rank)
                .FirstOrDefault().Category;
            }
            if (scores.Where(a => a.Category != surveyResponse.PositiveCategory && a.Category != surveyResponse.NegativeCategory1)
                .FirstOrDefault() != null)
            {
                surveyResponse.NegativeCategory2 = scores
                .Where(a => a.Category != surveyResponse.PositiveCategory && a.Category != surveyResponse.NegativeCategory1)
                .OrderBy(a => a.Percent)
                .ThenBy(a => a.Rank)
                .FirstOrDefault().Category;
            }

            //If some are null because not questions are all answers were skipped for that category, still try to put something in.
            if (surveyResponse.PositiveCategory == null)
            {
                surveyResponse.PositiveCategory = surveyResponse.Survey.Categories.Where(a => !a.DoNotUseForCalculations && a.IsActive && a.IsCurrentVersion).OrderBy(a => a.Rank).FirstOrDefault();
            }
            if (surveyResponse.NegativeCategory1 == null)
            {
                surveyResponse.NegativeCategory1 = surveyResponse.Survey.Categories.Where(a => !a.DoNotUseForCalculations && a.IsActive && a.IsCurrentVersion && a != surveyResponse.PositiveCategory).OrderBy(a => a.Rank).FirstOrDefault();
            }
            if (surveyResponse.NegativeCategory2 == null)
            {
                surveyResponse.NegativeCategory2 = surveyResponse.Survey.Categories.Where(a => !a.DoNotUseForCalculations && a.IsActive && a.IsCurrentVersion && a != surveyResponse.PositiveCategory && a != surveyResponse.NegativeCategory1).OrderBy(a => a.Rank).FirstOrDefault();
            }

            return;
        }