Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 400 and 403] [Versions 401 and 403] [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  namespace qbank_comment;
  18  
  19  use core_question\local\bank\column_base;
  20  use question_bank;
  21  
  22  /**
  23   * A column to show the number of comments.
  24   *
  25   * @package    qbank_comment
  26   * @copyright  2021 Catalyst IT Australia Pty Ltd
  27   * @author     Safat Shahin <safatshahin@catalyst-au.net>
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class comment_count_column extends column_base {
  31  
  32      /**
  33       * @var bool Comments enabled or not from config.
  34       */
  35      protected $commentsenabled = true;
  36  
  37      /**
  38       * Load javascript module if enabled.
  39       *
  40       * @return void
  41       */
  42      public function init(): void {
  43          parent::init();
  44          $this->check_comments_status();
  45          if ($this->commentsenabled) {
  46              global $PAGE;
  47              $PAGE->requires->js_call_amd('qbank_comment/comment', 'init');
  48          }
  49      }
  50  
  51      /**
  52       * Check if comments is turned on in the system or not.
  53       */
  54      protected function check_comments_status(): void {
  55          global $CFG;
  56          if (!$CFG->usecomments) {
  57              $this->commentsenabled = false;
  58          }
  59      }
  60  
  61      /**
  62       * Get the name of the column, used internally.
  63       *
  64       * @return string
  65       */
  66      public function get_name(): string {
  67          return 'commentcount';
  68      }
  69  
  70      /**
  71       * Get the title of the column that will be displayed.
  72       *
  73       * @return string
  74       */
  75      public function get_title(): string {
  76          return get_string('commentplural', 'qbank_comment');
  77      }
  78  
  79      /**
  80       * Generate the content to be displayed.
  81       *
  82       * @param object $question The question object.
  83       * @param string $rowclasses Classes that can be added.
  84       */
  85      protected function display_content($question, $rowclasses): void {
  86          global $DB;
  87          $syscontext = \context_system::instance();
  88          $args = [
  89              'component' => 'qbank_comment',
  90              'commentarea' => 'question',
  91              'itemid' => $question->id,
  92              'contextid' => $syscontext->id,
  93          ];
  94          $commentcount = $DB->count_records('comments', $args);
  95          $attributes = [];
  96          if (question_has_capability_on($question, 'comment')) {
  97              $target = 'questioncommentpreview_' . $question->id;
  98              $attributes = [
  99                  'href' => '#',
 100                  'data-target' => $target,
 101                  'data-questionid' => $question->id,
 102                  'data-courseid' => $this->qbank->course->id,
 103                  'data-contextid' => $syscontext->id,
 104              ];
 105          }
 106          echo \html_writer::tag('a', $commentcount, $attributes);
 107      }
 108  
 109      public function get_extra_classes(): array {
 110          return ['pr-3'];
 111      }
 112  
 113      public function get_default_width(): int {
 114          return 150;
 115      }
 116  }