Пример #1
0
        // points = 100
        // reason = PointAwardReason.MiniGameCompletion
        // MGID = Mini Game ID, if the reason is MiniGameCompletion, else 0
        //
        //
        // returns: a string based list of the badges they earned, or an empty string
        public string AwardPointsToPatron(int points, PointAwardReason reason,
                                          // Minigame
                                          int MGID = 0,
                                          // reading
                                          ActivityType readingActivity = ActivityType.Pages, int readingAmount = 0, string author = "", string title = "", string review = "",
                                          // event
                                          string eventCode = "", int eventID = 0,
                                          // book List
                                          int bookListID = 0,

                                          DateTime?forceDate = null
                                          )
        {
            if (forceDate != null)
            {
                now = (DateTime)forceDate;
            }

            string retValue = string.Empty;

            #region Reading - Log to PatronReadingLog
            PatronReadingLog rl = null;
            if (reason == PointAwardReason.Reading)
            {
                rl = new PatronReadingLog {
                    PID              = patron.PID,
                    ReadingType      = (int)readingActivity,
                    ReadingTypeLabel = (readingActivity).ToString(),
                    ReadingAmount    = readingAmount,
                    ReadingPoints    = points,
                    LoggingDate      = FormatHelper.ToNormalDate(now),
                    Author           = author.Trim(),
                    Title            = title.Trim(),
                    HasReview        = (review.Trim().Length > 0),
                    ReviewID         = 0
                };
                rl.Insert();

                // If there is a review, record the review
                if (review.Trim().Length > 0)
                {
                    var r = new PatronReview {
                        PID        = patron.PID,
                        PRLID      = rl.PRLID,
                        Author     = rl.Author,
                        Title      = rl.Title,
                        Review     = review.Trim(),
                        isApproved = false
                    };
                    r.Insert();

                    rl.ReviewID = r.PRID;
                    rl.Update();
                    HttpContext.Current.Session["LastPRID"] = r.PRID;
                }
            }
            #endregion

            #region Award PatronPoints

            var pp = new PatronPoints {
                PID                 = patron.PID,
                NumPoints           = points,
                AwardDate           = now,
                AwardReasonCd       = (int)reason,
                AwardReason         = PatronPoints.PointAwardReasonCdToDescription(reason),
                BadgeAwardedFlag    = false,
                isReading           = (reason == PointAwardReason.Reading),
                isEvent             = (reason == PointAwardReason.EventAttendance),
                isGameLevelActivity = (reason == PointAwardReason.MiniGameCompletion),
                isBookList          = (reason == PointAwardReason.BookListCompletion),
                isGame              = false,
                GameLevelActivityID = MGID,
                EventCode           = eventCode,
                EventID             = eventID,
                BookListID          = bookListID,
                LogID               = (rl == null ? 0 : rl.PRLID)
            };
            pp.Insert();

            var badgeAwarded = false;
            int badgeToAward = 0;               // <===========

            DAL.Minigame mg = null;
            if (reason == PointAwardReason.MiniGameCompletion)
            {
                mg           = DAL.Minigame.FetchObject(MGID);
                badgeAwarded = mg.AwardedBadgeID > 0;
                badgeToAward = mg.AwardedBadgeID;
            }
            if (reason == PointAwardReason.EventAttendance)
            {
                var evt = Event.GetEvent(eventID);
                badgeAwarded = evt == null ? false : evt.BadgeID > 0;
                badgeToAward = evt == null ? 0 : evt.BadgeID;
            }
            if (reason == PointAwardReason.BookListCompletion)
            {
                var bl = BookList.FetchObject(bookListID);
                ;
                badgeAwarded = (bl.AwardBadgeID > 0);
                badgeToAward = bl.AwardBadgeID;
            }

            DataSet pbds = null;
            if (badgeAwarded)
            {
                if (AwardBadgeToPatron(badgeToAward, patron, ref EarnedBadges))
                {
                    if (pp.PPID != 0)
                    {
                        pp.BadgeAwardedFlag = true;
                        pp.BadgeID          = badgeToAward;
                        pp.Update();
                    }
                }
            }

            #endregion

            #region If jumped level, award another badge(s)

            // since thay just earned points, check if they also advanced a level in the board game (if there is a board game for the program)
            EndingPoints = PatronPoints.GetTotalPatronPoints(patron.PID);
            EarnedBadge  = null;

            var earnedBadges2 = new List <Badge>();

            EarnedBadge = TallyPoints(patron, pgm, StartingPoints, EndingPoints, ref earnedBadges2);
            pbds        = PatronBadges.GetAll(patron.PID);

            foreach (var badge in earnedBadges2)
            {
                EarnedBadge = badge;

                if (EarnedBadge != null)
                {
                    AwardBadgeToPatron(EarnedBadge.BID, patron, ref EarnedBadges);
                }
            }

            #endregion

            #region Check and give awards if any

            AwardBadgeToPatronViaMatchingAwards(patron, ref EarnedBadges);

            #endregion

            #region Prepare return code
            // did they earn one or more badges?
            if (EarnedBadges.Count > 0)
            {
                retValue = string.Join("|", EarnedBadges.Select(b => b.BID).Distinct());
            }
            #endregion

            return(retValue);
        }
        // points = 100
        // reason = PointAwardReason.MiniGameCompletion
        // MGID = Mini Game ID, if the reason is MiniGameCompletion, else 0
        //
        //
        // returns: a string based list of the badges they earned, or an empty string
        public string AwardPointsToPatron(int points, PointAwardReason reason,
                                          // Minigame
                                          int MGID = 0,
                                          // reading
                                          ActivityType readingActivity = ActivityType.Pages, int readingAmount = 0, string author = "", string title = "", string review = "",
                                          // event
                                          string eventCode = "", int eventID = 0,
                                          // book List
                                          int bookListID = 0,

                                          DateTime?forceDate = null
                                          )
        {
            if (forceDate != null)
            {
                now = (DateTime)forceDate;
            }

            string retValue = "";

            #region Reading - Log to PatronReadingLog
            PatronReadingLog rl = null;
            if (reason == PointAwardReason.Reading)
            {
                rl = new PatronReadingLog
                {
                    PID              = patron.PID,
                    ReadingType      = (int)readingActivity,
                    ReadingTypeLabel = (readingActivity).ToString(),
                    ReadingAmount    = readingAmount,
                    ReadingPoints    = points,
                    LoggingDate      = FormatHelper.ToNormalDate(now),
                    Author           = author.Trim(),
                    Title            = title.Trim(),
                    HasReview        = (review.Trim().Length > 0),
                    ReviewID         = 0
                };
                rl.Insert();

                // If there is a review, record the review
                if (review.Trim().Length > 0)
                {
                    var r = new PatronReview
                    {
                        PID        = patron.PID,
                        PRLID      = rl.PRLID,
                        Author     = rl.Author,
                        Title      = rl.Title,
                        Review     = review.Trim(),
                        isApproved = false
                    };
                    r.Insert();

                    rl.ReviewID = r.PRID;
                    rl.Update();
                }
            }
            #endregion

            #region Award PatronPoints

            var pp = new PatronPoints
            {
                PID                 = patron.PID,
                NumPoints           = points,
                AwardDate           = now,
                AwardReasonCd       = (int)reason,
                AwardReason         = PatronPoints.PointAwardReasonCdToDescription(reason),
                BadgeAwardedFlag    = false,
                isReading           = (reason == PointAwardReason.Reading),
                isEvent             = (reason == PointAwardReason.EventAttendance),
                isGameLevelActivity = (reason == PointAwardReason.MiniGameCompletion),
                isBookList          = (reason == PointAwardReason.BookListCompletion),
                isGame              = false,
                GameLevelActivityID = MGID,
                EventCode           = eventCode,
                EventID             = eventID,
                BookListID          = bookListID,
                LogID               = (rl == null ? 0 : rl.PRLID)
            };
            pp.Insert();

            var badgeAwarded = false;
            int badgeToAward = 0;               // <===========

            DAL.Minigame mg = null;
            if (reason == PointAwardReason.MiniGameCompletion)
            {
                mg           = DAL.Minigame.FetchObject(MGID);
                badgeAwarded = mg.AwardedBadgeID > 0;
                badgeToAward = mg.AwardedBadgeID;
            }
            if (reason == PointAwardReason.EventAttendance)
            {
                var evt = Event.GetEvent(eventID);
                badgeAwarded = evt.BadgeID > 0;
                badgeToAward = evt.BadgeID;
            }
            if (reason == PointAwardReason.BookListCompletion)
            {
                var bl = BookList.FetchObject(bookListID);;
                badgeAwarded = (bl.AwardBadgeID > 0);
                badgeToAward = bl.AwardBadgeID;
            }

            DataSet pbds = null;
            //DataTable newTable = null;
            if (badgeAwarded)
            {
                if (AwardBadgeToPatron(badgeToAward, patron, ref EarnedBadges))
                {
                    if (pp.PPID != 0)
                    {
                        pp.BadgeAwardedFlag = true;
                        pp.BadgeID          = badgeToAward;
                        pp.Update();
                    }
                }
                #region AwardBadgeToPatron code
                //// check if badge was already earned...
                //pbds = PatronBadges.GetAll(patron.PID);
                //var a = pbds.Tables[0].AsEnumerable().Where(r => r.Field<int>("BadgeID") == badgeToAward);

                //newTable = new DataTable();
                //try { newTable = a.CopyToDataTable(); } catch { }

                //// badge not found, award it!
                //if (newTable.Rows.Count == 0)
                //{
                //    var pb = new PatronBadges { BadgeID = badgeToAward, DateEarned = now, PID = patron.PID };
                //    pb.Insert();

                //    EarnedBadge = Badge.GetBadge(badgeToAward);
                //    EarnedBadges.Add(EarnedBadge);

                //    //if badge generates notification, then generate the notification
                //    if (EarnedBadge.GenNotificationFlag)
                //    {
                //        var not = new Notifications
                //        {
                //            PID_To = patron.PID,
                //            PID_From = 0,  //0 == System Notification
                //            Subject = EarnedBadge.NotificationSubject,
                //            Body = EarnedBadge.NotificationBody,
                //            isQuestion = false,
                //            AddedDate = now,
                //            LastModDate = now,
                //            AddedUser = patron.Username,
                //            LastModUser = "******"
                //        };
                //        not.Insert();
                //    }

                //    //if badge generates prize, then generate the prize
                //    if (EarnedBadge.IncludesPhysicalPrizeFlag)
                //    {
                //        var ppp = new DAL.PatronPrizes
                //                      {
                //                          PID = patron.PID,
                //                          PrizeSource = 1,
                //                          PrizeName = EarnedBadge.PhysicalPrizeName,
                //                          RedeemedFlag = false,
                //                          AddedUser = patron.Username,
                //                          LastModUser = "******",
                //                          AddedDate = now,
                //                          LastModDate = now
                //                      };

                //        ppp.Insert();
                //    }



                //    // if badge generates award code, then generate the code
                //    if (EarnedBadge.AssignProgramPrizeCode)
                //    {
                //        var RewardCode = "";
                //        // get the Code value
                //        // save the code value for the patron
                //        RewardCode = ProgramCodes.AssignCodeForPatron(patron.ProgID, patron.ProgID);

                //        // generate the notification
                //        var not = new Notifications
                //        {
                //            PID_To = patron.PID,
                //            PID_From = 0,  //0 == System Notification
                //            Subject = EarnedBadge.PCNotificationSubject,
                //            Body = EarnedBadge.PCNotificationBody.Replace("{ProgramRewardCode}", RewardCode),
                //            isQuestion = false,
                //            AddedDate = now,
                //            LastModDate = now,
                //            AddedUser = patron.Username,
                //            LastModUser = "******"
                //        };
                //        not.Insert();
                //    }

                //    if (pp.PPID != 0)
                //    {
                //        pp.BadgeAwardedFlag = true;
                //        pp.BadgeID = badgeToAward;
                //        pp.Update();
                //    }
                //}
                #endregion
            }

            #endregion

            #region If jumped level, award another badge(s)

            // since thay just earned points, check if they also advanced a level in the board game (if there is a board game for the program)
            EndingPoints = PatronPoints.GetTotalPatronPoints(patron.PID);
            EarnedBadge  = null;

            var earnedBadges2 = new List <Badge>();

            EarnedBadge = TallyPoints(patron, pgm, StartingPoints, EndingPoints, ref earnedBadges2);
            pbds        = PatronBadges.GetAll(patron.PID);

            foreach (var badge in earnedBadges2)
            {
                EarnedBadge = badge;

                if (EarnedBadge != null)
                {
                    AwardBadgeToPatron(EarnedBadge.BID, patron, ref EarnedBadges);
                }

                #region AwardBadgeToPatron call2
                //// check if badge was already earned...
                //if (EarnedBadge != null)
                //{
                //    var aa = pbds.Tables[0].AsEnumerable().Where(r => r.Field<int>("BadgeID") == badgeToAward);

                //    newTable = new DataTable();
                //    try { newTable = aa.CopyToDataTable(); }
                //    catch { }

                //    // badge not found, award it!
                //}
                //if (EarnedBadge != null && newTable != null && newTable.Rows.Count == 0)
                //{
                //    // Award the badge

                //    EarnedBadges.Add(badge);  // Add badge tot he list used to display to user ...

                //    var newPBID = 0;
                //    var pb = new PatronBadges { BadgeID = EarnedBadge.BID, DateEarned = now, PID = patron.PID };
                //    pb.Insert();
                //    newPBID = pb.PBID;

                //    //if badge generates notification, then generate the notification
                //    if (EarnedBadge.GenNotificationFlag)
                //    {
                //        var not = new Notifications
                //        {
                //            PID_To = patron.PID,
                //            PID_From = 0,  //0 == System Notification
                //            Subject = EarnedBadge.NotificationSubject,
                //            Body = EarnedBadge.NotificationBody,
                //            isQuestion = false,
                //            AddedDate = now,
                //            LastModDate = now,
                //            AddedUser = patron.Username,
                //            LastModUser = "******"
                //        };
                //        not.Insert();
                //    }

                //    //if badge generates prize, then generate the prize
                //    if (EarnedBadge.IncludesPhysicalPrizeFlag)
                //    {
                //        var ppp = new DAL.PatronPrizes
                //        {
                //            PID = patron.PID,
                //            PrizeSource = 1,
                //            PrizeName = EarnedBadge.PhysicalPrizeName,
                //            RedeemedFlag = false,
                //            AddedUser = patron.Username,
                //            LastModUser = "******",
                //            AddedDate = now,
                //            LastModDate = now
                //        };

                //        ppp.Insert();
                //    }

                //    // if badge generates award code, then generate the code
                //    if (EarnedBadge.AssignProgramPrizeCode)
                //    {
                //        var RewardCode = "";
                //        // get the Code value
                //        // save the code value for the patron
                //        RewardCode = ProgramCodes.AssignCodeForPatron(patron.ProgID, patron.ProgID);

                //        // generate the notification
                //        var not = new Notifications
                //        {
                //            PID_To = patron.PID,
                //            PID_From = 0,  //0 == System Notification
                //            Subject = EarnedBadge.PCNotificationSubject,
                //            Body = EarnedBadge.PCNotificationBody.Replace("{ProgramRewardCode}", RewardCode),
                //            isQuestion = false,
                //            AddedDate = now,
                //            LastModDate = now,
                //            AddedUser = patron.Username,
                //            LastModUser = "******"
                //        };
                //        not.Insert();
                //    }
                //}
                #endregion
            }

            #region deprecated - replaced by abbility to earn multiple badges above
            //EarnedBadge = TallyPoints(patron, pgm, StartingPoints, EndingPoints, ref EarnedBadges);

            //// check if badge was already earned...
            //if (EarnedBadge != null) {
            //    pbds = PatronBadges.GetAll(patron.PID);
            //    var aa = pbds.Tables[0].AsEnumerable().Where(r => r.Field<int>("BadgeID") == badgeToAward);

            //    newTable = new DataTable();
            //    try { newTable = aa.CopyToDataTable(); } catch { }

            //    // badge not found, award it!
            //}
            //if (EarnedBadge != null && newTable!= null && newTable.Rows.Count == 0)
            //{
            //    // Award the badge
            //    var newPBID = 0;
            //    var pb = new PatronBadges { BadgeID = EarnedBadge.BID, DateEarned = now, PID = patron.PID };
            //    pb.Insert();
            //    newPBID = pb.PBID;

            //    //if badge generates notification, then generate the notification
            //    if (EarnedBadge.GenNotificationFlag)
            //    {
            //        var not = new Notifications
            //        {
            //            PID_To = patron.PID,
            //            PID_From = 0,  //0 == System Notification
            //            Subject = EarnedBadge.NotificationSubject,
            //            Body = EarnedBadge.NotificationBody,
            //            isQuestion = false,
            //            AddedDate = now,
            //            LastModDate = now,
            //            AddedUser = patron.Username,
            //            LastModUser = "******"
            //        };
            //        not.Insert();
            //    }

            //    //if badge generates prize, then generate the prize
            //    if (EarnedBadge.IncludesPhysicalPrizeFlag)
            //    {
            //        var ppp = new DAL.PatronPrizes
            //        {
            //            PID = patron.PID,
            //            PrizeSource = 1,
            //            PrizeName = EarnedBadge.PhysicalPrizeName,
            //            RedeemedFlag = false,
            //            AddedUser = patron.Username,
            //            LastModUser = "******",
            //            AddedDate = now,
            //            LastModDate = now
            //        };

            //        ppp.Insert();
            //    }

            //    // if badge generates award code, then generate the code
            //    if (EarnedBadge.AssignProgramPrizeCode)
            //    {
            //        var RewardCode = "";
            //        // get the Code value
            //        // save the code value for the patron
            //        RewardCode = ProgramCodes.AssignCodeForPatron(patron.ProgID, patron.ProgID);

            //        // generate the notification
            //        var not = new Notifications
            //        {
            //            PID_To = patron.PID,
            //            PID_From = 0,  //0 == System Notification
            //            Subject = EarnedBadge.PCNotificationSubject,
            //            Body = EarnedBadge.PCNotificationBody.Replace("{ProgramRewardCode}", RewardCode),
            //            isQuestion = false,
            //            AddedDate = now,
            //            LastModDate = now,
            //            AddedUser = patron.Username,
            //            LastModUser = "******"
            //        };
            //        not.Insert();
            //    }
            #endregion


            #endregion

            #region Check and give awards if any

            AwardBadgeToPatronViaMatchingAwards(patron, ref EarnedBadges);

            #endregion

            #region Prepare return code
            // did they earn one or more badges?
            if (EarnedBadges.Count > 0)
            {
                var badges = EarnedBadges.Count.ToString();
                //foreach(Badge b in EarnedBadges)
                //{
                //    badges = badges + "|" + b.BID.ToString();
                //}
                badges = EarnedBadges.Aggregate(badges, (current, b) => current + "|" + b.BID.ToString());
                //Response.Redirect("~/BadgeAward.aspx?b=" + badges);
                retValue = badges;
            }
            #endregion

            return(retValue);
        }