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  declare(strict_types=1);
  18  
  19  namespace core_course\reportbuilder\datasource;
  20  
  21  use core_cohort\reportbuilder\local\entities\cohort;
  22  use core_course\reportbuilder\local\entities\course_category;
  23  use core_reportbuilder\datasource;
  24  use core_reportbuilder\local\entities\{course, user};
  25  use core_role\reportbuilder\local\entities\role;
  26  
  27  /**
  28   * Course categories datasource
  29   *
  30   * @package     core_course
  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 categories extends datasource {
  35  
  36      /**
  37       * Return user friendly name of the datasource
  38       *
  39       * @return string
  40       */
  41      public static function get_name(): string {
  42          return get_string('coursecategories');
  43      }
  44  
  45      /**
  46       * Initialise report
  47       */
  48      protected function initialise(): void {
  49          $categoryentity = new course_category();
  50  
  51          $categoryalias = $categoryentity->get_table_alias('course_categories');
  52          $contextalias = $categoryentity->get_table_alias('context');
  53  
  54          $this->set_main_table('course_categories', $categoryalias);
  55          $this->add_entity($categoryentity);
  56  
  57          // Join course entity.
  58          $courseentity = new course();
  59          $coursealias = $courseentity->get_table_alias('course');
  60          $this->add_entity($courseentity
  61              ->add_join("LEFT JOIN {course} {$coursealias} ON {$coursealias}.category = {$categoryalias}.id"));
  62  
  63          // Join cohort entity (amend alias to avoid clash with course entity, indicate context table join alias).
  64          $cohortentity = (new cohort())
  65              ->set_table_alias('cohort', 'ch')
  66              ->set_table_join_alias('context', $contextalias);
  67          $this->add_entity($cohortentity
  68              ->add_join($categoryentity->get_context_join())
  69              ->add_join("LEFT JOIN {cohort} ch ON ch.contextid = {$contextalias}.id"));
  70  
  71          // Join role entity.
  72          $roleentity = (new role())
  73              ->set_table_alias('context', $contextalias);
  74          $role = $roleentity->get_table_alias('role');
  75          $this->add_entity($roleentity
  76              ->add_join($categoryentity->get_context_join())
  77              ->add_join("LEFT JOIN {role_assignments} ras ON ras.contextid = {$contextalias}.id")
  78              ->add_join("LEFT JOIN {role} {$role} ON {$role}.id = ras.roleid"));
  79  
  80          // Join user entity.
  81          $userentity = new user();
  82          $user = $userentity->get_table_alias('user');
  83          $this->add_entity($userentity
  84              ->add_joins($roleentity->get_joins())
  85              ->add_join("LEFT JOIN {user} {$user} ON {$user}.id = ras.userid"));
  86  
  87          // Add all elements from entities to be available in custom reports.
  88          $this->add_all_from_entities();
  89      }
  90  
  91      /**
  92       * Return the columns that will be added to the report as part of default setup
  93       *
  94       * @return string[]
  95       */
  96      public function get_default_columns(): array {
  97          return [
  98              'course_category:name',
  99              'course_category:idnumber',
 100              'course_category:coursecount',
 101          ];
 102      }
 103  
 104      /**
 105       * Return the default sorting that will be added to the report as part of default setup
 106       *
 107       * @return int[]
 108       */
 109      public function get_default_column_sorting(): array {
 110          return [
 111              'course_category:name' => SORT_ASC,
 112          ];
 113      }
 114  
 115      /**
 116       * Return the filters that will be added to the report as part of default setup
 117       *
 118       * @return string[]
 119       */
 120      public function get_default_filters(): array {
 121          return [
 122              'course_category:name',
 123          ];
 124      }
 125  
 126      /**
 127       * Return the conditions that will be added to the report as part of default setup
 128       *
 129       * @return string[]
 130       */
 131      public function get_default_conditions(): array {
 132          return [];
 133      }
 134  }