User tests: Successful: Unsuccessful:
Pull Request for Issue # n/a.
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.
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").
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;
}
}
The new methods should be documented with the other content plugin methods.
Status | New | ⇒ | Pending |
Category | ⇒ | Front End com_content |
Title |
|
Labels |
Added:
?
?
|
I have tested this item
Looks good to me. I tested the patch with the provided example and checked the passed parameters. Works for single articles and categories.
I have tested this item
Looks good to me. I tested the patch with the provided example and checked the passed parameters. Works for single articles and categories.
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
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.
Title |
|
Category | Front End com_content | ⇒ | com_content Feature Request Front End |
Rebase for J4?
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 |
@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.