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 402] [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 html_writer;
  24  use lang_string;
  25  use moodle_url;
  26  use stdClass;
  27  use core_reportbuilder\local\entities\base;
  28  use core_reportbuilder\local\filters\{date, text};
  29  use core_reportbuilder\local\helpers\format;
  30  use core_reportbuilder\local\report\{column, filter};
  31  
  32  /**
  33   * Group entity
  34   *
  35   * @package     core_group
  36   * @copyright   2022 Paul Holden <paulh@moodle.com>
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class group extends base {
  40  
  41      /**
  42       * Database tables that this entity uses and their default aliases
  43       *
  44       * @return array
  45       */
  46      protected function get_default_table_aliases(): array {
  47          return [
  48              'context' => 'gctx',
  49              'groups' => 'g',
  50          ];
  51      }
  52  
  53      /**
  54       * The default title for this entity
  55       *
  56       * @return lang_string
  57       */
  58      protected function get_default_entity_title(): lang_string {
  59          return new lang_string('group', 'core_group');
  60      }
  61  
  62      /**
  63       * Initialise the entity
  64       *
  65       * @return base
  66       */
  67      public function initialise(): base {
  68          $columns = $this->get_all_columns();
  69          foreach ($columns as $column) {
  70              $this->add_column($column);
  71          }
  72  
  73          // All the filters defined by the entity can also be used as conditions.
  74          $filters = $this->get_all_filters();
  75          foreach ($filters as $filter) {
  76              $this
  77                  ->add_filter($filter)
  78                  ->add_condition($filter);
  79          }
  80  
  81          return $this;
  82      }
  83  
  84      /**
  85       * Returns list of all available columns
  86       *
  87       * @return column[]
  88       */
  89      protected function get_all_columns(): array {
  90          global $DB;
  91  
  92          $contextalias = $this->get_table_alias('context');
  93          $groupsalias = $this->get_table_alias('groups');
  94  
  95          // Name column.
  96          $columns[] = (new column(
  97              'name',
  98              new lang_string('name'),
  99              $this->get_entity_name()
 100          ))
 101              ->add_joins($this->get_joins())
 102              ->set_type(column::TYPE_TEXT)
 103              ->add_fields("{$groupsalias}.name, {$groupsalias}.courseid")
 104              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 105              ->set_is_sortable(true)
 106              ->set_callback(static function($name, stdClass $group): string {
 107                  if ($name === null) {
 108                      return '';
 109                  }
 110  
 111                  context_helper::preload_from_record($group);
 112                  $context = context_course::instance($group->courseid);
 113  
 114                  return format_string($group->name, true, ['context' => $context]);
 115              });
 116  
 117          // ID number column.
 118          $columns[] = (new column(
 119              'idnumber',
 120              new lang_string('idnumber'),
 121              $this->get_entity_name()
 122          ))
 123              ->add_joins($this->get_joins())
 124              ->set_type(column::TYPE_TEXT)
 125              ->add_fields("{$groupsalias}.idnumber")
 126              ->set_is_sortable(true);
 127  
 128          // Description column.
 129          $descriptionfieldsql = "{$groupsalias}.description";
 130          if ($DB->get_dbfamily() === 'oracle') {
 131              $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
 132          }
 133          $columns[] = (new column(
 134              'description',
 135              new lang_string('description'),
 136              $this->get_entity_name()
 137          ))
 138              ->add_joins($this->get_joins())
 139              ->set_type(column::TYPE_LONGTEXT)
 140              ->add_field($descriptionfieldsql, 'description')
 141              ->add_fields("{$groupsalias}.descriptionformat, {$groupsalias}.id, {$groupsalias}.courseid")
 142              ->add_fields(context_helper::get_preload_record_columns_sql($contextalias))
 143              ->set_is_sortable(false)
 144              ->set_callback(static function(?string $description, stdClass $group): string {
 145                  global $CFG;
 146  
 147                  if ($description === null) {
 148                      return '';
 149                  }
 150  
 151                  require_once("{$CFG->libdir}/filelib.php");
 152  
 153                  context_helper::preload_from_record($group);
 154                  $context = context_course::instance($group->courseid);
 155  
 156                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $context->id, 'group',
 157                      'description', $group->id);
 158  
 159                  return format_text($description, $group->descriptionformat, ['context' => $context]);
 160              });
 161  
 162          // Enrolment key column.
 163          $columns[] = (new column(
 164              'enrolmentkey',
 165              new lang_string('enrolmentkey', 'core_group'),
 166              $this->get_entity_name()
 167          ))
 168              ->add_joins($this->get_joins())
 169              ->set_type(column::TYPE_TEXT)
 170              ->add_fields("{$groupsalias}.enrolmentkey")
 171              ->set_is_sortable(true);
 172  
 173          // Picture column.
 174          $columns[] = (new column(
 175              'picture',
 176              new lang_string('picture'),
 177              $this->get_entity_name()
 178          ))
 179              ->add_joins($this->get_joins())
 180              ->set_type(column::TYPE_INTEGER)
 181              ->add_fields("{$groupsalias}.picture, {$groupsalias}.id, {$contextalias}.id AS contextid")
 182              ->set_is_sortable(false)
 183              // It doesn't make sense to offer integer aggregation methods for this column.
 184              ->set_disabled_aggregation(['avg', 'max', 'min', 'sum'])
 185              ->set_callback(static function ($picture, stdClass $group): string {
 186                  if (empty($group->picture)) {
 187                      return '';
 188                  }
 189  
 190                  $pictureurl = moodle_url::make_pluginfile_url($group->contextid, 'group', 'icon', $group->id, '/', 'f2');
 191                  $pictureurl->param('rev', $group->picture);
 192  
 193                  return html_writer::img($pictureurl, '');
 194              });
 195  
 196          // Time created column.
 197          $columns[] = (new column(
 198              'timecreated',
 199              new lang_string('timecreated', 'core_reportbuilder'),
 200              $this->get_entity_name()
 201          ))
 202              ->add_joins($this->get_joins())
 203              ->set_type(column::TYPE_TIMESTAMP)
 204              ->add_fields("{$groupsalias}.timecreated")
 205              ->set_is_sortable(true)
 206              ->set_callback([format::class, 'userdate']);
 207  
 208          // Time modified column.
 209          $columns[] = (new column(
 210              'timemodified',
 211              new lang_string('timemodified', 'core_reportbuilder'),
 212              $this->get_entity_name()
 213          ))
 214              ->add_joins($this->get_joins())
 215              ->set_type(column::TYPE_TIMESTAMP)
 216              ->add_fields("{$groupsalias}.timemodified")
 217              ->set_is_sortable(true)
 218              ->set_callback([format::class, 'userdate']);
 219  
 220          return $columns;
 221      }
 222  
 223      /**
 224       * Return list of all available filters
 225       *
 226       * @return filter[]
 227       */
 228      protected function get_all_filters(): array {
 229          $groupsalias = $this->get_table_alias('groups');
 230  
 231          // Name filter.
 232          $filters[] = (new filter(
 233              text::class,
 234              'name',
 235              new lang_string('name'),
 236              $this->get_entity_name(),
 237              "{$groupsalias}.name"
 238          ))
 239              ->add_joins($this->get_joins());
 240  
 241          // ID number filter.
 242          $filters[] = (new filter(
 243              text::class,
 244              'idnumber',
 245              new lang_string('idnumber'),
 246              $this->get_entity_name(),
 247              "{$groupsalias}.idnumber"
 248          ))
 249              ->add_joins($this->get_joins());
 250  
 251          // Time created filter.
 252          $filters[] = (new filter(
 253              date::class,
 254              'timecreated',
 255              new lang_string('timecreated', 'core_reportbuilder'),
 256              $this->get_entity_name(),
 257              "{$groupsalias}.timecreated"
 258          ))
 259              ->add_joins($this->get_joins());
 260  
 261          return $filters;
 262      }
 263  }