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