public SurveyInstance CreateSurveyInstance(SurveyKeyValue[] keyValues, long surveyModelId)
        {
            try
            {
                var surveyInstance = new SurveyInstance();
                surveyInstance.DateTaken = DateTime.Now;
                foreach (var keyValue in keyValues)
                {
                    surveyInstance.KeyValues.Add(keyValue);
                }

                var survey = _smr.Find(surveyModelId);

                if (survey != null)
                {
                    survey.SurveyInstances.Add(surveyInstance);
                    _smr.Update(survey);
                }

                return(surveyInstance);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                return(null);
            }
        }
Ejemplo n.º 2
0
 public void Update(SurveyInstance surveyInstance)
 {
     try
     {
         db.Entry(surveyInstance).State = EntityState.Modified;
         db.SaveChanges();
     }
     catch (Exception ex)
     {
         Log.Error(ex.Message, ex);
     }
 }
Ejemplo n.º 3
0
        public long Save(SurveyInstance surveyInstance)
        {
            try
            {
                db.SurveyInstances.Add(surveyInstance);
                db.SaveChanges();

                return(surveyInstance.SurveyInstanceID);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message, ex);
                return(-1);
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AnonymousSurvey(int id, AnonymousSurveyViewModel viewModel)
        {
            SurveyInstance AnonymousSurvey = new SurveyInstance();

            AnonymousSurvey.SurveyId    = id;
            AnonymousSurvey.DateCreated = DateTime.Now;
            _context.Add(AnonymousSurvey);

            foreach (AnswerSurveyInstance answer in viewModel.Answers)
            {
                answer.SurveyInstanceId = AnonymousSurvey.SurveyInstanceId;
                _context.Add(answer);
            }

            await _context.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 5
0
        public async Task Execute(SaveSurveyAnswers command)
        {
            var questionStorageMap = await repos.GetQuestionStorageMap(command.SurveyId);

            var currentAnswers = await repos.Get(new GetSurveyAnswers
            {
                CaseId     = command.CaseId,
                SurveyId   = command.SurveyId,
                ProfileId  = command.ProfileId,
                OutbreakId = command.OutbreakId
            });

            //unwrap jArrays to complex types
            SurveyAnswerBindingHelpers.Bind(command.Answers);

            var answers = command.Answers.ToDictionary(entry => entry.Key.ToUpper(), entry => entry.Value);

            //get only the differences
            var deltas = SurveyAnswerHelpers.GetDeltas(currentAnswers, answers);

            //if no delta, exit
            if (!deltas.Any())
            {
                return;
            }

            var writer = COR <SurveyAnswerWriteContext> .CreateChain(
                new SymptomWriteHandler(),
                new TravelHistoryWriteHandler(),
                new MappedWriteHandler(dataServices),
                new RepeaterWriteHandler(),
                new MultipleWriteHandler(),
                new DefaultWriteHandler());

            var allkeys = deltas
                          .Select(delta => delta.Key)
                          .ToList();

            var storageMapping = writeContext.SurveyObjectMapping
                                 .Where(mapping => mapping.CdMappingtype == "TARGETDS")
                                 .Where(mapping => allkeys.Contains(mapping.IdQuestion))
                                 .ToDictionary(mapping => mapping.IdQuestion.ToUpper());

            var surveyInstanceId = await repos.GetSurveyInstanceId(command.ProfileId, command.CaseId, command.OutbreakId);

            var connection = writeContext.Database.GetDbConnection().EnsureOpen();

            using (var transaction = writeContext.Database.BeginTransaction().GetDbTransaction())
            {
                //TODO:  This needs better DB design
                //if no survey instance, create one
                if (surveyInstanceId == 0)
                {
                    surveyInstanceId = (int)await sequencerGenerator.GetNextAsync(SequenceType.SurveyInstance);

                    var instance = new SurveyInstance
                    {
                        IdSurveyInstance = surveyInstanceId.Value,
                        IdProfile        = command.ProfileId,
                    };

                    if (command.OutbreakId != null)
                    {
                        instance.CdEntityType = "O";
                        instance.IdEntity     = command.OutbreakId;
                    }

                    if (command.CaseId != null)
                    {
                        instance.CdEntityType = "C";
                        instance.IdEntity     = command.CaseId;
                    }

                    await writeContext.SurveyInstance.AddAsync(instance);

                    await writeContext.SaveChangesAsync();
                }
                //end

                var context = new SurveyAnswerWriteContext
                {
                    DB                 = writeContext,
                    CaseId             = command.CaseId,
                    ProfileId          = command.ProfileId,
                    OutbreakId         = command.OutbreakId,
                    SurveyInstanceId   = surveyInstanceId.Value, //should be assigned a value by this point
                    UserId             = usernameProvider.GetUsername(),
                    Timestamp          = DateTime.Now,
                    QuestionStorageMap = questionStorageMap
                };

                context.Transaction = transaction;
                context.Connection  = connection;

                //apply the changes
                foreach (var delta in deltas)
                {
                    context.Delta = delta;

                    await writer.HandleAsync(context);

                    //need to mark question as answered
                    if (SurveyAnswerBindingHelpers.IsQuestion(delta.Key))
                    {
                        var param = new
                        {
                            questionId = delta.Key
                        };
                        try
                        {
                            string sql = @"update survey_question_bank
                                set IN_ANSWERED = 1
                                where id_question = @questionId
                                and IN_ANSWERED = 0;";

                            await connection.ExecuteAsync(sql, param, 1, transaction);
                        }
                        catch (Exception e)
                        {
                            //do nothing for now
                        }
                    }
                }
                transaction.Commit();
            }
        }