Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]
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. 13 // 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/>. 16 17 /** 18 * Functions and classes for commenting 19 * 20 * @package core 21 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 defined('MOODLE_INTERNAL') || die(); 25 26 /** 27 * Comment is helper class to add/delete comments anywhere in moodle 28 * 29 * @package core 30 * @category comment 31 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} 32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 33 */ 34 class comment { 35 /** @var int there may be several comment box in one page so we need a client_id to recognize them */ 36 private $cid; 37 /** @var string commentarea is used to specify different parts shared the same itemid */ 38 private $commentarea; 39 /** @var int itemid is used to associate with commenting content */ 40 private $itemid; 41 /** @var string this html snippet will be used as a template to build comment content */ 42 private $template; 43 /** @var int The context id for comments */ 44 private $contextid; 45 /** @var stdClass The context itself */ 46 private $context; 47 /** @var int The course id for comments */ 48 private $courseid; 49 /** @var stdClass course module object, only be used to help find pluginname automatically */ 50 private $cm; 51 /** 52 * The component that this comment is for. 53 * 54 * It is STRONGLY recommended to set this. 55 * Added as a database field in 2.9, old comments will have a null component. 56 * 57 * @var string 58 */ 59 private $component; 60 /** @var string This is calculated by normalising the component */ 61 private $pluginname; 62 /** @var string This is calculated by normalising the component */ 63 private $plugintype; 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 */ 69 private $linktext; 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; 80 81 /** 82 * Set to true to remove the col attribute from the textarea making it full width. 83 * @var bool 84 */ 85 protected $fullwidth = false; 86 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; 99 /** @var stdClass comment paramaters for callback. */ 100 protected $comment_param; 101 102 /** 103 * Construct function of comment class, initialise 104 * class members 105 * 106 * @param stdClass $options { 107 * context => context context to use for the comment [required] 108 * component => string which plugin will comment being added to [required] 109 * itemid => int the id of the associated item (forum post, glossary item etc) [required] 110 * area => string comment area 111 * cm => stdClass course module 112 * course => course course object 113 * client_id => string an unique id to identify comment area 114 * autostart => boolean automatically expend comments 115 * showcount => boolean display the number of comments 116 * displaycancel => boolean display cancel button 117 * notoggle => boolean don't show/hide button 118 * linktext => string title of show/hide button 119 * } 120 */ 121 public function __construct(stdClass $options) { 122 $this->viewcap = false; 123 $this->postcap = false; 124 125 // setup client_id 126 if (!empty($options->client_id)) { 127 $this->cid = $options->client_id; 128 } else { 129 $this->cid = uniqid(); 130 } 131 132 // setup context 133 if (!empty($options->context)) { 134 $this->context = $options->context; 135 $this->contextid = $this->context->id; 136 } else if(!empty($options->contextid)) { 137 $this->contextid = $options->contextid; 138 $this->context = context::instance_by_id($this->contextid); 139 } else { 140 throw new \moodle_exception('invalidcontext'); 141 } 142 143 if (!empty($options->component)) { 144 // set and validate component 145 $this->set_component($options->component); 146 } else { 147 // component cannot be empty 148 throw new comment_exception('invalidcomponent'); 149 } 150 151 // setup course 152 // course will be used to generate user profile link 153 if (!empty($options->course)) { 154 $this->courseid = $options->course->id; 155 } else if (!empty($options->courseid)) { 156 $this->courseid = $options->courseid; 157 } else { 158 if ($coursecontext = $this->context->get_course_context(false)) { 159 $this->courseid = $coursecontext->instanceid; 160 } else { 161 $this->courseid = SITEID; 162 } 163 } 164 165 // setup coursemodule 166 if (!empty($options->cm)) { 167 $this->cm = $options->cm; 168 } else { 169 $this->cm = null; 170 } 171 172 // setup commentarea 173 if (!empty($options->area)) { 174 $this->commentarea = $options->area; 175 } 176 177 // setup itemid 178 if (!empty($options->itemid)) { 179 $this->itemid = $options->itemid; 180 } else { 181 $this->itemid = 0; 182 } 183 184 // setup customized linktext 185 if (!empty($options->linktext)) { 186 $this->linktext = $options->linktext; 187 } else { 188 $this->linktext = get_string('comments'); 189 } 190 191 // setup options for callback functions 192 $this->comment_param = new stdClass(); 193 $this->comment_param->context = $this->context; 194 $this->comment_param->courseid = $this->courseid; 195 $this->comment_param->cm = $this->cm; 196 $this->comment_param->commentarea = $this->commentarea; 197 $this->comment_param->itemid = $this->itemid; 198 199 // setup notoggle 200 if (!empty($options->notoggle)) { 201 $this->set_notoggle($options->notoggle); 202 } 203 204 // setup notoggle 205 if (!empty($options->autostart)) { 206 $this->set_autostart($options->autostart); 207 } 208 209 // setup displaycancel 210 if (!empty($options->displaycancel)) { 211 $this->set_displaycancel($options->displaycancel); 212 } 213 214 // setup displaytotalcount 215 if (!empty($options->showcount)) { 216 $this->set_displaytotalcount($options->showcount); 217 } 218 219 // setting post and view permissions 220 $this->check_permissions(); 221 222 // load template 223 $this->template = html_writer::start_tag('div', array('class' => 'comment-message')); 224 225 $this->template .= html_writer::start_tag('div', array('class' => 'comment-message-meta mr-3')); 226 227 $this->template .= html_writer::tag('span', '___picture___', array('class' => 'picture')); 228 $this->template .= html_writer::tag('span', '___name___', array('class' => 'user')) . ' - '; 229 $this->template .= html_writer::tag('span', '___time___', array('class' => 'time')); 230 231 $this->template .= html_writer::end_tag('div'); // .comment-message-meta 232 $this->template .= html_writer::tag('div', '___content___', array('class' => 'text')); 233 234 $this->template .= html_writer::end_tag('div'); // .comment-message 235 236 if (!empty($this->plugintype)) { 237 $this->template = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'template', array($this->comment_param), $this->template); 238 } 239 240 unset($options); 241 } 242 243 /** 244 * Receive nonjs comment parameters 245 * 246 * @param moodle_page $page The page object to initialise comments within 247 * If not provided the global $PAGE is used 248 */ 249 public static function init(moodle_page $page = null) { 250 global $PAGE; 251 252 if (empty($page)) { 253 $page = $PAGE; 254 } 255 // setup variables for non-js interface 256 self::$nonjs = optional_param('nonjscomment', '', PARAM_ALPHANUM); 257 self::$comment_itemid = optional_param('comment_itemid', '', PARAM_INT); 258 self::$comment_component = optional_param('comment_component', '', PARAM_COMPONENT); 259 self::$comment_context = optional_param('comment_context', '', PARAM_INT); 260 self::$comment_page = optional_param('comment_page', '', PARAM_INT); 261 self::$comment_area = optional_param('comment_area', '', PARAM_AREA); 262 263 $page->requires->strings_for_js(array( 264 'addcomment', 265 'comments', 266 'commentscount', 267 'commentsrequirelogin', 268 'deletecommentbyon' 269 ), 270 'moodle' 271 ); 272 } 273 274 /** 275 * Sets the component. 276 * 277 * This method shouldn't be public, changing the component once it has been set potentially 278 * invalidates permission checks. 279 * A coding_error is now thrown if code attempts to change the component. 280 * 281 * @throws coding_exception if you try to change the component after it has been set. 282 * @param string $component 283 */ 284 public function set_component($component) { 285 if (!empty($this->component) && $this->component !== $component) { 286 throw new coding_exception('You cannot change the component of a comment once it has been set'); 287 } 288 $this->component = $component; 289 list($this->plugintype, $this->pluginname) = core_component::normalize_component($component); 290 } 291 292 /** 293 * Determines if the user can view the comment. 294 * 295 * @param bool $value 296 */ 297 public function set_view_permission($value) { 298 $this->viewcap = (bool)$value; 299 } 300 301 /** 302 * Determines if the user can post a comment 303 * 304 * @param bool $value 305 */ 306 public function set_post_permission($value) { 307 $this->postcap = (bool)$value; 308 } 309 310 /** 311 * check posting comments permission 312 * It will check based on user roles and ask modules 313 * If you need to check permission by modules, a 314 * function named $pluginname_check_comment_post must be implemented 315 */ 316 private function check_permissions() { 317 $this->postcap = has_capability('moodle/comment:post', $this->context); 318 $this->viewcap = has_capability('moodle/comment:view', $this->context); 319 if (!empty($this->plugintype)) { 320 $permissions = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'permissions', array($this->comment_param), array('post'=>false, 'view'=>false)); 321 $this->postcap = $this->postcap && $permissions['post']; 322 $this->viewcap = $this->viewcap && $permissions['view']; 323 } 324 } 325 326 /** 327 * Gets a link for this page that will work with JS disabled. 328 * 329 * @global moodle_page $PAGE 330 * @param moodle_page $page 331 * @return moodle_url 332 */ 333 public function get_nojslink(moodle_page $page = null) { 334 if ($page === null) { 335 global $PAGE; 336 $page = $PAGE; 337 } 338 339 $link = new moodle_url($page->url, array( 340 'nonjscomment' => true, 341 'comment_itemid' => $this->itemid, 342 'comment_context' => $this->context->id, 343 'comment_component' => $this->get_component(), 344 'comment_area' => $this->commentarea, 345 )); 346 $link->remove_params(array('comment_page')); 347 return $link; 348 } 349 350 /** 351 * Sets the value of the notoggle option. 352 * 353 * If set to true then the user will not be able to expand and collase 354 * the comment section. 355 * 356 * @param bool $newvalue 357 */ 358 public function set_notoggle($newvalue = true) { 359 $this->notoggle = (bool)$newvalue; 360 } 361 362 /** 363 * Sets the value of the autostart option. 364 * 365 * If set to true then the comments will be loaded during page load. 366 * Normally this happens only once the user expands the comment section. 367 * 368 * @param bool $newvalue 369 */ 370 public function set_autostart($newvalue = true) { 371 $this->autostart = (bool)$newvalue; 372 } 373 374 /** 375 * Sets the displaycancel option 376 * 377 * If set to true then a cancel button will be shown when using the form 378 * to post comments. 379 * 380 * @param bool $newvalue 381 */ 382 public function set_displaycancel($newvalue = true) { 383 $this->displaycancel = (bool)$newvalue; 384 } 385 386 /** 387 * Sets the displaytotalcount option 388 * 389 * If set to true then the total number of comments will be displayed 390 * when printing comments. 391 * 392 * @param bool $newvalue 393 */ 394 public function set_displaytotalcount($newvalue = true) { 395 $this->displaytotalcount = (bool)$newvalue; 396 } 397 398 /** 399 * Initialises the JavaScript that enchances the comment API. 400 * 401 * @param moodle_page $page The moodle page object that the JavaScript should be 402 * initialised for. 403 */ 404 public function initialise_javascript(moodle_page $page) { 405 406 $options = new stdClass; 407 $options->client_id = $this->cid; 408 $options->commentarea = $this->commentarea; 409 $options->itemid = $this->itemid; 410 $options->page = 0; 411 $options->courseid = $this->courseid; 412 $options->contextid = $this->contextid; 413 $options->component = $this->component; 414 $options->notoggle = $this->notoggle; 415 $options->autostart = $this->autostart; 416 417 $page->requires->js_init_call('M.core_comment.init', array($options), true); 418 419 return true; 420 } 421 422 /** 423 * Prepare comment code in html 424 * @param boolean $return 425 * @return string|void 426 */ 427 public function output($return = true) { 428 global $PAGE, $OUTPUT; 429 static $template_printed; 430 431 $this->initialise_javascript($PAGE); 432 433 if (!empty(self::$nonjs)) { 434 // return non js comments interface 435 return $this->print_comments(self::$comment_page, $return, true); 436 } 437 438 $html = ''; 439 440 // print html template 441 // Javascript will use the template to render new comments 442 if (empty($template_printed) && $this->can_view()) { 443 $html .= html_writer::tag('div', $this->template, array('style' => 'display:none', 'id' => 'cmt-tmpl')); 444 $template_printed = true; 445 } 446 447 if ($this->can_view()) { 448 // print commenting icon and tooltip 449 $html .= html_writer::start_tag('div', array('class' => 'mdl-left')); 450 $html .= html_writer::link($this->get_nojslink($PAGE), get_string('showcommentsnonjs'), array('class' => 'showcommentsnonjs')); 451 452 if (!$this->notoggle) { 453 // If toggling is enabled (notoggle=false) then print the controls to toggle 454 // comments open and closed 455 $countstring = ''; 456 if ($this->displaytotalcount) { 457 $countstring = '('.$this->count().')'; 458 } 459 $collapsedimage= 't/collapsed'; 460 if (right_to_left()) { 461 $collapsedimage= 't/collapsed_rtl'; 462 } else { 463 $collapsedimage= 't/collapsed'; 464 } 465 $html .= html_writer::start_tag('a', array( 466 'class' => 'comment-link', 467 'id' => 'comment-link-'.$this->cid, 468 'href' => '#', 469 'role' => 'button', 470 'aria-expanded' => 'false') 471 ); 472 $html .= $OUTPUT->pix_icon($collapsedimage, $this->linktext); 473 $html .= html_writer::tag('span', $this->linktext.' '.$countstring, array('id' => 'comment-link-text-'.$this->cid)); 474 $html .= html_writer::end_tag('a'); 475 } 476 477 $html .= html_writer::start_tag('div', array('id' => 'comment-ctrl-'.$this->cid, 'class' => 'comment-ctrl')); 478 479 if ($this->autostart) { 480 // If autostart has been enabled print the comments list immediatly 481 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list comments-loaded')); 482 $html .= html_writer::tag('li', '', array('class' => 'first')); 483 $html .= $this->print_comments(0, true, false); 484 $html .= html_writer::end_tag('ul'); // .comment-list 485 $html .= $this->get_pagination(0); 486 } else { 487 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list')); 488 $html .= html_writer::tag('li', '', array('class' => 'first')); 489 $html .= html_writer::end_tag('ul'); // .comment-list 490 $html .= html_writer::tag('div', '', array('id' => 'comment-pagination-'.$this->cid, 'class' => 'comment-pagination')); 491 } 492 493 if ($this->can_post()) { 494 // print posting textarea 495 $textareaattrs = array( 496 'name' => 'content', 497 'rows' => 2, 498 'id' => 'dlg-content-'.$this->cid, 499 'aria-label' => get_string('addcomment') 500 ); 501 if (!$this->fullwidth) { 502 $textareaattrs['cols'] = '20'; 503 } else { 504 $textareaattrs['class'] = 'fullwidth'; 505 } 506 507 $html .= html_writer::start_tag('div', array('class' => 'comment-area')); 508 $html .= html_writer::start_tag('div', array('class' => 'db')); 509 $html .= html_writer::tag('textarea', '', $textareaattrs); 510 $html .= html_writer::end_tag('div'); // .db 511 512 $html .= html_writer::start_tag('div', array('class' => 'fd', 'id' => 'comment-action-'.$this->cid)); 513 $html .= html_writer::link('#', get_string('savecomment'), array('id' => 'comment-action-post-'.$this->cid)); 514 515 if ($this->displaycancel) { 516 $html .= html_writer::tag('span', ' | '); 517 $html .= html_writer::link('#', get_string('cancel'), array('id' => 'comment-action-cancel-'.$this->cid)); 518 } 519 520 $html .= html_writer::end_tag('div'); // .fd 521 $html .= html_writer::end_tag('div'); // .comment-area 522 $html .= html_writer::tag('div', '', array('class' => 'clearer')); 523 } 524 525 $html .= html_writer::end_tag('div'); // .comment-ctrl 526 $html .= html_writer::end_tag('div'); // .mdl-left 527 } else { 528 $html = ''; 529 } 530 531 if ($return) { 532 return $html; 533 } else { 534 echo $html; 535 } 536 } 537 538 /** 539 * Return matched comments 540 * 541 * @param int $page 542 * @param str $sortdirection sort direction, ASC or DESC 543 * @return array 544 */ 545 public function get_comments($page = '', $sortdirection = 'DESC') { 546 global $DB, $CFG, $USER, $OUTPUT; 547 if (!$this->can_view()) { 548 return false; 549 } 550 if (!is_numeric($page)) { 551 $page = 0; 552 } 553 $params = array(); 554 $perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15; 555 $start = $page * $perpage; 556 $userfieldsapi = \core_user\fields::for_userpic(); 557 $ufields = $userfieldsapi->get_sql('u', false, '', '', false)->selects; 558 559 list($componentwhere, $component) = $this->get_component_select_sql('c'); 560 if ($component) { 561 $params['component'] = $component; 562 } 563 564 $sortdirection = ($sortdirection === 'ASC') ? 'ASC' : 'DESC'; 565 $sql = "SELECT $ufields, c.id AS cid, c.content AS ccontent, c.format AS cformat, c.timecreated AS ctimecreated 566 FROM {comments} c 567 JOIN {user} u ON u.id = c.userid 568 WHERE c.contextid = :contextid AND 569 c.commentarea = :commentarea AND 570 c.itemid = :itemid AND 571 $componentwhere 572 ORDER BY c.timecreated $sortdirection, c.id $sortdirection"; 573 $params['contextid'] = $this->contextid; 574 $params['commentarea'] = $this->commentarea; 575 $params['itemid'] = $this->itemid; 576 577 $comments = array(); 578 $formatoptions = array('overflowdiv' => true, 'blanktarget' => true); 579 $rs = $DB->get_recordset_sql($sql, $params, $start, $perpage); 580 foreach ($rs as $u) { 581 $c = new stdClass(); 582 $c->id = $u->cid; 583 $c->content = $u->ccontent; 584 $c->format = $u->cformat; 585 $c->timecreated = $u->ctimecreated; 586 $c->strftimeformat = get_string('strftimerecentfull', 'langconfig'); 587 $url = new moodle_url('/user/view.php', array('id'=>$u->id, 'course'=>$this->courseid)); 588 $c->profileurl = $url->out(false); // URL should not be escaped just yet. 589 $c->fullname = fullname($u); 590 $c->time = userdate($c->timecreated, $c->strftimeformat); 591 $c->content = format_text($c->content, $c->format, $formatoptions); 592 $c->avatar = $OUTPUT->user_picture($u, array('size' => 16)); 593 $c->userid = $u->id; 594 595 if ($this->can_delete($c)) { 596 $c->delete = true; 597 } 598 $comments[] = $c; 599 } 600 $rs->close(); 601 602 if (!empty($this->plugintype)) { 603 // moodle module will filter comments 604 $comments = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'display', array($comments, $this->comment_param), $comments); 605 } 606 607 return $comments; 608 } 609 610 /** 611 * Returns an SQL fragment and param for selecting on component. 612 * @param string $alias 613 * @return array 614 */ 615 protected function get_component_select_sql($alias = '') { 616 $component = $this->get_component(); 617 if ($alias) { 618 $alias = $alias.'.'; 619 } 620 if (empty($component)) { 621 $componentwhere = "{$alias}component IS NULL"; 622 $component = null; 623 } else { 624 $componentwhere = "({$alias}component IS NULL OR {$alias}component = :component)"; 625 } 626 return array($componentwhere, $component); 627 } 628 629 /** 630 * Returns the number of comments associated with the details of this object 631 * 632 * @global moodle_database $DB 633 * @return int 634 */ 635 public function count() { 636 global $DB; 637 if ($this->totalcommentcount === null) { 638 list($where, $component) = $this->get_component_select_sql(); 639 $where .= ' AND itemid = :itemid AND commentarea = :commentarea AND contextid = :contextid'; 640 $params = array( 641 'itemid' => $this->itemid, 642 'commentarea' => $this->commentarea, 643 'contextid' => $this->context->id, 644 ); 645 if ($component) { 646 $params['component'] = $component; 647 } 648 649 $this->totalcommentcount = $DB->count_records_select('comments', $where, $params); 650 } 651 return $this->totalcommentcount; 652 } 653 654 /** 655 * Returns HTML to display a pagination bar 656 * 657 * @global stdClass $CFG 658 * @global core_renderer $OUTPUT 659 * @param int $page 660 * @return string 661 */ 662 public function get_pagination($page = 0) { 663 global $CFG, $OUTPUT; 664 $count = $this->count(); 665 $perpage = (!empty($CFG->commentsperpage))?$CFG->commentsperpage:15; 666 $pages = (int)ceil($count/$perpage); 667 if ($pages == 1 || $pages == 0) { 668 return html_writer::tag('div', '', array('id' => 'comment-pagination-'.$this->cid, 'class' => 'comment-pagination')); 669 } 670 if (!empty(self::$nonjs)) { 671 // used in non-js interface 672 return $OUTPUT->paging_bar($count, $page, $perpage, $this->get_nojslink(), 'comment_page'); 673 } else { 674 // return ajax paging bar 675 $str = ''; 676 $str .= '<div class="comment-paging" id="comment-pagination-'.$this->cid.'">'; 677 for ($p=0; $p<$pages; $p++) { 678 if ($p == $page) { 679 $class = 'curpage'; 680 } else { 681 $class = 'pageno'; 682 } 683 $str .= '<a href="#" class="'.$class.'" id="comment-page-'.$this->cid.'-'.$p.'">'.($p+1).'</a> '; 684 } 685 $str .= '</div>'; 686 } 687 return $str; 688 } 689 690 /** 691 * Add a new comment 692 * 693 * @global moodle_database $DB 694 * @param string $content 695 * @param int $format 696 * @return stdClass 697 */ 698 public function add($content, $format = FORMAT_MOODLE) { 699 global $CFG, $DB, $USER, $OUTPUT; 700 if (!$this->can_post()) { 701 throw new comment_exception('nopermissiontocomment'); 702 } 703 $now = time(); 704 $newcmt = new stdClass; 705 $newcmt->contextid = $this->contextid; 706 $newcmt->commentarea = $this->commentarea; 707 $newcmt->itemid = $this->itemid; 708 $newcmt->component = !empty($this->component) ? $this->component : null; 709 $newcmt->content = $content; 710 $newcmt->format = $format; 711 $newcmt->userid = $USER->id; 712 $newcmt->timecreated = $now; 713 714 // This callback allow module to modify the content of comment, such as filter or replacement 715 plugin_callback($this->plugintype, $this->pluginname, 'comment', 'add', array(&$newcmt, $this->comment_param)); 716 717 $cmt_id = $DB->insert_record('comments', $newcmt); 718 if (!empty($cmt_id)) { 719 $newcmt->id = $cmt_id; 720 $newcmt->strftimeformat = get_string('strftimerecentfull', 'langconfig'); 721 $newcmt->fullname = fullname($USER); 722 $url = new moodle_url('/user/view.php', array('id' => $USER->id, 'course' => $this->courseid)); 723 $newcmt->profileurl = $url->out(); 724 $formatoptions = array('overflowdiv' => true, 'blanktarget' => true); 725 $newcmt->content = format_text($newcmt->content, $newcmt->format, $formatoptions); 726 $newcmt->avatar = $OUTPUT->user_picture($USER, array('size'=>16)); 727 728 $commentlist = array($newcmt); 729 730 if (!empty($this->plugintype)) { 731 // Call the display callback to allow the plugin to format the newly added comment. 732 $commentlist = plugin_callback($this->plugintype, 733 $this->pluginname, 734 'comment', 735 'display', 736 array($commentlist, $this->comment_param), 737 $commentlist); 738 $newcmt = $commentlist[0]; 739 } 740 $newcmt->time = userdate($newcmt->timecreated, $newcmt->strftimeformat); 741 742 // Trigger comment created event. 743 if (core_component::is_core_subsystem($this->component)) { 744 $eventclassname = '\\core\\event\\' . $this->component . '_comment_created'; 745 } else { 746 $eventclassname = '\\' . $this->component . '\\event\comment_created'; 747 } 748 if (class_exists($eventclassname)) { 749 $event = $eventclassname::create( 750 array( 751 'context' => $this->context, 752 'objectid' => $newcmt->id, 753 'other' => array( 754 'itemid' => $this->itemid 755 ) 756 )); 757 $event->trigger(); 758 } 759 760 return $newcmt; 761 } else { 762 throw new comment_exception('dbupdatefailed'); 763 } 764 } 765 766 /** 767 * delete by context, commentarea and itemid 768 * @param stdClass|array $param { 769 * contextid => int the context in which the comments exist [required] 770 * commentarea => string the comment area [optional] 771 * itemid => int comment itemid [optional] 772 * } 773 * @return boolean 774 */ 775 public static function delete_comments($param) { 776 global $DB; 777 $param = (array)$param; 778 if (empty($param['contextid'])) { 779 return false; 780 } 781 $DB->delete_records('comments', $param); 782 return true; 783 } 784 785 /** 786 * Delete page_comments in whole course, used by course reset 787 * 788 * @param stdClass $context course context 789 */ 790 public static function reset_course_page_comments($context) { 791 global $DB; 792 $contexts = array(); 793 $contexts[] = $context->id; 794 $children = $context->get_child_contexts(); 795 foreach ($children as $c) { 796 $contexts[] = $c->id; 797 } 798 list($ids, $params) = $DB->get_in_or_equal($contexts); 799 $DB->delete_records_select('comments', "commentarea='page_comments' AND contextid $ids", $params); 800 } 801 802 /** 803 * Delete a comment 804 * 805 * @param int|stdClass $comment The id of a comment, or a comment record. 806 * @return bool 807 */ 808 public function delete($comment) { 809 global $DB; 810 if (is_object($comment)) { 811 $commentid = $comment->id; 812 } else { 813 $commentid = $comment; 814 $comment = $DB->get_record('comments', ['id' => $commentid]); 815 } 816 817 if (!$comment) { 818 throw new comment_exception('dbupdatefailed'); 819 } 820 if (!$this->can_delete($comment)) { 821 throw new comment_exception('nopermissiontocomment'); 822 } 823 $DB->delete_records('comments', array('id'=>$commentid)); 824 // Trigger comment delete event. 825 if (core_component::is_core_subsystem($this->component)) { 826 $eventclassname = '\\core\\event\\' . $this->component . '_comment_deleted'; 827 } else { 828 $eventclassname = '\\' . $this->component . '\\event\comment_deleted'; 829 } 830 if (class_exists($eventclassname)) { 831 $event = $eventclassname::create( 832 array( 833 'context' => $this->context, 834 'objectid' => $commentid, 835 'other' => array( 836 'itemid' => $this->itemid 837 ) 838 )); 839 $event->add_record_snapshot('comments', $comment); 840 $event->trigger(); 841 } 842 return true; 843 } 844 845 /** 846 * Print comments 847 * 848 * @param int $page 849 * @param bool $return return comments list string or print it out 850 * @param bool $nonjs print nonjs comments list or not? 851 * @return string|void 852 */ 853 public function print_comments($page = 0, $return = true, $nonjs = true) { 854 global $DB, $CFG, $PAGE; 855 856 if (!$this->can_view()) { 857 return ''; 858 } 859 860 if (!(self::$comment_itemid == $this->itemid && 861 self::$comment_context == $this->context->id && 862 self::$comment_area == $this->commentarea && 863 self::$comment_component == $this->component 864 )) { 865 $page = 0; 866 } 867 $comments = $this->get_comments($page); 868 869 $html = ''; 870 if ($nonjs) { 871 $html .= html_writer::tag('h3', get_string('comments')); 872 $html .= html_writer::start_tag('ul', array('id' => 'comment-list-'.$this->cid, 'class' => 'comment-list')); 873 } 874 // Reverse the comments array to display them in the correct direction 875 foreach (array_reverse($comments) as $cmt) { 876 $html .= html_writer::tag('li', $this->print_comment($cmt, $nonjs), array('id' => 'comment-'.$cmt->id.'-'.$this->cid)); 877 } 878 if ($nonjs) { 879 $html .= html_writer::end_tag('ul'); 880 $html .= $this->get_pagination($page); 881 } 882 if ($nonjs && $this->can_post()) { 883 // Form to add comments 884 $html .= html_writer::start_tag('form', array('method' => 'post', 'action' => new moodle_url('/comment/comment_post.php'))); 885 // Comment parameters 886 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'contextid', 'value' => $this->contextid)); 887 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'action', 'value' => 'add')); 888 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'area', 'value' => $this->commentarea)); 889 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'component', 'value' => $this->component)); 890 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'itemid', 'value' => $this->itemid)); 891 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'courseid', 'value' => $this->courseid)); 892 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey())); 893 $html .= html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'returnurl', 'value' => $PAGE->url)); 894 // Textarea for the actual comment 895 $html .= html_writer::tag('textarea', '', array('name' => 'content', 'rows' => 2)); 896 // Submit button to add the comment 897 $html .= html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('submit'))); 898 $html .= html_writer::end_tag('form'); 899 } 900 if ($return) { 901 return $html; 902 } else { 903 echo $html; 904 } 905 } 906 907 /** 908 * Returns an array containing comments in HTML format. 909 * 910 * @global core_renderer $OUTPUT 911 * @param stdClass $cmt { 912 * id => int comment id 913 * content => string comment content 914 * format => int comment text format 915 * timecreated => int comment's timecreated 916 * profileurl => string link to user profile 917 * fullname => comment author's full name 918 * avatar => string user's avatar 919 * delete => boolean does user have permission to delete comment? 920 * } 921 * @param bool $nonjs 922 * @return array 923 */ 924 public function print_comment($cmt, $nonjs = true) { 925 global $OUTPUT; 926 $patterns = array(); 927 $replacements = array(); 928 929 if (!empty($cmt->delete) && empty($nonjs)) { 930 $strdelete = get_string('deletecommentbyon', 'moodle', (object)['user' => $cmt->fullname, 'time' => $cmt->time]); 931 $deletelink = html_writer::start_tag('div', array('class'=>'comment-delete')); 932 $deletelink .= html_writer::start_tag('a', array('href' => '#', 'id' => 'comment-delete-'.$this->cid.'-'.$cmt->id, 933 'class' => 'icon-no-margin', 'title' => $strdelete)); 934 935 $deletelink .= $OUTPUT->pix_icon('t/delete', $strdelete); 936 $deletelink .= html_writer::end_tag('a'); 937 $deletelink .= html_writer::end_tag('div'); 938 $cmt->content = $deletelink . $cmt->content; 939 } 940 $patterns[] = '___picture___'; 941 $patterns[] = '___name___'; 942 $patterns[] = '___content___'; 943 $patterns[] = '___time___'; 944 $replacements[] = $cmt->avatar; 945 $replacements[] = html_writer::link($cmt->profileurl, $cmt->fullname); 946 $replacements[] = $cmt->content; 947 $replacements[] = $cmt->time; 948 949 // use html template to format a single comment. 950 return str_replace($patterns, $replacements, $this->template); 951 } 952 953 /** 954 * Revoke validate callbacks 955 * 956 * @param stdClass $params addtionall parameters need to add to callbacks 957 */ 958 protected function validate($params=array()) { 959 foreach ($params as $key=>$value) { 960 $this->comment_param->$key = $value; 961 } 962 $validation = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'validate', array($this->comment_param), false); 963 if (!$validation) { 964 throw new comment_exception('invalidcommentparam'); 965 } 966 } 967 968 /** 969 * Returns true if the user is able to view comments 970 * @return bool 971 */ 972 public function can_view() { 973 $this->validate(); 974 return !empty($this->viewcap); 975 } 976 977 /** 978 * Returns true if the user can add comments against this comment description 979 * @return bool 980 */ 981 public function can_post() { 982 $this->validate(); 983 return isloggedin() && !empty($this->postcap); 984 } 985 986 /** 987 * Returns true if the user can delete this comment. 988 * 989 * The user can delete comments if it is one they posted and they can still make posts, 990 * or they have the capability to delete comments. 991 * 992 * A database call is avoided if a comment record is passed. 993 * 994 * @param int|stdClass $comment The id of a comment, or a comment record. 995 * @return bool 996 */ 997 public function can_delete($comment) { 998 global $USER, $DB; 999 if (is_object($comment)) { 1000 $commentid = $comment->id; 1001 } else { 1002 $commentid = $comment; 1003 } 1004 1005 $this->validate(array('commentid'=>$commentid)); 1006 1007 if (!is_object($comment)) { 1008 // Get the comment record from the database. 1009 $comment = $DB->get_record('comments', array('id' => $commentid), 'id, userid', MUST_EXIST); 1010 } 1011 1012 $hascapability = has_capability('moodle/comment:delete', $this->context); 1013 $owncomment = $USER->id == $comment->userid; 1014 1015 return ($hascapability || ($owncomment && $this->can_post())); 1016 } 1017 1018 /** 1019 * Returns the component associated with the comment. 1020 * 1021 * @return string 1022 */ 1023 public function get_component() { 1024 return $this->component; 1025 } 1026 1027 /** 1028 * Do not call! I am a deprecated method because of the typo in my name. 1029 * @deprecated since 2.9 1030 * @see comment::get_component() 1031 * @return string 1032 */ 1033 public function get_compontent() { 1034 return $this->get_component(); 1035 } 1036 1037 /** 1038 * Returns the context associated with the comment 1039 * @return stdClass 1040 */ 1041 public function get_context() { 1042 return $this->context; 1043 } 1044 1045 /** 1046 * Returns the course id associated with the comment 1047 * @return int 1048 */ 1049 public function get_courseid() { 1050 return $this->courseid; 1051 } 1052 1053 /** 1054 * Returns the course module associated with the comment 1055 * 1056 * @return stdClass 1057 */ 1058 public function get_cm() { 1059 return $this->cm; 1060 } 1061 1062 /** 1063 * Returns the item id associated with the comment 1064 * 1065 * @return int 1066 */ 1067 public function get_itemid() { 1068 return $this->itemid; 1069 } 1070 1071 /** 1072 * Returns the comment area associated with the commentarea 1073 * 1074 * @return stdClass 1075 */ 1076 public function get_commentarea() { 1077 return $this->commentarea; 1078 } 1079 1080 /** 1081 * Make the comments textarea fullwidth. 1082 * 1083 * @since 2.8.1 + 2.7.4 1084 * @param bool $fullwidth 1085 */ 1086 public function set_fullwidth($fullwidth = true) { 1087 $this->fullwidth = (bool)$fullwidth; 1088 } 1089 1090 /** 1091 * Return the template. 1092 * 1093 * @since 3.1 1094 * @return string 1095 */ 1096 public function get_template() { 1097 return $this->template; 1098 } 1099 1100 /** 1101 * Return the cid. 1102 * 1103 * @since 3.1 1104 * @return string 1105 */ 1106 public function get_cid() { 1107 return $this->cid; 1108 } 1109 1110 /** 1111 * Return the link text. 1112 * 1113 * @since 3.1 1114 * @return string 1115 */ 1116 public function get_linktext() { 1117 return $this->linktext; 1118 } 1119 1120 /** 1121 * Return no toggle. 1122 * 1123 * @since 3.1 1124 * @return bool 1125 */ 1126 public function get_notoggle() { 1127 return $this->notoggle; 1128 } 1129 1130 /** 1131 * Return display total count. 1132 * 1133 * @since 3.1 1134 * @return bool 1135 */ 1136 public function get_displaytotalcount() { 1137 return $this->displaytotalcount; 1138 } 1139 1140 /** 1141 * Return display cancel. 1142 * 1143 * @since 3.1 1144 * @return bool 1145 */ 1146 public function get_displaycancel() { 1147 return $this->displaycancel; 1148 } 1149 1150 /** 1151 * Return fullwidth. 1152 * 1153 * @since 3.1 1154 * @return bool 1155 */ 1156 public function get_fullwidth() { 1157 return $this->fullwidth; 1158 } 1159 1160 /** 1161 * Return autostart. 1162 * 1163 * @since 3.1 1164 * @return bool 1165 */ 1166 public function get_autostart() { 1167 return $this->autostart; 1168 } 1169 1170 } 1171 1172 /** 1173 * Comment exception class 1174 * 1175 * @package core 1176 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} 1177 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 1178 */ 1179 class comment_exception extends moodle_exception { 1180 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body