Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Ratings 2.0

From MoodleDocs

Objectives

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

Andrew, did you see I had dumped some unsolicited comments on the talk page?--Tim Hunt 11:13, 21 January 2010 (UTC)

Overview

The ratings 2.0 provides APIs to:

  1. Add ratings
  2. Update ratings
  3. Access collected ratings for grade calculation purposes

Ratings database table

Field Type Default Info
id int(10) auto-incrementing The unique ID for 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)
scaleid int(10) ID of the scale (1-5, 0-100, custom) from which the user selected their rating. Including this allows smarter handling of scales being changed.
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
userid int(10) who wrote this comment
timecreated int(10)
timemodified int(10)

Removed database tables

The following tables will be have their data migrated to the above ratings table and then be removed:

data_ratings

forum_ratings

glossary_ratings

Unaltered database relationships

Course modules will continue to store the scale associated with their ratings. For example the glossary table has a scale column. Numbers greater than zero indicate a numerical rating should be used with the scale value being the maximum value. Numbers below zero are the primary key of a row in the scale table (-2 = a primary key of 2). The behaviour of a scale with the value 0 is currently unknown.

When a rating is made the scale id will be included in the new row in the rating table. This will allow for intelligent handling of entered ratings should the scale be changed after some ratings have been entered.

Ratings API

lib/ratinglib.php will contain...

class Rating

__construct($contextid, $itemid))

Initialize class instance storing the context and item id

add($userid, $rating)

Add rating to database

delete($userid)

delete the rating for the supplied user

get($userid)

get the rating for the supplied user

get()

get an array of ratings for the current context and item


Ratings Renderer

Located in mod/ratings/renderer.php the new class core_rating_renderer should extend core_renderer defined in lib/outputrenderers.php

core_rating_renderer methods:

render($rating)

returns rating UI html snippet. Used to include ratings in pages.

Javascript API

A Javascript API will not be required initially.

Moodle modules callback

Ratings API allows modules/blocks/blog controlling ratings

Permission control

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

Blocks need to overwrite rating_permissions function of block_base.

Blog need to implement blog_rating_permissions function.

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

Check new added rating

Modules can implement a function named modname_rating_add.

Blocks need to overwrite rating_add function.

Blog need to implement blog_rating_add function.

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

Filter ratings

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 ratings when user requests to display rating.

Interface

RatingUI.gif

When Javascript is enabled ajax submission means the button can be removed. In the future it would be possible to automatically interpret a 1 to 5 rating as a star rating style UI element.