Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/comment/ -> lib.php (source)

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

Functions and classes for commenting

Copyright: 2010 Dongsheng Cai {@link http://dongsheng.org}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1177 lines (43 kb)
Included or required:0 times
Referenced: 35 times
Includes or requires: 0 files

Defines 2 classes


Class: comment  - X-Ref

Comment is helper class to add/delete comments anywhere in moodle

__construct(stdClass $options)   X-Ref
Construct function of comment class, initialise
class members

param: stdClass $options {

init(moodle_page $page = null)   X-Ref
Receive nonjs comment parameters

param: moodle_page $page The page object to initialise comments within

set_component($component)   X-Ref
Sets the component.

This method shouldn't be public, changing the component once it has been set potentially
invalidates permission checks.
A coding_error is now thrown if code attempts to change the component.

param: string $component

set_view_permission($value)   X-Ref
Determines if the user can view the comment.

param: bool $value

set_post_permission($value)   X-Ref
Determines if the user can post a comment

param: bool $value

check_permissions()   X-Ref
check posting comments permission
It will check based on user roles and ask modules
If you need to check permission by modules, a
function named $pluginname_check_comment_post must be implemented


get_nojslink(moodle_page $page = null)   X-Ref
Gets a link for this page that will work with JS disabled.

param: moodle_page $page
return: moodle_url

set_notoggle($newvalue = true)   X-Ref
Sets the value of the notoggle option.

If set to true then the user will not be able to expand and collase
the comment section.

param: bool $newvalue

set_autostart($newvalue = true)   X-Ref
Sets the value of the autostart option.

If set to true then the comments will be loaded during page load.
Normally this happens only once the user expands the comment section.

param: bool $newvalue

set_displaycancel($newvalue = true)   X-Ref
Sets the displaycancel option

If set to true then a cancel button will be shown when using the form
to post comments.

param: bool $newvalue

set_displaytotalcount($newvalue = true)   X-Ref
Sets the displaytotalcount option

If set to true then the total number of comments will be displayed
when printing comments.

param: bool $newvalue

initialise_javascript(moodle_page $page)   X-Ref
Initialises the JavaScript that enchances the comment API.

param: moodle_page $page The moodle page object that the JavaScript should be

output($return = true)   X-Ref
Prepare comment code in html

param: boolean $return
return: string|void

get_comments($page = '', $sortdirection = 'DESC')   X-Ref
Return matched comments

param: int $page
param: str $sortdirection sort direction, ASC or DESC
return: array

get_component_select_sql($alias = '')   X-Ref
Returns an SQL fragment and param for selecting on component.

param: string $alias
return: array

count()   X-Ref
Returns the number of comments associated with the details of this object

return: int

get_pagination($page = 0)   X-Ref
Returns HTML to display a pagination bar

param: int $page
return: string

add($content, $format = FORMAT_MOODLE)   X-Ref
Add a new comment

param: string $content
param: int $format
return: stdClass

delete_comments($param)   X-Ref
delete by context, commentarea and itemid

param: stdClass|array $param {
return: boolean

reset_course_page_comments($context)   X-Ref
Delete page_comments in whole course, used by course reset

param: stdClass $context course context

delete($comment)   X-Ref
Delete a comment

param: int|stdClass $comment The id of a comment, or a comment record.
return: bool

print_comments($page = 0, $return = true, $nonjs = true)   X-Ref
Print comments

param: int $page
param: bool $return return comments list string or print it out
param: bool $nonjs print nonjs comments list or not?
return: string|void

print_comment($cmt, $nonjs = true)   X-Ref
Returns an array containing comments in HTML format.

param: stdClass $cmt {
param: bool $nonjs
return: array

validate($params=array()   X-Ref
Revoke validate callbacks

param: stdClass $params addtionall parameters need to add to callbacks

can_view()   X-Ref
Returns true if the user is able to view comments

return: bool

can_post()   X-Ref
Returns true if the user can add comments against this comment description

return: bool

can_delete($comment)   X-Ref
Returns true if the user can delete this comment.

The user can delete comments if it is one they posted and they can still make posts,
or they have the capability to delete comments.

A database call is avoided if a comment record is passed.

param: int|stdClass $comment The id of a comment, or a comment record.
return: bool

get_component()   X-Ref
Returns the component associated with the comment.

return: string

get_compontent()   X-Ref
Do not call! I am a deprecated method because of the typo in my name.

return: string

get_context()   X-Ref
Returns the context associated with the comment

return: stdClass

get_courseid()   X-Ref
Returns the course id associated with the comment

return: int

get_cm()   X-Ref
Returns the course module associated with the comment

return: stdClass

get_itemid()   X-Ref
Returns the item id associated with the comment

return: int

get_commentarea()   X-Ref
Returns the comment area associated with the commentarea

return: stdClass

set_fullwidth($fullwidth = true)   X-Ref
Make the comments textarea fullwidth.

param: bool $fullwidth

get_template()   X-Ref
Return the template.

return: string

get_cid()   X-Ref
Return the cid.

return: string

get_linktext()   X-Ref
Return the link text.

return: string

get_notoggle()   X-Ref
Return no toggle.

return: bool

get_displaytotalcount()   X-Ref
Return display total count.

return: bool

get_displaycancel()   X-Ref
Return display cancel.

return: bool

get_fullwidth()   X-Ref
Return fullwidth.

return: bool

get_autostart()   X-Ref
Return autostart.

return: bool

Class: comment_exception  - X-Ref

Comment exception class