public int UpdateSurvey(Survey survey) { int moduleId; Survey surveyToUpdate; var surveyRepository = new SurveyRepository(); if (survey.SurveyId > 0) { surveyToUpdate = surveyRepository.LoadSurvey(survey.SurveyId); surveyToUpdate.RevisingUser = surveyToUpdate.Sections[0].RevisingUser = survey.RevisingUser; moduleId = surveyToUpdate.ModuleId; } else { surveyToUpdate = surveyRepository.CreateSurvey(survey.RevisingUser, survey.PortalId, survey.ModuleId); moduleId = survey.ModuleId; } if (!this.CanEditModule(moduleId)) { this.DenyAccess(); } // TODO: store dates in UTC surveyToUpdate.Text = survey.Text; surveyToUpdate.PortalId = survey.PortalId; surveyToUpdate.ModuleId = survey.ModuleId; surveyToUpdate.ShowText = true; surveyToUpdate.StartDate = survey.StartDate; surveyToUpdate.PreStartMessage = survey.PreStartMessage; surveyToUpdate.EndDate = survey.EndDate; surveyToUpdate.PostEndMessage = survey.PostEndMessage; surveyToUpdate.SendNotification = survey.SendNotification; surveyToUpdate.NotificationFromEmailAddress = survey.NotificationFromEmailAddress; surveyToUpdate.NotificationToEmailAddresses = survey.NotificationToEmailAddresses; surveyToUpdate.SendThankYou = survey.SendThankYou; surveyToUpdate.ThankYouFromEmailAddress = survey.ThankYouFromEmailAddress; surveyToUpdate.FinalMessageOption = survey.FinalMessageOption; surveyToUpdate.FinalMessage = survey.FinalMessage; surveyToUpdate.FinalUrl = survey.FinalUrl; surveyToUpdate.Sections.First().Text = survey.Sections.First().Text; surveyToUpdate.Sections.First().ShowText = true; surveyRepository.SubmitChanges(); return surveyToUpdate.SurveyId; }
public void ReorderQuestions(int surveyId, Dictionary<string, int> questionOrderMap) { var surveyRepository = new SurveyRepository(); var moduleId = surveyRepository.GetModuleIdForSurvey(surveyId); if (!this.CanEditModule(moduleId)) { this.DenyAccess(); } var survey = surveyRepository.LoadSurvey(surveyId); foreach (var questionIdOrderPair in questionOrderMap) { var questionId = int.Parse(questionIdOrderPair.Key, CultureInfo.InvariantCulture); var relativeOrder = questionIdOrderPair.Value; survey.Sections[0].Questions.Where(q => q.QuestionId == questionId).Single().RelativeOrder = relativeOrder; } surveyRepository.SubmitChanges(); }
public object UpdateQuestion(int surveyId, Question question) { var surveyRepository = new SurveyRepository(); var survey = surveyRepository.LoadSurvey(surveyId); if (!this.CanEditModule(survey.ModuleId)) { this.DenyAccess(); } Question questionToUpdate; if (question.QuestionId > 0) { questionToUpdate = survey.Sections.First().Questions.Where(q => q.QuestionId == question.QuestionId).Single(); questionToUpdate.RevisingUser = question.RevisingUser; } else { questionToUpdate = surveyRepository.CreateQuestion(question.RevisingUser); survey.Sections.First().Questions.Add(questionToUpdate); } questionToUpdate.Text = question.Text; questionToUpdate.IsRequired = question.IsRequired; questionToUpdate.RelativeOrder = question.RelativeOrder; questionToUpdate.ControlType = question.ControlType; var answersToDelete = from answer in questionToUpdate.Answers where !question.Answers.Any(a => a.AnswerId == answer.AnswerId) select answer; surveyRepository.DeleteAnswers(answersToDelete, true); int answerOrder = 0; foreach (var answer in question.Answers) { Answer answerToUpdate; if (answer.AnswerId > 0) { var lambdaAnswer = answer; answerToUpdate = questionToUpdate.Answers.Where(a => a.AnswerId == lambdaAnswer.AnswerId).Single(); answerToUpdate.RevisingUser = question.RevisingUser; } else { answerToUpdate = surveyRepository.CreateAnswer(question.RevisingUser); questionToUpdate.Answers.Add(answerToUpdate); } answerToUpdate.Text = answer.Text; answerToUpdate.RelativeOrder = ++answerOrder; } surveyRepository.SubmitChanges(); return new { questionToUpdate.QuestionId, questionToUpdate.ControlType, questionToUpdate.RelativeOrder, questionToUpdate.Text, questionToUpdate.IsRequired, Answers = questionToUpdate.Answers.OrderBy(a => a.RelativeOrder).Select( a => new { a.AnswerId, a.RelativeOrder, a.Text }) }; }