public ActionResult updateSurvey(SurveyViewModel model) { Dictionary <int, bool> outdatedSurveys = new Dictionary <int, bool>(); var surveysCompletions = modelContext .SurveysCompletion .Include("CategoryObj") .Include("Questions") .FirstOrDefault(x => x.Id == model.SurveyCompletionId); List <int> answeredQuestionIds = new List <int>(); surveysCompletions.Questions.ToList().ForEach(z => answeredQuestionIds.Add(z.QuestionId)); var questions = modelContext .Questions .Include("Survey") .Include("Answers") .Where(x => x.Survey.Id == surveysCompletions.SurveyId) // && !answeredQuestionIds.Contains(x.Id) && x.Old == false) .ToList(); var surveysCompletion = modelContext .SurveysCompletion .Include("CategoryObj") .Include("Questions") .Include("Questions.Answers") .FirstOrDefault(x => x.Id == model.SurveyCompletionId); foreach (var DTO in model.surveyCompletionDTOs) { if (DTO.Answers != null) { bool nueva = false; var question = surveysCompletion.Questions.FirstOrDefault(x => x.QuestionId == DTO.QuestionId); if (question == null) { question = new SurveyCompletionQuestion(); nueva = true; } question.Question = DTO.Question; question.QuestionId = DTO.QuestionId; if (!nueva && question.Answers != null && question.Answers.Count > 0) { question.Answers.Clear(); } question.Answers = questions .Where(x => x.Id == DTO.QuestionId) .FirstOrDefault() .Answers .Where(x => DTO.Answers.Contains(x.Id)) .Select(x => new SurveyCompletionAnswer { Answer = x.SupplyAnswer, AnswerValue = x.Value }) .ToList(); if (nueva) { surveysCompletion.Questions.Add(question); } } } modelContext.SaveChanges(); return(RedirectToAction("UpdateProduct/" + model.ProductId)); }
private SurveyCompletionParent InsertSurveyCompletion(SurveyViewModel model, bool partial) { var category = this.modelContext .Categories .Where(x => x.Id == model.CategoryId) .FirstOrDefault(); var role = this.roleManager.FindByName("DEMANDA"); var surveyCompletionParent = new SurveyCompletionParent { Product = null, Role = role, Status = "Aprobado", PartialSave = partial, Category = category, Email = model.Email, PartialSaveKey = Guid.NewGuid().ToString() }; foreach (var survey in model.SurveyDTOs) { var surveyCompletion = new SurveyCompletion { SurveyId = survey.SurveyId, CategoryId = survey.CategoryId, Category = survey.CategoryName, Email = model.Email, Role = role, Parent = surveyCompletionParent, PartialSave = partial }; var surveyQuestions = this.modelContext .Questions .Include("Answers") .Where(x => x.Survey.Id == survey.SurveyId) .ToList(); foreach (var question in model.surveyCompletionDTOs) { var answers = new List <SurveyCompletionAnswer>(); if (question.SurveyId == survey.SurveyId) { if (question.Answers != null) { answers = surveyQuestions .Where(x => x.Id == question.QuestionId) .FirstOrDefault() .Answers .Where(x => question.Answers.Contains(x.Id)) .Select(x => new SurveyCompletionAnswer { Answer = x.DemandAnswer, AnswerValue = x.Value }) .ToList(); } string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = this.modelContext.Users.FirstOrDefault(x => x.Id == currentUserId); var surveyCompletionQuestion = new SurveyCompletionQuestion { Question = question.Question, QuestionId = question.QuestionId, Answers = answers }; surveyCompletion.Questions.Add(surveyCompletionQuestion); } } this.modelContext.SurveysCompletion.Add(surveyCompletion); } this.modelContext.SurveyCompletionParent.Add(surveyCompletionParent); this.modelContext.SaveChanges(); return(surveyCompletionParent); }
public Task GenerarRanking(int surveyCompletionId) { var parentSurveyCompletionByDemanda = this.modelContext .SurveyCompletionParent .Include("SurveyCompletions.Questions.QuestionObj.AnswerType") .Include("SurveyCompletions.Questions.QuestionObj.Answers") .Include("SurveyCompletions.Questions.Answers") .Include("Category") .Single(x => x.Id == surveyCompletionId); var parentSurveysCompletionsByOferta = this.modelContext .SurveyCompletionParent .Include("SurveyCompletions.Questions") .Include("SurveyCompletions.Questions.Answers") .Where(x => //x.SurveyId == surveyCompletionByDemand.SurveyId && x.Role.Name == "Oferta" && x.Category.Id == parentSurveyCompletionByDemanda.Category.Id && x.Status == "Aprobado" && x.DeletedAt == null && x.Company != null && x.Product != null) .ToList(); var rankings = new List <Ranking>(); foreach (var parentSurveyCompletionByOferta in parentSurveysCompletionsByOferta) { var ranking = new Ranking() { SurveyCompletionParentByDemanda = parentSurveyCompletionByDemanda, SurveyCompletionParentByOferta = parentSurveyCompletionByOferta, Rank = 0 }; foreach (var surveyCompletionByDemanda in parentSurveyCompletionByDemanda.SurveyCompletions) { foreach (var demandaQuestion in surveyCompletionByDemanda.Questions) { /*SurveyCompletionQuestion supplyQuestion = null; * int answersCountTotal = 0; * foreach (var surveyCompletionBySupplySingle in surveyCompletionBySupply.SurveyCompletions) * { * supplyQuestion = surveyCompletionByDemandSingle * .Questions * .Where(x => x.QuestionId == surveyCompletionDemandQuestion.QuestionId) * .FirstOrDefault(); * * answersCountTotal = this.modelContext * .Surveys * .Include("Questions") * .Include("Questions.Answers") * .FirstOrDefault(x => x.Id == surveyCompletionByDemandSingle.SurveyId) * .Questions * .FirstOrDefault(x => x.Id == surveyCompletionDemandQuestion.QuestionId) * .Answers * .Count(); * }*/ var surveyCompletion = parentSurveyCompletionByOferta .SurveyCompletions .Where(s => s.Questions.Where(q => q.QuestionId == demandaQuestion.QuestionId ).Count() == 1 ) .FirstOrDefault(); if (surveyCompletion != null) { SurveyCompletionQuestion supplyQuestion = surveyCompletion.Questions.FirstOrDefault(q => q.QuestionObj.Old == false); int answersCountTotal = demandaQuestion.Answers.Count(); int possibleAnswersCountTotal = demandaQuestion.QuestionObj.Answers.Count(); int possibleAnswersSumTotal = demandaQuestion.QuestionObj.Answers.Sum(x => x.Value); if (supplyQuestion != null) { var answerTypeName = demandaQuestion.QuestionObj.AnswerType.Name; if (answerTypeName != "Multiple") { foreach (var supplyAnswer in supplyQuestion.Answers) { var rakingValue = 0; var demandAnswer = demandaQuestion.Answers.FirstOrDefault(); int result = 0; if (demandAnswer != null) { if (possibleAnswersCountTotal == possibleAnswersSumTotal) { //Comparando respuestas excluyentes entre si result = demandAnswer.Answer == supplyAnswer.Answer ? 10 : 0; } else { //Comparando respuestas que incluyen la funcionalidad de la anterior result = this.getRankingValue( this.getAnswerValueInScale(answersCountTotal, demandAnswer.AnswerValue), this.getAnswerValueInScale(answersCountTotal, supplyAnswer.AnswerValue)); } } rakingValue = result; ranking.Rank = ranking.Rank + rakingValue; } } else { if (answersCountTotal > 0) { int supportedAnswers = supplyQuestion.Answers.Select(x => x.Answer).Intersect(demandaQuestion.Answers.Select(x => x.Answer)).Count(); ranking.Rank += (MaxValuePerAnswer * supportedAnswers) / answersCountTotal; } } } } } } rankings.Add(ranking); } this.modelContext.Rankings.AddRange(rankings); this.modelContext.SaveChanges(); return(Task.FromResult(true)); }
private SurveyCompletionParent InsertSurveyCompletion(SurveyViewModel model, bool partial = false) { this.RemovePartialSurveyCompletion(model); var category = this.modelContext .Categories .Where(x => x.Id == model.CategoryId) .FirstOrDefault(); var userId = User.Identity.GetUserId(); var role = this.roleManager.FindByName("OFERTA"); var user = this.modelContext .Users .FirstOrDefault(x => x.Id == userId); var company = this.modelContext .Companies .Include("ComercialContact") .FirstOrDefault(x => x.Id == model.CompanyId); //aca crear el pridcuto con el contactro del producto var producto = new Product { Name = model.ProductName, Description = model.ProductDescription, WebSite = model.ProductWebSite, Version = model.ProductVersion, User = user, ProductContact = new Contact { FullName = model.ProductContactFullName, Position = model.ProductContactPosition, Phone = model.ProductContactPhone, Email = model.ProductContactEmail } }; producto.Company = company; modelContext.Products.Add(producto); modelContext.SaveChanges(); var surveyCompletionParent = new SurveyCompletionParent(company) { Product = producto, Role = role, Status = "Pendiente", PartialSave = partial, Category = category }; foreach (var surveyDTO in model.SurveyDTOs) { var categoryObj = this.modelContext .Categories .Where(x => x.Id == surveyDTO.CategoryId) .FirstOrDefault(); var surveyCompletion = new SurveyCompletion(company) { SurveyId = surveyDTO.SurveyId, CategoryId = surveyDTO.CategoryId, Product = producto, Category = surveyDTO.CategoryName, CategoryObj = categoryObj, Role = role, PartialSave = partial, Parent = surveyCompletionParent }; var surveyQuestions = this.modelContext .Questions .Include("Answers") .Where(x => x.Survey.Id == surveyDTO.SurveyId) .ToList(); foreach (var question in model.surveyCompletionDTOs) { var answers = new List <SurveyCompletionAnswer>(); if (question.SurveyId == surveyDTO.SurveyId) { if (question.Answers != null) { answers = surveyQuestions .Where(x => x.Id == question.QuestionId) .FirstOrDefault() .Answers .Where(x => question.Answers.Contains(x.Id)) .Select(x => new SurveyCompletionAnswer { Answer = x.SupplyAnswer, AnswerValue = x.Value }) .ToList(); } string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = this.modelContext.Users.FirstOrDefault(x => x.Id == currentUserId); var surveyCompletionQuestion = new SurveyCompletionQuestion { Question = question.Question, QuestionId = question.QuestionId, Answers = answers }; surveyCompletion.Questions.Add(surveyCompletionQuestion); } } this.modelContext.SurveysCompletion.Add(surveyCompletion); } this.modelContext.SurveyCompletionParent.Add(surveyCompletionParent); this.modelContext.SaveChanges(); return(surveyCompletionParent); }
public ActionResult Update(SurveyViewModel model) { var surveysCompletions = modelContext .SurveysCompletion .Include("CategoryObj") .Include("Questions") .FirstOrDefault(x => x.Id == model.SurveyCompletionId); var questions = modelContext .Questions .Include("Survey") .Include("Answers") .Where(x => x.Survey.Id == surveysCompletions.SurveyId) .ToList(); var surveysCompletion = modelContext .SurveysCompletion .Include("CategoryObj") .Include("Questions") .Include("Questions.Answers") .FirstOrDefault(x => x.Id == model.SurveyCompletionId); foreach (var DTO in model.surveyCompletionDTOs) { if (DTO.Answers != null) { bool nueva = false; var question = surveysCompletion .Questions .FirstOrDefault(x => x.QuestionId == DTO.QuestionId); if (question == null) { question = new SurveyCompletionQuestion(); nueva = true; } question.Question = DTO.Question; question.QuestionId = DTO.QuestionId; if (!nueva && question.Answers != null && question.Answers.Count > 0) { question.Answers.Clear(); } question.Answers = questions .Where(x => x.Id == DTO.QuestionId) .FirstOrDefault() .Answers .Where(x => DTO.Answers.Contains(x.Id)) .Select(x => new SurveyCompletionAnswer { Answer = x.DemandAnswer, AnswerValue = x.Value }) .ToList(); if (nueva) { surveysCompletion.Questions.Add(question); } } } modelContext.SaveChanges(); return(RedirectToAction("../../Continue", new { Category = model.CategoryId, ParentSurveyCompletionId = model.ParentSurveyCompletionId, PartialSaveKey = model.PartialSaveKey })); }