|
| 1 | +--- |
| 2 | +title: Comment API |
| 3 | +tags: |
| 4 | + - comment |
| 5 | +--- |
| 6 | + |
| 7 | +The Comment API provides a standardized way to manage comments within Moodle. It allows developers to create, retrieve, update, and delete comments associated with various Moodle entities, such as courses, activities, and user submissions. |
| 8 | + |
| 9 | +## Usage |
| 10 | + |
| 11 | +The `\core_comment\manager` is responsible for handling all comment-related operations within Moodle. This includes rendering and managing comment data. |
| 12 | + |
| 13 | +The instantiation of the comment class is done by a single arguments object that defines the context and other relevant information for the comment being created or managed. |
| 14 | + |
| 15 | +The basic attributes for the argument object are: |
| 16 | + |
| 17 | +- `context`: the `core\context` object the comment is associated with. |
| 18 | +- `component`: the name of the component the comment is related to. |
| 19 | +- `itemid`: optional, the ID of the item the comment is associated with. |
| 20 | +- `area`: optional, the area the comment is associated with. |
| 21 | +- `cm`: optional, the `core_course\cm_info` object. |
| 22 | +- `course`: optional, course object (will be loaded from context if not provided). |
| 23 | + |
| 24 | +Optional attributes related to the comment area rendering: |
| 25 | + |
| 26 | +- `client_id`: a unique id to identify the comment area (it will be auto-generated if not provided). |
| 27 | +- `autostart`: a boolean indicating whether the comment area should be automatically expanded. |
| 28 | +- `showcount`: a boolean indicating whether to show the comment count. |
| 29 | +- `displaycancel`: a boolean indicating whether to display a cancel button. |
| 30 | +- `notoggle`: a boolean indicating whether to disable toggling of the comment area. |
| 31 | +- `linktext`: a string to indicate alternative text for the show/hide link. |
| 32 | + |
| 33 | +Once instantiated, the class provides simple methods to handle comment-related operations. |
| 34 | + |
| 35 | +## Rendering a comment area |
| 36 | + |
| 37 | +The manager class has an `output` method to get the comment area HTML. Here is a simple example of how to use it: |
| 38 | + |
| 39 | +```php |
| 40 | +$args = (object) [ |
| 41 | + 'context' => \core\context\module::instance($cm->id), |
| 42 | + 'component' => 'mod_data', |
| 43 | + 'area' => 'database_entry', |
| 44 | + 'itemid' => $entry->id, |
| 45 | + 'cm' => $cm, |
| 46 | +]; |
| 47 | +$comment = new \core_comment\manager($args); |
| 48 | +echo $comment->output(); |
| 49 | +``` |
| 50 | + |
| 51 | +Additionally, the manager class offers methods to customize the default options for the comment area: |
| 52 | + |
| 53 | +- `set_view_permission(bool $newvalue)`: Set the view permissions for the comment area. |
| 54 | +- `set_post_permission(bool $newvalue)`: Set the post permissions for the comment area. |
| 55 | +- `set_notoggle(bool $newvalue)`: Set the toggle option for the comment area. |
| 56 | +- `set_autostart(bool $newvalue)`: Set the autostart option for the comment area. |
| 57 | +- `set_displaycancel(bool $newvalue)`: Set the display cancel option for the comment area. |
| 58 | +- `set_displaytotalcount(bool $newvalue)`: Set the display total count option for the comment area. |
| 59 | + |
| 60 | +## Retrieving comments |
| 61 | + |
| 62 | +You can use the `get_comments()` method to fetch all comments associated with the defined context. The method has the following parameters: |
| 63 | + |
| 64 | +- `int $page`: The page number to retrieve. |
| 65 | +- `int $sortdirection` (default `DESC`): The sorting for the comments. |
| 66 | + |
| 67 | +The number of comments displayed per page is defined by the `$CFG->commentsperpage` configuration setting. The default value is 15. |
| 68 | + |
| 69 | +There is also a `count` method to get the total number of comments for the defined context. |
| 70 | + |
| 71 | +```php |
| 72 | +$comment = new \core_comment\manager($args); |
| 73 | + |
| 74 | +$count = $comment->count(); |
| 75 | +echo get_string('totalcomments', 'mod_myplugin', $count); |
| 76 | + |
| 77 | +$comments = $comment->get_comments(); |
| 78 | +foreach ($comments as $comment) { |
| 79 | + echo $comment->content; |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Add plugin control |
| 84 | + |
| 85 | +Similar to files, when a plugin wants to use comments it must provide a callback method to validate the comment area is correct. To do so, they must implement a callback in their `lib.php` file, specifically in the `PLUGINNAME_comment_display` function. |
| 86 | + |
| 87 | +This is an example of a possible callback for plugin `mod_myplugin`: |
| 88 | + |
| 89 | +```php |
| 90 | +/** |
| 91 | + * Validate comment data before displaying comments |
| 92 | + * |
| 93 | + * @param stdClass $comment |
| 94 | + * @param stdClass $args |
| 95 | + * @return boolean |
| 96 | + */ |
| 97 | +function mod_myplugin_comment_display(stdClass $comments, stdClass $args): stdClass { |
| 98 | + if ($args->commentarea != 'entry_comments') { |
| 99 | + throw new comment_exception('invalidcommentarea'); |
| 100 | + } |
| 101 | + if ($args->itemid != 0) { |
| 102 | + throw new comment_exception('invalidcommentitemid'); |
| 103 | + } |
| 104 | + return $comments; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Add and remove comments |
| 109 | + |
| 110 | +The manager class provides several methods to add and remove comments: |
| 111 | + |
| 112 | +- `add(string $content, int $format = FORMAT_MOODLE): stdClass`: Add a new comment with the given content and format. |
| 113 | +- `delete(int|stdClass $comment): bool`: Delete a specific comment. |
| 114 | +- `static delete_comments(stdClass $args): bool`: Delete all comments for the specified `context`, `itemid`, and `commentarea`. |
0 commit comments