Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
/rating/ -> lib.php (source)

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

A class representing a single rating and containing some static methods for manipulating ratings

Copyright: 2010 Andrew Davis
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1237 lines (53 kb)
Included or required: 1 time
Referenced: 12 times
Includes or requires: 0 files

Defines 3 classes


Class: rating  - X-Ref

The rating class represents a single rating by a single user

__construct($options)   X-Ref
Constructor.

param: stdClass $options {

update_rating($rating)   X-Ref
Update this rating in the database

param: int $rating the integer value of this rating

get_rating()   X-Ref
Retreive the integer value of this rating

return: int the integer value of this rating object

get_aggregate_string()   X-Ref
Returns this ratings aggregate value as a string.

return: string ratings aggregate value

user_can_rate($userid = null)   X-Ref
Returns true if the user is able to rate this rating object

param: int $userid Current user assumed if left empty
return: bool true if the user is able to rate this rating object

user_can_view_aggregate($userid = null)   X-Ref
Returns true if the user is able to view the aggregate for this rating object.

param: int|null $userid If left empty the current user is assumed.
return: bool true if the user is able to view the aggregate for this rating object

get_view_ratings_url($popup = false)   X-Ref
Returns a URL to view all of the ratings for the item this rating is for.

If this is a rating of a post then this URL will take the user to a page that shows all of the ratings for the post
(this one included).

param: bool $popup whether of not the URL should be loaded in a popup
return: moodle_url URL to view all of the ratings for the item this rating is for.

get_rate_url($rating = null, $returnurl = null)   X-Ref
Returns a URL that can be used to rate the associated item.

param: int|null          $rating    The rating to give the item, if null then no rating param is added.
param: moodle_url|string $returnurl The URL to return to.
return: moodle_url can be used to rate the associated item.

Class: rating_manager  - X-Ref

The rating_manager class provides the ability to retrieve sets of ratings from the database

delete_ratings($options)   X-Ref
Delete one or more ratings. Specify either a rating id, an item id or just the context id.

param: stdClass $options {

get_all_ratings_for_item($options)   X-Ref
Returns an array of ratings for a given item (forum post, glossary entry etc).

This returns all users ratings for a single item

param: stdClass $options {
return: array an array of ratings

get_ratings($options)   X-Ref
Adds rating objects to an array of items (forum posts, glossary entries etc). Rating objects are available at $item->rating

param: stdClass $options {
return: array the array of items with their ratings attached at $items[0]->rating

generate_rating_settings_object($options)   X-Ref
Generates a rating settings object based upon the options it is provided.

param: stdClass $options {
return: stdClass rating settings object

generate_rating_scale_object($scaleid)   X-Ref
Generates a scale object that can be returned

param: int $scaleid scale-type identifier
return: stdClass scale for ratings

get_item_time_created($item)   X-Ref
Gets the time the given item was created

TODO: MDL-31511 - Find a better solution for this, its not ideal to test for fields really we should be
asking the component the item belongs to what field to look for or even the value we
are looking for.

param: stdClass $item
return: int|null return null if the created time is unavailable, otherwise return a timestamp

get_user_grades($options)   X-Ref
Returns an array of grades calculated by aggregating item ratings.

param: stdClass $options {
return: array the array of the user's grades

get_aggregate_types()   X-Ref
Returns array of aggregate types. Used by ratings.

return: array aggregate types

get_aggregation_method($aggregate)   X-Ref
Converts an aggregation method constant into something that can be included in SQL

param: int $aggregate An aggregation constant. For example, RATING_AGGREGATE_AVERAGE.
return: string an SQL aggregation method

get_plugin_permissions_array($contextid, $component, $ratingarea)   X-Ref
Looks for a callback like forum_rating_permissions() to retrieve permissions from the plugin whose items are being rated

param: int $contextid The current context id
param: string $component the name of the component that is using ratings ie 'mod_forum'
param: string $ratingarea The area the rating is associated with
return: array rating related permissions

check_rating_is_valid($params)   X-Ref
Validates a submitted rating

param: array $params submitted data
return: boolean true if the rating is valid, false if callback not found, throws rating_exception if rating is invalid

initialise_rating_javascript(moodle_page $page)   X-Ref
Initialises JavaScript to enable AJAX ratings on the provided page

param: moodle_page $page
return: true always returns true

get_aggregate_label($aggregationmethod)   X-Ref
Returns a string that describes the aggregation method that was provided.

param: string $aggregationmethod
return: string describes the aggregation method that was provided

add_rating($cm, $context, $component, $ratingarea, $itemid, $scaleid, $userrating, $rateduserid,$aggregationmethod)   X-Ref
Adds a new rating

param: stdClass $cm course module object
param: stdClass $context context object
param: string $component component name
param: string $ratingarea rating area
param: int $itemid the item id
param: int $scaleid the scale id
param: int $userrating the user rating
param: int $rateduserid the rated user id
param: int $aggregationmethod the aggregation method

get_component_ratings_since($context, $component, $since)   X-Ref
Get ratings created since a given time.

param: stdClass $context   context object
param: string $component  component name
param: int $since         the time to check
return: array list of ratings db records since the given timelimit

Class: rating_exception  - X-Ref

The rating_exception class for exceptions specific to the ratings system

__construct($errorcode)   X-Ref
Generate exceptions that can be easily identified as coming from the ratings system

param: string $errorcode the error code to generate