Merge branch 'MDL-27852' of git://github.com/timhunt/moodle
[moodle.git] / comment / lib.php
blob184e320f5698fc5a5efb405ef03c753992a63c9a
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Comment is helper class to add/delete comments anywhere in moodle
20 * @package comment
21 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 class comment {
28 /**
29 * there may be several comment box in one page
30 * so we need a client_id to recognize them
31 * @var integer
33 private $cid;
34 /**
35 * commentarea is used to specify different
36 * parts shared the same itemid
37 * @var string
39 private $commentarea;
40 /**
41 * itemid is used to associate with commenting content
42 * @var integer
44 private $itemid;
45 /**
46 * this html snippet will be used as a template
47 * to build comment content
48 * @var string
50 private $template;
51 /**
52 * The context id for comments
53 * @var int
55 private $contextid;
56 /**
57 * The context itself
58 * @var stdClass
60 private $context;
61 /**
62 * The course id for comments
63 * @var int
65 private $courseid;
66 /**
67 * course module object, only be used to help find pluginname automatically
68 * if pluginname is specified, it won't be used at all
69 * @var stdClass
71 private $cm;
72 /**
73 * The component that this comment is for. It is STRONGLY recommended to set this.
74 * @var string
76 private $component;
77 /**
78 * This is calculated by normalising the component
79 * @var string
81 private $pluginname;
82 /**
83 * This is calculated by normalising the component
84 * @var string
86 private $plugintype;
87 /**
88 * Whether the user has the required capabilities/permissions to view comments.
89 * @var bool
91 private $viewcap = false;
92 /**
93 * Whether the user has the required capabilities/permissions to post comments.
94 * @var bool
96 private $postcap = false;
97 /**
98 * to costomize link text
99 * @var string
101 private $linktext;
103 * If set to true then comment sections won't be able to be opened and closed
104 * instead they will always be visible.
105 * @var bool
107 protected $notoggle = false;
109 * If set to true comments are automatically loaded as soon as the page loads.
110 * Normally this happens when the user expands the comment section.
111 * @var bool
113 protected $autostart = false;
115 * If set to true the total count of comments is displayed when displaying comments.
116 * @var bool
118 protected $displaytotalcount = false;
120 * If set to true a cancel button will be shown on the form used to submit comments.
121 * @var bool
123 protected $displaycancel = false;
125 * The number of comments associated with this comments params
126 * @var int
128 protected $totalcommentcount = null;
130 * When set to true any user to the system is able to view comments.
132 * This can be set to true by a plugin by implementing a allow_anonymous_access callback.
133 * By default it is false except on the front page.
135 * @var bool
137 protected $allowanonymousaccess = false;
139 /**#@+
140 * static variable will be used by non-js comments UI
142 private static $nonjs = false;
143 private static $comment_itemid = null;
144 private static $comment_context = null;
145 private static $comment_area = null;
146 private static $comment_page = null;
147 private static $comment_component = null;
148 /**#@-*/
151 * Construct function of comment class, initialise
152 * class members
153 * @param stdClass $options
154 * @param object $options {
155 * context => context context to use for the comment [required]
156 * component => string which plugin will comment being added to [required]
157 * itemid => int the id of the associated item (forum post, glossary item etc) [required]
158 * area => string comment area
159 * cm => stdClass course module
160 * course => course course object
161 * client_id => string an unique id to identify comment area
162 * autostart => boolean automatically expend comments
163 * showcount => boolean display the number of comments
164 * displaycancel => boolean display cancel button
165 * notoggle => boolean don't show/hide button
166 * linktext => string title of show/hide button
169 public function __construct(stdClass $options) {
170 $this->viewcap = false;
171 $this->postcap = false;
173 // setup client_id
174 if (!empty($options->client_id)) {
175 $this->cid = $options->client_id;
176 } else {
177 $this->cid = uniqid();
180 // setup context
181 if (!empty($options->context)) {
182 $this->context = $options->context;
183 $this->contextid = $this->context->id;
184 } else if(!empty($options->contextid)) {
185 $this->contextid = $options->contextid;
186 $this->context = get_context_instance_by_id($this->contextid);
187 } else {
188 print_error('invalidcontext');
191 if (!empty($options->component)) {
192 // set and validate component
193 $this->set_component($options->component);
194 } else {
195 // component cannot be empty
196 throw new comment_exception('invalidcomponent');
199 // setup course
200 // course will be used to generate user profile link
201 if (!empty($options->course)) {
202 $this->courseid = $options->course->id;
203 } else if (!empty($options->courseid)) {
204 $this->courseid = $options->courseid;
205 } else {
206 $this->courseid = SITEID;
209 // setup coursemodule
210 if (!empty($options->cm)) {
211 $this->cm = $options->cm;
212 } else {
213 $this->cm = null;
216 // setup commentarea
217 if (!empty($options->area)) {
218 $this->commentarea = $options->area;
221 // setup itemid
222 if (!empty($options->itemid)) {
223 $this->itemid = $options->itemid;
224 } else {
225 $this->itemid = 0;
228 // setup customized linktext
229 if (!empty($options->linktext)) {
230 $this->linktext = $options->linktext;
231 } else {
232 $this->linktext = get_string('comments');
235 // setup options for callback functions
236 $this->comment_param = new stdClass();
237 $this->comment_param->context = $this->context;
238 $this->comment_param->courseid = $this->courseid;
239 $this->comment_param->cm = $this->cm;
240 $this->comment_param->commentarea = $this->commentarea;
241 $this->comment_param->itemid = $this->itemid;
243 $this->allowanonymousaccess = false;
244 // By default everyone can view comments on the front page
245 if ($this->context->contextlevel == CONTEXT_COURSE && $this->context->instanceid == SITEID) {
246 $this->allowanonymousaccess = true;
247 } else if ($this->context->contextlevel == CONTEXT_MODULE && $this->courseid == SITEID) {
248 $this->allowanonymousaccess = true;
250 if (!empty($this->plugintype) && !empty($this->pluginname)) {
251 // Plugins can override this if they wish.
252 $this->allowanonymousaccess = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'allow_anonymous_access', array($this), $this->allowanonymousaccess);
255 // setup notoggle
256 if (!empty($options->notoggle)) {
257 $this->set_notoggle($options->notoggle);
260 // setup notoggle
261 if (!empty($options->autostart)) {
262 $this->set_autostart($options->autostart);
265 // setup displaycancel
266 if (!empty($options->displaycancel)) {
267 $this->set_displaycancel($options->displaycancel);
270 // setup displaytotalcount
271 if (!empty($options->showcount)) {
272 $this->set_displaytotalcount($options->showcount);
275 // setting post and view permissions
276 $this->check_permissions();
278 // load template
279 $this->template = html_writer::tag('div', '___picture___', array('class' => 'comment-userpicture'));
280 $this->template .= html_writer::start_tag('div', array('class' => 'comment-content'));
281 $this->template .= '___name___ - ';
282 $this->template .= html_writer::tag('span', '___time___');
283 $this->template .= html_writer::tag('div', '___content___');
284 $this->template .= html_writer::end_tag('div'); // .comment-content
285 if (!empty($this->plugintype)) {
286 $this->template = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'template', array($this->comment_param), $this->template);
289 unset($options);
293 * Receive nonjs comment parameters
295 * @param moodle_page $page The page object to initialise comments within
296 * If not provided the global $PAGE is used
298 public static function init(moodle_page $page = null) {
299 global $PAGE;
301 if (empty($page)) {
302 $page = $PAGE;
304 // setup variables for non-js interface
305 self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHANUM);
306 self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT);
307 self::$comment_context = optional_param('comment_context', '', PARAM_INT);
308 self::$comment_page = optional_param('comment_page', '', PARAM_INT);
309 self::$comment_area = optional_param('comment_area', '', PARAM_ALPHAEXT);
311 $page->requires->string_for_js('addcomment', 'moodle');
312 $page->requires->string_for_js('deletecomment', 'moodle');
313 $page->requires->string_for_js('comments', 'moodle');
314 $page->requires->string_for_js('commentsrequirelogin', 'moodle');
318 * Sets the component.
320 * This method shouldn't be public, changing the component once it has been set potentially
321 * invalidates permission checks.
322 * A coding_error is now thrown if code attempts to change the component.
324 * @param string $component
325 * @return void
327 public function set_component($component) {
328 if (!empty($this->component) && $this->component !== $component) {
329 throw new coding_exception('You cannot change the component of a comment once it has been set');
331 $this->component = $component;
332 list($this->plugintype, $this->pluginname) = normalize_component($component);
336 * Determines if the user can view the comment.
338 * @param bool $value
340 public function set_view_permission($value) {
341 $this->viewcap = (bool)$value;
345 * Determines if the user can post a comment
347 * @param bool $value
349 public function set_post_permission($value) {
350 $this->postcap = (bool)$value;
354 * check posting comments permission
355 * It will check based on user roles and ask modules
356 * If you need to check permission by modules, a
357 * function named $pluginname_check_comment_post must be implemented
359 private function check_permissions() {
360 $this->postcap = has_capability('moodle/comment:post', $this->context);
361 $this->viewcap = has_capability('moodle/comment:view', $this->context);
362 if (!empty($this->plugintype)) {
363 $permissions = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'permissions', array($this->comment_param), array('post'=>false, 'view'=>false));
364 if ($this->allowanonymousaccess) {
365 $this->postcap = $permissions['post'];
366 $this->viewcap = $permissions['view'];
367 } else {
368 $this->postcap = $this->postcap && $permissions['post'];
369 $this->viewcap = $this->viewcap && $permissions['view'];
375 * Gets a link for this page that will work with JS disabled.
377 * @global moodle_page $PAGE
378 * @param moodle_page $page
379 * @return moodle_url
381 public function get_nojslink(moodle_page $page = null) {
382 if ($page === null) {
383 global $PAGE;
384 $page = $PAGE;
387 $link = new moodle_url($page->url, array(
388 'nonjscomment' => true,
389 'comment_itemid' => $this->itemid,
390 'comment_context' => $this->context->id,
391 'comment_area' => $this->commentarea,
393 $link->remove_params(array('comment_page'));
394 return $link;
398 * Sets the value of the notoggle option.
400 * If set to true then the user will not be able to expand and collase
401 * the comment section.
403 * @param bool $newvalue
405 public function set_notoggle($newvalue = true) {
406 $this->notoggle = (bool)$newvalue;
410 * Sets the value of the autostart option.
412 * If set to true then the comments will be loaded during page load.
413 * Normally this happens only once the user expands the comment section.
415 * @param bool $newvalue
417 public function set_autostart($newvalue = true) {
418 $this->autostart = (bool)$newvalue;
422 * Sets the displaycancel option
424 * If set to true then a cancel button will be shown when using the form
425 * to post comments.
427 * @param bool $newvalue
429 public function set_displaycancel($newvalue = true) {
430 $this->displaycancel = (bool)$newvalue;
434 * Sets the displaytotalcount option
436 * If set to true then the total number of comments will be displayed
437 * when printing comments.
439 * @param bool $newvalue
441 public function set_displaytotalcount($newvalue = true) {
442 $this->displaytotalcount = (bool)$newvalue;
446 * Initialises the JavaScript that enchances the comment API.
448 * @param moodle_page $page The moodle page object that the JavaScript should be
449 * initialised for.
451 public function initialise_javascript(moodle_page $page) {
453 $options = new stdClass;
454 $options->client_id = $this->cid;
455 $options->commentarea = $this->commentarea;
456 $options->itemid = $this->itemid;
457 $options->page = 0;
458 $options->courseid = $this->courseid;
459 $options->contextid = $this->contextid;
460 $options->component = $this->component;
461 $options->notoggle = $this->notoggle;
462 $options->autostart = $this->autostart;
464 $page->requires->js_init_call('M.core_comment.init', array($options), true);
466 return true;
470 * Prepare comment code in html
471 * @param boolean $return
472 * @return mixed
474 public function output($return = true) {
475 global $PAGE, $OUTPUT;
476 static $template_printed;
478 $this->initialise_javascript($PAGE);
480 if (!empty(self::$nonjs)) {
481 // return non js comments interface
482 return $this->print_comments(self::$comment_page, $return, true);
485 $html = '';
487 // print html template
488 // Javascript will use the template to render new comments
489 if (empty($template_printed) && $this->can_view()) {
490 $html .= html_writer::tag('div', $this->template, array('style' => 'display:none', 'id' => 'cmt-tmpl'));
491 $template_printed = true;
494 if ($this->can_view()) {
495 // print commenting icon and tooltip
496 $html .= html_writer::start_tag('div', array('class' => 'mdl-left'));
497 $html .= html_writer::link($this->get_nojslink($PAGE), get_string('showcommentsnonjs'), array('class' => 'showcommentsnonjs'));
499 if (!$this->notoggle) {
500 // If toggling is enabled (notoggle=false) then print the controls to toggle
501 // comments open and closed
502 $countstring = '';
503 if ($this->displaytotalcount) {
504 $countstring = '('.$this->count().')';
506 $html .= html_writer::start_tag('a', array('class' => 'comment-link', 'id' => 'comment-link-'.$this->cid, 'href' => '#'));
507 $html .= html_writer::empty_tag('img', array('id' => 'comment-img-'.$this->cid, 'src' => $OUTPUT->pix_url('t/collapsed'), 'alt' => $this->linktext, 'title' => $this->linktext));
508 $html .= html_writer::tag('span', $this->linktext.' '.$countstring, array('id' => 'comment-link-text-'.$this->cid));
509 $html .= html_writer::end_tag('a');
512 $html .= html_writer::start_tag('div', array('id' => 'comment-ctrl-'.$this->cid, 'class' => 'comment-ctrl'));
514 if ($this->autostart) {
515 // If autostart has been enabled print the comments list immediatly
516 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list comments-loaded'));
517 $html .= html_writer::tag('li', '', array('class' => 'first'));
518 $html .= $this->print_comments(0, true, false);
519 $html .= html_writer::end_tag('ul'); // .comment-list
520 $html .= $this->get_pagination(0);
521 } else {
522 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list'));
523 $html .= html_writer::tag('li', '', array('class' => 'first'));
524 $html .= html_writer::end_tag('ul'); // .comment-list
525 $html .= html_writer::tag('div', '', array('id' => 'comment-pagination-'.$this->cid, 'class' => 'comment-pagination'));
528 if ($this->can_post()) {
529 // print posting textarea
530 $html .= html_writer::start_tag('div', array('class' => 'comment-area'));
531 $html .= html_writer::start_tag('div', array('class' => 'db'));
532 $html .= html_writer::tag('textarea', '', array('name' => 'content', 'rows' => 2, 'cols' => 20, 'id' => 'dlg-content-'.$this->cid));
533 $html .= html_writer::end_tag('div'); // .db
535 $html .= html_writer::start_tag('div', array('class' => 'fd', 'id' => 'comment-action-'.$this->cid));
536 $html .= html_writer::link('#', get_string('savecomment'), array('id' => 'comment-action-post-'.$this->cid));
538 if ($this->displaycancel) {
539 $html .= html_writer::tag('span', ' | ');
540 $html .= html_writer::link('#', get_string('cancel'), array('id' => 'comment-action-cancel-'.$this->cid));
543 $html .= html_writer::end_tag('div'); // .fd
544 $html .= html_writer::end_tag('div'); // .comment-area
545 $html .= html_writer::tag('div', '', array('class' => 'clearer'));
548 $html .= html_writer::end_tag('div'); // .comment-ctrl
549 $html .= html_writer::end_tag('div'); // .mdl-left
550 } else {
551 $html = '';
554 if ($return) {
555 return $html;
556 } else {
557 echo $html;
562 * Return matched comments
564 * @param int $page
565 * @return mixed
567 public function get_comments($page = '') {
568 global $DB, $CFG, $USER, $OUTPUT;
569 if (!$this->can_view()) {
570 return false;
572 if (!is_numeric($page)) {
573 $page = 0;
575 $params = array();
576 $perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15;
577 $start = $page * $perpage;
578 $ufields = user_picture::fields('u');
579 $sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated
580 FROM {comments} c
581 JOIN {user} u ON u.id = c.userid
582 WHERE c.contextid = :contextid AND c.commentarea = :commentarea AND c.itemid = :itemid
583 ORDER BY c.timecreated DESC";
584 $params['contextid'] = $this->contextid;
585 $params['commentarea'] = $this->commentarea;
586 $params['itemid'] = $this->itemid;
588 $comments = array();
589 $formatoptions = array('overflowdiv' => true);
590 $rs = $DB->get_recordset_sql($sql, $params, $start, $perpage);
591 foreach ($rs as $u) {
592 $c = new stdClass();
593 $c->id = $u->cid;
594 $c->content = $u->ccontent;
595 $c->format = $u->cformat;
596 $c->timecreated = $u->ctimecreated;
597 $url = new moodle_url('/user/view.php', array('id'=>$u->id, 'course'=>$this->courseid));
598 $c->profileurl = $url->out();
599 $c->fullname = fullname($u);
600 $c->time = userdate($c->timecreated, get_string('strftimerecent', 'langconfig'));
601 $c->content = format_text($c->content, $c->format, $formatoptions);
602 $c->avatar = $OUTPUT->user_picture($u, array('size'=>18));
604 $candelete = $this->can_delete($c->id);
605 if (($USER->id == $u->id) || !empty($candelete)) {
606 $c->delete = true;
608 $comments[] = $c;
610 $rs->close();
612 if (!empty($this->plugintype)) {
613 // moodle module will filter comments
614 $comments = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'display', array($comments, $this->comment_param), $comments);
617 return $comments;
621 * Returns the number of comments associated with the details of this object
623 * @global moodle_database $DB
624 * @return int
626 public function count() {
627 global $DB;
628 if ($this->totalcommentcount === null) {
629 $this->totalcommentcount = $DB->count_records('comments', array('itemid' => $this->itemid, 'commentarea' => $this->commentarea, 'contextid' => $this->context->id));
631 return $this->totalcommentcount;
635 * Returns HTML to display a pagination bar
637 * @global stdClass $CFG
638 * @global core_renderer $OUTPUT
639 * @param int $page
640 * @return string
642 public function get_pagination($page = 0) {
643 global $CFG, $OUTPUT;
644 $count = $this->count();
645 $perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15;
646 $pages = (int)ceil($count/$perpage);
647 if ($pages == 1 || $pages == 0) {
648 return html_writer::tag('div', '', array('id' => 'comment-pagination-'.$this->cid, 'class' => 'comment-pagination'));
650 if (!empty(self::$nonjs)) {
651 // used in non-js interface
652 return $OUTPUT->paging_bar($count, $page, $perpage, $this->get_nojslink(), 'comment_page');
653 } else {
654 // return ajax paging bar
655 $str = '';
656 $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid.'">';
657 for ($p=0; $p<$pages; $p++) {
658 if ($p == $page) {
659 $class = 'curpage';
660 } else {
661 $class = 'pageno';
663 $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid.'-'.$p.'">'.($p+1).'</a> ';
665 $str .= '</div>';
667 return $str;
671 * Add a new comment
673 * @global moodle_database $DB
674 * @param string $content
675 * @return mixed
677 public function add($content, $format = FORMAT_MOODLE) {
678 global $CFG, $DB, $USER, $OUTPUT;
679 if (!$this->can_post()) {
680 throw new comment_exception('nopermissiontocomment');
682 $now = time();
683 $newcmt = new stdClass;
684 $newcmt->contextid = $this->contextid;
685 $newcmt->commentarea = $this->commentarea;
686 $newcmt->itemid = $this->itemid;
687 $newcmt->content = $content;
688 $newcmt->format = $format;
689 $newcmt->userid = $USER->id;
690 $newcmt->timecreated = $now;
692 // This callback allow module to modify the content of comment, such as filter or replacement
693 plugin_callback($this->plugintype, $this->pluginname, 'comment', 'add', array(&$newcmt, $this->comment_param));
695 $cmt_id = $DB->insert_record('comments', $newcmt);
696 if (!empty($cmt_id)) {
697 $newcmt->id = $cmt_id;
698 $newcmt->time = userdate($now, get_string('strftimerecent', 'langconfig'));
699 $newcmt->fullname = fullname($USER);
700 $url = new moodle_url('/user/view.php', array('id' => $USER->id, 'course' => $this->courseid));
701 $newcmt->profileurl = $url->out();
702 $newcmt->content = format_text($newcmt->content, $format, array('overflowdiv'=>true));
703 $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16));
704 return $newcmt;
705 } else {
706 throw new comment_exception('dbupdatefailed');
711 * delete by context, commentarea and itemid
712 * @param stdClass|array $param {
713 * contextid => int the context in which the comments exist [required]
714 * commentarea => string the comment area [optional]
715 * itemid => int comment itemid [optional]
717 * @return boolean
719 public function delete_comments($param) {
720 global $DB;
721 $param = (array)$param;
722 if (empty($param['contextid'])) {
723 return false;
725 $DB->delete_records('comments', $param);
726 return true;
730 * Delete page_comments in whole course, used by course reset
732 * @param stdClass $context course context
734 public function reset_course_page_comments($context) {
735 global $DB;
736 $contexts = array();
737 $contexts[] = $context->id;
738 $children = get_child_contexts($context);
739 foreach ($children as $c) {
740 $contexts[] = $c->id;
742 list($ids, $params) = $DB->get_in_or_equal($contexts);
743 $DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params);
747 * Delete a comment
749 * @param int $commentid
750 * @return mixed
752 public function delete($commentid) {
753 global $DB, $USER;
754 $candelete = has_capability('moodle/comment:delete', $this->context);
755 if (!$comment = $DB->get_record('comments', array('id'=>$commentid))) {
756 throw new comment_exception('dbupdatefailed');
758 if (!($USER->id == $comment->userid || !empty($candelete))) {
759 throw new comment_exception('nopermissiontocomment');
761 $DB->delete_records('comments', array('id'=>$commentid));
762 return true;
766 * Print comments
768 * @param int $page
769 * @param boolean $return return comments list string or print it out
770 * @param boolean $nonjs print nonjs comments list or not?
771 * @return mixed
773 public function print_comments($page = 0, $return = true, $nonjs = true) {
774 global $DB, $CFG, $PAGE;
776 if (!$this->can_view()) {
777 return '';
780 $html = '';
781 if (!(self::$comment_itemid == $this->itemid &&
782 self::$comment_context == $this->context->id &&
783 self::$comment_area == $this->commentarea)) {
784 $page = 0;
786 $comments = $this->get_comments($page);
788 $html = '';
789 if ($nonjs) {
790 $html .= html_writer::tag('h3', get_string('comments'));
791 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list'));
793 // Reverse the comments array to display them in the correct direction
794 foreach (array_reverse($comments) as $cmt) {
795 $html .= html_writer::tag('li', $this->print_comment($cmt, $nonjs), array('id' => 'comment-'.$cmt->id.'-'.$this->cid));
797 if ($nonjs) {
798 $html .= html_writer::end_tag('ul');
799 $html .= $this->get_pagination($page);
801 if ($nonjs && $this->can_post()) {
802 // Form to add comments
803 $html .= html_writer::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/comment/comment_post.php')));
804 // Comment parameters
805 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'contextid', 'value' => $this->contextid));
806 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'add'));
807 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'area', 'value' => $this->commentarea));
808 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'component', 'value' => $this->component));
809 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'itemid', 'value' => $this->itemid));
810 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'courseid', 'value' => $this->courseid));
811 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
812 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'returnurl', 'value' => $PAGE->url));
813 // Textarea for the actual comment
814 $html .= html_writer::tag('textarea', '', array('name' => 'content', 'rows' => 2));
815 // Submit button to add the comment
816 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('submit')));
817 $html .= html_writer::end_tag('form');
819 if ($return) {
820 return $html;
821 } else {
822 echo $html;
827 * Returns an array containing comments in HTML format.
829 * @global core_renderer $OUTPUT
830 * @param stdClass $cmt {
831 * id => int comment id
832 * content => string comment content
833 * format => int comment text format
834 * timecreated => int comment's timecreated
835 * profileurl => string link to user profile
836 * fullname => comment author's full name
837 * avatar => string user's avatar
838 * delete => boolean does user have permission to delete comment?
840 * @param bool $nonjs
841 * @return array
843 public function print_comment($cmt, $nonjs = true) {
844 global $OUTPUT;
845 $patterns = array();
846 $replacements = array();
848 if (!empty($cmt->delete) && empty($nonjs)) {
849 $deletelink = html_writer::start_tag('div', array('class'=>'comment-delete'));
850 $deletelink .= html_writer::start_tag('a', array('href' => '#', 'id' => 'comment-delete-'.$this->cid.'-'.$cmt->id));
851 $deletelink .= $OUTPUT->pix_icon('t/delete', get_string('delete'));
852 $deletelink .= html_writer::end_tag('a');
853 $deletelink .= html_writer::end_tag('div');
854 $cmt->content = $deletelink . $cmt->content;
856 $patterns[] = '___picture___';
857 $patterns[] = '___name___';
858 $patterns[] = '___content___';
859 $patterns[] = '___time___';
860 $replacements[] = $cmt->avatar;
861 $replacements[] = html_writer::link($cmt->profileurl, $cmt->fullname);
862 $replacements[] = $cmt->content;
863 $replacements[] = userdate($cmt->timecreated, get_string('strftimerecent', 'langconfig'));
865 // use html template to format a single comment.
866 return str_replace($patterns, $replacements, $this->template);
870 * Revoke validate callbacks
872 * @param stdClass $params addtionall parameters need to add to callbacks
874 protected function validate($params=array()) {
875 foreach ($params as $key=>$value) {
876 $this->comment_param->$key = $value;
878 $validation = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'validate', array($this->comment_param), false);
879 if (!$validation) {
880 throw new comment_exception('invalidcommentparam');
885 * Returns true if the user is able to view comments
886 * @return bool
888 public function can_view() {
889 $this->validate();
890 return !empty($this->viewcap);
894 * Returns true if the user can add comments against this comment description
895 * @return bool
897 public function can_post() {
898 $this->validate();
899 return isloggedin() && !empty($this->postcap);
903 * Returns true if the user can delete this comment
904 * @param int $commentid
905 * @return bool
907 public function can_delete($commentid) {
908 $this->validate(array('commentid'=>$commentid));
909 return has_capability('moodle/comment:delete', $this->context);
913 * Returns the component associated with the comment
914 * @return string
916 public function get_compontent() {
917 return $this->component;
921 * Returns the context associated with the comment
922 * @return stdClass
924 public function get_context() {
925 return $this->context;
929 * Returns the course id associated with the comment
930 * @return int
932 public function get_courseid() {
933 return $this->courseid;
937 * Returns the course module associated with the comment
939 * @return stdClass
941 public function get_cm() {
942 return $this->cm;
946 * Returns the item id associated with the comment
948 * @return int
950 public function get_itemid() {
951 return $this->itemid;
955 * Returns the comment area associated with the commentarea
957 * @return stdClass
959 public function get_commentarea() {
960 return $this->commentarea;
964 class comment_exception extends moodle_exception {