Example #1
0
        public async Task <IActionResult> Activate(string id)
        {
            var quiz = await _quizDataContext.ActivateQuizAsync(id);

            quiz ??= QuizViewModel.CreateBlank();

            await _quizHub.Clients.All.Quiz(quiz);

            if (quiz != null)
            {
                var resultsBuilder = new QuizResultBuilder(_quizDataContext);
                var results        = await resultsBuilder.GetResultsAsync(quiz.ID);

                await _quizResultsHub.Clients.Group(HubConstants.Active).SendAsync(HubConstants.ResultsMethod, results);
            }

            return(View(quiz));
        }
Example #2
0
        public async Task QuizResponse(ResponseViewModel quizResponse)
        {
            if (quizResponse == null)
            {
                throw new ArgumentNullException(nameof(quizResponse));
            }

            await _quizDataContext.UpsertResponseAsync(quizResponse);

            await Clients.Caller.Quiz(QuizViewModel.CreateBlank());

            // Submit this response to the reports view
            var resultsBuilder = new QuizResultBuilder(_quizDataContext);
            var results        = await resultsBuilder.GetResultsAsync(quizResponse.ID);

            await _quizResultsHub.Clients.Group(HubConstants.Active).SendAsync(HubConstants.ResultsMethod, results);

            await _quizResultsHub.Clients.Group(quizResponse.ID).SendAsync(HubConstants.ResultsMethod, results);
        }
Example #3
0
        private async Task <QuizViewModel> GetQuizAsync(string id)
        {
            var quiz = await _quizDataContext.GetQuizAsync(id);

            return(quiz ?? QuizViewModel.CreateBlank());
        }
        public async Task <ResultViewModel> GetResultsAsync(string id)
        {
            var quizEntity = await _quizDataContext.GetQuizAsync(id);

            var quiz = quizEntity != null?
                       QuizViewModel.FromJson(quizEntity.Json) :
                           QuizViewModel.CreateBlank();

            var results = new ResultViewModel
            {
                ID    = quiz.ID,
                Title = quiz.Title
            };

            if (quizEntity != null)
            {
                var responseCount = 0;
                var responses     = new Dictionary <string, int>();
                await foreach (var response in _quizDataContext.GetQuizResponsesAsync(id))
                {
                    // Process: "q1=option1,option2;q2=optionA"
                    responseCount++;
                    var userResponses = response.Split(';');
                    foreach (var userResponse in userResponses)
                    {
                        var userResponseString = userResponse.Split('=');
                        var key     = userResponseString[0];
                        var options = userResponseString[1].Split(',');

                        foreach (var option in options)
                        {
                            var answerKey = $"{key}={option}";
                            if (responses.ContainsKey(answerKey))
                            {
                                responses[answerKey]++;
                            }
                            else
                            {
                                responses[answerKey] = 1;
                            }
                        }
                    }
                }

                foreach (var question in quiz.Questions)
                {
                    var list = new List <ResultQuestionAnswerViewModel>();
                    foreach (var option in question.Options)
                    {
                        var key   = $"{question.ID}={option.OptionId}";
                        var count = responses.ContainsKey(key) ? responses[key] : 0;

                        var row = new ResultQuestionAnswerViewModel()
                        {
                            Name  = option.OptionText,
                            Count = count
                        };

                        list.Add(row);
                    }

                    results.Results.Add(new ResultQuestionViewModel()
                    {
                        ID      = question.ID,
                        Title   = question.Title,
                        Answers = list
                    });
                }

                results.Responses = responseCount;
            }

            return(results);
        }