Exemplo n.º 1
0
 public QuestionStat GetQuestionStats(long questionId)
 {
     using (var redis = RedisManager.GetReadOnlyClient())
     {
         var result = new QuestionStat
         {
             VotesUpCount   = (int)redis.GetSetCount(QuestionUserIndex.UpVotes(questionId)),
             VotesDownCount = (int)redis.GetSetCount(QuestionUserIndex.DownVotes(questionId))
         };
         result.VotesTotal = result.VotesUpCount - result.VotesDownCount;
         return(result);
     }
 }
Exemplo n.º 2
0
		public QuestionStat GetQuestionStats(long questionId)
		{
			using (var redis = RedisManager.GetReadOnlyClient())
			{
				var result = new QuestionStat
				{
					VotesUpCount = redis.GetSetCount(QuestionUserIndex.UpVotes(questionId)),
					VotesDownCount = redis.GetSetCount(QuestionUserIndex.DownVotes(questionId))
				};
				result.VotesTotal = result.VotesUpCount - result.VotesDownCount;
				return result;
			}
		}
Exemplo n.º 3
0
        /// <summary>
        /// This is for Endpoint 3 as discussed on Discord
        /// </summary>
        /// <returns></returns>
        public QuizStat[] GetQuizStats()
        {
            Mutex.WaitOne();
            List <Controller.Database.Tables.Quiz> quizzes = GetDbSet <Controller.Database.Tables.Quiz>()
                                                             .Include(quiz => quiz.Questions)
                                                             .ThenInclude(questionQuiz => questionQuiz.Question)
                                                             .ThenInclude(question => question.Questions)
                                                             .ThenInclude(questionAttempt => questionAttempt.Account)
                                                             .Include(quiz => quiz.Questions)
                                                             .ThenInclude(questionQuiz => questionQuiz.Question)
                                                             .ThenInclude(question => question.Answers)
                                                             .Include(quiz => quiz.Sessions)
                                                             .ThenInclude(session => session.Account)
                                                             .Where(quiz => quiz.IsCustom == 1 && quiz.Sessions.Count > 0).ToList();

            // Process the data now
            QuizStat[] quizStats = new QuizStat[quizzes.Count];
            for (int i = 0; i < quizStats.Length; i++)
            {
                Controller.Database.Tables.Quiz quiz        = quizzes.ElementAt(i);
                Dictionary <string, Student>    StudentStat = new Dictionary <string, Student>();
                List <int> questionList = quiz.Questions.Select(quizQuestion => quizQuestion.QuestionId).ToList();
                foreach (Session session in quiz.Sessions)
                {
                    if (!StudentStat.ContainsKey(session.AccountId))
                    {
                        StudentStat.Add(session.AccountId, new Student(questionList, session.Account));
                    }
                }
                List <QuestionStat> questionStats = new List <QuestionStat>();
                List <string>       sessions      = quiz.Sessions.Select(x => x.Id).ToList();
                var quizStat = new QuizStat();
                quizStat.QuizName = quiz.Name;
                int questionCount = 0;
                foreach (QuestionQuiz questionQuiz in quiz.Questions)
                {
                    QuestionStat                        questionStat   = new QuestionStat();
                    List <QuestionAttempted>            allAttempt     = new List <QuestionAttempted>();
                    Controller.Database.Tables.Question questionEntity = questionQuiz.Question;
                    questionStat.QuestionId   = ++questionCount;
                    questionStat.QuestionText = questionEntity.Text;
                    var questionAttemptedForQuiz = questionEntity.Questions.Where(x => sessions.Contains(x.AttemptedFor)).ToList();
                    var individualAttempts       = questionAttemptedForQuiz.GroupBy(x => x.AttemptedBy);
                    foreach (var studentAttempt in individualAttempts)
                    {
                        QuestionAttempted qa = studentAttempt.Last();
                        allAttempt.Add(qa);
                        StudentStat[qa.Account.Id].QuestionsAnswers[qa.QuestionId] = qa.AnswerId;
                        if (qa.Answer.IsCorrect == 1)
                        {
                            StudentStat[qa.Account.Id].NumOfCorrect += 1;
                        }
                    }
                    questionStat.CorrectAnswerCount = allAttempt.Where(attempt => attempt.Answer.IsCorrect == 1).Count();
                    questionStat.CorrectAnswer      = questionEntity.Answers.Where(answer => answer.IsCorrect == 1).First().Text;
                    List <Controller.Database.Tables.Answer> wrongAnswers = questionEntity.Answers.Where(answer => answer.IsCorrect == 0).OrderBy(answer => answer.Id).ToList();

                    questionStat.WrongAnswerOneCount   = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(0).Id).Count();
                    questionStat.WrongAnswerOne        = wrongAnswers.ElementAt(0).Text;
                    questionStat.WrongAnswerTwoCount   = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(1).Id).Count();
                    questionStat.WrongAnswerTwo        = wrongAnswers.ElementAt(1).Text;
                    questionStat.WrongAnswerThreeCount = allAttempt.Where(attempt => attempt.AnswerId == wrongAnswers.ElementAt(2).Id).Count();
                    questionStat.WrongAnswerThree      = wrongAnswers.ElementAt(2).Text;
                    questionStats.Add(questionStat);
                }
                quizStat.QuestionStats    = questionStats.ToArray();
                quizStat.StudentAttempted = StudentStat.Values.ToArray();
                quizStats[i] = quizStat;
            }
            Mutex.ReleaseMutex();
            return(quizStats);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Stats(int id)
        {
            var user = await _userManager.GetUserAsync(HttpContext.User);

            var test = await _context.Tests.SingleOrDefaultAsync(t => t.Id == id);

            if (test.CreatedById != user.Id)
            {
                Forbid();
            }
            var questions = _context.Questions.Where(q => q.TestId == test.Id && !q.IsDeleted);
            var stat      = new Stat {
                TestName = test.Name, TestId = test.Id
            };

            foreach (var question in questions)
            {
                QuestionStat questionStat = new QuestionStat
                {
                    QuestionId    = question.Id,
                    QuestionType  = question.GetTypeString(),
                    QuestionTitle = question.Title
                };
                var answersForQuestion = _context.Answers.Where(a => a.QuestionId == question.Id);
                questionStat.MaxScore = question.Score;
                float averageSum = 0, derivationSum = 0;
                int   n = 0;
                foreach (var answer in answersForQuestion)
                {
                    switch (answer.Result)
                    {
                    case null:
                    {
                        questionStat.NullCount++;
                        break;
                    }

                    case AnswerResult.Wrong:
                    {
                        questionStat.WrongCount++;
                        break;
                    }

                    case AnswerResult.PartiallyRight:
                    {
                        questionStat.PartiallyRightCount++;
                        break;
                    }

                    case AnswerResult.Right:
                    {
                        questionStat.RightCount++;
                        break;
                    }
                    }
                    n++;
                    averageSum    += answer.Score;
                    derivationSum += answer.Score * answer.Score;
                }

                var average = averageSum / n;
                var derivationSumAverage = derivationSum / n;
                var stDer = Math.Sqrt(derivationSumAverage - (average * average));
                questionStat.AverageScore            = (float)Math.Round(average, 2);
                questionStat.ScoreStandartDerivation = (float)Math.Round(stDer, 2);
                stat.QuestionStats.Add(questionStat);
            }

            stat.MostDifficult = questions.SingleOrDefault(x => x.Id == stat.GetMostDifficultId());
            stat.MostEasy      = questions.SingleOrDefault(x => x.Id == stat.GetMostEasyId());
            return(View("Stat", stat));
        }