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_blog\reportbuilder\datasource;
  20  
  21  use lang_string;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\{course, user};
  24  use core_blog\reportbuilder\local\entities\blog;
  25  use core_files\reportbuilder\local\entities\file;
  26  use core_comment\reportbuilder\local\entities\comment;
  27  use core_tag\reportbuilder\local\entities\tag;
  28  
  29  /**
  30   * Blogs datasource
  31   *
  32   * @package     core_blog
  33   * @copyright   2022 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class blogs extends datasource {
  37  
  38      /**
  39       * Return user friendly name of the report source
  40       *
  41       * @return string
  42       */
  43      public static function get_name(): string {
  44          return get_string('blogs', 'core_blog');
  45      }
  46  
  47      /**
  48       * Initialise report
  49       */
  50      protected function initialise(): void {
  51          $blogentity = new blog();
  52  
  53          $postalias = $blogentity->get_table_alias('post');
  54          $this->set_main_table('post', $postalias);
  55          $this->add_base_condition_simple("{$postalias}.module", 'blog');
  56  
  57          $this->add_entity($blogentity);
  58  
  59          // Join the files entity.
  60          $fileentity = (new file())
  61              ->set_entity_title(new lang_string('blogattachment', 'core_blog'));
  62          $filesalias = $fileentity->get_table_alias('files');
  63          $this->add_entity($fileentity
  64              ->add_join("LEFT JOIN {files} {$filesalias}
  65                  ON {$filesalias}.contextid = " . SYSCONTEXTID . "
  66                 AND {$filesalias}.component = 'blog'
  67                 AND {$filesalias}.filearea = 'attachment'
  68                 AND {$filesalias}.itemid = {$postalias}.id
  69                 AND {$filesalias}.filename != '.'"));
  70  
  71          // Join the tag entity.
  72          $tagentity = (new tag())
  73              ->set_entity_title(new lang_string('blogtags', 'core_blog'))
  74              ->set_table_alias('tag', $blogentity->get_table_alias('tag'));
  75          $this->add_entity($tagentity
  76              ->add_joins($blogentity->get_tag_joins()));
  77  
  78          // Join the user entity to represent the blog author.
  79          $authorentity = (new user())
  80              ->set_entity_title(new lang_string('author', 'core_blog'));
  81          $authoralias = $authorentity->get_table_alias('user');
  82          $this->add_entity($authorentity
  83              ->add_join("LEFT JOIN {user} {$authoralias} ON {$authoralias}.id = {$postalias}.userid"));
  84  
  85          // Join the course entity for course blogs.
  86          $courseentity = new course();
  87          $coursealias = $courseentity->get_table_alias('course');
  88          $this->add_entity($courseentity
  89              ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid"));
  90  
  91          // Join the comment entity (ensure differing alias from that used by course entity).
  92          $commententity = (new comment())
  93              ->set_table_alias('comments', 'bcmt');
  94          $this->add_entity($commententity
  95              ->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id"));
  96  
  97          // Join the user entity to represent the comment author. Override table aliases to avoid clash with first instance.
  98          $commenterentity = (new user())
  99              ->set_entity_name('commenter')
 100              ->set_entity_title(new lang_string('commenter', 'core_comment'))
 101              ->set_table_aliases([
 102                  'user' => 'cu',
 103                  'context' => 'cuctx',
 104              ]);
 105          $this->add_entity($commenterentity
 106              ->add_joins($commententity->get_joins())
 107              ->add_join("LEFT JOIN {user} cu ON cu.id = bcmt.userid"));
 108  
 109          // Add report elements from each of the entities we added to the report.
 110          $this->add_all_from_entity($blogentity->get_entity_name());
 111  
 112          // Add specific file/tag entity elements.
 113          $this->add_columns_from_entity($fileentity->get_entity_name(), ['name', 'size', 'type', 'timecreated']);
 114          $this->add_filters_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 115          $this->add_conditions_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 116  
 117          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
 118          $this->add_filter($tagentity->get_filter('name'));
 119          $this->add_condition($tagentity->get_condition('name'));
 120  
 121          $this->add_all_from_entity($authorentity->get_entity_name());
 122          $this->add_all_from_entity($courseentity->get_entity_name());
 123  
 124          // Add specific comment entity elements.
 125          $this->add_columns_from_entity($commententity->get_entity_name(), ['content', 'timecreated']);
 126          $this->add_filter($commententity->get_filter('timecreated'));
 127          $this->add_condition($commententity->get_filter('timecreated'));
 128  
 129          $this->add_all_from_entity($commenterentity->get_entity_name());
 130      }
 131  
 132      /**
 133       * Return the columns that will be added to the report upon creation
 134       *
 135       * @return string[]
 136       */
 137      public function get_default_columns(): array {
 138          return [
 139              'user:fullname',
 140              'course:fullname',
 141              'blog:title',
 142              'blog:timecreated',
 143          ];
 144      }
 145  
 146      /**
 147       * Return the column sorting that will be added to the report upon creation
 148       *
 149       * @return int[]
 150       */
 151      public function get_default_column_sorting(): array {
 152          return [
 153              'user:fullname' => SORT_ASC,
 154              'blog:timecreated' => SORT_ASC,
 155          ];
 156      }
 157  
 158      /**
 159       * Return the filters that will be added to the report upon creation
 160       *
 161       * @return string[]
 162       */
 163      public function get_default_filters(): array {
 164          return [
 165              'user:fullname',
 166              'blog:title',
 167              'blog:timecreated',
 168          ];
 169      }
 170  
 171      /**
 172       * Return the conditions that will be added to the report upon creation
 173       *
 174       * @return string[]
 175       */
 176      public function get_default_conditions(): array {
 177          return [
 178              'blog:publishstate',
 179          ];
 180      }
 181  }