// When user visits his own library
        public System.Web.Mvc.ActionResult List()
        {
            DocumentsModel model = new DocumentsModel();
            AccountInfo currentUser = CurrentUser.Details(User.Identity.Name);
            model.PageTitle = string.Format(ClientDefault.Documents_Title, currentUser.FullName());

            string result = InitModelData(model, currentUser);
            if (string.IsNullOrEmpty(result))
            {
                return View("Documents", model);
            }
            else
            {
                ModelState.AddModelError("Model", result);
                return View("Documents", model);
            }
        }
        // When carer visits patient's library
        public System.Web.Mvc.ActionResult UserList(long id)
        {
            DocumentsModel model = new DocumentsModel();
            AccountInfo libraryUser = new AccountInfo();
            string getProfileResult = OBSDataSource.GetUserProfile(id, out libraryUser);
            if (string.IsNullOrEmpty(getProfileResult))
            {
                model.PageTitle = string.Format(ClientDefault.Documents_Title, libraryUser.FullName());
                model.UserId = id;

                string result = InitModelData(model, libraryUser);
                if (string.IsNullOrEmpty(result))
                {
                    return View("Documents", model);
                }
                else
                {
                    ModelState.AddModelError("Model", result);
                    return View("Documents", model);
                }
            }
            else
            {
                return RedirectToAction("List", "Users");
            }
        }
        private string InitModelData(DocumentsModel model, AccountInfo user)
        {
            string result = string.Empty;
            List<BaseDocumentInfo> documents = new List<BaseDocumentInfo>();
            List<LibraryLabel> labels = new List<LibraryLabel>();
            AccountInfo currentUser = CurrentUser.Details(User.Identity.Name);

            result = OBSDataSource.GetUserDocuments(user.AccountId, -1, out documents);
            if (!string.IsNullOrEmpty(result))
            {
                return result;

            }
            result = OBSDataSource.GetUserLabels(out labels, user.AccountId);
            if (!string.IsNullOrEmpty(result))
            {
                return result;
            }

            // User is viewing own documents
            if (currentUser.AccountId == user.AccountId)
            {
                model.IsUser = true;
            }
            // Carer is viewing user's documents
            else if (currentUser.Role == AccountRoles.Carer)
            {
                model.IsUser = false;
            }

            List<object> lDocumentsJSON = new List<object>();
            documents = documents.OrderByDescending(x => x.IsFavourite).ThenByDescending(x => x.DateModified).ToList();
            foreach (BaseDocumentInfo doc in documents)
            {
                lDocumentsJSON.Add(new
                {
                    Id = doc.Id,
                    Title = doc.Title,
                    Summary = doc.Summary,
                    IsFavourite = doc.IsFavourite,
                    IsChecked = false,
                    Labels = doc.DocumentLabels,
                    CreatedOn = doc.DateModified.ToString("dd.MM.yyyy"),
                    IsDone = doc.IsCompleted,
                    IsRead = doc.IsRead,
                    IsGlobal = doc.AuthorId == 0
                });
            }

            List<object> lLabelsJSON = new List<object>();
            foreach (LibraryLabel label in labels)
            {
                lLabelsJSON.Add(new
                {
                    Id = label.ID,
                    Name = label.Name,
                    @Color = label.LabelColor.ToHEX(),
                    FontColor = label.FontColor.ToHEX()
                });
            }

            model.DocumentsJSON = JsonConvert.SerializeObject(lDocumentsJSON);
            model.LabelsJSON = JsonConvert.SerializeObject(lLabelsJSON);

            return result;
        }