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.

Differences Between: [Versions 401 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\format;
  28  use core_reportbuilder\local\report\{column, filter};
  29  
  30  /**
  31   * Group member 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          $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          $contextalias = $this->get_table_alias('context');
  91          $groupingsalias = $this->get_table_alias('groupings');
  92  
  93          // Name column.
  94          $columns[] = (new column(
  95              'name',
  96              new lang_string('name'),
  97              $this->get_entity_name()
  98          ))
  99              ->add_joins($this->get_joins())
 100              ->set_type(column::TYPE_TEXT)
 101              ->add_fields("{$groupingsalias}.name, {$groupingsalias}.courseid")
 102              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 103              ->set_is_sortable(true)
 104              ->set_callback(static function($name, stdClass $grouping): string {
 105                  if ($name === null) {
 106                      return '';
 107                  }
 108  
 109                  context_helper::preload_from_record($grouping);
 110                  $context = context_course::instance($grouping->courseid);
 111  
 112                  return format_string($grouping->name, true, ['context' => $context]);
 113              });
 114  
 115          // ID number column.
 116          $columns[] = (new column(
 117              'idnumber',
 118              new lang_string('idnumber'),
 119              $this->get_entity_name()
 120          ))
 121              ->add_joins($this->get_joins())
 122              ->set_type(column::TYPE_TEXT)
 123              ->add_fields("{$groupingsalias}.idnumber")
 124              ->set_is_sortable(true);
 125  
 126          // Description column.
 127          $descriptionfieldsql = "{$groupingsalias}.description";
 128          if ($DB->get_dbfamily() === 'oracle') {
 129              $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
 130          }
 131          $columns[] = (new column(
 132              'description',
 133              new lang_string('description'),
 134              $this->get_entity_name()
 135          ))
 136              ->add_joins($this->get_joins())
 137              ->set_type(column::TYPE_LONGTEXT)
 138              ->add_field($descriptionfieldsql, 'description')
 139              ->add_fields("{$groupingsalias}.descriptionformat, {$groupingsalias}.id, {$groupingsalias}.courseid")
 140              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 141              ->set_is_sortable(false)
 142              ->set_callback(static function(?string $description, stdClass $grouping): string {
 143                  global $CFG;
 144  
 145                  if ($description === null) {
 146                      return '';
 147                  }
 148  
 149                  require_once("{$CFG->libdir}/filelib.php");
 150  
 151                  context_helper::preload_from_record($grouping);
 152                  $context = context_course::instance($grouping->courseid);
 153  
 154                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'grouping',
 155                      'description', $grouping->id);
 156  
 157                  return format_text($description, $grouping->descriptionformat, ['context' => $context]);
 158              });
 159  
 160          // Time created column.
 161          $columns[] = (new column(
 162              'timecreated',
 163              new lang_string('timecreated', 'core_reportbuilder'),
 164              $this->get_entity_name()
 165          ))
 166              ->add_joins($this->get_joins())
 167              ->set_type(column::TYPE_TIMESTAMP)
 168              ->add_fields("{$groupingsalias}.timecreated")
 169              ->set_is_sortable(true)
 170              ->set_callback([format::class, 'userdate']);
 171  
 172          // Time modified column.
 173          $columns[] = (new column(
 174              'timemodified',
 175              new lang_string('timemodified', 'core_reportbuilder'),
 176              $this->get_entity_name()
 177          ))
 178              ->add_joins($this->get_joins())
 179              ->set_type(column::TYPE_TIMESTAMP)
 180              ->add_fields("{$groupingsalias}.timemodified")
 181              ->set_is_sortable(true)
 182              ->set_callback([format::class, 'userdate']);
 183  
 184          return $columns;
 185      }
 186  
 187      /**
 188       * Return list of all available filters
 189       *
 190       * @return filter[]
 191       */
 192      protected function get_all_filters(): array {
 193          $groupingsalias = $this->get_table_alias('groupings');
 194  
 195          // Name filter.
 196          $filters[] = (new filter(
 197              text::class,
 198              'name',
 199              new lang_string('name'),
 200              $this->get_entity_name(),
 201              "{$groupingsalias}.name"
 202          ))
 203              ->add_joins($this->get_joins());
 204  
 205          // ID number filter.
 206          $filters[] = (new filter(
 207              text::class,
 208              'idnumber',
 209              new lang_string('idnumber'),
 210              $this->get_entity_name(),
 211              "{$groupingsalias}.idnumber"
 212          ))
 213              ->add_joins($this->get_joins());
 214  
 215          // Time created filter.
 216          $filters[] = (new filter(
 217              date::class,
 218              'timecreated',
 219              new lang_string('timecreated', 'core_reportbuilder'),
 220              $this->get_entity_name(),
 221              "{$groupingsalias}.timecreated"
 222          ))
 223              ->add_joins($this->get_joins());
 224  
 225          return $filters;
 226      }
 227  }