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 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_course\reportbuilder\local\entities\access;
  23  use core_course\reportbuilder\local\entities\completion;
  24  use core_course\reportbuilder\local\entities\enrolment;
  25  use core_group\reportbuilder\local\entities\group;
  26  use core_reportbuilder\datasource;
  27  use core_reportbuilder\local\entities\course;
  28  use core_reportbuilder\local\entities\user;
  29  use core_reportbuilder\local\helpers\database;
  30  
  31  /**
  32   * Course participants datasource
  33   *
  34   * @package     core_course
  35   * @copyright   2022 David Matamoros <davidmc@moodle.com>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class participants extends datasource {
  39  
  40      /**
  41       * Initialise report
  42       */
  43      protected function initialise(): void {
  44          $courseentity = new course();
  45          $course = $courseentity->get_table_alias('course');
  46          $this->add_entity($courseentity);
  47  
  48          $this->set_main_table('course', $course);
  49  
  50          // Exclude site course.
  51          $paramsiteid = database::generate_param_name();
  52          $this->add_base_condition_sql("{$course}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
  53  
  54          // Join the course category entity.
  55          $coursecatentity = new course_category();
  56          $categories = $coursecatentity->get_table_alias('course_categories');
  57          $this->add_entity($coursecatentity
  58              ->add_join("JOIN {course_categories} {$categories} ON {$categories}.id = {$course}.category"));
  59  
  60          // Join the enrolments entity.
  61          $enrolmententity = new enrolment();
  62          $userenrolment = $enrolmententity->get_table_alias('user_enrolments');
  63          $enrol = $enrolmententity->get_table_alias('enrol');
  64          $enroljoin = "LEFT JOIN {enrol} {$enrol} ON {$enrol}.courseid = {$course}.id";
  65          $userenrolmentjoin = " LEFT JOIN {user_enrolments} {$userenrolment} ON {$userenrolment}.enrolid = {$enrol}.id";
  66          $enrolmententity->add_joins([$enroljoin, $userenrolmentjoin]);
  67          $this->add_entity($enrolmententity);
  68  
  69          // Join user entity.
  70          $userentity = new user();
  71          $user = $userentity->get_table_alias('user');
  72          $userentity->add_joins($enrolmententity->get_joins());
  73          $userentity->add_join("LEFT JOIN {user} {$user} ON {$userenrolment}.userid = {$user}.id AND {$user}.deleted = 0");
  74          $this->add_entity($userentity);
  75  
  76          // Join group entity.
  77          $groupentity = (new group())
  78              ->set_table_alias('context', $courseentity->get_table_alias('context'));
  79          $groups = $groupentity->get_table_alias('groups');
  80  
  81          // Sub-select for all course group members.
  82          $groupsinnerselect = "
  83              SELECT grs.*, grms.userid
  84                FROM {groups} grs
  85                JOIN {groups_members} grms ON grms.groupid = grs.id";
  86  
  87          $this->add_entity($groupentity
  88              ->add_join($courseentity->get_context_join())
  89              ->add_joins($userentity->get_joins())
  90              ->add_join("
  91                  LEFT JOIN ({$groupsinnerselect}) {$groups}
  92                         ON {$groups}.courseid = {$course}.id
  93                        AND {$groups}.userid = {$user}.id")
  94          );
  95  
  96          // Join completion entity.
  97          $completionentity = new completion();
  98          $completion = $completionentity->get_table_alias('course_completion');
  99          $completionentity->add_joins($userentity->get_joins());
 100          $completionentity->add_join("
 101              LEFT JOIN {course_completions} {$completion}
 102                     ON {$completion}.course = {$course}.id AND {$completion}.userid = {$user}.id
 103          ");
 104          $completionentity->set_table_alias('user', $user);
 105          $this->add_entity($completionentity);
 106  
 107          // Join course access entity.
 108          $accessentity = new access();
 109          $lastaccess = $accessentity->get_table_alias('user_lastaccess');
 110          $accessentity->add_joins($userentity->get_joins());
 111          $accessentity->add_join("
 112              LEFT JOIN {user_lastaccess} {$lastaccess}
 113                     ON {$lastaccess}.userid = {$user}.id AND {$lastaccess}.courseid = {$course}.id
 114          ");
 115          $this->add_entity($accessentity);
 116  
 117          // Add all entities columns/filters/conditions.
 118          $this->add_all_from_entities();
 119      }
 120  
 121      /**
 122       * Return user friendly name of the datasource
 123       *
 124       * @return string
 125       */
 126      public static function get_name(): string {
 127          return get_string('courseparticipants', 'course');
 128      }
 129  
 130      /**
 131       * Return the columns that will be added to the report as part of default setup
 132       *
 133       * @return string[]
 134       */
 135      public function get_default_columns(): array {
 136          return [
 137              'course:coursefullnamewithlink',
 138              'enrolment:method',
 139              'user:fullnamewithlink',
 140          ];
 141      }
 142  
 143      /**
 144       * Return the filters that will be added to the report once is created
 145       *
 146       * @return string[]
 147       */
 148      public function get_default_filters(): array {
 149          return [
 150              'user:suspended',
 151              'user:confirmed',
 152          ];
 153      }
 154  
 155      /**
 156       * Return the conditions that will be added to the report once is created
 157       *
 158       * @return string[]
 159       */
 160      public function get_default_conditions(): array {
 161          return [
 162              'user:suspended',
 163              'user:confirmed',
 164          ];
 165      }
 166  }