Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Functions and classes for comments management
  20   *
  21   * @package   core
  22   * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * comment_manager is helper class to manage moodle comments in admin page (Reports->Comments)
  29   *
  30   * @package   core
  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_manager {
  35  
  36      /** @var int The number of comments to display per page */
  37      private $perpage;
  38  
  39      /**
  40       * Constructs the comment_manage object
  41       */
  42      public function __construct() {
  43          global $CFG;
  44          $this->perpage = $CFG->commentsperpage;
  45      }
  46  
  47      /**
  48       * Return comments by pages
  49       *
  50       * @global moodle_database $DB
  51       * @param int $page
  52       * @return array An array of comments
  53       */
  54      function get_comments($page) {
  55          global $DB;
  56  
  57          if ($page == 0) {
  58              $start = 0;
  59          } else {
  60              $start = $page * $this->perpage;
  61          }
  62          $comments = array();
  63  
  64          $userfieldsapi = \core_user\fields::for_name();
  65          $usernamefields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
  66          $sql = "SELECT c.id, c.contextid, c.itemid, c.component, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
  67                    FROM {comments} c
  68                    JOIN {user} u
  69                         ON u.id=c.userid
  70                ORDER BY c.timecreated ASC";
  71          $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage);
  72          $formatoptions = array('overflowdiv' => true, 'blanktarget' => true);
  73          foreach ($rs as $item) {
  74              // Set calculated fields
  75              $item->fullname = fullname($item);
  76              $item->time = userdate($item->timecreated);
  77              $item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions);
  78              // Unset fields not related to the comment
  79              foreach (\core_user\fields::get_name_fields() as $namefield) {
  80                  unset($item->$namefield);
  81              }
  82              unset($item->timecreated);
  83              // Record the comment
  84              $comments[] = $item;
  85          }
  86          $rs->close();
  87  
  88          return $comments;
  89      }
  90  
  91      /**
  92       * Records the course object
  93       *
  94       * @global moodle_page $PAGE
  95       * @global moodle_database $DB
  96       * @param int $courseid
  97       */
  98      private function setup_course($courseid) {
  99          global $PAGE, $DB;
 100          if (!empty($this->course) && $this->course->id == $courseid) {
 101              // already set, stop
 102              return;
 103          }
 104          if ($courseid == $PAGE->course->id) {
 105              $this->course = $PAGE->course;
 106          } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) {
 107              $this->course = null;
 108          }
 109      }
 110  
 111      /**
 112       * Sets up the module or block information for a comment
 113       *
 114       * @global moodle_database $DB
 115       * @param stdClass $comment
 116       * @return bool
 117       */
 118      private function setup_plugin($comment) {
 119          global $DB;
 120          $this->context = context::instance_by_id($comment->contextid, IGNORE_MISSING);
 121          if (!$this->context) {
 122              return false;
 123          }
 124          switch ($this->context->contextlevel) {
 125              case CONTEXT_BLOCK:
 126                  if ($block = $DB->get_record('block_instances', array('id' => $this->context->instanceid))) {
 127                      $this->plugintype = 'block';
 128                      $this->pluginname = $block->blockname;
 129                  } else {
 130                      return false;
 131                  }
 132                  break;
 133              case CONTEXT_MODULE:
 134                  $this->plugintype = 'mod';
 135                  $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
 136                  $this->setup_course($this->cm->course);
 137                  $this->modinfo = get_fast_modinfo($this->course);
 138                  $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
 139                  break;
 140          }
 141          return true;
 142      }
 143  
 144      /**
 145       * Print comments
 146       * @param int $page
 147       * @return bool return false if no comments available
 148       */
 149      public function print_comments($page = 0) {
 150          global $OUTPUT, $CFG, $OUTPUT, $DB;
 151  
 152          $count = $DB->count_records('comments');
 153          $comments = $this->get_comments($page);
 154          if (count($comments) == 0) {
 155              echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
 156              return false;
 157          }
 158  
 159          $table = new html_table();
 160          $table->head = array (
 161              html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id' => 'comment_select_all',
 162                  'class' => 'mr-1')),
 163              get_string('author', 'search'),
 164              get_string('content'),
 165              get_string('action')
 166          );
 167          $table->colclasses = array ('leftalign', 'leftalign', 'leftalign', 'leftalign');
 168          $table->attributes = array('class'=>'admintable generaltable');
 169          $table->id = 'commentstable';
 170          $table->data = array();
 171  
 172          $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
 173          foreach ($comments as $c) {
 174              $userdata = html_writer::link(new moodle_url('/user/profile.php', ['id' => $c->userid]), $c->fullname);
 175              $this->setup_plugin($c);
 176              if (!empty($this->plugintype)) {
 177                  $context_url = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'url', array($c));
 178              }
 179              $checkbox = html_writer::checkbox('comments', $c->id, false);
 180              $action = html_writer::link(new moodle_url($link, array('commentid' => $c->id)), get_string('delete'));
 181              if (!empty($context_url)) {
 182                  $action .= html_writer::empty_tag('br');
 183                  $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
 184              }
 185              $table->data[] = array($checkbox, $userdata, $c->content, $action);
 186          }
 187          echo html_writer::table($table);
 188          echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
 189          return true;
 190      }
 191  
 192      /**
 193       * Delete a comment
 194       *
 195       * @param int $commentid
 196       * @return bool
 197       */
 198      public function delete_comment($commentid) {
 199          global $DB;
 200          if ($DB->record_exists('comments', array('id' => $commentid))) {
 201              $DB->delete_records('comments', array('id' => $commentid));
 202              return true;
 203          }
 204          return false;
 205      }
 206      /**
 207       * Delete comments
 208       *
 209       * @param string $list A list of comment ids separated by hyphens
 210       * @return bool
 211       */
 212      public function delete_comments($list) {
 213          global $DB;
 214          $ids = explode('-', $list);
 215          foreach ($ids as $id) {
 216              $id = (int)$id;
 217              if ($DB->record_exists('comments', array('id' => $id))) {
 218                  $DB->delete_records('comments', array('id' => $id));
 219              }
 220          }
 221          return true;
 222      }
 223  
 224      /**
 225       * Get comments created since a given time.
 226       *
 227       * @param  stdClass $course    course object
 228       * @param  stdClass $context   context object
 229       * @param  string $component   component name
 230       * @param  int $since          the time to check
 231       * @param  stdClass $cm        course module object
 232       * @return array list of comments db records since the given timelimit
 233       * @since Moodle 3.2
 234       */
 235      public function get_component_comments_since($course, $context, $component, $since, $cm = null) {
 236          global $DB;
 237  
 238          $commentssince = array();
 239          $where = 'contextid = ? AND component = ? AND timecreated > ?';
 240          $comments = $DB->get_records_select('comments', $where, array($context->id, $component, $since));
 241          // Check item by item if we have permissions.
 242          $managersviewstatus = array();
 243          foreach ($comments as $comment) {
 244              // Check if the manager for the item is cached.
 245              if (!isset($managersviewstatus[$comment->commentarea]) or
 246                      !isset($managersviewstatus[$comment->commentarea][$comment->itemid])) {
 247  
 248                  $args = new stdClass;
 249                  $args->area      = $comment->commentarea;
 250                  $args->itemid    = $comment->itemid;
 251                  $args->context   = $context;
 252                  $args->course    = $course;
 253                  $args->client_id = 0;
 254                  $args->component = $component;
 255                  if (!empty($cm)) {
 256                      $args->cm = $cm;
 257                  }
 258  
 259                  $manager = new comment($args);
 260                  $managersviewstatus[$comment->commentarea][$comment->itemid] = $manager->can_view();
 261              }
 262  
 263              if ($managersviewstatus[$comment->commentarea][$comment->itemid]) {
 264                  $commentssince[$comment->id] = $comment;
 265              }
 266          }
 267          return $commentssince;
 268      }
 269  }