Ejemplo n.º 1
0
        public async Task <IActionResult> DeleteAllResponses(int surveyId, [ModelBinder(typeof(SurveyRespondentEntityBinder))] SurveyRespondent respondent)
        {
            var user = await _userManager.FindByNameAsync(this.User.Identity.Name);

            await this._respondentService.RemoveAllResponses(surveyId, respondent?.Id ?? -1, user);

            return(new OkResult());
        }
Ejemplo n.º 2
0
        public async Task <bool> DeleteAllResponsesForUser(SurveyRespondent user, int surveyId)
        {
            this._entities.RemoveRange(_entities.Where(x => x.Respondent == user && x.QuestionPart.Survey.Id == surveyId));

            await this._appContext.SaveChangesAsync();

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="group"></param>
        /// <param name="respondent"></param>
        /// <returns></returns>
        public async void RemoveRespondent(SurveyRespondentGroup group, SurveyRespondent respondent)

        {
            var responses = await this._unitOfWork.SurveyResponses.ListQuestionResponsesForRespondentAsync(respondent.Id, "");

            this._unitOfWork.SurveyResponses.RemoveRange(responses);

            group.GroupMembers.Remove(respondent);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="surveyId"></param>
        /// <param name="user"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public async Task <List <SurveyResponse> > ListSurveyResponsesForRespondentByTypeAsync(int surveyId, SurveyRespondent user, string type)
        {
            var r3 = ((PrimaryRespondent)user).SurveyAccessRecords;

            var records = this._appContext.SurveyAccessRecords.Where(r => r.AccessUser == ((PrimaryRespondent)user).User).ToListAsync();

            var responses = new List <SurveyResponse> ();
            IQueryable <SurveyResponse> query = this._appContext.SurveyResponses.Where(r => r.QuestionPart.Survey.Id == surveyId)
                                                .Distinct()
                                                .Where(r => user.SurveyRespondentGroup.GroupMembers.Contains(r.Respondent))
                                                .Include(r => r.ResponseValues)
                                                .Include(r => r.Respondent)
                                                .Include(r => ((SurveyUser)r.SurveyAccessRecord.AccessUser).PrimaryRespondent.SurveyAccessRecords)
                                                .Include(r => r.QuestionPart).ThenInclude(q => q.QuestionConfigurations);

            if (type == "location")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 3));
            }
            else if (type == "timeline")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 7));
            }
            else if (type == "string")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 1));
            }
            else if (type == "decimal")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 2));
            }
            else if (type == "integer")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 3));
            }
            else if (type == "optionlist")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 4));
            }
            else if (type == "json")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 6));
            }
            else if (type == "datetime")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 8));
            }
            else if (type == "option-select")
            {
                query = query.Where(r => r.ResponseValues.Any(r2 => EF.Property <int> (r2, "ResponseType") == 8));
            }

            query = query.GroupBy(r => r.QuestionPart).Select(
                r => r.OrderByDescending(p => p.UpdatedDate).First()
                ).Include(r => r.ResponseValues).Include(r => r.Respondent);

            query = query.OrderByDescending(r => r.UpdatedDate);

            var result = await query.ToListAsync();

            foreach (var r in result)
            {
                responses.Add(r);
            }
            return(responses);
        }
Ejemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="surveyId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <List <SurveyResponse> > ListMostRecentQuestionResponsesForRespondentAsync(int surveyId,
                                                                                                     SurveyRespondent user)
        {
            var result = await this._entities
                         .Where(r => r.QuestionPart.SurveyId == surveyId && r.Respondent == user)
                         .ToListAsync();

            return(result);
        }
Ejemplo n.º 6
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="user"></param>
 /// <param name="questionName"></param>
 /// <returns></returns>
 public async Task <SurveyResponse> GetQuestionResponeByQuestionName(SurveyRespondent user, string questionName) => await this._entities.Where(u => u.Respondent == user)
 .Include(r => ((SurveyUser)r.SurveyAccessRecord.AccessUser).PrimaryRespondent.SurveyAccessRecords)
 .Include(s => s.ResponseValues).ThenInclude(s => s.SurveyResponse)
 .Where(q => q.QuestionPart.Name.ToLower() == questionName.ToLower())
 .Where(r => r.SurveyAccessRecord == ((SurveyUser)r.SurveyAccessRecord.AccessUser).PrimaryRespondent.SurveyAccessRecords.OrderByDescending(o => o.AccessDateTime))
 .FirstOrDefaultAsync();
Ejemplo n.º 7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="questionNames"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <List <SurveyResponse> > ListMostRecentSurveyResponsesForQuestionsByNameAsync(List <string> questionNames, SurveyRespondent user)
        {
            var result = await this._entities.Where(s => s.Respondent == user && questionNames.Contains(s.QuestionPart.Name)).OrderByDescending(s => s.UpdatedDate)
                         .Include(v => v.ResponseValues).Include(v => v.QuestionPart).
                         GroupBy(s => s.QuestionPart).
                         Select(
                r => new {
                Response = r.OrderByDescending(t => t.UpdatedDate).First()
            })
                         .Select(r => r.Response)
                         .ToListAsync();

            return(result);
        }
