Exemplo n.º 1
0
        private void loadNextEntries()
        {
            if (!divPhotos.Visible) return;

            #region Prepare entries

            if (currentEntry == null)
            {
                if (Session["LastShowedEntries_" + contestId] is PhotoContestEntry[])
                {
                    leftEntry = ((PhotoContestEntry[])Session["LastShowedEntries_" + contestId])[0];
                    rightEntry = ((PhotoContestEntry[])Session["LastShowedEntries_" + contestId])[1];
                }
                else
                {
                    PhotoContestEntry[] entries = PhotoContestEntry.Load(null, contestId, null, null,
                                                                         CurrentUserSession.Username, 2, true);

                    if (entries == null || entries.Length < 2)
                    {
                        // TODO: show that there are no more entries
                        divPhotos.Visible = false;
                        return;
                    }
                    else
                    {
                        leftEntry = entries[0];
                        rightEntry = entries[1];
                    }
                }
            }
            else
            {
                PhotoContestRank[] ranks = PhotoContestRank.Load(CurrentUserSession.Username, contestId);

                if (ranks == null || ranks.Length == 0)
                {
                    // Should not happen
                    Global.Logger.LogWarning("PhotoContest_loadNextEntries",
                                             "Warning: There is a current photo but there are no ranks");
                    return;
                }

                leftEntry = currentEntry;
                if (currentEntryMinRank == null && currentEntryMaxRank == null
                    && ranks.Length > Config.Ratings.FavoriteEntriesCount)
                {
                    rightEntry = PhotoContestEntry.Load(ranks[Config.Ratings.FavoriteEntriesCount - 1].EntryId);
                }
                else
                {
                    if (currentEntryMinRank == null) currentEntryMinRank = 0;
                    if (currentEntryMaxRank == null) currentEntryMaxRank = ranks.Length + 1;

                    rightEntry = PhotoContestEntry.Load(
                        ranks[(currentEntryMinRank.Value + currentEntryMaxRank.Value)/2 - 1].EntryId);
                }
            }

            #endregion

            #region Display entries

            imgLeftPhoto.ImageUrl = ImageHandler.CreateImageUrl(leftEntry.PhotoId, 200, 300, false, false, false);
            lnkLeftPhoto.HRef = ImageHandler.CreateImageUrl(leftEntry.PhotoId, 800, 600, false, false, false);
            lnkLeftPhoto.Title = leftEntry.Username;
            linkLeftUsername.InnerHtml = leftEntry.Username;
            linkLeftUsername.HRef = UrlRewrite.CreateShowUserUrl(leftEntry.Username);
            linkLeftUsername.Target = "_new";

            imgRightPhoto.ImageUrl = ImageHandler.CreateImageUrl(rightEntry.PhotoId, 200, 300, false, false, false);
            lnkRightPhoto.HRef = ImageHandler.CreateImageUrl(rightEntry.PhotoId, 800, 600, false, false, false);
            lnkRightPhoto.Title = rightEntry.Username;
            linkRightUsername.InnerHtml = rightEntry.Username;
            linkRightUsername.HRef = UrlRewrite.CreateShowUserUrl(rightEntry.Username);
            linkRightUsername.Target = "_new";

            #endregion

            #region Save last two showed entries

            Session["LastShowedEntries_" + contestId] = new PhotoContestEntry[] { leftEntry, rightEntry };

            #endregion
        }
