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_course\local\entities;
  20  
  21  use lang_string;
  22  use stdClass;
  23  use core_course_category;
  24  use core_reportbuilder\local\entities\base;
  25  use core_reportbuilder\local\filters\select;
  26  use core_reportbuilder\local\filters\text;
  27  use core_reportbuilder\local\report\column;
  28  use core_reportbuilder\local\report\filter;
  29  
  30  /**
  31   * Course category entity
  32   *
  33   * @package     core_course
  34   * @copyright   2021 Paul Holden <paulh@moodle.com>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class course_category 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' => 'ccctx',
  47              'course_categories' => 'cc',
  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('coursecategory');
  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('course_categories');
  91          $tablealiascontext = $this->get_table_alias('context');
  92  
  93          // Name column.
  94          $columns[] = (new column(
  95              'name',
  96              new lang_string('categoryname'),
  97              $this->get_entity_name()
  98          ))
  99              ->add_joins($this->get_joins())
 100              ->set_type(column::TYPE_TEXT)
 101              ->add_fields("{$tablealias}.name, {$tablealias}.id")
 102              ->add_callback(static function(?string $name, stdClass $category): string {
 103                  return empty($category->id) ? '' :
 104                      core_course_category::get($category->id, MUST_EXIST, true)->get_formatted_name();
 105              })
 106              ->set_is_sortable(true);
 107  
 108          // Path column.
 109          $columns[] = (new column(
 110              'path',
 111              new lang_string('categorypath'),
 112              $this->get_entity_name()
 113          ))
 114              ->add_joins($this->get_joins())
 115              ->set_type(column::TYPE_TEXT)
 116              ->add_fields("{$tablealias}.name, {$tablealias}.id")
 117              ->add_callback(static function(?string $name, stdClass $category): string {
 118                  return empty($category->id) ? '' :
 119                      core_course_category::get($category->id, MUST_EXIST, true)->get_nested_name(false);
 120              })
 121              ->set_disabled_aggregation(['groupconcat', 'groupconcatdistinct'])
 122              ->set_is_sortable(true);
 123  
 124          // ID number column.
 125          $columns[] = (new column(
 126              'idnumber',
 127              new lang_string('idnumbercoursecategory'),
 128              $this->get_entity_name()
 129          ))
 130              ->add_joins($this->get_joins())
 131              ->set_type(column::TYPE_TEXT)
 132              ->add_fields("{$tablealias}.idnumber")
 133              ->set_is_sortable(true);
 134  
 135          // Description column (note we need to join/select from the context table in order to format the column).
 136          $descriptionfieldsql = "{$tablealias}.description";
 137          if ($DB->get_dbfamily() === 'oracle') {
 138              $descriptionfieldsql = $DB->sql_order_by_text($descriptionfieldsql, 1024);
 139          }
 140          $columns[] = (new column(
 141              'description',
 142              new lang_string('description'),
 143              $this->get_entity_name()
 144          ))
 145              ->add_joins($this->get_joins())
 146              ->add_join("
 147                  JOIN {context} {$tablealiascontext}
 148                    ON {$tablealiascontext}.instanceid = {$tablealias}.id
 149                   AND {$tablealiascontext}.contextlevel = " . CONTEXT_COURSECAT)
 150              ->set_type(column::TYPE_LONGTEXT)
 151              ->add_field($descriptionfieldsql, 'description')
 152              ->add_fields("{$tablealias}.descriptionformat, {$tablealiascontext}.id AS contextid")
 153              ->add_callback(static function(?string $description, stdClass $category): string {
 154                  global $CFG;
 155                  require_once("{$CFG->libdir}/filelib.php");
 156  
 157                  if ($description === null) {
 158                      return '';
 159                  }
 160  
 161                  $description = file_rewrite_pluginfile_urls($description, 'pluginfile.php', $category->contextid, 'coursecat',
 162                      'description', null);
 163  
 164                  return format_text($description, $category->descriptionformat, ['context' => $category->contextid]);
 165              })
 166              ->set_is_sortable(false);
 167  
 168          return $columns;
 169      }
 170  
 171      /**
 172       * Return list of all available filters
 173       *
 174       * @return filter[]
 175       */
 176      protected function get_all_filters(): array {
 177          $tablealias = $this->get_table_alias('course_categories');
 178  
 179          // Name filter.
 180          $filters[] = (new filter(
 181              select::class,
 182              'name',
 183              new lang_string('categoryname'),
 184              $this->get_entity_name(),
 185              "{$tablealias}.id"
 186          ))
 187              ->add_joins($this->get_joins())
 188              ->set_options_callback(static function(): array {
 189                  return core_course_category::make_categories_list('moodle/category:viewcourselist');
 190              });
 191  
 192          // ID number filter.
 193          $filters[] = (new filter(
 194              text::class,
 195              'idnumber',
 196              new lang_string('idnumbercoursecategory'),
 197              $this->get_entity_name(),
 198              "{$tablealias}.idnumber"
 199          ))
 200              ->add_joins($this->get_joins());
 201  
 202          return $filters;
 203      }
 204  }