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 */
52 * The component that this comment is for.
54 * It is STRONGLY recommended to set this.
55 * Added as a database field in 2.9, old comments will have a null component.
60 /** @var string This is calculated by normalising the component */
62 /** @var string This is calculated by normalising the component */
64 /** @var bool Whether the user has the required capabilities/permissions to view comments. */
65 private $viewcap = false;
66 /** @var bool Whether the user has the required capabilities/permissions to post comments. */
67 private $postcap = false;
68 /** @var string to customize link text */
70 /** @var bool If set to true then comment sections won't be able to be opened and closed instead they will always be visible. */
71 protected $notoggle = false;
72 /** @var bool If set to true comments are automatically loaded as soon as the page loads. */
73 protected $autostart = false;
74 /** @var bool If set to true the total count of comments is displayed when displaying comments. */
75 protected $displaytotalcount = false;
76 /** @var bool If set to true a cancel button will be shown on the form used to submit comments. */
77 protected $displaycancel = false;
78 /** @var int The number of comments associated with this comments params */
79 protected $totalcommentcount = null;
82 * Set to true to remove the col attribute from the textarea making it full width.
85 protected $fullwidth = false;
87 /** @var bool Use non-javascript UI */
88 private static $nonjs = false;
89 /** @var int comment itemid used in non-javascript UI */
90 private static $comment_itemid = null;
91 /** @var int comment context used in non-javascript UI */
92 private static $comment_context = null;
93 /** @var string comment area used in non-javascript UI */
94 private static $comment_area = null;
95 /** @var string comment page used in non-javascript UI */
96 private static $comment_page = null;
97 /** @var string comment itemid component in non-javascript UI */
98 private static $comment_component = null;
101 * Construct function of comment class, initialise
104 * @param stdClass $options {
105 * context => context context to use for the comment [required]
106 * component => string which plugin will comment being added to [required]
107 * itemid => int the id of the associated item (forum post, glossary item etc) [required]
108 * area => string comment area
109 * cm => stdClass course module
110 * course => course course object
111 * client_id => string an unique id to identify comment area
112 * autostart => boolean automatically expend comments
113 * showcount => boolean display the number of comments
114 * displaycancel => boolean display cancel button
115 * notoggle => boolean don't show/hide button
116 * linktext => string title of show/hide button
119 public function __construct(stdClass
$options) {
120 $this->viewcap
= false;
121 $this->postcap
= false;
124 if (!empty($options->client_id
)) {
125 $this->cid
= $options->client_id
;
127 $this->cid
= uniqid();
131 if (!empty($options->context
)) {
132 $this->context
= $options->context
;
133 $this->contextid
= $this->context
->id
;
134 } else if(!empty($options->contextid
)) {
135 $this->contextid
= $options->contextid
;
136 $this->context
= context
::instance_by_id($this->contextid
);
138 print_error('invalidcontext');
141 if (!empty($options->component
)) {
142 // set and validate component
143 $this->set_component($options->component
);
145 // component cannot be empty
146 throw new comment_exception('invalidcomponent');
150 // course will be used to generate user profile link
151 if (!empty($options->course
)) {
152 $this->courseid
= $options->course
->id
;
153 } else if (!empty($options->courseid
)) {
154 $this->courseid
= $options->courseid
;
156 if ($coursecontext = $this->context
->get_course_context(false)) {
157 $this->courseid
= $coursecontext->instanceid
;
159 $this->courseid
= SITEID
;
163 // setup coursemodule
164 if (!empty($options->cm
)) {
165 $this->cm
= $options->cm
;
171 if (!empty($options->area
)) {
172 $this->commentarea
= $options->area
;
176 if (!empty($options->itemid
)) {
177 $this->itemid
= $options->itemid
;
182 // setup customized linktext
183 if (!empty($options->linktext
)) {
184 $this->linktext
= $options->linktext
;
186 $this->linktext
= get_string('comments');
189 // setup options for callback functions
190 $this->comment_param
= new stdClass();
191 $this->comment_param
->context
= $this->context
;
192 $this->comment_param
->courseid
= $this->courseid
;
193 $this->comment_param
->cm
= $this->cm
;
194 $this->comment_param
->commentarea
= $this->commentarea
;
195 $this->comment_param
->itemid
= $this->itemid
;
198 if (!empty($options->notoggle
)) {
199 $this->set_notoggle($options->notoggle
);
203 if (!empty($options->autostart
)) {
204 $this->set_autostart($options->autostart
);
207 // setup displaycancel
208 if (!empty($options->displaycancel
)) {
209 $this->set_displaycancel($options->displaycancel
);
212 // setup displaytotalcount
213 if (!empty($options->showcount
)) {
214 $this->set_displaytotalcount($options->showcount
);
217 // setting post and view permissions
218 $this->check_permissions();
221 $this->template
= html_writer
::start_tag('div', array('class' => 'comment-message'));
223 $this->template
.= html_writer
::start_tag('div', array('class' => 'comment-message-meta m-r-3'));
225 $this->template
.= html_writer
::tag('span', '___picture___', array('class' => 'picture'));
226 $this->template
.= html_writer
::tag('span', '___name___', array('class' => 'user')) . ' - ';
227 $this->template
.= html_writer
::tag('span', '___time___', array('class' => 'time'));
229 $this->template
.= html_writer
::end_tag('div'); // .comment-message-meta
230 $this->template
.= html_writer
::tag('div', '___content___', array('class' => 'text'));
232 $this->template
.= html_writer
::end_tag('div'); // .comment-message
234 if (!empty($this->plugintype
)) {
235 $this->template
= plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'template', array($this->comment_param
), $this->template
);
242 * Receive nonjs comment parameters
244 * @param moodle_page $page The page object to initialise comments within
245 * If not provided the global $PAGE is used
247 public static function init(moodle_page
$page = null) {
253 // setup variables for non-js interface
254 self
::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHANUM
);
255 self
::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT
);
256 self
::$comment_component = optional_param('comment_component', '', PARAM_COMPONENT
);
257 self
::$comment_context = optional_param('comment_context', '', PARAM_INT
);
258 self
::$comment_page = optional_param('comment_page', '', PARAM_INT
);
259 self
::$comment_area = optional_param('comment_area', '', PARAM_AREA
);
261 $page->requires
->strings_for_js(array(
265 'commentsrequirelogin',
273 * Sets the component.
275 * This method shouldn't be public, changing the component once it has been set potentially
276 * invalidates permission checks.
277 * A coding_error is now thrown if code attempts to change the component.
279 * @throws coding_exception if you try to change the component after it has been set.
280 * @param string $component
282 public function set_component($component) {
283 if (!empty($this->component
) && $this->component
!== $component) {
284 throw new coding_exception('You cannot change the component of a comment once it has been set');
286 $this->component
= $component;
287 list($this->plugintype
, $this->pluginname
) = core_component
::normalize_component($component);
291 * Determines if the user can view the comment.
295 public function set_view_permission($value) {
296 $this->viewcap
= (bool)$value;
300 * Determines if the user can post a comment
304 public function set_post_permission($value) {
305 $this->postcap
= (bool)$value;
309 * check posting comments permission
310 * It will check based on user roles and ask modules
311 * If you need to check permission by modules, a
312 * function named $pluginname_check_comment_post must be implemented
314 private function check_permissions() {
315 $this->postcap
= has_capability('moodle/comment:post', $this->context
);
316 $this->viewcap
= has_capability('moodle/comment:view', $this->context
);
317 if (!empty($this->plugintype
)) {
318 $permissions = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'permissions', array($this->comment_param
), array('post'=>false, 'view'=>false));
319 $this->postcap
= $this->postcap
&& $permissions['post'];
320 $this->viewcap
= $this->viewcap
&& $permissions['view'];
325 * Gets a link for this page that will work with JS disabled.
327 * @global moodle_page $PAGE
328 * @param moodle_page $page
331 public function get_nojslink(moodle_page
$page = null) {
332 if ($page === null) {
337 $link = new moodle_url($page->url
, array(
338 'nonjscomment' => true,
339 'comment_itemid' => $this->itemid
,
340 'comment_context' => $this->context
->id
,
341 'comment_component' => $this->get_component(),
342 'comment_area' => $this->commentarea
,
344 $link->remove_params(array('comment_page'));
349 * Sets the value of the notoggle option.
351 * If set to true then the user will not be able to expand and collase
352 * the comment section.
354 * @param bool $newvalue
356 public function set_notoggle($newvalue = true) {
357 $this->notoggle
= (bool)$newvalue;
361 * Sets the value of the autostart option.
363 * If set to true then the comments will be loaded during page load.
364 * Normally this happens only once the user expands the comment section.
366 * @param bool $newvalue
368 public function set_autostart($newvalue = true) {
369 $this->autostart
= (bool)$newvalue;
373 * Sets the displaycancel option
375 * If set to true then a cancel button will be shown when using the form
378 * @param bool $newvalue
380 public function set_displaycancel($newvalue = true) {
381 $this->displaycancel
= (bool)$newvalue;
385 * Sets the displaytotalcount option
387 * If set to true then the total number of comments will be displayed
388 * when printing comments.
390 * @param bool $newvalue
392 public function set_displaytotalcount($newvalue = true) {
393 $this->displaytotalcount
= (bool)$newvalue;
397 * Initialises the JavaScript that enchances the comment API.
399 * @param moodle_page $page The moodle page object that the JavaScript should be
402 public function initialise_javascript(moodle_page
$page) {
404 $options = new stdClass
;
405 $options->client_id
= $this->cid
;
406 $options->commentarea
= $this->commentarea
;
407 $options->itemid
= $this->itemid
;
409 $options->courseid
= $this->courseid
;
410 $options->contextid
= $this->contextid
;
411 $options->component
= $this->component
;
412 $options->notoggle
= $this->notoggle
;
413 $options->autostart
= $this->autostart
;
415 $page->requires
->js_init_call('M.core_comment.init', array($options), true);
421 * Prepare comment code in html
422 * @param boolean $return
423 * @return string|void
425 public function output($return = true) {
426 global $PAGE, $OUTPUT;
427 static $template_printed;
429 $this->initialise_javascript($PAGE);
431 if (!empty(self
::$nonjs)) {
432 // return non js comments interface
433 return $this->print_comments(self
::$comment_page, $return, true);
438 // print html template
439 // Javascript will use the template to render new comments
440 if (empty($template_printed) && $this->can_view()) {
441 $html .= html_writer
::tag('div', $this->template
, array('style' => 'display:none', 'id' => 'cmt-tmpl'));
442 $template_printed = true;
445 if ($this->can_view()) {
446 // print commenting icon and tooltip
447 $html .= html_writer
::start_tag('div', array('class' => 'mdl-left'));
448 $html .= html_writer
::link($this->get_nojslink($PAGE), get_string('showcommentsnonjs'), array('class' => 'showcommentsnonjs'));
450 if (!$this->notoggle
) {
451 // If toggling is enabled (notoggle=false) then print the controls to toggle
452 // comments open and closed
454 if ($this->displaytotalcount
) {
455 $countstring = '('.$this->count().')';
457 $collapsedimage= 't/collapsed';
458 if (right_to_left()) {
459 $collapsedimage= 't/collapsed_rtl';
461 $collapsedimage= 't/collapsed';
463 $html .= html_writer
::start_tag('a', array(
464 'class' => 'comment-link',
465 'id' => 'comment-link-'.$this->cid
,
468 'aria-expanded' => 'false')
470 $html .= $OUTPUT->pix_icon($collapsedimage, $this->linktext
);
471 $html .= html_writer
::tag('span', $this->linktext
.' '.$countstring, array('id' => 'comment-link-text-'.$this->cid
));
472 $html .= html_writer
::end_tag('a');
475 $html .= html_writer
::start_tag('div', array('id' => 'comment-ctrl-'.$this->cid
, 'class' => 'comment-ctrl'));
477 if ($this->autostart
) {
478 // If autostart has been enabled print the comments list immediatly
479 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list comments-loaded'));
480 $html .= html_writer
::tag('li', '', array('class' => 'first'));
481 $html .= $this->print_comments(0, true, false);
482 $html .= html_writer
::end_tag('ul'); // .comment-list
483 $html .= $this->get_pagination(0);
485 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list'));
486 $html .= html_writer
::tag('li', '', array('class' => 'first'));
487 $html .= html_writer
::end_tag('ul'); // .comment-list
488 $html .= html_writer
::tag('div', '', array('id' => 'comment-pagination-'.$this->cid
, 'class' => 'comment-pagination'));
491 if ($this->can_post()) {
492 // print posting textarea
493 $textareaattrs = array(
496 'id' => 'dlg-content-'.$this->cid
498 if (!$this->fullwidth
) {
499 $textareaattrs['cols'] = '20';
501 $textareaattrs['class'] = 'fullwidth';
504 $html .= html_writer
::start_tag('div', array('class' => 'comment-area'));
505 $html .= html_writer
::start_tag('div', array('class' => 'db'));
506 $html .= html_writer
::tag('textarea', '', $textareaattrs);
507 $html .= html_writer
::end_tag('div'); // .db
509 $html .= html_writer
::start_tag('div', array('class' => 'fd', 'id' => 'comment-action-'.$this->cid
));
510 $html .= html_writer
::link('#', get_string('savecomment'), array('id' => 'comment-action-post-'.$this->cid
));
512 if ($this->displaycancel
) {
513 $html .= html_writer
::tag('span', ' | ');
514 $html .= html_writer
::link('#', get_string('cancel'), array('id' => 'comment-action-cancel-'.$this->cid
));
517 $html .= html_writer
::end_tag('div'); // .fd
518 $html .= html_writer
::end_tag('div'); // .comment-area
519 $html .= html_writer
::tag('div', '', array('class' => 'clearer'));
522 $html .= html_writer
::end_tag('div'); // .comment-ctrl
523 $html .= html_writer
::end_tag('div'); // .mdl-left
536 * Return matched comments
541 public function get_comments($page = '') {
542 global $DB, $CFG, $USER, $OUTPUT;
543 if (!$this->can_view()) {
546 if (!is_numeric($page)) {
550 $perpage = (!empty($CFG->commentsperpage
))?
$CFG->commentsperpage
:15;
551 $start = $page * $perpage;
552 $ufields = user_picture
::fields('u');
554 list($componentwhere, $component) = $this->get_component_select_sql('c');
556 $params['component'] = $component;
559 $sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
561 JOIN {user} u ON u.id = c.userid
562 WHERE c.contextid = :contextid AND
563 c.commentarea = :commentarea AND
564 c.itemid = :itemid AND
566 ORDER BY c.timecreated DESC";
567 $params['contextid'] = $this->contextid
;
568 $params['commentarea'] = $this->commentarea
;
569 $params['itemid'] = $this->itemid
;
572 $formatoptions = array('overflowdiv' => true);
573 $rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
574 foreach ($rs as $u) {
577 $c->content
= $u->ccontent
;
578 $c->format
= $u->cformat
;
579 $c->timecreated
= $u->ctimecreated
;
580 $c->strftimeformat
= get_string('strftimerecentfull', 'langconfig');
581 $url = new moodle_url('/user/view.php', array('id'=>$u->id
, 'course'=>$this->courseid
));
582 $c->profileurl
= $url->out(false); // URL should not be escaped just yet.
583 $c->fullname
= fullname($u);
584 $c->time
= userdate($c->timecreated
, $c->strftimeformat
);
585 $c->content
= format_text($c->content
, $c->format
, $formatoptions);
586 $c->avatar
= $OUTPUT->user_picture($u, array('size'=>18));
589 $candelete = $this->can_delete($c->id
);
590 if (($USER->id
== $u->id
) ||
!empty($candelete)) {
597 if (!empty($this->plugintype
)) {
598 // moodle module will filter comments
599 $comments = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'display', array($comments, $this->comment_param
), $comments);
606 * Returns an SQL fragment and param for selecting on component.
607 * @param string $alias
610 protected function get_component_select_sql($alias = '') {
611 $component = $this->get_component();
615 if (empty($component)) {
616 $componentwhere = "{$alias}component IS NULL";
619 $componentwhere = "({$alias}component IS NULL OR {$alias}component = :component)";
621 return array($componentwhere, $component);
625 * Returns the number of comments associated with the details of this object
627 * @global moodle_database $DB
630 public function count() {
632 if ($this->totalcommentcount
=== null) {
633 list($where, $component) = $this->get_component_select_sql();
634 $where .= ' AND itemid = :itemid AND commentarea = :commentarea AND contextid = :contextid';
636 'itemid' => $this->itemid
,
637 'commentarea' => $this->commentarea
,
638 'contextid' => $this->context
->id
,
641 $params['component'] = $component;
644 $this->totalcommentcount
= $DB->count_records_select('comments', $where, $params);
646 return $this->totalcommentcount
;
650 * Returns HTML to display a pagination bar
652 * @global stdClass $CFG
653 * @global core_renderer $OUTPUT
657 public function get_pagination($page = 0) {
658 global $CFG, $OUTPUT;
659 $count = $this->count();
660 $perpage = (!empty($CFG->commentsperpage
))?
$CFG->commentsperpage
:15;
661 $pages = (int)ceil($count/$perpage);
662 if ($pages == 1 ||
$pages == 0) {
663 return html_writer
::tag('div', '', array('id' => 'comment-pagination-'.$this->cid
, 'class' => 'comment-pagination'));
665 if (!empty(self
::$nonjs)) {
666 // used in non-js interface
667 return $OUTPUT->paging_bar($count, $page, $perpage, $this->get_nojslink(), 'comment_page');
669 // return ajax paging bar
671 $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid
.'">';
672 for ($p=0; $p<$pages; $p++
) {
678 $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid
.'-'.$p.'">'.($p+
1).'</a> ';
688 * @global moodle_database $DB
689 * @param string $content
693 public function add($content, $format = FORMAT_MOODLE
) {
694 global $CFG, $DB, $USER, $OUTPUT;
695 if (!$this->can_post()) {
696 throw new comment_exception('nopermissiontocomment');
699 $newcmt = new stdClass
;
700 $newcmt->contextid
= $this->contextid
;
701 $newcmt->commentarea
= $this->commentarea
;
702 $newcmt->itemid
= $this->itemid
;
703 $newcmt->component
= !empty($this->component
) ?
$this->component
: null;
704 $newcmt->content
= $content;
705 $newcmt->format
= $format;
706 $newcmt->userid
= $USER->id
;
707 $newcmt->timecreated
= $now;
709 // This callback allow module to modify the content of comment, such as filter or replacement
710 plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'add', array(&$newcmt, $this->comment_param
));
712 $cmt_id = $DB->insert_record('comments', $newcmt);
713 if (!empty($cmt_id)) {
714 $newcmt->id
= $cmt_id;
715 $newcmt->strftimeformat
= get_string('strftimerecentfull', 'langconfig');
716 $newcmt->fullname
= fullname($USER);
717 $url = new moodle_url('/user/view.php', array('id' => $USER->id
, 'course' => $this->courseid
));
718 $newcmt->profileurl
= $url->out();
719 $newcmt->content
= format_text($newcmt->content
, $newcmt->format
, array('overflowdiv'=>true));
720 $newcmt->avatar
= $OUTPUT->user_picture($USER, array('size'=>16));
722 $commentlist = array($newcmt);
724 if (!empty($this->plugintype
)) {
725 // Call the display callback to allow the plugin to format the newly added comment.
726 $commentlist = plugin_callback($this->plugintype
,
730 array($commentlist, $this->comment_param
),
732 $newcmt = $commentlist[0];
734 $newcmt->time
= userdate($newcmt->timecreated
, $newcmt->strftimeformat
);
736 // Trigger comment created event.
737 if (core_component
::is_core_subsystem($this->component
)) {
738 $eventclassname = '\\core\\event\\' . $this->component
. '_comment_created';
740 $eventclassname = '\\' . $this->component
. '\\event\comment_created';
742 if (class_exists($eventclassname)) {
743 $event = $eventclassname::create(
745 'context' => $this->context
,
746 'objectid' => $newcmt->id
,
748 'itemid' => $this->itemid
756 throw new comment_exception('dbupdatefailed');
761 * delete by context, commentarea and itemid
762 * @param stdClass|array $param {
763 * contextid => int the context in which the comments exist [required]
764 * commentarea => string the comment area [optional]
765 * itemid => int comment itemid [optional]
769 public static function delete_comments($param) {
771 $param = (array)$param;
772 if (empty($param['contextid'])) {
775 $DB->delete_records('comments', $param);
780 * Delete page_comments in whole course, used by course reset
782 * @param stdClass $context course context
784 public static function reset_course_page_comments($context) {
787 $contexts[] = $context->id
;
788 $children = $context->get_child_contexts();
789 foreach ($children as $c) {
790 $contexts[] = $c->id
;
792 list($ids, $params) = $DB->get_in_or_equal($contexts);
793 $DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params);
799 * @param int $commentid
802 public function delete($commentid) {
804 $candelete = has_capability('moodle/comment:delete', $this->context
);
805 if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) {
806 throw new comment_exception('dbupdatefailed');
808 if (!($USER->id
== $comment->userid ||
!empty($candelete))) {
809 throw new comment_exception('nopermissiontocomment');
811 $DB->delete_records('comments', array('id'=>$commentid));
812 // Trigger comment delete event.
813 if (core_component
::is_core_subsystem($this->component
)) {
814 $eventclassname = '\\core\\event\\' . $this->component
. '_comment_deleted';
816 $eventclassname = '\\' . $this->component
. '\\event\comment_deleted';
818 if (class_exists($eventclassname)) {
819 $event = $eventclassname::create(
821 'context' => $this->context
,
822 'objectid' => $commentid,
824 'itemid' => $this->itemid
827 $event->add_record_snapshot('comments', $comment);
837 * @param bool $return return comments list string or print it out
838 * @param bool $nonjs print nonjs comments list or not?
839 * @return string|void
841 public function print_comments($page = 0, $return = true, $nonjs = true) {
842 global $DB, $CFG, $PAGE;
844 if (!$this->can_view()) {
848 if (!(self
::$comment_itemid == $this->itemid
&&
849 self
::$comment_context == $this->context
->id
&&
850 self
::$comment_area == $this->commentarea
&&
851 self
::$comment_component == $this->component
855 $comments = $this->get_comments($page);
859 $html .= html_writer
::tag('h3', get_string('comments'));
860 $html .= html_writer
::start_tag('ul', array('id' => 'comment-list-'.$this->cid
, 'class' => 'comment-list'));
862 // Reverse the comments array to display them in the correct direction
863 foreach (array_reverse($comments) as $cmt) {
864 $html .= html_writer
::tag('li', $this->print_comment($cmt, $nonjs), array('id' => 'comment-'.$cmt->id
.'-'.$this->cid
));
867 $html .= html_writer
::end_tag('ul');
868 $html .= $this->get_pagination($page);
870 if ($nonjs && $this->can_post()) {
871 // Form to add comments
872 $html .= html_writer
::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/comment/comment_post.php')));
873 // Comment parameters
874 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'contextid', 'value' => $this->contextid
));
875 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'add'));
876 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'area', 'value' => $this->commentarea
));
877 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'component', 'value' => $this->component
));
878 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'itemid', 'value' => $this->itemid
));
879 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'courseid', 'value' => $this->courseid
));
880 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
881 $html .= html_writer
::empty_tag('input', array('type' => 'hidden', 'name' => 'returnurl', 'value' => $PAGE->url
));
882 // Textarea for the actual comment
883 $html .= html_writer
::tag('textarea', '', array('name' => 'content', 'rows' => 2));
884 // Submit button to add the comment
885 $html .= html_writer
::empty_tag('input', array('type' => 'submit', 'value' => get_string('submit')));
886 $html .= html_writer
::end_tag('form');
896 * Returns an array containing comments in HTML format.
898 * @global core_renderer $OUTPUT
899 * @param stdClass $cmt {
900 * id => int comment id
901 * content => string comment content
902 * format => int comment text format
903 * timecreated => int comment's timecreated
904 * profileurl => string link to user profile
905 * fullname => comment author's full name
906 * avatar => string user's avatar
907 * delete => boolean does user have permission to delete comment?
912 public function print_comment($cmt, $nonjs = true) {
915 $replacements = array();
917 if (!empty($cmt->delete
) && empty($nonjs)) {
918 $strdelete = get_string('deletecommentbyon', 'moodle', (object)['user' => $cmt->fullname
, 'time' => $cmt->time
]);
919 $deletelink = html_writer
::start_tag('div', array('class'=>'comment-delete'));
920 $deletelink .= html_writer
::start_tag('a', array('href' => '#', 'id' => 'comment-delete-'.$this->cid
.'-'.$cmt->id
,
921 'title' => $strdelete));
923 $deletelink .= $OUTPUT->pix_icon('t/delete', get_string('delete'));
924 $deletelink .= html_writer
::end_tag('a');
925 $deletelink .= html_writer
::end_tag('div');
926 $cmt->content
= $deletelink . $cmt->content
;
928 $patterns[] = '___picture___';
929 $patterns[] = '___name___';
930 $patterns[] = '___content___';
931 $patterns[] = '___time___';
932 $replacements[] = $cmt->avatar
;
933 $replacements[] = html_writer
::link($cmt->profileurl
, $cmt->fullname
);
934 $replacements[] = $cmt->content
;
935 $replacements[] = $cmt->time
;
937 // use html template to format a single comment.
938 return str_replace($patterns, $replacements, $this->template
);
942 * Revoke validate callbacks
944 * @param stdClass $params addtionall parameters need to add to callbacks
946 protected function validate($params=array()) {
947 foreach ($params as $key=>$value) {
948 $this->comment_param
->$key = $value;
950 $validation = plugin_callback($this->plugintype
, $this->pluginname
, 'comment', 'validate', array($this->comment_param
), false);
952 throw new comment_exception('invalidcommentparam');
957 * Returns true if the user is able to view comments
960 public function can_view() {
962 return !empty($this->viewcap
);
966 * Returns true if the user can add comments against this comment description
969 public function can_post() {
971 return isloggedin() && !empty($this->postcap
);
975 * Returns true if the user can delete this comment
976 * @param int $commentid
979 public function can_delete($commentid) {
980 $this->validate(array('commentid'=>$commentid));
981 return has_capability('moodle/comment:delete', $this->context
);
985 * Returns the component associated with the comment.
989 public function get_component() {
990 return $this->component
;
994 * Do not call! I am a deprecated method because of the typo in my name.
995 * @deprecated since 2.9
996 * @see comment::get_component()
999 public function get_compontent() {
1000 return $this->get_component();
1004 * Returns the context associated with the comment
1007 public function get_context() {
1008 return $this->context
;
1012 * Returns the course id associated with the comment
1015 public function get_courseid() {
1016 return $this->courseid
;
1020 * Returns the course module associated with the comment
1024 public function get_cm() {
1029 * Returns the item id associated with the comment
1033 public function get_itemid() {
1034 return $this->itemid
;
1038 * Returns the comment area associated with the commentarea
1042 public function get_commentarea() {
1043 return $this->commentarea
;
1047 * Make the comments textarea fullwidth.
1049 * @since 2.8.1 + 2.7.4
1050 * @param bool $fullwidth
1052 public function set_fullwidth($fullwidth = true) {
1053 $this->fullwidth
= (bool)$fullwidth;
1057 * Return the template.
1062 public function get_template() {
1063 return $this->template
;
1072 public function get_cid() {
1077 * Return the link text.
1082 public function get_linktext() {
1083 return $this->linktext
;
1092 public function get_notoggle() {
1093 return $this->notoggle
;
1097 * Return display total count.
1102 public function get_displaytotalcount() {
1103 return $this->displaytotalcount
;
1107 * Return display cancel.
1112 public function get_displaycancel() {
1113 return $this->displaycancel
;
1122 public function get_fullwidth() {
1123 return $this->fullwidth
;
1132 public function get_autostart() {
1133 return $this->autostart
;
1139 * Comment exception class
1142 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
1143 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1145 class comment_exception
extends moodle_exception
{