Ratings 2.0

From MoodleDocs
Revision as of 07:35, 20 January 2010 by Andrew Davis (talk | contribs) (Largely copied from en/Development:Comments_2.0)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The goals of ratings 2.0:

  • Manage ratings centrally
  • Use a consistent approach for all ratings throughout Moodle
  • Easily integrate ratings 2.0 with existing modules
  • Remove duplicate implementations of ratings functionality


The ratings 2.0 provides APIs to:

  1. Add ratings
  2. Update ratings

And provides an ajax interface to allow adding ratings on a floating DIV.

Ratings database table

Field Type Default Info
id int(10) auto-incrementing The unique ID for this comment.
userid int(10) who wrote this comment
contextid int(10) The context id defined in context table - identifies the instance of plugin owning the comment.
itemid int(10) Some plugin specific item id (eg. forum post, blog entry or assignment submission)
rating int(10) for example, in user profile, you can comment user's description or interests, but they share the same itemid(==userid), we need comment_area to separate them
timecreated int(10)
timemodified int(10)
content text content of comment

Ratings API


Add an option to format_text function

Using this format_text function will add a comment icon automatically at the end of the text:

For example, using the following code in the forum module will add a rating icon to every post:

$cmt = new stdclass; $cmt->contextid = $modcontext->id; $cmt->area = 'format_post'; $cmt->itemid = $post->id; $options->ratings = $cmt;

echo format_text($post->message, $post->messageformat, $options, $course->id)."



class rating()

__construct($contextid, $comment_area, $itemid))

Initialize class members


Print Javascript required by rating API, this is a static function, which must be called before html head printed</head>

init($return = false)

Will print the html snippet for rating UI, will be called inside format_text

print_comments($params = array())

print comments


Add rating to database


delete a rating

Javascript API

Can be found in lib/rating.js


Submit a rating

view_ratings(client_id, contextid, itemid, page)

Display ratings, will call get_ratings to load comments.

get_comments(client_id, contextid, itemid, page)

Load comments by AJAX

Moodle modules callback

Ratings API allows modules/blocks/blog controlling comments

Permission control

Modules can implement a function named modname_rating_permissions to control post and view permission.

Blocks need to overwrite review_permissions function of block_base.

Blog need to implement review_comment_permissions function.

This function will return an array: array('post'=>true, 'view'=>true)

Check new added rating

Modules can implement a function named modname_comment_add.

Blocks need to overwrite comment_add function.

Blog need to implement blog_comment_add function.

Comments API will pass the comment to this function which allow modules check/modify ratings, or reject this rating.

Filter/format comments

Modules can implement a function named modname_rating_display.

Blocks need to overwrite rating_display function.

Blog need to implement blog_rating_display function.

This callback allows modules check/format ratings when user requests to display rating.

get original url


Inline UI

File:moodle commenting inline UIXX.png