Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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_tag\reportbuilder\local\entities;
  20  
  21  use context_system;
  22  use core_tag_tag;
  23  use html_writer;
  24  use lang_string;
  25  use stdClass;
  26  use core_reportbuilder\local\entities\base;
  27  use core_reportbuilder\local\filters\{boolean_select, date, tags};
  28  use core_reportbuilder\local\helpers\format;
  29  use core_reportbuilder\local\report\{column, filter};
  30  
  31  /**
  32   * Tag entity
  33   *
  34   * @package     core_tag
  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 tag extends base {
  39  
  40      /**
  41       * Database tables that this entity uses and their default aliases
  42       *
  43       * @return array
  44       */
  45      protected function get_default_table_aliases(): array {
  46          return ['tag' => 't'];
  47      }
  48  
  49      /**
  50       * The default title for this entity
  51       *
  52       * @return lang_string
  53       */
  54      protected function get_default_entity_title(): lang_string {
  55          return new lang_string('tag', 'core_tag');
  56      }
  57  
  58      /**
  59       * Initialise the entity
  60       *
  61       * @return base
  62       */
  63      public function initialise(): base {
  64          $columns = $this->get_all_columns();
  65          foreach ($columns as $column) {
  66              $this->add_column($column);
  67          }
  68  
  69          // All the filters defined by the entity can also be used as conditions.
  70          $filters = $this->get_all_filters();
  71          foreach ($filters as $filter) {
  72              $this
  73                  ->add_filter($filter)
  74                  ->add_condition($filter);
  75          }
  76  
  77          return $this;
  78      }
  79  
  80      /**
  81       * Returns list of all available columns
  82       *
  83       * @return column[]
  84       */
  85      protected function get_all_columns(): array {
  86          global $DB;
  87  
  88          $tagalias = $this->get_table_alias('tag');
  89  
  90          // Name.
  91          $columns[] = (new column(
  92              'name',
  93              new lang_string('name', 'core_tag'),
  94              $this->get_entity_name()
  95          ))
  96              ->add_joins($this->get_joins())
  97              ->set_type(column::TYPE_TEXT)
  98              ->add_fields("{$tagalias}.rawname, {$tagalias}.name")
  99              ->set_is_sortable(true)
 100              ->add_callback(static function($rawname, stdClass $tag): string {
 101                  if ($rawname === null) {
 102                      return '';
 103                  }
 104                  return core_tag_tag::make_display_name($tag);
 105              });
 106  
 107          // Name with link.
 108          $columns[] = (new column(
 109              'namewithlink',
 110              new lang_string('namewithlink', 'core_tag'),
 111              $this->get_entity_name()
 112          ))
 113              ->add_joins($this->get_joins())
 114              ->set_type(column::TYPE_TEXT)
 115              ->add_fields("{$tagalias}.rawname, {$tagalias}.name, {$tagalias}.tagcollid")
 116              ->set_is_sortable(true)
 117              ->add_callback(static function($rawname, stdClass $tag): string {
 118                  if ($rawname === null) {
 119                      return '';
 120                  }
 121                  return html_writer::link(core_tag_tag::make_url($tag->tagcollid, $tag->rawname),
 122                      core_tag_tag::make_display_name($tag));
 123              });
 124  
 125          // Description.
 126          $descriptionfieldsql = "{$tagalias}.description";
 127          if ($DB->get_dbfamily() === 'oracle') {
 128              $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
 129          }
 130          $columns[] = (new column(
 131              'description',
 132              new lang_string('tagdescription', 'core_tag'),
 133              $this->get_entity_name()
 134          ))
 135              ->add_joins($this->get_joins())
 136              ->set_type(column::TYPE_LONGTEXT)
 137              ->add_field($descriptionfieldsql, 'description')
 138              ->add_fields("{$tagalias}.descriptionformat, {$tagalias}.id")
 139              ->add_callback(static function(?string $description, stdClass $tag): string {
 140                  global $CFG;
 141                  require_once("{$CFG->libdir}/filelib.php");
 142  
 143                  if ($description === null) {
 144                      return '';
 145                  }
 146  
 147                  $context = context_system::instance();
 148                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'tag',
 149                      'description', $tag->id);
 150  
 151                  return format_text($description, $tag->descriptionformat, ['context' => $context->id]);
 152              });
 153  
 154          // Standard.
 155          $columns[] = (new column(
 156              'standard',
 157              new lang_string('standardtag', 'core_tag'),
 158              $this->get_entity_name()
 159          ))
 160              ->add_joins($this->get_joins())
 161              ->set_type(column::TYPE_BOOLEAN)
 162              ->add_fields("{$tagalias}.isstandard")
 163              ->set_is_sortable(true)
 164              ->add_callback([format::class, 'boolean_as_text']);
 165  
 166          // Flagged.
 167          $columns[] = (new column(
 168              'flagged',
 169              new lang_string('flagged', 'core_tag'),
 170              $this->get_entity_name()
 171          ))
 172              ->add_joins($this->get_joins())
 173              ->set_type(column::TYPE_BOOLEAN)
 174              ->add_field("CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END", 'flag')
 175              ->set_is_sortable(true, ["{$tagalias}.flag"])
 176              ->add_callback([format::class, 'boolean_as_text']);
 177  
 178          // Time modified.
 179          $columns[] = (new column(
 180              'timemodified',
 181              new lang_string('timemodified', 'core_reportbuilder'),
 182              $this->get_entity_name()
 183          ))
 184              ->add_joins($this->get_joins())
 185              ->set_type(column::TYPE_TIMESTAMP)
 186              ->add_fields("{$tagalias}.timemodified")
 187              ->set_is_sortable(true)
 188              ->add_callback([format::class, 'userdate']);
 189  
 190          return $columns;
 191      }
 192  
 193      /**
 194       * Return list of all available filters
 195       *
 196       * @return filter[]
 197       */
 198      protected function get_all_filters(): array {
 199          $tagalias = $this->get_table_alias('tag');
 200  
 201          // Name.
 202          $filters[] = (new filter(
 203              tags::class,
 204              'name',
 205              new lang_string('name', 'core_tag'),
 206              $this->get_entity_name(),
 207              "{$tagalias}.id"
 208          ))
 209              ->add_joins($this->get_joins());
 210  
 211          // Standard.
 212          $filters[] = (new filter(
 213              boolean_select::class,
 214              'standard',
 215              new lang_string('standardtag', 'core_tag'),
 216              $this->get_entity_name(),
 217              "{$tagalias}.isstandard"
 218          ))
 219              ->add_joins($this->get_joins());
 220  
 221          // Flagged.
 222          $filters[] = (new filter(
 223              boolean_select::class,
 224              'flagged',
 225              new lang_string('flagged', 'core_tag'),
 226              $this->get_entity_name(),
 227              "CASE WHEN {$tagalias}.flag > 0 THEN 1 ELSE {$tagalias}.flag END"
 228          ))
 229              ->add_joins($this->get_joins());
 230  
 231          // Time modified.
 232          $filters[] = (new filter(
 233              date::class,
 234              'timemodified',
 235              new lang_string('timemodified', 'core_reportbuilder'),
 236              $this->get_entity_name(),
 237              "{$tagalias}.timemodified"
 238          ))
 239              ->add_joins($this->get_joins())
 240              ->set_limited_operators([
 241                  date::DATE_ANY,
 242                  date::DATE_CURRENT,
 243                  date::DATE_LAST,
 244                  date::DATE_RANGE,
 245              ]);
 246  
 247          return $filters;
 248      }
 249  }