public ActionResult PatientIndex(string dfn, string page)
        {
            // TODO: Make education items pregnancy specific?
            // TODO: Allow passed in pregnancy

            PatientEducationIndex model = new PatientEducationIndex();

            model.Patient = this.CurrentPatient;

            //PregnancyUtilities util = new PregnancyUtilities(this.DashboardRepository);

            PregnancyDetails preg = PregnancyUtilities.GetWorkingPregnancy(this.DashboardRepository, dfn, "");

            if (preg != null)
            {
                if ((preg.EDD != DateTime.MinValue) || (preg.EndDate != DateTime.MinValue))
                {
                    GetMergedList(model, preg);
                }
                else
                {
                    GetPatientEducationItemList(model);
                }
            }
            else
            {
                GetPatientEducationItemList(model);
            }

            return(View(model));
        }
        public ActionResult Complete(PatientEducationIndex model)
        {
            //PatientEducationItem patItem = new PatientEducationItem()
            //{
            //    PatientDfn = model.Patient.Dfn,
            //    EducationItemIen = model.SelectedEducationIen,
            //    CompletedOn = DateTime.Now
            //};

            //IenResult saveResult = this.DashboardRepository.Education.SavePatientItem(patItem);

            //if (!saveResult.Success)
            //    this.Error(saveResult.Message);
            //else
            //{
            //    PregnancyChecklistItemsResult result = this.DashboardRepository.Checklist.GetPregnancyItems(model.Patient.Dfn, "", model.SelectedChecklistIen);

            //    if (!result.Success)
            //        this.Error(result.Message);
            //    else
            //    {
            //        if (result.Items != null)
            //            if (result.Items.Count > 0)
            //            {
            //                PregnancyChecklistItem checkItem = result.Items[0];

            //                checkItem.CompletionStatus = DsioChecklistCompletionStatus.Complete;
            //                checkItem.CompletionLink = saveResult.Ien;

            //                IenResult ienResult = this.DashboardRepository.Checklist.SavePregnancyItem(checkItem);

            //                if (!saveResult.Success)
            //                    this.Error(saveResult.Message);

            //                this.Information("Education item completed");
            //            }
            //    }
            //}

            BrokerOperationResult result = ChecklistUtility.CompleteEducationItem(
                this.DashboardRepository,
                model.Patient.Dfn,
                model.SelectedEducationIen,
                model.SelectedChecklistIen);

            if (result.Success)
            {
                this.Information(result.Message);
            }
            else
            {
                this.Error(result.Message);
            }

            return(RedirectToAction("PatientIndex", new { dfn = model.Patient.Dfn }));
        }
        private void GetPatientEducationItemList(PatientEducationIndex model)
        {
            // *** Get patient education items ***

            PatientEducationItemsResult edResult = this.DashboardRepository.Education.GetPatientItems(
                model.Patient.Dfn,
                "",
                DateTime.MinValue,
                DateTime.MinValue,
                EducationItemType.Unknown,
                1,
                1000
                );

            if (!edResult.Success)
            {
                this.Error(edResult.Message);
            }
            else
            {
                if (edResult.Items != null)
                {
                    foreach (PatientEducationItem patEdItem in edResult.Items)
                    {
                        PatientEducationChecklistItem newItem = new PatientEducationChecklistItem();
                        newItem.PatientEducationItem = patEdItem;
                        model.ItemList.Add(newItem);
                    }
                }

                model.ItemList.Sort(delegate(PatientEducationChecklistItem x, PatientEducationChecklistItem y)
                {
                    return(x.CompareDate.CompareTo(y.CompareDate));
                });
            }
        }
        private void GetMergedList(PatientEducationIndex model, PregnancyDetails preg)
        {
            PregnancyChecklistItemsResult result = this.DashboardRepository.Checklist.GetPregnancyItems(model.Patient.Dfn, preg.Ien, "");

            if (!result.Success)
            {
                this.Error(result.Message);
            }
            else
            {
                // *** Create view items ***

                if (result.Items != null)
                {
                    foreach (PregnancyChecklistItem item in result.Items)
                    {
                        if (item.ItemType == DsioChecklistItemType.EducationItem)
                        {
                            PatientEducationChecklistItem newItem = new PatientEducationChecklistItem();
                            newItem.PregnancyChecklistItem = item;
                            model.ItemList.Add(newItem);
                        }
                    }
                }

                // *** Get patient education items ***

                PatientEducationItemsResult edResult = this.DashboardRepository.Education.GetPatientItems(
                    model.Patient.Dfn,
                    "",
                    DateTime.MinValue,
                    DateTime.MinValue,
                    EducationItemType.Unknown,
                    1,
                    1000
                    );

                if (!edResult.Success)
                {
                    this.Error(edResult.Message);
                }
                else
                {
                    Dictionary <string, PatientEducationItem> patEdItems = new Dictionary <string, PatientEducationItem>();

                    // *** Place patient education items in dictionary for lookup ***

                    if (edResult.Items != null)
                    {
                        foreach (PatientEducationItem patEdItem in edResult.Items)
                        {
                            patEdItems.Add(patEdItem.Ien, patEdItem);
                        }
                    }

                    // *** Go through checklist to find links to patient education ***

                    foreach (PatientEducationChecklistItem finalItem in model.ItemList)
                    {
                        if (finalItem.PregnancyChecklistItem != null)
                        {
                            if (finalItem.PregnancyChecklistItem.CompletionStatus == DsioChecklistCompletionStatus.Complete)
                            {
                                if (!string.IsNullOrWhiteSpace(finalItem.PregnancyChecklistItem.CompletionLink))
                                {
                                    if (patEdItems.ContainsKey(finalItem.PregnancyChecklistItem.CompletionLink))
                                    {
                                        // *** If found add to final ***
                                        finalItem.PatientEducationItem = patEdItems[finalItem.PregnancyChecklistItem.CompletionLink];

                                        // *** Remove since already added ***
                                        patEdItems.Remove(finalItem.PregnancyChecklistItem.CompletionLink);
                                    }
                                }
                            }
                        }
                    }

                    // *** Now that we've added all items that are linked, add remainder ***
                    foreach (PatientEducationItem patEdItem in patEdItems.Values)
                    {
                        PatientEducationChecklistItem newItem = new PatientEducationChecklistItem();
                        newItem.PatientEducationItem = patEdItem;
                        model.ItemList.Add(newItem);
                    }

                    // *** Finally get education items linked by a checklist but incomplete ***
                    foreach (PatientEducationChecklistItem finalItem in model.ItemList)
                    {
                        if (finalItem.PregnancyChecklistItem != null)
                        {
                            if (finalItem.PregnancyChecklistItem.CompletionStatus != DsioChecklistCompletionStatus.Complete)
                            {
                                if (!string.IsNullOrWhiteSpace(finalItem.Link))
                                {
                                    EducationItemsResult edItemsResult = this.DashboardRepository.Education.GetEducationItems(finalItem.Link, "", EducationItemType.Unknown, 0, 0, EducationItemSort.Type);

                                    if (edItemsResult.Success)
                                    {
                                        if (edItemsResult.EducationItems != null)
                                        {
                                            if (edItemsResult.EducationItems.Count > 0)
                                            {
                                                finalItem.EducationItem = edItemsResult.EducationItems[0];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                model.ItemList.AddPregnancyDates(preg.EDD, preg.EndDate);

                model.ItemList.Sort(delegate(PatientEducationChecklistItem x, PatientEducationChecklistItem y)
                {
                    return(x.CompareDate.CompareTo(y.CompareDate));
                });
            }
        }