Exemplo n.º 2
0
        private void rankEntries(PhotoContestEntry pick, PhotoContestEntry nonpick)
        {
            Session["LastShowedEntries_" + contestId] = null;

            #region Check for duplicate ranking (usually caused by refreshing)

            string lastRanked = leftEntry.Id + "_" + rightEntry.Id;
            if ((string) Session["PhotoContestPage_lastRankEntries"] == lastRanked) return;
            Session["PhotoContestPage_lastRankEntries"] = lastRanked;

            #endregion

            #region Show last ranked

            divNoRankedMessage.Visible = false;
            divLastRankedMessage.Visible = true;

            imgLastLeft.ImageUrl = ImageHandler.CreateImageUrl(pick.PhotoId, 50, 50, false, false, false);
            linkLastLeft.InnerHtml = pick.Username;
            linkLastLeft.HRef = UrlRewrite.CreateShowUserUrl(pick.Username);
            linkLastLeft.Target = "_new";

            imgLastRight.ImageUrl = ImageHandler.CreateImageUrl(nonpick.PhotoId, 50, 50, false, false, false);
            linkLastRight.InnerHtml = nonpick.Username;
            linkLastRight.HRef = UrlRewrite.CreateShowUserUrl(nonpick.Username);
            linkLastRight.Target = "_new";

            lblVotersAgree.Text = String.Format(Lang.Trans("{0}% of voters agree"), Convert.ToInt32(
                                                                                       PhotoContestVotes.FetchPercentage
                                                                                           (pick.Id, nonpick.Id).
                                                                                           GetValueOrDefault(100)));

            #endregion

            #region Save vote

            try
            {
                PhotoContestVotes.SaveVote(CurrentUserSession.Username, pick.Id, nonpick.Id);
            }
            catch (Exception err)
            {
                Global.Logger.LogError("rankEntries", err);
            }

            #endregion

            #region Update ranking

            PhotoContestRank[] ranks = PhotoContestRank.Load(CurrentUserSession.Username, contestId);

            if (ranks == null || ranks.Length == 0)
            {
                PhotoContestRank rank1 = new PhotoContestRank(CurrentUserSession.Username, contestId,
                                                              pick.Id, 1);
                rank1.Save();
                PhotoContestRank rank2 = new PhotoContestRank(CurrentUserSession.Username, contestId,
                                                              nonpick.Id, 2);
                rank2.Save();
                currentEntry = null;
                currentEntryMinRank = null;
                currentEntryMaxRank = null;
                loadPersonalFavs();
            }
            else if (currentEntry == null)
            {
                currentEntry = pick;
                currentEntryMinRank = null;
                currentEntryMaxRank = null;
            }
            else
            {
                if (currentEntry.Id == pick.Id)
                {
                    // The user picked the newly introduced photo
                    foreach (PhotoContestRank rank in ranks)
                    {
                        // Find the rank of the other (already ranked) photo
                        if (rank.EntryId == nonpick.Id)
                        {
                            // Check if the picked photo reached first rank
                            if (rank.Value == 1)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, pick.Id, 1);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                                // Check if the picked photo found its rank
                            else if (rank.Value - currentEntryMinRank == 1)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, pick.Id, rank.Value);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                            else
                            {
                                // Limit the maximum rank
                                currentEntryMaxRank = rank.Value;
                            }
                            break;
                        }
                    }
                }
                else
                {
                    // The user picked one if his previously ranked photos
                    foreach (PhotoContestRank rank in ranks)
                    {
                        // Find the rank of the picked (already ranked) photo
                        if (rank.EntryId == pick.Id)
                        {
                            // Check if the non picked photo is going out of the chart
                            if (rank.Value == Config.Ratings.FavoriteEntriesCount)
                            {
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                            }
                                // Check if the picked photo found its rank
                            else if (currentEntryMaxRank - rank.Value == 1 || rank.Value == ranks.Length)
                            {
                                PhotoContestRank newRank = new PhotoContestRank(CurrentUserSession.Username,
                                                                                contestId, nonpick.Id, rank.Value + 1);
                                newRank.Save();
                                currentEntry = null;
                                currentEntryMinRank = null;
                                currentEntryMaxRank = null;
                                loadPersonalFavs();
                            }
                            else
                            {
                                // Limit the minimum rank
                                currentEntryMinRank = rank.Value;
                            }
                            break;
                        }
                    }
                }
            }

            #endregion
        }
Exemplo n.º 3
0
        protected void btnEnterContest_Click(object sender, EventArgs e)
        {
            int photoId = 0;
            for (int i = 0; i < dlPhotos.Items.Count; i++)
            {
                RadioButton rb = (RadioButton)dlPhotos.Items[i].FindControl("rbPhoto");
                if (rb == null) continue;
                if (rb.Checked)
                {
                    HiddenField hid = (HiddenField)dlPhotos.Items[i].FindControl("hidPhotoId");
                    int.TryParse(hid.Value, out photoId);
                    break;
                }
            }
            if (photoId == 0)
            {
                lblError.Text = Lang.Trans("Please select a photo!");
                return;
            }

            PhotoContestEntry prevEntry = PhotoContestEntry.Load(contestId, CurrentUserSession.Username);
            if (prevEntry != null)
            {
                lblError.Text = Lang.Trans("You already participate in this contest!");
                return;
            }

            PhotoContestEntry entry = new PhotoContestEntry(contestId, CurrentUserSession.Username, photoId);
            entry.Save();

            #region Add Event

            Event newEvent = new Event(CurrentUserSession.Username);

            newEvent.Type = Event.eType.FriendEntersContest;
            FriendEntersContest friendEntersContest = new FriendEntersContest();
            friendEntersContest.PhotoContestEntriesID = entry.Id;
            newEvent.DetailsXML = Misc.ToXml(friendEntersContest);

            newEvent.Save();

            PhotoContest contest = PhotoContest.Load(entry.ContestId);
            string[] usernames = Classes.User.FetchMutuallyFriends(CurrentUserSession.Username);

            foreach (string friendUsername in usernames)
            {
                if (Config.Users.NewEventNotification)
                {
                    if (contest != null)
                    {
                        string text =
                                    String.Format("Your friend {0} has entered the {1} contest".Translate(),
                                                  "<b>" + CurrentUserSession.Username + "</b>", contest.Name);
                        string thumbnailUrl = ImageHandler.CreateImageUrl(photoId, 50, 50, false, true, false);
                        Classes.User.SendOnlineEventNotification(CurrentUserSession.Username, friendUsername, text,
                                                                 thumbnailUrl, "PhotoContest.aspx?cid" + contest.Id);
                    }
                }
            }

            #endregion

            StatusPageMessage = Lang.Trans("Your contest entry has been saved!");
            StatusPageLinkText = Lang.Trans("Back to contest");
            StatusPageLinkURL = "PhotoContest.aspx?cid=" + contestId;
            Response.Redirect("ShowStatus.aspx");
        }