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 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  declare(strict_types=1);
  18  
  19  namespace core_comment\reportbuilder\local\systemreports;
  20  
  21  use context_system;
  22  use lang_string;
  23  use moodle_url;
  24  use pix_icon;
  25  use stdClass;
  26  use core\reportbuilder\local\entities\context;
  27  use core_reportbuilder\system_report;
  28  use core_reportbuilder\local\entities\user;
  29  use core_reportbuilder\local\report\action;
  30  use core_comment\reportbuilder\local\entities\comment;
  31  
  32  /**
  33   * Comments system report
  34   *
  35   * @package     core_comment
  36   * @copyright   2022 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class comments extends system_report {
  40  
  41      /**
  42       * Initialise report, we need to set the main table, load our entities and set columns/filters
  43       */
  44      protected function initialise(): void {
  45          $commententity = new comment();
  46          $commentalias = $commententity->get_table_alias('comments');
  47  
  48          $this->set_main_table('comments', $commentalias);
  49          $this->add_entity($commententity);
  50  
  51          // Base fields required for action callbacks and checkbox toggle.
  52          $this->add_base_fields("{$commentalias}.id");
  53          $this->set_checkbox_toggleall(static function(stdClass $row): array {
  54              return [$row->id, get_string('select')];
  55          });
  56  
  57          // Join the context entity.
  58          $contextentity = (new context())
  59              ->set_table_alias('context', $commententity->get_table_alias('context'));
  60          $this->add_entity($contextentity
  61              ->add_join($commententity->get_context_join())
  62          );
  63  
  64          // Join the user entity to the comment userid (author).
  65          $userentity = new user();
  66          $useralias = $userentity->get_table_alias('user');
  67          $this->add_entity($userentity
  68              ->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$commentalias}.userid"));
  69  
  70          $this->add_columns();
  71          $this->add_filters();
  72          $this->add_actions();
  73  
  74          $this->set_downloadable(true, get_string('comments'));
  75      }
  76  
  77      /**
  78       * Validates access to view this report
  79       *
  80       * @return bool
  81       */
  82      protected function can_view(): bool {
  83          return has_capability('moodle/comment:delete', context_system::instance());
  84      }
  85  
  86      /**
  87       * Add columns to the report
  88       */
  89      protected function add_columns(): void {
  90          $this->add_columns_from_entities([
  91              'user:fullnamewithlink',
  92              'comment:content',
  93              'context:link',
  94              'comment:timecreated',
  95          ]);
  96  
  97          // Default sorting.
  98          $this->set_initial_sort_column('comment:timecreated', SORT_DESC);
  99      }
 100  
 101      /**
 102       * Add filters to the report
 103       */
 104      protected function add_filters(): void {
 105          $this->add_filters_from_entities([
 106              'user:fullname',
 107              'comment:content',
 108              'comment:timecreated',
 109          ]);
 110      }
 111  
 112      /**
 113       * Add actions to report
 114       */
 115      protected function add_actions(): void {
 116          $this->add_action(new action(
 117              new moodle_url('#'),
 118              new pix_icon('t/delete', ''),
 119              ['data-action' => 'comment-delete', 'data-comment-id' => ':id'],
 120              false,
 121              new lang_string('delete')
 122          ));
 123      }
 124  }