Ejemplo n.º 8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="questionNames"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <List <SurveyResponse> > ListSurveyResponsesForQuestionsByNameAsync(List <string> questionNames, SurveyRespondent user)
        {
            var result = await this._entities.Where(s => s.Respondent == user && questionNames.Contains(s.QuestionPart.Name))
                         .Include(v => v.ResponseValues).Include(v => v.QuestionPart).OrderBy(s => questionNames.IndexOf(s.QuestionPart.Name)).ThenByDescending(s => s.UpdatedDate).ToListAsync();

            //.ToAsyncEnumerable ().OrderBy (s => questionNames.IndexOf (s.QuestionPart.Name)).ThenByDescending (s => s.UpdatedDate).ToList ();

            result.ForEach(r => r.QuestionPart = null);
            return(result);
        }
Ejemplo n.º 9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="questionId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <SurveyResponse> GetMostRecentResponseForQuestionByRespondentAsync(int questionId,
                                                                                             SurveyRespondent user, int repeat)
        {
            var result = await this._entities.Where(s => s.Respondent == user && s.QuestionPart.Id == questionId && s.Repeat == repeat)
                         .Include(v => v.ResponseValues)
                         .Include(v => v.SurveyAccessRecord).OrderByDescending(s => s.UpdatedDate).FirstOrDefaultAsync();

            // .ToAsyncEnumerable ().OrderByDescending (s => s.UpdatedDate).FirstOrDefault ();

            return(result);
        }
Ejemplo n.º 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="survey"></param>
        /// <param name="question"></param>
        /// <param name="respondent"></param>
        /// <param name="responseData"></param>
        /// <param name="repeat"></param>
        /// <returns></returns>
        public async Task <bool> SaveResponse(Survey survey, QuestionPart question, SurveyRespondent respondent, JObject responseData, int repeat)
        {
            var type = this._questionTypeManager.QuestionTypeDefinitions[question.QuestionType];

            if (type.ResponseValidator != null)
            {
                var responseDataUnwrapped = this.UnwrapResponseData(type.ResponseType, responseData);
                if (!type.ResponseValidator.ValidateResponse(responseDataUnwrapped, question.QuestionConfigurations.Cast <IQuestionConfiguration> ().ToHashSet()))
                {
                    return(false);
                }
            }

            var surveyResponse = await this._unitOfWork.SurveyResponses.GetMostRecentResponseForQuestionByRespondentAsync(question.Id,
                                                                                                                          (SurveyRespondent)respondent, repeat);

            if (surveyResponse == null ||
                (
                    surveyResponse.SurveyAccessRecord.AccessDateTime < ((PrimaryRespondent)respondent).SurveyAccessRecords.First().AccessDateTime))
            {
                surveyResponse = new SurveyResponse()
                {
                    QuestionPart       = question,
                    Respondent         = respondent,
                    SurveyAccessRecord = ((PrimaryRespondent)respondent).SurveyAccessRecords.First(),
                    Repeat             = repeat
                };
                this._unitOfWork.SurveyResponses.Add(surveyResponse);
            }

            // surveyResponse.Repeat = repeat;

            switch (type.ResponseType)
            {
            case QuestionResponseType.String:
                SaveStringResponse(survey, question, responseData, surveyResponse);
                break;

            case QuestionResponseType.Decimal:
                SaveDecimalResponse(survey, question, responseData, surveyResponse);
                break;

            case QuestionResponseType.Integer:
                SaveIntegerResponse(survey, question, responseData, surveyResponse);
                break;

            case QuestionResponseType.DateTime:
                SaveDateTimeResponse(surveyResponse, responseData);
                break;

            case QuestionResponseType.Path:
                SavePathResponse(surveyResponse, responseData);
                break;

            case QuestionResponseType.Json:
                SaveJsonResponse(surveyResponse, responseData);
                break;

            case QuestionResponseType.Location:
                SaveLocationResponse(survey, question, responseData, surveyResponse);
                break;

            case QuestionResponseType.Timeline:
                SaveTimelineResponse(surveyResponse, responseData);
                break;

            case QuestionResponseType.OptionSelect:
                //
                SaveOptionSelectResponse(survey, question, responseData, surveyResponse);
                break;
            }
            try {
                await this._unitOfWork.SaveChangesAsync();
            } catch (Exception e) {
                this._logger.LogError(e, "Error saving response.");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="questionNames"></param>
 /// <param name="respondent"></param>
 /// <returns></returns>
 public async Task <List <SurveyResponse> > ListSurveyResponsesForQuestionsByNameAsync(List <string> questionNames, SurveyRespondent respondent)
 {
     return(await this._unitOfWork.SurveyResponses.ListMostRecentSurveyResponsesForQuestionsByNameAsync(questionNames, respondent));
 }
Ejemplo n.º 12
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="questionIds"></param>
        /// <param name="respondent"></param>
        /// <returns></returns>
        public async Task <List <SurveyResponse> > ListSurveyResponsesForQuestionsAsync(List <int> questionIds, SurveyRespondent respondent)
        {
            var responses = await this._unitOfWork.SurveyResponses.ListSurveyResponsesForQuestionsAsync(questionIds, respondent);

            return(responses);
        }
Ejemplo n.º 13
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="question"></param>
 /// <param name="respondent"></param>
 /// <param name="repeat"></param>
 /// <returns></returns>
 private async Task <SurveyResponse> GenerateSurveyResponse(QuestionPart question, SurveyRespondent respondent, int repeat)
 {
     return(await this._unitOfWork.SurveyResponses.GetMostRecentResponseForQuestionByRespondentAsync(question.Id,
                                                                                                     (SurveyRespondent)respondent, repeat));
 }