Ejemplo n.º 1
0
        public JsonResult InsertAnswer(int quizPassQuestionId, int newMark, int evaluatedBy)
        {
            var dateNow = DateTime.Now;

            var userAnswerScore = new UserAnswerScore();

            userAnswerScore.QuizPassQuestionId = quizPassQuestionId;
            userAnswerScore.Score       = (byte)newMark;
            userAnswerScore.EvaluatedBy = evaluatedBy;
            userAnswerScore.EvaluatedAt = dateNow;


            var newUserAnswerScore = _repository.Insert <UserAnswerScore>(userAnswerScore);

            return(Json(newUserAnswerScore));
        }
Ejemplo n.º 2
0
        public void SetQuizFinishTime(int quizPassId)
        {
            User currentUser = _repository.GetSingle <User>(u => u.Email == User.Identity.Name);

            var quizPassWithFinishTime = _repository.GetSingle <QuizPass>(qp => qp.Id == quizPassId);

            quizPassWithFinishTime.FinishTime = DateTime.UtcNow;
            _repository.Update <QuizPass>(quizPassWithFinishTime);

            var userResult = _repository.Get <QuizPassQuestion>(q => q.QuizPassId == quizPassId, q => q.Question.QuestionType, q => q.QuizPass);

            foreach (var elem in userResult)
            {
                if (elem.Question.QuestionType.TypeName == "Select one")
                {
                    var             userAnswer = _repository.GetSingle <UserAnswer>(ur => ur.QuizPassQuestionId == elem.Id, ur => ur.Answer);
                    UserAnswerScore userAnswerScoreToInsert;
                    if (userAnswer == null)
                    {
                        userAnswerScoreToInsert = new UserAnswerScore
                        {
                            QuizPassQuestionId = elem.Id,
                            Score       = 0,
                            EvaluatedBy = currentUser.Id,
                            EvaluatedAt = DateTime.UtcNow
                        };

                        _repository.Insert <UserAnswerScore>(userAnswerScoreToInsert);
                    }
                    else if (userAnswer.Answer.IsRight.HasValue && userAnswer.Answer.IsRight.Value)
                    {
                        userAnswerScoreToInsert = new UserAnswerScore
                        {
                            QuizPassQuestionId = elem.Id,
                            Score       = _repository.GetSingle <QuizQuestion>(qq => qq.Id == elem.Question.Id).QuestionScore,
                            EvaluatedBy = currentUser.Id,
                            EvaluatedAt = DateTime.UtcNow
                        };

                        _repository.Insert <UserAnswerScore>(userAnswerScoreToInsert);
                    }
                    else
                    {
                        userAnswerScoreToInsert = new UserAnswerScore
                        {
                            QuizPassQuestionId = elem.Id,
                            Score       = 0,
                            EvaluatedBy = currentUser.Id,
                            EvaluatedAt = DateTime.UtcNow
                        };

                        _repository.Insert <UserAnswerScore>(userAnswerScoreToInsert);
                    }
                }
                else if (elem.Question.QuestionType.TypeName == "Select many")
                {
                    var userAnswers = _repository.Get <UserAnswer>(ur => ur.QuizPassQuestionId == elem.Id, ur => ur.Answer, ur => ur.QuizPassQuestion);

                    UserAnswerScore userAnswerScoreToInsert;
                    if (userAnswers == null || userAnswers.Count == 0)
                    {
                        userAnswerScoreToInsert = new UserAnswerScore
                        {
                            QuizPassQuestionId = elem.Id,
                            Score       = 0,
                            EvaluatedBy = currentUser.Id,
                            EvaluatedAt = DateTime.UtcNow
                        };
                        _repository.Insert <UserAnswerScore>(userAnswerScoreToInsert);
                    }
                    else
                    {
                        double mark          = 0;
                        byte   questionScore = _repository.GetSingle <QuizQuestion>(qq => qq.Id == elem.Question.Id).QuestionScore;

                        var quizInfo = _repository.Get <QuizQuestion>(q => q.QuizVariant.QuizId == elem.QuizPass.QuizId && q.QuestionId == elem.QuestionId,
                                                                      q => q.Question,
                                                                      q => q.Question.QuestionAnswers).SingleOrDefault();
                        var questionAnswers = quizInfo.Question.QuestionAnswers;
                        int amountOfTrueAns = 0;

                        foreach (var qa in questionAnswers)
                        {
                            qa.Answer =
                                _repository.GetSingle <QuestionAnswer>(el => el.Id == qa.Id, el => el.Answer)
                                .Answer;
                            if (qa.Answer.IsRight.HasValue && qa.Answer.IsRight.Value)
                            {
                                amountOfTrueAns++;
                            }
                        }
                        double pointsPerRightAnswer = Convert.ToDouble(questionScore) / Convert.ToDouble(questionAnswers.Count);

                        var checkedTrueAns = 0;

                        foreach (var answer in userAnswers)
                        {
                            if (answer.Answer.IsRight.HasValue && answer.Answer.IsRight.Value)
                            {
                                mark += pointsPerRightAnswer;
                                checkedTrueAns++;
                            }
                        }
                        var notCheckedTrueAns = amountOfTrueAns - checkedTrueAns;

                        var pointsToAdd = ((questionAnswers.Count - userAnswers.Count) - notCheckedTrueAns) * pointsPerRightAnswer;

                        mark += pointsToAdd;

                        userAnswerScoreToInsert = new UserAnswerScore
                        {
                            QuizPassQuestionId = elem.Id,
                            Score       = mark, // TODO
                            EvaluatedBy = currentUser.Id,
                            EvaluatedAt = DateTime.UtcNow
                        };

                        _repository.Insert <UserAnswerScore>(userAnswerScoreToInsert);
                    }
                }
            }
        }