title | tags | |
---|---|---|
Comment API |
|
import { Since } from '@site/src/components';
<Since versions={["2.0"]} />
The goals of Comment API:
- Manage comments centrally
- Use a consistent approach for all comments throughout Moodle
- Easily integrate Comment API with existing modules
- Works no matter JavaScript is enabled or not
Comment API provides following functionalities:
- Add comments
- Manage comments
- Delete comments
And provides a fancy ajax interface to add/delete comments without reloading page.
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 |
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:
$cmt = 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)."<hr />";
To use Comment API elsewhere, using following code:
$options->area = 'database_entry';
$options->context = $context;
$options->itemid = $record->id;
$options->component = 'mod_data';
$options->showcount = true;
$comment = new comment($options);
$comment->output(false);
Generally speaking, only two functions you need to know to get comment API worked:
- Use
comment::init
to initialize Comment API - Use
$comment->output
to display comments
The comment class has been implemented in comment/lib.php
.
Initialize class members.
It is a static function used to initialize comments, setting up languages, which must be called before html head printed.
Will print the html snippet for commenting interface, if set $return
as true, it will return html string instead of printing out.
Used by non-JavaScript comment interface, will print a list of comments.
Public instance funciton, add a comment to database, used in comment/comment_ajax.php
.
Counting the number of comments.
Delete a comment from database, used in comment/comment_ajax.php
Delete all comments in a specific contexts (like all comments belonging to a forum post).
Comment API 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.
Comment API allows modules/blocks/blog
to decide how comments display.
This callback method is required, it must be implemented. Otherwise, new comment will be rejected by default.
Plugins must implement pluginname_comment_validate
callback to validate comments parameters, it must return true to pass validation.
Modules must implement function pluginname_comment_permissions
to return post and view permission.
Blocks need to overwrite blockname_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)
.
The callback function allows you to change the comment content before inserting into database or reject this comment.
It takes two arguments, the comment object which contains comment details, and $params which contains context and course information.
Modules can implement a function named modname_comment_add
.
Blocks need to overwrite blockname_comment_add
function.
Blog need to implement blog_comment_add
function.
This function should return a boolean value.
This callback allows modules check/format comments when user request to display comments.
It takes the same arguments as pluginname_comment_add
.
Modules can implement a function named pluginname_comment_display
.
Blocks need to overwrite blockname_comment_display
function.
Blog need to implement blog_comment_display
function.
It will return the comment object.
Modules can implement a function named pluginname_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
Comments will automatically be included in the activity/course backups and restored provided the itemids can be updated during the restore process. To do this either:
- do not use item ids and rely just on the context
- make the commentarea the same as one of the mappings in the restore (set_mapping); or
- override the function
get_comment_mapping_itemname
in the restore activity class
If you do not do one of these the comments will be silently ignored on restore.