Пример #1
0
        public ActionResult SubmitQuiz(TblQuiz objQuiz)
        {
            JavaScriptSerializer json_serializer = new JavaScriptSerializer();

            TblUser sessionUser = (TblUser)Session["UserSession"];

            if (sessionUser == null)
            {
                //newException.AddDummyException("In - 333 ");
                CommonFunctions common   = new CommonFunctions();
                var             password = common.GetEncodePassword("123456");
                sessionUser = ur.IsValidUser("*****@*****.**", password, Request.Url.Host);
            }
            if (sessionUser.RoleId == 2)
            {
                int result = quizRepository.DeleteResponse(objQuiz.QuizId, sessionUser.UserId, 1);
            }
            List <QueOptions> lstQueOptions = new List <QueOptions>();

            object[] objQueResponse = (object[])json_serializer.DeserializeObject(objQuiz.hdnResponseData);
            var      attempt        = quizRepository.GetQuizAttemptByUserID(objQuiz.QuizId, sessionUser.UserId);

            attempt = attempt + 1;

            if (objQuiz.completeTime == "0" || string.IsNullOrEmpty(objQuiz.completeTime))
            {
                var      durationInSeconds = Convert.ToInt32(objQuiz.Duration) * 60;
                TimeSpan t = TimeSpan.FromSeconds(durationInSeconds);
                objQuiz.completeTime = string.Format("{0:D2}:{1:D2}", (int)t.Minutes, t.Seconds);
            }
            else
            {
                var cTime         = objQuiz.completeTime;
                int index1        = cTime.IndexOf(":");
                int index2        = cTime.IndexOf("Minutes");
                int index3        = cTime.IndexOf(",");
                int index4        = cTime.IndexOf("Seconds");
                var cMin          = cTime.Substring(index1 + 1, (index2 - (index1 + 2)));
                var cSec          = cTime.Substring(index3 + 1, (index4 - (index3 + 2)));
                var remainingTime = (Convert.ToInt32(cMin) * 60) + Convert.ToInt32(cSec);
                remainingTime = Convert.ToInt32(objQuiz.Duration * 60) - remainingTime;
                TimeSpan t = TimeSpan.FromSeconds(remainingTime);
                objQuiz.completeTime = string.Format("{0:D2}:{1:D2}", (int)t.Minutes, t.Seconds);
            }


            foreach (var item in objQueResponse)
            {
                QuizResponse quizResponse = new QuizResponse();
                quizResponse.QuizId  = objQuiz.QuizId;
                quizResponse.UserId  = sessionUser.UserId;
                quizResponse.Attempt = attempt;

                foreach (Dictionary <string, object> newItem in (object[])item)
                {
                    var questionId = newItem["questionId"];
                    quizResponse.QuestionId       = Convert.ToInt32(newItem["questionId"]);
                    quizResponse.QuestionFeedback = Convert.ToString(newItem["queFeedback"]);
                    if (string.IsNullOrEmpty(quizResponse.OptionIds))
                    {
                        quizResponse.OptionIds = Convert.ToString(newItem["optionId"]);
                    }
                    else
                    {
                        quizResponse.OptionIds += "," + Convert.ToString(newItem["optionId"]);
                    }
                }
                QueOptions newQueOption = new QueOptions();
                newQueOption.QuestionId = quizResponse.QuestionId;
                newQueOption.OptionsIds = quizResponse.OptionIds;
                lstQueOptions.Add(newQueOption);

                var result = quizRepository.CaptureResponses(quizResponse);
            }

            List <TblQuiz> lstAllQuiz = new List <TblQuiz>();

            lstAllQuiz = quizRepository.GetQuizForLaunch(objQuiz.QuizId, sessionUser.UserId);
            int noOfQues = 0;
            var score    = 0;

            foreach (var question in lstAllQuiz[0].TblQuestions)
            {
                if (question.QuestionTypeId == 1)
                {
                    noOfQues++;
                    foreach (var option in question.TblQuestionOptions)
                    {
                        if (option.CorrectOption == true)
                        {
                            foreach (var que in lstQueOptions)
                            {
                                if (que.QuestionId == question.QuestionId)
                                {
                                    if (option.OptionId == Convert.ToInt32(que.OptionsIds))
                                    {
                                        score++;
                                    }
                                }
                            }
                        }
                    }
                }
                if (question.QuestionTypeId == 2)
                {
                    noOfQues++;
                    int   correctCount = 0;
                    int[] Ids          = new int[question.TblQuestionOptions.Count];
                    foreach (var option in question.TblQuestionOptions)
                    {
                        if (option.CorrectOption == true)
                        {
                            Ids[correctCount] = option.OptionId;
                            correctCount++;
                        }
                    }
                    foreach (var item in lstQueOptions)
                    {
                        if (item.QuestionId == question.QuestionId)
                        {
                            var optionIds = item.OptionsIds.Split(',');
                            if (correctCount == optionIds.Length)
                            {
                                var correct = 0;
                                foreach (var option in optionIds)
                                {
                                    foreach (var id in Ids)
                                    {
                                        if (id == Convert.ToInt32(option))
                                        {
                                            correct++;
                                        }
                                    }
                                }
                                if (correctCount == correct)
                                {
                                    score++;
                                }
                            }
                        }
                    }
                }
            }
            if (noOfQues == 0)
            {
                score = 0;
            }
            var scoreResult = quizRepository.CaptureScore(objQuiz, sessionUser.UserId, score, attempt);
            var emailBody   = "Thank you for taking Quiz.";

            if (noOfQues > 0)
            {
                emailBody = "Thank you for taking Quiz. </br> Your score is " + ((score * 100) / noOfQues) + "% <br />";
            }
            //var emailBody = "Thank you for taking Quiz. </br> Your score is " + ((score * 100) / noOfQues) + "% <br />";
            var       emailSubject = "Quiz Result";
            tblEmails objEmail     = new tblEmails();

            objEmail.EmailTo      = sessionUser.EmailId;
            objEmail.EmailSubject = emailSubject;
            objEmail.EmailBody    = emailBody;
            if (sessionUser.TenantId != 6)
            {
                var emailResult = ur.InsertEmail(objEmail);
            }
            newException.AddDummyException("Responses Saved Successfully");
            TempData["Message"] = "Responses Saved Successfully";
            return(RedirectToAction("ReviewQuiz", new { @QuizId = objQuiz.QuizId }));
        }