public ActionResult TestInAction(int number)
 {
     using (var db = new UsersContext())
     {
         var userId = WebSecurity.GetUserId(User.Identity.Name);
         var action = db.Actions.FirstOrDefault(a => a.User.UserId == userId && a.Status == 1);
         if (action != null)
         {
             var currentQeustion =
                 action.TestsInProgres.FirstOrDefault(q => q.Question.Number==number);
             if (currentQeustion != null)
             {
                 if (currentQeustion.Question is QuestionWithVariants)
                 {
                     var questionInProgres = new QuestionInProgresWrapper(number, currentQeustion, action);
                     return View(questionInProgres);
                 }
             }
         }
     }
     ViewBag.WarningMessage = DataConst.WarningMessageForTest;
     return RedirectToAction("Tests", "Test");
 }
        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 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 });
     }
 }
 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();
 }
 public ActionResult GoToQuestion(QuestionInProgresWrapper result, string submit)
 {
     using (var db = new UsersContext())
     {
         var userId = WebSecurity.GetUserId(User.Identity.Name);
         var action = db.Actions.FirstOrDefault(a => a.User.UserId == userId && a.Status == 1);
         var response = new ResponseAction(submit);
         if (action != null && response.CurrentNumber != null)
         {
             var currentQuestion =
                 action.TestsInProgres.FirstOrDefault(q => q.Question.Number == response.CurrentNumber);
             if (currentQuestion != null)
             {
                return CheckCurrentQuestion(result, response, userId, action, currentQuestion, db);
             }
         }
     }
     ViewBag.WarningMessage = DataConst.WarningMessageForTest;
     return RedirectToAction("TestInAction", "Test", new { number = 1 });
 }