Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [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       * Get the name of the column, used internally.
  34       *
  35       * @return string
  36       */
  37      public function get_name(): string {
  38          return 'commentcount';
  39      }
  40  
  41      /**
  42       * Get the title of the column that will be displayed.
  43       *
  44       * @return string
  45       */
  46      public function get_title(): string {
  47          return get_string('commentplural', 'qbank_comment');
  48      }
  49  
  50      /**
  51       * Generate the content to be displayed.
  52       *
  53       * @param object $question The question object.
  54       * @param string $rowclasses Classes that can be added.
  55       */
  56      protected function display_content($question, $rowclasses): void {
  57          global $DB, $PAGE;
  58          $syscontext = \context_system::instance();
  59          $args = [
  60              'component' => 'qbank_comment',
  61              'commentarea' => 'question',
  62              'itemid' => $question->id,
  63              'contextid' => $syscontext->id,
  64          ];
  65          $commentcount = $DB->count_records('comments', $args);
  66          $attributes = [];
  67          if (question_has_capability_on($question, 'comment')) {
  68              $target = 'questioncommentpreview_' . $question->id;
  69              $datatarget = '[data-target="' . $target . '"]';
  70              $PAGE->requires->js_call_amd('qbank_comment/comment', 'init', [$datatarget]);
  71              $attributes = [
  72                  'href' => '#',
  73                  'data-target' => $target,
  74                  'data-questionid' => $question->id,
  75                  'data-courseid' => $this->qbank->course->id,
  76                  'data-contextid' => $syscontext->id,
  77              ];
  78          }
  79          echo \html_writer::tag('a', $commentcount, $attributes);
  80      }
  81  
  82      public function get_extra_classes(): array {
  83          return ['pr-3'];
  84      }
  85  
  86  }