コード例 #1
0
        public ActionResult SetAnswers(List <QuestionnaireAnswer> answerquestion)
        {
            string jsonResponseText;

            try
            {
                if (ModelState.IsValid)
                {
                    var raspunsuriCorecte = 0;
                    foreach (var a in answerquestion)
                    {
                        var ans = _db.AnswersQuestions.FirstOrDefault(x => x.AnswerID == a.AnswerID && x.QuestionID == a.QuestionID);
                        if (ans != null &&
                            ans.Status == Constants.AnswerResponses.Corect)
                        {
                            raspunsuriCorecte++;
                        }
                    }


                    var test = new TestResult
                    {
                        Date   = DateTime.Now,
                        Status = $"{raspunsuriCorecte}/{_db.Questions.Count()} {Translations.Questions}"
                    };

                    if (AuthenticationManager.IsUserAuthenticated(Session))
                    {
                        test.UserID = AuthenticationManager.UserId(Session);
                    }
                    if (TestManager.IsContactRegistered(Session))
                    {
                        test.ContactID = TestManager.ContactId(Session);
                    }

                    _db.TestResults.Add(test);
                    _db.SaveChanges();

                    var correct   = string.Format(Translations.CongratulationsYouHadXResponsesCorrect, raspunsuriCorecte);
                    var incorrect = Translations.NoResponsesCorrect;
                    jsonResponseText = TestManager.JsonMessage(false, new
                    {
                        raspunsuriCorecte,
                        header = raspunsuriCorecte > 0 ? correct : incorrect
                    });
                }
                else
                {
                    jsonResponseText = TestManager.JsonMessage(false, Translations.ModelInvalid);
                }

                return(Content(jsonResponseText, "application/json"));
            }
            catch (Exception ex)
            {
                jsonResponseText = TestManager.JsonMessage(false, ex.Message);

                return(Content(jsonResponseText, "application/json"));
            }
        }