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_notes\reportbuilder\datasource;
  20  
  21  use lang_string;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\{course, user};
  24  use core_notes\reportbuilder\local\entities\note;
  25  
  26  defined('MOODLE_INTERNAL') || die;
  27  
  28  global $CFG;
  29  require_once("{$CFG->dirroot}/notes/lib.php");
  30  
  31  /**
  32   * Notes datasource
  33   *
  34   * @package     core_notes
  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 notes extends datasource {
  39  
  40      /**
  41       * Return user friendly name of the report source
  42       *
  43       * @return string
  44       */
  45      public static function get_name(): string {
  46          return get_string('notes', 'core_notes');
  47      }
  48  
  49      /**
  50       * Initialise report
  51       */
  52      protected function initialise(): void {
  53          $noteentity = new note();
  54  
  55          $postalias = $noteentity->get_table_alias('post');
  56          $this->set_main_table('post', $postalias);
  57          $this->add_base_condition_simple("{$postalias}.module", 'notes');
  58  
  59          $this->add_entity($noteentity);
  60  
  61          // Join the user entity to represent the note recipient.
  62          $recipiententity = (new user())
  63              ->set_entity_name('recipient')
  64              ->set_entity_title(new lang_string('recipient', 'core_notes'));
  65          $recipientalias = $recipiententity->get_table_alias('user');
  66          $this->add_entity($recipiententity->add_join("
  67              LEFT JOIN {user} {$recipientalias}
  68                     ON {$recipientalias}.id = {$postalias}.userid")
  69          );
  70  
  71          // Join the user entity to represent the note author. Override all entity table aliases to avoid clash with first instance.
  72          $authorentity = (new user())
  73              ->set_entity_name('author')
  74              ->set_entity_title(new lang_string('author', 'core_notes'))
  75              ->set_table_aliases([
  76                  'user' => 'au',
  77                  'context' => 'auctx',
  78              ]);
  79          $this->add_entity($authorentity->add_join("
  80              LEFT JOIN {user} au
  81                     ON au.id = {$postalias}.usermodified")
  82          );
  83  
  84          // Join the course entity for course notes.
  85          $courseentity = new course();
  86          $coursealias = $courseentity->get_table_alias('course');
  87          $this->add_entity($courseentity->add_join("
  88              LEFT JOIN {course} {$coursealias}
  89                     ON {$coursealias}.id = {$postalias}.courseid
  90                    AND {$postalias}.publishstate = '" . NOTES_STATE_PUBLIC . "'")
  91          );
  92  
  93          // Add report elements from each of the entities we added to the report.
  94          $this->add_all_from_entities();
  95      }
  96  
  97      /**
  98       * Return the columns that will be added to the report upon creation
  99       *
 100       * @return string[]
 101       */
 102      public function get_default_columns(): array {
 103          return [
 104              'recipient:fullname',
 105              'note:publishstate',
 106              'course:fullname',
 107              'note:content',
 108              'note:timecreated',
 109          ];
 110      }
 111  
 112      /**
 113       * Return the column sorting that will be added to the report upon creation
 114       *
 115       * @return int[]
 116       */
 117      public function get_default_column_sorting(): array {
 118          return [
 119              'recipient:fullname' => SORT_ASC,
 120              'note:timecreated' => SORT_ASC,
 121          ];
 122      }
 123  
 124      /**
 125       * Return the filters that will be added to the report upon creation
 126       *
 127       * @return string[]
 128       */
 129      public function get_default_filters(): array {
 130          return [
 131              'recipient:fullname',
 132          ];
 133      }
 134  
 135      /**
 136       * Return the conditions that will be added to the report upon creation
 137       *
 138       * @return string[]
 139       */
 140      public function get_default_conditions(): array {
 141          return [
 142              'note:publishstate',
 143              'course:fullname',
 144              'recipient:fullname',
 145          ];
 146      }
 147  }