Note:

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

Comment API: Difference between revisions

From MoodleDocs
mNo edit summary
mNo edit summary
Line 6: Line 6:
* Use a consistent approach for all comments throughout Moodle
* Use a consistent approach for all comments throughout Moodle
* Easily integrate comments 2.0 with existing modules
* Easily integrate comments 2.0 with existing modules
* Works no matter Javascript is enabled or not




Line 12: Line 13:
The comments 2.0 provides APIs to:
The comments 2.0 provides APIs to:
# Add comments
# Add comments
# Update comments
# Manage comments
# Delete comments


And provides an ajax interface to allow adding comments on a floating DIV.
And provides a fancy ajax interface to add/delete comments without loading a new page.


==Comments database table==
==Comments database table==
Line 66: Line 68:
|}
|}


==Comments API==
==Use Comments API==


lib/weblib.php
===Add an option to format_text function===
===Add an option to format_text function===


Line 76: Line 77:


<code php>
<code php>
$cmt = new stdclass;
$to = new stdclass;
$cmt->contextid = $modcontext->id;
$cmt->contextid = $modcontext->id;
$cmt->area      = 'format_post';
$cmt->area      = 'format_post';
Line 84: Line 85:
</code>
</code>


===Use comment class===
To use Comments API elsewhere, using following code:
<code php>
$options->area    = 'database_entry';
$options->context = $context;
$options->itemid  = $record->id;
$options->showcount = true;
$comment = new comment($options);
$comment->output(false);
</code>


lib/commentlib.php
==Comments API overview==


Generally speaking, only two functions you need to know to get comments 2.0 worked:
# Use comment::init to initialize comments 2.0
# Use $comment->output to display comments
The comment class has been implemented in comment/lib.php.
===class comment()===
===class comment()===
====__construct($contextid, $comment_area, $itemid))====
====__construct($contextid, $comment_area, $itemid))====
Initialize class members
Initialize class members


====js()====
====init()====
Print Javascript required by commenting API, this is a static function, which must be called before html head printed</head>
It is a static function used to initialize comments, setting up languages, which must be called before html head printed


====init($return = false)====
====output($return = false)====
Will print the html snippet for commenting UI, will be called inside format_text
Will print the html snippet for commenting interface, if set $return as true, it will return html string instead of printing out.


====print_comments($params = array())====
====print_comments($params = array())====
print comments
Used by non-javascript comment interface, will print a list of comments.


====add($content)====
====add($content)====
Add comment to database
Public instance funciton, add a comment to database, used in comment/comment_ajax.php
 
====count()====
Counting the number of comments


====delete($id)====
====delete($id)====
delete a comment
Delete a comment from database, used in comment/comment_ajax.php
 
====delete_comments====
Delete all comments in a specific contexts (like all comments belonging to a forum post)


==Javascript API==
==Javascript API==
Can be found in lib/comment.js
Comments 2.0 implemented a YUI3 module M.core_comment to deal with the communication between browsers and moodle.
 
It can be found in comment/comment.js
===post_comment(client_id)===
Submit a comment


===view_comments(client_id, contextid, comment_area, itemid, page)===
Call M.core_comment.init will create an instance of CommentHelper class. You don't need to make any calls to this instance, it simply works out of box.
Display comments, will call get_cmt to load comments.
 
===get_comments(client_id, ctx, area, itemid, page)===
Load comments by AJAX


== Moodle modules callback ==
== Moodle modules callback ==
Comments API allows modules/blocks/blog controlling comments
Comments API allows modules/blocks/blog to  how comments display.


===Permission control===
===Permission control===
Line 131: Line 147:


=== Check new added comment ===
=== Check new added comment ===
The callback function allows you to change the comment content before inserting into database or reject this comment.
Modules can implement a function named '''modname_comment_add'''.
Modules can implement a function named '''modname_comment_add'''.


