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 400 and 401] [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_course\reportbuilder\datasource;
  20  
  21  use core_course\reportbuilder\local\entities\course_category;
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\course;
  24  use core_reportbuilder\local\helpers\database;
  25  use core_tag\reportbuilder\local\entities\tag;
  26  
  27  /**
  28   * Courses datasource
  29   *
  30   * @package     core_course
  31   * @copyright   2021 Paul Holden <paulh@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class courses 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('courses');
  43      }
  44  
  45      /**
  46       * Initialise report
  47       */
  48      protected function initialise(): void {
  49          $courseentity = new course();
  50          $coursetablealias = $courseentity->get_table_alias('course');
  51  
  52          // Exclude site course.
  53          $paramsiteid = database::generate_param_name();
  54  
  55          $this->set_main_table('course', $coursetablealias);
  56          $this->add_base_condition_sql("{$coursetablealias}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
  57  
  58          $this->add_entity($courseentity);
  59  
  60          // Join the course category entity.
  61          $coursecatentity = new course_category();
  62          $coursecattablealias = $coursecatentity->get_table_alias('course_categories');
  63          $this->add_entity($coursecatentity
  64              ->add_join("JOIN {course_categories} {$coursecattablealias}
  65                  ON {$coursecattablealias}.id = {$coursetablealias}.category"));
  66  
  67          // Join the tag entity.
  68          $tagentity = (new tag())
  69              ->set_table_alias('tag', $courseentity->get_table_alias('tag'));
  70          $this->add_entity($tagentity
  71              ->add_joins($courseentity->get_tag_joins()));
  72  
  73          // Add all columns/filters/conditions from entities to be available in custom reports.
  74          $this->add_all_from_entity($coursecatentity->get_entity_name());
  75          $this->add_all_from_entity($courseentity->get_entity_name());
  76  
  77          // Add specific tag entity elements.
  78          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
  79          $this->add_filter($tagentity->get_filter('name'));
  80          $this->add_condition($tagentity->get_condition('name'));
  81      }
  82  
  83      /**
  84       * Return the columns that will be added to the report as part of default setup
  85       *
  86       * @return string[]
  87       */
  88      public function get_default_columns(): array {
  89          return [
  90              'course_category:name',
  91              'course:shortname',
  92              'course:fullname',
  93              'course:idnumber',
  94          ];
  95      }
  96  
  97      /**
  98       * Return the filters that will be added to the report once is created
  99       *
 100       * @return string[]
 101       */
 102      public function get_default_filters(): array {
 103          return ['course_category:name', 'course:fullname', 'course:idnumber'];
 104      }
 105  
 106      /**
 107       * Return the conditions that will be added to the report once is created
 108       *
 109       * @return string[]
 110       */
 111      public function get_default_conditions(): array {
 112          return ['course_category:name'];
 113      }
 114  
 115      /**
 116       * Return the default sorting that will be added to the report once it is created
 117       *
 118       * @return array|int[]
 119       */
 120      public function get_default_column_sorting(): array {
 121          return [
 122              'course_category:name' => SORT_ASC,
 123              'course:shortname' => SORT_ASC,
 124              'course:fullname' => SORT_ASC,
 125          ];
 126      }
 127  }