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]

   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\reportbuilder\datasource;
  20  
  21  use core_cohort\reportbuilder\local\entities\{cohort, cohort_member};
  22  use core_reportbuilder\datasource;
  23  use core_reportbuilder\local\entities\user;
  24  
  25  /**
  26   * Cohorts datasource
  27   *
  28   * @package     core_cohort
  29   * @copyright   2021 Paul Holden <paulh@moodle.com>
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class cohorts extends datasource {
  33  
  34      /**
  35       * Return user friendly name of the datasource
  36       *
  37       * @return string
  38       */
  39      public static function get_name(): string {
  40          return get_string('cohorts', 'core_cohort');
  41      }
  42  
  43      /**
  44       * Initialise report
  45       */
  46      protected function initialise(): void {
  47          $cohortentity = new cohort();
  48          $cohorttablealias = $cohortentity->get_table_alias('cohort');
  49  
  50          $this->set_main_table('cohort', $cohorttablealias);
  51  
  52          $this->add_entity($cohortentity);
  53  
  54          // Join the cohort member entity to the cohort entity.
  55          $cohortmemberentity = new cohort_member();
  56          $cohortmembertablealias = $cohortmemberentity->get_table_alias('cohort_members');
  57  
  58          $this->add_entity($cohortmemberentity
  59              ->add_join("LEFT JOIN {cohort_members} {$cohortmembertablealias}
  60                  ON {$cohortmembertablealias}.cohortid = {$cohorttablealias}.id")
  61          );
  62  
  63          // Join the user entity to the cohort member entity.
  64          $userentity = new user();
  65          $usertablealias = $userentity->get_table_alias('user');
  66  
  67          $this->add_entity($userentity
  68              ->add_joins($cohortmemberentity->get_joins())
  69              ->add_join("LEFT JOIN {user} {$usertablealias}
  70                  ON {$usertablealias}.id = {$cohortmembertablealias}.userid")
  71          );
  72  
  73          // Add all columns/filters/conditions from entities to be available in custom reports.
  74          $this->add_all_from_entities();
  75      }
  76  
  77      /**
  78       * Return the columns that will be added to the report as part of default setup
  79       *
  80       * @return string[]
  81       */
  82      public function get_default_columns(): array {
  83          return [
  84              'cohort:name',
  85              'cohort:context',
  86              'cohort:idnumber',
  87              'cohort:description',
  88          ];
  89      }
  90  
  91      /**
  92       * Return the filters that will be added to the report once is created
  93       *
  94       * @return string[]
  95       */
  96      public function get_default_filters(): array {
  97          return ['cohort:context', 'cohort:name'];
  98      }
  99  
 100      /**
 101       * Return the conditions that will be added to the report once is created
 102       *
 103       * @return string[]
 104       */
 105      public function get_default_conditions(): array {
 106          return [];
 107      }
 108  
 109      /**
 110       * Return the default sorting that will be added to the report once it is created
 111       *
 112       * @return array|int[]
 113       */
 114      public function get_default_column_sorting(): array {
 115          return [
 116              'cohort:name' => SORT_ASC,
 117          ];
 118      }
 119  }