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.
   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\local\entities;
  20  
  21  use lang_string;
  22  use stdClass;
  23  use core_reportbuilder\local\entities\base;
  24  use core_reportbuilder\local\filters\{date, select, text};
  25  use core_reportbuilder\local\helpers\format;
  26  use core_reportbuilder\local\report\{column, filter};
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  global $CFG;
  31  require_once("{$CFG->dirroot}/notes/lib.php");
  32  
  33  /**
  34   * Note entity
  35   *
  36   * @package     core_notes
  37   * @copyright   2022 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class note extends base {
  41  
  42      /**
  43       * Database tables that this entity uses and their default aliases
  44       *
  45       * @return array
  46       */
  47      protected function get_default_table_aliases(): array {
  48          return ['post' => 'np'];
  49      }
  50  
  51      /**
  52       * The default title for this entity
  53       *
  54       * @return lang_string
  55       */
  56      protected function get_default_entity_title(): lang_string {
  57          return new lang_string('note', 'core_notes');
  58      }
  59  
  60      /**
  61       * Initialise the entity
  62       *
  63       * @return base
  64       */
  65      public function initialise(): base {
  66          $columns = $this->get_all_columns();
  67          foreach ($columns as $column) {
  68              $this->add_column($column);
  69          }
  70  
  71          // All the filters defined by the entity can also be used as conditions.
  72          $filters = $this->get_all_filters();
  73          foreach ($filters as $filter) {
  74              $this
  75                  ->add_filter($filter)
  76                  ->add_condition($filter);
  77          }
  78  
  79          return $this;
  80      }
  81  
  82      /**
  83       * Returns list of all available columns
  84       *
  85       * @return column[]
  86       */
  87      protected function get_all_columns(): array {
  88          global $DB;
  89  
  90          $postalias = $this->get_table_alias('post');
  91  
  92          // Content.
  93          $contentfieldsql = "{$postalias}.content";
  94          if ($DB->get_dbfamily() === 'oracle') {
  95              $contentfieldsql = $DB->sql_order_by_text($contentfieldsql, 1024);
  96          }
  97          $columns[] = (new column(
  98              'content',
  99              new lang_string('content', 'core_notes'),
 100              $this->get_entity_name()
 101          ))
 102              ->add_joins($this->get_joins())
 103              ->set_type(column::TYPE_LONGTEXT)
 104              ->add_field($contentfieldsql, 'content')
 105              ->add_field("{$postalias}.format")
 106              ->add_callback(static function(?string $content, stdClass $note): string {
 107                  if ($content === null) {
 108                      return '';
 109                  }
 110                  return format_text($content, $note->format);
 111              });
 112  
 113          // Publish state.
 114          $columns[] = (new column(
 115              'publishstate',
 116              new lang_string('publishstate', 'core_notes'),
 117              $this->get_entity_name()
 118          ))
 119              ->add_joins($this->get_joins())
 120              ->set_type(column::TYPE_TEXT)
 121              ->add_fields("{$postalias}.publishstate")
 122              ->set_is_sortable(true)
 123              ->add_callback(static function(string $publishstate): string {
 124                  $states = [
 125                      NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
 126                      NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
 127                      NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
 128                  ];
 129  
 130                  return (string) ($states[$publishstate] ?? $publishstate);
 131              });
 132  
 133          // Time created.
 134          $columns[] = (new column(
 135              'timecreated',
 136              new lang_string('timecreated', 'core_reportbuilder'),
 137              $this->get_entity_name()
 138          ))
 139              ->add_joins($this->get_joins())
 140              ->set_type(column::TYPE_TIMESTAMP)
 141              ->add_fields("{$postalias}.created")
 142              ->set_is_sortable(true)
 143              ->add_callback([format::class, 'userdate']);
 144  
 145          // Time modified.
 146          $columns[] = (new column(
 147              'timemodified',
 148              new lang_string('timemodified', 'core_reportbuilder'),
 149              $this->get_entity_name()
 150          ))
 151              ->add_joins($this->get_joins())
 152              ->set_type(column::TYPE_TIMESTAMP)
 153              ->add_fields("{$postalias}.lastmodified")
 154              ->set_is_sortable(true)
 155              ->add_callback([format::class, 'userdate']);
 156  
 157          return $columns;
 158      }
 159  
 160      /**
 161       * Return list of all available filters
 162       *
 163       * @return filter[]
 164       */
 165      protected function get_all_filters(): array {
 166          global $DB;
 167  
 168          $postalias = $this->get_table_alias('post');
 169  
 170          // Content.
 171          $filters[] = (new filter(
 172              text::class,
 173              'content',
 174              new lang_string('content', 'core_notes'),
 175              $this->get_entity_name(),
 176              $DB->sql_cast_to_char("{$postalias}.content")
 177          ))
 178              ->add_joins($this->get_joins());
 179  
 180          // Publish state.
 181          $filters[] = (new filter(
 182              select::class,
 183              'publishstate',
 184              new lang_string('publishstate', 'core_notes'),
 185              $this->get_entity_name(),
 186              "{$postalias}.publishstate"
 187          ))
 188              ->add_joins($this->get_joins())
 189              ->set_options([
 190                  NOTES_STATE_SITE => new lang_string('sitenotes', 'core_notes'),
 191                  NOTES_STATE_PUBLIC => new lang_string('coursenotes', 'core_notes'),
 192                  NOTES_STATE_DRAFT => new lang_string('personalnotes', 'core_notes'),
 193              ]);
 194  
 195          // Time created.
 196          $filters[] = (new filter(
 197              date::class,
 198              'timecreated',
 199              new lang_string('timecreated', 'core_reportbuilder'),
 200              $this->get_entity_name(),
 201              "{$postalias}.created"
 202          ))
 203              ->add_joins($this->get_joins())
 204              ->set_limited_operators([
 205                  date::DATE_ANY,
 206                  date::DATE_CURRENT,
 207                  date::DATE_LAST,
 208                  date::DATE_RANGE,
 209              ]);
 210  
 211          // Time modified.
 212          $filters[] = (new filter(
 213              date::class,
 214              'timemodified',
 215              new lang_string('timemodified', 'core_reportbuilder'),
 216              $this->get_entity_name(),
 217              "{$postalias}.lastmodified"
 218          ))
 219              ->add_joins($this->get_joins())
 220              ->set_limited_operators([
 221                  date::DATE_ANY,
 222                  date::DATE_CURRENT,
 223                  date::DATE_LAST,
 224                  date::DATE_RANGE,
 225              ]);
 226  
 227          return $filters;
 228      }
 229  }