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.
   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 core_tag_collection;
  22  use lang_string;
  23  use stdClass;
  24  use core_reportbuilder\local\entities\base;
  25  use core_reportbuilder\local\filters\{boolean_select, select};
  26  use core_reportbuilder\local\helpers\format;
  27  use core_reportbuilder\local\report\{column, filter};
  28  
  29  /**
  30   * Tag collection entity
  31   *
  32   * @package     core_tag
  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 collection extends base {
  37  
  38      /**
  39       * Database tables that this entity uses and their default aliases
  40       *
  41       * @return array
  42       */
  43      protected function get_default_table_aliases(): array {
  44          return ['tag_coll' => 'tc'];
  45      }
  46  
  47      /**
  48       * The default title for this entity
  49       *
  50       * @return lang_string
  51       */
  52      protected function get_default_entity_title(): lang_string {
  53          return new lang_string('tagcollection', 'core_tag');
  54      }
  55  
  56      /**
  57       * Initialise the entity
  58       *
  59       * @return base
  60       */
  61      public function initialise(): base {
  62          $columns = $this->get_all_columns();
  63          foreach ($columns as $column) {
  64              $this->add_column($column);
  65          }
  66  
  67          // All the filters defined by the entity can also be used as conditions.
  68          $filters = $this->get_all_filters();
  69          foreach ($filters as $filter) {
  70              $this
  71                  ->add_filter($filter)
  72                  ->add_condition($filter);
  73          }
  74  
  75          return $this;
  76      }
  77  
  78      /**
  79       * Returns list of all available columns
  80       *
  81       * @return column[]
  82       */
  83      protected function get_all_columns(): array {
  84          $collectionalias = $this->get_table_alias('tag_coll');
  85  
  86          // Name.
  87          $columns[] = (new column(
  88              'name',
  89              new lang_string('name'),
  90              $this->get_entity_name()
  91          ))
  92              ->add_joins($this->get_joins())
  93              ->set_type(column::TYPE_TEXT)
  94              ->add_fields("{$collectionalias}.name, {$collectionalias}.component, {$collectionalias}.isdefault,
  95                  {$collectionalias}.id")
  96              ->set_is_sortable(true)
  97              ->add_callback(static function(?string $name, stdClass $collection): string {
  98                  return core_tag_collection::display_name($collection);
  99              });
 100  
 101          // Default.
 102          $columns[] = (new column(
 103              'default',
 104              new lang_string('defautltagcoll', 'core_tag'),
 105              $this->get_entity_name()
 106          ))
 107              ->add_joins($this->get_joins())
 108              ->set_type(column::TYPE_BOOLEAN)
 109              ->add_fields("{$collectionalias}.isdefault")
 110              ->set_is_sortable(true)
 111              ->add_callback([format::class, 'boolean_as_text']);
 112  
 113          // Component.
 114          $columns[] = (new column(
 115              'component',
 116              new lang_string('component', 'core_tag'),
 117              $this->get_entity_name()
 118          ))
 119              ->add_joins($this->get_joins())
 120              ->set_type(column::TYPE_TEXT)
 121              ->add_fields("{$collectionalias}.component")
 122              ->set_is_sortable(true);
 123  
 124          // Searchable.
 125          $columns[] = (new column(
 126              'searchable',
 127              new lang_string('searchable', 'core_tag'),
 128              $this->get_entity_name()
 129          ))
 130              ->add_joins($this->get_joins())
 131              ->set_type(column::TYPE_BOOLEAN)
 132              ->add_fields("{$collectionalias}.searchable")
 133              ->set_is_sortable(true)
 134              ->add_callback([format::class, 'boolean_as_text']);
 135  
 136          // Custom URL.
 137          $columns[] = (new column(
 138              'customurl',
 139              new lang_string('url'),
 140              $this->get_entity_name()
 141          ))
 142              ->add_joins($this->get_joins())
 143              ->set_type(column::TYPE_TEXT)
 144              ->add_fields("{$collectionalias}.customurl")
 145              ->set_is_sortable(true);
 146  
 147          return $columns;
 148      }
 149  
 150      /**
 151       * Return list of all available filters
 152       *
 153       * @return filter[]
 154       */
 155      protected function get_all_filters(): array {
 156          $collectionalias = $this->get_table_alias('tag_coll');
 157  
 158          // Name.
 159          $filters[] = (new filter(
 160              select::class,
 161              'name',
 162              new lang_string('name'),
 163              $this->get_entity_name(),
 164              "{$collectionalias}.id"
 165          ))
 166              ->add_joins($this->get_joins())
 167              ->set_options_callback(static function(): array {
 168                  global $DB;
 169  
 170                  $collections = $DB->get_records('tag_coll', [], 'sortorder', 'id, name, component, isdefault');
 171                  return array_map(static function(stdClass $collection): string {
 172                      return core_tag_collection::display_name($collection);
 173                  }, $collections);
 174              });
 175  
 176          // Default.
 177          $filters[] = (new filter(
 178              boolean_select::class,
 179              'default',
 180              new lang_string('defautltagcoll', 'core_tag'),
 181              $this->get_entity_name(),
 182              "{$collectionalias}.isdefault"
 183          ))
 184              ->add_joins($this->get_joins());
 185  
 186          // Searchable.
 187          $filters[] = (new filter(
 188              boolean_select::class,
 189              'searchable',
 190              new lang_string('searchable', 'core_tag'),
 191              $this->get_entity_name(),
 192              "{$collectionalias}.searchable"
 193          ))
 194              ->add_joins($this->get_joins());
 195  
 196          return $filters;
 197      }
 198  }