Exemple #1
0
        /// <summary>
        /// Gets the rating statistics for the page on which the RatingBlock resides
        /// </summary>
        /// <param name="target">The current page on which the RatingBlock resides</param>
        /// <param name="blockModel">a reference to the RatingBlockViewModel to
        /// populate with rating statistics for the current page and errors, if any</param>
        private void GetRatingStatistics(string target, RatingBlockViewModel blockModel)
        {
            blockModel.NoStatisticsFoundMessage = string.Empty;

            try
            {
                var result = _ratingRepository.GetRatingStatistics(target);
                if (result != null)
                {
                    blockModel.Average    = result.Average;
                    blockModel.TotalCount = result.TotalCount;
                }
                else
                {
                    var loggedInMessage          = "This page has not been rated. Be the first!";
                    var loggedOutMessage         = "This page has not been rated. Log in and be the first!";
                    var loggedInNotMemberMessage = "This page has not been rated. Join group and be the first!";
                    blockModel.NoStatisticsFoundMessage = User.Identity.IsAuthenticated
                                                            ? (blockModel.IsMemberOfGroup ? loggedInMessage : loggedInNotMemberMessage)
                                                            : loggedOutMessage;
                }
            }
            catch (SocialRepositoryException ex)
            {
                blockModel.Messages.Add(new MessageViewModel(ex.Message, ErrorMessage));
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets the rating for the logged in user
        /// </summary>
        /// <param name="target">The current page on which the RatingBlock resides</param>
        /// <param name="blockModel">a reference to the RatingBlockViewModel to
        /// populate with rating for the logged in user and errors, if any</param>
        private void GetRating(string target, RatingBlockViewModel blockModel)
        {
            blockModel.CurrentRating = null;

            try
            {
                var userId = _userRepository.GetUserId(User);
                if (!string.IsNullOrWhiteSpace(userId))
                {
                    blockModel.CurrentRating =
                        _ratingRepository.GetRating(new PageRatingFilter
                    {
                        Rater  = userId,
                        Target = target
                    });
                }
                else
                {
                    var message = "There was an error identifying the logged in user. Please make sure you are logged in and try again.";
                    blockModel.Messages.Add(new MessageViewModel(message, ErrorMessage));
                }
            }
            catch (SocialRepositoryException ex)
            {
                blockModel.Messages.Add(new MessageViewModel(ex.Message, ErrorMessage));
            }
        }
Exemple #3
0
        /// <summary>
        /// Render the rating block frontend view.
        /// </summary>
        /// <param name="currentBlock">The current frontend block instance.</param>
        /// <returns>The index action result.</returns>
        public override ActionResult Index(RatingBlock currentBlock)
        {
            var target = _pageRouteHelper.Page.ContentGuid.ToString();

            var groupName = _pageRouteHelper.Page is CommunityPage
                            ? ((CommunityPage)_pageRouteHelper.Page).Memberships.GroupName
                            : "";

            var group = string.IsNullOrEmpty(groupName)
                        ? null
                        : _communityRepository.Get(groupName);

            var currentPageLink = _pageRouteHelper.PageLink;

            // Create a rating block view model to fill the frontend block view
            var blockModel = new RatingBlockViewModel(currentBlock, currentPageLink)
            {
                //get messages for view
                Messages = RetrieveMessages(MessageKey)
            };

            // If user logged in, check if logged in user has already rated the page
            if (User.Identity.IsAuthenticated)
            {
                //Validate that the group exists
                if (group != null)
                {
                    var groupId      = group.Id;
                    var memberFilter = new CommunityMemberFilter
                    {
                        CommunityId = groupId,
                        PageSize    = 10000
                    };
                    var socialMembers = _memberRepository.Get(memberFilter).ToList();
                    var userId        = _userRepository.GetUserId(User);
                    blockModel.IsMemberOfGroup = socialMembers.FirstOrDefault(m => m.User.IndexOf(userId) > -1) != null;
                }
                GetRating(target, blockModel);
            }

            //Conditionally retrieving rating statistics based on any errors that might have been encountered
            var noMessages = blockModel.Messages.Count == 0;
            var noErrors   = blockModel.Messages.Any(x => x.Type != ErrorMessage);

            if (noMessages || noErrors)
            {
                GetRatingStatistics(target, blockModel);
            }

            return(PartialView("~/Features/Blocks/RatingBlock/RatingBlock.cshtml", blockModel));
        }