/// <summary> /// Uploads the survey asynchronously. /// </summary> /// <param name="response">The survey response.</param> /// <returns>A task to await the completion of the upload.</returns> public static async Task UploadAsync(this UploadedItem <SurveyResponseModel> response) { try { DependencyService.Get <IMetricsManagerService>().TrackEvent("UploadSurveyResponse"); await SurveyCloudService.SubmitSurveyResponseAsync(response.Item); response.Uploaded = DateTime.Now; await response.SaveAsync(); } catch (Exception ex) { DependencyService.Get <IMetricsManagerService>().TrackException("UploadSurveyResponseFailed", ex); throw; } }
public Task SaveAsync() { UpdateLastLocation(); return(_response.SaveAsync()); }
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()); }