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.

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