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_group\reportbuilder\local\entities;
  20  
  21  use context_course;
  22  use context_helper;
  23  use lang_string;
  24  use stdClass;
  25  use core_reportbuilder\local\entities\base;
  26  use core_reportbuilder\local\filters\{date, text};
  27  use core_reportbuilder\local\helpers\{custom_fields, format};
  28  use core_reportbuilder\local\report\{column, filter};
  29  
  30  /**
  31   * Grouping entity
  32   *
  33   * @package     core_group
  34   * @copyright   2022 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class grouping extends base {
  38  
  39      /**
  40       * Database tables that this entity uses and their default aliases
  41       *
  42       * @return array
  43       */
  44      protected function get_default_table_aliases(): array {
  45          return [
  46              'context' => 'ggctx',
  47              'groupings' => 'gg',
  48          ];
  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('grouping', 'core_group');
  58      }
  59  
  60      /**
  61       * Initialise the entity
  62       *
  63       * @return base
  64       */
  65      public function initialise(): base {
  66          $groupingsalias = $this->get_table_alias('groupings');
  67  
  68          $customfields = (new custom_fields(
  69              "{$groupingsalias}.id",
  70              $this->get_entity_name(),
  71              'core_group',
  72              'grouping',
  73          ))
  74              ->add_joins($this->get_joins());
  75  
  76          $columns = array_merge($this->get_all_columns(), $customfields->get_columns());
  77          foreach ($columns as $column) {
  78              $this->add_column($column);
  79          }
  80  
  81          // All the filters defined by the entity can also be used as conditions.
  82          $filters = array_merge($this->get_all_filters(), $customfields->get_filters());
  83          foreach ($filters as $filter) {
  84              $this
  85                  ->add_filter($filter)
  86                  ->add_condition($filter);
  87          }
  88  
  89          return $this;
  90      }
  91  
  92      /**
  93       * Returns list of all available columns
  94       *
  95       * @return column[]
  96       */
  97      protected function get_all_columns(): array {
  98          global $DB;
  99  
 100          $contextalias = $this->get_table_alias('context');
 101          $groupingsalias = $this->get_table_alias('groupings');
 102  
 103          // Name column.
 104          $columns[] = (new column(
 105              'name',
 106              new lang_string('name'),
 107              $this->get_entity_name()
 108          ))
 109              ->add_joins($this->get_joins())
 110              ->set_type(column::TYPE_TEXT)
 111              ->add_fields("{$groupingsalias}.name, {$groupingsalias}.courseid")
 112              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 113              ->set_is_sortable(true)
 114              ->set_callback(static function($name, stdClass $grouping): string {
 115                  if ($name === null) {
 116                      return '';
 117                  }
 118  
 119                  context_helper::preload_from_record($grouping);
 120                  $context = context_course::instance($grouping->courseid);
 121  
 122                  return format_string($grouping->name, true, ['context' => $context]);
 123              });
 124  
 125          // ID number column.
 126          $columns[] = (new column(
 127              'idnumber',
 128              new lang_string('idnumber'),
 129              $this->get_entity_name()
 130          ))
 131              ->add_joins($this->get_joins())
 132              ->set_type(column::TYPE_TEXT)
 133              ->add_fields("{$groupingsalias}.idnumber")
 134              ->set_is_sortable(true);
 135  
 136          // Description column.
 137          $descriptionfieldsql = "{$groupingsalias}.description";
 138          if ($DB->get_dbfamily() === 'oracle') {
 139              $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
 140          }
 141          $columns[] = (new column(
 142              'description',
 143              new lang_string('description'),
 144              $this->get_entity_name()
 145          ))
 146              ->add_joins($this->get_joins())
 147              ->set_type(column::TYPE_LONGTEXT)
 148              ->add_field($descriptionfieldsql, 'description')
 149              ->add_fields("{$groupingsalias}.descriptionformat, {$groupingsalias}.id, {$groupingsalias}.courseid")
 150              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 151              ->set_is_sortable(false)
 152              ->set_callback(static function(?string $description, stdClass $grouping): string {
 153                  global $CFG;
 154  
 155                  if ($description === null) {
 156                      return '';
 157                  }
 158  
 159                  require_once("{$CFG->libdir}/filelib.php");
 160  
 161                  context_helper::preload_from_record($grouping);
 162                  $context = context_course::instance($grouping->courseid);
 163  
 164                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'grouping',
 165                      'description', $grouping->id);
 166  
 167                  return format_text($description, $grouping->descriptionformat, ['context' => $context]);
 168              });
 169  
 170          // Time created column.
 171          $columns[] = (new column(
 172              'timecreated',
 173              new lang_string('timecreated', 'core_reportbuilder'),
 174              $this->get_entity_name()
 175          ))
 176              ->add_joins($this->get_joins())
 177              ->set_type(column::TYPE_TIMESTAMP)
 178              ->add_fields("{$groupingsalias}.timecreated")
 179              ->set_is_sortable(true)
 180              ->set_callback([format::class, 'userdate']);
 181  
 182          // Time modified column.
 183          $columns[] = (new column(
 184              'timemodified',
 185              new lang_string('timemodified', 'core_reportbuilder'),
 186              $this->get_entity_name()
 187          ))
 188              ->add_joins($this->get_joins())
 189              ->set_type(column::TYPE_TIMESTAMP)
 190              ->add_fields("{$groupingsalias}.timemodified")
 191              ->set_is_sortable(true)
 192              ->set_callback([format::class, 'userdate']);
 193  
 194          return $columns;
 195      }
 196  
 197      /**
 198       * Return list of all available filters
 199       *
 200       * @return filter[]
 201       */
 202      protected function get_all_filters(): array {
 203          $groupingsalias = $this->get_table_alias('groupings');
 204  
 205          // Name filter.
 206          $filters[] = (new filter(
 207              text::class,
 208              'name',
 209              new lang_string('name'),
 210              $this->get_entity_name(),
 211              "{$groupingsalias}.name"
 212          ))
 213              ->add_joins($this->get_joins());
 214  
 215          // ID number filter.
 216          $filters[] = (new filter(
 217              text::class,
 218              'idnumber',
 219              new lang_string('idnumber'),
 220              $this->get_entity_name(),
 221              "{$groupingsalias}.idnumber"
 222          ))
 223              ->add_joins($this->get_joins());
 224  
 225          // Time created filter.
 226          $filters[] = (new filter(
 227              date::class,
 228              'timecreated',
 229              new lang_string('timecreated', 'core_reportbuilder'),
 230              $this->get_entity_name(),
 231              "{$groupingsalias}.timecreated"
 232          ))
 233              ->add_joins($this->get_joins());
 234  
 235          return $filters;
 236      }
 237  }