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 401 and 402] [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          $userentity = new user();
  80          $useralias = $userentity->get_table_alias('user');
  81          $this->add_entity($userentity
  82              ->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$postalias}.userid"));
  83  
  84          // Join the course entity for course blogs.
  85          $courseentity = new course();
  86          $coursealias = $courseentity->get_table_alias('course');
  87          $this->add_entity($courseentity
  88              ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.id = {$postalias}.courseid"));
  89  
  90          // Join the comment entity (ensure differing alias from that used by course entity).
  91          $commententity = (new comment())
  92              ->set_table_alias('comments', 'bcmt');
  93          $this->add_entity($commententity
  94              ->add_join("LEFT JOIN {comments} bcmt ON bcmt.component = 'blog' AND bcmt.itemid = {$postalias}.id"));
  95  
  96          // Add report elements from each of the entities we added to the report.
  97          $this->add_all_from_entity($blogentity->get_entity_name());
  98  
  99          // Add specific file/tag entity elements.
 100          $this->add_columns_from_entity($fileentity->get_entity_name(), ['name', 'size', 'type', 'timecreated']);
 101          $this->add_filters_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 102          $this->add_conditions_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 103  
 104          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
 105          $this->add_filter($tagentity->get_filter('name'));
 106          $this->add_condition($tagentity->get_condition('name'));
 107  
 108          $this->add_all_from_entity($userentity->get_entity_name());
 109          $this->add_all_from_entity($courseentity->get_entity_name());
 110  
 111          // Add specific comment entity elements.
 112          $this->add_columns_from_entity($commententity->get_entity_name(), ['content', 'timecreated']);
 113          $this->add_filter($commententity->get_filter('timecreated'));
 114          $this->add_condition($commententity->get_filter('timecreated'));
 115      }
 116  
 117      /**
 118       * Return the columns that will be added to the report upon creation
 119       *
 120       * @return string[]
 121       */
 122      public function get_default_columns(): array {
 123          return [
 124              'user:fullname',
 125              'course:fullname',
 126              'blog:title',
 127              'blog:timecreated',
 128          ];
 129      }
 130  
 131      /**
 132       * Return the filters that will be added to the report upon creation
 133       *
 134       * @return string[]
 135       */
 136      public function get_default_filters(): array {
 137          return [
 138              'user:fullname',
 139              'blog:title',
 140              'blog:timecreated',
 141          ];
 142      }
 143  
 144      /**
 145       * Return the conditions that will be added to the report upon creation
 146       *
 147       * @return string[]
 148       */
 149      public function get_default_conditions(): array {
 150          return [
 151              'blog:publishstate',
 152          ];
 153      }
 154  }