| 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_user\reportbuilder\datasource;
 | 
        
           |  |  | 20 |   | 
        
           |  |  | 21 | use lang_string;
 | 
        
           |  |  | 22 | use core_cohort\reportbuilder\local\entities\cohort;
 | 
        
           |  |  | 23 | use core_reportbuilder\datasource;
 | 
        
           |  |  | 24 | use core_reportbuilder\local\entities\user;
 | 
        
           |  |  | 25 | use core_reportbuilder\local\filters\boolean_select;
 | 
        
           |  |  | 26 | use core_reportbuilder\local\helpers\database;
 | 
        
           |  |  | 27 | use core_tag\reportbuilder\local\entities\tag;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | /**
 | 
        
           |  |  | 30 |  * Users datasource
 | 
        
           |  |  | 31 |  *
 | 
        
           |  |  | 32 |  * @package   core_user
 | 
        
           |  |  | 33 |  * @copyright 2021 David Matamoros <davidmc@moodle.com>
 | 
        
           |  |  | 34 |  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 35 |  */
 | 
        
           |  |  | 36 | class users 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('users');
 | 
        
           |  |  | 45 |     }
 | 
        
           |  |  | 46 |   | 
        
           |  |  | 47 |     /**
 | 
        
           |  |  | 48 |      * Initialise report
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     protected function initialise(): void {
 | 
        
           |  |  | 51 |         global $CFG;
 | 
        
           |  |  | 52 |   | 
        
           |  |  | 53 |         $userentity = new user();
 | 
        
           |  |  | 54 |         $useralias = $userentity->get_table_alias('user');
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         $this->set_main_table('user', $useralias);
 | 
        
           |  |  | 57 |         $this->add_entity($userentity);
 | 
        
           |  |  | 58 |   | 
        
           |  |  | 59 |         $userparamguest = database::generate_param_name();
 | 
        
           |  |  | 60 |         $this->add_base_condition_sql("{$useralias}.id != :{$userparamguest} AND {$useralias}.deleted = 0", [
 | 
        
           |  |  | 61 |             $userparamguest => $CFG->siteguest,
 | 
        
           |  |  | 62 |         ]);
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |         // Join the tag entity.
 | 
        
           |  |  | 65 |         $tagentity = (new tag())
 | 
        
           |  |  | 66 |             ->set_table_alias('tag', $userentity->get_table_alias('tag'))
 | 
        
           |  |  | 67 |             ->set_entity_title(new lang_string('interests'));
 | 
        
           |  |  | 68 |         $this->add_entity($tagentity
 | 
        
           |  |  | 69 |             ->add_joins($userentity->get_tag_joins()));
 | 
        
           |  |  | 70 |   | 
        
           |  |  | 71 |         // Join the cohort entity.
 | 
        
           |  |  | 72 |         $cohortentity = new cohort();
 | 
        
           |  |  | 73 |         $cohortalias = $cohortentity->get_table_alias('cohort');
 | 
        
           |  |  | 74 |         $cohortmemberalias = database::generate_alias();
 | 
        
           |  |  | 75 |         $this->add_entity($cohortentity->add_joins([
 | 
        
           |  |  | 76 |             "LEFT JOIN {cohort_members} {$cohortmemberalias} ON {$cohortmemberalias}.userid = {$useralias}.id",
 | 
        
           |  |  | 77 |             "LEFT JOIN {cohort} {$cohortalias} ON {$cohortalias}.id = {$cohortmemberalias}.cohortid",
 | 
        
           |  |  | 78 |         ]));
 | 
        
           |  |  | 79 |   | 
        
           |  |  | 80 |         // Add all columns/filters/conditions from entities to be available in custom reports.
 | 
        
           |  |  | 81 |         $this->add_all_from_entity($userentity->get_entity_name());
 | 
        
           |  |  | 82 |         $this->add_all_from_entity($tagentity->get_entity_name(), ['name', 'namewithlink'], ['name'], ['name']);
 | 
        
           |  |  | 83 |         $this->add_all_from_entity($cohortentity->get_entity_name(), ['name', 'idnumber', 'description', 'customfield*'],
 | 
        
           |  |  | 84 |             ['cohortselect', 'name', 'idnumber', 'customfield*'], ['cohortselect', 'name', 'idnumber', 'customfield*']);
 | 
        
           |  |  | 85 |     }
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |     /**
 | 
        
           |  |  | 88 |      * Return the columns that will be added to the report once is created
 | 
        
           |  |  | 89 |      *
 | 
        
           |  |  | 90 |      * @return string[]
 | 
        
           |  |  | 91 |      */
 | 
        
           |  |  | 92 |     public function get_default_columns(): array {
 | 
        
           |  |  | 93 |         return ['user:fullname', 'user:username', 'user:email'];
 | 
        
           |  |  | 94 |     }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |     /**
 | 
        
           |  |  | 97 |      * Return the filters that will be added to the report once is created
 | 
        
           |  |  | 98 |      *
 | 
        
           |  |  | 99 |      * @return string[]
 | 
        
           |  |  | 100 |      */
 | 
        
           |  |  | 101 |     public function get_default_filters(): array {
 | 
        
           |  |  | 102 |         return ['user:fullname', 'user:username', 'user:email'];
 | 
        
           |  |  | 103 |     }
 | 
        
           |  |  | 104 |   | 
        
           |  |  | 105 |     /**
 | 
        
           |  |  | 106 |      * Return the conditions that will be added to the report once is created
 | 
        
           |  |  | 107 |      *
 | 
        
           |  |  | 108 |      * @return string[]
 | 
        
           |  |  | 109 |      */
 | 
        
           |  |  | 110 |     public function get_default_conditions(): array {
 | 
        
           |  |  | 111 |         return [
 | 
        
           |  |  | 112 |             'user:fullname',
 | 
        
           |  |  | 113 |             'user:username',
 | 
        
           |  |  | 114 |             'user:email',
 | 
        
           |  |  | 115 |             'user:suspended',
 | 
        
           |  |  | 116 |         ];
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /**
 | 
        
           |  |  | 120 |      * Return the conditions values that will be added to the report once is created
 | 
        
           |  |  | 121 |      *
 | 
        
           |  |  | 122 |      * @return array
 | 
        
           |  |  | 123 |      */
 | 
        
           |  |  | 124 |     public function get_default_condition_values(): array {
 | 
        
           |  |  | 125 |         return [
 | 
        
           |  |  | 126 |             'user:suspended_operator' => boolean_select::NOT_CHECKED,
 | 
        
           |  |  | 127 |         ];
 | 
        
           |  |  | 128 |     }
 | 
        
           |  |  | 129 |   | 
        
           |  |  | 130 |     /**
 | 
        
           |  |  | 131 |      * Return the default sorting that will be added to the report once it is created
 | 
        
           |  |  | 132 |      *
 | 
        
           |  |  | 133 |      * @return int[]
 | 
        
           |  |  | 134 |      */
 | 
        
           |  |  | 135 |     public function get_default_column_sorting(): array {
 | 
        
           |  |  | 136 |         return [
 | 
        
           |  |  | 137 |             'user:fullname' => SORT_ASC,
 | 
        
           |  |  | 138 |         ];
 | 
        
           |  |  | 139 |     }
 | 
        
           |  |  | 140 | }
 |