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 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  declare(strict_types=1);
  18  
  19  namespace core_comment\reportbuilder\datasource;
  20  
  21  use core\reportbuilder\local\entities\context;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\user;
  24  use core_comment\reportbuilder\local\entities\comment;
  25  
  26  /**
  27   * Comments datasource
  28   *
  29   * @package     core_comment
  30   * @copyright   2022 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class comments extends datasource {
  34  
  35      /**
  36       * Return user friendly name of the report source
  37       *
  38       * @return string
  39       */
  40      public static function get_name(): string {
  41          return get_string('comments', 'core_comment');
  42      }
  43  
  44      /**
  45       * Initialise report
  46       */
  47      protected function initialise(): void {
  48          $commententity = new comment();
  49          $commentalias = $commententity->get_table_alias('comments');
  50  
  51          $this->set_main_table('comments', $commentalias);
  52          $this->add_entity($commententity);
  53  
  54          // Join the context entity.
  55          $contextentity = (new context())
  56              ->set_table_alias('context', $commententity->get_table_alias('context'));
  57          $this->add_entity($contextentity
  58              ->add_join($commententity->get_context_join())
  59          );
  60  
  61          // Join the user entity to the comment userid (author).
  62          $userentity = new user();
  63          $useralias = $userentity->get_table_alias('user');
  64          $this->add_entity($userentity
  65              ->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$commentalias}.userid"));
  66  
  67          // Add report elements from each of the entities we added to the report.
  68          $this->add_all_from_entities();
  69      }
  70  
  71      /**
  72       * Return the columns that will be added to the report upon creation
  73       *
  74       * @return string[]
  75       */
  76      public function get_default_columns(): array {
  77          return [
  78              'user:fullname',
  79              'context:name',
  80              'comment:content',
  81              'comment:timecreated',
  82          ];
  83      }
  84  
  85      /**
  86       * Return the column sorting that will be added to the report upon creation
  87       *
  88       * @return int[]
  89       */
  90      public function get_default_column_sorting(): array {
  91          return [
  92              'user:fullname' => SORT_ASC,
  93              'comment:timecreated' => SORT_ASC,
  94          ];
  95      }
  96  
  97      /**
  98       * Return the filters that will be added to the report upon creation
  99       *
 100       * @return string[]
 101       */
 102      public function get_default_filters(): array {
 103          return [
 104              'comment:content',
 105          ];
 106      }
 107  
 108      /**
 109       * Return the conditions that will be added to the report upon creation
 110       *
 111       * @return string[]
 112       */
 113      public function get_default_conditions(): array {
 114          return [
 115              'user:fullname',
 116          ];
 117      }
 118  }