private async void PreviousQuestion() { var properties = new Dictionary <string, string> { { "CurrentQuestionID", CurrentQuestion?.QuestionID.ToString() }, }; DependencyService.Get <IMetricsManagerService>().TrackEvent("SurveyPreviousQuestion", properties, null); // Save the response await _response.SaveAsync(); // If the survey is ended, set the question to the last answered question int?previousId; if (_isSurveyEnded) { IsSurveyEnded = false; previousId = _response.Item.QuestionResponses.Max(q => q.QuestionID); } else { // Otherwise, set the question to the question with the next lowest question number previousId = PreviousQuestion(CurrentQuestion.QuestionNum); } // If the question identifier cannot be found, set to the first question (should not happen) _index = QuestionIndex(previousId) ?? FirstQuestionIndex(); UpdateCommands(); QuestionChanged?.Invoke(this, new EventArgs()); }
private async void NextQuestion() { // Add empty answers if necessary var questionResponse = _response.Item.QuestionResponses.FirstOrDefault(r => r.QuestionID == CurrentQuestion?.QuestionID); if (questionResponse == null) { _response.Item.QuestionResponses.Add(new SurveyQuestionResponseModel { QuestionID = CurrentQuestion.QuestionID, }); } // Check if we should prompt in case the user did not fill out the additional information field var shouldPrompt = false; var questionAnswers = CurrentQuestion.AnswerChoices.ToDictionary(a => a.AnswerChoiceID, a => a); foreach (var answer in CurrentAnswers) { if (string.IsNullOrEmpty(answer.AdditionalAnswerData)) { SurveyQuestionAnswerChoiceModel questionAnswer; if (questionAnswers.TryGetValue(answer.AnswerChoiceID, out questionAnswer) && questionAnswer.AdditionalAnswerDataFormat != AnswerFormat.None) { shouldPrompt = true; break; } } } // Prompt the user if any additional information entries are empty if (shouldPrompt) { var shouldContinue = await App.DisplayAlertAsync( "Incomplete Answer", "At least one answer requires more information, are you sure you want to continue?", "Yes", "No"); if (!shouldContinue) { return; } } var properties = new Dictionary <string, string> { { "CurrentQuestionID", CurrentQuestion?.QuestionID.ToString() }, }; DependencyService.Get <IMetricsManagerService>().TrackEvent("SurveyNextQuestion", properties, null); // Save the response await _response.SaveAsync(); // Create a lookup table for all the currently selected answers var currentAnswerIds = new HashSet <int>(CurrentAnswers.Select(a => a.AnswerChoiceID)); // Get a reference to any matching question answer model from the survey question var matchingAnswer = CurrentQuestion.AnswerChoices.FirstOrDefault(a => currentAnswerIds.Contains(a.AnswerChoiceID)); // Set the next question to the next question identifier from the question answer model if available; // otherwise, set the next question to the next highest question number var nextQuestionIndex = QuestionIndex(matchingAnswer?.NextQuestionID) ?? NextQuestion(CurrentQuestion.QuestionNum); _index = nextQuestionIndex ?? _index + 1; // End the survey if the matching question answer disqualifies the survey, or if there are no more questions if ((matchingAnswer?.EndSurvey ?? false) || nextQuestionIndex == null) { EndSurvey(); } UpdateCommands(); QuestionChanged?.Invoke(this, new EventArgs()); }