Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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_cohort\reportbuilder\datasource;
20
 
21
use core_cohort\reportbuilder\local\entities\{cohort, cohort_member};
22
use core_reportbuilder\local\filters\boolean_select;
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
        $this->add_entity($cohortmemberentity
60
            ->add_join("LEFT JOIN {cohort_members} {$cohortmembertablealias}
61
                ON {$cohortmembertablealias}.cohortid = {$cohorttablealias}.id")
62
        );
63
 
64
        // Join the user entity to the cohort member entity.
65
        $userentity = new user();
66
        $usertablealias = $userentity->get_table_alias('user');
67
 
68
        $this->add_entity($userentity
69
            ->add_joins($cohortmemberentity->get_joins())
70
            ->add_join("LEFT JOIN {user} {$usertablealias}
71
                ON {$usertablealias}.id = {$cohortmembertablealias}.userid")
72
        );
73
 
74
        // Add all columns/filters/conditions from entities to be available in custom reports.
75
        $this->add_all_from_entities();
76
    }
77
 
78
    /**
79
     * Return the columns that will be added to the report as part of default setup
80
     *
81
     * @return string[]
82
     */
83
    public function get_default_columns(): array {
84
        return [
85
            'cohort:name',
86
            'cohort:context',
87
            'cohort:idnumber',
88
            'cohort:description',
89
        ];
90
    }
91
 
92
    /**
93
     * Return the filters that will be added to the report once is created
94
     *
95
     * @return string[]
96
     */
97
    public function get_default_filters(): array {
98
        return ['cohort:context', 'cohort:name'];
99
    }
100
 
101
    /**
102
     * Return the conditions that will be added to the report once is created
103
     *
104
     * @return string[]
105
     */
106
    public function get_default_conditions(): array {
107
        return [
108
            'cohort:visible',
109
        ];
110
    }
111
 
112
    /**
113
     * Return the condition values that will be set for the report upon creation
114
     *
115
     * @return array
116
     */
117
    public function get_default_condition_values(): array {
118
        return [
119
            'cohort:visible_operator' => boolean_select::CHECKED,
120
        ];
121
    }
122
 
123
    /**
124
     * Return the default sorting that will be added to the report once it is created
125
     *
126
     * @return array|int[]
127
     */
128
    public function get_default_column_sorting(): array {
129
        return [
130
            'cohort:name' => SORT_ASC,
131
        ];
132
    }
133
}