public QuestionInProgresWrapper(int number, TestInProgres currentQeustion, Action action)
        {
            Number = number;
            TestType = currentQeustion.Question.TestType;
            QuestionText = currentQeustion.Question.Text;
            HasPrev = action.TestsInProgres.FirstOrDefault(q => q.Question.Number == (number - 1)) != null;
            HasNext = action.TestsInProgres.FirstOrDefault(q => q.Question.Number == (number + 1)) != null;
            AnswerVariantList = new List<VariantItem>();
            QuestionCount = ((QuestionWithVariants) currentQeustion.Question).VariantAnswers.Count;
            SelectedAnswer = ConvertAnswer(currentQeustion.Question.TestType, currentQeustion.UserAnswer);
            StatusList = new List<StatusQuestion>();

            foreach (var variant in ((QuestionWithVariants) currentQeustion.Question).VariantAnswers)
            {
                var variantId =
                    ((QuestionWithVariants) currentQeustion.Question).VariantAnswers.IndexOf(variant);
                AnswerVariantList.Add(new VariantItem
                {
                    Text = variant.Text,
                    VariantId = variantId,
                    Selected =
                        InitializeChecked(currentQeustion.Question.TestType, currentQeustion.UserAnswer,
                            variantId)
                });
            }

            foreach (var index in action.TestsInProgres.Select(test => action.TestsInProgres.IndexOf(test) + 1))
            {
                StatusList.Add(new StatusQuestion
                {
                    Number = index,
                    IsActive = index == number
                });
            }
        }
 private static double CalculateRadio(TestInProgres test)
 {
     if (test.UserAnswer != null)
     {
         return test.Question.Answer == test.UserAnswer ? 1 : 0;
     }
     return 0;
 }
 private static double CalculateChecked(TestInProgres test)
 {
     if (test.UserAnswer.Count() != test.Question.Answer.Count())
     {
         return 0;
     }
     var answerPoints = 0;
     var userPoints = 0;
     if (test.UserAnswer != null)
     {
         for (int index = 0; index < test.Question.Answer.Count(); ++index)
         {
             if (test.Question.Answer[index] == '1')
             {
                 ++answerPoints;
                 switch (test.UserAnswer[index])
                 {
                     case '1':
                         ++userPoints;
                         break;
                     default:
                         --userPoints;
                         break;
                 }
             }
             else
             {
                 switch (test.UserAnswer[index])
                 {
                     case '1':
                         --userPoints;
                         break;
                 }
             }
         }
     }
     return userPoints > 0 ? userPoints/(double) answerPoints : 0;
 }
        private void SaveResultOfRadioQuestion(QuestionInProgresWrapper result, TestInProgres currentQuestion, UsersContext db)
        {
            var userAnswer = "";

            for (var index = 0; index < ((QuestionWithVariants)currentQuestion.Question).VariantAnswers.Count; index++)
            {
                int number;
                if (Int32.TryParse(result.SelectedAnswer, out number))
                {
                    userAnswer += (number == index) ? "1" : "0";
                }
                else
                {
                    userAnswer += "0";
                }
            }
            currentQuestion.UserAnswer = userAnswer;
            currentQuestion.Submitted = true;
            db.SaveChanges();
        }
 private void SaveResultOfCheckedQuestion(QuestionInProgresWrapper result, TestInProgres currentQuestion, UsersContext db)
 {
     var userAnswer = result.AnswerVariantList.Aggregate("",
                         (current, res) => current + (res.Selected ? "1" : "0"));
     currentQuestion.UserAnswer = userAnswer;
     currentQuestion.Submitted = true;
     db.SaveChanges();
 }
 private void SubbmitTest(int userId, TestInProgres currentQuestion, Action action, UsersContext db)
 {
         currentQuestion.Submitted = true;
         action.DatePassing = DateTime.Now;
         action.Status = 2;
         db.SaveChanges();
         CalculateResult(userId, action, db);
         CopyToHistory(action, db);
         RemoveFromProgres(action, db);
 }
 private ActionResult ExecuteCommand(ResponseAction response, int userId,
     Action action, TestInProgres currentQuestion, UsersContext db
     )
 {
     switch (response.Command)
     {
         case DataConst.Subbmit:
         {
             SubbmitTest(userId, currentQuestion, action, db);
             return RedirectToAction("Results", "Result");
         }
         case DataConst.Next:
         {
             if (response.CurrentNumber.HasValue)
             {
                 return RedirectToAction("TestInAction", "Test", new { number = response.CurrentNumber + 1});
             }
             break;
         }
         case DataConst.Prev:
         {
             if (response.CurrentNumber.HasValue)
             {
                 return RedirectToAction("TestInAction", "Test", new { number = response.CurrentNumber - 1 });
             }
             break;
         }
         case DataConst.GoTo:
         {
             if (response.GoTo.HasValue)
             {
                 return RedirectToAction("TestInAction", "Test", new { number = response.GoTo });
             }
             break;
         }
     }
     ViewBag.WarningMessage = DataConst.WarningErrorMessage;
     return RedirectToAction("TestInAction", "Test", new { number = 1 });
 }
 private ActionResult CheckCurrentQuestion(QuestionInProgresWrapper result, ResponseAction response, int userId,
     Action action, TestInProgres currentQuestion, UsersContext db)
 {
     try
     {
         if (currentQuestion.Question.TestType == DataConst.CheckedType)
         {
             SaveResultOfCheckedQuestion(result, currentQuestion, db);
         }
         if (currentQuestion.Question.TestType == DataConst.RadioType)
         {
             SaveResultOfRadioQuestion(result, currentQuestion, db);
         }
        return ExecuteCommand(response, userId, action, currentQuestion, db);
     }
     catch (Exception)
     {
         ViewBag.WarningMessage = DataConst.WarningErrorMessage;
         return RedirectToAction("TestInAction", "Test", new { number = 1 });
     }
 }