Line 137: Line 155:
Blog need to implement '''blog_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 comments content, or reject this comment.
=== Filter/format comments ===
This callback allows modules check/format comments when user request to display comments.


=== Filter/format comments ===
Modules can implement a function named '''modname_comment_display'''.
Modules can implement a function named '''modname_comment_display'''.


Line 146: Line 164:
Blog need to implement '''blog_comment_display''' function.
Blog need to implement '''blog_comment_display''' function.


This callback allows modules check/format comments when user request to display comments.


=== Define a comment template ===
=== Define a comment template ===
Modules can implement a function named '''modname_comment_template''', which allow modules define a comment template.
Modules can implement a function named '''modname_comment_template''', which allow modules define a comment template.
The template must have 4 embedding variables, ___id___, ___content___, ___time___, ___name___, they will be replaced with html id, comments content, comment time and commenter name
The template must have 4 embedding variables, ___id___, ___content___, ___time___, ___name___, they will be replaced with html id, comments content, comment time and commenter name
=== get original url ===
==Interface==
=== Popup UI ===
[[Image:moodle_commenting_forum_module.png]]
=== Inline UI ===
[[Image:moodle_commenting_inline_UI.png]]


==See also==
==See also==
* MDL-19118 - Comments 2.0 issue
* MDL-19118 - Comments 2.0 issue

Revision as of 07:49, 15 March 2010

Objectives

The goals of comments 2.0:

  • Manage comments centrally
  • Use a consistent approach for all comments throughout Moodle
  • Easily integrate comments 2.0 with existing modules
  • Works no matter Javascript is enabled or not


Overview

The comments 2.0 provides APIs to:

  1. Add comments
  2. Manage comments
  3. Delete comments

And provides a fancy ajax interface to add/delete comments without loading a new page.

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

Use Comments 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 comment icon to every post:

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

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


";

Use comment class

To use Comments API elsewhere, using following code: $options->area = 'database_entry'; $options->context = $context; $options->itemid = $record->id; $options->showcount = true; $comment = new comment($options); $comment->output(false);

Comments API overview

Generally speaking, only two functions you need to know to get comments 2.0 worked:

  1. Use comment::init to initialize comments 2.0
  2. Use $comment->output to display comments

The comment class has been implemented in comment/lib.php.

class comment()

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

Initialize class members

init()

It is a static function used to initialize comments, setting up languages, which must be called before html head printed

output($return = false)

Will print the html snippet for commenting interface, if set $return as true, it will return html string instead of printing out.

print_comments($params = array())

Used by non-javascript comment interface, will print a list of comments.

add($content)

Public instance funciton, add a comment to database, used in comment/comment_ajax.php

count()

Counting the number of comments

delete($id)

Delete a comment from database, used in comment/comment_ajax.php

delete_comments

Delete all comments in a specific contexts (like all comments belonging to a forum post)

Javascript API

Comments 2.0 implemented a YUI3 module M.core_comment to deal with the communication between browsers and moodle. It can be found in comment/comment.js

Call M.core_comment.init will create an instance of CommentHelper class. You don't need to make any calls to this instance, it simply works out of box.

Moodle modules callback

Comments API allows modules/blocks/blog to how comments display.

Permission control

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

Blocks need to overwrite comment_permissions function of block_base.

Blog need to implement blog_comment_permissions function.

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

Check new added comment

The callback function allows you to change the comment content before inserting into database or reject this comment.

Modules can implement a function named modname_comment_add.

Blocks need to overwrite comment_add function.

Blog need to implement blog_comment_add function.

Filter/format comments

This callback allows modules check/format comments when user request to display comments.

Modules can implement a function named modname_comment_display.

Blocks need to overwrite comment_display function.

Blog need to implement blog_comment_display function.


Define a comment template

Modules can implement a function named modname_comment_template, which allow modules define a comment template. The template must have 4 embedding variables, ___id___, ___content___, ___time___, ___name___, they will be replaced with html id, comments content, comment time and commenter name

See also