Beispiel #1
0
        private bool Validate(Form_submission s)
        {
            var result = true;

            if (s?.Answers != null)
            {
                foreach (var ans in s.Answers)
                {
                    if (ans.SectionQuestion?.required == true)
                    {
                        result = ValidateAnswer(ans) && result;
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
 private void HandleFiles(Form_submission s, Form form)
 {
     foreach (var qa in s.Answers.SelectMany(a => a.QuestionAnswers).Where(x => x.Files != null && x.Files.Count > 0).ToList())
     {
         foreach (var file in qa.Files)
         {
             var destFolder = Path.Combine(hostingEnvironment.WebRootPath, "files", GetUser()?.id.ToString());
             if (!Directory.Exists(destFolder))
             {
                 Directory.CreateDirectory(destFolder);
             }
             var destPath = Path.Combine(destFolder, file.filename);
             var data     = Base64UrlTextEncoder.Decode(file.data.Substring(28));
             System.IO.File.WriteAllBytes(destPath, data);
         }
     }
 }
Beispiel #3
0
 private void AdjustSubObjects(Form_submission s)
 {
     foreach (var a in s.Answers)
     {
         a.SectionQuestion = null;
         foreach (var qa in a.QuestionAnswers)
         {
             qa.Question   = null;
             qa.Validation = null;
             if (qa.AnswerChoices != null)
             {
                 qa.AnswerChoices = qa.AnswerChoices.Where(ac => ac.selected)
                                    .Select(ac => new Form_submission_answer_choice
                 {
                     answer_id = ac.answer_id,
                     choice_id = ac.choice_id
                 }).ToList();
             }
         }
     }
 }
Beispiel #4
0
        public object SubmitForm(Form_submission s)
        {
            if (s != null)
            {
                var form             = unitOfWork.OnlineFormRepository.Get(f => f.id == s.form_id).FirstOrDefault();
                var sectionQuestions = form.Sections.SelectMany(x => x.Section.Questions).ToDictionary(x => (int?)x.id);
                foreach (var a in s.Answers)
                {
                    a.SectionQuestion = sectionQuestions.ContainsKey(a.formsection_question_id)
                                            ? sectionQuestions[a.formsection_question_id]
                                            : null;
                }
                if (Validate(s))
                {
                    AdjustSubObjects(s);
                    HandleFiles(s, form);
                    if (s.id <= 0)
                    {
                        s.dateCreated = DateTime.Now;
                        s.user_id     = GetUser()?.id;
                        unitOfWork.OnlineFormSubmissionRepository.Insert(s);
                    }
                    else
                    {
                        s.dateUpdated = DateTime.Now;
                        unitOfWork.OnlineFormSubmissionRepository.Update(s);
                    }

                    unitOfWork.Save();
                    return(s);
                }
                else
                {
                    return(BadRequest(s));
                }
            }
            return(null);
        }
Beispiel #5
0
        private void AddAnswersToForm(Form form, Form_submission submission)
        {
            var dictAnswers      = submission?.Answers?.ToDictionary(a => a.formsection_question_id);
            var sectionQuestions = form.Sections.SelectMany(s => s.Section.Questions).ToList();

            foreach (var sq in sectionQuestions)
            {
                if (dictAnswers != null && dictAnswers.ContainsKey(sq.id))
                {
                    sq.Answer = dictAnswers[sq.id];
                    foreach (var qa in sq.Answer.QuestionAnswers)
                    {
                        ProcessAnswerChoices(qa);
                    }

                    if (sq.QuestionGroup != null)
                    {
                        //Order answers by group sequence
                        sq.Answer.QuestionAnswers = sq.QuestionGroup.Questions.OrderBy(q => q.sequence)
                                                    .Select(q => sq.Answer.QuestionAnswers.FirstOrDefault(x => x.question_id == q.question_id)).ToList();
                    }
                }
                else
                {
                    sq.Answer = new Form_submission_answer
                    {
                        formsection_question_id = sq.id,
                        QuestionAnswers         = sq.Question != null
                                                        ? new List <form_question_answer> {
                            CreateEmptyQuestionAnswer(sq.Question)
                        }
                                                        : sq.QuestionGroup?.Questions.OrderBy(x => x.sequence).Select(x => CreateEmptyQuestionAnswer(x.Question)).ToList()
                    };
                }

                sq.Answer.Submission = null;
            }
        }