Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 400 and 402] [Versions 401 and 402]

   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_files\reportbuilder\local\entities\file;
  23  use core_reportbuilder\datasource;
  24  use core_reportbuilder\local\entities\course;
  25  use core_reportbuilder\local\helpers\database;
  26  use core_tag\reportbuilder\local\entities\tag;
  27  use lang_string;
  28  
  29  /**
  30   * Courses datasource
  31   *
  32   * @package     core_course
  33   * @copyright   2021 Paul Holden <paulh@moodle.com>
  34   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class courses extends datasource {
  37  
  38      /**
  39       * Return user friendly name of the datasource
  40       *
  41       * @return string
  42       */
  43      public static function get_name(): string {
  44          return get_string('courses');
  45      }
  46  
  47      /**
  48       * Initialise report
  49       */
  50      protected function initialise(): void {
  51          $courseentity = new course();
  52          $coursetablealias = $courseentity->get_table_alias('course');
  53  
  54          // Exclude site course.
  55          $paramsiteid = database::generate_param_name();
  56  
  57          $this->set_main_table('course', $coursetablealias);
  58          $this->add_base_condition_sql("{$coursetablealias}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
  59  
  60          $this->add_entity($courseentity);
  61  
  62          // Join the course category entity.
  63          $coursecatentity = new course_category();
  64          $coursecattablealias = $coursecatentity->get_table_alias('course_categories');
  65          $this->add_entity($coursecatentity
  66              ->add_join("JOIN {course_categories} {$coursecattablealias}
  67                  ON {$coursecattablealias}.id = {$coursetablealias}.category"));
  68  
  69          // Join the tag entity.
  70          $tagentity = (new tag())
  71              ->set_table_alias('tag', $courseentity->get_table_alias('tag'));
  72          $this->add_entity($tagentity
  73              ->add_joins($courseentity->get_tag_joins()));
  74  
  75          // Join the files entity.
  76          $contextalias = $courseentity->get_table_alias('context');
  77          $fileentity = (new file())
  78              ->set_entity_title(new lang_string('courseoverviewfiles'));
  79          $filesalias = $fileentity->get_table_alias('files');
  80          $this->add_entity($fileentity
  81              ->add_join($courseentity->get_context_join())
  82              ->add_join("LEFT JOIN {files} {$filesalias}
  83                  ON {$filesalias}.contextid = {$contextalias}.id
  84                 AND {$filesalias}.component = 'course'
  85                 AND {$filesalias}.filearea = 'overviewfiles'
  86                 AND {$filesalias}.itemid = 0
  87                 AND {$filesalias}.filename != '.'"));
  88  
  89          // Add all columns/filters/conditions from entities to be available in custom reports.
  90          $this->add_all_from_entity($coursecatentity->get_entity_name());
  91          $this->add_all_from_entity($courseentity->get_entity_name());
  92  
  93          // Add specific tag entity elements.
  94          $this->add_columns_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink']);
  95          $this->add_filter($tagentity->get_filter('name'));
  96          $this->add_condition($tagentity->get_condition('name'));
  97  
  98          // Add specific file entity elements.
  99          $this->add_columns_from_entity($fileentity->get_entity_name(), ['name', 'size', 'type', 'timecreated']);
 100          $this->add_filters_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 101          $this->add_conditions_from_entity($fileentity->get_entity_name(), ['name', 'size', 'timecreated']);
 102  
 103      }
 104  
 105      /**
 106       * Return the columns that will be added to the report as part of default setup
 107       *
 108       * @return string[]
 109       */
 110      public function get_default_columns(): array {
 111          return [
 112              'course_category:name',
 113              'course:shortname',
 114              'course:fullname',
 115              'course:idnumber',
 116          ];
 117      }
 118  
 119      /**
 120       * Return the filters that will be added to the report once is created
 121       *
 122       * @return string[]
 123       */
 124      public function get_default_filters(): array {
 125          return ['course_category:name', 'course:fullname', 'course:idnumber'];
 126      }
 127  
 128      /**
 129       * Return the conditions that will be added to the report once is created
 130       *
 131       * @return string[]
 132       */
 133      public function get_default_conditions(): array {
 134          return ['course_category:name'];
 135      }
 136  
 137      /**
 138       * Return the default sorting that will be added to the report once it is created
 139       *
 140       * @return array|int[]
 141       */
 142      public function get_default_column_sorting(): array {
 143          return [
 144              'course_category:name' => SORT_ASC,
 145              'course:shortname' => SORT_ASC,
 146              'course:fullname' => SORT_ASC,
 147          ];
 148      }
 149  }