Exemple #1
0
        public JsonResult SaveAnswers(string answer, string answers, string questionid, string ipaddress, string browser)
        {
            List <QuestionAnswerViewModel> q = AccountController.GetQuestionAnswer();
            RespondentViewModel            r = AccountController.GetRespondent();

            if (r == null)
            {
                r    = new RespondentViewModel();
                r.Id = Guid.NewGuid();
                if (AccountController.CheckUser())
                {
                    r.UserId       = AccountController.GetUser().Id;
                    r.IsRegistered = true;
                }
                r.IPAdress    = ipaddress;
                r.BrowserName = browser;
                r.SurveyId    = AccountController.GetSurveyId();
            }
            //q.Add(new QuestionViewModel{Id = new Guid(questionid)});

            //int i = r.Count - 1;
            if (r.Responses == null)
            {
                r.Responses = new List <ResponseViewModel>();
            }
            if (q == null)
            {
                q = new List <QuestionAnswerViewModel>();
            }
            if (answer != null)
            {
                QuestionAnswerViewModel qa = new QuestionAnswerViewModel
                {
                    Id         = Guid.NewGuid(),
                    QuestionId = new Guid(questionid),
                    Answer     = answer
                };
                q.Add(qa);
                AccountController.RememberQuestionAnswer(q);

                ResponseViewModel rs = new ResponseViewModel
                {
                    Id             = Guid.NewGuid(),
                    QuestionId     = new Guid(questionid),
                    ResponseAnswer = qa.Id,
                    RespondentId   = r.Id
                };
                r.Responses.Add(rs);
            }
            else if (answers != "")
            {
                string[] a = answers.Split(',');
                //UpdateList();
                //var type = listQuestion.Find(x=>x.Id==new Guid(questionid)).Type;
                //if (type == "Yes/No")
                //{
                //    answers = new List<string>();
                //}
                foreach (var item in a)
                {
                    if (item != "")
                    {
                        ResponseViewModel rs = new ResponseViewModel
                        {
                            Id             = Guid.NewGuid(),
                            QuestionId     = new Guid(questionid),
                            ResponseAnswer = new Guid(item),
                            RespondentId   = r.Id
                        };
                        r.Responses.Add(rs);
                    }
                }
            }
            AccountController.RememberRespondent(r);
            return(Json(true, JsonRequestBehavior.AllowGet));
        }
        // GET: Questions/Create
        //public ActionResult CreateQuestion()
        //{
        //    UpdateList();
        //    ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type");
        //    ViewBag.SurveyId = new SelectList(listSurvey, "Id", "Title");
        //    return View();
        //}

        public ActionResult CreateQuestion(Guid QuestionTypeId, string Content, int?Count, string[] dynamicField)
        {
            QuestionViewModel questionViewModel = new QuestionViewModel();

            if (QuestionTypeId != null && Content != null && Content != "")
            {
                questionViewModel.Id             = Guid.NewGuid();
                questionViewModel.QuestionTypeId = QuestionTypeId;
                questionViewModel.Content        = Content;
                questionViewModel.Number         = AccountController.Count();
                questionViewModel.SurveyId       = AccountController.GetSurveyId();
                questionViewModel.Survey         = AccountController.GetSurvey();
                questionViewModel.TimeLength     = TimeSpan.FromSeconds(3);

                if (Count != null)
                {
                    if (Count < 2)
                    {
                        return(RedirectToAction("Create", "Surveys", new { error = "Scale should more than two" }));
                    }
                    questionViewModel.IsScale = true;
                    questionViewModel.Count   = (int)Count;
                    questionViewModel.AddQuestion(questionViewModel);
                }
                else if (dynamicField.Count() > 0 && dynamicField[0] != "")
                {
                    questionViewModel.AddQuestion(questionViewModel);

                    List <QuestionAnswerViewModel> answer = new List <QuestionAnswerViewModel>();
                    QuestionAnswerViewModel        q      = new QuestionAnswerViewModel();
                    int i = 1;
                    foreach (var item in dynamicField)
                    {
                        if (item != "" && item != null)
                        {
                            q             = new QuestionAnswerViewModel();
                            q.Id          = Guid.NewGuid();
                            q.QuestionId  = questionViewModel.Id;
                            q.Answer      = item;
                            q.OrderNumber = i;
                            answer.Add(q);
                            i++;
                        }
                        else
                        {
                            return(RedirectToAction("Create", "Surveys", new { error = "Answer can't be empty" }));
                        }
                    }
                    foreach (var item in answer)
                    {
                        item.AddQuestionAnswer(item);
                    }
                }
                else
                {
                    questionViewModel.IsFreeText = true;
                    questionViewModel.AddQuestion(questionViewModel);
                }
                TemporaryQuestion();
                UpdateList();
                ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type", questionViewModel.QuestionTypeId);
                ViewBag.SurveyId       = new SelectList(listSurvey, "Id", "Title", questionViewModel.SurveyId);
                return(RedirectToAction("Create", "Surveys"));
            }
            UpdateList();
            ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type", questionViewModel.QuestionTypeId);
            ViewBag.SurveyId       = new SelectList(listSurvey, "Id", "Title", questionViewModel.SurveyId);
            return(RedirectToAction("Create", "Surveys", new { error = "Question can't be empty" }));
        }