public ActionResult Create(Question question)
 {
     ValidateQuestionModel(question);
     question.CreationDate = DateTime.Now;
     if (ModelState.IsValid)
     {
         if (_questionsServices.Add(question))
         {
             return(RedirectToAction("Index", new { @questionnaire_id = question.Category.Questionnaire_Id }));
         }
     }
     InitializeViews(null, null);
     return(View(_questionsViewModel));
 }
示例#2
0
        public ActionResult LoadExcel(HttpPostedFileBase postedFile)
        {
            if (postedFile != null && postedFile.ContentLength > (1024 * 1024 * 50))  // 50MB limit
            {
                ModelState.AddModelError("postedFile", "Your file is to large. Maximum size allowed is 50MB !");
            }

            if (postedFile != null)
            {
                string[]  validFileTypes = { ".xls", ".xlsx", ".csv" };
                string    filePath       = string.Empty;
                string    path           = Server.MapPath("~/Uploads/");
                ExcelRead dt             = new ExcelRead();
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(postedFile.FileName);
                string extension = Path.GetExtension(postedFile.FileName);
                postedFile.SaveAs(filePath);

                string conString = string.Empty;
                if (validFileTypes.Contains(extension))
                {
                    if (extension == ".csv")
                    {
                        dt           = Utility.ConvertCSVtoDataTable(filePath);
                        ViewBag.Data = dt;
                    }
                    //Connection String to Excel Workbook
                    else if (extension.Trim() == ".xls")
                    {
                        conString    = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                        dt           = Utility.ConvertXSLXtoDataTable(filePath, conString);
                        ViewBag.Data = dt;
                    }
                    else if (extension.Trim() == ".xlsx")
                    {
                        conString    = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                        dt           = Utility.ConvertXSLXtoDataTable(filePath, conString);
                        ViewBag.Data = dt;
                    }
                }
                else
                {
                    ModelState.AddModelError("postedFile", "");
                }

                try
                {
                    foreach (var ex in dt.excelContent)
                    {
                        int id = _questionsType.GetAllRecords().Where(x => x.Name == ex.Type).FirstOrDefault().Id;

                        if (string.IsNullOrEmpty(ex.Category) || string.IsNullOrEmpty(ex.Question))
                        {
                            //return View(new LoadExcelViewModel(new LoadExcel()));
                            return(View(new LoadExcelViewModel()));
                        }

                        if (id == 1)
                        {
                            if (string.IsNullOrEmpty(ex.Option) || string.IsNullOrEmpty(ex.Value))
                            {
                                //return View(new LoadExcelViewModel(new LoadExcel()));
                                return(View(new LoadExcelViewModel()));
                            }
                        }

                        if (id == 3)
                        {
                            if (string.IsNullOrEmpty(ex.Option) || string.IsNullOrEmpty(ex.Value))
                            {
                                //return View(new LoadExcelViewModel(new LoadExcel()));
                                return(View(new LoadExcelViewModel()));
                            }

                            if (dt.excelContent.Where(x => x.Question == ex.Question && x.Type == ex.Type).Count() != 2)
                            {
                                //return View(new LoadExcelViewModel(new LoadExcel()));
                                return(View(new LoadExcelViewModel()));
                            }
                        }
                    }

                    Questionnaire questionnaire = new Questionnaire();
                    Category      category;

                    List <Question> Listquestion = new List <Question>();
                    List <Category> Listcategory = new List <Category>();
                    List <Option>   Listoption   = new List <Option>();

                    Option option;

                    questionnaire.Name         = dt.Name;
                    questionnaire.Description  = dt.Name;
                    questionnaire.CreationDate = DateTime.Now;
                    questionnaire.User_Id      = new UsersServices().GetByUserName(User.Identity.Name).Id;
                    questionnaire.Template     = true;
                    questionnaire.Instructions = dt.Name;

                    _questionnaireService.Add(questionnaire);

                    string LastCategory = "";
                    string LastQuestion = "";

                    foreach (var ex in dt.excelContent.Select(x => x.Category).Distinct())
                    {
                        if (LastCategory != ex)
                        {
                            category = new Category();
                            int i = 1;
                            category.Name             = ex;
                            category.Questionnaire_Id = questionnaire.Id;
                            category.CreationDate     = DateTime.Now;
                            category.User_Id          = new UsersServices().GetByUserName(User.Identity.Name).Id;
                            category.Description      = ex;

                            _categoryService.Add(category);
                            Listcategory.Add(category);

                            LastCategory = ex;

                            foreach (var ez in dt.excelContent.Where(x => x.Category == ex).Select(x => new { x.Question, x.Positive, x.Type }).Distinct())
                            {
                                if (LastQuestion != ez.Question)
                                {
                                    Question question = new Question();

                                    question.Category_Id     = category.Id;
                                    question.Text            = ez.Question;
                                    question.CreationDate    = DateTime.Now;
                                    question.Positive        = (ez.Positive == "VERDADERO" || ez.Positive == int.Parse("1").ToString()) ? true : false;
                                    question.QuestionType_Id = _questionsType.GetAllRecords().Where(x => x.Name == ez.Type).FirstOrDefault().Id;
                                    question.SortOrder       = i;

                                    _questionsServices.Add(question);
                                    Listquestion.Add(question);

                                    LastQuestion = ez.Question;
                                    i           += 1;
                                }
                            }
                        }
                    }

                    foreach (var q in Listquestion)
                    {
                        if (q.QuestionType_Id == 1)
                        {
                            dt.excelContent.Where(x => x.Question == q.Text).ToList().ForEach(x =>
                            {
                                option                  = new Option();
                                option.Text             = x.Option;
                                option.CreationDate     = DateTime.Now;
                                option.Value            = int.Parse(x.Value);
                                option.Question_Id      = q.Id;
                                option.Questionnaire_Id = questionnaire.Id;

                                _optionsServices.Add(option);
                            });
                        }

                        if (q.QuestionType_Id == 3)
                        {
                            if (!string.IsNullOrEmpty(dt.excelContent.Where(x => x.Question == q.Text).FirstOrDefault().Option) &&

                                !string.IsNullOrEmpty(dt.excelContent.Where(x => x.Question == q.Text).FirstOrDefault().Value))
                            {
                                dt.excelContent.Where(x => x.Question == q.Text).ToList().ForEach(x =>
                                {
                                    option                  = new Option();
                                    option.Text             = x.Option;
                                    option.CreationDate     = DateTime.Now;
                                    option.Value            = int.Parse(x.Value);
                                    option.Question_Id      = q.Id;
                                    option.Questionnaire_Id = questionnaire.Id;

                                    _optionsServices.Add(option);
                                });
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    return(Json("error" + e.Message));
                }
                //return RedirectToAction("Index");
            }

            //return View(postedFile);
            //return View(new LoadExcelViewModel(new LoadExcel()));
            return(RedirectToAction("Index"));
        }