| 1 | 
           efrain | 
           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_group\reportbuilder\datasource;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use core_group\reportbuilder\local\entities\{grouping, group, group_member};
  | 
        
        
            | 
            | 
           22 | 
           use core_reportbuilder\datasource;
  | 
        
        
            | 
            | 
           23 | 
           use core_reportbuilder\local\entities\{course, user};
  | 
        
        
            | 
            | 
           24 | 
           use core_reportbuilder\local\helpers\database;
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           /**
  | 
        
        
            | 
            | 
           27 | 
            * Groups datasource
  | 
        
        
            | 
            | 
           28 | 
            *
  | 
        
        
            | 
            | 
           29 | 
            * @package     core_group
  | 
        
        
            | 
            | 
           30 | 
            * @copyright   2022 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           31 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
            | 
            | 
           33 | 
           class groups 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('groups', 'core_group');
  | 
        
        
            | 
            | 
           42 | 
               }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               /**
  | 
        
        
            | 
            | 
           45 | 
                * Initialise report
  | 
        
        
            | 
            | 
           46 | 
                */
  | 
        
        
            | 
            | 
           47 | 
               protected function initialise(): void {
  | 
        
        
            | 
            | 
           48 | 
                   $courseentity = new course();
  | 
        
        
            | 
            | 
           49 | 
                   $coursealias = $courseentity->get_table_alias('course');
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   $this->set_main_table('course', $coursealias);
  | 
        
        
            | 
            | 
           52 | 
                   $this->add_entity($courseentity);
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
                   $paramsiteid = database::generate_param_name();
  | 
        
        
            | 
            | 
           55 | 
                   $this->add_base_condition_sql("{$coursealias}.id != :{$paramsiteid}", [$paramsiteid => SITEID]);
  | 
        
        
            | 
            | 
           56 | 
              | 
        
        
            | 
            | 
           57 | 
                   // Re-use the context table alias/join from the course entity in subsequent entities.
  | 
        
        
            | 
            | 
           58 | 
                   $contextalias = $courseentity->get_table_alias('context');
  | 
        
        
            | 
            | 
           59 | 
                   $this->add_join($courseentity->get_context_join());
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   // Group entity.
  | 
        
        
            | 
            | 
           62 | 
                   $groupentity = (new group())
  | 
        
        
            | 
            | 
           63 | 
                       ->set_table_alias('context', $contextalias);
  | 
        
        
            | 
            | 
           64 | 
                   $groupsalias = $groupentity->get_table_alias('groups');
  | 
        
        
            | 
            | 
           65 | 
                   $this->add_entity($groupentity
  | 
        
        
            | 
            | 
           66 | 
                       ->add_join("LEFT JOIN {groups} {$groupsalias} ON {$groupsalias}.courseid = {$coursealias}.id"));
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
                   // Grouping entity.
  | 
        
        
            | 
            | 
           69 | 
                   $groupingentity = (new grouping())
  | 
        
        
            | 
            | 
           70 | 
                       ->set_table_alias('context', $contextalias);
  | 
        
        
            | 
            | 
           71 | 
                   $groupingsalias = $groupingentity->get_table_alias('groupings');
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
                   // Sub-select for all groupings groups.
  | 
        
        
            | 
            | 
           74 | 
                   $groupinginnerselect = "
  | 
        
        
            | 
            | 
           75 | 
                       SELECT gr.*, grg.groupid
  | 
        
        
            | 
            | 
           76 | 
                         FROM {groupings} gr
  | 
        
        
            | 
            | 
           77 | 
                         JOIN {groupings_groups} grg ON grg.groupingid = gr.id";
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
                   $this->add_entity($groupingentity
  | 
        
        
            | 
            | 
           80 | 
                       ->add_joins($groupentity->get_joins())
  | 
        
        
            | 
            | 
           81 | 
                       ->add_join("LEFT JOIN ({$groupinginnerselect}) {$groupingsalias}
  | 
        
        
            | 
            | 
           82 | 
                           ON {$groupingsalias}.courseid = {$coursealias}.id AND {$groupingsalias}.groupid = {$groupsalias}.id"));
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   // Group member entity.
  | 
        
        
            | 
            | 
           85 | 
                   $groupmemberentity = new group_member();
  | 
        
        
            | 
            | 
           86 | 
                   $groupsmembersalias = $groupmemberentity->get_table_alias('groups_members');
  | 
        
        
            | 
            | 
           87 | 
                   $this->add_entity($groupmemberentity
  | 
        
        
            | 
            | 
           88 | 
                       ->add_joins($groupentity->get_joins())
  | 
        
        
            | 
            | 
           89 | 
                       ->add_join("LEFT JOIN {groups_members} {$groupsmembersalias} ON {$groupsmembersalias}.groupid = {$groupsalias}.id"));
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   // User entity.
  | 
        
        
            | 
            | 
           92 | 
                   $userentity = new user();
  | 
        
        
            | 
            | 
           93 | 
                   $useralias = $userentity->get_table_alias('user');
  | 
        
        
            | 
            | 
           94 | 
                   $this->add_entity($userentity
  | 
        
        
            | 
            | 
           95 | 
                       ->add_joins($groupmemberentity->get_joins())
  | 
        
        
            | 
            | 
           96 | 
                       ->add_join("LEFT JOIN {user} {$useralias} ON {$useralias}.id = {$groupsmembersalias}.userid"));
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
                   // Add all elements from entities to be available in custom reports.
  | 
        
        
            | 
            | 
           99 | 
                   $this->add_all_from_entities();
  | 
        
        
            | 
            | 
           100 | 
               }
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
               /**
  | 
        
        
            | 
            | 
           103 | 
                * Return the columns that will be added to the report as part of default setup
  | 
        
        
            | 
            | 
           104 | 
                *
  | 
        
        
            | 
            | 
           105 | 
                * @return string[]
  | 
        
        
            | 
            | 
           106 | 
                */
  | 
        
        
            | 
            | 
           107 | 
               public function get_default_columns(): array {
  | 
        
        
            | 
            | 
           108 | 
                   return [
  | 
        
        
            | 
            | 
           109 | 
                       'course:coursefullnamewithlink',
  | 
        
        
            | 
            | 
           110 | 
                       'group:name',
  | 
        
        
            | 
            | 
           111 | 
                       'user:fullname',
  | 
        
        
            | 
            | 
           112 | 
                   ];
  | 
        
        
            | 
            | 
           113 | 
               }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
               /**
  | 
        
        
            | 
            | 
           116 | 
                * Return the column sorting that will be added to the report upon creation
  | 
        
        
            | 
            | 
           117 | 
                *
  | 
        
        
            | 
            | 
           118 | 
                * @return int[]
  | 
        
        
            | 
            | 
           119 | 
                */
  | 
        
        
            | 
            | 
           120 | 
               public function get_default_column_sorting(): array {
  | 
        
        
            | 
            | 
           121 | 
                   return [
  | 
        
        
            | 
            | 
           122 | 
                       'course:coursefullnamewithlink' => SORT_ASC,
  | 
        
        
            | 
            | 
           123 | 
                       'group:name' => SORT_ASC,
  | 
        
        
            | 
            | 
           124 | 
                       'user:fullname' => SORT_ASC,
  | 
        
        
            | 
            | 
           125 | 
                   ];
  | 
        
        
            | 
            | 
           126 | 
               }
  | 
        
        
            | 
            | 
           127 | 
              | 
        
        
            | 
            | 
           128 | 
               /**
  | 
        
        
            | 
            | 
           129 | 
                * Return the filters that will be added to the report as part of default setup
  | 
        
        
            | 
            | 
           130 | 
                *
  | 
        
        
            | 
            | 
           131 | 
                * @return string[]
  | 
        
        
            | 
            | 
           132 | 
                */
  | 
        
        
            | 
            | 
           133 | 
               public function get_default_filters(): array {
  | 
        
        
            | 
            | 
           134 | 
                   return [
  | 
        
        
            | 
            | 
           135 | 
                       'course:fullname',
  | 
        
        
            | 
            | 
           136 | 
                       'group:name',
  | 
        
        
            | 
            | 
           137 | 
                   ];
  | 
        
        
            | 
            | 
           138 | 
               }
  | 
        
        
            | 
            | 
           139 | 
              | 
        
        
            | 
            | 
           140 | 
               /**
  | 
        
        
            | 
            | 
           141 | 
                * Return the conditions that will be added to the report as part of default setup
  | 
        
        
            | 
            | 
           142 | 
                *
  | 
        
        
            | 
            | 
           143 | 
                * @return string[]
  | 
        
        
            | 
            | 
           144 | 
                */
  | 
        
        
            | 
            | 
           145 | 
               public function get_default_conditions(): array {
  | 
        
        
            | 
            | 
           146 | 
                   return [
  | 
        
        
            | 
            | 
           147 | 
                       'course:fullname',
  | 
        
        
            | 
            | 
           148 | 
                       'group:name',
  | 
        
        
            | 
            | 
           149 | 
                   ];
  | 
        
        
            | 
            | 
           150 | 
               }
  | 
        
        
            | 
            | 
           151 | 
           }
  |