Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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          $usernamefields = get_all_user_name_fields(true, 'u');
      65          $sql = "SELECT c.id, c.contextid, c.itemid, c.commentarea, c.userid, c.content, $usernamefields, c.timecreated
      66                    FROM {comments} c
      67                    JOIN {user} u
      68                         ON u.id=c.userid
      69                ORDER BY c.timecreated ASC";
      70          $rs = $DB->get_recordset_sql($sql, null, $start, $this->perpage);
      71          $formatoptions = array('overflowdiv' => true);
      72          foreach ($rs as $item) {
      73              // Set calculated fields
      74              $item->fullname = fullname($item);
      75              $item->time = userdate($item->timecreated);
      76              $item->content = format_text($item->content, FORMAT_MOODLE, $formatoptions);
      77              // Unset fields not related to the comment
      78              foreach (get_all_user_name_fields() as $namefield) {
      79                  unset($item->$namefield);
      80              }
      81              unset($item->timecreated);
      82              // Record the comment
      83              $comments[] = $item;
      84          }
      85          $rs->close();
      86  
      87          return $comments;
      88      }
      89  
      90      /**
      91       * Records the course object
      92       *
      93       * @global moodle_page $PAGE
      94       * @global moodle_database $DB
      95       * @param int $courseid
      96       */
      97      private function setup_course($courseid) {
      98          global $PAGE, $DB;
      99          if (!empty($this->course) && $this->course->id == $courseid) {
     100              // already set, stop
     101              return;
     102          }
     103          if ($courseid == $PAGE->course->id) {
     104              $this->course = $PAGE->course;
     105          } else if (!$this->course = $DB->get_record('course', array('id' => $courseid))) {
     106              $this->course = null;
     107          }
     108      }
     109  
     110      /**
     111       * Sets up the module or block information for a comment
     112       *
     113       * @global moodle_database $DB
     114       * @param stdClass $comment
     115       * @return bool
     116       */
     117      private function setup_plugin($comment) {
     118          global $DB;
     119          $this->context = context::instance_by_id($comment->contextid, IGNORE_MISSING);
     120          if (!$this->context) {
     121              return false;
     122          }
     123          switch ($this->context->contextlevel) {
     124              case CONTEXT_BLOCK:
     125                  if ($block = $DB->get_record('block_instances', array('id' => $this->context->instanceid))) {
     126                      $this->plugintype = 'block';
     127                      $this->pluginname = $block->blockname;
     128                  } else {
     129                      return false;
     130                  }
     131                  break;
     132              case CONTEXT_MODULE:
     133                  $this->plugintype = 'mod';
     134                  $this->cm = get_coursemodule_from_id('', $this->context->instanceid);
     135                  $this->setup_course($this->cm->course);
     136                  $this->modinfo = get_fast_modinfo($this->course);
     137                  $this->pluginname = $this->modinfo->cms[$this->cm->id]->modname;
     138                  break;
     139          }
     140          return true;
     141      }
     142  
     143      /**
     144       * Print comments
     145       * @param int $page
     146       * @return bool return false if no comments available
     147       */
     148      public function print_comments($page = 0) {
     149          global $OUTPUT, $CFG, $OUTPUT, $DB;
     150  
     151          $count = $DB->count_records('comments');
     152          $comments = $this->get_comments($page);
     153          if (count($comments) == 0) {
     154              echo $OUTPUT->notification(get_string('nocomments', 'moodle'));
     155              return false;
     156          }
     157  
     158          $table = new html_table();
     159          $table->head = array (
     160              html_writer::checkbox('selectall', '', false, get_string('selectall'), array('id'=>'comment_select_all', 'class'=>'comment-report-selectall')),
     161              get_string('author', 'search'),
     162              get_string('content'),
     163              get_string('action')
     164          );
     165          $table->colclasses = array ('leftalign', 'leftalign', 'leftalign', 'leftalign');
     166          $table->attributes = array('class'=>'admintable generaltable');
     167          $table->id = 'commentstable';
     168          $table->data = array();
     169  
     170          $link = new moodle_url('/comment/index.php', array('action' => 'delete', 'sesskey' => sesskey()));
     171          foreach ($comments as $c) {
     172              $this->setup_plugin($c);
     173              if (!empty($this->plugintype)) {
     174                  $context_url = plugin_callback($this->plugintype, $this->pluginname, 'comment', 'url', array($c));
     175              }
     176              $checkbox = html_writer::checkbox('comments', $c->id, false);
     177              $action = html_writer::link(new moodle_url($link, array('commentid' => $c->id)), get_string('delete'));
     178              if (!empty($context_url)) {
     179                  $action .= html_writer::empty_tag('br');
     180                  $action .= html_writer::link($context_url, get_string('commentincontext'), array('target'=>'_blank'));
     181              }
     182              $table->data[] = array($checkbox, $c->fullname, $c->content, $action);
     183          }
     184          echo html_writer::table($table);
     185          echo $OUTPUT->paging_bar($count, $page, $this->perpage, $CFG->wwwroot.'/comment/index.php');
     186          return true;
     187      }
     188  
     189      /**
     190       * Delete a comment
     191       *
     192       * @param int $commentid
     193       * @return bool
     194       */
     195      public function delete_comment($commentid) {
     196          global $DB;
     197          if ($DB->record_exists('comments', array('id' => $commentid))) {
     198              $DB->delete_records('comments', array('id' => $commentid));
     199              return true;
     200          }
     201          return false;
     202      }
     203      /**
     204       * Delete comments
     205       *
     206       * @param string $list A list of comment ids separated by hyphens
     207       * @return bool
     208       */
     209      public function delete_comments($list) {
     210          global $DB;
     211          $ids = explode('-', $list);
     212          foreach ($ids as $id) {
     213              $id = (int)$id;
     214              if ($DB->record_exists('comments', array('id' => $id))) {
     215                  $DB->delete_records('comments', array('id' => $id));
     216              }
     217          }
     218          return true;
     219      }
     220  }
    

    Search This Site: