protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                var crLoginHtml = DAL.SRPSettings.GetSettingValue("CRLoginHtml");
                if(!string.IsNullOrEmpty(crLoginHtml))
                {
                    CRLoginHtml.Text = crLoginHtml;
                    CRLoginHtml.Visible = true;
                }
                else
                {
                    CRLoginHtml.Visible = false;
                }

                var defaultRibbon = StandardModuleRibbons.DefaultRibbon();
                if(defaultRibbon.Count > 0)
                {
                    SetPageRibbon(defaultRibbon);
                }
            }
            if (CRTenantID != null)
            {
                var result = new TenantStatus((int)CRTenantID).CurrentStatus();
                RegisteredPatrons.Text = result.RegisteredPatrons.ToString("N0");
                PointsEarned.Text = result.PointsEarned.ToString("N0");
                PointsEarnedReading.Text = result.PointsEarnedReading.ToString("N0");
                ChallengesCompleted.Text = result.ChallengesCompleted.ToString("N0");
                AdventuresCompleted.Text = result.AdventuresCompleted.ToString("N0");
                BadgesAwarded.Text = result.BadgesAwarded.ToString("N0");
                SecretCodesRedeemed.Text = result.SecretCodesRedeemed.ToString("N0");
                ProgramCodesRedeemed.Text = result.RedeemedProgramCodes.ToString("N0");

                if (DAL.ProgramCodes.GetCountByTenantId((int)CRTenantID) > 0)
                {
                    ProgramCodesDiv.Visible = true;
                    var redeemed = result.SecretCodesRedeemed.ToString();
                    string codeName = StringResources.getString("myaccount-program-reward-code");
                    if(!codeName.EndsWith("s"))
                    {
                        codeName += "s";
                    }
                    ProgramRewardCodeLabel.Text = codeName;
                }
                else
                {
                    ProgramCodesDiv.Visible = false;
                }
                MasterPage.PageTitle = "At-a-glance Program Status - Items Earned by Patrons";

                var tenant = GRA.SRP.Core.Utilities.Tenant.FetchObject((int)CRTenantID);
                OrganizationName.Text = tenant.LandingName;
                var defaultBannerPath = string.Format("~/images/Banners/{0}.png",
                    DAL.Programs.GetDefaultProgramID());
                var defaultBannerFilePath = Server.MapPath(defaultBannerPath);
                if (System.IO.File.Exists(defaultBannerFilePath))
                {
                    ProgramImage.ImageUrl = defaultBannerPath;
                    ProgramImage.CssClass = new WebTools().CssRemoveClass("img-rounded",
                        ProgramImage.CssClass);
                }
                else
                {
                    ProgramImage.ImageUrl = "~/images/meadow.jpg";
                    ProgramImage.CssClass = new WebTools().CssEnsureClass("img-rounded",
                        ProgramImage.CssClass);
                }

                StatusPanel.Visible = true;
            }
            else
            {
                MasterPage.PageTitle = "Control Room";
                StatusPanel.Visible = false;
            }
        }
        protected void UpdateReport()
        {
            if (CRTenantID == null)
            {
                this.Log().Info("CRTenantID is null, unable to call UpdateReport(), probable session expiration");
                return;
            }
            var ts = new TenantStatus((int)CRTenantID);
            if (ProgramList.SelectedValue != "0")
            {
                ts.ProgramId = int.Parse(ProgramList.SelectedValue);
            }
            if (LibraryBranchList.SelectedValue != "0")
            {
                ts.BranchId = int.Parse(LibraryBranchList.SelectedValue);
            }
            else
            {
                if (LibraryDistrictList.SelectedValue != "0")
                {
                    ts.DistrictId = int.Parse(LibraryDistrictList.SelectedValue);
                }
            }
            var result = ts.CurrentStatus();
            RegisteredPatrons.Text = result.RegisteredPatrons.ToString("N0");
            PointsEarned.Text = result.PointsEarned.ToString("N0");
            PointsEarnedReading.Text = result.PointsEarnedReading.ToString("N0");
            ChallengesCompleted.Text = result.ChallengesCompleted.ToString("N0");
            AdventuresCompleted.Text = result.AdventuresCompleted.ToString("N0");
            BadgesAwarded.Text = result.BadgesAwarded.ToString("N0");
            SecretCodesRedeemed.Text = result.SecretCodesRedeemed.ToString("N0");
            ProgramCodesRedeemed.Text = result.RedeemedProgramCodes.ToString("N0");

            if (DAL.ProgramCodes.GetCountByTenantId((int)CRTenantID) > 0)
            {
                ProgramCodesDiv.Visible = true;
                var redeemed = result.SecretCodesRedeemed.ToString();
                string codeName = StringResources.getString("myaccount-program-reward-code");
                if (!codeName.EndsWith("s"))
                {
                    codeName += "s";
                }
                ProgramRewardCodeLabel.Text = codeName;
            }
            else
            {
                ProgramCodesDiv.Visible = false;
            }

            ProgramName.Text = ProgramList.SelectedItem.Text;
            DistrictName.Text = LibraryDistrictList.SelectedItem.Text;
            BranchName.Text = LibraryBranchList.SelectedItem.Text;

            var selectedProgram = ProgramList.SelectedValue;
            if (selectedProgram == "0")
            {
                selectedProgram = DAL.Programs.GetDefaultProgramID().ToString();
            }

            var bannerPath = new Logic.Banner().GetBannerPath(selectedProgram, Server);

            ProgramImage.ImageUrl = bannerPath.Item1;
            ProgramImage.CssClass = new WebTools().CssEnsureClass("img-rounded",
                ProgramImage.CssClass);
        }
        public void ProcessRequest(HttpContext context)
        {
            var tenant = context.Session["TenantID"];
            int tenantId = tenant as int? ?? -1;
            if (tenantId == -1)
            {
                tenantId = Core.Utilities.Tenant.GetMasterID();
            }

            var sessionTool = new SessionTools(context.Session);
            var cachedStatus = sessionTool.GetCache(context.Cache, CacheKey.Feed, tenantId) as JsonStatus;

            try
            {
                if (cachedStatus != null)
                {
                    context.Response.ContentType = "application/json";
                    context.Response.Write(JsonConvert.SerializeObject(cachedStatus));
                    return;
                }
            }
            catch (Exception ex)
            {
                this.Log().Error("Error looking up status data in cache: {0}", ex.Message);
            }

            var jsonResponse = new JsonStatus();
            try
            {
                TenantStatusReport result = null;
                result = new TenantStatus(tenantId).CurrentStatus();

                jsonResponse.PointsEarned = result.PointsEarned;
                jsonResponse.BadgesAwarded = result.BadgesAwarded;
                jsonResponse.ChallengesCompleted = result.ChallengesCompleted;
                jsonResponse.Since = "All Participants";
                jsonResponse.Success = true;
            }
            catch (Exception ex)
            {
                this.Log().Error("Status update error: {0}", ex.Message);
                jsonResponse.Success = false;
            }

            if (jsonResponse.Success)
            {
                try
                {
                    DateTime cacheUntil = DateTime.UtcNow.AddSeconds(30);
                    if (sessionTool.GetCache(context.Cache, CacheKey.Feed, tenantId) == null)
                    {
                        //this.Log().Debug("Caching status data until {0}",
                        //                 cacheUntil.ToLocalTime().ToLongTimeString());
                        string tenantCacheKey = sessionTool.GetTenantCacheKey(CacheKey.Status, tenantId);
                        context.Cache.Insert(tenantCacheKey,
                                             jsonResponse,
                                             null,
                                             cacheUntil,
                                             System.Web.Caching.Cache.NoSlidingExpiration,
                                             System.Web.Caching.CacheItemPriority.Default,
                                             null);
                    }
                }
                catch (Exception ex)
                {
                    this.Log().Error("Error caching status response: {0}", ex.Message);
                }
            }

            context.Response.ContentType = "application/json";
            context.Response.Write(JsonConvert.SerializeObject(jsonResponse));
        }
        protected void UpdateReport()
        {
            var ts = new TenantStatus((int)CRTenantID);
            if (ProgramList.SelectedValue != "0")
            {
                ts.ProgramId = int.Parse(ProgramList.SelectedValue);
            }
            if (LibraryBranchList.SelectedValue != "0")
            {
                ts.BranchId = int.Parse(LibraryBranchList.SelectedValue);
            }
            else
            {
                if (LibraryDistrictList.SelectedValue != "0")
                {
                    ts.DistrictId = int.Parse(LibraryDistrictList.SelectedValue);
                }
            }
            var result = ts.CurrentStatus();
            RegisteredPatrons.Text = result.RegisteredPatrons.ToString("N0");
            PointsEarned.Text = result.PointsEarned.ToString("N0");
            PointsEarnedReading.Text = result.PointsEarnedReading.ToString("N0");
            ChallengesCompleted.Text = result.ChallengesCompleted.ToString("N0");
            AdventuresCompleted.Text = result.AdventuresCompleted.ToString("N0");
            BadgesAwarded.Text = result.BadgesAwarded.ToString("N0");
            SecretCodesRedeemed.Text = result.SecretCodesRedeemed.ToString("N0");
            ProgramCodesRedeemed.Text = result.RedeemedProgramCodes.ToString("N0");

            if (DAL.ProgramCodes.GetCountByTenantId((int)CRTenantID) > 0)
            {
                ProgramCodesDiv.Visible = true;
                var redeemed = result.SecretCodesRedeemed.ToString();
                string codeName = StringResources.getString("myaccount-program-reward-code");
                if (!codeName.EndsWith("s"))
                {
                    codeName += "s";
                }
                ProgramRewardCodeLabel.Text = codeName;
            }
            else
            {
                ProgramCodesDiv.Visible = false;
            }

            ProgramName.Text = ProgramList.SelectedItem.Text;
            DistrictName.Text = LibraryDistrictList.SelectedItem.Text;
            BranchName.Text = LibraryBranchList.SelectedItem.Text;

            var selectedProgram = ProgramList.SelectedValue;
            if (selectedProgram == "0")
            {
                selectedProgram = DAL.Programs.GetDefaultProgramID().ToString();
            }

            var defaultBannerPath = string.Format("~/images/Banners/{0}.png", selectedProgram);
            var defaultBannerFilePath = Server.MapPath(defaultBannerPath);
            if (System.IO.File.Exists(defaultBannerFilePath))
            {
                ProgramImage.ImageUrl = defaultBannerPath;
                ProgramImage.CssClass = new WebTools().CssRemoveClass("img-rounded", ProgramImage.CssClass);
            }
            else
            {
                ProgramImage.ImageUrl = "~/images/meadow.jpg";
                ProgramImage.CssClass = new WebTools().CssEnsureClass("img-rounded", ProgramImage.CssClass);
            }
        }