Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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_cohort\local\entities;
  20  
  21  use context;
  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;
  27  use core_reportbuilder\local\filters\select;
  28  use core_reportbuilder\local\filters\text;
  29  use core_reportbuilder\local\helpers\format;
  30  use core_reportbuilder\local\report\column;
  31  use core_reportbuilder\local\report\filter;
  32  
  33  /**
  34   * Cohort entity
  35   *
  36   * @package     core_cohort
  37   * @copyright   2021 Paul Holden <paulh@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class cohort extends base {
  41  
  42      /**
  43       * Database tables that this entity uses and their default aliases
  44       *
  45       * @return array
  46       */
  47      protected function get_default_table_aliases(): array {
  48          return ['cohort' => 'c'];
  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('cohort', 'core_cohort');
  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          $tablealias = $this->get_table_alias('cohort');
  91  
  92          // Category/context column.
  93          $columns[] = (new column(
  94              'context',
  95              new lang_string('category'),
  96              $this->get_entity_name()
  97          ))
  98              ->add_joins($this->get_joins())
  99              ->set_type(column::TYPE_TEXT)
 100              ->add_fields("{$tablealias}.contextid")
 101              ->set_is_sortable(true)
 102              ->add_callback(static function($contextid): string {
 103                  return context::instance_by_id((int) $contextid)->get_context_name(false);
 104              });
 105  
 106          // Name column.
 107          $columns[] = (new column(
 108              'name',
 109              new lang_string('name', 'core_cohort'),
 110              $this->get_entity_name()
 111          ))
 112              ->add_joins($this->get_joins())
 113              ->set_type(column::TYPE_TEXT)
 114              ->add_fields("{$tablealias}.name")
 115              ->set_is_sortable(true);
 116  
 117          // ID number column.
 118          $columns[] = (new column(
 119              'idnumber',
 120              new lang_string('idnumber', 'core_cohort'),
 121              $this->get_entity_name()
 122          ))
 123              ->add_joins($this->get_joins())
 124              ->set_type(column::TYPE_TEXT)
 125              ->add_fields("{$tablealias}.idnumber")
 126              ->set_is_sortable(true);
 127  
 128          // Description column.
 129          $descriptionfieldsql = "{$tablealias}.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("{$tablealias}.descriptionformat, {$tablealias}.id, {$tablealias}.contextid")
 142              ->add_callback(static function(?string $description, stdClass $cohort): string {
 143                  global $CFG;
 144                  require_once("{$CFG->libdir}/filelib.php");
 145  
 146                  if ($description === null) {
 147                      return '';
 148                  }
 149  
 150                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $cohort->contextid, 'cohort',
 151                      'description', $cohort->id);
 152  
 153                  return format_text($description, $cohort->descriptionformat, ['context' => $cohort->contextid]);
 154              })
 155              ->set_is_sortable(false);
 156  
 157          // Visible column.
 158          $columns[] = (new column(
 159              'visible',
 160              new lang_string('visible', 'core_cohort'),
 161              $this->get_entity_name()
 162          ))
 163              ->add_joins($this->get_joins())
 164              ->set_type(column::TYPE_BOOLEAN)
 165              ->add_fields("{$tablealias}.visible")
 166              ->set_is_sortable(true)
 167              ->set_callback([format::class, 'boolean_as_text']);
 168  
 169          // Time created column.
 170          $columns[] = (new column(
 171              'timecreated',
 172              new lang_string('timecreated', 'core_reportbuilder'),
 173              $this->get_entity_name()
 174          ))
 175              ->add_joins($this->get_joins())
 176              ->set_type(column::TYPE_TIMESTAMP)
 177              ->add_fields("{$tablealias}.timecreated")
 178              ->set_is_sortable(true)
 179              ->set_callback([format::class, 'userdate']);
 180  
 181          // Time modified column.
 182          $columns[] = (new column(
 183              'timemodified',
 184              new lang_string('timemodified', 'core_reportbuilder'),
 185              $this->get_entity_name()
 186          ))
 187              ->add_joins($this->get_joins())
 188              ->set_type(column::TYPE_TIMESTAMP)
 189              ->add_fields("{$tablealias}.timemodified")
 190              ->set_is_sortable(true)
 191              ->set_callback([format::class, 'userdate']);
 192  
 193          // Component column.
 194          $columns[] = (new column(
 195              'component',
 196              new lang_string('component', 'core_cohort'),
 197              $this->get_entity_name()
 198          ))
 199              ->add_joins($this->get_joins())
 200              ->set_type(column::TYPE_TEXT)
 201              ->add_fields("{$tablealias}.component")
 202              ->set_is_sortable(true)
 203              ->add_callback(static function(string $component): string {
 204                  return empty($component)
 205                      ? get_string('nocomponent', 'cohort')
 206                      : get_string('pluginname', $component);
 207              });
 208  
 209          // Theme column.
 210          $columns[] = (new column(
 211              'theme',
 212              new lang_string('theme'),
 213              $this->get_entity_name()
 214          ))
 215              ->add_joins($this->get_joins())
 216              ->set_type(column::TYPE_TEXT)
 217              ->add_fields("{$tablealias}.theme")
 218              ->set_is_sortable(true);
 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          $tablealias = $this->get_table_alias('cohort');
 230  
 231          // Context filter.
 232          $filters[] = (new filter(
 233              select::class,
 234              'context',
 235              new lang_string('category'),
 236              $this->get_entity_name(),
 237              "{$tablealias}.contextid"
 238          ))
 239              ->add_joins($this->get_joins())
 240              ->set_options_callback(static function(): array {
 241                  global $DB;
 242  
 243                  // Load all contexts in which there are cohorts.
 244                  $ctxfields = context_helper::get_preload_record_columns_sql('ctx');
 245                  $contexts = $DB->get_records_sql("
 246                      SELECT DISTINCT {$ctxfields}, c.contextid
 247                        FROM {context} ctx
 248                        JOIN {cohort} c ON c.contextid = ctx.id");
 249  
 250                  // Transform context record into it's name (used as the filter options).
 251                  return array_map(static function(stdClass $contextrecord): string {
 252                      context_helper::preload_from_record($contextrecord);
 253  
 254                      return context::instance_by_id($contextrecord->contextid)
 255                          ->get_context_name(false);
 256                  }, $contexts);
 257              });
 258  
 259          // Name filter.
 260          $filters[] = (new filter(
 261              text::class,
 262              'name',
 263              new lang_string('name', 'core_cohort'),
 264              $this->get_entity_name(),
 265              "{$tablealias}.name"
 266          ))
 267              ->add_joins($this->get_joins());
 268  
 269          // ID number filter.
 270          $filters[] = (new filter(
 271              text::class,
 272              'idnumber',
 273              new lang_string('idnumber', 'core_cohort'),
 274              $this->get_entity_name(),
 275              "{$tablealias}.idnumber"
 276          ))
 277              ->add_joins($this->get_joins());
 278  
 279          // Time created filter.
 280          $filters[] = (new filter(
 281              date::class,
 282              'timecreated',
 283              new lang_string('timecreated', 'core_reportbuilder'),
 284              $this->get_entity_name(),
 285              "{$tablealias}.timecreated"
 286          ))
 287              ->add_joins($this->get_joins());
 288  
 289          return $filters;
 290      }
 291  }