/// <summary> /// Executes the default behavior for saving an aswer. /// </summary> /// <param name="savedAnswer"></param> /// <param name="answer"></param> /// <param name="repository"></param> /// <param name="question"></param> /// <param name="insert"></param> /// <param name="update"></param> private void SaveAnswer(Answer savedAnswer, AnswerModelViewBase answer, IRepository repository, Domain.Question question, Func <IRepository, AnswerQuestionOption> insert, Func <IRepository, AnswerQuestionOption, bool> update) { // If it came until here, we know that it is likely to be a valid request savedAnswer.AnsweredAt = DateTime.Now; //Changing to know the last time the user answered // Loading the last option that the user selected AnswerQuestionOption answerOption = null; if (savedAnswer.Answers != null) { answerOption = savedAnswer.Answers.FirstOrDefault(r => r.Question.Id == answer.Question.Id); } else { savedAnswer.Answers = new List <AnswerQuestionOption>(); } // If it is null, means that the user have not answered the question yet if (answerOption == null) { answerOption = insert(repository); answerOption.Question = question; savedAnswer.Answers.Add(answerOption); } else { update(repository, answerOption); } repository.SaveChanges(); }
/// <summary> /// Implements the default behavior for saving an answer, such as checking /// the ownership of that quiz, identifying the parameters and etc. /// At the end of the execution, this method will delegate the operation to /// one of the functions sent as parameter, depending on the results encontered here. /// </summary> /// <param name="answer">The answer to be persisted</param> /// <param name="insert">The instructions for including a new value</param> /// <param name="update">The instructions for updating a item that is already in the database</param> /// <returns></returns> protected ResultOperation SaveAnswer(AnswerModelViewBase answer, Func <IRepository, AnswerQuestionOption> insert, Func <IRepository, AnswerQuestionOption, bool> update) { try { // Disposing repository to avoid overloading the data context using (IRepository repository = Repository.Repository.CreateRepository()) { // Loading the quiz from the database Domain.Quiz quiz = repository.GetQuiz(answer.Question.IdQuiz); // Loading the answer for that qu iz and for that user Domain.Answer savedAnswer = repository.GetAnswer(quiz.Id, userId); if (savedAnswer.Id == answer.Id) { // If there is no answer, it might mean that someone is trying to send bad data. // We also will allow only changes to quizes that are open and have not been evaluated if (savedAnswer != null && savedAnswer.IsOpen && !savedAnswer.Evaluated) { // Making sure that the question received belongs to that quiz var question = quiz.Questions.FirstOrDefault(r => r.Id == answer.Question.Id); if (question != null) { SaveAnswer(savedAnswer, answer, repository, question, insert, update); } } else { messages.Add("There is no quiz active for this question!"); } } else { messages.Add("There is no quiz active for this question!"); } } } catch (Exception ex) { LogException(ex); messages.Add(DefaultErrorMessage()); } return(messages); }
/// <summary> /// Auxiliar method for getting the persisted answer for a quiz application and determined question. /// </summary> /// <param name="repository"></param> /// <param name="answer"></param> /// <param name="idQuestion"></param> /// <returns></returns> private AnswerModelViewBase CreateInfoAnswer(IRepository repository, Answer answer, int idQuestion) { AnswerQuestionOption infoAnswer = repository.GetAnswerQuestionOptionByQuestioId(answer.Id, idQuestion); if (infoAnswer != null) { AnswerModelViewBase returnAnswer = CreateAnswerModelView(repository, answer, idQuestion, infoAnswer); if (returnAnswer != null) { returnAnswer.Id = infoAnswer.Id; return(returnAnswer); } } return(null); }
/// <summary> /// Create the specific AnswerView based on the question type. /// Can return a TrueFalseAnswerview, MultipleChoiceAnswerView or OpenEndedAnswerView /// </summary> /// <param name="repository"></param> /// <param name="answer"></param> /// <param name="idQuestion"></param> /// <param name="infoAnswer"></param> /// <returns></returns> private AnswerModelViewBase CreateAnswerModelView(IRepository repository, Answer answer, int idQuestion, Domain.AnswerQuestionOption infoAnswer) { AnswerModelViewBase returnAnswer = null; switch (repository.GetQuestion(idQuestion).QuestionType) { case QuestionType.TrueFalse_Question: returnAnswer = new TrueFalseAnswerView() { Option = (infoAnswer as TrueFalseAnswer).Choice }; break; case QuestionType.Multiple_Choice: returnAnswer = new MultipleChoiceAnswerView() { IdAnswers = new List <int>() }; foreach (var item in repository.GetMultipleChoiceAnswer(answer.Id, idQuestion).MultipleChoice_Choices) { (returnAnswer as MultipleChoiceAnswerView).IdAnswers.Add(item.IdChoice); } break; case QuestionType.Open_Ended: returnAnswer = new OpenEndedAnswerView() { Content = (infoAnswer as OpenEnded).Response }; break; } return(returnAnswer); }