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  namespace core_enrol\reportbuilder\local\entities;
  18  
  19  use enrol_plugin;
  20  use lang_string;
  21  use stdClass;
  22  use core_reportbuilder\local\entities\base;
  23  use core_reportbuilder\local\filters\{boolean_select, date, duration, select, text};
  24  use core_reportbuilder\local\helpers\format;
  25  use core_reportbuilder\local\report\{column, filter};
  26  
  27  /**
  28   * Enrolment method entity
  29   *
  30   * @package     core_enrol
  31   * @copyright   2023 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class enrol extends base {
  35  
  36      /**
  37       * Database tables that this entity uses and their default aliases
  38       *
  39       * @return array
  40       */
  41      protected function get_default_table_aliases(): array {
  42          return [
  43              'enrol' => 'e',
  44          ];
  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('enrolmentmethod', 'core_enrol');
  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          global $DB;
  85  
  86          $enrolalias = $this->get_table_alias('enrol');
  87  
  88          // Plugin column.
  89          $columns[] = (new column(
  90              'plugin',
  91              new lang_string('plugin'),
  92              $this->get_entity_name()
  93          ))
  94              ->add_joins($this->get_joins())
  95              ->set_type(column::TYPE_TEXT)
  96              ->add_fields("{$enrolalias}.enrol")
  97              ->set_is_sortable(true)
  98              ->set_callback(static function(?string $enrol): string {
  99                  if ($enrol === null || !$plugin = enrol_get_plugin($enrol)) {
 100                      return '';
 101                  }
 102  
 103                  return $plugin->get_instance_name(null);
 104              });
 105  
 106          // Name column.
 107          $columns[] = (new column(
 108              'name',
 109              new lang_string('name'),
 110              $this->get_entity_name()
 111          ))
 112              ->add_joins($this->get_joins())
 113              ->set_type(column::TYPE_TEXT)
 114              ->add_fields("{$enrolalias}.enrol, {$enrolalias}.name, {$enrolalias}.courseid, " .
 115                  "{$enrolalias}.roleid, {$enrolalias}.customint1")
 116              ->set_is_sortable(true)
 117              ->set_callback(static function(?string $enrol, stdClass $instance): string {
 118                  if ($enrol === null || !$plugin = enrol_get_plugin($enrol)) {
 119                      return '';
 120                  }
 121  
 122                  return $plugin->get_instance_name($instance);
 123              });
 124  
 125          // Enabled column.
 126          $columns[] = (new column(
 127              'enabled',
 128              new lang_string('enabled', 'core_admin'),
 129              $this->get_entity_name()
 130          ))
 131              ->add_joins($this->get_joins())
 132              ->set_type(column::TYPE_BOOLEAN)
 133              // For accurate aggregation, we need to return boolean enabled = true by xor'ing the field value.
 134              ->add_field($DB->sql_bitxor("{$enrolalias}.status", 1), 'status')
 135              ->set_is_sortable(true)
 136              ->set_callback([format::class, 'boolean_as_text']);
 137  
 138          // Period column.
 139          $columns[] = (new column(
 140              'period',
 141              new lang_string('enrolperiod', 'core_enrol'),
 142              $this->get_entity_name()
 143          ))
 144              ->add_joins($this->get_joins())
 145              ->set_type(column::TYPE_TIMESTAMP)
 146              ->add_fields("{$enrolalias}.enrolperiod")
 147              ->set_is_sortable(true)
 148              ->set_callback(static function(?int $enrolperiod): string {
 149                  if (!$enrolperiod) {
 150                      return '';
 151                  }
 152  
 153                  return format_time($enrolperiod);
 154              });
 155  
 156          // Start date column.
 157          $columns[] = (new column(
 158              'startdate',
 159              new lang_string('enroltimestart', 'core_enrol'),
 160              $this->get_entity_name()
 161          ))
 162              ->add_joins($this->get_joins())
 163              ->set_type(column::TYPE_TIMESTAMP)
 164              ->add_fields("{$enrolalias}.enrolstartdate")
 165              ->set_is_sortable(true)
 166              ->set_callback([format::class, 'userdate']);
 167  
 168          // End date column.
 169          $columns[] = (new column(
 170              'enddate',
 171              new lang_string('enroltimeend', 'core_enrol'),
 172              $this->get_entity_name()
 173          ))
 174              ->add_joins($this->get_joins())
 175              ->set_type(column::TYPE_TIMESTAMP)
 176              ->add_fields("{$enrolalias}.enrolenddate")
 177              ->set_is_sortable(true)
 178              ->set_callback([format::class, 'userdate']);
 179  
 180          return $columns;
 181      }
 182  
 183      /**
 184       * Return list of all available filters
 185       *
 186       * @return filter[]
 187       */
 188      protected function get_all_filters(): array {
 189          global $DB;
 190  
 191          $enrolalias = $this->get_table_alias('enrol');
 192  
 193          // Plugin filter.
 194          $filters[] = (new filter(
 195              select::class,
 196              'plugin',
 197              new lang_string('plugin'),
 198              $this->get_entity_name(),
 199              "{$enrolalias}.enrol"
 200          ))
 201              ->add_joins($this->get_joins())
 202              ->set_options_callback(static function(): array {
 203                  return array_map(static function(enrol_plugin $plugin): string {
 204                      return $plugin->get_instance_name(null);
 205                  }, enrol_get_plugins(true));
 206              });
 207  
 208          // Custom name filter.
 209          $filters[] = (new filter(
 210              text::class,
 211              'customname',
 212              new lang_string('custominstancename', 'core_enrol'),
 213              $this->get_entity_name(),
 214              "{$enrolalias}.name"
 215          ))
 216              ->add_joins($this->get_joins());
 217  
 218          // Enabled filter.
 219          $filters[] = (new filter(
 220              boolean_select::class,
 221              'enabled',
 222              new lang_string('enabled', 'core_admin'),
 223              $this->get_entity_name(),
 224              $DB->sql_bitxor("{$enrolalias}.status", 1)
 225          ))
 226              ->add_joins($this->get_joins());
 227  
 228          // Period filter.
 229          $filters[] = (new filter(
 230              duration::class,
 231              'period',
 232              new lang_string('enrolperiod', 'core_enrol'),
 233              $this->get_entity_name(),
 234              "{$enrolalias}.enrolperiod"
 235          ))
 236              ->add_joins($this->get_joins());
 237  
 238          // Start date filter.
 239          $filters[] = (new filter(
 240              date::class,
 241              'startdate',
 242              new lang_string('enroltimestart', 'core_enrol'),
 243              $this->get_entity_name(),
 244              "{$enrolalias}.enrolstartdate"
 245          ))
 246              ->add_joins($this->get_joins());
 247  
 248          // End date filter.
 249          $filters[] = (new filter(
 250              date::class,
 251              'enddate',
 252              new lang_string('enroltimeend', 'core_enrol'),
 253              $this->get_entity_name(),
 254              "{$enrolalias}.enrolenddate"
 255          ))
 256              ->add_joins($this->get_joins());
 257  
 258          return $filters;
 259      }
 260  }