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

Ratings are grades entered away from the gradebook. They can be entered by students and teachers and are aggregated into grades.

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

Overview

The ratings 2.0 provides APIs to:

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

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 previously entered ratings should the scales be 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)

Altered tables

Add two new columns to the table Scale. A non-numeric scale has a list of comma separated values in the field "scale". A numeric scale has a null "scale" and instead has a "scale_min" and "scale_max". Numeric scales should not appear in lists of scales presented to the user.

Field Type Default Info
scale_max int(10) For numerical scales. The maximum value.
scale_min int(10) For numerical scales. The minimum value.

When the administrator selects a numerical scale, check for a suitable one in the Scales table. Create one if one isn't found.

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 previously entered ratings should the scale be changed after some ratings have been entered.

Removed database tables

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

data_ratings

forum_ratings

glossary_ratings

Migrating Scales

Course modules will continue to store the scale associated with their ratings. For example the glossary table has a scale column.

Previously a Scale value, like glossary.scale, that was greater than zero indicated a numerical rating with minimum value of zero and the Scale value being the maximum value. Numbers below zero were 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 unknown. This behaviour made joins in SQL difficult or impossible and meant it wasn't posssible to have a non-zero minimum value for numerical scales.

Existing scale columns will need to be converted. Numerical scales used will need to be added to the Scale table and the Scale column, ie glossary.scale, updated to reference this new row in Scale. Once that is done any scales with negative values can have their values flipped, -4 to 4, so that they also reference a row in the Scale table.

This should cause all Scale values like Glossary.Scale and every row in the Rating table being able to be joined with a row in the Scale table.

Ratings API

lib/ratinglib.php will contain...

class rating

__construct($contextid, $itemid, $scaleid, $userid)

Initialize a class instance.

update_rating($rating)

Add rating to database

get_rating()

get the rating

delete_rating()

delete the rating

render_rating($page = null)

Load the rating renderer and use it to output this rating's UI. See Rating Submission for required fields.

Ratings Renderer

Located in mod/ratings/renderer.php the new class core_rating_renderer should extend plugin_renderer_base defined in lib/outputrenderers.php Almost all renderers appear to inherit from plugin_renderer_base rather than core_renderer.

core_rating_renderer methods

render_rating($rating)

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

Using the rating renderer

The process to render ratings is as follows:

There are two possible methods that could be implemented to retrieve ratings from the database.

Method 1

Access an item's rating like this: $post->rating // in Some forum/lib.php function call that loads forum posts (for example) $sql = "select forumfields {rating_system::get_ratings_sql_fields()} from forumtables {rating_system::get_ratings_sql_join()}"; $rs = $DB->get_recordset_sql($sql);

foreach ($rs as $post) { $post = make_rating_subobj($post); $posts[$post->id] = $post; }

foreach ($posts as $postid => $post) {

   $forumoutput->post($post, $post->rating);//or rating member variable could just be accessed with the function instead of being passed in

}

This would require that lib/ratinglib.php define three functions:

  • get_ratings_sql_fields(): The field names to go into the select part of an SQL query
  • get_ratings_sql_join(): The table join to go into an SQL query
  • make_rating_subobj(): Converts fields from a result set into an object. This will work in the same was as make_context_subobj() in lib/accesslib.php

Method 2

Access an item's rating like this: $ratings[$post->id] // in mod/forum/discuss.php (for example) $posts = // Some forum/lib.php function call.

//The current scaleid comes from the forum or glossary object and may be changed at any time so supply it each time //Also, a user should only see their own ratings $ratings = rating_system::load_ratings($contextid, $scaleid, $userid, $itemids = null/* Optional array of items ids. If null returns all ratings for the context by the user*/);

//Or, just return all ratings for the given context. Used when you want to aggregate the ratings in some way. $ratings = rating_system::load_ratings($contextid); /* This will probably return a multidimensional array accessed by item id (the forum post or glossary item id) then user id. $ratings[$itemid][$userid]

foreach($ratings[$itemid] as $itemrating) { //do something with the ratings for this item }

  • /

// $posts and $ratings are both indexed by $postid == $itemid.

foreach ($posts as $postid => $post) {

   $forumoutput->post($post, $ratings[$postid]);

}

Common code

The following is common to both methods $permissions = forum_rating_permission();//implemented by the module. returns array('view'=>true/false,'post'=>true/false)

// in mod/forum/renderer.php, in the post(post, $rating) method:

// ... output most of the post ... echo $post->rating->render_rating($this->page,$permissions); // ... output the rest.


// and finally in rating/lib.php in the Rating class:

public function Rating::render_rating($page = null, $permissions=null) {

   if (is_null($page) {
       global $PAGE;
       $page = $PAGE;
   }
   $ratingoutput = $page->get_renderer('core', 'rating');
   return $ratingoutput->render_rating($this,$permissions);

}


The key points there are:

  1. the details of how the rating is rendered (that is, that you need to get a particular sort of renderer) should be encapsulated, rather than having to be duplicated everywhere that a rating is output. This is the approach I have adopted in my new question engine, where there is even more complication to hide. Try following though the code for outputting a question, starting from http://github.com/timhunt/Moodle-Question-Engine-2/blob/new_qe/mod/quiz/attempt.php#L150.
  2. the rating data for all the ratings needed by a particular page need to be loaded from the database in a single query, that should be hidden behind some convenient API. It has to be a single query for performance reasons. Actually, it would be even better if the ratings could be loaded as part of the same query that loads the forum posts, by left-joining on the ratings table. That requires closer coupling between the forum and rating code, but is worth it for the performance gain.

Rating Submission

lib/rating.php will be the target for posted ratings. Currently each module implements their own ratings submission. For example mod/glossary/rate.php within the glossary module.

The supplied fields should consist of

Field Type Default Info
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
returnurl string Null for ajax requests. If not null the url to which the user should be redirected after recording the rating

The process to record a rating is as follows:

$permissions = forum_rating_permission(); if($permissions['post']) { $rating = N; //the actual rating from the user $ratingObj = new Rating($contextid, $scaleid, $userid, array($itemid)); $ratingObj->set_rating($rating); //redirect to return url if supplied }

//within the class Rating function Rating::set_rating($rating) { $ratings = rating_system::load_ratings($scaleid, $userid, $contextid, array($itemid)); if( !$ratings || sizeof($ratings)==0) { $data->contextid = $this->contextid; $data->scaleid = $this->scaleid; $data->userid = $this->userid; $data->rating = $rating; $DB->insert_record($this->table, $data); } else { $data->id = $this->id; $data->rating = $rating; $DB->update_record($this->table, $data); }

}


Moodle modules callback

modname_rating_permissions

Modules can implement a function named modname_rating_permissions to control post and view permission. This is called prior to rendering a set of ratings. It is also called by lib/rating.php when it receives and ajax rating submission.

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

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.

The dropdown box and submission button will be displayed if the user has 'post' permission as returned by modname_rating_permissions().

The text to the left of the dropdown box will be displayed if the user has 'view' permissions. The displayed text consists of the aggregate of the ratings, 4 out of 5 in the example. The number in brackets is the number of ratings that have been submitted. There have been two ratings submitted in the example.