? ? ? Success

User tests: Successful: Unsuccessful:

avatar Spudley
Spudley
2 Oct 2018

Pull Request for Issue # n/a.

Summary of Changes

This PR adds two new events to the Joomla core, which are triggered when a user votes for an article using the built-in star ratings system.

The events are named onRatingBeforeSave and onRatingAfterSave. Methods with these names can be added to any content plugin. As the names suggest, they are called immediately before and after the rating vote is saved.

Both methods have the same signature. Arguments are:

  • $context - this is currently always 'com_content.article', but is provided to allow for future expansion.
  • $id - the article ID that has been voted on.
  • $user_rating - the score that the user gave the article.

If onRatingBeforeSave returns false then it will prevent the rating from being saved. No error message will be generated; it is up to the plugin to provide one.

If onRatingAfterSave returns false, then it will suppress the default success message. This allows the plugin to provide its own alternative message.

Motivation

I wrote this PR after realising that the built-in star ratings system was somewhat limited. For example, it only stores the single most recent IP address that voted, so it would be very easy for someone to vote an unlimited times simply by alternating their IP.

This PR provides a way for these limitations to be overcome without having to overhaul the existing system. Plugins can now be used to give additional validation. It also gives the possibility of logging votes, reporting on votes, emailing content authors "someone has voted on your article!", providing more detailed messages after voting (eg "thank you. you are the 12th person to vote on this article today").

Testing Instructions

A simple plugin to test this could look like this:

class plgContentRatingsTest extends JPlugin
{
    public function onRatingBeforeSave($context, $id, $rate)
    {
        if ($rate == 5) {
            // Prove that it works by not allowing a score of 5.
            JFactory::getApplication()->enqueueMessage("Sorry, this content isn't good enough; you can't give it top marks.", 'error');
            return false;
        }
        return true;
    }

    public function onRatingAfterSave($context, $id, $rate)
    {
        // Just override the default message.
        JFactory::getApplication()->enqueueMessage('Good news! Your vote has been recorded!', 'message');
        return false;
    }
}

Documentation Changes Required

The new methods should be documented with the other content plugin methods.

avatar Spudley Spudley - open - 2 Oct 2018
avatar Spudley Spudley - change - 2 Oct 2018
Status New Pending
avatar joomla-cms-bot joomla-cms-bot - change - 2 Oct 2018
Category Front End com_content
avatar infograf768 infograf768 - change - 3 Oct 2018
Title
Add event handlers for star rating votes.
[New Feature] Add event handlers for star rating votes.
avatar infograf768 infograf768 - edited - 3 Oct 2018
avatar Spudley Spudley - change - 3 Oct 2018
Labels Added: ? ?
avatar Spudley
Spudley - comment - 3 Oct 2018

@Quy Oops, sorry. I added those returns at the last minute and forgot to double-check the formatting on them. Hopefully that's better now.

avatar Harmageddon
Harmageddon - comment - 3 Oct 2018

I have tested this item successfully on d41071b

Looks good to me. I tested the patch with the provided example and checked the passed parameters. Works for single articles and categories.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/22468.

avatar Harmageddon
Harmageddon - comment - 3 Oct 2018

I have tested this item successfully on d41071b

Looks good to me. I tested the patch with the provided example and checked the passed parameters. Works for single articles and categories.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/22468.

avatar Harmageddon Harmageddon - test_item - 3 Oct 2018 - Tested successfully
avatar B3nito
B3nito - comment - 12 Nov 2018

In this connection is it possible to collect voting for each level seperately? Maybe by json data? its' useful to know if 55 Stars and 51 Stars or 52 and 54

avatar Spudley
Spudley - comment - 12 Nov 2018

In this connection is it possible to collect voting for each level seperately? Maybe by json data? its' useful to know if 5_5 Stars and 5_1 Stars or 5_2 and 5_4

If this PR gets approved, then theoretically yes, you could do that, because you'd be able to write a plugin for it.

avatar franz-wohlkoenig franz-wohlkoenig - change - 29 Mar 2019
Title
[New Feature] Add event handlers for star rating votes.
Add event handlers for star rating votes
avatar joomla-cms-bot joomla-cms-bot - edited - 29 Mar 2019
avatar franz-wohlkoenig franz-wohlkoenig - change - 29 Mar 2019
Category Front End com_content com_content Feature Request Front End
avatar Quy
Quy - comment - 15 May 2019

Rebase for J4?

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 16 May 2019

@Spudley please rebase as new Features go in J4.

avatar franz-wohlkoenig franz-wohlkoenig - change - 16 May 2019
Status Pending Closed
Closed_Date 0000-00-00 00:00:00 2019-05-16 05:20:50
Closed_By franz-wohlkoenig
Labels Added: ?
Removed: J3 Issue
avatar franz-wohlkoenig franz-wohlkoenig - close - 16 May 2019

Add a Comment

Login with GitHub to post a comment