Note: You are currently viewing documentation for Moodle 3.0. Up-to-date documentation for the latest stable version of Moodle may be available here: Ratings 2.0.

Development:Ratings 2.0: Difference between revisions

From MoodleDocs
Line 78: Line 78:
==Ratings API==
==Ratings API==


lib/weblib.php
lib/ratinglib.php will contain...
===Add an option to format_text function===
 
Using this format_text function will add a rating 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:
 
<code php>
$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)."<hr />";
</code>
 
 
lib/ratinglib.php


===class rating()===
===class rating()===
====__construct($contextid, $comment_area, $itemid))====
====__construct($contextid, $itemid))====
Initialize class members
Initialize class instance storing the context and item id


====js()====
====add($userid, $rating)====
Print Javascript required by rating API, this is a static function, which must be called before html head printed</head>
Add rating to database


====init($return = false)====
====delete($userid)====
Will print the html snippet for rating UI, will be called inside format_text
delete the rating for the supplied user


====print_comments($params = array())====
====get($userid)====
print comments
get the rating for the supplied user


====add($content)====
====get()====
Add rating to database
get an array of ratings for the current context and item


====delete($id)====
====get_rating_html()====
delete a rating
return rating UI html snippet. Used to include ratings in pages.


==Javascript API==
==Javascript API==

Revision as of 03:43, 21 January 2010

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


Overview

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)
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)

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.

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

get_rating_html()

return 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 review_permissions function of block_base.

Blog need to implement review_rating_permissions function.

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

Check new added rating

Modules can implement a function named modname_review_add.

Blocks need to overwrite review_add function.

Blog need to implement blog_review_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.


get original url

Interface

The interface will not change as part of this development. In future the use of an ajax star rating style UI element could be introduced.

RatingUI.gif