Ejemplo n.º 1
0
        public ActionResult Create(int poll_id = 0)
        {
            Poll poll = openQuestionDatabase.Polls.Find(poll_id);
            if (poll == null)
            {
                return HttpNotFound();
            }

            Question question = new Question();
            question.Poll = poll;
            question.Label = "autolabel_q" + (poll.Questions.Count+1).ToString();
            ViewBag.SheetNumber = 1;
            ViewBag.HasHeader = false;
            return View(question);
        }
Ejemplo n.º 2
0
        //public ActionResult Create(Question question, HttpPostedFileBase xlsFile, int poll_id, int sheetNumber, bool? header)
        public ActionResult Create(int pollId, HttpPostedFileBase xlsFile, bool? hasHeader, int sheetNumber, string Wording, string Label)
        {
            Poll poll = openQuestionDatabase.Polls.Find(pollId);
            if (poll == null)
            {
                return HttpNotFound();
            }
            Question question = new Question();
            question.Poll = poll;
            question.Wording = Wording;
            question.Label = Label;
            ViewBag.SheetNumber = sheetNumber;
            ViewBag.HasHeader = hasHeader ?? false;
            if (xlsFile == null)
                ModelState.AddModelError("xlsFile", "No file uploaded");
            else if (xlsFile.ContentType != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                ModelState.AddModelError("xlsFile", "Wrong file type");
            if (!ModelState.IsValid)
            {
                return View(question);
            }

            try
            {
                RespondentAnswerTable RAT = new RespondentAnswerTable(xlsFile, ViewBag.HasHeader, ViewBag.SheetNumber);
                if (!ViewBag.HasHeader)
                {
                    if (RAT.answers.Length != 1)
                        throw new InvalidOperationException("One question was expected");
                    // добавляем один вопрос
                    // question.Poll
                    // question.Wording
                    // question.Label
                    openQuestionDatabase.Questions.Add(question);

                    foreach (var resp_answ in RAT.answers[0])
                    {
                        Answer answer = new Answer();
                        answer.Question = question;
                        answer.RespondentId = resp_answ.Key;
                        answer.Text = resp_answ.Value;
                        openQuestionDatabase.Answers.Add(answer);
                    }
                }
                else
                {
                    // добавляем несколько вопросов
                    for (int i = 0; i < RAT.answers.Length; ++i)
                    {
                        Question qstn = new Question();
                        qstn.Poll = poll;
                        qstn.Wording = RAT.questionLabels[i];
                        qstn.Label = RAT.questionLabels[i];
                        openQuestionDatabase.Questions.Add(qstn);

                        foreach (var resp_answ in RAT.answers[i])
                        {
                            Answer answer = new Answer();
                            answer.Question = qstn;
                            answer.RespondentId = resp_answ.Key;
                            answer.Text = resp_answ.Value;
                            openQuestionDatabase.Answers.Add(answer);
                        }
                    }
                }

                openQuestionDatabase.SaveChanges();
                return RedirectToAction("Details", "OpenPoll", new { id = poll.PollId });
            }
            catch (Exception e)
            {
                ModelState.AddModelError(String.Empty, e);
            }

            return View(question);
        }