public ActionResult Student(int id, int? participationId, int? petitionId, List<AnswerPost> answers ) { var survey = Repository.OfType<Survey>().GetNullableById(id); RegistrationParticipation participation = null; RegistrationPetition petition = null; var isParticipation = false; if (participationId != null) { participation = Repository.OfType<RegistrationParticipation>().GetNullableById(participationId.Value); isParticipation = true; } else if(petitionId != null) { petition = Repository.OfType<RegistrationPetition>().GetNullableById(petitionId.Value); } if (survey == null || (participation == null && isParticipation) || (petition == null && !isParticipation)) { Message = "Unable to locate survey"; return RedirectToAction("Index", "Student"); } if(isParticipation) { // need to validate the student is currently eligible to take this survey if (!participation.Ceremony.CeremonySurveys.Any(a => a.Survey == survey && a.College == participation.Major.MajorCollege)) { return RedirectToAction("DisplayRegistration", "Student"); } } var response = new RegistrationSurvey() {Ceremony = isParticipation ? participation.Ceremony : petition.Ceremony, RegistrationParticipation = isParticipation ? participation: null, RegistrationPetition = !isParticipation ? petition : null, Survey = survey}; var viewModel = new SurveyViewModel() {Survey = survey, AnswerPosts = answers, Errors = new List<string>()}; foreach (var answer in answers) { var question = Repository.OfType<SurveyField>().GetById(answer.QuestionId); if (question.SurveyFieldType.Name == "Boolean/Other") { if (answer.Answers.Count() == 1) { answer.Answer = answer.Answers.First(); } else if (answer.Answers.Count() > 1) { answer.Answer = answer.Answers.FirstOrDefault(a => a != "Yes"); } else { answer.Answer = string.Empty; } } // check for required validation if (question.SurveyFieldValidators.Select(a => a.Name.ToLower()).Contains("required")) { // single answer if (!question.SurveyFieldType.HasMultiAnswer && string.IsNullOrEmpty(answer.Answer)) { viewModel.Errors.Add(question.Prompt); } if (question.SurveyFieldType.HasMultiAnswer && answer.Answers == null) { viewModel.Errors.Add(question.Prompt); } } // add it regardless response.AddSurveyAnswer(new SurveyAnswer() {Answer = question.SurveyFieldType.HasMultiAnswer ? answer.GetAnswers() : answer.Answer, SurveyField = question}); } if (viewModel.Errors.Any()) { return View(viewModel); } if(isParticipation) { // save the survey and update the participation participation.ExitSurvey = true; Repository.OfType<RegistrationParticipation>().EnsurePersistent(participation); } else { petition.ExitSurvey = true; Repository.OfType<RegistrationPetition>().EnsurePersistent(petition); } Repository.OfType<RegistrationSurvey>().EnsurePersistent(response); Message = "Finished!"; return RedirectToAction("DisplayRegistration", "Student"); }
public ActionResult Preview(int id) { var survey = Repository.OfType<Survey>().GetNullableById(id); var viewModel = new SurveyViewModel() {Survey = survey, Errors = new List<string>(new[] {"Blank blank is required.", "These are sample error messages."})}; return View(viewModel); }