2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Functions and classes for commenting
21 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') ||
die();
27 * Comment is helper class to add/delete comments anywhere in moodle
31 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 /** @var int there may be several comment box in one page so we need a client_id to recognize them */
37 /** @var string commentarea is used to specify different parts shared the same itemid */
39 /** @var int itemid is used to associate with commenting content */
41 /** @var string this html snippet will be used as a template to build comment content */
43 /** @var int The context id for comments */
45 /** @var stdClass The context itself */
47 /** @var int The course id for comments */
49 /** @var stdClass course module object, only be used to help find pluginname automatically */
51 /** @var string The component that this comment is for. It is STRONGLY recommended to set this. */
53 /** @var string This is calculated by normalising the component */
55 /** @var string This is calculated by normalising the component */
57 /** @var bool Whether the user has the required capabilities/permissions to view comments. */
58 private $viewcap = false;
59 /** @var bool Whether the user has the required capabilities/permissions to post comments. */
60 private $postcap = false;
61 /** @var string to customize link text */
63 /** @var bool If set to true then comment sections won't be able to be opened and closed instead they will always be visible. */
64 protected $notoggle = false;
65 /** @var bool If set to true comments are automatically loaded as soon as the page loads. */
66 protected $autostart = false;
67 /** @var bool If set to true the total count of comments is displayed when displaying comments. */
68 protected $displaytotalcount = false;
69 /** @var bool If set to true a cancel button will be shown on the form used to submit comments. */
70 protected $displaycancel = false;
71 /** @var int The number of comments associated with this comments params */
72 protected $totalcommentcount = null;
74 /** @var bool Use non-javascript UI */
75 private static $nonjs = false;
76 /** @var int comment itemid used in non-javascript UI */
77 private static $comment_itemid = null;
78 /** @var int comment context used in non-javascript UI */
79 private static $comment_context = null;
80 /** @var string comment area used in non-javascript UI */
81 private static $comment_area = null;
82 /** @var string comment page used in non-javascript UI */
83 private static $comment_page = null;
84 /** @var string comment itemid component in non-javascript UI */
85 private static $comment_component = null;
88 * Construct function of comment class, initialise
91 * @param stdClass $options {
92 * context => context context to use for the comment [required]
93 * component => string which plugin will comment being added to [required]
94 * itemid => int the id of the associated item (forum post, glossary item etc) [required]
95 * area => string comment area
96 * cm => stdClass course module
97 * course => course course object
98 * client_id => string an unique id to identify comment area
99 * autostart => boolean automatically expend comments
100 * showcount => boolean display the number of comments
101 * displaycancel => boolean display cancel button
102 * notoggle => boolean don't show/hide button
103 * linktext => string title of show/hide button
106 public function __construct(stdClass
$options) {
107 $this->viewcap
= false;
108 $this->postcap
= false;
111 if (!empty($options->client_id
)) {
112 $this->cid
= $options->client_id
;
114 $this->cid
= uniqid();
118 if (!empty($options->context
)) {
119 $this->context
= $options->context
;
120 $this->contextid
= $this->context
->id
;
121 } else if(!empty($options->contextid
)) {
122 $this->contextid
= $options->contextid
;
123 $this->context
= get_context_instance_by_id($this->contextid
);
125 print_error('invalidcontext');
128 if (!empty($options->component
)) {
129 // set and validate component
130 $this->set_component($options->component
);
132 // component cannot be empty
133 throw new comment_exception('invalidcomponent');
137 // course will be used to generate user profile link
138 if (!empty($options->course
)) {
139 $this->courseid
= $options->course
->id
;
140 } else if (!empty($options->courseid
)) {
141 $this->courseid
= $options->courseid
;
143 $this->courseid
= SITEID
;
146 // setup coursemodule
147 if (!empty($options->cm
)) {
148 $this->cm
= $options->cm
;
154 if (!empty($options->area
)) {
155 $this->commentarea
= $options->area
;
159 if (!empty($options->itemid
)) {
160 $this->itemid
= $options->itemid
;
165 // setup customized linktext
166 if (!empty($options->linktext
)) {
167 $this->linktext
= $options->linktext
;
169 $this->linktext
= get_string('comments');
172 // setup options for callback functions
173 $this->comment_param
= new stdClass();
174 $this->comment_param
->context
= $this->context
;
175 $this->comment_param
->courseid
= $this->courseid
;
176 $this->comment_param
->cm
= $this->cm
;
177 $this->comment_param
->commentarea
= $this->commentarea
;
178 $this->comment_param
->itemid
= $this->itemid
;
181 if (!empty($options->notoggle
)) {
182 $this->set_notoggle($options->notoggle
);
186 if (!empty($options->autostart
)) {
187 $this->set_autostart($options->autostart
);
190 // setup displaycancel
191 if (!empty($options->displaycancel
)) {
192 $this->set_displaycancel($options->displaycancel
);
195 // setup displaytotalcount
196 if (!empty($options->showcount
)) {
197 $this->set_displaytotalcount($options->showcount
);
200 // setting post and view permissions
201 $this->check_permissions();
204 $this->template
= html_writer
::tag('div', '___picture___', array('class' => 'comment-userpicture'));
205 $this->template
.= html_writer
::start_tag('div', array('class' => 'comment-content'));
206 $this->template
.= '___name___ - ';
207 $this->template
.= html_writer
::tag('span', '___time___');
208 $this->template
.= html_writer
::tag('div', '___content___');
209 $this->template
.= html_writer
::end_tag('div'); // .comment-content
210 if (!empty($this->plugintype
)) {
211 $this->template
= plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'template', array($this->comment_param
), $this->template
);
218 * Receive nonjs comment parameters
220 * @param moodle_page $page The page object to initialise comments within
221 * If not provided the global $PAGE is used
223 public static function init(moodle_page
$page = null) {
229 // setup variables for non-js interface
230 self
::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHANUM
);
231 self
::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT
);
232 self
::$comment_context = optional_param('comment_context', '', PARAM_INT
);
233 self
::$comment_page = optional_param('comment_page', '', PARAM_INT
);
234 self
::$comment_area = optional_param('comment_area', '', PARAM_AREA
);
236 $page->requires
->string_for_js('addcomment', 'moodle');
237 $page->requires
->string_for_js('deletecomment', 'moodle');
238 $page->requires
->string_for_js('comments', 'moodle');
239 $page->requires
->string_for_js('commentsrequirelogin', 'moodle');
243 * Sets the component.
245 * This method shouldn't be public, changing the component once it has been set potentially
246 * invalidates permission checks.
247 * A coding_error is now thrown if code attempts to change the component.
249 * @param string $component
251 public function set_component($component) {
252 if (!empty($this->component
) && $this->component
!== $component) {
253 throw new coding_exception('You cannot change the component of a comment once it has been set');
255 $this->component
= $component;
256 list($this->plugintype
, $this->pluginname
) = normalize_component($component);
260 * Determines if the user can view the comment.
264 public function set_view_permission($value) {
265 $this->viewcap
= (bool)$value;
269 * Determines if the user can post a comment
273 public function set_post_permission($value) {
274 $this->postcap
= (bool)$value;
278 * check posting comments permission
279 * It will check based on user roles and ask modules
280 * If you need to check permission by modules, a
281 * function named $pluginname_check_comment_post must be implemented
283 private function check_permissions() {
284 $this->postcap
= has_capability('moodle/comment:post', $this->context
);
285 $this->viewcap
= has_capability('moodle/comment:view', $this->context
);
286 if (!empty($this->plugintype
)) {
287 $permissions = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'permissions', array($this->comment_param
), array('post'=>false, 'view'=>false));
288 $this->postcap
= $this->postcap
&& $permissions['post'];
289 $this->viewcap
= $this->viewcap
&& $permissions['view'];
294 * Gets a link for this page that will work with JS disabled.
296 * @global moodle_page $PAGE
297 * @param moodle_page $page
300 public function get_nojslink(moodle_page
$page = null) {
301 if ($page === null) {
306 $link = new moodle_url($page->url
, array(
307 'nonjscomment' => true,
308 'comment_itemid' => $this->itemid
,
309 'comment_context' => $this->context
->id
,
310 'comment_area' => $this->commentarea
,
312 $link->remove_params(array('comment_page'));
317 * Sets the value of the notoggle option.
319 * If set to true then the user will not be able to expand and collase
320 * the comment section.
322 * @param bool $newvalue
324 public function set_notoggle($newvalue = true) {
325 $this->notoggle
= (bool)$newvalue;
329 * Sets the value of the autostart option.
331 * If set to true then the comments will be loaded during page load.
332 * Normally this happens only once the user expands the comment section.
334 * @param bool $newvalue
336 public function set_autostart($newvalue = true) {
337 $this->autostart
= (bool)$newvalue;
341 * Sets the displaycancel option
343 * If set to true then a cancel button will be shown when using the form
346 * @param bool $newvalue
348 public function set_displaycancel($newvalue = true) {
349 $this->displaycancel
= (bool)$newvalue;
353 * Sets the displaytotalcount option
355 * If set to true then the total number of comments will be displayed
356 * when printing comments.
358 * @param bool $newvalue
360 public function set_displaytotalcount($newvalue = true) {
361 $this->displaytotalcount
= (bool)$newvalue;
365 * Initialises the JavaScript that enchances the comment API.
367 * @param moodle_page $page The moodle page object that the JavaScript should be
370 public function initialise_javascript(moodle_page
$page) {
372 $options = new stdClass
;
373 $options->client_id
= $this->cid
;
374 $options->commentarea
= $this->commentarea
;
375 $options->itemid
= $this->itemid
;
377 $options->courseid
= $this->courseid
;
378 $options->contextid
= $this->contextid
;
379 $options->component
= $this->component
;
380 $options->notoggle
= $this->notoggle
;
381 $options->autostart
= $this->autostart
;
383 $page->requires
->js_init_call('M.core_comment.init', array($options), true);
389 * Prepare comment code in html
390 * @param boolean $return
391 * @return string|void
393 public function output($return = true) {
394 global $PAGE, $OUTPUT;
395 static $template_printed;
397 $this->initialise_javascript($PAGE);
399 if (!empty(self
::$nonjs)) {
400 // return non js comments interface
401 return $this->print_comments(self
::$comment_page, $return, true);
406 // print html template
407 // Javascript will use the template to render new comments
408 if (empty($template_printed) && $this->can_view()) {
409 $html .= html_writer
::tag('div', $this->template
, array('style' => 'display:none', 'id' => 'cmt-tmpl'));
410 $template_printed = true;
413 if ($this->can_view()) {
414 // print commenting icon and tooltip
415 $html .= html_writer
::start_tag('div', array('class' => 'mdl-left'));
416 $html .= html_writer
::link($this->get_nojslink($PAGE), get_string('showcommentsnonjs'), array('class' => 'showcommentsnonjs'));
418 if (!$this->notoggle
) {
419 // If toggling is enabled (notoggle=false) then print the controls to toggle
420 // comments open and closed
422 if ($this->displaytotalcount
) {
423 $countstring = '('.$this->count().')';
425 $collapsedimage= 't/collapsed';
426 if (right_to_left()) {
427 $collapsedimage= 't/collapsed_rtl';
429 $collapsedimage= 't/collapsed';
431 $html .= html_writer
::start_tag('a', array('class' => 'comment-link', 'id' => 'comment-link-'.$this->cid
, 'href' => '#'));
432 $html .= html_writer
::empty_tag('img', array('id' => 'comment-img-'.$this->cid
, 'src' => $OUTPUT->pix_url($collapsedimage), 'alt' => $this->linktext
, 'title' => $this->linktext
));
433 $html .= html_writer
::tag('span', $this->linktext
.' '.$countstring, array('id' => 'comment-link-text-'.$this->cid
));
434 $html .= html_writer
::end_tag('a');
437 $html .= html_writer
::start_tag('div', array('id' => 'comment-ctrl-'.$this->cid
, 'class' => 'comment-ctrl'));
439 if ($this->autostart
) {
440 // If autostart has been enabled print the comments list immediatly
441 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list comments-loaded'));
442 $html .= html_writer
::tag('li', '', array('class' => 'first'));
443 $html .= $this->print_comments(0, true, false);
444 $html .= html_writer
::end_tag('ul'); // .comment-list
445 $html .= $this->get_pagination(0);
447 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list'));
448 $html .= html_writer
::tag('li', '', array('class' => 'first'));
449 $html .= html_writer
::end_tag('ul'); // .comment-list
450 $html .= html_writer
::tag('div', '', array('id' => 'comment-pagination-'.$this->cid
, 'class' => 'comment-pagination'));
453 if ($this->can_post()) {
454 // print posting textarea
455 $html .= html_writer
::start_tag('div', array('class' => 'comment-area'));
456 $html .= html_writer
::start_tag('div', array('class' => 'db'));
457 $html .= html_writer
::tag('textarea', '', array('name' => 'content', 'rows' => 2, 'cols' => 20, 'id' => 'dlg-content-'.$this->cid
));
458 $html .= html_writer
::end_tag('div'); // .db
460 $html .= html_writer
::start_tag('div', array('class' => 'fd', 'id' => 'comment-action-'.$this->cid
));
461 $html .= html_writer
::link('#', get_string('savecomment'), array('id' => 'comment-action-post-'.$this->cid
));
463 if ($this->displaycancel
) {
464 $html .= html_writer
::tag('span', ' | ');
465 $html .= html_writer
::link('#', get_string('cancel'), array('id' => 'comment-action-cancel-'.$this->cid
));
468 $html .= html_writer
::end_tag('div'); // .fd
469 $html .= html_writer
::end_tag('div'); // .comment-area
470 $html .= html_writer
::tag('div', '', array('class' => 'clearer'));
473 $html .= html_writer
::end_tag('div'); // .comment-ctrl
474 $html .= html_writer
::end_tag('div'); // .mdl-left
487 * Return matched comments
492 public function get_comments($page = '') {
493 global $DB, $CFG, $USER, $OUTPUT;
494 if (!$this->can_view()) {
497 if (!is_numeric($page)) {
501 $perpage = (!empty($CFG->commentsperpage
))?
$CFG->commentsperpage
:15;
502 $start = $page * $perpage;
503 $ufields = user_picture
::fields('u');
504 $sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
506 JOIN {user} u ON u.id = c.userid
507 WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid
508 ORDER BY c.timecreated DESC";
509 $params['contextid'] = $this->contextid
;
510 $params['commentarea'] = $this->commentarea
;
511 $params['itemid'] = $this->itemid
;
514 $formatoptions = array('overflowdiv' => true);
515 $rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
516 foreach ($rs as $u) {
519 $c->content
= $u->ccontent
;
520 $c->format
= $u->cformat
;
521 $c->timecreated
= $u->ctimecreated
;
522 $url = new moodle_url('/user/view.php', array('id'=>$u->id
, 'course'=>$this->courseid
));
523 $c->profileurl
= $url->out(true);
524 $c->fullname
= fullname($u);
525 $c->time
= userdate($c->timecreated
, get_string('strftimerecent', 'langconfig'));
526 $c->content
= format_text($c->content
, $c->format
, $formatoptions);
527 $c->avatar
= $OUTPUT->user_picture($u, array('size'=>18));
529 $candelete = $this->can_delete($c->id
);
530 if (($USER->id
== $u->id
) ||
!empty($candelete)) {
537 if (!empty($this->plugintype
)) {
538 // moodle module will filter comments
539 $comments = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'display', array($comments, $this->comment_param
), $comments);
546 * Returns the number of comments associated with the details of this object
548 * @global moodle_database $DB
551 public function count() {
553 if ($this->totalcommentcount
=== null) {
554 $this->totalcommentcount
= $DB->count_records('comments', array('itemid' => $this->itemid
, 'commentarea' => $this->commentarea
, 'contextid' => $this->context
->id
));
556 return $this->totalcommentcount
;
560 * Returns HTML to display a pagination bar
562 * @global stdClass $CFG
563 * @global core_renderer $OUTPUT
567 public function get_pagination($page = 0) {
568 global $CFG, $OUTPUT;
569 $count = $this->count();
570 $perpage = (!empty($CFG->commentsperpage
))?
$CFG->commentsperpage
:15;
571 $pages = (int)ceil($count/$perpage);
572 if ($pages == 1 ||
$pages == 0) {
573 return html_writer
::tag('div', '', array('id' => 'comment-pagination-'.$this->cid
, 'class' => 'comment-pagination'));
575 if (!empty(self
::$nonjs)) {
576 // used in non-js interface
577 return $OUTPUT->paging_bar($count, $page, $perpage, $this->get_nojslink(), 'comment_page');
579 // return ajax paging bar
581 $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid
.'">';
582 for ($p=0; $p<$pages; $p++
) {
588 $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid
.'-'.$p.'">'.($p+
1).'</a> ';
598 * @global moodle_database $DB
599 * @param string $content
603 public function add($content, $format = FORMAT_MOODLE
) {
604 global $CFG, $DB, $USER, $OUTPUT;
605 if (!$this->can_post()) {
606 throw new comment_exception('nopermissiontocomment');
609 $newcmt = new stdClass
;
610 $newcmt->contextid
= $this->contextid
;
611 $newcmt->commentarea
= $this->commentarea
;
612 $newcmt->itemid
= $this->itemid
;
613 $newcmt->content
= $content;
614 $newcmt->format
= $format;
615 $newcmt->userid
= $USER->id
;
616 $newcmt->timecreated
= $now;
618 // This callback allow module to modify the content of comment, such as filter or replacement
619 plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'add', array(&$newcmt, $this->comment_param
));
621 $cmt_id = $DB->insert_record('comments', $newcmt);
622 if (!empty($cmt_id)) {
623 $newcmt->id
= $cmt_id;
624 $newcmt->time
= userdate($now, get_string('strftimerecent', 'langconfig'));
625 $newcmt->fullname
= fullname($USER);
626 $url = new moodle_url('/user/view.php', array('id' => $USER->id
, 'course' => $this->courseid
));
627 $newcmt->profileurl
= $url->out();
628 $newcmt->content
= format_text($newcmt->content
, $format, array('overflowdiv'=>true));
629 $newcmt->avatar
= $OUTPUT->user_picture($USER, array('size'=>16));
632 throw new comment_exception('dbupdatefailed');
637 * delete by context, commentarea and itemid
638 * @param stdClass|array $param {
639 * contextid => int the context in which the comments exist [required]
640 * commentarea => string the comment area [optional]
641 * itemid => int comment itemid [optional]
645 public static function delete_comments($param) {
647 $param = (array)$param;
648 if (empty($param['contextid'])) {
651 $DB->delete_records('comments', $param);
656 * Delete page_comments in whole course, used by course reset
658 * @param stdClass $context course context
660 public static function reset_course_page_comments($context) {
663 $contexts[] = $context->id
;
664 $children = get_child_contexts($context);
665 foreach ($children as $c) {
666 $contexts[] = $c->id
;
668 list($ids, $params) = $DB->get_in_or_equal($contexts);
669 $DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params);
675 * @param int $commentid
678 public function delete($commentid) {
680 $candelete = has_capability('moodle/comment:delete', $this->context
);
681 if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) {
682 throw new comment_exception('dbupdatefailed');
684 if (!($USER->id
== $comment->userid ||
!empty($candelete))) {
685 throw new comment_exception('nopermissiontocomment');
687 $DB->delete_records('comments', array('id'=>$commentid));
695 * @param bool $return return comments list string or print it out
696 * @param bool $nonjs print nonjs comments list or not?
697 * @return string|void
699 public function print_comments($page = 0, $return = true, $nonjs = true) {
700 global $DB, $CFG, $PAGE;
702 if (!$this->can_view()) {
707 if (!(self
::$comment_itemid == $this->itemid
&&
708 self
::$comment_context == $this->context
->id
&&
709 self
::$comment_area == $this->commentarea
)) {
712 $comments = $this->get_comments($page);
716 $html .= html_writer
::tag('h3', get_string('comments'));
717 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list'));
719 // Reverse the comments array to display them in the correct direction
720 foreach (array_reverse($comments) as $cmt) {
721 $html .= html_writer
::tag('li', $this->print_comment($cmt, $nonjs), array('id' => 'comment-'.$cmt->id
.'-'.$this->cid
));
724 $html .= html_writer
::end_tag('ul');
725 $html .= $this->get_pagination($page);
727 if ($nonjs && $this->can_post()) {
728 // Form to add comments
729 $html .= html_writer
::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/comment/comment_post.php')));
730 // Comment parameters
731 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'contextid', 'value' => $this->contextid
));
732 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'add'));
733 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'area', 'value' => $this->commentarea
));
734 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'component', 'value' => $this->component
));
735 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'itemid', 'value' => $this->itemid
));
736 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'courseid', 'value' => $this->courseid
));
737 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
738 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'returnurl', 'value' => $PAGE->url
));
739 // Textarea for the actual comment
740 $html .= html_writer
::tag('textarea', '', array('name' => 'content', 'rows' => 2));
741 // Submit button to add the comment
742 $html .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('submit')));
743 $html .= html_writer
::end_tag('form');
753 * Returns an array containing comments in HTML format.
755 * @global core_renderer $OUTPUT
756 * @param stdClass $cmt {
757 * id => int comment id
758 * content => string comment content
759 * format => int comment text format
760 * timecreated => int comment's timecreated
761 * profileurl => string link to user profile
762 * fullname => comment author's full name
763 * avatar => string user's avatar
764 * delete => boolean does user have permission to delete comment?
769 public function print_comment($cmt, $nonjs = true) {
772 $replacements = array();
774 if (!empty($cmt->delete
) && empty($nonjs)) {
775 $deletelink = html_writer
::start_tag('div', array('class'=>'comment-delete'));
776 $deletelink .= html_writer
::start_tag('a', array('href' => '#', 'id' => 'comment-delete-'.$this->cid
.'-'.$cmt->id
));
777 $deletelink .= $OUTPUT->pix_icon('t/delete', get_string('delete'));
778 $deletelink .= html_writer
::end_tag('a');
779 $deletelink .= html_writer
::end_tag('div');
780 $cmt->content
= $deletelink . $cmt->content
;
782 $patterns[] = '___picture___';
783 $patterns[] = '___name___';
784 $patterns[] = '___content___';
785 $patterns[] = '___time___';
786 $replacements[] = $cmt->avatar
;
787 $replacements[] = html_writer
::link($cmt->profileurl
, $cmt->fullname
);
788 $replacements[] = $cmt->content
;
789 $replacements[] = userdate($cmt->timecreated
, get_string('strftimerecent', 'langconfig'));
791 // use html template to format a single comment.
792 return str_replace($patterns, $replacements, $this->template
);
796 * Revoke validate callbacks
798 * @param stdClass $params addtionall parameters need to add to callbacks
800 protected function validate($params=array()) {
801 foreach ($params as $key=>$value) {
802 $this->comment_param
->$key = $value;
804 $validation = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'validate', array($this->comment_param
), false);
806 throw new comment_exception('invalidcommentparam');
811 * Returns true if the user is able to view comments
814 public function can_view() {
816 return !empty($this->viewcap
);
820 * Returns true if the user can add comments against this comment description
823 public function can_post() {
825 return isloggedin() && !empty($this->postcap
);
829 * Returns true if the user can delete this comment
830 * @param int $commentid
833 public function can_delete($commentid) {
834 $this->validate(array('commentid'=>$commentid));
835 return has_capability('moodle/comment:delete', $this->context
);
839 * Returns the component associated with the comment
842 public function get_compontent() {
843 return $this->component
;
847 * Returns the context associated with the comment
850 public function get_context() {
851 return $this->context
;
855 * Returns the course id associated with the comment
858 public function get_courseid() {
859 return $this->courseid
;
863 * Returns the course module associated with the comment
867 public function get_cm() {
872 * Returns the item id associated with the comment
876 public function get_itemid() {
877 return $this->itemid
;
881 * Returns the comment area associated with the commentarea
885 public function get_commentarea() {
886 return $this->commentarea
;
891 * Comment exception class
894 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
895 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
897 class comment_exception
extends moodle_exception
{