public async Task <bool> SubmitQuestionAnswer(QuestionAnswer answer)
        {
            using (var db = new DatabaseConfiguration())
            {
                try
                {
                    await db.QuestionAnswers.AddAsync(answer);

                    QuestionnaireTrack questionnaireTrack = await db.QuestionnaireTracks.SingleAsync(track => track.FormID == answer.FormID && track.PatientID == answer.PatientID);

                    Questionnaire questionnaire = await db.Questionnaires.SingleAsync(quest => quest.FormID == questionnaireTrack.FormID);

                    string[] questionIDs  = questionnaire.QuestionIDs.Split(",");
                    int      currentIndex = Array.IndexOf(questionIDs, answer.QuestionID.ToString());
                    if (currentIndex + 1 > questionIDs.Length)
                    {
                        questionnaireTrack.Review = true;
                    }
                    else
                    {
                        questionnaireTrack.CurrentQuestionID = int.Parse(questionIDs[currentIndex + 1]);
                    }
                    await db.QuestionnaireTracks.AddAsync(questionnaireTrack);

                    await db.SaveChangesAsync();

                    return(true);
                }
                catch (Exception ex)
                {
                    // TODO
                    return(false);
                }
            }
        }
        public async Task <string> CallQuestionnaire(int FormID, int PatientID)
        {
            using (var db = new DatabaseConfiguration())
            {
                bool agreed = await db.PatientAgreements.AnyAsync(a => a.FormID == FormID && a.PatientID == PatientID);

                if (agreed)
                {
                    bool trackExist = await db.QuestionnaireTracks.AnyAsync(t => t.FormID == FormID && t.PatientID == PatientID);

                    if (trackExist)
                    {
                        QuestionnaireTrack track = await db.QuestionnaireTracks.SingleAsync(t => t.FormID == FormID && t.PatientID == PatientID);

                        if (track.Complete)
                        {
                            return("complete");
                        }
                        else if (track.Review)
                        {
                            return("review");
                        }
                        return(track.CurrentQuestionID.ToString());
                    }
                    Questionnaire questionnaire = await db.Questionnaires.SingleAsync(q => q.FormID == FormID);

                    return(questionnaire.QuestionIDs.Split(",").First());
                }
                return("notagreed");
            }
        }
        public async Task <bool> AcceptQuestionnaire(int FormID, int PatientID, string Date)
        {
            using (var db = new DatabaseConfiguration())
            {
                bool agreed = await db.PatientAgreements.AnyAsync(a => a.FormID == FormID && a.PatientID == PatientID);

                if (!agreed)
                {
                    try
                    {
                        PatientAgreement agreement = new PatientAgreement();
                        agreement.FormID    = FormID;
                        agreement.PatientID = PatientID;
                        agreement.Time      = Date;
                        await db.PatientAgreements.AddAsync(agreement);

                        QuestionnaireTrack track = new QuestionnaireTrack();
                        track.PatientID = PatientID;
                        track.FormID    = FormID;
                        track.Review    = false;
                        track.Complete  = false;

                        Questionnaire questionnaire = await db.Questionnaires.SingleAsync(q => q.FormID == FormID);

                        track.CurrentQuestionID = int.Parse(questionnaire.QuestionIDs.Split(",").First());

                        await db.QuestionnaireTracks.AddAsync(track);

                        await db.SaveChangesAsync();

                        return(true);
                    }
                    catch
                    {
                        // TODO
                        return(false);
                    }
                }
                return(true);
            }
        }