Exemplo n.º 1
0
        // GET
        public ActionResult FinishedQuizListByUserView(int?id)
        {
            var finishedQuizByUserList = db.FinishedQuizByUser.Include(u => u.Quiz).Include(u => u.User).Where(x => x.QuizId == id).GroupBy(x => x.User.NickName).ToList();

            List <FinishedQuizByUser>        forScoreCounting = new List <FinishedQuizByUser>();
            List <UserFinishedQuizViewModel> result           = new List <UserFinishedQuizViewModel>();


            for (int i = 0; i < finishedQuizByUserList.Count; i++)
            {
                FinishedQuizByUser finishedQuizByUser = new FinishedQuizByUser();

                UserFinishedQuizViewModel userFinishedQuizViewModel = new UserFinishedQuizViewModel();

                forScoreCounting = finishedQuizByUserList[i].ToList();
                int score = 0;

                userFinishedQuizViewModel.QuestionName     = new List <string>();
                userFinishedQuizViewModel.ScorePerQuestion = new List <int>();

                for (int j = 0; j < forScoreCounting.Count; j++)
                {
                    int    scorePerQuestion = forScoreCounting[j].Score;
                    string questionName     = forScoreCounting[j].Question.Question1;
                    score += forScoreCounting[j].Score;
                    userFinishedQuizViewModel.ScorePerQuestion.Add(scorePerQuestion);
                    userFinishedQuizViewModel.QuestionName.Add(questionName);
                }

                userFinishedQuizViewModel.Id         = finishedQuizByUserList[i].FirstOrDefault().Id;
                userFinishedQuizViewModel.QuizName   = finishedQuizByUserList[i].FirstOrDefault().Quiz.Name;
                userFinishedQuizViewModel.NickName   = finishedQuizByUserList[i].FirstOrDefault().User.NickName;
                userFinishedQuizViewModel.Date       = finishedQuizByUserList[i].FirstOrDefault().Date;
                userFinishedQuizViewModel.TotalScore = score;

                result.Add(userFinishedQuizViewModel);
            }
            return(View(result));
        }
Exemplo n.º 2
0
        public ActionResult NewQuizStart(QuizTestViewModel quiz)
        {
            // Create a list what is used for counting the total score per User per Quiz
            List <Question> questionList = db.Question.Include(q => q.Answer).AsEnumerable().Where(x => x.QuizId == quiz.Question[0].QuizId).ToList();

            var quizId   = quiz.Question.FirstOrDefault().QuizId;
            var quizName = db.Quiz.Where(x => x.Id == quizId).Select(x => x.Name).FirstOrDefault();

            var userId       = quiz.Question.FirstOrDefault().UserId;
            var userNickName = db.User.Where(x => x.Id == userId).Select(x => x.NickName).FirstOrDefault();

            // Create unique model instance
            CompletedQuiz completedQuiz = new CompletedQuiz();

            // Create database list to save User's answers
            var dbList     = db.FinishedQuizByUser.ToList();
            int totalScore = 0;

            for (int i = 0; i < quiz.Question.Count; i++)
            {
                completedQuiz = new CompletedQuiz
                {
                    UserId       = userId,
                    UserNickName = userNickName,
                    QuizId       = quizId,
                    QuizName     = quizName,
                    QuestionId   = quiz.Question[i].QuestionId,
                    Date         = quiz.Question[i].Date,
                };

                if (quiz.Question[i].QuestionType == 1)
                {
                    if (quiz.Question[i].SelectedAnswer == questionList[i].Answer.Where(x => x.AnswerCheck == true).FirstOrDefault().Id)
                    {
                        totalScore         += questionList[i].Point;
                        completedQuiz.Score = questionList[i].Point;
                    }
                }
                if (quiz.Question[i].QuestionType == 2)
                {
                    int trueAnswerCounterInDatabase  = 0;
                    int correctAnswerCounterFromUser = 0;

                    foreach (var item in questionList[i].Answer)
                    {
                        if (item.AnswerCheck == true)
                        {
                            trueAnswerCounterInDatabase++;
                        }
                    }

                    for (int j = 0; j < quiz.Question[i].AnswerList.Count; j++)
                    {
                        if (quiz.Question[i].AnswerList[j].AnswerCheck == questionList[i].Answer.ElementAt(j).AnswerCheck&& questionList[i].Answer.ElementAt(j).AnswerCheck == true)
                        {
                            correctAnswerCounterFromUser++;

                            if (correctAnswerCounterFromUser == trueAnswerCounterInDatabase)
                            {
                                completedQuiz.Score = questionList[i].Point;
                                totalScore         += questionList[i].Point;
                                break;
                            }
                        }
                    }
                }

                FinishedQuizByUser result = new FinishedQuizByUser();
                result.UserId     = completedQuiz.UserId;
                result.QuizId     = completedQuiz.QuizId;
                result.QuestionId = completedQuiz.QuestionId;
                result.Score      = completedQuiz.Score;
                result.Date       = completedQuiz.Date;

                var finishedQuiz = db.FinishedQuizByUser?.Where(q => q.QuestionId == completedQuiz.QuestionId && q.UserId == completedQuiz.UserId)?.FirstOrDefault();
                if (finishedQuiz == null)
                {
                    db.FinishedQuizByUser.Add(result);
                    db.SaveChanges();
                }
                else
                {
                    finishedQuiz.QuestionId      = completedQuiz.QuestionId;
                    finishedQuiz.Score           = completedQuiz.Score;
                    finishedQuiz.Date            = completedQuiz.Date;
                    db.Entry(finishedQuiz).State = EntityState.Modified;
                    db.SaveChanges();
                }
            }
            completedQuiz.TotalScore = totalScore;

            return(View("FinishedQuizView", completedQuiz));
        }